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