Advertisement
xerpi

Camera class with glm lib

Jul 13th, 2013
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.65 KB | None | 0 0
  1. #ifndef _CAMERA_H_
  2. #define _CAMERA_H_
  3.  
  4. #include <SFML/Graphics.hpp>
  5. #include <stdio.h>
  6. #include <glm/glm.hpp>
  7. #include <glm/gtc/matrix_transform.hpp>
  8. #include <glm/gtc/type_ptr.hpp>
  9.  
  10. #define PI   3.14159265359
  11. #define PI_2 6.28318530718
  12.  
  13. extern glm::vec3 UP_VECTOR;
  14. extern glm::vec3 DOWN_VECTOR;
  15.  
  16. class Camera
  17. {
  18.     public:
  19.         Camera(glm::vec3 *position, float *pitch, float *yaw)
  20.         {
  21.             this->position = position;
  22.             this->pitch = pitch;
  23.             this->yaw = yaw;
  24.             setupVectors();
  25.         }
  26.  
  27.         glm::vec3 getLookVector()
  28.         {
  29.             return *lookVector;
  30.         }
  31.         glm::vec3 getUpVector()
  32.         {
  33.             return *upVector;
  34.         }
  35.         glm::vec3 getRightVector()
  36.         {
  37.             return *rightVector;
  38.         }
  39.         glm::vec3 getForwardVector()
  40.         {
  41.             return glm::cross(*rightVector, UP_VECTOR);
  42.         }
  43.         glm::mat4 getLookAt()
  44.         {
  45.             //return glm::lookAt(*position, *position + getLookVector(), getUpVector());
  46.             glm::vec3 right = getRightVector();
  47.             glm::vec3 up = getUpVector();
  48.             glm::vec3 forward = getLookVector();
  49.             glm::mat4 tmp = glm::mat4(
  50.                     right[0], up[0], forward[0], 0.0f,
  51.                     right[1], up[1], forward[1], 0.0f,
  52.                     right[2], up[2], forward[2], 0.0f,
  53.                     0.0f,    0.0f,  0.0f,        1.0f
  54.             );
  55.             return glm::translate(tmp, (*position));
  56.         }
  57.  
  58.         void update()
  59.         {
  60.             matrix = glm::rotate(glm::mat4(1.0f),  glm::degrees(*pitch), glm::vec3(1.0f, 0.0f, 0.0f));
  61.             matrix = glm::rotate(matrix,  glm::degrees(*yaw),   glm::vec3(0.0f, 1.0f, 0.0f));
  62.             matrix = glm::translate(matrix, *position);
  63.             #define printvect(s, v) (printf("%s  %f  %f  %f\n", s, v->x, v->y, v->z))
  64.             printvect("right:", rightVector);
  65.             printvect("up:", upVector);
  66.             printvect("look", lookVector);
  67.         }
  68.         glm::mat4 *const getMatrixP()
  69.         {
  70.             return &matrix;
  71.         }
  72.         float *const getMatrixValueP()
  73.         {
  74.             return glm::value_ptr(matrix);
  75.         }
  76.     private:
  77.         glm::vec3 *position;
  78.         float *pitch, *yaw;
  79.         glm::mat4 matrix;
  80.         glm::vec3 *upVector, *lookVector, *rightVector;
  81.  
  82.         void setupVectors()
  83.         {
  84.             float (*p)[4] = (float (*)[4])glm::value_ptr(matrix);
  85.             rightVector = (glm::vec3 *)&p[0];
  86.             upVector    = (glm::vec3 *)&p[1];
  87.             lookVector  = (glm::vec3 *)&p[2];
  88.         }
  89. };
  90.  
  91. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement