瀏覽代碼

Patched interpolation , still need better fix.

angel 7 年之前
父節點
當前提交
dcc207bc6e
共有 5 個文件被更改,包括 51 次插入18 次删除
  1. 2 0
      include/vector3.h
  2. 2 2
      src/engine.cpp
  3. 26 4
      src/rasterizer.cpp
  4. 16 12
      src/renderManager.cpp
  5. 5 0
      src/vector.cpp

+ 2 - 0
include/vector3.h

@@ -25,6 +25,8 @@ struct Vector3{
 
     Vector3 crossProduct(Vector3 &rhs);
 
+    Vector3 neg();
+
     float   dotProduct(Vector3 &rhs);
 
     void print();

+ 2 - 2
src/engine.cpp

@@ -78,7 +78,7 @@ void Engine::loadModels(){
     //We also initialize the model position here position here
     TransformParameters initParameters;
     //initParameters.scaling = Vector3(1, 60, 60);
-    initParameters.rotation = Vector3(0,-45* M_PI/180,0);
+    initParameters.rotation = Vector3(0,0,0);
     initParameters.translation = Vector3(0, -1, 0);
 
     sceneModels->initPosition(initParameters);
@@ -89,7 +89,7 @@ void Engine::loadModels(){
 //This should be its own class in the future
 void Engine::updateCamera(){
     float t = static_cast<float>(SDL_GetTicks());
-    float radius = 7;
+    float radius = 8;
     float camX   = std::sin(t/4000) * radius;
     float camZ   = std::cos(t/4000) * radius;
     Vector3 pos(camX, 0, camZ);

+ 26 - 4
src/rasterizer.cpp

@@ -35,7 +35,7 @@ void Rasterizer::testPattern(){
 //Also shades based on a light coming directly from the camera
 void Rasterizer::drawTriangles(Vector3 &a, Vector3 &b, Vector3 &c, float intensity ){
     Uint32 color = SDL_MapRGBA(mappingFormat,
-                256*intensity,256*intensity,256*intensity,0xFF);
+                255*intensity,255*intensity,255*intensity,0xFF);
 
     //Converting to screen space
     std::array<int, 3> xVerts;
@@ -69,6 +69,8 @@ void Rasterizer::drawTriangles(Vector3 &a, Vector3 &b, Vector3 &c, float intensi
     //printf("ymin: %d, ymax: %d, xmin: %d, xmax: %d\n",yMin, yMax, xMin, xMax);
     for(int y = yMin; y <= yMax; ++y){
         for(int x = xMin; x <= xMax; ++x){
+            //Throw pixel away if not in screen
+            if( x < 0 || x > mCanvas->mWidth || y < 0 || y > mCanvas->mHeight ) continue;
 
             float lambda0 = ((x-xVerts[0])*(yVerts[1]-yVerts[0]) 
                             - (xVerts[1]-xVerts[0])*(y-yVerts[0])) / area;
@@ -78,14 +80,34 @@ void Rasterizer::drawTriangles(Vector3 &a, Vector3 &b, Vector3 &c, float intensi
 
             float lambda2 = ((x-xVerts[2])*(yVerts[0]-yVerts[2]) 
                             - (xVerts[0]-xVerts[2])*(y-yVerts[2])) / area;
-            float depth = lambda0*zVerts[0] + lambda1*zVerts[1] + lambda2*zVerts[2];
 
-            //If any of the edge functions are smaller than zero, discard the point
+            //Throw pixel away if not in triangle
             if(lambda0 < 0 || lambda1 < 0 || lambda2 < 0) continue;
+
+            float depth = lambda1*zVerts[0] + lambda2*zVerts[1] + lambda0*zVerts[2];
+
+            //If any of the edge functions are smaller than zero, discard the point
+            
             if(getDepthBufferAtLocation(x,y) < depth){
-                if( x < 0 || x > mCanvas->mWidth || y < 0 || y > mCanvas->mHeight ) continue;
+                float depthDiff = std::abs(depth - getDepthBufferAtLocation(x,y));
+                if(depth <= 1.0){
+                    if(depthDiff < 0.1){
+                    //printf("Bari: %f, %f, %f\n",lambda0,lambda1,lambda2);
+                    //printf("Depth Buffer: %f\n",getDepthBufferAtLocation(x,y));
+                    //printf("Pixel Depth: %f \n",depth);
                     setDepthBufferAtLocation(x,y,depth);
                     setPixelColor(x, y, color);
+
+                    }
+                    else{
+
+                        setDepthBufferAtLocation(x,y,depth);
+                        setPixelColor(x, y, color);
+                    }
+                }
+                
+                
+                    
             } 
 
         }

+ 16 - 12
src/renderManager.cpp

@@ -58,8 +58,6 @@ bool RenderManager::createCanvas(){
     for(int i = 0; i < mainCanvas->mPixelCount;++i){
         mainCanvas->mDBuffer[i]  = 0.0f;
     }
-    //printf("I:%p, %p | P:%p, %p\n",mainCanvas->mBuffer,mainCanvas->mBuffer + mainCanvas->mPixelCount -1, mainCanvas->mDBuffer,mainCanvas->mDBuffer + mainCanvas->mPixelCount -1);
-    //SDL_Delay(3000);
     return mainCanvas != NULL;
 }
 
@@ -96,23 +94,29 @@ void RenderManager::render(Model *models, Matrix4 &viewMatrix){
     std::vector<Vector3> * vertices = &modelMesh->vertices;
 
     //Kind of a vertex shader I guess?
-    //printf("Starting per face operations..\n");
     for (Vector3 f : *faces ){
-        Vector3 v1 = viewProjectionMatrix.matMultVec((*vertices)[f.x-1]); //-1 because .obj file starts face count
-        Vector3 v2 = viewProjectionMatrix.matMultVec((*vertices)[f.y-1]); // from 1. Should probably fix this 
-        Vector3 v3 = viewProjectionMatrix.matMultVec((*vertices)[f.z-1]); // At some point
+        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 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
-        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);
+        Vector3 N1 = v1 - v0;
+        Vector3 N2 = v2 - v0;
+        Vector3 N  = (N1.crossProduct(N2)).normalized();
+        Vector3 neg = (v0.neg()).normalized();
+        float intensity = N.dotProduct(neg);
         if(intensity > 0.0){
-            raster->drawTriangles(v1,v2,v3,intensity);   
+            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

+ 5 - 0
src/vector.cpp

@@ -43,4 +43,9 @@ float Vector3::length(){
 
 void Vector3::print(){
     printf("Vec: %f\t%f\t%f\n",x,y,z);
+}
+
+Vector3 Vector3::neg(){
+        Vector3 negval(-this->x,-this->y,-this->z);
+    return negval;
 }