Advertisement
Guest User

Untitled

a guest
Oct 30th, 2013
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.98 KB | None | 0 0
  1. class Pipeline
  2. {
  3.         Vector3f pos, scale, rotation;
  4.         Matrix4f m;
  5.  
  6.         Pipeline() :    pos(0,0,0),
  7.                         rotation(0,0,0),
  8.                         scale(1,1,1)
  9.         {
  10.             calculateModel();
  11.         }
  12.  
  13.         void setRotation(float angleX, float angleY, float angleZ)
  14.         {
  15.             rotation = Vector3f(angleX, angleY, angleZ);
  16.  
  17.             calculateModel();
  18.         }
  19.  
  20.         void setScale(float factorX, float factorY, float factorZ)
  21.         {
  22.             scale = Vector3f(factorX, factorY, factorZ);
  23.  
  24.             calculateModel();
  25.         }
  26.  
  27.         void setTranslation(float transX, float transY, float transZ)
  28.         {
  29.             pos = Vector3f(transX, transY, transZ);
  30.  
  31.             calculateModel();
  32.         }
  33.  
  34.         Matrix4f getMatrix(Camera* c)
  35.         {
  36.             return c->getPerspective() * c->getView() * m;
  37.         }
  38.  
  39.         void calculateModel()
  40.         {
  41.             Matrix4f rot =
  42.                 Matrix4f(1.0f, 0.0f, 0.0f, 0.0f,
  43.                 0.0f, cosf(rotation.x()), -sinf(rotation.x()), 0.0f,
  44.                 0.0f, sinf(rotation.x()), cosf(rotation.x()), 0.0f,
  45.                 0.0f, 0.0f, 0.0f, 1.0f)
  46.                 *
  47.                 Matrix4f(cosf(rotation.y()), 0.0f, -sinf(rotation.y()), 0.0f,
  48.                 0.0f, 1.0f, 0.0f, 0.0f,
  49.                 sinf(rotation.y()), 0.0f, cosf(rotation.y()), 0.0f,
  50.                 0.0f, 0.0f, 0.0f, 1.0f)
  51.                 *
  52.                 Matrix4f(cosf(rotation.z()), -sinf(rotation.z()), 0.0f, 0.0f,
  53.                 sinf(rotation.z()), cosf(rotation.z()), 0.0f, 0.0f,
  54.                 0.0f, 0.0f, 1.0f, 0.0f,
  55.                 0.0f, 0.0f, 0.0f, 1.0f);
  56.  
  57.             Matrix4f trans =
  58.                 Matrix4f(1.0f, 0.0f, 0.0f, pos.x(),
  59.                 0.0f, 1.0f, 0.0f, pos.y(),
  60.                 0.0f, 0.0f, 1.0f, pos.z(),
  61.                 0.0f, 0.0f, 0.0f, 1.0f);
  62.  
  63.             Matrix4f scaling =
  64.                 Matrix4f(scale.x(), 0.0f, 0.0f, 0.0f,
  65.                 0.0f, scale.y(), 0.0f, 0.0f,
  66.                 0.0f, 0.0f, scale.z(), 0.0f,
  67.                 0.0f, 0.0f, 0.0f, 1.0f);
  68.  
  69.             m = trans * rot * scaling;
  70.         }
  71. }
  72.  
  73. class Camera
  74. {
  75.  
  76.         Matrix4f perspective;
  77.         Matrix4f view;
  78.         float ar, near, far, fov;
  79.         float x, y, z;
  80.         Vector3f n, u, v;
  81.  
  82.  
  83.         Camera(float near, float far, float fov, float ar) : near(near), far(far), fov(fov), ar(ar), view(Matrix4f::identity()), perspective(Matrix4f::identity()), x(0), y(0), z(0)
  84.         {
  85.             setLookat(Vector3f(0, 0, 1), Vector3f(0, 1, 0));
  86.         }
  87.  
  88.         void computePerspective()
  89.         {
  90.             float invTanHalveFOV = 1 / tan(fov / 2);
  91.             perspective = Matrix4f(1 / tan(fov / 2), 0, 0, 0,
  92.                 0, 1 / (ar * tan(fov / 2) ), 0, 0,
  93.                 0, 0, (-near - (far * 2)) / (near - far), (2 * far * near) / (near - far),
  94.                 0, 0, 1, 0);
  95.         }
  96.  
  97.         void computeView()
  98.         {
  99.             view =  Matrix4f(u.x(), u.y(), u.z(), 0  ,
  100.                              v.x(), v.y(), v.z(), 0  ,
  101.                              n.x(), n.y(), n.z(), 0  ,
  102.                              0    , 0    , 0    , 1  )
  103.                     *
  104.                     Matrix4f(1    , 0    , 0    , -x ,
  105.                              0    , 1    , 0    , -y ,
  106.                              0    , 0    , 1    , -z ,
  107.                              0    , 0    , 0    , 1  );
  108.         }
  109.  
  110.         void setLookat(const Vector3f& target, const Vector3f& up)
  111.         {
  112.             n = target.normalized();
  113.             u = up.normalized().cross(n);
  114.             v = n.cross(u);
  115.  
  116.             computeView();
  117.         }
  118.  
  119. }
  120.  
  121. // And the code that uses all the above:
  122.  
  123. Camera c(1, 100, .25*TAU, WINDOW_WIDTH / WINDOW_HEIGHT);
  124.  
  125. Pipeline p;
  126. p.setRotation(0, 0, .125*TAU);
  127.  
  128. Matrix4f MVP = p.getMatrix(&c);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement