angel 7 жил өмнө
parent
commit
134acfec68

+ 1 - 1
include/rasterizer.h

@@ -10,7 +10,7 @@ class Rasterizer{
     public:
         Rasterizer(Canvas *canvas) :mCanvas(canvas){}
 
-        void drawTriangles(Vector3 &v1, Vector3 &v2, Vector3 &v3);
+        void drawTriangles(Vector3 &v1, Vector3 &v2, Vector3 &v3, float intensity);
 
         void drawWireFrame(Vector3 &v1, Vector3 &v2, Vector3 &v3);
 

+ 2 - 3
src/engine.cpp

@@ -86,16 +86,15 @@ void Engine::loadModels(){
     //sceneModels->describeMesh();
 }
 
-
 //This should be its own class in the future
 void Engine::updateCamera(){
     float t = static_cast<float>(SDL_GetTicks());
-    float radius = 7;
+    float radius = 6;
     float camX   = std::sin(t/4000) * radius;
     float camZ   = std::cos(t/4000) * radius;
     Vector3 pos(camX, 0, camZ);
     Vector3 tar;
     Vector3 v(0,1,0);
     viewMatrix = Matrix4::lookAt(pos,tar,v);
-    viewMatrix = (Matrix4::makeTranslateMat(0,camX*0.25,0)*viewMatrix);
+    //viewMatrix = (Matrix4::makeTranslateMat(0,camX*0.25,0)*viewMatrix);
 }

+ 1 - 2
src/matrix.cpp

@@ -171,7 +171,6 @@ Matrix4 Matrix4::makeProjectionMatrix(float fov, float AR, float near, float far
     float right =  top * AR;
     float left  =  bot * AR;
     Matrix4 projectionMat;
-    float scale   = 1 / tan( (fov/2) * (M_PI / 180) );
 
 
     //First Row
@@ -195,7 +194,7 @@ Matrix4 Matrix4::makeProjectionMatrix(float fov, float AR, float near, float far
 Matrix4 Matrix4::lookAt(Vector3& position, Vector3& target, Vector3& temp){
 
     Vector3 forward = (position - target).normalized();
-    Vector3 side    = forward.crossProduct(temp.normalized());
+    Vector3 side    = temp.crossProduct(forward);
     Vector3 up      = forward.crossProduct(side);
     //Vector3 up      = side.crossProduct(forward);
 

+ 32 - 28
src/rasterizer.cpp

@@ -32,42 +32,46 @@ void Rasterizer::testPattern(){
 }
 
 //Should probably do something fancier in the future
-void Rasterizer::drawTriangles(Vector3 &v0, Vector3 &v1, Vector3 &v2 ){
-    std::array<int, 3> xVerts;
-    std::array<int, 3> yVerts;
-    xVerts[0] = (v0.x + 1 ) * mCanvas->mWidth * 0.5;
-    yVerts[0] = (-v0.y + 1 ) * mCanvas->mHeight * 0.5;
+void Rasterizer::drawTriangles(Vector3 &v0, Vector3 &v1, Vector3 &v2, float intensity ){
+        Uint32 color = SDL_MapRGBA(mappingFormat,
+                    256*intensity,256*intensity,256*intensity,0xFF);
 
-    xVerts[1] = (v1.x +1 ) * mCanvas->mWidth  * 0.5;
-    yVerts[1] = (-v1.y +1 ) * mCanvas->mHeight * 0.5;
+        std::array<int, 3> xVerts;
+        std::array<int, 3> yVerts;
+        xVerts[0] = (v0.x + 1 ) * mCanvas->mWidth * 0.5;
+        yVerts[0] = (-v0.y + 1 ) * mCanvas->mHeight * 0.5;
 
-    xVerts[2] = (v2.x +1 ) * mCanvas->mWidth  * 0.5;
-    yVerts[2] = (-v2.y +1 ) * mCanvas->mHeight * 0.5;
+        xVerts[1] = (v1.x +1 ) * mCanvas->mWidth  * 0.5;
+        yVerts[1] = (-v1.y +1 ) * mCanvas->mHeight * 0.5;
 
-    int xMax = *std::max_element(xVerts.begin(),xVerts.end());
-    int yMax = *std::max_element(yVerts.begin(),yVerts.end());
+        xVerts[2] = (v2.x +1 ) * mCanvas->mWidth  * 0.5;
+        yVerts[2] = (-v2.y +1 ) * mCanvas->mHeight * 0.5;
 
-    int xMin = *std::min_element(xVerts.begin(),xVerts.end());
-    int yMin = *std::min_element(yVerts.begin(),yVerts.end());
+        int xMax = *std::max_element(xVerts.begin(),xVerts.end());
+        int yMax = *std::max_element(yVerts.begin(),yVerts.end());
 
+        int xMin = *std::min_element(xVerts.begin(),xVerts.end());
+        int yMin = *std::min_element(yVerts.begin(),yVerts.end());
 
-    for(int y = yMin; y < yMax; ++y){
-        for(int x = xMin; x < xMax; ++x){
-            float edge1 = (x-xVerts[0])*(yVerts[1]-yVerts[0]) 
-                            - (xVerts[1]-xVerts[0])*(y-yVerts[0]);
-            float edge2 = (x-xVerts[1])*(yVerts[2]-yVerts[1]) 
-                            - (xVerts[2]-xVerts[1])*(y-yVerts[1]);
-            float edge3 = (x-xVerts[2])*(yVerts[0]-yVerts[2]) 
-                            - (xVerts[0]-xVerts[2])*(y-yVerts[2]);
 
-            if(edge1 >= 0 && edge2 >= 0 && edge3 >= 0){
-                setPixelColor(white, x, y);
-            } 
-        
+        for(int y = yMin; y <= yMax; ++y){
+            for(int x = xMin; x <= xMax; ++x){
+                float edge1 = (x-xVerts[0])*(yVerts[1]-yVerts[0]) 
+                                - (xVerts[1]-xVerts[0])*(y-yVerts[0]);
+
+                float edge2 = (x-xVerts[1])*(yVerts[2]-yVerts[1]) 
+                                - (xVerts[2]-xVerts[1])*(y-yVerts[1]);
+
+                float edge3 = (x-xVerts[2])*(yVerts[0]-yVerts[2]) 
+                                - (xVerts[0]-xVerts[2])*(y-yVerts[2]);
+
+                //If any of the edge functions are smaller than zero, discard the point
+                if(edge1 < 0 || edge2 < 0 || edge3 < 0) continue;
+                setPixelColor(color, x, y);
+            }
         }
-    }
-    
-}   
+
+}  
 
 void Rasterizer::drawWireFrame(Vector3 &v1, Vector3 &v2, Vector3 &v3 ){
     drawLine(v1, v2, red);

+ 12 - 3
src/renderManager.cpp

@@ -32,7 +32,7 @@ bool RenderManager::startUp(WindowManager WindowManager){
 }
 
 void RenderManager::createProjectionMatrix(){
-    float fov = 75;
+    float fov = 60;
     float aspectRatio = mainCanvas->mWidth / (float)mainCanvas->mHeight;
     float near = 0.1;
     float far  = 100;
@@ -83,6 +83,7 @@ void RenderManager::render(Model *models, Matrix4 &viewMatrix){
 
     //Combination of both matrices
     Matrix4 viewProjectionMatrix = projectionMatrix*viewMatrix;
+    Vector3 forward(viewMatrix(2,0),viewMatrix(2,1),viewMatrix(2,2));
 
     //Getting the meshes and faces 
     Mesh * modelMesh = models->getMesh();
@@ -98,8 +99,16 @@ void RenderManager::render(Model *models, Matrix4 &viewMatrix){
         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;
        
-        //Rasterizing
-        raster->drawTriangles(v1,v2,v3);
+        //Back face culling
+        Vector3 N1 = (*vertices)[f.y-1] - (*vertices)[f.x-1];
+        Vector3 N2 = (*vertices)[f.z-1] - (*vertices)[f.x-1];
+        Vector3 N  = N1.crossProduct(N2);
+        N = N.normalized();
+        float intensity = N.dotProduct(forward);
+        if(intensity > 0){
+            //rasterizing
+            raster->drawTriangles(v1,v2,v3,intensity);
+        }
     }
     //Apply the pixel changes and redraw
     updateScreen();

+ 1 - 1
src/vector.cpp

@@ -26,7 +26,7 @@ Vector3& Vector3::normalized(){
 Vector3 Vector3::crossProduct(Vector3 &rhs){
     Vector3 temp;
     temp.x = (this->y)*rhs.z - (this->z)*rhs.y;
-    temp.y = (this->x)*rhs.z - (this->z)*rhs.x;
+    temp.y = (this->z)*rhs.x - (this->x)*rhs.z;
     temp.z = (this->x)*rhs.y - (this->y)*rhs.x;
     return temp;
 }