Ticket #554: cameraConstraints.diff
File cameraConstraints.diff, 6.7 KB (added by , 14 years ago) |
---|
-
graphics/Terrain.cpp
94 94 } 95 95 96 96 /////////////////////////////////////////////////////////////////////////////// 97 // GetEdgeNearestPoint: get the nearest edge point close to the passed 98 // returns true if the point was outside the map 99 bool CTerrain::GetEdgeNearestPoint(float *x, float *z) const 100 { 101 debug_assert(x != NULL && z != NULL && "NULL parameters have been passed"); 97 102 103 // do nothing if the point is already on the map 104 if(IsOnMap(*x, *z)) 105 return false; 106 107 float min_x = GetMinX(); 108 float max_x = GetMaxX() - 1.0f / CELL_SIZE; 109 float min_z = GetMinZ(); 110 float max_z = GetMaxZ() - 1.0f / CELL_SIZE; 111 112 // x constraint 113 if(*x < min_x) 114 *x = min_x; 115 else if(*x > max_x) 116 *x = max_x; 117 118 // z constraint 119 if(*z < min_z) 120 *z = min_z; 121 else if(*z > max_z) 122 *z = max_z; 123 124 return true; 125 } 126 127 /////////////////////////////////////////////////////////////////////////////// 128 98 129 CStr8 CTerrain::GetMovementClass(ssize_t i, ssize_t j) const 99 130 { 100 131 CMiniPatch* tile = GetTile(i, j); -
graphics/Camera.h
82 82 // Get the point on the plane at height h corresponding to pixel (px,py) 83 83 CVector3D GetWorldCoordinates(int px, int py, float h) const; 84 84 // Get the point on the terrain the camera is pointing towards 85 CVector3D GetFocus( );85 CVector3D GetFocus(bool *is_on_map = NULL); 86 86 87 87 // Build an orientation matrix from camera position, camera focus point, and up-vector 88 88 void LookAt(const CVector3D& camera, const CVector3D& orientation, const CVector3D& up); -
graphics/GameView.cpp
60 60 const float CGameView::defaultNear = 4.f; 61 61 const float CGameView::defaultFar = 4096.f; 62 62 const float CGameView::defaultCullFOV = CGameView::defaultFOV + DEGTORAD(6.0f); //add 6 degrees to the default FOV for use with the culling frustum 63 const float CGameView::cameraPivotMargin = 20.0f; 63 64 64 65 /** 65 66 * A value with exponential decay towards the target value. … … 739 740 m->PosY.AddSmoothly(limit - nearPoint.Y); 740 741 } 741 742 */ 742 743 743 744 m->RotateY.Wrap(-(float)M_PI, (float)M_PI); 744 745 746 // Ensure the ViewCamera focus is inside the map with the chosen margins 747 // if not so - apply margins to the camera 748 if (m->ConstrainCamera) 749 { 750 CCamera targetCam = m->ViewCamera; 751 SetupCameraMatrix(m, &targetCam.m_Orientation); 752 CVector3D pivot = targetCam.m_Orientation.GetTranslation() + targetCam.m_Orientation.GetIn() * m->ViewZoomDefault; 753 ApplyCameraPivotMargins(pivot); 754 } 755 745 756 // Update the camera matrix 746 757 SetupCameraMatrix(m, &m->ViewCamera.m_Orientation); 747 758 m->ViewCamera.UpdateFrustum(); 748 759 } 749 760 761 // ApplyCameraPivotMargins: constraint camera pivot 762 // returns true if margins were applied to the pivot passed 763 bool CGameView::ApplyCameraPivotMargins(CVector3D &pivot) 764 { 765 bool margins_applied = false; 766 CTerrain *pter = m->Game->GetWorld()->GetTerrain(); 767 debug_assert(pter != NULL && "NULL terrain pointer"); 768 CVector3D old_pivot(pivot); 769 770 float min_x = pter->GetMinX() + cameraPivotMargin; 771 float max_x = pter->GetMaxX() - cameraPivotMargin; 772 float min_z = pter->GetMinZ() + cameraPivotMargin; 773 float max_z = pter->GetMaxZ() - cameraPivotMargin; 774 775 if(pivot.X < min_x) { pivot.X = min_x; margins_applied = true; } 776 else if(pivot.X > max_x) { pivot.X = max_x; margins_applied = true; } 777 if(pivot.Z < min_z) { pivot.Z = min_z; margins_applied = true; } 778 else if(pivot.Z > max_z) { pivot.Z = max_z; margins_applied = true; } 779 780 if(margins_applied) 781 { 782 m->PosX.Add(pivot.X - old_pivot.X); 783 m->PosZ.Add(pivot.Z - old_pivot.Z); 784 785 // doesn't need because of the fixed Y camera position 786 // pivot.Y = pter->GetExactGroundLevel(pivot.X, pivot.Z); 787 // m->PosY.Add(pivot.Y - old_pivot.Y); 788 } 789 790 return margins_applied; 791 } 792 750 793 void CGameView::MoveCameraTarget(const CVector3D& target) 751 794 { 752 795 // Maintain the same orientation and level of zoom, if we can -
graphics/Terrain.h
66 66 // return number of patches along edge of the terrain 67 67 ssize_t GetPatchesPerSide() const { return m_MapSizePatches; } 68 68 69 float GetMinX() const { return 0.0f; } 70 float GetMinZ() const { return 0.0f; } 71 float GetMaxX() const { return (float)((m_MapSize-1) * CELL_SIZE); } 72 float GetMaxZ() const { return (float)((m_MapSize-1) * CELL_SIZE); } 73 69 74 bool IsOnMap(float x, float z) const 70 75 { 71 return ((x >= 0.0f) && (x < (float)((m_MapSize-1) * CELL_SIZE))72 && (z >= 0.0f) && (z < (float)((m_MapSize-1) * CELL_SIZE)));76 return ((x >= GetMinX()) && (x < GetMaxX()) 77 && (z >= GetMinZ()) && (z < GetMaxZ())); 73 78 } 79 80 // GetEdgeNearestPoint: get the nearest edge point close to the passed 81 // returns true if the point was outside the map 82 bool GetEdgeNearestPoint(float *x, float *z) const; 74 83 75 84 CStr8 GetMovementClass(ssize_t i, ssize_t j) const; 76 85 -
graphics/GameView.h
44 44 static const float defaultFOV, defaultCullFOV, defaultNear, defaultFar; 45 45 46 46 private: 47 static const float cameraPivotMargin; 47 48 CGameViewImpl* m; 48 49 49 50 // Check whether lighting environment has changed and update vertex data if necessary … … 76 77 // *presentation* 77 78 void Update(float DeltaTime); 78 79 80 // ApplyCameraPivotMargins: constraint pivot 81 // returns true if margins were applied to the pivot passed 82 bool ApplyCameraPivotMargins(CVector3D &pivot); 83 79 84 // Render: Render the World 80 85 void Render(); 81 86 -
graphics/Camera.cpp
285 285 return CVector3D(0.f, h, 0.f); 286 286 } 287 287 288 CVector3D CCamera::GetFocus( )288 CVector3D CCamera::GetFocus(bool *is_on_map) 289 289 { 290 290 // Basically the same as GetWorldCoordinates 291 291 … … 298 298 dir = m_Orientation.GetIn(); 299 299 300 300 if (tracer.RayIntersect(origin, dir, x, z, currentTarget)) 301 { 302 if(is_on_map != NULL) *is_on_map = true; 301 303 return (currentTarget); 304 } 302 305 303 306 // Off the edge of the world? 304 307 // Work out where it /would/ hit, if the map were extended out to infinity with average height. 305 308 309 if(is_on_map != NULL) *is_on_map = false; 306 310 return (origin + dir * ((50.0f - origin.Y) / dir.Y)); 307 311 } 308 312