Browse Source

Separated model matrix, added correct specular shading. Fixed normals.

angel 7 years ago
parent
commit
3d4bdf058d

+ 4 - 4
CMakeLists.txt

@@ -3,16 +3,16 @@ project(softwareRenderer)
 
 
 set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/modules")
-set(SDL2_PATH "C:\\vs_dev_lib\\SDL2-2.0.8")
+#set(SDL2_PATH "C:\\vs_dev_lib\\SDL2-2.0.8")
 find_package(SDL2 REQUIRED)
 include_directories(${SDL2_INCLUDE_DIR} include)
 
 file(GLOB source_files "*.h" "*.cpp" "src/*.cpp" "include/*.h")
 
 add_executable(softwareRenderer ${source_files})
-set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g")
-set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -g")
-set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -g")
+set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ")
+set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ")
+set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ")
 target_link_libraries(softwareRenderer ${SDL2_LIBRARY})
 
 if(WIN32)

+ 0 - 1
include/displayManager.h

@@ -1,7 +1,6 @@
 #ifndef DISPLAYMANAGER_H
 #define DISPLAYMANAGER_H
 
-#include "SDL.h"
 #include "buffer.h"
 
 

+ 1 - 0
include/geometry.h

@@ -14,6 +14,7 @@ struct AABox{
 
     //Builds axis aligned bounding box of the given mesh
     void buildAABB(const Mesh &mesh);
+    void update(const Matrix4 &modelMatrix);
 };
 
 struct Plane{

+ 6 - 3
include/matrix.h

@@ -2,7 +2,7 @@
 #define MATRIX_H
 
 #include <array>
-#include <vector3D.h>
+#include "vector3D.h"
 
 //Data struct holding all of the data you need to make a transform matrix
 struct TransformParameters{
@@ -21,8 +21,11 @@ class Matrix4{
             return mMatrix[y*4 + x];
         }
         Matrix4 operator* (Matrix4 &rhs);
-        Vector3f matMultVec(Vector3f &vec); 
-        
+        Vector3f matMultVec(const Vector3f &vec); 
+        Vector3f matMultDir(const Vector3f &vec);
+        Matrix4 transpose();
+        Matrix4 inverse();
+
         //Named constructor idiom to build the basic matrices we need for 
         //transformation.
         Matrix4 static makeTestMat();

+ 3 - 7
include/model.h

@@ -5,14 +5,14 @@
 #include "mesh.h"
 #include "geometry.h"
 #include "matrix.h"
-#include "objParser.h"
+
 
 class Model{
     public:
         Model(std::string path, TransformParameters &initParameters); 
 
         Mesh *getMesh();
-
+        Matrix4 &getModelMatrix();
         AABox *getBounds();
 
         void update();
@@ -20,13 +20,9 @@ class Model{
         //Prints the mesh vertices for debugging
         void describeMesh();
     private:
-        //Transform matrix from object space to worldSpace
-        void initPosition(TransformParameters initPos);
-
-        //Calculates the boundary box of the mesh in object space
-        void buildBoundaryBox();
         Mesh mMesh;
         AABox mBounds;
+        Matrix4 mModelMatrix;
 };
 
 #endif

+ 0 - 1
include/rasterizer.h

@@ -30,7 +30,6 @@ class Rasterizer{
         //Transforms coordinates from NDC to pixel values
         static void viewportTransform(Buffer<Uint32> *pixelBuffer, Vector3f *vertices);
 
-
         static void triBoundBox(int &xMax, int &xMin, int &yMax, int &yMin, Vector3f *vertices, Buffer<Uint32> *pixelBuffer);
 
         static float edge(Vector3f &a, Vector3f &b, Vector3f &c);

+ 17 - 0
include/renderData.h

@@ -0,0 +1,17 @@
+#ifndef RENDERDATA_H
+#define RENDERDATA_H
+
+#include "matrix.h"
+#include "mesh.h"
+#include "shader.h"
+
+struct RenderData{
+    Matrix4 *modelMatrix; 
+    Mesh *modelMesh;
+    IShader *shader;
+};
+
+
+
+
+#endif

+ 4 - 2
include/renderManager.h

@@ -4,7 +4,7 @@
 #include "displayManager.h"
 #include "sceneManager.h"
 #include "softwareRenderer.h"
-#include "mesh.h"
+#include "model.h"
 #include <queue>
 
 //High level render operations that shouldn't be done by the
@@ -33,8 +33,10 @@ class RenderManager{
         DisplayManager * screen;
 
         SoftwareRenderer renderInstance;
-        std::queue<Mesh*> renderQueue;        
+        std::queue<Model*> *renderObjectQueue;        
 };
 
 
+
+
 #endif

+ 4 - 2
include/scene.h

@@ -3,9 +3,11 @@
 
 #include <string>
 #include <vector>
+#include <queue>
 #include "model.h"
 #include "camera.h"
 
+
 //Keeps track of all the lights, models and cameras contained in a current
 //scene. Also performs frustrumc ulling to determine what objects to send to
 //the render queue visibility.
@@ -20,7 +22,7 @@ class Scene{
         void update();
 
         //Returns the list of models not culled by the frustrum
-        std::vector<Model*>* getVisiblemodels();
+        std::queue<Model*>* getVisiblemodels();
 
         Camera * getCurrentCamera();
 
@@ -36,7 +38,7 @@ class Scene{
         std::vector<Model*> modelsInScene;
 
         //Contains the models that remain after frustrum culling
-        std::vector<Model*> visibleModels;
+        std::queue<Model*> visibleModels;
         
         bool loadSceneModels(std::string &path);
 

+ 43 - 5
include/shader.h

@@ -3,22 +3,22 @@
 
 #include "vector3D.h"
 #include "matrix.h"
-#include "rasterizer.h"
 #include <array>
 
 //Shader Interface for a class that emulates modern GPU fragment and vertex shaders
 struct IShader {
     virtual ~IShader() {};
-    virtual Vector3f vertex(Vector3f &vertex, Matrix4 &MVP, Vector3f &normals, Vector3f &light, int i, Vector3f &viewDir) = 0;
+    virtual Vector3f vertex(Vector3f &vertex, Vector3f &normals, Vector3f &light, int i) = 0;
     virtual bool fragment(const Vector3f &bari, Vector3f &color, float &depth, Vector3f &zVerts) = 0;
 };
 
 //Simplest shader. Calculates light intensity per triangle.
 struct FlatShader : public IShader {
+    Matrix4 MVP, MV;
     float varIntensity;
     Vector3f rgb{255,255,255};
 
-    Vector3f vertex(Vector3f &vertex, Matrix4 &MVP, Vector3f &normals, Vector3f &light, int index, Vector3f &viewDir) override{
+    Vector3f vertex(Vector3f &vertex, Vector3f &normals, Vector3f &light, int index) override{
         varIntensity = std::max(0.0f,normals.dotProduct(light));
         return MVP.matMultVec(vertex); //Transforms verts into projected space
     }
@@ -34,14 +34,52 @@ struct FlatShader : public IShader {
 //More Complex shader that calculates a per-vertex intensity and interpolates 
 //through the fragments of the triangle
 struct GouraudShader : public IShader {
+    Matrix4 MVP, MV, V, N;
+    float ambientStrength = 0.05, diffStrength = 0, specularStrength = 0.5, spec = 0;
+    Vector3f ambient, diffuse, specular;
+    Vector3f lightColor1{1,1,1}, lightColor2{0,0,1}, lightColor3{1,1,1};
+    Vector3f varying_diffuse, varying_specular, reflectDir, viewDir, light2;
+    Vector3f rgb{255,255,255};
+
+    Vector3f vertex(Vector3f &vertex, Vector3f &normal, Vector3f &light, int index) override{
+        normal = N.matMultDir(normal).normalized();
+        light2 = V.matMultDir(light).normalized();
+        reflectDir = Vector3f::reflect(-light2, normal);
+        viewDir = MV.matMultVec(vertex).normalized();
+        varying_specular.data[index] = std::pow( std::max( -viewDir.dotProduct(reflectDir), 0.0f), 32.0f);
+        varying_diffuse.data[index] = std::max(0.0f, (normal).dotProduct(light2));
+        return MVP.matMultVec(vertex);
+    }
+
+    bool fragment(const Vector3f &bari, Vector3f &color, float &depth, Vector3f &zVerts) override{
+        ambient = lightColor1 * ambientStrength;
+
+        diffStrength = bari.dotProduct(varying_diffuse);
+        diffuse = lightColor2 * diffStrength;
+
+        spec = bari.dotProduct(varying_specular);
+        specular = lightColor3 * (specularStrength * spec);
+
+        color = (ambient + diffuse + specular) * rgb;
+
+        depth = bari.dotProduct(zVerts);
+        return false;
+    }
+
+};
+//Even more complex shader that interpolates normals and calculates intensities per fragment instead
+//of per normals. Uses half angle optimization.
+struct BlinnPhongShader : public IShader {
+    Matrix4 MVP, MV;
     float ambientStrength = 0.05, diffStrength = 0, specularStrength = 0.5, spec = 0;
     Vector3f ambient, diffuse, specular;
     Vector3f lightColor1{1,0,0}, lightColor2{0,0,1}, lightColor3{1,1,1};
-    Vector3f varying_diffuse, varying_specular, reflectDir;
+    Vector3f varying_diffuse, varying_specular, reflectDir, viewDir;
     Vector3f rgb{255,255,255};
 
-    Vector3f vertex(Vector3f &vertex, Matrix4 &MVP, Vector3f &normal, Vector3f &light, int index, Vector3f &viewDir) override{
+    Vector3f vertex(Vector3f &vertex, Vector3f &normal, Vector3f &light, int index) override{
         reflectDir = Vector3f::reflect(-light, normal);
+        viewDir = MV.matMultVec(vertex);
         varying_specular.data[index] = std::pow( std::max(viewDir.dotProduct(reflectDir), 0.0f), 32.0f);
         varying_diffuse.data[index] = std::max(0.0f, normal.dotProduct(light));
         return MVP.matMultVec(vertex);

+ 2 - 2
include/softwareRenderer.h

@@ -3,7 +3,7 @@
 
 #include "rasterizer.h" 
 #include "buffer.h"
-#include "mesh.h"
+#include "model.h"
 #include "camera.h"
 
 class SoftwareRenderer {
@@ -34,7 +34,7 @@ class SoftwareRenderer {
         //14.-zBuffer update
         //15.-Writes to frame buffer
         //16.-Swap buffer
-        void drawTriangularMesh(Mesh * triMesh);
+        void drawTriangularMesh(Model * currentModel);
 
         void clearBuffers();
 

+ 2 - 12
models/teapot.mtl

@@ -1,5 +1,5 @@
-# Blender MTL File: 'None'
-# Material Count: 2
+# Blender MTL File: 'test.blend'
+# Material Count: 1
 
 newmtl None
 Ns 0.000000
@@ -10,13 +10,3 @@ Ke 0.000000 0.000000 0.000000
 Ni 1.000000
 d 1.000000
 illum 2
-
-newmtl None.001
-Ns 0.000000
-Ka 0.000000 0.000000 0.000000
-Kd 0.800000 0.800000 0.800000
-Ks 0.800000 0.800000 0.800000
-Ke 0.000000 0.000000 0.000000
-Ni 1.000000
-d 1.000000
-illum 2

File diff suppressed because it is too large
+ 4720 - 19651
models/teapot.obj


+ 1 - 1
src/camera.cpp

@@ -11,7 +11,7 @@ Camera::Camera(){
 
 void Camera::update(){
     float t = static_cast<float>(SDL_GetTicks());
-    float radius = 12;
+    float radius = 5;
     float camX   = std::sin(t/4000) * radius;
     float camZ   = std::cos(t/4000) * radius;
     position.x   = camX;

+ 35 - 0
src/geometry.cpp

@@ -26,6 +26,40 @@ void AABox::buildAABB(const Mesh &mesh){
     maxPoints = maxVals;
 }
 
+void AABox::update(const Matrix4 &modelMatrix){
+    Vector3f minVals(std::numeric_limits<float>::max());
+    Vector3f maxVals(std::numeric_limits<float>::min());
+    Vector3f vertices[8];
+
+    vertices[0] = Vector3f(minPoints.x, minPoints.y, minPoints.z);
+    vertices[1] = Vector3f(maxPoints.x, minPoints.y, minPoints.z);
+    vertices[2] = Vector3f(minPoints.x, maxPoints.y, minPoints.z);
+    vertices[3] = Vector3f(maxPoints.x, maxPoints.y, minPoints.z);
+    vertices[4] = Vector3f(minPoints.x, minPoints.y, maxPoints.z);
+    vertices[5] = Vector3f(maxPoints.x, minPoints.y, maxPoints.z);
+    vertices[6] = Vector3f(minPoints.x, maxPoints.y, maxPoints.z);
+    vertices[7] = Vector3f(maxPoints.x, maxPoints.y, maxPoints.z);
+
+    //Iterating through every vertx in the mesh
+    for(int i = 0; i < 8; ++i){
+        //Checking the current vertex for all axes
+        for(int ax = 0; ax < 3; ++ax){
+            //Setting max values
+            if(vertices[i].data[ax] > maxVals.data[ax]){
+                maxVals.data[ax] = vertices[i].data[ax];
+            }
+            //Setting min values
+            if(vertices[i].data[ax] < minVals.data[ax]){
+                minVals.data[ax] = vertices[i].data[ax];
+            }
+        }
+    }
+
+    minPoints = minVals;
+    maxPoints = maxVals;
+}
+
+
 //---------------------------------PLANE------------------------------------//
 
 float Plane::distance(const Vector3f &points){
@@ -48,6 +82,7 @@ void Frustrum::setCamInternals(){
     farW  = farH * AR;
 }
 
+//Calculates frustrum planes in world space
 void Frustrum::updatePlanes(Matrix4 &viewMat, const Vector3f &cameraPos){
     Vector3f X(viewMat(0,0), viewMat(0,1), viewMat(0,2));
     Vector3f Y(viewMat(1,0), viewMat(1,1), viewMat(1,2));

+ 183 - 1
src/matrix.cpp

@@ -1,7 +1,7 @@
 #include "matrix.h"
 #include <math.h>
 
-Vector3f Matrix4::matMultVec(Vector3f &vec){
+Vector3f Matrix4::matMultVec(const Vector3f &vec){
     Vector3f newVec(0,0,0);
     float w2 = 0;
     newVec.x = vec.x*(*this)(0,0)+
@@ -29,6 +29,188 @@ Vector3f Matrix4::matMultVec(Vector3f &vec){
     return newVec;
 }
 
+Vector3f Matrix4::matMultDir(const Vector3f &vec){
+    Vector3f newVec(0,0,0);
+    newVec.x = vec.x*(*this)(0,0)+
+               vec.y*(*this)(0,1)+
+               vec.z*(*this)(0,2); //Assuming wO of vec always  = 1
+
+    newVec.y = vec.x*(*this)(1,0)+
+               vec.y*(*this)(1,1)+
+               vec.z*(*this)(1,2); //Assuming wO of vec always  = 1
+
+    newVec.z = vec.x*(*this)(2,0)+
+               vec.y*(*this)(2,1)+
+               vec.z*(*this)(2,2);; //Assuming wO of vec always  = 1
+
+    return newVec;
+}
+
+Matrix4 Matrix4::transpose(){
+    Matrix4 transposeMatrix;
+    //First row
+    transposeMatrix(0,0) = (*this)(0,0);
+    transposeMatrix(0,1) = (*this)(1,0);
+    transposeMatrix(0,2) = (*this)(2,0);
+    transposeMatrix(0,3) = (*this)(3,0);
+
+    //Second row
+    transposeMatrix(1,0) = (*this)(0,1);
+    transposeMatrix(1,1) = (*this)(1,1);
+    transposeMatrix(1,2) = (*this)(2,1);
+    transposeMatrix(1,3) = (*this)(3,1);
+
+    //Third row
+    transposeMatrix(2,0) = (*this)(0,2);
+    transposeMatrix(2,1) = (*this)(1,2);
+    transposeMatrix(2,2) = (*this)(2,2);
+    transposeMatrix(2,3) = (*this)(3,2);
+
+    //Fourth row
+    transposeMatrix(3,0) = (*this)(0,3);
+    transposeMatrix(3,1) = (*this)(1,3);
+    transposeMatrix(3,2) = (*this)(2,3);
+    transposeMatrix(3,3) = (*this)(3,3);
+    return transposeMatrix;
+}
+
+//Taken from Mesa implementation of GLU library
+Matrix4 Matrix4::inverse(){
+    Matrix4 inverseMat;
+
+    float det;
+    int i, j;
+    
+    inverseMat(0,0) = mMatrix[5]  * mMatrix[10] * mMatrix[15] - 
+             mMatrix[5]  * mMatrix[11] * mMatrix[14] - 
+             mMatrix[9]  * mMatrix[6]  * mMatrix[15] + 
+             mMatrix[9]  * mMatrix[7]  * mMatrix[14] +
+             mMatrix[13] * mMatrix[6]  * mMatrix[11] - 
+             mMatrix[13] * mMatrix[7]  * mMatrix[10];
+
+    inverseMat(1,0) = -mMatrix[4]  * mMatrix[10] * mMatrix[15] + 
+              mMatrix[4]  * mMatrix[11] * mMatrix[14] + 
+              mMatrix[8]  * mMatrix[6]  * mMatrix[15] - 
+              mMatrix[8]  * mMatrix[7]  * mMatrix[14] - 
+              mMatrix[12] * mMatrix[6]  * mMatrix[11] + 
+              mMatrix[12] * mMatrix[7]  * mMatrix[10];
+
+    inverseMat(2,0) = mMatrix[4]  * mMatrix[9] * mMatrix[15] - 
+             mMatrix[4]  * mMatrix[11] * mMatrix[13] - 
+             mMatrix[8]  * mMatrix[5] * mMatrix[15] + 
+             mMatrix[8]  * mMatrix[7] * mMatrix[13] + 
+             mMatrix[12] * mMatrix[5] * mMatrix[11] - 
+             mMatrix[12] * mMatrix[7] * mMatrix[9];
+
+    inverseMat(3,0) = -mMatrix[4]  * mMatrix[9] * mMatrix[14] + 
+               mMatrix[4]  * mMatrix[10] * mMatrix[13] +
+               mMatrix[8]  * mMatrix[5] * mMatrix[14] - 
+               mMatrix[8]  * mMatrix[6] * mMatrix[13] - 
+               mMatrix[12] * mMatrix[5] * mMatrix[10] + 
+               mMatrix[12] * mMatrix[6] * mMatrix[9];
+
+    inverseMat(0,1) = -mMatrix[1]  * mMatrix[10] * mMatrix[15] + 
+              mMatrix[1]  * mMatrix[11] * mMatrix[14] + 
+              mMatrix[9]  * mMatrix[2] * mMatrix[15] - 
+              mMatrix[9]  * mMatrix[3] * mMatrix[14] - 
+              mMatrix[13] * mMatrix[2] * mMatrix[11] + 
+              mMatrix[13] * mMatrix[3] * mMatrix[10];
+
+    inverseMat(1,1) = mMatrix[0]  * mMatrix[10] * mMatrix[15] - 
+             mMatrix[0]  * mMatrix[11] * mMatrix[14] - 
+             mMatrix[8]  * mMatrix[2] * mMatrix[15] + 
+             mMatrix[8]  * mMatrix[3] * mMatrix[14] + 
+             mMatrix[12] * mMatrix[2] * mMatrix[11] - 
+             mMatrix[12] * mMatrix[3] * mMatrix[10];
+
+    inverseMat(2,1) = -mMatrix[0]  * mMatrix[9] * mMatrix[15] + 
+              mMatrix[0]  * mMatrix[11] * mMatrix[13] + 
+              mMatrix[8]  * mMatrix[1] * mMatrix[15] - 
+              mMatrix[8]  * mMatrix[3] * mMatrix[13] - 
+              mMatrix[12] * mMatrix[1] * mMatrix[11] + 
+              mMatrix[12] * mMatrix[3] * mMatrix[9];
+
+    inverseMat(3,1)= mMatrix[0]  * mMatrix[9] * mMatrix[14] - 
+              mMatrix[0]  * mMatrix[10] * mMatrix[13] - 
+              mMatrix[8]  * mMatrix[1] * mMatrix[14] + 
+              mMatrix[8]  * mMatrix[2] * mMatrix[13] + 
+              mMatrix[12] * mMatrix[1] * mMatrix[10] - 
+              mMatrix[12] * mMatrix[2] * mMatrix[9];
+
+    inverseMat(0,2) = mMatrix[1]  * mMatrix[6] * mMatrix[15] - 
+             mMatrix[1]  * mMatrix[7] * mMatrix[14] - 
+             mMatrix[5]  * mMatrix[2] * mMatrix[15] + 
+             mMatrix[5]  * mMatrix[3] * mMatrix[14] + 
+             mMatrix[13] * mMatrix[2] * mMatrix[7] - 
+             mMatrix[13] * mMatrix[3] * mMatrix[6];
+
+    inverseMat(1,2) = -mMatrix[0]  * mMatrix[6] * mMatrix[15] + 
+              mMatrix[0]  * mMatrix[7] * mMatrix[14] + 
+              mMatrix[4]  * mMatrix[2] * mMatrix[15] - 
+              mMatrix[4]  * mMatrix[3] * mMatrix[14] - 
+              mMatrix[12] * mMatrix[2] * mMatrix[7] + 
+              mMatrix[12] * mMatrix[3] * mMatrix[6];
+
+    inverseMat(2,2) = mMatrix[0]  * mMatrix[5] * mMatrix[15] - 
+              mMatrix[0]  * mMatrix[7] * mMatrix[13] - 
+              mMatrix[4]  * mMatrix[1] * mMatrix[15] + 
+              mMatrix[4]  * mMatrix[3] * mMatrix[13] + 
+              mMatrix[12] * mMatrix[1] * mMatrix[7] - 
+              mMatrix[12] * mMatrix[3] * mMatrix[5];
+
+    inverseMat(3,2) = -mMatrix[0]  * mMatrix[5] * mMatrix[14] + 
+               mMatrix[0]  * mMatrix[6] * mMatrix[13] + 
+               mMatrix[4]  * mMatrix[1] * mMatrix[14] - 
+               mMatrix[4]  * mMatrix[2] * mMatrix[13] - 
+               mMatrix[12] * mMatrix[1] * mMatrix[6] + 
+               mMatrix[12] * mMatrix[2] * mMatrix[5];
+
+    inverseMat(0,3) = -mMatrix[1] * mMatrix[6] * mMatrix[11] + 
+              mMatrix[1] * mMatrix[7] * mMatrix[10] + 
+              mMatrix[5] * mMatrix[2] * mMatrix[11] - 
+              mMatrix[5] * mMatrix[3] * mMatrix[10] - 
+              mMatrix[9] * mMatrix[2] * mMatrix[7] + 
+              mMatrix[9] * mMatrix[3] * mMatrix[6];
+
+    inverseMat(1,3) = mMatrix[0] * mMatrix[6] * mMatrix[11] - 
+             mMatrix[0] * mMatrix[7] * mMatrix[10] - 
+             mMatrix[4] * mMatrix[2] * mMatrix[11] + 
+             mMatrix[4] * mMatrix[3] * mMatrix[10] + 
+             mMatrix[8] * mMatrix[2] * mMatrix[7] - 
+             mMatrix[8] * mMatrix[3] * mMatrix[6];
+
+    inverseMat(2,3) = -mMatrix[0] * mMatrix[5] * mMatrix[11] + 
+               mMatrix[0] * mMatrix[7] * mMatrix[9] + 
+               mMatrix[4] * mMatrix[1] * mMatrix[11] - 
+               mMatrix[4] * mMatrix[3] * mMatrix[9] - 
+               mMatrix[8] * mMatrix[1] * mMatrix[7] + 
+               mMatrix[8] * mMatrix[3] * mMatrix[5];
+
+    inverseMat(3,3) = mMatrix[0] * mMatrix[5] * mMatrix[10] - 
+              mMatrix[0] * mMatrix[6] * mMatrix[9] - 
+              mMatrix[4] * mMatrix[1] * mMatrix[10] + 
+              mMatrix[4] * mMatrix[2] * mMatrix[9] + 
+              mMatrix[8] * mMatrix[1] * mMatrix[6] - 
+              mMatrix[8] * mMatrix[2] * mMatrix[5];
+
+    det = mMatrix[0] * inverseMat(0,0) + mMatrix[1] * inverseMat(1,0) + mMatrix[2] * inverseMat(2,0) + mMatrix[3] * inverseMat(3,0);
+
+   // if (det == 0)
+        //return false;
+
+    det = 1.0 / det;
+
+    for (i = 0; i < 4; ++i){
+        for (j = 0; j < 4; ++j){
+            inverseMat(i,j) = inverseMat(i,j) * det;
+        }   
+    }
+
+
+    //return true;
+    return inverseMat;
+}
+
 //Way too general, should probably change in the future since I think
 //I'll only need 4x4 stuff and this technically allows for any n matrix.
 //Traverse the matrix cell by cell and find the final value through a step of 

+ 8 - 15
src/model.cpp

@@ -1,33 +1,26 @@
 #include "model.h"
-#include "vector3D.h"
+#include "objParser.h"
 
 Model::Model(std::string path, TransformParameters &initParameters){
     OBJ::buildMeshFromFile(mMesh, path);
-    initPosition(initParameters);
     mBounds.buildAABB(mMesh);
+    mModelMatrix = Matrix4::transformMatrix(initParameters);
 }
 
 Mesh * Model::getMesh(){
     return &mMesh;
 }
 
-void Model::initPosition(TransformParameters initVals){
-    Matrix4 modelMatrix = Matrix4::transformMatrix(initVals);
-    int size = mMesh.numVertices;
-    std::vector<Vector3f> * vertices = &mMesh.vertices;
-
-    //Applying the multiplication
-    for (int i = 0;i < size; ++i){
-        (*vertices)[i] = modelMatrix.matMultVec((*vertices)[i]);
-    }
-}
-//TO DO 
 void Model::update(){
     //You'd get physics updates or user input updates or whatever here
-    //Move
-    //Update AABB
+    //Recalculate model matrix for movement or scaling
+    mBounds.update(mModelMatrix);
 }
 
 AABox *Model::getBounds(){
     return &mBounds;
+}
+
+Matrix4 &Model::getModelMatrix(){
+    return mModelMatrix;
 }

+ 9 - 14
src/renderManager.cpp

@@ -1,6 +1,5 @@
 #include "renderManager.h"
-#include <vector>
-#include "model.h"
+
 
 //Dummy constructors / Destructors
 RenderManager::RenderManager(){}
@@ -33,9 +32,9 @@ void RenderManager::render(){
 
     //Draw all meshes in the render queue so far we assume they are
     //normal triangular meshes.
-    while( !renderQueue.empty() ){
-        renderInstance.drawTriangularMesh(renderQueue.front());
-        renderQueue.pop();
+    while( !renderObjectQueue->empty() ){
+        renderInstance.drawTriangularMesh(renderObjectQueue->front());
+        renderObjectQueue->pop();
     }
 
     //Swapping pixel buffer with final rendered image with the
@@ -46,8 +45,7 @@ void RenderManager::render(){
     renderInstance.setCameraToRenderFrom(nullptr);
 }
 
-//Gets the list of visible models from the current scene and 
-//extracts a list of pointers to their meshes. 
+//Gets the list of visible models from the current scene
 //Done every frame in case scene changes
 void RenderManager::buildRenderQueue(){
     
@@ -55,13 +53,8 @@ void RenderManager::buildRenderQueue(){
     Scene* currentScene = sceneLocator->getCurrentScene();
     renderInstance.setCameraToRenderFrom(currentScene->getCurrentCamera());
 
-    //Get pointers to the visible models
-    std::vector<Model*>* visibleModels = currentScene->getVisiblemodels();
-    
-    //Builds render queue from visibleModels list
-    for (Model* model : *visibleModels ){
-        renderQueue.push(model->getMesh());
-    }
+    //Get pointers to the visible model queu
+    renderObjectQueue = currentScene->getVisiblemodels();
 }
 
 bool RenderManager::initSoftwareRenderer(){
@@ -77,3 +70,5 @@ bool RenderManager::initSoftwareRenderer(){
 
 
 
+
+

+ 6 - 6
src/scene.cpp

@@ -1,4 +1,4 @@
-#include <scene.h>
+#include "scene.h"
 #include "objParser.h"
 
 Scene::Scene(std::string path){
@@ -15,7 +15,6 @@ Scene::~Scene(){
 }
 
 void Scene::update(){
-    visibleModels.clear();
     mainCamera.update();
     for(Model *models : modelsInScene){
         models->update();
@@ -35,7 +34,8 @@ bool Scene::loadSceneModels(std::string &path){
     }
     else{
         TransformParameters initParameters;
-        initParameters.translation = Vector3f(0, -5, 0);
+        //initParameters.rotation = Vector3f(90*M_PI/180.0f, 0 , 0);
+        initParameters.translation = Vector3f(0, 0, 0);
         modelsInScene.push_back(new Model(fullPath, initParameters));
         return false;
     }
@@ -47,13 +47,13 @@ void Scene::frustrumCulling(){
 
         visible = mainCamera.checkVisibility(model->getBounds());
 
-        if (visible){
-            visibleModels.push_back(model);
+        if (visible) {
+            visibleModels.push(model);
         }
     }
 }
 
-std::vector<Model*>* Scene::getVisiblemodels(){
+ std::queue<Model*>* Scene::getVisiblemodels(){
     return &visibleModels;
 }
 

+ 1 - 1
src/sceneManager.cpp

@@ -23,7 +23,7 @@ bool SceneManager::switchScene(){
 
 //for now just loads a single .obj based on the  given string
 bool SceneManager::loadScene(){
-    currentScene = new Scene("dragon.obj");
+    currentScene = new Scene("cow.obj");
     return  !currentScene->checkIfEmpty(); //True if empty, so it's negated for startup
 }
 

+ 10 - 7
src/softwareRenderer.cpp

@@ -1,5 +1,6 @@
 #include "softwareRenderer.h"
 #include "shader.h"
+#include "mesh.h"
 
 SoftwareRenderer::SoftwareRenderer(){}
 SoftwareRenderer::~SoftwareRenderer(){}
@@ -20,9 +21,9 @@ void SoftwareRenderer::shutDown(){
     }
 }
 
-void SoftwareRenderer::drawTriangularMesh(Mesh* triMesh){
-
+void SoftwareRenderer::drawTriangularMesh(Model * currentModel){
     //Getting the vertices, faces 
+    Mesh *triMesh = currentModel->getMesh();
     std::vector<Vector3i> * vIndices = &triMesh->vertexIndices;
     std::vector<Vector3i> * nIndices = &triMesh->normalsIndices;
     std::vector<Vector3f> * vertices = &triMesh->vertices;
@@ -37,11 +38,14 @@ void SoftwareRenderer::drawTriangularMesh(Mesh* triMesh){
     GouraudShader shader;
 
     //Basic light direction
-    Vector3f lightDir{1, 0, 1};
+    Vector3f lightDir{1, 1, 1};
     Vector3f viewDir;
 
     //Building ModelViewProjection matrix
-    Matrix4 MVP = (mCamera->projectionMatrix)*(mCamera->viewMatrix);
+    shader.MV  = (mCamera->viewMatrix)*(currentModel->getModelMatrix());
+    shader.MVP = (mCamera->projectionMatrix)*shader.MV;
+    shader.V   = (mCamera->viewMatrix);
+    shader.N   = (shader.MV.inverse()).transpose(); 
 
     //Iterate through every triangle
     for (int j = 0; j < numFaces; ++j){
@@ -54,12 +58,11 @@ void SoftwareRenderer::drawTriangularMesh(Mesh* triMesh){
         buildTri(n,normalPrim, *normals);
 
         //Skip faces that are pointing away from us
-       if (backFaceCulling(trianglePrimitive, viewDir)) continue;
+       //if (backFaceCulling(trianglePrimitive, viewDir)) continue;
 
         //Apply vertex shader
         for(int i = 0; i < 3; ++i){
-            //Vector3 normal = (mCamera->viewMatrix).matMultVec(normalPrim[i]);
-            trianglePrimitive[i] = shader.vertex(trianglePrimitive[i], MVP, normalPrim[i], lightDir.normalized(), i, viewDir);
+            trianglePrimitive[i] = shader.vertex(trianglePrimitive[i], normalPrim[i], lightDir.normalized(), i);
         }
 
         //Skip triangles that are outside viewing frustrum

Some files were not shown because too many files changed in this diff