Browse Source

Starting big engine refactor. Placing revert point.

angel 7 years ago
parent
commit
f52fc2a64b
7 changed files with 127 additions and 25 deletions
  1. 18 0
      include/bound3.h
  2. 1 0
      include/engine.h
  3. 4 1
      include/model.h
  4. 2 0
      include/renderManager.h
  5. 6 6
      src/main.cpp
  6. 60 0
      src/model.cpp
  7. 36 18
      src/renderManager.cpp

+ 18 - 0
include/bound3.h

@@ -0,0 +1,18 @@
+#ifndef BOUND3_H
+#define BOUND3_H
+
+#include "vector3.h"
+
+//Struct containing vertex data for a tight bounding box around a model.
+//Primarily for use in frustum culling
+struct Bound3{
+    float mMinX = 0.0;
+    float mMinY = 0.0;
+    float mMinZ = 0.0;
+
+    float mMaxX = 0.0;
+    float mMaxY = 0.0;
+    float mMaxZ = 0.0;
+};
+
+#endif

+ 1 - 0
include/engine.h

@@ -6,6 +6,7 @@
 #include "renderManager.h"
 #include "renderManager.h"
 #include "inputManager.h"
 #include "inputManager.h"
 #include "model.h"
 #include "model.h"
+#include "matrix.h"
 
 
 class Engine{
 class Engine{
 
 

+ 4 - 1
include/model.h

@@ -3,7 +3,8 @@
 
 
 #include "mesh.h"
 #include "mesh.h"
 #include "string"
 #include "string"
-#include <matrix.h>
+#include "bound3.h"
+#include "matrix.h"
 
 
 class Model{
 class Model{
 
 
@@ -14,10 +15,12 @@ class Model{
         void initPosition(TransformParameters initPos);
         void initPosition(TransformParameters initPos);
 
 
     private:
     private:
+        void buildBoundaryBox();
         void buildMesh(std::string path);
         void buildMesh(std::string path);
         void loadVertices(std::ifstream &fileHandle);
         void loadVertices(std::ifstream &fileHandle);
         void loadFaces(std::ifstream &fileHandle);
         void loadFaces(std::ifstream &fileHandle);
         Mesh mMesh;
         Mesh mMesh;
+        Bound3 mBounds;
 };
 };
 
 
 #endif
 #endif

+ 2 - 0
include/renderManager.h

@@ -34,6 +34,8 @@ class RenderManager {
 
 
         //Per vertex stuff
         //Per vertex stuff
         
         
+        //Culling
+        bool frustrumCulling(Model *model, Matrix4 &viewMatrix);
 
 
         SDL_Renderer *mainRenderer;
         SDL_Renderer *mainRenderer;
         Texture screenTexture;
         Texture screenTexture;

+ 6 - 6
src/main.cpp

@@ -2,15 +2,15 @@
 
 
 int main(int argc, char *argv[]){
 int main(int argc, char *argv[]){
 
 
-    Engine mainEngine;
-    if(mainEngine.startUp()){
-        mainEngine.loadModels();
-        mainEngine.mainLoop();
+    Engine SSGE; //Simple Software Graphics Engine
+    if(SSGE.startUp()){
+        SSGE.loadModels();
+        SSGE.mainLoop();
     }
     }
     else{
     else{
-        printf("Engine could not initialize successfully. Shutting down.\n");
+        printf("SSGE could not initialize successfully. Shutting down.\n");
     }
     }
-    mainEngine.shutDown();
+    SSGE.shutDown();
 
 
     return 0;
     return 0;
 }
 }

+ 60 - 0
src/model.cpp

@@ -5,9 +5,11 @@
 #include <sstream>
 #include <sstream>
 #include "vector3.h"
 #include "vector3.h"
 #include "matrix.h"
 #include "matrix.h"
+#include <limits>
 
 
 Model::Model(std::string path){
 Model::Model(std::string path){
     buildMesh(path);
     buildMesh(path);
+    buildBoundaryBox();
 }
 }
 
 
 Mesh * Model::getMesh(){
 Mesh * Model::getMesh(){
@@ -59,6 +61,8 @@ void Model::loadFaces(std::ifstream &file){
 }
 }
 
 
 void Model::loadVertices(std::ifstream &file){
 void Model::loadVertices(std::ifstream &file){
+    
+
     std::string line, v, x ,y ,z;
     std::string line, v, x ,y ,z;
     while(!file.eof()){
     while(!file.eof()){
         std::getline(file,line);
         std::getline(file,line);
@@ -84,4 +88,60 @@ void Model::initPosition(TransformParameters initVals){
     for (int i = 0;i < size; ++i){
     for (int i = 0;i < size; ++i){
         (*vertices)[i] = modelMatrix.matMultVec((*vertices)[i]);
         (*vertices)[i] = modelMatrix.matMultVec((*vertices)[i]);
     }
     }
+}
+
+//I know this is incredibly inefficient, I only need to do it once for now
+//If if I ever need something faster I will move this into the vertex loading
+//function and make use of the read there.
+void Model::buildBoundaryBox(){
+    float minX = std::numeric_limits<float>::max();
+    float maxX = std::numeric_limits<float>::min();
+
+    float minY = minX;
+    float maxY = maxX;
+
+    float minZ = minX;
+    float maxZ = maxX;
+
+    for(int i = 0; i < mMesh.numVertices; ++i){
+        float x = mMesh.vertices[i].x;
+        float y = mMesh.vertices[i].y;
+        float z = mMesh.vertices[i].z;
+
+        if(x > maxX){
+            maxX = x;
+        }
+        else{
+            if(x < minX){
+                minX = x;
+            }
+        }
+
+        if(y > maxY){
+            maxY = y;
+        }
+        else{
+            if(y < minY){
+                minY = y;
+            }
+        }
+
+        if(z > maxZ){
+            maxZ = z;
+        }
+        else{
+            if(z < minZ){
+                minZ = z;
+            }
+        }
+    }
+
+    mBounds.mMaxX = maxX;
+    mBounds.mMaxY = maxY;
+    mBounds.mMaxZ = maxZ;
+
+    mBounds.mMinX = minX;
+    mBounds.mMinY = minY;
+    mBounds.mMinZ = minZ;
+
 }
 }

+ 36 - 18
src/renderManager.cpp

@@ -73,6 +73,7 @@ bool RenderManager::createRenderer(SDL_Window *window){
 void RenderManager::shutDown(){
 void RenderManager::shutDown(){
     delete raster;
     delete raster;
     delete mainCanvas->mBuffer;
     delete mainCanvas->mBuffer;
+    delete mainCanvas->mDBuffer;
     delete mainCanvas;
     delete mainCanvas;
     screenTexture.free();
     screenTexture.free();
     SDL_DestroyRenderer(mainRenderer);
     SDL_DestroyRenderer(mainRenderer);
@@ -84,40 +85,48 @@ void RenderManager::render(Model *models, Matrix4 &viewMatrix){
     //Clear Screen back to black
     //Clear Screen back to black
     clearScreen();
     clearScreen();
 
 
-    //Combination of both matrices
-    Matrix4 viewProjectionMatrix = projectionMatrix*viewMatrix;
-    Vector3 forward(viewMatrix(2,0),viewMatrix(2,1),viewMatrix(2,2));
-
     //Getting the meshes and faces 
     //Getting the meshes and faces 
     Mesh * modelMesh = models->getMesh();
     Mesh * modelMesh = models->getMesh();
     std::vector<Vector3> * faces = &modelMesh->faces;
     std::vector<Vector3> * faces = &modelMesh->faces;
     std::vector<Vector3> * vertices = &modelMesh->vertices;
     std::vector<Vector3> * vertices = &modelMesh->vertices;
 
 
+    //If the frustrum culling returns true it means the model has been culled
+    //Because no other models are in the scene we return
+    if (frustrumCulling(models, viewMatrix)){
+        printf("Model culled!\n");
+        return;
+    } 
+
     //Kind of a vertex shader I guess?
     //Kind of a vertex shader I guess?
     for (Vector3 f : *faces ){
     for (Vector3 f : *faces ){
+
+        //View matrix transform
         Vector3 v0 = viewMatrix.matMultVec((*vertices)[f.x-1]); //-1 because .obj file starts face count
         Vector3 v0 = viewMatrix.matMultVec((*vertices)[f.x-1]); //-1 because .obj file starts face count
         Vector3 v1 = viewMatrix.matMultVec((*vertices)[f.y-1]); // from 1. Should probably fix this 
         Vector3 v1 = viewMatrix.matMultVec((*vertices)[f.y-1]); // from 1. Should probably fix this 
         Vector3 v2 = viewMatrix.matMultVec((*vertices)[f.z-1]); // At some point
         Vector3 v2 = viewMatrix.matMultVec((*vertices)[f.z-1]); // At some point
 
 
-        //Clipping
-        //if(v1.x < -1 || v1.x > 1 || v1.y < -1 || v1.y > 1 || v1.z > 1 || v1.z < -1) continue;
-        //if(v2.x < -1 || v2.x > 1 || v2.y < -1 || v2.y > 1 || v2.z > 1 || v2.z < -1) continue;
-        //if(v3.x < -1 || v3.x > 1 || v3.y < -1 || v3.y > 1 || v3.z > 1 || v3.z < -1) continue;
-       
-        //Back face culling in world space
+        //Back face culling in view space
         Vector3 N1 = v1 - v0;
         Vector3 N1 = v1 - v0;
         Vector3 N2 = v2 - v0;
         Vector3 N2 = v2 - v0;
         Vector3 N  = (N1.crossProduct(N2)).normalized();
         Vector3 N  = (N1.crossProduct(N2)).normalized();
         Vector3 neg = (v0.neg()).normalized();
         Vector3 neg = (v0.neg()).normalized();
         float intensity = N.dotProduct(neg);
         float intensity = N.dotProduct(neg);
-        if(intensity > 0.0){
-            v0 = projectionMatrix.matMultVec(v0);
-            v1 = projectionMatrix.matMultVec(v1);
-            v2 = projectionMatrix.matMultVec(v2);
-            
-            //Rasterizing and shading in one
-            raster->drawTriangles(v0,v1,v2,intensity);   
-        }
+        if(intensity < 0.0) continue; //Exit if you can't even see it
+
+
+        //stupid frustrum culling
+        //if(v1.x < -1 || v1.x > 1 || v1.y < -1 || v1.y > 1 || v1.z > 1 || v1.z < -1) continue;
+        //if(v2.x < -1 || v2.x > 1 || v2.y < -1 || v2.y > 1 || v2.z > 1 || v2.z < -1) continue;
+        //if(v3.x < -1 || v3.x > 1 || v3.y < -1 || v3.y > 1 || v3.z > 1 || v3.z < -1) continue;
+
+        // Projection matrix transformation
+        v0 = projectionMatrix.matMultVec(v0);
+        v1 = projectionMatrix.matMultVec(v1);
+        v2 = projectionMatrix.matMultVec(v2);
+
+        //Rasterizing and shading in one
+        raster->drawTriangles(v0,v1,v2,intensity);
+               
     }
     }
     //Apply the pixel changes and redraw
     //Apply the pixel changes and redraw
     updateScreen();
     updateScreen();
@@ -143,3 +152,12 @@ void RenderManager::clearScreen(){
     }
     }
 }
 }
 
 
+bool RenderManager::frustrumCulling(Model *model, Matrix4 &viewMatrix){
+    bool cull = false;
+    Matrix4 viewProjectionMat = projectionMatrix*viewMatrix;
+
+    
+
+    return cull;
+}
+