// engine-level shader parameters void Context::SetCurrentTransformMatrix( const math::Matrix44& transform ) { sTransform = transform; math::Matrix44 lookAtMatrix, projectionMatrix; content::Ref< Camera > camera = GetCurrentCamera(); camera->GetLookAtMatrix( lookAtMatrix ); camera->GetProjectionMatrix( projectionMatrix ); math::Matrix44 modelViewProjectionMatrix = projectionMatrix * lookAtMatrix * sTransform; // compute object space positions based on current object transform math::Matrix44 transformInverseTranspose = transform; transformInverseTranspose.InvertTranspose(); math::Vector3 lightPosition( 0.0f, 1000.0f, 0.0f ); if ( content::ParameterManager::Contains( "rendering", "debugLightWorldSpacePosition" ) ) { lightPosition = content::ParameterManager::GetParameter< math::Vector3 >( "rendering", "debugLightWorldSpacePosition" ); } math::Vector3 objectSpaceLightPosition = transformInverseTranspose.Transform( lightPosition ); math::Vector3 objectSpaceCameraPosition = transformInverseTranspose.Transform( camera->GetPosition() ); rtgi::SetSharedShaderParameter( "modelViewProjectionMatrix", modelViewProjectionMatrix ); rtgi::SetSharedShaderParameter( "objectSpaceLightPosition", objectSpaceLightPosition ); rtgi::SetSharedShaderParameter( "objectSpaceCameraPosition", objectSpaceCameraPosition ); if ( sEffectParameters.Contains( "modelViewProjectionMatrix" ) ) { SetEffectParameter( "modelViewProjectionMatrix", modelViewProjectionMatrix ); } }
void WorldManager::SetCurrentCamera( UInt32 pCameraIndex ) { Super::SetCurrentCamera( pCameraIndex ); // Notice the listener of the new camera. FireCameraChanged( GetCurrentCamera() ); }
void EditorScene::UpdateCameraLight() { Camera *camera = GetCurrentCamera(); if(!camera || !cameraLight || !cameraLight->GetParent()) return; Matrix4 m = Matrix4::MakeTranslation(camera->GetPosition() + camera->GetLeft() * 20.f + camera->GetUp() * 20.f); cameraLight->SetLocalTransform(m); }
void CameraOrbit(KFbxScene* pScene, KFbxVector4 lOrigCamPos, double OrigRoll, int dX, int dY) { KFbxCamera* lCamera = GetCurrentCamera(pScene); if (!lCamera) return; KFbxGlobalCameraSettings& lGlobalCameraSettings = pScene->GlobalCameraSettings(); if (lCamera != lGlobalCameraSettings.GetCameraProducerPerspective()) return; if (lCamera->LockMode.Get()) return; if (dX == 0 && dY == 0) return; KFbxVector4 lRotationVector, lNewPosition, lCurPosition; KFbxXMatrix lRotation; KFbxVector4 lCenter = lCamera->InterestPosition.Get(); KFbxVector4 lPosition = lCamera->Position.Get(); lCurPosition = lPosition-lCenter; lNewPosition = lOrigCamPos-lCenter; int rotX; if (lNewPosition[2] == 0) { rotX = 90; } else { rotX = (int) (180.0* atan((double)lNewPosition[0]/(double)lNewPosition[2]) / 3.14159); } bool bRoll = (((int)OrigRoll % 360) != 0); if ( (lNewPosition[2] < 0 && !bRoll) || (lNewPosition[2] > 0 && bRoll) ) { dY = -dY; } if (bRoll) dX = -dX; lRotationVector[1] = -rotX; lRotation.SetR(lRotationVector); lNewPosition = lRotation.MultT(lNewPosition); lRotationVector[1] = 0; lRotationVector[0] = dY; lRotation.SetR(lRotationVector); lNewPosition = lRotation.MultT(lNewPosition); lRotationVector[0] = 0; lRotationVector[1] = rotX; lRotation.SetR(lRotationVector); lNewPosition = lRotation.MultT(lNewPosition); lRotationVector[1] = -dX; lRotation.SetR(lRotationVector); lNewPosition = lRotation.MultT(lNewPosition); if ( lNewPosition[0]*lCurPosition[0] < 0 && lNewPosition[2]*lCurPosition[2] < 0) { double lRoll = lCamera->Roll.Get(); lRoll = 180.0-lRoll; lCamera->Roll.Set(lRoll); } lNewPosition = lNewPosition + lCenter; lCamera->Position.Set(lNewPosition); }
Entity* WorldManager::DoSelectionTest( const Vector2f& pSelectionPoint ) { Int32 viewport[4]; Renderer* renderer = GraphicSubsystem::Instance()->GetRenderer(); renderer->GetViewport(viewport); Vector3f screenRay( pSelectionPoint.x, viewport[3] - pSelectionPoint.y - 1, 0 ); Vector3f worldRay = renderer->ScreenToWorld( screenRay ); // Get the vector from the mouse positions. Vector3f testOrigin = worldRay; Vector3f testDir = worldRay - GetCurrentCamera()->GetPosition(); testDir.Normalize(); return LineTrace( testOrigin, testDir ); }
void CameraThirdPerson::Update(float deltaTime) { render::Camera* camera = GetCurrentCamera(); if(camera) { const ion::Matrix4& transform = camera->GetTransform(); const ion::Vector3& currentPosition = transform.GetTranslation(); ion::Vector3 newPosition = currentPosition.Lerp(m_targetHeadPosition, m_moveSpeed * deltaTime); ion::Vector3 newFocus = m_focusPosition.Lerp(m_targetFocusPosition, m_moveSpeed * deltaTime); camera->SetPosition(newPosition); camera->SetLookAt(newFocus); m_focusPosition = newFocus; } }
void CameraPan(KFbxScene* pScene, KFbxVector4 lOrigCamPos, KFbxVector4 lOrigCamCenter, double OrigRoll, int dX, int dY) { KFbxCamera* lCamera = GetCurrentCamera(pScene); if (!lCamera) return; if (!IsProducerCamera(pScene, lCamera)) return; if (lCamera->LockMode.Get()) return; if (dX == 0 && dY == 0) return; KFbxGlobalCameraSettings& lGlobalCameraSettings = pScene->GlobalCameraSettings(); KFbxVector4 lRotationXV, lRotationYV, lTranslationV; KFbxXMatrix lRotationX, lRotationY, lRotationXInverse, lRotationYInverse, lTranslation; KFbxVector4 lNewPosition = lOrigCamPos; KFbxVector4 lNewCenter = lOrigCamCenter; if (lCamera == lGlobalCameraSettings.GetCameraProducerTop()) { lTranslationV[0] = -dX; lTranslationV[1] = 0; lTranslationV[2] = dY; } else if (lCamera == lGlobalCameraSettings.GetCameraProducerBottom()) { lTranslationV[0] = dX; lTranslationV[1] = 0; lTranslationV[2] = dY; } else if (lCamera == lGlobalCameraSettings.GetCameraProducerFront()) { lTranslationV[0] = -dX; lTranslationV[1] = -dY; lTranslationV[2] = 0; } else if (lCamera == lGlobalCameraSettings.GetCameraProducerBack()) { lTranslationV[0] = dX; lTranslationV[1] = -dY; lTranslationV[2] = 0; } else if (lCamera == lGlobalCameraSettings.GetCameraProducerRight()) { lTranslationV[0] = 0; lTranslationV[1] = -dY; lTranslationV[2] = dX; } else if (lCamera == lGlobalCameraSettings.GetCameraProducerLeft()) { lTranslationV[0] = 0; lTranslationV[1] = -dY; lTranslationV[2] = -dX; } else { bool bRoll = (((int)OrigRoll % 360) != 0); if (bRoll) { dX = -dX; dY = -dY; } double aZ, aY; KFbxVector4 lDist = lNewPosition - lNewCenter; double dist = (double)(lDist[0]*lDist[0]+lDist[1]*lDist[1]+lDist[2]*lDist[2]); if (lDist[2] == 0) { aZ = 90.0; } else { aZ = (180.0* atan((double)lDist[0]/(double)lDist[2]) / 3.14159); } if (lNewPosition[2] < lNewCenter[2]) aZ += 180; if (dist > 0.001) { aY = (180.0 * asin(sqrt((double)(lDist[1]*lDist[1])/ dist)) / 3.14159); } else { aY = 0; } if (lNewPosition[1] < lNewCenter[1]) aY = -aY; lTranslationV[0] = -dX; lTranslationV[1] = -dY; lTranslationV[2] = 0; lRotationYV[0] = 0; lRotationYV[1] = -aZ; lRotationYV[2] = 0; lRotationY.SetR(lRotationYV); lRotationXV[0] = aY; lRotationXV[1] = 0; lRotationXV[2] = 0; lRotationX.SetR(lRotationXV); lTranslation.SetT(lTranslationV); lRotationYInverse = lRotationY.Inverse(); lRotationXInverse = lRotationX.Inverse(); lTranslation = lRotationYInverse * lRotationXInverse * lTranslation * lRotationY * lRotationX; lTranslationV = lTranslation.GetT(); } lNewPosition += lTranslationV; lNewCenter += lTranslationV; lCamera->Position.Set(lNewPosition); lCamera->InterestPosition.Set(lNewCenter); }
void CameraZoomOut(KFbxScene* pScene, int dY) { KFbxCamera* lCamera = GetCurrentCamera(pScene); if (lCamera) { if (lCamera->ProjectionType.Get() == KFbxCamera::ePERSPECTIVE) { double lOriginalAperture = sqrt(lCamera->GetApertureWidth()); double lApertureTransform = lOriginalAperture+dY/400.0; if (lApertureTransform <0.25) { lApertureTransform =0.25; } if (lApertureTransform*lApertureTransform >179) { lApertureTransform = sqrt(179.0); } double lApertureH = lApertureTransform/lOriginalAperture*sqrt(lCamera->GetApertureHeight()); double lApertureWidthOrig = lCamera->GetApertureWidth(); double lApertureHeightOrig = lCamera->GetApertureHeight(); if (lCamera->GetApertureMode() == KFbxCamera::eFOCAL_LENGTH) { double lFLOrig = lCamera->FocalLength.Get(); double lFOVOrig = lCamera->ComputeFieldOfView(lFLOrig); // recompute to be sure. lCamera->SetApertureWidth(lApertureTransform * lApertureTransform); lCamera->SetApertureHeight(lApertureH * lApertureH); double lFLNew = lCamera->ComputeFocalLength(lFOVOrig); double lFOVNew = lCamera->ComputeFieldOfView(lFLNew); lCamera->FocalLength.Set(lFLNew); lCamera->FieldOfView.Set(lFOVNew); } else if (lCamera->GetApertureMode() == KFbxCamera::eVERTICAL || lCamera->GetApertureMode() == KFbxCamera::eHORIZONTAL) { double lFOVOrig = lCamera->FieldOfView.Get(); double lFLOrig = lCamera->ComputeFocalLength(lFOVOrig); // recompute to be sure. lCamera->SetApertureWidth(lApertureTransform * lApertureTransform); lCamera->SetApertureHeight(lApertureH * lApertureH); double lFOVNew = lCamera->ComputeFieldOfView(lFLOrig); double lFLNew = lCamera->ComputeFocalLength(lFOVNew); lCamera->FieldOfView.Set(lFOVNew); lCamera->FocalLength.Set(lFLNew); } else if (lCamera->GetApertureMode() == KFbxCamera::eHORIZONTAL_AND_VERTICAL) { double lFOVOrig = lCamera->FieldOfViewY.Get(); double lFLOrig = lCamera->ComputeFocalLength(lFOVOrig); // recompute to be sure. lCamera->SetApertureWidth(lApertureTransform * lApertureTransform); lCamera->SetApertureHeight(lApertureH * lApertureH); double lFOVNew = lCamera->ComputeFieldOfView(lFLOrig); double lFLNew = lCamera->ComputeFocalLength(lFOVNew); lCamera->FieldOfViewY.Set(lFOVNew); lCamera->FocalLength.Set(lFLNew); } lCamera->SetApertureWidth(lApertureWidthOrig); lCamera->SetApertureHeight(lApertureHeightOrig); } else { gsOrthoCameraScale *= 1.25; } } }
// 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); } }
void CameraPan(FbxScene* pScene, FbxVector4 lOrigCamPos, FbxVector4 lOrigCamCenter, double OrigRoll, int dX, int dY) { // Pan the camera horizontally dX degrees, vertically dY degrees. FbxCamera* lCamera = GetCurrentCamera(pScene); if (!lCamera) return; if (!IsProducerCamera(pScene, lCamera)) return; if (lCamera->LockMode.Get()) return; if (dX == 0 && dY == 0) return; FbxGlobalCameraSettings& lGlobalCameraSettings = pScene->GlobalCameraSettings(); FbxVector4 lRotationXV, lRotationYV, lTranslationV; FbxAMatrix lRotationX, lRotationY, lRotationXInverse, lRotationYInverse, lTranslation; FbxVector4 lNewPosition = lOrigCamPos; FbxVector4 lNewCenter = lOrigCamCenter; // Translate the camera in dX and dY according to its point of view. if (lCamera == lGlobalCameraSettings.GetCameraProducerTop()) { lTranslationV[0] = -dX; lTranslationV[1] = 0; lTranslationV[2] = dY; } else if (lCamera == lGlobalCameraSettings.GetCameraProducerBottom()) { lTranslationV[0] = dX; lTranslationV[1] = 0; lTranslationV[2] = dY; } else if (lCamera == lGlobalCameraSettings.GetCameraProducerFront()) { lTranslationV[0] = -dX; lTranslationV[1] = -dY; lTranslationV[2] = 0; } else if (lCamera == lGlobalCameraSettings.GetCameraProducerBack()) { lTranslationV[0] = dX; lTranslationV[1] = -dY; lTranslationV[2] = 0; } else if (lCamera == lGlobalCameraSettings.GetCameraProducerRight()) { lTranslationV[0] = 0; lTranslationV[1] = -dY; lTranslationV[2] = dX; } else if (lCamera == lGlobalCameraSettings.GetCameraProducerLeft()) { lTranslationV[0] = 0; lTranslationV[1] = -dY; lTranslationV[2] = -dX; } else { // Perspective view. More computation. // Adjust displacement when there is roll bool bRoll = (((int)OrigRoll % 360) != 0); if (bRoll) { dX = -dX; dY = -dY; } // Compute angles aY and aZ of the camera with Y and Z axis. double aZ, aY; /// Vector of distance between camera and center (lookat) FbxVector4 lDist = lNewPosition - lNewCenter; // Euclidian distance between camera and lookat double dist = (double)(lDist[0]*lDist[0]+lDist[1]*lDist[1]+lDist[2]*lDist[2]); // aZ if (lDist[2] == 0) { aZ = 90.0; } else { aZ = (atan((double)lDist[0]/(double)lDist[2]) * FBXSDK_180_DIV_PI); } if (lNewPosition[2] < lNewCenter[2]) aZ += 180; // aY if (dist > 0.001) { aY = (asin(sqrt((double)(lDist[1]*lDist[1])/ dist)) * FBXSDK_180_DIV_PI); } else { aY = 0; } if (lNewPosition[1] < lNewCenter[1]) aY = -aY; // Basis translation lTranslationV[0] = -dX; lTranslationV[1] = -dY; lTranslationV[2] = 0; // Rotation around Y axis lRotationYV[0] = 0; lRotationYV[1] = -aZ; lRotationYV[2] = 0; lRotationY.SetR(lRotationYV); // Rotation around X axis lRotationXV[0] = aY; lRotationXV[1] = 0; lRotationXV[2] = 0; lRotationX.SetR(lRotationXV); // Modify translation according to aY and aZ. lTranslation.SetT(lTranslationV); lRotationYInverse = lRotationY.Inverse(); lRotationXInverse = lRotationX.Inverse(); lTranslation = lRotationYInverse * lRotationXInverse * lTranslation * lRotationY * lRotationX; lTranslationV = lTranslation.GetT(); } // Translate camera and center according to pan. lNewPosition += lTranslationV; lNewCenter += lTranslationV; lCamera->Position.Set(lNewPosition); lCamera->InterestPosition.Set(lNewCenter); }
void CameraOrbit(FbxScene* pScene, FbxVector4 lOrigCamPos, double OrigRoll, int dX, int dY) { // Orbit the camera horizontally dX degrees, vertically dY degrees. FbxCamera* lCamera = GetCurrentCamera(pScene); if (!lCamera) return; FbxGlobalCameraSettings& lGlobalCameraSettings = pScene->GlobalCameraSettings(); if (lCamera != lGlobalCameraSettings.GetCameraProducerPerspective()) return; if (lCamera->LockMode.Get()) return; if (dX == 0 && dY == 0) return; FbxVector4 lRotationVector, lNewPosition, lCurPosition; FbxAMatrix lRotation; FbxVector4 lCenter = lCamera->InterestPosition.Get(); // current position FbxVector4 lPosition = lCamera->Position.Get(); lCurPosition = lPosition-lCenter; // translate lNewPosition = lOrigCamPos-lCenter; int rotX; if (lNewPosition[2] == 0) { rotX = 90; } else { rotX = (int) (atan((double)lNewPosition[0]/(double)lNewPosition[2]) * FBXSDK_180_DIV_PI); } bool bRoll = (((int)OrigRoll % 360) != 0); if ( (lNewPosition[2] < 0 && !bRoll) || (lNewPosition[2] > 0 && bRoll) ) { dY = -dY; } if (bRoll) dX = -dX; // Center on the X axis (push) lRotationVector[1] = -rotX; lRotation.SetR(lRotationVector); lNewPosition = lRotation.MultT(lNewPosition); // Rotation for the vertical movement: around the X axis lRotationVector[1] = 0; lRotationVector[0] = dY; lRotation.SetR(lRotationVector); lNewPosition = lRotation.MultT(lNewPosition); // Back from the X axis (pop) lRotationVector[0] = 0; lRotationVector[1] = rotX; lRotation.SetR(lRotationVector); lNewPosition = lRotation.MultT(lNewPosition); // Rotation for the horizontal movement lRotationVector[1] = -dX; lRotation.SetR(lRotationVector); lNewPosition = lRotation.MultT(lNewPosition); // Detect camera flip if ( lNewPosition[0]*lCurPosition[0] < 0 && lNewPosition[2]*lCurPosition[2] < 0) { // flip -> roll 180. double lRoll = lCamera->Roll.Get(); lRoll = 180.0-lRoll; lCamera->Roll.Set(lRoll); } // Back from center lNewPosition = lNewPosition + lCenter; lCamera->Position.Set(lNewPosition); }
void CameraZoom(FbxScene* pScene, int pZoomDepth, int pZoomMode) { FbxCamera* lCamera = GetCurrentCamera(pScene); if( lCamera == NULL) return; if( pZoomMode == SceneContext::ZOOM_FOCAL_LENGTH) { if (lCamera->ProjectionType.Get() == FbxCamera::ePerspective) { double lTransform = 0 - pZoomDepth / 100.0; double lApertureW = lCamera->GetApertureWidth(); lApertureW = TransformAperture( lApertureW, lTransform); double lApertureH = lCamera->GetApertureHeight(); lApertureH = TransformAperture( lApertureH, lTransform); UpdatePerspCameraAttributes( lCamera, lApertureW, lApertureH); } else { if( pZoomDepth > 0) gsOrthoCameraScale *= 0.8; else gsOrthoCameraScale *= 1.25; } } else { FbxNode* lCameraNode = lCamera ? lCamera->GetNode() : NULL; // Compute the camera position and direction. FbxVector4 lEye(0,0,1); FbxVector4 lCenter(0,0,0); FbxVector4 lForward(0,0,0); if (lCamera) { lEye = lCamera->Position.Get(); } if (lCameraNode && lCameraNode->GetTarget()) { lCenter = lCameraNode->GetTarget()->LclTranslation.Get(); lForward = lCenter - lEye; } else { if (!lCameraNode || IsProducerCamera(pScene, lCamera)) { if (lCamera) { lCenter = lCamera->InterestPosition.Get(); lForward = lCenter - lEye; } } else { // Get the direction FbxAMatrix lGlobalRotation; FbxVector4 lRotationVector( lCameraNode->LclRotation.Get()); lGlobalRotation.SetR(lRotationVector); // Set the center. // A camera with rotation = {0,0,0} points to the X direction. So create a // vector in the X direction, rotate that vector by the global rotation amount // and then position the center by scaling and translating the resulting vector lRotationVector = FbxVector4(1.0,0,0); lForward = lGlobalRotation.MultT(lRotationVector); } } lForward.Normalize(); lEye += lForward * pZoomDepth; FbxDouble3 lPosition(lEye[0], lEye[1], lEye[2]); lCamera->Position.Set(lPosition); } }
// Set the view to the current camera settings. void SetCamera(FbxScene* pScene, FbxTime& pTime, FbxAnimLayer* pAnimLayer, const FbxArray<FbxNode*>& pCameraArray, int pWindowWidth, int pWindowHeight) { // Find the current camera at the given time. FbxCamera* lCamera = GetCurrentCamera(pScene, pTime, pAnimLayer, pCameraArray); if( lCamera == NULL) return; FbxNode* lCameraNode = lCamera ? lCamera->GetNode() : NULL; // Compute the camera position and direction. FbxVector4 lEye(0,0,1); FbxVector4 lCenter(0,0,0); FbxVector4 lUp(0,1,0); FbxVector4 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 { // Get the direction FbxAMatrix lGlobalRotation; FbxVector4 lRotationVector(GetGlobalPosition(lCameraNode, pTime).GetR()); lGlobalRotation.SetR(lRotationVector); // Get the length FbxVector4 lInterestPosition(lCamera->InterestPosition.Get()); FbxVector4 lCameraGlobalPosition(GetGlobalPosition(lCameraNode, pTime).GetT()); double lLength = (FbxVector4(lInterestPosition - lCameraGlobalPosition).Length()); // Set the center. // A camera with rotation = {0,0,0} points to the X direction. So create a // vector in the X direction, rotate that vector by the global rotation amount // and then position the center by scaling and translating the resulting vector lRotationVector = FbxVector4(1.0,0,0); lCenter = lGlobalRotation.MultT(lRotationVector); lCenter *= lLength; lCenter += lEye; // Update the default up vector with the camera rotation. lRotationVector = FbxVector4(0,1.0,0); lUp = lGlobalRotation.MultT(lRotationVector); } } // Align the up vector. lForward = lCenter - lEye; lForward.Normalize(); lRight = lForward.CrossProduct(lUp); lRight.Normalize(); lUp = lRight.CrossProduct(lForward); lUp.Normalize(); // Rotate the up vector with the roll value. double lRadians = 0; if (lCamera) lRadians = lCamera->Roll.Get() * FBXSDK_PI_DIV_180; lUp = lUp * cos( lRadians) + lRight * sin(lRadians); double lNearPlane = 0.01; if (lCamera) lNearPlane = lCamera->GetNearPlane(); double lFarPlane = 4000.0; if (lCamera) lFarPlane = lCamera->GetFarPlane(); //Get global scaling. FbxVector4 lCameraScaling = GetGlobalPosition(lCameraNode, pTime).GetS(); static const int FORWARD_SCALE = 2; //scaling near plane and far plane lNearPlane *= lCameraScaling[ FORWARD_SCALE]; lFarPlane *= lCameraScaling[ FORWARD_SCALE]; // Get the relevant camera settings for a perspective view. if (lCamera && lCamera->ProjectionType.Get() == FbxCamera::ePerspective) { //get the aspect ratio 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(); } double lRealScreenRatio = (double)pWindowWidth / (double)pWindowHeight; int lViewPortPosX = 0, lViewPortPosY = 0, lViewPortSizeX = pWindowWidth, lViewPortSizeY = pWindowHeight; //compute the view port if( lRealScreenRatio > lAspectRatio) { lViewPortSizeY = pWindowHeight; lViewPortSizeX = (int)( lViewPortSizeY * lAspectRatio); lViewPortPosY = 0; lViewPortPosX = (int)((pWindowWidth - lViewPortSizeX) * 0.5); } else { lViewPortSizeX = pWindowWidth; lViewPortSizeY = (int)(lViewPortSizeX / lAspectRatio); lViewPortPosX = 0; lViewPortPosY = (int)((pWindowHeight - lViewPortSizeY) * 0.5); } //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; GlSetCameraPerspective( lFieldOfViewY, lAspectRatio, lNearPlane, lFarPlane, lEye, lCenter, lUp, lFilmOffsetX, lFilmOffsetY); //glMatrixMode(GL_PROJECTION); //double lTestPerpMatrix[ 16]; //glGetDoublev( GL_PROJECTION_MATRIX, lTestPerpMatrix); //lTestPerpMatrix[ 8] -= lFilmOffsetX; //lTestPerpMatrix[ 9] -= lFilmOffsetY; // //glLoadMatrixd( lTestPerpMatrix); //glMatrixMode(GL_MODELVIEW); glViewport( lViewPortPosX, lViewPortPosY, lViewPortSizeX, lViewPortSizeY); } // Get the relevant camera settings for an orthogonal view. else { double lPixelRatio = 1.0; if (lCamera) lPixelRatio = lCamera->GetPixelRatio(); double lLeftPlane, lRightPlane, lBottomPlane, lTopPlane; if(pWindowWidth < pWindowHeight) { lLeftPlane = -gsOrthoCameraScale * lPixelRatio; lRightPlane = gsOrthoCameraScale * lPixelRatio; lBottomPlane = -gsOrthoCameraScale * pWindowHeight / pWindowWidth; lTopPlane = gsOrthoCameraScale * pWindowHeight / pWindowWidth; } else { pWindowWidth *= (int) lPixelRatio; lLeftPlane = -gsOrthoCameraScale * pWindowWidth / pWindowHeight; lRightPlane = gsOrthoCameraScale * pWindowWidth / pWindowHeight; lBottomPlane = -gsOrthoCameraScale; lTopPlane = gsOrthoCameraScale; } GlSetCameraOrthogonal(lLeftPlane, lRightPlane, lBottomPlane, lTopPlane, lNearPlane, lFarPlane, lEye, lCenter, lUp); } }
void World::Render() { if( !mWorldInitialized ) return; Renderer* renderer = GraphicSubsystem::Instance()->GetRenderer(); GD_ASSERT(renderer); UInt32 viewWidth = renderer->GetRenderTarget()->GetWidth(); UInt32 viewHeight = renderer->GetRenderTarget()->GetHeight(); // Init. //renderer->BeginScene( Renderer::PointList ); renderer->SetViewport( 0, 0, viewWidth, viewHeight ); renderer->SetClearColor(Color4f(0.0f, 0.0f, 0.0f, 1.0f)); renderer->SetRenderState( Renderer::Texture2D, false ); renderer->SetRenderState( Renderer::DepthTest, true ); //renderer->SetRenderState( Renderer::Lighting, true ); //renderer->SetCulling( Renderer::CullBackFace ); // Render. renderer->Clear( Renderer::ColorBuffer | Renderer::DepthBuffer ); renderer->SetMatrixMode(Renderer::ProjectionMatrix); renderer->LoadIdentity(); Camera* currentCamera = GetCurrentCamera(); renderer->Perspective(currentCamera->GetFovAngle(), viewWidth / static_cast<Float>(viewHeight), currentCamera->GetNearView(), currentCamera->GetFarView()); renderer->SetMatrixMode(Renderer::ModelViewMatrix); renderer->LoadIdentity(); currentCamera->ApplyViewMatrix(); /* Light light; light.mPosition = Vector3f( 4.32f, -3.0f, -3.4f ).Normalize(); light.mAmbient = Color4f(1.0f,1.0f, 1.0f,1.0f); light.mDiffuse = Color4f(1.0f,1.0f, 1.0f,1.0f); light.mSpecular = Color4f(1.0f,1.0f, 1.0f,1.0f); light.mType = Renderer::LightDirectional; renderer->SetRenderState( Renderer::Light_i, true, 0 ); renderer->SetLight( 0, light );*/ #if 0 Matrix4d modelView; renderer->GetModelViewMatrix( modelView ); Vector3f vRight( modelView[0], modelView[4], modelView[8] ); Vector3f vUp( modelView[1], modelView[5], modelView[9] ); Vector3f vBottomLeft = light.mPosition + (-vRight - vUp).Normalize() * 2; Vector3f vBottomRight = light.mPosition + ( vRight - vUp).Normalize() * 2; Vector3f vTopRight = light.mPosition + ( vRight + vUp).Normalize() * 2; Vector3f vTopLeft = light.mPosition + (-vRight + vUp).Normalize() * 2; /*renderer->SetRenderState( Renderer::DepthMask, false ); renderer->SetRenderState( Renderer::Lighting, false ); renderer->SetRenderState( Renderer::Blend, true ); renderer->SetBlendFunc( Renderer::BlendSrcAlpha, Renderer::BlendOne );*/ renderer->SetRenderState( Renderer::Lighting, false ); renderer->DrawQuad( vBottomLeft, vBottomRight, vTopRight, vTopLeft, false ); renderer->SetRenderState( Renderer::Lighting, true ); #endif renderer->SetAmbiantColor( Color4f(1.0f, 1.0f, 1.0f, 1.0f) ); /*Light lightPoint; lightPoint.mPosition = currentCamera->GetPosition(); lightPoint.mAmbient = Color4f(0.00f,0.00f, 0.00f,1.0f); lightPoint.mDiffuse = Color4f(1.0f,1.0f, 1.0f,1.0f); lightPoint.mSpecular = Color4f(1.0f,1.0f, 1.0f,1.0f); lightPoint.mType = Renderer::LightPoint; lightPoint.mAttenuationConstant = 0; lightPoint.mAttenuationLinear = 0; lightPoint.mAttenuationQuadratic = 0.01f; renderer->SetRenderState( Renderer::Light_i, true, 0 ); renderer->SetLight( 0, lightPoint );*/ // Get the frustum. Matrix4f modelViewMatrix; Matrix4f projectionMatrix; Frustum frustum; renderer->GetModelViewMatrix(modelViewMatrix); renderer->GetProjectionMatrix(projectionMatrix); frustum.CalculateFrustum(projectionMatrix, modelViewMatrix); mNbRenderedEntities = 0; // Render the objects in the world. /* List<Entity*> visibleEntities; mSpacePartition->Query(frustum, visibleEntities); */ List<Entity*>::const_iterator itEntity; for( itEntity = mEntities.begin(); itEntity != mEntities.end(); ++itEntity ) { if( *itEntity != currentCamera ) { // Do frustum culling. if((*itEntity)->GetClass() != Terrain::StaticClass() && (*itEntity)->GetClass() != SkyDome::StaticClass()) { BoundingBox boundingBox = (*itEntity)->GetBoundingBox(); if( (*itEntity)->GetClass() != ParticleEmitter::StaticClass() ) { Matrix4f translation = Matrix4f::Translation((*itEntity)->GetPosition()); Matrix4f rotation; (*itEntity)->GetOrientation().ToMatrix(rotation); Matrix4f trsMatrix = rotation * translation; boundingBox.SetMin( boundingBox.Min() * trsMatrix ); boundingBox.SetMax( boundingBox.Max() * trsMatrix ); } if( frustum.BoxInFrustum(boundingBox) ) { mNbRenderedEntities++; } else { continue; } } renderer->PushMatrix(); if( String((*itEntity)->GetClass()->GetName()) == "SkyDome" ) renderer->Translate(currentCamera->GetPosition()-Vector3f(0,100,0) ); else renderer->Translate((*itEntity)->GetPosition()); renderer->Rotate((*itEntity)->GetOrientation()); if( (*itEntity)->IsSelected() ) (*itEntity)->RenderSelected(); (*itEntity)->Render(); renderer->PopMatrix(); } } renderer->SetMatrixMode(Renderer::ModelViewMatrix); renderer->LoadIdentity(); currentCamera->ApplyViewMatrix(); //renderer->EndScene(); }