Changeset 2780 for GTP/trunk/App/Demos/Vis/CHC_revisited/Camera.cpp
- Timestamp:
- 06/19/08 18:43:17 (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
GTP/trunk/App/Demos/Vis/CHC_revisited/Camera.cpp
r2779 r2780 7 7 { 8 8 9 using namespace std;10 11 9 12 10 Camera::Camera() … … 15 13 mHeight = 100; 16 14 mFovy = 90.0f * M_PI / 180.0f; 17 mIsOrtho = false;18 19 SetPosition(Vector3(0, 0, 0));20 21 mPitch = mYaw = 0;22 Precompute();23 15 } 24 16 … … 30 22 31 23 mFovy = fieldOfView * M_PI / 180.0f; 32 33 mIsOrtho = false;34 35 SetPosition(Vector3(0, 0, 0));36 37 SetPosition(Vector3(0, 0, 0));38 39 mPitch = mYaw = 0;40 Precompute();41 24 } 42 25 … … 44 27 void Camera::Precompute() 45 28 { 46 Vector3 direction = Vector3(0, 1, 0); 47 48 Vector3 side = CrossProd(Vector3(1, 0, 0), direction); 49 50 Vector3 up = -Normalize(CrossProd(side, direction)); 51 Vector3 right = -Normalize(CrossProd(direction, up)); 52 53 mBaseOrientation = Matrix4x4(right, up, direction); 54 mViewOrientation = mBaseOrientation; 55 cout<<"view: " << direction << " right: " << right << " up " << up << endl; 56 } 57 29 mDirection.Normalize(); 30 31 //Vector3 side = CrossProd(Vector3(0, 1, 0), mDirection); 32 Vector3 side = CrossProd(Vector3(0, 0, 1), mDirection); 33 34 mUp = -Normalize(CrossProd(side, mDirection)); 35 mRight = -Normalize(CrossProd(mDirection, mUp)); 36 37 38 float k = tan(mFovy/2); 39 mUp *= k; 40 mRight *= k*mWidth/mHeight; 41 } 58 42 59 43 … … 61 45 { 62 46 mPosition = pos; 47 Precompute(); 48 } 49 50 51 void Camera::SetDirection(const Vector3 &dir) 52 { 53 mDirection = dir; 54 Precompute(); 63 55 } 64 56 … … 67 59 { 68 60 mNear = nearDist; 61 } 62 63 64 void Camera::LookInBox(const AxisAlignedBox3 &box) 65 { 66 //mDirection = Vector3(0, 0, 1); 67 mDirection = Vector3(0, 1, 0); 68 mPosition = box.Center(); 69 mPosition.z = box.Min(2) + 0.9f * box.Size(2); 70 71 Precompute(); 72 } 73 74 75 void Camera::LookAtBox(const AxisAlignedBox3 &box) 76 { 77 mDirection = Vector3(0, box.Min().y - box.Max().y, 0); 78 mPosition = Vector3(0);//box.Max() - mDirection; 79 80 Precompute(); 69 81 } 70 82 … … 147 159 void Camera::SetupCameraView() 148 160 { 149 Matrix4x4 viewOrientation = mViewOrientation;150 Vector3 pos = -mPosition;151 pos = mViewOrientation * pos;152 153 viewOrientation.x[3][0] = pos.x;154 viewOrientation.x[3][1] = pos.y;155 viewOrientation.x[3][2] = pos.z;156 157 161 glLoadIdentity(); 158 glMultMatrixf((float *)viewOrientation.x); 162 gluLookAt(mPosition.x, mPosition.y, mPosition.z, 163 mPosition.x + mDirection.x, mPosition.y + mDirection.y, mPosition.z + mDirection.z, 164 mUp.x, mUp.y, mUp.z); 165 166 //std::cout << "dir: " << mDirection << " pos: " << mPosition << " up: " << mUp << std::endl; 159 167 } 160 168 … … 172 180 const float w_far = h_far * GetAspect(); 173 181 174 const Vector3 view = GetDirection();182 const Vector3 view = mDirection; 175 183 const Vector3 fc = mPosition + view * z_far; 176 184 177 const Vector3 up = GetUpVector();178 179 const Vector3 right = GetRightVector();185 const Vector3 up = mUp; 186 187 const Vector3 right = mRight; 180 188 181 189 Vector3 t1, t2; … … 189 197 fbr = fc - t1 + t2; 190 198 191 const Vector3 nc = mPosition + view* z_near;199 const Vector3 nc = mPosition + mDirection * z_near; 192 200 193 201 t1 = h_near * 0.5f * up; … … 201 209 202 210 203 void Camera::SetOrtho(bool ortho) 204 { 205 mIsOrtho = true; 206 } 207 208 209 void Camera::Yaw(float angle) 210 { 211 mYaw += angle; 212 CalculateFromPitchAndYaw(); 213 /* 214 Matrix4x4 viewOrientation(mRight, mUp, mDirection); 215 Matrix4x4 rot = RotationYMatrix(angle); 216 217 viewOrientation = viewOrientation * rot; 218 219 mDirection = Vector3(viewOrientation.x[2][0], viewOrientation.x[2][1], viewOrientation.x[2][2]); 220 mRight = Vector3(viewOrientation.x[0][0], viewOrientation.x[0][1], viewOrientation.x[0][2]); 221 mUp = Vector3(viewOrientation.x[1][0], viewOrientation.x[1][1], viewOrientation.x[1][2]);*/ 222 } 223 224 225 void Camera::Pitch(float angle) 226 { 227 mPitch += angle; 228 CalculateFromPitchAndYaw(); 229 } 230 231 232 void Camera::CalculateFromPitchAndYaw() 233 { 234 mViewOrientation = mBaseOrientation; 235 236 Matrix4x4 roty = RotationYMatrix(mPitch); 237 Matrix4x4 rotx = RotationXMatrix(mYaw); 238 239 mViewOrientation *= roty; 240 mViewOrientation *= rotx; 241 } 242 243 244 Vector3 Camera::GetDirection() const 245 { 246 return Vector3(mViewOrientation.x[0][2], mViewOrientation.x[1][2], mViewOrientation.x[2][2]); 247 } 248 249 250 Vector3 Camera::GetUpVector() const 251 { 252 return Vector3(mViewOrientation.x[0][1], mViewOrientation.x[1][1], mViewOrientation.x[2][1]); 253 } 254 255 256 Vector3 Camera::GetRightVector() const 257 { 258 return Vector3(mViewOrientation.x[0][0], mViewOrientation.x[1][0], mViewOrientation.x[2][0]); 259 260 } 261 262 263 } 264 211 } 212
Note: See TracChangeset
for help on using the changeset viewer.