BambooIsland: C:/Users/easterbunny/Desktop/Echtzeitgraphik-LU/trunk/src/Camera.cpp Source File

BambooIsland Abgabe 2

- Main Page
- Related Pages
- Classes
- Files

- File List

# C:/Users/easterbunny/Desktop/Echtzeitgraphik-LU/trunk/src/Camera.cpp

    00001 #include <glm/gtc/matrix_projection.hpp>
    00002 #include <glm/gtc/matrix_transform.hpp>
    00003 #include <glm/gtx/transform.hpp>
    00004 #include <glm/gtx/transform2.hpp>
    00005 #include <glm/gtc/type_ptr.hpp>
    00006
    00007 #include <iostream>
    00008 #include <fstream>
    00009
    00010 #include "common_gl.h"
    00011 #include "LightManager.h"
    00012 #include "Camera.h"
    00013
    00014 #define CAMERA_RECORD
    00015 //#define CAMERA_PLAY
    00016
    00017 using namespace std;
    00018
    00019 const float move_factor = 2.7f;
    00020 const double pi = 3.14159265;
    00021
    00022 const double DEFAULT_FOV = 45.0;
    00023 const double DEFAULT_RATIO = 1.33;
    00024 const double DEFAULT_NEARPLANE = 0.1f;
    00025 const double DEFAULT_FARPLANE = 100.0f;
    00026
    00027 const double x_border = 50.0;       //the border of the camera space in x direction - camera position is forced between [-x_border, x_border]
    00028 const double y_border = 30.0;       //the border of the camera space in y direction - camera position is forced between [-y_border, y_border]
    00029 const double z_border = 50.0;       //the border of the camera space in z direction - camera position is forced between [-z_border, z_border]
    00030
    00031 Camera::Camera(glm::vec3 position, double angle_x, double angle_y)
    00032 {
    00033     this->m_m4ProjectionMatrix = glm::perspective(DEFAULT_FOV, DEFAULT_RATIO, DEFAULT_NEARPLANE, DEFAULT_FARPLANE);
    00034     this->m_v3Position = position;
    00035     this->m_dRotationX = angle_x;
    00036     this->m_dRotationY = angle_y;
    00037
    00038     setMode(Camera::Replay);
    00039 }
    00040
    00041 Camera::~Camera()
    00042 {
    00043     m_camera_recorder.close();
    00044     m_camera_path.close();
    00045 }
    00046
    00047 glm::mat4 Camera::getUsedLightProjModelViewMatrix() const
    00048 {
    00049     return this->m_m4LightProjModelViewMatrix;
    00050 }
    00051
    00052 glm::mat4 Camera::getProjectionMatrix() const
    00053 {
    00054     return this->m_m4ProjectionMatrix;
    00055 }
    00056
    00057 void Camera::setMode(CameraMode mode)
    00058 {
    00059     if (m_camera_recorder.is_open())
    00060         m_camera_recorder.close();
    00061
    00062     if (m_camera_path.is_open())
    00063         m_camera_path.close();
    00064
    00065     if (mode == Camera::Record)
    00066     {
    00067         m_camera_recorder.open("camera_recorded.txt", ios::out);
    00068     }
    00069     else if (mode == Camera::Replay)
    00070     {
    00071         m_camera_path.open("camera.txt", ios::in);
    00072     }
    00073
    00074     m_mode = mode;
    00075     m_dLastReplayedTime = 0;
    00076     m_dRecordReplayTimer = glfwGetTime();
    00077 }
    00078
    00079 void Camera::setLightMatrixForShadowMapping()
    00080 {
    00081     glm::vec3 pos = LightManager::instance()->getCurrentLightPositionAsVec3();
    00082     glm::vec3 lookAtPoint = glm::vec3(12.5f, 0.0f, 12.5f);
    00083
    00084     m_m4SavedModelViewMatrix = m_m4ModelViewMatrix;
    00085     m_m4SavedProjectionMatrix = m_m4ProjectionMatrix;
    00086
    00087     m_m4ModelViewMatrix = glm::lookAt(pos, lookAtPoint, glm::vec3(0.0f, 1.0f, 0.0f));
    00088     m_m4ProjectionMatrix = glm::perspective(16.0, 2.0, 40.0, 100.0);
    00089
    00090     m_m4LightProjModelViewMatrix = m_m4ProjectionMatrix * m_m4ModelViewMatrix;
    00091 }
    00092
    00093 void Camera::unsetLightMatrix()
    00094 {
    00095     m_m4ModelViewMatrix = m_m4SavedModelViewMatrix;
    00096     m_m4ProjectionMatrix = m_m4SavedProjectionMatrix;
    00097 }
    00098
    00099 void Camera::move()
    00100 {
    00101     if (m_mode == Camera::Record)
    00102     {
    00103         glm::vec3 move_x;
    00104         glm::vec3 move_y;
    00105         glm::vec3 move_z;
    00106
    00107         if (this->m_v3MovingVector.x != 0)
    00108         {
    00109             //if camera should move in x-direction, we must calculate the look-at point
    00110             //on the camera-sphere and use this vector as reference, and move normal to this vector
    00111
    00112             move_x.x = sin(((this->m_dRotationX+90) / 180.0) * pi);
    00113             move_x.y = 0.0;
    00114             move_x.z = cos(((this->m_dRotationX+90) / 180.0) * pi);
    00115
    00116             //normalize to project point on our sphere
    00117             move_x /= sqrt(move_x.x*move_x.x + move_x.y*move_x.y + move_x.z*move_x.z);
    00118
    00119             //and multiplicate with given x-factor to adjust movement length
    00120             move_x *= this->m_v3MovingVector.x;
    00121         }
    00122
    00123         if (this->m_v3MovingVector.y != 0)
    00124         {
    00125             //if camera should move in y-direction, we can use the given value directly
    00126
    00127             move_x.y = this->m_v3MovingVector.y;
    00128         }
    00129
    00130         if (this->m_v3MovingVector.z != 0)
    00131         {
    00132             //if camera should move in z-direction, we must calculate the look-at point
    00133             //on the camera-sphere and use this vector as reference, and move along this vector
    00134
    00135             move_z.x = sin((this->m_dRotationX / 180.0) * pi);
    00136             move_z.y = tan((this->m_dRotationY / 180.0) * pi);
    00137             move_z.z = cos((this->m_dRotationX / 180.0) * pi);
    00138
    00139             //normalize to project point on our sphere
    00140             move_z /= sqrt(move_z.x*move_z.x + move_z.y*move_z.y + move_z.z*move_z.z);
    00141
    00142             //and multiplicate with given z-factor to adjust movement length
    00143             move_z *= this->m_v3MovingVector.z;
    00144         }
    00145
    00146         this->m_v3Position += (move_x + move_y + move_z) * move_factor;
    00147
    00148         if (this->m_v3Position.x > x_border)
    00149             this->m_v3Position.x = x_border;
    00150
    00151         if (this->m_v3Position.x < -x_border)
    00152             this->m_v3Position.x = -x_border;
    00153
    00154         if (this->m_v3Position.y > y_border)
    00155             this->m_v3Position.y = y_border;
    00156
    00157         if (this->m_v3Position.y < 1)
    00158             this->m_v3Position.y = 1;
    00159
    00160         if (this->m_v3Position.z > z_border)
    00161             this->m_v3Position.z = z_border;
    00162
    00163         if (this->m_v3Position.z < -z_border)
    00164             this->m_v3Position.z = -z_border;
    00165
    00166         // Output camera position and rotation
    00167
    00168
    00169
    00170         m_camera_recorder << glfwGetTime() - m_dRecordReplayTimer << " " << m_v3Position.x << " " << m_v3Position.y << " " << m_v3Position.z << " " << m_dRotationX << " " << m_dRotationY << endl;
    00171     }
    00172     else if (m_mode == Camera::Replay)
    00173     {
    00174
    00175     static double pos_x, pos_y, pos_z, rot_x, rot_y;
    00176
    00177     while (m_dLastReplayedTime < (glfwGetTime() - m_dRecordReplayTimer) && !m_camera_path.eof())
    00178     {
    00179         m_camera_path >> m_dLastReplayedTime;
    00180         m_camera_path >> pos_x;
    00181         m_camera_path >> pos_y;
    00182         m_camera_path >> pos_z;
    00183         m_camera_path >> rot_x;
    00184         m_camera_path >> rot_y;
    00185     }
    00186
    00187     m_v3Position.x = pos_x;
    00188     m_v3Position.y = pos_y;
    00189     m_v3Position.z = pos_z;
    00190     m_dRotationX = rot_x;
    00191     m_dRotationY = rot_y;
    00192
    00193     }
    00194
    00195     //Calculate modelview matrix
    00196
    00197     glm::vec3 lookAt;
    00198
    00199     lookAt.x = sin((this->m_dRotationX / 180.0) * pi);
    00200     lookAt.y = tan((this->m_dRotationY / 180.0) * pi);
    00201     lookAt.z = cos((this->m_dRotationX / 180.0) * pi);
    00202
    00203     m_m4LastFrameModelViewMatrix = m_m4ModelViewMatrix;
    00204     m_m4ModelViewMatrix = glm::lookAt(this->m_v3Position, this->m_v3Position + lookAt, glm::vec3(0,1,0));
    00205
    00206    // m_camera_recorder << glfwGetTime() << " " << m_v3Position.x << " " << m_v3Position.y << " " << m_v3Position.z << " " << m_dRotationX << " " << m_dRotationY << endl;
    00207 }
    00208
    00209 glm::mat4 Camera::getModelviewMatrix() const
    00210 {
    00211     return m_m4ModelViewMatrix;
    00212 }
    00213
    00214 glm::mat4 Camera::getLastFrameModelViewMatrix() const
    00215 {
    00216     return m_m4LastFrameModelViewMatrix;
    00217 }
    00218
    00219 glm::mat4 Camera::getInverseTranslationMatrix() const
    00220 {
    00221     glm::vec3 puffer = this->m_v3Position;
    00222    // puffer.y = 0.0;
    00223     return glm::translate(puffer);
    00224 }
    00225
    00226 void Camera::addToMoveVector(glm::vec3 vector)
    00227 {
    00228     this->m_v3MovingVector += vector;
    00229 }
    00230
    00231 void Camera::incRotationX(float value)
    00232 {
    00233     this->m_dRotationX += value;
    00234
    00235     //map to [0.0, 360.0[
    00236     if (this->m_dRotationX >= 360)
    00237         this->m_dRotationX -= 360;
    00238
    00239     if (this->m_dRotationX < 0)
    00240         this->m_dRotationX += 360;
    00241 }
    00242
    00243 void Camera::incRotationY(float value)
    00244 {
    00245     this->m_dRotationY += value;
    00246
    00247     //restrict to [-89.5, 89.5]
    00248     if (this->m_dRotationY > 89.5)
    00249         this->m_dRotationY = 89.5;
    00250
    00251     if (this->m_dRotationY < -89.5)
    00252         this->m_dRotationY = -89.5;
    00253 }
    00254
    00255 Camera *Camera::instance()
    00256 {
    00257     static Camera camera_singelton_instance(glm::vec3(0,5,10), 180, 0);
    00258     return &camera_singelton_instance;
    00259 }

All Classes Functions Variables Enumerations Enumerator