// Set the view to the current camera settings. void SetCamera(KFbxScene* pScene, KTime& pTime, KFbxAnimLayer* pAnimLayer, KArrayTemplate<KFbxNode*>& pCameraArray) { KFbxCamera* lCamera = GetCurrentCamera(pScene, pTime, pCameraArray); KFbxNode* lCameraNode = lCamera ? lCamera->GetNode() : NULL; KFbxVector4 lEye(0,0,1); KFbxVector4 lCenter(0,0,0); KFbxVector4 lUp(0,1,0); KFbxVector4 lForward, lRight; if (lCamera) { lEye = lCamera->Position.Get(); lUp = lCamera->UpVector.Get(); } if (lCameraNode && lCameraNode->GetTarget()) { lCenter = GetGlobalPosition(lCameraNode->GetTarget(), pTime).GetT(); } else { if (!lCameraNode || IsProducerCamera(pScene, lCamera)) { if (lCamera) lCenter = lCamera->InterestPosition.Get(); } else { KFbxXMatrix lGlobalRotation; KFbxVector4 lRotationVector(GetGlobalPosition(lCameraNode, pTime).GetR()); lGlobalRotation.SetR(lRotationVector); KFbxVector4 lInterestPosition(lCamera->InterestPosition.Get()); KFbxVector4 lCameraGlobalPosition(GetGlobalPosition(lCameraNode, pTime).GetT()); double lLength = (KFbxVector4(lInterestPosition - lCameraGlobalPosition).Length()); lRotationVector = KFbxVector4(1.0,0,0); lCenter = lGlobalRotation.MultT(lRotationVector); lCenter *= lLength; lCenter += lEye; lRotationVector = KFbxVector4(0,1.0,0); lUp = lGlobalRotation.MultT(lRotationVector); } } lForward = lCenter - lEye; lForward.Normalize(); lRight = lForward.CrossProduct(lUp); lRight.Normalize(); lUp = lRight.CrossProduct(lForward); lUp.Normalize(); double lRadians = 0; if (lCamera) lRadians = 3.1416 * lCamera->Roll.Get() / 180.0; lUp *= cos(lRadians); lRight *= sin(lRadians); lUp = lUp + lRight; double lNearPlane = 0.01; if (lCamera) lNearPlane = lCamera->GetNearPlane(); double lFarPlane = 1000.0; if (lCamera) lFarPlane = lCamera->GetFarPlane(); if (lCamera && lCamera->ProjectionType.Get() == KFbxCamera::ePERSPECTIVE) { double lFieldOfViewY=0.0; double lAspect = lCamera->GetApertureWidth() * lCamera->GetSqueezeRatio() / lCamera->GetApertureHeight(); if (lCamera->GetApertureMode() == KFbxCamera::eHORIZONTAL || lCamera->GetApertureMode() == KFbxCamera::eVERTICAL) { lFieldOfViewY = lCamera->FieldOfView.Get(); if (lCamera->GetApertureMode() == KFbxCamera::eHORIZONTAL) lFieldOfViewY /= lAspect; } else if (lCamera->GetApertureMode() == KFbxCamera::eFOCAL_LENGTH) { lFieldOfViewY = lCamera->ComputeFieldOfView(lCamera->FocalLength.Get()); lFieldOfViewY /= lAspect; } else if (lCamera->GetApertureMode() == KFbxCamera::eHORIZONTAL_AND_VERTICAL) { lFieldOfViewY = lCamera->FieldOfViewY.Get(); } GlSetCameraPerspective(lFieldOfViewY, lAspect, lNearPlane, lFarPlane, lEye, lCenter, lUp); } else { double lPixelRatio = 1.0; if (lCamera) lPixelRatio = lCamera->GetPixelRatio(); int lWidth, lHeight; double lLeftPlane, lRightPlane, lBottomPlane, lTopPlane; GlGetWindowSize(lWidth, lHeight); if(lWidth < lHeight) { lLeftPlane = -gsOrthoCameraScale * lPixelRatio; lRightPlane = gsOrthoCameraScale * lPixelRatio; lBottomPlane = -gsOrthoCameraScale * lHeight / lWidth; lTopPlane = gsOrthoCameraScale * lHeight / lWidth; } else { lWidth *= (int) lPixelRatio; lLeftPlane = -gsOrthoCameraScale * lWidth / lHeight; lRightPlane = gsOrthoCameraScale * lWidth / lHeight; lBottomPlane = -gsOrthoCameraScale; lTopPlane = gsOrthoCameraScale; } GlSetCameraOrthogonal(lLeftPlane, lRightPlane, lBottomPlane, lTopPlane, lNearPlane, lFarPlane, lEye, lCenter, lUp); } }
KFbxXMatrix FilmboxNode::GetWorldTransform( int frame ) const { KFbxXMatrix value; value.SetIdentity(); if( m_node ) { KFbxScene* scene = m_node->GetScene(); KFbxAnimEvaluator* evaluator = scene->GetEvaluator(); KTime frame_time; if( frame == 0xFFFFFFFF ) { frame_time = KTIME_INFINITE; } else { frame_time.SetTime( 0, 0, 0, frame ); } value = evaluator->GetNodeGlobalTransform( m_node, frame_time ); if( m_type == MCE::FBX::SceneObjectType::CAMERA ) { KFbxNode* fbx_target_node = m_node->GetTarget(); if( fbx_target_node ) { // For target cameras we need to manually replace the rotation to point towards the target KFbxVector4 camera_position = value.GetT(); MCE::FBX::FilmboxNode mce_target_node( fbx_target_node ); KFbxXMatrix target_transform = mce_target_node.GetWorldTransform( frame ); KFbxVector4 target_position = target_transform.GetT(); KFbxVector4 up_vector( 0.0, 1.0, 0.0, 0.0 ); KFbxVector4 look_axis = -( target_position - camera_position ); look_axis.Normalize(); KFbxVector4 right_axis = up_vector.CrossProduct( look_axis ); KFbxVector4 up_axis = look_axis.CrossProduct( right_axis ); KFbxMatrix rotation_matrix; rotation_matrix.SetRow( 0, right_axis ); rotation_matrix.SetRow( 1, up_axis ); rotation_matrix.SetRow( 2, look_axis ); rotation_matrix.SetRow( 3, KFbxVector4( 0.0, 0.0, 0.0, 1.0 ) ); KFbxQuaternion camera_rotation; double determinant; rotation_matrix.GetElements( KFbxVector4(), camera_rotation, KFbxVector4(), KFbxVector4(), determinant ); KFbxVector4 camera_scale( 1.0, 1.0, 1.0, 0.0 ); value.SetTQS( camera_position, camera_rotation, camera_scale ); } else { // Even though the SDK docs say that GetNodeGlobalTransform takes all transforms into account // For cameras it appears that you have to manually apply post-rotation KFbxVector4 post_rotation = m_node->GetPostRotation( KFbxNode::eSOURCE_SET ); KFbxXMatrix fbx_to_mce_camera; fbx_to_mce_camera.SetR( post_rotation ); value *= fbx_to_mce_camera; } } } return value; }