Changeset 2778 for GTP/trunk/App/Demos/Vis/CHC_revisited/Camera.cpp
- Timestamp:
- 06/19/08 18:07:09 (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
GTP/trunk/App/Demos/Vis/CHC_revisited/Camera.cpp
r2776 r2778 7 7 { 8 8 9 using namespace std; 10 9 11 10 12 Camera::Camera() … … 13 15 mHeight = 100; 14 16 mFovy = 90.0f * M_PI / 180.0f; 17 mIsOrtho = false; 18 19 SetPosition(Vector3(0, 0, 0)); 20 21 mPitch = mYaw = 0; 22 Precompute(); 15 23 } 16 24 … … 22 30 23 31 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(); 24 41 } 25 42 … … 27 44 void Camera::Precompute() 28 45 { 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 } 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 } 56 42 57 43 58 … … 45 60 { 46 61 mPosition = pos; 47 Precompute();48 }49 50 51 void Camera::SetDirection(const Vector3 &dir)52 {53 mDirection = dir;54 Precompute();55 62 } 56 63 … … 59 66 { 60 67 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();81 68 } 82 69 … … 159 146 void Camera::SetupCameraView() 160 147 { 148 #if 1 149 //Matrix4x4 viewOrientation(mRight, mDirection, mUp); 150 //Matrix4x4 viewOrientation(mRight, mUp, mDirection); 151 Matrix4x4 tview = mViewOrientation; 152 153 //Vector3 pos = tview * mPosition; 154 Vector3 pos = -mPosition; 155 pos = tview * pos; 156 157 Debug << "vieworient:\n" << mViewOrientation << " pos " << pos << " position " << mPosition << endl; 158 159 Matrix4x4 viewOrientation = mViewOrientation; 160 161 viewOrientation.x[3][0] = pos.x; 162 viewOrientation.x[3][1] = pos.y; 163 viewOrientation.x[3][2] = pos.z; 164 161 165 glLoadIdentity(); 166 glMultMatrixf((float *)viewOrientation.x); 167 //glMultMatrixf((float *)tm.x); 168 Debug << "matrix:\n" << viewOrientation << endl; 169 #else 170 glLoadIdentity(); 171 162 172 gluLookAt(mPosition.x, mPosition.y, mPosition.z, 163 173 mPosition.x + mDirection.x, mPosition.y + mDirection.y, mPosition.z + mDirection.z, 164 174 mUp.x, mUp.y, mUp.z); 165 175 #endif 166 176 //std::cout << "dir: " << mDirection << " pos: " << mPosition << " up: " << mUp << std::endl; 167 177 } … … 180 190 const float w_far = h_far * GetAspect(); 181 191 182 const Vector3 view = mDirection;192 const Vector3 view = GetDirection(); 183 193 const Vector3 fc = mPosition + view * z_far; 184 194 185 const Vector3 up = mUp;186 187 const Vector3 right = mRight;195 const Vector3 up = GetUpVector(); 196 197 const Vector3 right = GetRightVector(); 188 198 189 199 Vector3 t1, t2; … … 197 207 fbr = fc - t1 + t2; 198 208 199 const Vector3 nc = mPosition + mDirection* z_near;209 const Vector3 nc = mPosition + view * z_near; 200 210 201 211 t1 = h_near * 0.5f * up; … … 209 219 210 220 211 } 212 221 void Camera::SetOrtho(bool ortho) 222 { 223 mIsOrtho = true; 224 } 225 226 227 void Camera::Yaw(float angle) 228 { 229 mYaw += angle; 230 CalculateFromPitchAndYaw(); 231 /* 232 Matrix4x4 viewOrientation(mRight, mUp, mDirection); 233 Matrix4x4 rot = RotationYMatrix(angle); 234 235 viewOrientation = viewOrientation * rot; 236 237 mDirection = Vector3(viewOrientation.x[2][0], viewOrientation.x[2][1], viewOrientation.x[2][2]); 238 mRight = Vector3(viewOrientation.x[0][0], viewOrientation.x[0][1], viewOrientation.x[0][2]); 239 mUp = Vector3(viewOrientation.x[1][0], viewOrientation.x[1][1], viewOrientation.x[1][2]);*/ 240 } 241 242 243 void Camera::Pitch(float angle) 244 { 245 mPitch += angle; 246 CalculateFromPitchAndYaw(); 247 } 248 249 250 void Camera::CalculateFromPitchAndYaw() 251 { 252 mViewOrientation = mBaseOrientation; 253 254 Matrix4x4 roty = RotationYMatrix(mPitch); 255 Matrix4x4 rotx = RotationXMatrix(mYaw); 256 257 mViewOrientation *= roty; 258 mViewOrientation *= rotx; 259 } 260 261 262 Vector3 Camera::GetDirection() const 263 { 264 return Vector3(mViewOrientation.x[0][2], mViewOrientation.x[1][2], mViewOrientation.x[2][2]); 265 } 266 267 268 Vector3 Camera::GetUpVector() const 269 { 270 return Vector3(mViewOrientation.x[0][1], mViewOrientation.x[1][1], mViewOrientation.x[2][1]); 271 } 272 273 274 Vector3 Camera::GetRightVector() const 275 { 276 return Vector3(mViewOrientation.x[0][0], mViewOrientation.x[1][0], mViewOrientation.x[2][0]); 277 278 } 279 280 281 } 282
Note: See TracChangeset
for help on using the changeset viewer.