示例#1
0
ofxFBXNode * ofxFBXScene::parseCameraInfo(FbxNode* pNode, FbxAnimLayer * pAnimLayer){
    FbxCamera* lCamera = pNode->GetCamera();

	ofxFBXCamera camera;

	camera.nodeName = lCamera->GetName();

	parsePositionCurve(camera,pAnimLayer,pNode->LclTranslation);

	parseScaleCurve(camera,pAnimLayer,pNode->LclScaling);
	//camera.originalScale.set(1,1,1);

	parseRotationCurve(camera,pAnimLayer,pNode,pNode->LclRotation);

	camera.setPosition(camera.originalPosition);

	if(pNode->GetTarget()){
		camera.target = ofxFBXNodePosition(pNode->GetTarget());
		//TODO: process lookAt animation
	}else{
		camera.target = toOf(lCamera->InterestPosition.Get());
	}


	float lNearPlane = lCamera->GetNearPlane();
	float lFarPlane = lCamera->GetFarPlane();

	//Get global scaling.
	FbxVector4 lCameraScaling = pNode->Scaling.Get();//GetGlobalPosition(pNode, 0).GetS();
	static const int  FORWARD_SCALE = 2;

	//scaling near plane and far plane
	//lNearPlane *= lCameraScaling[FORWARD_SCALE];
	//lFarPlane *= lCameraScaling[FORWARD_SCALE];


	FbxCamera::EAspectRatioMode lCamAspectRatioMode = lCamera->GetAspectRatioMode();
	double lAspectX = lCamera->AspectWidth.Get();
	double lAspectY = lCamera->AspectHeight.Get();
	double lAspectRatio = 1.333333;
	switch( lCamAspectRatioMode)
	{
	case FbxCamera::eWindowSize:
		lAspectRatio = lAspectX / lAspectY;
		break;
	case FbxCamera::eFixedRatio:
		lAspectRatio = lAspectX;

		break;
	case FbxCamera::eFixedResolution:
		lAspectRatio = lAspectX / lAspectY * lCamera->GetPixelRatio();
		break;
	case FbxCamera::eFixedWidth:
		lAspectRatio = lCamera->GetPixelRatio() / lAspectY;
		break;
	case FbxCamera::eFixedHeight:
		lAspectRatio = lCamera->GetPixelRatio() * lAspectX;
		break;
	default:
		break;

	}

	//get the aperture ratio
	double lFilmHeight = lCamera->GetApertureHeight();
	double lFilmWidth = lCamera->GetApertureWidth() * lCamera->GetSqueezeRatio();
	//here we use Height : Width
	double lApertureRatio = lFilmHeight / lFilmWidth;


	//change the aspect ratio to Height : Width
	lAspectRatio = 1 / lAspectRatio;
	//revise the aspect ratio and aperture ratio
	FbxCamera::EGateFit lCameraGateFit = lCamera->GateFit.Get();
	switch( lCameraGateFit )
	{

	case FbxCamera::eFitFill:
		if( lApertureRatio > lAspectRatio)  // the same as eHORIZONTAL_FIT
		{
			lFilmHeight = lFilmWidth * lAspectRatio;
			lCamera->SetApertureHeight( lFilmHeight);
			lApertureRatio = lFilmHeight / lFilmWidth;
		}
		else if( lApertureRatio < lAspectRatio) //the same as eVERTICAL_FIT
		{
			lFilmWidth = lFilmHeight / lAspectRatio;
			lCamera->SetApertureWidth( lFilmWidth);
			lApertureRatio = lFilmHeight / lFilmWidth;
		}
		break;
	case FbxCamera::eFitVertical:
		lFilmWidth = lFilmHeight / lAspectRatio;
		lCamera->SetApertureWidth( lFilmWidth);
		lApertureRatio = lFilmHeight / lFilmWidth;
		break;
	case FbxCamera::eFitHorizontal:
		lFilmHeight = lFilmWidth * lAspectRatio;
		lCamera->SetApertureHeight( lFilmHeight);
		lApertureRatio = lFilmHeight / lFilmWidth;
		break;
	case FbxCamera::eFitStretch:
		lAspectRatio = lApertureRatio;
		break;
	case FbxCamera::eFitOverscan:
		if( lFilmWidth > lFilmHeight)
		{
			lFilmHeight = lFilmWidth * lAspectRatio;
		}
		else
		{
			lFilmWidth = lFilmHeight / lAspectRatio;
		}
		lApertureRatio = lFilmHeight / lFilmWidth;
		break;
	case FbxCamera::eFitNone:
	default:
		break;
	}
	//change the aspect ratio to Width : Height
	lAspectRatio = 1 / lAspectRatio;

	double lFieldOfViewX = 0.0;
	double lFieldOfViewY = 0.0;
	if ( lCamera->GetApertureMode() == FbxCamera::eVertical)
	{
		lFieldOfViewY = lCamera->FieldOfView.Get();
		lFieldOfViewX = VFOV2HFOV( lFieldOfViewY, 1 / lApertureRatio);
	}
	else if (lCamera->GetApertureMode() == FbxCamera::eHorizontal)
	{
		lFieldOfViewX = lCamera->FieldOfView.Get(); //get HFOV
		lFieldOfViewY = HFOV2VFOV( lFieldOfViewX, lApertureRatio);
	}
	else if (lCamera->GetApertureMode() == FbxCamera::eFocalLength)
	{
		lFieldOfViewX = lCamera->ComputeFieldOfView(lCamera->FocalLength.Get());    //get HFOV
		lFieldOfViewY = HFOV2VFOV( lFieldOfViewX, lApertureRatio);
	}
	else if (lCamera->GetApertureMode() == FbxCamera::eHorizAndVert) {
		lFieldOfViewX = lCamera->FieldOfViewX.Get();
		lFieldOfViewY = lCamera->FieldOfViewY.Get();
	}


	//revise the Perspective since we have film offset
	double lFilmOffsetX = lCamera->FilmOffsetX.Get();
	double lFilmOffsetY = lCamera->FilmOffsetY.Get();
	lFilmOffsetX = 0 - lFilmOffsetX / lFilmWidth * 2.0;
	lFilmOffsetY = 0 - lFilmOffsetY / lFilmHeight * 2.0;

	camera.setFov(lFieldOfViewY);
	camera.setNearClip(lNearPlane);
	camera.setFarClip(lFarPlane);


	camerasList.push_back(camera);
	return &camerasList.back();


}