void Camera::MenuCameraRotation() { // Create rotation matrix m_menuRotation += (float)GLOBAL::GetInstance().GetDeltaTime() / 16.0f; if (m_menuRotation > 6.28f) { m_menuRotation = 0.0f; } DirectX::XMMATRIX rotation = DirectX::XMMatrixRotationY(m_menuRotation); // Lock shadows in center. DirectX::XMFLOAT3 shadowPosition = GetPosition(); DirectX::XMStoreFloat3(&shadowPosition, DirectX::XMVector3TransformCoord(DirectX::XMLoadFloat3(&shadowPosition), rotation)); shadowPosition.x *= 0.8f; DirectX::XMFLOAT3 position = DirectX::XMFLOAT3(25.0f + shadowPosition.x, 100.0f, 50.0f); UpdatePosition(position); UpdateTarget(DirectX::XMFLOAT3(shadowPosition.x, 5.0f, 0.0f)); UpdateViewMatrix(); UpdateProjectionMatrix(true); GraphicsEngine::SetLightViewAndProjection(GetViewMatrix(), GetProjectionMatrix()); // Lock camera in center and rotate camera. position = DirectX::XMFLOAT3(0.0f, 40.0f, -24.0f); UpdatePosition(position); UpdateTarget(DirectX::XMFLOAT3(0.0f, 7.5f, 0.0f)); UpdateViewMatrix(); UpdateProjectionMatrix(false); DirectX::XMStoreFloat4x4(&m_viewMatrix, DirectX::XMMatrixMultiply(rotation, DirectX::XMLoadFloat4x4(&m_viewMatrix))); GraphicsEngine::SetViewAndProjection(GetViewMatrix(), GetProjectionMatrix()); }
void StaticCamera::SetFromSunlight(D3DXVECTOR3* position, D3DXVECTOR3* direction, D3DXVECTOR4 Power, float Width, float Height, float Depth) { D3DXVECTOR3 d3dLookAt = D3DXVECTOR3(Width, Height, Depth) * 0.5f;//(*position + *Direction * m_fFarPlane); D3DXVECTOR3 eye = *position / D3DXVec3Length(position); eye *= D3DXVec3Length(&d3dLookAt) * 1.1f; eye += d3dLookAt; CBaseCamera::SetViewParams(&eye, &d3dLookAt); mLightColor = Power; *position = eye; d3dLookAt = d3dLookAt - eye; d3dLookAt /= D3DXVec3Length(&d3dLookAt); *direction = d3dLookAt; D3DXVECTOR3 cornersWorld[] = { D3DXVECTOR3(0,0,0), D3DXVECTOR3(Width, 0, 0), D3DXVECTOR3(Width, Height, 0), D3DXVECTOR3(Width, Height, Depth), D3DXVECTOR3(0, Height, 0), D3DXVECTOR3(0, Height, Depth), D3DXVECTOR3(0,0,Depth), D3DXVECTOR3(Width, 0, Depth) }; D3DXVECTOR3 lightSpaceBoxMin = D3DXVECTOR3(FLT_MAX,FLT_MAX,FLT_MAX); D3DXVECTOR3 lightSpaceBoxMax = D3DXVECTOR3(FLT_MIN, FLT_MIN, FLT_MIN); for (int i = 0; i < 8; ++i) { D3DXVec3TransformCoord(&cornersWorld[i], &cornersWorld[i], GetViewMatrix()); lightSpaceBoxMin.x = lightSpaceBoxMin.x < cornersWorld[i].x ? lightSpaceBoxMin.x : cornersWorld[i].x; lightSpaceBoxMin.y = lightSpaceBoxMin.y < cornersWorld[i].y ? lightSpaceBoxMin.y : cornersWorld[i].y; lightSpaceBoxMin.z = lightSpaceBoxMin.z < cornersWorld[i].z ? lightSpaceBoxMin.z : cornersWorld[i].z; lightSpaceBoxMax.x = lightSpaceBoxMax.x > cornersWorld[i].x ? lightSpaceBoxMax.x : cornersWorld[i].x; lightSpaceBoxMax.y = lightSpaceBoxMax.y > cornersWorld[i].y ? lightSpaceBoxMax.y : cornersWorld[i].y; lightSpaceBoxMax.z = lightSpaceBoxMax.z > cornersWorld[i].z ? lightSpaceBoxMax.z : cornersWorld[i].z; }; SetOrthoParams(lightSpaceBoxMax.x - lightSpaceBoxMin.x, lightSpaceBoxMax.y - lightSpaceBoxMin.y, lightSpaceBoxMin.z, lightSpaceBoxMax.z); //set constant buffer D3DXMATRIX identityMatrix; D3DXMatrixIdentity(&identityMatrix); cameraCBuffer->Data.World = identityMatrix; cameraCBuffer->Data.refractiveIndexETA = 1.0f; cameraCBuffer->Data.View = *GetViewMatrix(); cameraCBuffer->Data.WorldViewProjection = identityMatrix * cameraCBuffer->Data.View * (*GetProjMatrix()); //float test = D3DXMatrixDeterminant(GetProjMatrix()); //test = D3DXMatrixDeterminant(GetViewMatrix()); //test = test +1; }
void Camera::FollowCharacter(DirectX::XMFLOAT3 p_playerPos) { // Lock shadows on the player. DirectX::XMFLOAT3 playerPosition = p_playerPos; DirectX::XMFLOAT3 position = DirectX::XMFLOAT3(playerPosition.x + 25.0f, playerPosition.y + 100.0f, playerPosition.z + 50.0f); DirectX::XMFLOAT3 target = playerPosition; UpdatePosition(position); UpdateTarget(target); UpdateViewMatrix(); UpdateProjectionMatrix(true); GraphicsEngine::SetLightViewAndProjection(GetViewMatrix(), GetProjectionMatrix()); // Visibility view projection.. playerPosition = p_playerPos; position = DirectX::XMFLOAT3(playerPosition.x, playerPosition.y + 30.0f, playerPosition.z); target = playerPosition; UpdatePosition(position); UpdateTarget(target); UpdateViewMatrix(); GraphicsEngine::SetViewPolygonMatrix(GetViewMatrix()); // Lock camera on the player. playerPosition = p_playerPos; position = DirectX::XMFLOAT3(playerPosition.x, playerPosition.y + 30.0f, playerPosition.z - 15.0f); target = playerPosition; SetOutliningRayPosition(position); SetOutliningRayTarget(target); if (GLOBAL::GetInstance().CAMERA_MOVING) { MovingCamera(playerPosition); } else { UpdatePosition(position); UpdateTarget(target); UpdateViewMatrix(); UpdateProjectionMatrix(false); GraphicsEngine::SetViewAndProjection(GetViewMatrix(), GetProjectionMatrix()); m_oldPosition = position; } GraphicsEngine::UpdateVisibilityPolygon(Point(playerPosition.x, playerPosition.z), (float)GLOBAL::GetInstance().GetDeltaTime()); }
void Camera::LocalRotate( float x, float y ) { Matrix4 viewSpaceRotation; MakeRotationMatrixX(viewSpaceRotation, x); Matrix4 viewSpaceRotationY; MakeRotationMatrixY(viewSpaceRotationY, y); MatrixMultiply(viewSpaceRotation, viewSpaceRotation, viewSpaceRotationY); Vector4& newViewDirection = viewSpaceRotation.Transform(Vector3(0.0f, 0.0f, 1.0f)); Matrix4 cameraRotation = GetViewMatrix(); cameraRotation.m41 = cameraRotation.m42 = cameraRotation.m43 = 0.0f; Matrix4 inverseCameraRotation; MatrixTranspose(inverseCameraRotation, cameraRotation); Vector4& newWorldSpaceViewDir = inverseCameraRotation.Transform(newViewDirection); Vector3 newWorldSpaceViewDir3; newWorldSpaceViewDir3.x = newWorldSpaceViewDir.x; newWorldSpaceViewDir3.y = newWorldSpaceViewDir.y; newWorldSpaceViewDir3.z = newWorldSpaceViewDir.z; newWorldSpaceViewDir3.Normalize(); m_position = m_lookAt - newWorldSpaceViewDir3 * m_distanceToLookAt; Vector3 right = m_up.Cross(newWorldSpaceViewDir3); m_up = newWorldSpaceViewDir3.Cross(right); m_up.Normalize(); m_viewMatrixDirty = true; }
int rViewport::GetSelectionRay(const rPoint& pos , rRay3& selectionRay) const{ rMatrix4 projection, view; GetProjectionMatrix(projection); GetViewMatrix(view); rVector3 nearPoint , farPoint; int result; rVector3 source((float)pos.x , m_rect.height - (float)pos.y , 0.0f); result = rMatrixUtil::Unproject(source, view, projection, m_rect, nearPoint); if (result == false) return false; source.z = 1.0f; result = rMatrixUtil::Unproject(source, view, projection, m_rect, farPoint); if (result == false) return false; rVector3 direction = farPoint - nearPoint; direction.Normalize(); selectionRay.Set(nearPoint, direction); return true; }
Vector4 View::Project( const Vector& Location ) const { const Matrix ViewProjectionMatrix = GetViewMatrix() * GetProjectionMatrix(); const Vector4 ProjectedLocation = Vector4( Location ) * ViewProjectionMatrix; return ProjectedLocation; }
void View::ApplyToRenderer( IRenderer& Renderer ) const { XTRACE_FUNCTION; Renderer.SetViewMatrix( GetViewMatrix() ); Renderer.SetProjectionMatrix( GetProjectionMatrix() ); }
void Camera::ResetCameraToLight() { // Reset camera. DirectX::XMFLOAT3 target = DirectX::XMFLOAT3(0, 0, 0); DirectX::XMFLOAT3 position = DirectX::XMFLOAT3(25.0f, 100.0f, 50.0f); UpdatePosition(position); UpdateTarget(target); m_upVector = DirectX::XMFLOAT3(-25.0f, 100.0f, -50.0f); DirectX::XMStoreFloat3(&m_upVector, DirectX::XMVector3Normalize(DirectX::XMLoadFloat3(&m_upVector))); m_look = DirectX::XMFLOAT3(-25.0f, -100.0f, -50.0f); DirectX::XMStoreFloat3(&m_look, DirectX::XMVector3Normalize(DirectX::XMLoadFloat3(&m_look))); m_right = DirectX::XMFLOAT3(25.0f, 0.0f, -50.0f); DirectX::XMStoreFloat3(&m_right, DirectX::XMVector3Normalize(DirectX::XMLoadFloat3(&m_right))); // Projection data. float aspectRatio = (float)GLOBAL::GetInstance().MAX_SCREEN_WIDTH / (float)GLOBAL::GetInstance().MAX_SCREEN_HEIGHT; UpdateAspectRatio(aspectRatio); UpdateFieldOfView(3.141592f * 0.25f); UpdateClippingPlanes(0.1f, 1000.0f); UpdateViewMatrix(); UpdateProjectionMatrix(true); GraphicsEngine::SetLightViewAndProjection(GetViewMatrix(), GetProjectionMatrix()); }
void My_TestGLDrawing::DrawTest() { GLfloat clearColor[4] = { 0.1f, 0.1f, 0.1f, 1.0f }; glClearBufferfv(GL_COLOR, 0, clearColor); GLfloat clearDepth[1] = { 1.0f }; glClearBufferfv(GL_DEPTH, 0, clearDepth); int width = GetWidth(), height = GetHeight(); GfMatrix4d viewMatrix = GetViewMatrix(); GfMatrix4d projMatrix = GetProjectionMatrix(); _driver->SetCullStyle(_cullStyle); // camera _driver->SetCamera(viewMatrix, projMatrix, GfVec4d(0, 0, width, height)); glViewport(0, 0, width, height); glEnable(GL_DEPTH_TEST); glBindVertexArray(vao); _driver->Draw(); glBindVertexArray(0); }
bool Camera::ObjectFrustumCulling( const RenderObject& obj ) { const AABB& aabb = obj.m_worldAABB; //物体坐标转换到相机空间进行裁减 VEC4 pos(aabb.GetCenter(), 1.0f); Common::Transform_Vec4_By_Mat44(pos, pos, GetViewMatrix()); float n = GetNearClip(); float f = GetFarClip(); float fov = GetFov(); float half_w = n * std::tan(fov/2); float half_h = half_w / GetAspectRatio(); //检测前后面 if(-pos.z+aabb.m_boundingRadius <= n || -pos.z-aabb.m_boundingRadius >= f) return true; //检测左右面 float planeX = half_w * pos.z / -n; if(pos.x - planeX >= aabb.m_boundingRadius || pos.x + aabb.m_boundingRadius <= -planeX) return true; //检测上下面 float planeY = half_h * pos.z / -n; if(pos.y - planeY >= aabb.m_boundingRadius || pos.y + aabb.m_boundingRadius <= -planeY) return true; return false; }
void Camera::ApplyViewProjTransform() { glMatrixMode(GL_PROJECTION); glLoadIdentity(); glMultMatrixf(GetProjMatrix()); // or glLoadMatrixf() glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glMultMatrixf(GetViewMatrix()); // or glLoadMatrixf() }
void Camera::ToggleFullscreen(bool p_fullscreen) { GLOBAL::GetInstance().SWITCHING_SCREEN_MODE = true; if (p_fullscreen) { // Go to fullscreen GLOBAL::GetInstance().CURRENT_SCREEN_WIDTH = GLOBAL::GetInstance().MAX_SCREEN_WIDTH; GLOBAL::GetInstance().CURRENT_SCREEN_HEIGHT = GLOBAL::GetInstance().MAX_SCREEN_HEIGHT; SetWindowPos(GraphicsEngine::GetWindowHandle(), HWND_TOP, 0, 0, GLOBAL::GetInstance().CURRENT_SCREEN_WIDTH, GLOBAL::GetInstance().CURRENT_SCREEN_HEIGHT, SWP_SHOWWINDOW); //GraphicsEngine::GetInstance()->ToggleFullscreen(true); // Update aspect ratio. float aspectRatio = (float)GLOBAL::GetInstance().CURRENT_SCREEN_WIDTH / (float)GLOBAL::GetInstance().CURRENT_SCREEN_HEIGHT; UpdateAspectRatio(aspectRatio); UpdateProjectionMatrix(false); GraphicsEngine::SetViewAndProjection(GetViewMatrix(), GetProjectionMatrix()); // Set both window positions. HWND console = GetConsoleWindow(); MoveWindow(console, GLOBAL::GetInstance().CURRENT_SCREEN_WIDTH, 0, 670, 1000, true); } else { // Go to windowed mode. GLOBAL::GetInstance().CURRENT_SCREEN_WIDTH = GLOBAL::GetInstance().MIN_SCREEN_WIDTH; GLOBAL::GetInstance().CURRENT_SCREEN_HEIGHT = GLOBAL::GetInstance().MIN_SCREEN_HEIGHT; //GraphicsEngine::GetInstance()->ToggleFullscreen(false); // Update aspect ratio. float aspectRatio = (float)GLOBAL::GetInstance().CURRENT_SCREEN_WIDTH / (float)GLOBAL::GetInstance().CURRENT_SCREEN_HEIGHT; UpdateAspectRatio(aspectRatio); UpdateProjectionMatrix(false); GraphicsEngine::SetViewAndProjection(GetViewMatrix(), GetProjectionMatrix()); // Set both window positions. HWND console = GetConsoleWindow(); MoveWindow(console, GLOBAL::GetInstance().CURRENT_SCREEN_WIDTH, 0, 670, 1000, true); SetWindowPos(GraphicsEngine::GetWindowHandle(), HWND_TOP, 0, 0, GLOBAL::GetInstance().CURRENT_SCREEN_WIDTH, GLOBAL::GetInstance().CURRENT_SCREEN_HEIGHT, SWP_SHOWWINDOW); } GLOBAL::GetInstance().SWITCHING_SCREEN_MODE = false; }
Vector2 Camera::WorldToScreenNDCPoint(const Vector3 &position) { Matrix4 p, v; GetProjectionMatrix(&p); GetViewMatrix(&v); Vector4 v4 = p * v * Vector4(position, 1); v4 /= v4.w; return Vector2(v4.xy()); }
void rViewport::GetViewProjectionMatrix(rMatrix4& matrix) const{ rMatrix4 projection, view; if (m_camera){ GetProjectionMatrix(projection); GetViewMatrix(view); } matrix = projection * view; }
glm::mat4 Camera::GetViewProjectionMatrix() const { // @TODO 1 - Calculate View Projection Matrix // The projection matrix is hardcoded below // The view matrix is set in the derived camera classes. //glm::mat4 viewProjection(15.0f); // identity, you need to change this //we need return GetProjectionMatrix() * GetViewMatrix(); }
void RenderInputScene::SetCamera(const GraphicsCamera & cam) { cam_position = cam.pos; cam_rotation = cam.rot; lod_far = cam.view_distance; projMatrix = GetProjMatrix(cam); viewMatrix = GetViewMatrix(cam); frustum.Extract(projMatrix.GetArray(), viewMatrix.GetArray()); }
void FWCamera::BuildFrustum() { // COMMENT : Calculate frustum planes Matrix4x4 kMatFrustum = GetViewMatrix() * GetProjectionMatrix(); // COMMENT : Near m_akFrustum[0] = Plane( kMatFrustum._14 + kMatFrustum._13, kMatFrustum._24 + kMatFrustum._23, kMatFrustum._34 + kMatFrustum._33, kMatFrustum._44 + kMatFrustum._43); // COMMENT : Far m_akFrustum[1] = Plane( kMatFrustum._14 - kMatFrustum._13, kMatFrustum._24 - kMatFrustum._23, kMatFrustum._34 - kMatFrustum._33, kMatFrustum._44 - kMatFrustum._43); // COMMENT : Left m_akFrustum[2] = Plane( kMatFrustum._14 + kMatFrustum._11, kMatFrustum._24 + kMatFrustum._21, kMatFrustum._34 + kMatFrustum._31, kMatFrustum._44 + kMatFrustum._41); // COMMENT : Right m_akFrustum[3] = Plane( kMatFrustum._14 - kMatFrustum._11, kMatFrustum._24 - kMatFrustum._21, kMatFrustum._34 - kMatFrustum._31, kMatFrustum._44 - kMatFrustum._41); // COMMENT : Top m_akFrustum[4] = Plane( kMatFrustum._14 - kMatFrustum._12, kMatFrustum._24 - kMatFrustum._22, kMatFrustum._34 - kMatFrustum._32, kMatFrustum._44 - kMatFrustum._42); // COMMENT : Bottom m_akFrustum[5] = Plane( kMatFrustum._14 + kMatFrustum._12, kMatFrustum._24 + kMatFrustum._22, kMatFrustum._34 + kMatFrustum._32, kMatFrustum._44 + kMatFrustum._42); m_akFrustum[0].kNormal.Normalize(); m_akFrustum[1].kNormal.Normalize(); m_akFrustum[2].kNormal.Normalize(); m_akFrustum[3].kNormal.Normalize(); m_akFrustum[4].kNormal.Normalize(); m_akFrustum[5].kNormal.Normalize(); }
const cMatrixf& cLight3DSpot::GetViewProjMatrix() { if(mlViewProjMatrixCount != GetTransformUpdateCount() || mbViewProjUpdated || mbProjectionUpdated) { m_mtxViewProj = cMath::MatrixMul(GetProjectionMatrix(),GetViewMatrix()); m_mtxViewProj = cMath::MatrixMul(g_mtxTextureUnitFix, m_mtxViewProj); mlViewProjMatrixCount = GetTransformUpdateCount(); mbViewProjUpdated = false; } return m_mtxViewProj; }
Vector3 Camera::ScreenNDCPointToWorld(const Vector2 &screenNDCPos, float zFromCamera) { Matrix4 p, v; GetProjectionMatrix(&p); GetViewMatrix(&v); // Pass coordinates to clip space, to invert them using projInversed Vector4 clipCoords = Vector4(screenNDCPos, 1.0, 1.0) * zFromCamera; Vector4 res4 = p.Inversed() * clipCoords; Vector3 res = res4.xyz(); res = (v.Inversed() * Vector4(res, 1.0f)).xyz(); return res; }
void Camera::MovingCamera(DirectX::XMFLOAT3 p_pos) { float moveX, moveY, centerX, centerY, posX, posY; centerX = GLOBAL::GetInstance().CURRENT_SCREEN_WIDTH * 0.5f; centerY = GLOBAL::GetInstance().CURRENT_SCREEN_HEIGHT * 0.5f; posX = InputManager::GetInstance()->GetMousePositionX() - centerX; posY = InputManager::GetInstance()->GetMousePositionY() - centerY; float procX = posX / 440; float procY = posY / 352; // 512 *0,68 //0.68 = 440/640; if (procX > 1.0) procX = 1.0; if (procX < -1.0) procX = -1.0; if (procY > 1.0) procY = 1.0; if (procY < -1.0) procY = -1.0; moveX = 8 * procX; moveY = 8 * procY; DirectX::XMFLOAT3 position, target, finalPos; DirectX::XMFLOAT3 playerPosition = p_pos; position = DirectX::XMFLOAT3(playerPosition.x + moveX, playerPosition.y + 30.0f, playerPosition.z - moveY - 15.0f); target = DirectX::XMFLOAT3(playerPosition.x + moveX, 0, playerPosition.z - moveY); DirectX::XMStoreFloat3(&finalPos, SmoothStep(DirectX::XMLoadFloat3(&m_oldPosition), DirectX::XMLoadFloat3(&position), 0.25f)); // Set max limits if (finalPos.x < -38) finalPos.x = -38; if (finalPos.x > 38) finalPos.x = 38; if (finalPos.z > 35) finalPos.z = 35; if (finalPos.z < -58) finalPos.z = -58; target = DirectX::XMFLOAT3(finalPos.x, 0, finalPos.z + 15.0f); UpdatePosition(finalPos); UpdateTarget(target); UpdateViewMatrix(); UpdateProjectionMatrix(false); GraphicsEngine::SetViewAndProjection(GetViewMatrix(), GetProjectionMatrix()); m_oldPosition = finalPos; SetOutliningRayPosition(finalPos); SetOutliningRayTarget(playerPosition); };
/** * @brief * Returns the absolute world view matrix of the scene node */ const Matrix4x4 &VisNode::GetWorldViewMatrix() const { // Recalculation required? if (m_nInternalFlags & RecalculateWorldView) { // Calculate the world view transform matrix m_mWorldView = GetViewMatrix()*m_mWorld; // Recalculation done m_nInternalFlags &= ~RecalculateWorldView; } // Return the matrix return m_mWorldView; }
cFrustum* cLight3DSpot::GetFrustum() { if(mlFrustumMatrixCount != GetTransformUpdateCount() || mbFrustumUpdated || mbProjectionUpdated) { mpFrustum->SetViewProjMatrix( GetProjectionMatrix(), GetViewMatrix(), mfFarAttenuation,mfNearClipPlane, mfFOV,mfAspect,GetWorldPosition(),false); mbFrustumUpdated = false; mlFrustumMatrixCount = GetTransformUpdateCount(); } return mpFrustum; }
Vector3f AbstractViewer::Project(const Nz::Vector3f& worldPosition) const { Vector4f pos4D(worldPosition, 1.f); pos4D = GetViewMatrix() * pos4D; pos4D = GetProjectionMatrix() * pos4D; pos4D /= pos4D.w; Rectf viewport = Rectf(GetViewport()); Nz::Vector3f screenPosition(pos4D.x * 0.5f + 0.5f, -pos4D.y * 0.5f + 0.5f, pos4D.z * 0.5f + 0.5f); screenPosition.x = screenPosition.x * viewport.width + viewport.x; screenPosition.y = screenPosition.y * viewport.height + viewport.y; return screenPosition; }
void My_TestGLDrawing::_SetPickParams() { HdxUnitTestUtils::PickParams pParams; pParams.pickRadius = GfVec2i(4,4); pParams.screenWidth = GetWidth(); pParams.screenHeight = GetHeight(); pParams.viewFrustum = GetFrustum(); pParams.viewMatrix = GetViewMatrix(); pParams.engine = &_engine; pParams.pickablesCol = &_pickablesCol; pParams.highlightMode = HdSelection::HighlightModeSelect; // unpickable occlusion is false by default. _picker.SetPickParams(pParams); }
//! Returns the camera frustum in world space. Frustum Camera::GetFrustum() { XMVECTOR scale, rotation, translation, detView; // The frustum is in view space, so we need to get the inverse view matrix // to transform it to world space. XMMATRIX invView = XMMatrixInverse(&detView, XMLoadFloat4x4(&GetViewMatrix())); // Decompose the inverse view matrix and transform the frustum with the components. XMMatrixDecompose(&scale, &rotation, &translation, invView); Frustum worldFrustum; TransformFrustum(&worldFrustum, &mFrustum, XMVectorGetX(scale), rotation, translation); // Return the transformed frustum that now is in world space. return worldFrustum; }
void My_TestGLDrawing:: DrawScene(PickParam const * pickParam) { int width = GetWidth(), height = GetHeight(); GfMatrix4d viewMatrix = GetViewMatrix(); GfFrustum frustum = GetFrustum(); GfVec4d viewport(0, 0, width, height); if (pickParam) { frustum = frustum.ComputeNarrowedFrustum( GfVec2d((2.0 * pickParam->location[0]) / width - 1.0, (2.0 * (height-pickParam->location[1])) / height - 1.0), GfVec2d(1.0 / width, 1.0 / height)); viewport = pickParam->viewport; } GfMatrix4d projMatrix = frustum.ComputeProjectionMatrix(); _delegate->SetCamera(viewMatrix, projMatrix); glViewport(viewport[0], viewport[1], viewport[2], viewport[3]); HdTaskSharedPtrVector tasks; SdfPath renderSetupTask("/renderSetupTask"); SdfPath renderTask("/renderTask"); tasks.push_back(_delegate->GetRenderIndex().GetTask(renderSetupTask)); tasks.push_back(_delegate->GetRenderIndex().GetTask(renderTask)); HdxRenderTaskParams param = _delegate->GetTaskParam( renderSetupTask, HdTokens->params).Get<HdxRenderTaskParams>(); param.enableIdRender = (pickParam != NULL); param.viewport = viewport; _delegate->SetTaskParam(renderSetupTask, HdTokens->params, VtValue(param)); glEnable(GL_DEPTH_TEST); glBindVertexArray(vao); _engine.Execute(_delegate->GetRenderIndex(), tasks); glBindVertexArray(0); }
void CMglMatrixManager::ConvertToScreenVector(D3DXVECTOR3 *pOut, CONST D3DXVECTOR3 *pInVector) { InitCheck(); _D3DVIEWPORTx vp; vp.X = 0; vp.Y = 0; vp.Width = m_myudg->GetWidth(); vp.Height = m_myudg->GetHeight(); vp.MinZ = 0.0f; vp.MaxZ = 1.0f; D3DXVec3Project(pOut, pInVector, &vp, &(GetProjectionMatrix()), &(GetViewMatrix()), +&(m_pWorldMgr->GetMatrix())); }
void CAMERA::Update(MOUSE &mouse, float timeDelta) { //Restrict focus movment to the xz-plane m_right.y = m_look.y = 0.0f; D3DXVec3Normalize(&m_look, &m_look); D3DXVec3Normalize(&m_right, &m_right); //Move Focus (i.e. Scroll) if(mouse.x < mouse.m_viewport.left + 10) Scroll(-m_right * timeDelta * 30.0f); if(mouse.x > mouse.m_viewport.right - 10) Scroll(m_right * timeDelta * 30.0f); if(mouse.y < mouse.m_viewport.top + 10) Scroll(m_look * timeDelta * 30.0f); if(mouse.y > mouse.m_viewport.bottom - 10) Scroll(-m_look * timeDelta * 30.0f); //Move Camera (i.e. Change Angle) if(KEYDOWN(VK_LEFT))Yaw(-timeDelta); if(KEYDOWN(VK_RIGHT))Yaw(timeDelta); if(KEYDOWN(VK_UP))Pitch(timeDelta); if(KEYDOWN(VK_DOWN))Pitch(-timeDelta); //Zoom (i.e. change fov) if(KEYDOWN(VK_ADD))Zoom(-timeDelta); if(KEYDOWN(VK_SUBTRACT))Zoom(timeDelta); //Change m_radius if(mouse.WheelUp()) ChangeRadius(-1.0f); if(mouse.WheelDown())ChangeRadius(1.0f); //Calculate Eye Position float sideRadius = m_radius * cos(m_beta); float height = m_radius * sin(m_beta); m_eye = D3DXVECTOR3(m_focus.x + sideRadius * cos(m_alpha), m_focus.y + height, m_focus.z + sideRadius * sin(m_alpha)); if(m_pDevice != NULL) { D3DXMATRIX view = GetViewMatrix(); D3DXMATRIX projection = GetProjectionMatrix(); m_pDevice->SetTransform(D3DTS_VIEW, &view); m_pDevice->SetTransform(D3DTS_PROJECTION, &projection); CalculateFrustum(view, projection); } }
void Ys3DDrawingEnvironment::TransformScreenCoordTo3DLine(YsVec3 &org,YsVec3 &vec,double sx,double sy) const { if(YSTRUE==screenOriginIsTopLeft) { sy=(double)windowHeight-sy; } const YsMatrix4x4 &viewMat=GetViewMatrix(); const YsMatrix4x4 &projMat=GetProjectionMatrix(); const YsMatrix4x4 projViewMat=projMat*viewMat; const YsVec3 p1(sx,sy,-1.0),p2(sx,sy,1.0); YsTransformScreenCoordTo3DCoord(org,p1,viewport,projViewMat); YsTransformScreenCoordTo3DCoord(vec,p2,viewport,projViewMat); vec-=org; vec.Normalize(); }
//! Updates the view matrix. void Camera::UpdateViewMatrix() { // Update the right vector. XMVECTOR up = XMLoadFloat3(&mUp); XMVECTOR dir = XMLoadFloat3(&GetDirection()); XMVECTOR right = XMVector3Cross(up, dir); right = XMVector3Normalize(right); XMStoreFloat3(&mRight, right); // Update the view matrix XMVECTOR pos = XMLoadFloat3(&mPosition); XMVECTOR target = XMLoadFloat3(&mTarget); XMStoreFloat4x4(&mView, XMMatrixLookAtLH(pos, target, up)); XMFLOAT4X4 viewProj; XMStoreFloat4x4(&viewProj, XMLoadFloat4x4(&GetViewMatrix()) * XMLoadFloat4x4(&GetProjectionMatrix())); mFrustum.BuildFromViewProjection(viewProj); }