Browse Source

Added translation, rotation and scaling.

angel 7 years ago
parent
commit
31a49e0bf7
6 changed files with 119 additions and 20 deletions
  1. 0 10
      include/Matrix.h
  2. 32 0
      include/matrix.h
  3. 7 0
      include/vector3.h
  4. 18 10
      src/engine.cpp
  5. 20 0
      src/matrix.cpp
  6. 42 0
      src/vector.cpp

+ 0 - 10
include/Matrix.h

@@ -1,10 +0,0 @@
-#ifndef MATRIX_H
-#define MATRIX_H
-
-#include <math.h>
-
-class Matrix{
-
-};
-
-#endif

+ 32 - 0
include/matrix.h

@@ -0,0 +1,32 @@
+#ifndef MATRIX_H
+#define MATRIX_H
+
+#include <math.h>
+#include <array>
+#include <vector3.h>
+
+class Matrix{
+    public:
+        float& operator()(size_t y, size_t x){
+            return mMatrix[y*3 + x];
+        }
+
+        void makeRotX(float theta);
+
+        Vector3 matXVec(Vector3 &vertex);
+
+    private:
+        std::array<float, 9>  mMatrix{0,0,0,0,0,0,0,0,0};
+};
+
+
+// template<class T>
+// class Mat4 : public MatrixM<T,4,4>{
+//     public:
+//         void fullRotMat(T thetaX, T thetaY, T thetaZ);
+        
+// };
+
+
+
+#endif

+ 7 - 0
include/vector3.h

@@ -14,6 +14,13 @@ struct Vector3{
     Vector3(std::string x1, std::string y1, std::string z1):
         x(std::stof(x1)), y(std::stof(y1)), z(std::stof(z1))
     {}
+
+    void rotX(float thetaX);
+    void rotY(float thetaY);
+    void rotZ(float thetaZ);
+
+    void scale(float scale);
+    void translate(float dx, float dy, float dz);
 };
 
 #endif

+ 18 - 10
src/engine.cpp

@@ -1,6 +1,6 @@
 #include "engine.h"
 #include <string>
-#include <math.h>
+#include <vector3.h>
 
 Engine::Engine(){
 
@@ -54,6 +54,8 @@ void Engine::mainLoop(){
         done = FEInputManager.processInput();
 
         //Update entities here in the future
+        //Right now only does simple demo stuff
+        //Maybe physics based in the future??
         moveModels();
 
         //Perform all render calculations and update screen
@@ -67,26 +69,32 @@ void Engine::mainLoop(){
 void Engine::loadModels(){
     //In the future I want to read all o the models in the model folder
     //And build them here.  For now I force it to be only one.
+    //Probably should be its own class in the future
     std::string path = "../models/teapot.obj";
     sceneModels = new Model(path);
 
     //sceneModels->describeMesh();
 }
 
-void Engine::moveModels(){
-    float theta  = 0.01;
-    float cosine = std::cos(theta);
-    float sine    = std::sin(theta);
 
+//Engine class moves stuff, for now
+//Some kind of physics module should be responsible of this in the future
+void Engine::moveModels(){
+    float thetax  = 0.001;
+    float thetay  = 0.001;
+    float thetaz  = 0.00;
+    float scale   = 0.999;
+    float dd      = 0.0;
     Mesh * modelMesh = sceneModels->getMesh();
     int size = modelMesh->numVertices;
-    std::vector<Vector3> * vertices = &modelMesh->vertices;
+    std::vector<Vector3> *   vertices = &modelMesh->vertices;
 
     for (int i = 0;i < size; ++i){
-        float xOld = (*vertices)[i].x;
-        float zOld = (*vertices)[i].z;
-        (*vertices)[i].x = xOld*cosine + zOld*sine;
-        (*vertices)[i].z = -xOld*sine   + zOld*cosine;
+        (*vertices)[i].scale(scale);
+        (*vertices)[i].rotX(thetax);
+        (*vertices)[i].rotY(thetay);
+        (*vertices)[i].rotZ(thetaz);
+        (*vertices)[i].translate(dd, dd, dd);
     }
 
 }

+ 20 - 0
src/matrix.cpp

@@ -0,0 +1,20 @@
+#include "matrix.h"
+
+//TEMPLATES ARE THE WORST
+// template<class T>
+// void Mat3::fullRotMat(T thetaX, T thetaY, T thetaZ){
+//     //We ignore the Y and Z for now
+//     Mat3
+//     Mat3 xRot = 
+// }
+
+
+// void Matrix::makeRotX(float theta){
+//     float cosine  = std::cos(theta);
+//     float sine    = std::sin(theta);
+//     mMatrix(0,1)  = 1;
+//     mMatrix(1,1)  = cosine;
+//     mMatrix(1,2)  = -sine;
+//     mMatrix(2,1)  = sine;
+//     mMatrix(2,2)  = cosine;
+// }

+ 42 - 0
src/vector.cpp

@@ -0,0 +1,42 @@
+#include "vector3.h"
+#include <math.h>
+
+void Vector3::rotX(float theta){
+    float cosine  = std::cos(theta);
+    float sine    = std::sin(theta);
+    float yTemp = y;
+    float zTemp = z;
+    y = yTemp*cosine + -zTemp*sine;
+    z = yTemp*sine   + zTemp*cosine;
+}   
+
+
+void Vector3::rotY(float theta){
+    float cosine  = std::cos(theta);
+    float sine    = std::sin(theta);
+    float xTemp = x;
+    float zTemp = z;
+    x = xTemp*cosine + zTemp*sine;
+    z = -xTemp*sine   + zTemp*cosine;
+}   
+
+void Vector3::rotZ(float theta){
+    float cosine  = std::cos(theta);
+    float sine    = std::sin(theta);
+    float xTemp = x;
+    float yTemp = y;
+    x = xTemp*cosine + -yTemp*sine;
+    y = xTemp*sine   + yTemp*cosine;
+}   
+
+void Vector3::scale(float scale){
+    x *= scale;
+    y *= scale;
+    z *= scale;
+}
+
+void Vector3::translate(float dx, float dy, float dz){
+    x +=dx;
+    y +=dy;
+    z +=dz;
+}