[1378] | 1 | #include "dxstdafx.h"
|
---|
| 2 | #include ".\camera.h"
|
---|
| 3 | #include "fmod.h"
|
---|
| 4 | #include "GameManager.h"
|
---|
| 5 |
|
---|
| 6 | Camera::Camera(void) : Node() {
|
---|
| 7 | angle = D3DX_PI/4;
|
---|
| 8 | nearClipping = 0.1f;
|
---|
| 9 | farClipping = 1000.0f;
|
---|
| 10 | ratio = 4.0f/3.0f;
|
---|
| 11 | updateClippingPlanes();
|
---|
| 12 | this->setPosition(0, 50, -15);
|
---|
| 13 | this->setLookAtTarget(0.0f, 0.0f, 0.0f);
|
---|
| 14 | this->setUpVector(0.0f, 1.0f, 0.0f);
|
---|
| 15 | D3DXMatrixIdentity(&myViewMatrix);
|
---|
| 16 | this->nodeType |= Scene::NODE_CAMERA;
|
---|
| 17 | D3DXMatrixPerspectiveFovLH( &matProj, this->angle, this->ratio, this->nearClipping, this->farClipping );
|
---|
| 18 | }
|
---|
| 19 |
|
---|
| 20 | Camera::~Camera(void) {};
|
---|
| 21 |
|
---|
| 22 | void Camera::setAngle(float _angle) {
|
---|
| 23 | angle = _angle;
|
---|
| 24 | updateClippingPlanes();
|
---|
| 25 | }
|
---|
| 26 | float Camera::getAngle() {
|
---|
| 27 | return angle;
|
---|
| 28 | }
|
---|
| 29 |
|
---|
| 30 | void Camera::setNearClipping(float _nearClipping) {
|
---|
| 31 | nearClipping = _nearClipping;
|
---|
| 32 | updateClippingPlanes();
|
---|
| 33 | }
|
---|
| 34 |
|
---|
| 35 | float Camera::getNearClipping() {
|
---|
| 36 | return nearClipping;
|
---|
| 37 | }
|
---|
| 38 |
|
---|
| 39 | void Camera::setFarClipping(float _farClipping) {
|
---|
| 40 | farClipping = _farClipping;
|
---|
| 41 | updateClippingPlanes();
|
---|
| 42 | }
|
---|
| 43 | float Camera::getFarClipping() {
|
---|
| 44 | return farClipping;
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 | void Camera::setRatio(float _ratio) {
|
---|
| 48 | ratio = _ratio;
|
---|
| 49 | updateClippingPlanes();
|
---|
| 50 | }
|
---|
| 51 | float Camera::getRatio() {
|
---|
| 52 | return ratio;
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | void Camera::getAbsoluteVector(Vector &pOut, Vector &pIn)
|
---|
| 56 | {
|
---|
| 57 | //D3DXVECTOR4 temp;
|
---|
| 58 | D3DXMATRIXA16 invViewMatrix;
|
---|
| 59 | D3DXMatrixInverse(&invViewMatrix, NULL, &this->myViewMatrix);
|
---|
| 60 | D3DXVec4Transform(&pOut, &pIn, &invViewMatrix);
|
---|
| 61 | //Vector v(temp.x, temp.y, temp.z);
|
---|
| 62 | //return v;
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | void Camera::calcWorldMatrix(D3DXMATRIX &pMatWorld)
|
---|
| 66 | {
|
---|
| 67 | /*D3DXVECTOR4 temp;
|
---|
| 68 | D3DXVec4Transform(&temp, &p, &pMatWorld);
|
---|
| 69 | Vector v(temp.x, temp.y, temp.z);*/
|
---|
| 70 |
|
---|
| 71 | D3DXVECTOR3 tLookAt;
|
---|
| 72 | tLookAt.x = this->lookAtTarget.x;
|
---|
| 73 | tLookAt.y = this->lookAtTarget.y;
|
---|
| 74 | tLookAt.z = this->lookAtTarget.z;
|
---|
| 75 | D3DXVECTOR3 tPos;
|
---|
| 76 | tPos.x = this->myPosition.x;
|
---|
| 77 | tPos.y = this->myPosition.y;
|
---|
| 78 | tPos.z = this->myPosition.z;
|
---|
| 79 | D3DXMatrixLookAtLH(&this->myViewMatrix, &tPos, &tLookAt, &this->upVector);
|
---|
| 80 | this->getAbsoluteVector(this->myAbsPos, Vector(0, 0, 0));
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | void Camera::update(float dt)
|
---|
| 84 | {
|
---|
| 85 | Vector apos = this->getAbsolutePosition();
|
---|
| 86 | Vector vel = apos-oldPos;
|
---|
| 87 | float p[3];
|
---|
| 88 | float v[3];
|
---|
| 89 | p[0] = apos.x; p[1] = apos.y; p[2] = apos.z;
|
---|
| 90 | vel = vel*dt;
|
---|
| 91 | v[0] = vel.x; v[1] = vel.y; v[2] = vel.z;
|
---|
| 92 |
|
---|
| 93 | //Vector forwardV = this->getAbsoluteVector(Vector(0, 0, 1)) - this->getAbsolutePosition();
|
---|
| 94 | Vector forwardV(0, 0, 1);
|
---|
| 95 | this->getAbsoluteDirectionVector(forwardV, forwardV);
|
---|
| 96 | forwardV.normalize();
|
---|
| 97 | Vector upV(0,1,0);
|
---|
| 98 | //= this->getAbsoluteVector(Vector(0, 1, 0)) - this->getAbsolutePosition();
|
---|
| 99 | this->getAbsoluteDirectionVector(upV, upV);
|
---|
| 100 | upV.normalize();
|
---|
| 101 | FSOUND_3D_Listener_SetAttributes(&p[0], &v[0],
|
---|
| 102 | forwardV.x, forwardV.y, forwardV.z,
|
---|
| 103 | upV.x, upV.y, upV.z);
|
---|
| 104 |
|
---|
| 105 | oldPos = apos;
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | void Camera::setUpVector(Vector &uv)
|
---|
| 109 | {
|
---|
| 110 | this->upVector.x = uv.x;
|
---|
| 111 | this->upVector.y = uv.y;
|
---|
| 112 | this->upVector.z = uv.z;
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 | void Camera::setUpVector(float x, float y, float z)
|
---|
| 116 | {
|
---|
| 117 | this->upVector.x = x;
|
---|
| 118 | this->upVector.y = y;
|
---|
| 119 | this->upVector.z = z;
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 | D3DXMATRIX* Camera::getViewMatrix()
|
---|
| 123 | {
|
---|
| 124 | return &this->myViewMatrix;
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 | void Camera::setViewMatrix(D3DXMATRIX &_viewMat) {
|
---|
| 128 | this->myViewMatrix = _viewMat;
|
---|
| 129 | }
|
---|
| 130 |
|
---|
| 131 | void Camera::setLookAtTarget(Vector &p)
|
---|
| 132 | {
|
---|
| 133 | this->lookAtTarget = p;
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 | void Camera::setLookAtTarget(float x, float y, float z)
|
---|
| 137 | {
|
---|
| 138 | this->lookAtTarget.x = x;
|
---|
| 139 | this->lookAtTarget.y = y;
|
---|
| 140 | this->lookAtTarget.z = z;
|
---|
| 141 | this->lookAtTarget.w = 1;
|
---|
| 142 | }
|
---|
| 143 |
|
---|
| 144 | Vector Camera::getLookAtTarget() {
|
---|
| 145 | return this->lookAtTarget;
|
---|
| 146 | }
|
---|
| 147 |
|
---|
| 148 | void Camera::setupProjectionMatrix() //Setzt auch gleich in directX die ProjectionsMatrix!!!
|
---|
| 149 | {
|
---|
| 150 | D3DXMatrixPerspectiveFovLH( &matProj, this->angle, this->ratio, this->nearClipping, this->farClipping );
|
---|
| 151 | this->myScene->device->SetTransform( D3DTS_PROJECTION, &matProj );
|
---|
| 152 | this->updateProjection = false;
|
---|
| 153 | }
|
---|
| 154 |
|
---|
| 155 | void Camera::setProjectionMatrix(D3DXMATRIX &_projMat) {
|
---|
| 156 | this->matProj = _projMat;
|
---|
| 157 | }
|
---|
| 158 |
|
---|
| 159 | D3DXMATRIX* Camera::getProjectionMatrix()
|
---|
| 160 | {
|
---|
| 161 | return &this->matProj;
|
---|
| 162 | }
|
---|
| 163 |
|
---|
| 164 | /*void Camera::translate(float dx, float dy, float dz) {
|
---|
| 165 | //MessageBox(NULL,"Translate in Camera!", "Wuermer DEBUG",MB_OK|MB_ICONQUESTION);
|
---|
| 166 | pos.x -= dx;
|
---|
| 167 | pos.y -= dy;
|
---|
| 168 | pos.z -= dz;
|
---|
| 169 | }
|
---|
| 170 |
|
---|
| 171 | void Camera::rotate(float dalpha, float dbeta, float dgamma) {
|
---|
| 172 | rot.x -= dalpha;
|
---|
| 173 | rot.y -= dbeta;
|
---|
| 174 | rot.z -= dgamma;
|
---|
| 175 | }
|
---|
| 176 |
|
---|
| 177 | void Camera::setPosition(float x, float y, float z) {
|
---|
| 178 | pos.x = -x;
|
---|
| 179 | pos.y = -y;
|
---|
| 180 | pos.z = -z;
|
---|
| 181 | }
|
---|
| 182 |
|
---|
| 183 | void Camera::setRotation(float alpha, float beta, float gamma) {
|
---|
| 184 | rot.x = -alpha;
|
---|
| 185 | rot.y = -beta;
|
---|
| 186 | rot.z = -gamma;
|
---|
| 187 | }
|
---|
| 188 |
|
---|
| 189 | void Camera::calcTransformations() {
|
---|
| 190 | glPushMatrix();
|
---|
| 191 | glLoadIdentity();
|
---|
| 192 | glMultMatrixf(this->world.getValues());
|
---|
| 193 | for(unsigned int i=0;i<children.size();i++) {
|
---|
| 194 | //MessageBox(NULL,"Camera has children!", "Wuermer DEBUG",MB_OK|MB_ICONQUESTION);
|
---|
| 195 | children.at(i)->calcTransformations();
|
---|
| 196 | }
|
---|
| 197 | glPopMatrix();
|
---|
| 198 | }
|
---|
| 199 |
|
---|
| 200 | void Camera::doViewingTransformation() {
|
---|
| 201 | Vector axis;
|
---|
| 202 | float angle=0;
|
---|
| 203 |
|
---|
| 204 |
|
---|
| 205 | glLoadIdentity();
|
---|
| 206 |
|
---|
| 207 | //Do rotation
|
---|
| 208 | this->curRot = this->curRot + rot;
|
---|
| 209 | rotQuat.eulerToQuat(rot.x, rot.y, rot.z);
|
---|
| 210 | rotQuat.getAxis(axis, angle);
|
---|
| 211 | glRotatef(angle, axis.x, axis.y, axis.z);
|
---|
| 212 |
|
---|
| 213 | //Do translation
|
---|
| 214 | glTranslatef(pos.x, pos.y, pos.z);
|
---|
| 215 |
|
---|
| 216 | glMultMatrixf(this->world.getValues());
|
---|
| 217 |
|
---|
| 218 | glGetFloatv(GL_MODELVIEW_MATRIX, this->world.getValues());
|
---|
| 219 |
|
---|
| 220 | pos.x = 0;
|
---|
| 221 | pos.y = 0;
|
---|
| 222 | pos.z = 0;
|
---|
| 223 |
|
---|
| 224 | rot.x = 0;
|
---|
| 225 | rot.y = 0;
|
---|
| 226 | rot.z = 0;
|
---|
| 227 | }*/
|
---|
| 228 |
|
---|
| 229 | Plane Camera::getNearPlane() {
|
---|
| 230 | return this->transformPlane(this->pNear);
|
---|
| 231 | }
|
---|
| 232 |
|
---|
| 233 | Plane Camera::getFarPlane() {
|
---|
| 234 | return this->transformPlane(this->pFar);
|
---|
| 235 | }
|
---|
| 236 |
|
---|
| 237 | Plane Camera::getLeftPlane() {
|
---|
| 238 | return this->transformPlane(this->pLeft);
|
---|
| 239 | }
|
---|
| 240 |
|
---|
| 241 | Plane Camera::getRightPlane() {
|
---|
| 242 | return this->transformPlane(this->pRight);
|
---|
| 243 | }
|
---|
| 244 |
|
---|
| 245 | Plane Camera::getTopPlane() {
|
---|
| 246 | return this->transformPlane(this->pTop);
|
---|
| 247 | }
|
---|
| 248 |
|
---|
| 249 | Plane Camera::getBottomPlane() {
|
---|
| 250 | return this->transformPlane(this->pBottom);
|
---|
| 251 | }
|
---|
| 252 |
|
---|
| 253 | Plane Camera::transformPlane(Plane &plane) {
|
---|
| 254 | Plane temp;
|
---|
| 255 |
|
---|
| 256 | Vector tPoint;
|
---|
| 257 | Vector tNormal;
|
---|
| 258 | Vector tOrigin;
|
---|
| 259 | tPoint = plane.getPoint();
|
---|
| 260 | tNormal = plane.getNormal();
|
---|
| 261 |
|
---|
| 262 | D3DXMATRIX invMat;
|
---|
| 263 | D3DXMatrixInverse(&invMat, NULL, &this->myViewMatrix);
|
---|
| 264 |
|
---|
| 265 | D3DXVec4Transform(&tPoint, &tPoint, &invMat);
|
---|
| 266 | D3DXVec4Transform(&tNormal, &tNormal, &invMat);
|
---|
| 267 | D3DXVec4Transform(&tOrigin, &tOrigin, &invMat);
|
---|
| 268 |
|
---|
| 269 | tNormal = tNormal - tOrigin;
|
---|
| 270 | tNormal.normalize();
|
---|
| 271 | temp.setPoint(tPoint);
|
---|
| 272 | temp.setNormal(tNormal);
|
---|
| 273 |
|
---|
| 274 | return temp;
|
---|
| 275 | }
|
---|
| 276 |
|
---|
| 277 | Node* Camera::clone()
|
---|
| 278 | {
|
---|
| 279 | //this->myScene->manager->printToConsole("Camera.clone not implemented!");
|
---|
| 280 | return NULL;
|
---|
| 281 | }
|
---|
| 282 |
|
---|
| 283 | void Camera::updateClippingPlanes() {
|
---|
| 284 | this->updateProjection = true;
|
---|
| 285 | //TODO!!!!!
|
---|
| 286 | Vector tPoint;
|
---|
| 287 | Vector tNormal;
|
---|
| 288 | D3DXMATRIX tMatrix;
|
---|
| 289 | float tAngle;
|
---|
| 290 |
|
---|
| 291 | //near
|
---|
| 292 | tPoint.x = 0;
|
---|
| 293 | tPoint.y = 0;
|
---|
| 294 | tPoint.z = this->nearClipping;
|
---|
| 295 | tNormal.x = 0;
|
---|
| 296 | tNormal.y = 0;
|
---|
| 297 | tNormal.z = 1;
|
---|
| 298 | this->pNear.setPoint(tPoint);
|
---|
| 299 | this->pNear.setNormal(tNormal);
|
---|
| 300 |
|
---|
| 301 | //far
|
---|
| 302 | tPoint.x = 0;
|
---|
| 303 | tPoint.y = 0;
|
---|
| 304 | tPoint.z = this->farClipping;
|
---|
| 305 | tNormal.x = 0;
|
---|
| 306 | tNormal.y = 0;
|
---|
| 307 | tNormal.z = -1;
|
---|
| 308 | this->pFar.setPoint(tPoint);
|
---|
| 309 | this->pFar.setNormal(tNormal);
|
---|
| 310 |
|
---|
| 311 | //left
|
---|
| 312 | tPoint.x = 0;
|
---|
| 313 | tPoint.y = 0;
|
---|
| 314 | tPoint.z = 0;
|
---|
| 315 | tNormal.x = -1;
|
---|
| 316 | tNormal.y = 0;
|
---|
| 317 | tNormal.z = 0;
|
---|
| 318 |
|
---|
| 319 | //tAngle = this->angle*1.4/2*D3DX_PI/180;
|
---|
| 320 | tAngle = (float)(this->angle*1.4/2);
|
---|
| 321 | D3DXMatrixRotationY(&tMatrix, tAngle);
|
---|
| 322 | D3DXVec4Transform(&tNormal, &tNormal, &tMatrix);
|
---|
| 323 | this->pLeft.setPoint(tPoint);
|
---|
| 324 | this->pLeft.setNormal(tNormal);
|
---|
| 325 |
|
---|
| 326 | //right
|
---|
| 327 | tPoint.x = 0;
|
---|
| 328 | tPoint.y = 0;
|
---|
| 329 | tPoint.z = 0;
|
---|
| 330 | tNormal.x = 1;
|
---|
| 331 | tNormal.y = 0;
|
---|
| 332 | tNormal.z = 0;
|
---|
| 333 |
|
---|
| 334 | //tAngle = -this->angle*1.4/2*D3DX_PI/180;
|
---|
| 335 | tAngle = (float)(-this->angle*1.4/2);
|
---|
| 336 | D3DXMatrixRotationY(&tMatrix, tAngle);
|
---|
| 337 | D3DXVec4Transform(&tNormal, &tNormal, &tMatrix);
|
---|
| 338 | this->pRight.setPoint(tPoint);
|
---|
| 339 | this->pRight.setNormal(tNormal);
|
---|
| 340 |
|
---|
| 341 | //top
|
---|
| 342 | tPoint.x = 0;
|
---|
| 343 | tPoint.y = 0;
|
---|
| 344 | tPoint.z = 0;
|
---|
| 345 | tNormal.x = 0;
|
---|
| 346 | tNormal.y = -1;
|
---|
| 347 | tNormal.z = 0;
|
---|
| 348 |
|
---|
| 349 | tAngle = this->angle/2*D3DX_PI/180;
|
---|
| 350 | D3DXMatrixRotationY(&tMatrix, tAngle);
|
---|
| 351 | D3DXVec4Transform(&tNormal, &tNormal, &tMatrix);
|
---|
| 352 | this->pTop.setPoint(tPoint);
|
---|
| 353 | this->pTop.setNormal(tNormal);
|
---|
| 354 |
|
---|
| 355 | //bottom
|
---|
| 356 | tPoint.x = 0;
|
---|
| 357 | tPoint.y = 0;
|
---|
| 358 | tPoint.z = 0;
|
---|
| 359 | tNormal.x = 0;
|
---|
| 360 | tNormal.y = 1;
|
---|
| 361 | tNormal.z = 0;
|
---|
| 362 | //tMatrix.setIdentity();
|
---|
| 363 | tAngle = -this->angle/2*D3DX_PI/180;
|
---|
| 364 | //tAngle = asin(sin(tAngle)/ratio)+M_PI;
|
---|
| 365 | D3DXMatrixRotationY(&tMatrix, tAngle);
|
---|
| 366 | D3DXVec4Transform(&tNormal, &tNormal, &tMatrix);
|
---|
| 367 | this->pBottom.setPoint(tPoint);
|
---|
| 368 | this->pBottom.setNormal(tNormal);
|
---|
| 369 | }
|
---|
| 370 |
|
---|
| 371 | /*Vector Camera::transformCameraToWorld(Vector p) {
|
---|
| 372 | Vector cPos;
|
---|
| 373 |
|
---|
| 374 | cPos.x = 0; cPos.y = 0; cPos.z = 0;
|
---|
| 375 | cPos = this->world.transform(cPos);
|
---|
| 376 | p = p - curPos;
|
---|
| 377 |
|
---|
| 378 | Matrix inv = this->world.invert();
|
---|
| 379 | p = inv.transform(p);
|
---|
| 380 | return p;
|
---|
| 381 | }*/
|
---|
| 382 |
|
---|
| 383 | /*Vector Camera::transformCameraToWorld(float x, float y, float z) {
|
---|
| 384 | Vector p;
|
---|
| 385 | p.x = x;
|
---|
| 386 | p.y = y;
|
---|
| 387 | p.z = z;
|
---|
| 388 |
|
---|
| 389 | return this->transformCameraToWorld(p);
|
---|
| 390 | }*/ |
---|