#include "dxstdafx.h" #include ".\camera.h" #include "fmod.h" #include "GameManager.h" Camera::Camera(void) : Node() { angle = D3DX_PI/4; nearClipping = 0.1f; farClipping = 1000.0f; ratio = 4.0f/3.0f; updateClippingPlanes(); this->setPosition(0, 50, -15); this->setLookAtTarget(0.0f, 0.0f, 0.0f); this->setUpVector(0.0f, 1.0f, 0.0f); D3DXMatrixIdentity(&myViewMatrix); this->nodeType |= Scene::NODE_CAMERA; D3DXMatrixPerspectiveFovLH( &matProj, this->angle, this->ratio, this->nearClipping, this->farClipping ); } Camera::~Camera(void) {}; void Camera::setAngle(float _angle) { angle = _angle; updateClippingPlanes(); } float Camera::getAngle() { return angle; } void Camera::setNearClipping(float _nearClipping) { nearClipping = _nearClipping; updateClippingPlanes(); } float Camera::getNearClipping() { return nearClipping; } void Camera::setFarClipping(float _farClipping) { farClipping = _farClipping; updateClippingPlanes(); } float Camera::getFarClipping() { return farClipping; } void Camera::setRatio(float _ratio) { ratio = _ratio; updateClippingPlanes(); } float Camera::getRatio() { return ratio; } void Camera::getAbsoluteVector(Vector &pOut, Vector &pIn) { //D3DXVECTOR4 temp; D3DXMATRIXA16 invViewMatrix; D3DXMatrixInverse(&invViewMatrix, NULL, &this->myViewMatrix); D3DXVec4Transform(&pOut, &pIn, &invViewMatrix); //Vector v(temp.x, temp.y, temp.z); //return v; } void Camera::calcWorldMatrix(D3DXMATRIX &pMatWorld) { /*D3DXVECTOR4 temp; D3DXVec4Transform(&temp, &p, &pMatWorld); Vector v(temp.x, temp.y, temp.z);*/ D3DXVECTOR3 tLookAt; tLookAt.x = this->lookAtTarget.x; tLookAt.y = this->lookAtTarget.y; tLookAt.z = this->lookAtTarget.z; D3DXVECTOR3 tPos; tPos.x = this->myPosition.x; tPos.y = this->myPosition.y; tPos.z = this->myPosition.z; D3DXMatrixLookAtLH(&this->myViewMatrix, &tPos, &tLookAt, &this->upVector); this->getAbsoluteVector(this->myAbsPos, Vector(0, 0, 0)); } void Camera::update(float dt) { Vector apos = this->getAbsolutePosition(); Vector vel = apos-oldPos; float p[3]; float v[3]; p[0] = apos.x; p[1] = apos.y; p[2] = apos.z; vel = vel*dt; v[0] = vel.x; v[1] = vel.y; v[2] = vel.z; //Vector forwardV = this->getAbsoluteVector(Vector(0, 0, 1)) - this->getAbsolutePosition(); Vector forwardV(0, 0, 1); this->getAbsoluteDirectionVector(forwardV, forwardV); forwardV.normalize(); Vector upV(0,1,0); //= this->getAbsoluteVector(Vector(0, 1, 0)) - this->getAbsolutePosition(); this->getAbsoluteDirectionVector(upV, upV); upV.normalize(); FSOUND_3D_Listener_SetAttributes(&p[0], &v[0], forwardV.x, forwardV.y, forwardV.z, upV.x, upV.y, upV.z); oldPos = apos; } void Camera::setUpVector(Vector &uv) { this->upVector.x = uv.x; this->upVector.y = uv.y; this->upVector.z = uv.z; } void Camera::setUpVector(float x, float y, float z) { this->upVector.x = x; this->upVector.y = y; this->upVector.z = z; } D3DXMATRIX* Camera::getViewMatrix() { return &this->myViewMatrix; } void Camera::setViewMatrix(D3DXMATRIX &_viewMat) { this->myViewMatrix = _viewMat; } void Camera::setLookAtTarget(Vector &p) { this->lookAtTarget = p; } void Camera::setLookAtTarget(float x, float y, float z) { this->lookAtTarget.x = x; this->lookAtTarget.y = y; this->lookAtTarget.z = z; this->lookAtTarget.w = 1; } Vector Camera::getLookAtTarget() { return this->lookAtTarget; } void Camera::setupProjectionMatrix() //Setzt auch gleich in directX die ProjectionsMatrix!!! { D3DXMatrixPerspectiveFovLH( &matProj, this->angle, this->ratio, this->nearClipping, this->farClipping ); this->myScene->device->SetTransform( D3DTS_PROJECTION, &matProj ); this->updateProjection = false; } void Camera::setProjectionMatrix(D3DXMATRIX &_projMat) { this->matProj = _projMat; } D3DXMATRIX* Camera::getProjectionMatrix() { return &this->matProj; } /*void Camera::translate(float dx, float dy, float dz) { //MessageBox(NULL,"Translate in Camera!", "Wuermer DEBUG",MB_OK|MB_ICONQUESTION); pos.x -= dx; pos.y -= dy; pos.z -= dz; } void Camera::rotate(float dalpha, float dbeta, float dgamma) { rot.x -= dalpha; rot.y -= dbeta; rot.z -= dgamma; } void Camera::setPosition(float x, float y, float z) { pos.x = -x; pos.y = -y; pos.z = -z; } void Camera::setRotation(float alpha, float beta, float gamma) { rot.x = -alpha; rot.y = -beta; rot.z = -gamma; } void Camera::calcTransformations() { glPushMatrix(); glLoadIdentity(); glMultMatrixf(this->world.getValues()); for(unsigned int i=0;icalcTransformations(); } glPopMatrix(); } void Camera::doViewingTransformation() { Vector axis; float angle=0; glLoadIdentity(); //Do rotation this->curRot = this->curRot + rot; rotQuat.eulerToQuat(rot.x, rot.y, rot.z); rotQuat.getAxis(axis, angle); glRotatef(angle, axis.x, axis.y, axis.z); //Do translation glTranslatef(pos.x, pos.y, pos.z); glMultMatrixf(this->world.getValues()); glGetFloatv(GL_MODELVIEW_MATRIX, this->world.getValues()); pos.x = 0; pos.y = 0; pos.z = 0; rot.x = 0; rot.y = 0; rot.z = 0; }*/ Plane Camera::getNearPlane() { return this->transformPlane(this->pNear); } Plane Camera::getFarPlane() { return this->transformPlane(this->pFar); } Plane Camera::getLeftPlane() { return this->transformPlane(this->pLeft); } Plane Camera::getRightPlane() { return this->transformPlane(this->pRight); } Plane Camera::getTopPlane() { return this->transformPlane(this->pTop); } Plane Camera::getBottomPlane() { return this->transformPlane(this->pBottom); } Plane Camera::transformPlane(Plane &plane) { Plane temp; Vector tPoint; Vector tNormal; Vector tOrigin; tPoint = plane.getPoint(); tNormal = plane.getNormal(); D3DXMATRIX invMat; D3DXMatrixInverse(&invMat, NULL, &this->myViewMatrix); D3DXVec4Transform(&tPoint, &tPoint, &invMat); D3DXVec4Transform(&tNormal, &tNormal, &invMat); D3DXVec4Transform(&tOrigin, &tOrigin, &invMat); tNormal = tNormal - tOrigin; tNormal.normalize(); temp.setPoint(tPoint); temp.setNormal(tNormal); return temp; } Node* Camera::clone() { //this->myScene->manager->printToConsole("Camera.clone not implemented!"); return NULL; } void Camera::updateClippingPlanes() { this->updateProjection = true; //TODO!!!!! Vector tPoint; Vector tNormal; D3DXMATRIX tMatrix; float tAngle; //near tPoint.x = 0; tPoint.y = 0; tPoint.z = this->nearClipping; tNormal.x = 0; tNormal.y = 0; tNormal.z = 1; this->pNear.setPoint(tPoint); this->pNear.setNormal(tNormal); //far tPoint.x = 0; tPoint.y = 0; tPoint.z = this->farClipping; tNormal.x = 0; tNormal.y = 0; tNormal.z = -1; this->pFar.setPoint(tPoint); this->pFar.setNormal(tNormal); //left tPoint.x = 0; tPoint.y = 0; tPoint.z = 0; tNormal.x = -1; tNormal.y = 0; tNormal.z = 0; //tAngle = this->angle*1.4/2*D3DX_PI/180; tAngle = (float)(this->angle*1.4/2); D3DXMatrixRotationY(&tMatrix, tAngle); D3DXVec4Transform(&tNormal, &tNormal, &tMatrix); this->pLeft.setPoint(tPoint); this->pLeft.setNormal(tNormal); //right tPoint.x = 0; tPoint.y = 0; tPoint.z = 0; tNormal.x = 1; tNormal.y = 0; tNormal.z = 0; //tAngle = -this->angle*1.4/2*D3DX_PI/180; tAngle = (float)(-this->angle*1.4/2); D3DXMatrixRotationY(&tMatrix, tAngle); D3DXVec4Transform(&tNormal, &tNormal, &tMatrix); this->pRight.setPoint(tPoint); this->pRight.setNormal(tNormal); //top tPoint.x = 0; tPoint.y = 0; tPoint.z = 0; tNormal.x = 0; tNormal.y = -1; tNormal.z = 0; tAngle = this->angle/2*D3DX_PI/180; D3DXMatrixRotationY(&tMatrix, tAngle); D3DXVec4Transform(&tNormal, &tNormal, &tMatrix); this->pTop.setPoint(tPoint); this->pTop.setNormal(tNormal); //bottom tPoint.x = 0; tPoint.y = 0; tPoint.z = 0; tNormal.x = 0; tNormal.y = 1; tNormal.z = 0; //tMatrix.setIdentity(); tAngle = -this->angle/2*D3DX_PI/180; //tAngle = asin(sin(tAngle)/ratio)+M_PI; D3DXMatrixRotationY(&tMatrix, tAngle); D3DXVec4Transform(&tNormal, &tNormal, &tMatrix); this->pBottom.setPoint(tPoint); this->pBottom.setNormal(tNormal); } /*Vector Camera::transformCameraToWorld(Vector p) { Vector cPos; cPos.x = 0; cPos.y = 0; cPos.z = 0; cPos = this->world.transform(cPos); p = p - curPos; Matrix inv = this->world.invert(); p = inv.transform(p); return p; }*/ /*Vector Camera::transformCameraToWorld(float x, float y, float z) { Vector p; p.x = x; p.y = y; p.z = z; return this->transformCameraToWorld(p); }*/