Advertisement
xerpi

C++ Matrix4x4 and Camera

Mar 2nd, 2013
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.08 KB | None | 0 0
  1. / ********************************************* Matrix4x4 ********************************************* /
  2.  
  3.  
  4. #ifndef _MATRIX_H_
  5. #define _MATRIX_H_
  6.  
  7. #include <math.h>
  8. #include <string.h>
  9.  
  10. class Matrix4x4
  11. {
  12.     public:
  13.         Matrix4x4()
  14.         {
  15.             identity();
  16.         }
  17.  
  18.         void identity()
  19.         {
  20.             float *p = (float *)matrix;
  21.             p[0]  =  p[5] =  p[10] = p[15] = 1.0;
  22.             p[1]  =  p[2] =  p[3]  =  p[4] = 0.0;
  23.             p[6]  =  p[7] =  p[8]  =  p[9] = 0.0;
  24.             p[11] = p[12] = p[13]  = p[14] = 0.0;
  25.         }
  26.  
  27.         void copy(Matrix4x4 &dst)
  28.         {
  29.             memcpy(dst.getMatrixPointer(), getMatrixPointer(), sizeof(float) * 16);
  30.         }
  31.         void copy(Matrix4x4 *dst)
  32.         {
  33.             memcpy(dst->getMatrixPointer(), getMatrixPointer(), sizeof(float) * 16);
  34.         }
  35.  
  36.  
  37.         float *getMatrixPointer()
  38.         {
  39.             return (float *)matrix;
  40.         }
  41.  
  42.         void multiply(Matrix4x4 &mtx, Matrix4x4 &dst)
  43.         {
  44.             float (*dest)[4] = (float (*)[4])dst.getMatrixPointer();
  45.             float (*m1)[4]   = (float (*)[4])getMatrixPointer();
  46.             float (*m2)[4]   = (float (*)[4])mtx.getMatrixPointer();
  47.             dest[0][0] = m1[0][0] * m2[0][0] + m1[0][1] * m2[1][0] + m1[0][2] * m2[2][0] + m1[0][3] * m2[3][0];
  48.             dest[0][1] = m1[0][0] * m2[0][1] + m1[0][1] * m2[1][1] + m1[0][2] * m2[2][1] + m1[0][3] * m2[3][1];
  49.             dest[0][2] = m1[0][0] * m2[0][2] + m1[0][1] * m2[1][2] + m1[0][2] * m2[2][2] + m1[0][3] * m2[3][2];
  50.             dest[0][3] = m1[0][0] * m2[0][3] + m1[0][1] * m2[1][3] + m1[0][2] * m2[2][3] + m1[0][3] * m2[3][3];
  51.  
  52.             dest[1][0] = m1[1][0] * m2[0][0] + m1[1][1] * m2[1][0] + m1[1][2] * m2[2][0] + m1[1][3] * m2[3][0];
  53.             dest[1][1] = m1[1][0] * m2[0][1] + m1[1][1] * m2[1][1] + m1[1][2] * m2[2][1] + m1[1][3] * m2[3][1];
  54.             dest[1][2] = m1[1][0] * m2[0][2] + m1[1][1] * m2[1][2] + m1[1][2] * m2[2][2] + m1[1][3] * m2[3][2];
  55.             dest[1][3] = m1[1][0] * m2[0][3] + m1[1][1] * m2[1][3] + m1[1][2] * m2[2][3] + m1[1][3] * m2[3][3];
  56.  
  57.             dest[2][0] = m1[2][0] * m2[0][0] + m1[2][1] * m2[1][0] + m1[2][2] * m2[2][0] + m1[2][3] * m2[3][0];
  58.             dest[2][1] = m1[2][0] * m2[0][1] + m1[2][1] * m2[1][1] + m1[2][2] * m2[2][1] + m1[2][3] * m2[3][1];
  59.             dest[2][2] = m1[2][0] * m2[0][2] + m1[2][1] * m2[1][2] + m1[2][2] * m2[2][2] + m1[2][3] * m2[3][2];
  60.             dest[2][3] = m1[2][0] * m2[0][3] + m1[2][1] * m2[1][3] + m1[2][2] * m2[2][3] + m1[2][3] * m2[3][3];
  61.  
  62.             dest[3][0] = m1[3][0] * m2[0][0] + m1[3][1] * m2[1][0] + m1[3][2] * m2[2][0] + m1[3][3] * m2[3][0];
  63.             dest[3][1] = m1[3][0] * m2[0][1] + m1[3][1] * m2[1][1] + m1[3][2] * m2[2][1] + m1[3][3] * m2[3][1];
  64.             dest[3][2] = m1[3][0] * m2[0][2] + m1[3][1] * m2[1][2] + m1[3][2] * m2[2][2] + m1[3][3] * m2[3][2];
  65.             dest[3][3] = m1[3][0] * m2[0][3] + m1[3][1] * m2[1][3] + m1[3][2] * m2[2][3] + m1[3][3] * m2[3][3];
  66.         }
  67.  
  68.         //Set matrix rotation X
  69.             void setRotationX(float angle)
  70.             {
  71.                 float s = sinf(angle);
  72.                 float c = cosf(angle);
  73.  
  74.                 matrix[1][1] = c;
  75.                 matrix[1][2] = s;
  76.  
  77.                 matrix[2][1] = -s;
  78.                 matrix[2][2] = c;
  79.             }
  80.  
  81.         //Set matrix rotation Y
  82.             void setRotationY(float angle)
  83.             {
  84.                 float s = sinf(angle);
  85.                 float c = cosf(angle);
  86.  
  87.                 matrix[0][0] = c;
  88.                 matrix[0][2] = -s;
  89.  
  90.                 matrix[2][0] = s;
  91.                 matrix[2][2] = c;
  92.             }
  93.  
  94.         //Set matrix rotation Z
  95.             void setRotationZ(float angle)
  96.             {
  97.                 float s = sinf(angle);
  98.                 float c = cosf(angle);
  99.  
  100.                 matrix[0][0] = c;
  101.                 matrix[0][1] = -s;
  102.  
  103.                 matrix[1][0] = s;
  104.                 matrix[1][1] = c;
  105.             }
  106.  
  107.         void translate(float x, float y, float z)
  108.         {
  109.             matrix[0][3] = matrix[0][0]*x + matrix[0][1]*y + matrix[0][2]*z;
  110.             matrix[1][3] = matrix[1][0]*x + matrix[1][1]*y + matrix[1][2]*z;
  111.             matrix[2][3] = matrix[2][0]*x + matrix[2][1]*y + matrix[2][2]*z;
  112.         }
  113.  
  114.      //Rotate this matrix around X axis
  115.         void rotateX(float angle)
  116.         {
  117.             Matrix4x4 rotated, tmp;
  118.             rotated.setRotationX(angle);
  119.             multiply(rotated, tmp);  // tmp = this * rotated
  120.             tmp.copy(this);  //this = tmp
  121.         }
  122.  
  123.     //Rotate this matrix around Y axis
  124.         void rotateY(float angle)
  125.         {
  126.             Matrix4x4 rotated, tmp;
  127.             rotated.setRotationY(angle);
  128.             multiply(rotated, tmp);  // tmp = this * rotated
  129.             tmp.copy(this);  //this = tmp
  130.         }
  131.  
  132.     //Rotate this matrix around Z axis
  133.         void rotateZ(float angle)
  134.         {
  135.             Matrix4x4 rotated, tmp;
  136.             rotated.setRotationZ(angle);
  137.             multiply(rotated, tmp); // tmp = this * rotated
  138.             tmp.copy(this); //this = tmp
  139.         }
  140.  
  141.     private:
  142.         float matrix[4][4];
  143. };
  144.  
  145. #endif
  146.  
  147.  
  148.  
  149. / ********************************************* Camera.h ********************************************* /
  150.  
  151. #ifndef _CAMERA_H_
  152. #define _CAMERA_H_
  153.  
  154. #include <SFML/OpenGL.hpp>
  155. #include <SFML/Audio.hpp>
  156. #include <SFML/Graphics.hpp>
  157. #include <SFML/Window.hpp>
  158. #include <stdio.h>
  159. #include "Matrix.h"
  160.  
  161. extern sf::Vector3f UP_VECTOR;
  162. extern sf::Vector3f DOWN_VECTOR;
  163.  
  164. float Dot(sf::Vector3f& vec1, sf::Vector3f& vec2);
  165. sf::Vector3f Cross(const sf::Vector3f& vec1, const sf::Vector3f& vec2);
  166. void Cross(sf::Vector3f& vec1, sf::Vector3f& vec2, sf::Vector3f& result);
  167. sf::Vector3f Cross(sf::Vector3f& vec1, sf::Vector3f& vec2);
  168.  
  169.  
  170. class Camera
  171. {
  172.     public:
  173.         Camera(sf::Vector3f *position, float *pitch, float *yaw)
  174.         {
  175.             this->position = position;
  176.             this->pitch = pitch;
  177.             this->yaw = yaw;
  178.             setupVectors();
  179.         }
  180.  
  181.         sf::Vector3f getLookVector()
  182.         {
  183.             return *lookVector;
  184.         }
  185.         sf::Vector3f getUpVector()
  186.         {
  187.             return *upVector;
  188.         }
  189.         sf::Vector3f getRightVector()
  190.         {
  191.             return *rightVector;
  192.         }
  193.         sf::Vector3f getForwardVector()
  194.         {
  195.             return Cross(*rightVector, UP_VECTOR);
  196.         }
  197.         void updateMatrix()
  198.         {
  199.             matrix.identity();
  200.             matrix.rotateX(*pitch);
  201.             matrix.rotateY(*yaw);
  202.             matrix.translate(-position->x, -position->y, -position->z);
  203.         }
  204.  
  205.     private:
  206.         sf::Vector3f *position;
  207.         float *pitch, *yaw;
  208.         Matrix4x4 matrix;
  209.         sf::Vector3f *upVector, *lookVector, *rightVector;
  210.  
  211.         void setupVectors()
  212.         {
  213.             float (*p)[4] = (float (*)[4])matrix.getMatrixPointer();
  214.             rightVector = (sf::Vector3f *)&p[0];
  215.             upVector    = (sf::Vector3f *)&p[1];
  216.             lookVector  = (sf::Vector3f *)&p[2];
  217.         }
  218. };
  219.  
  220. #endif
  221.  
  222.  
  223. / ********************************************* Camera.cpp ********************************************* /
  224.  
  225. #include "Camera.h"
  226.  
  227. sf::Vector3f UP_VECTOR(0.0f, 1.0f, 0.0f);
  228. sf::Vector3f DOWN_VECTOR(0.0f, -1.0f, 0.0f);
  229.  
  230.  
  231. float Dot(sf::Vector3f& vec1, sf::Vector3f& vec2)
  232. {
  233.     return vec1.x*vec2.x + vec1.y*vec2.y + vec1.z*vec2.z;
  234. }
  235.  
  236. sf::Vector3f Cross(const sf::Vector3f& vec1, const sf::Vector3f& vec2)
  237. {
  238.     return sf::Vector3f(vec1.y*vec2.z - vec1.z*vec2.y, vec1.z*vec2.x - vec1.x*vec2.z, vec1.x*vec2.y - vec1.y*vec2.x);
  239. }
  240.  
  241. void Cross(sf::Vector3f& vec1, sf::Vector3f& vec2, sf::Vector3f& result)
  242. {
  243.     result.x = vec1.y*vec2.z - vec1.z*vec2.y;
  244.     result.y = vec1.z*vec2.x - vec1.x*vec2.z;
  245.     result.z = vec1.x*vec2.y - vec1.y*vec2.x;
  246. }
  247.  
  248. sf::Vector3f Cross(sf::Vector3f& vec1, sf::Vector3f& vec2)
  249. {
  250.     return (sf::Vector3f){vec1.y*vec2.z - vec1.z*vec2.y,
  251.                           vec1.z*vec2.x - vec1.x*vec2.z,
  252.                           vec1.x*vec2.y - vec1.y*vec2.x};
  253. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement