Browse Source

Reverted to flat shading, made sure that worked.

angel 7 years ago
parent
commit
c56aab7f71
8 changed files with 80 additions and 50 deletions
  1. 6 5
      include/shader.h
  2. 1 1
      include/softwareRenderer.h
  3. BIN
      models/.elephant.obj.swp
  4. 44 30
      models/cube.obj
  5. 2 0
      src/objParser.cpp
  6. 1 2
      src/rasterizer.cpp
  7. 1 1
      src/scene.cpp
  8. 25 11
      src/softwareRenderer.cpp

+ 6 - 5
include/shader.h

@@ -8,7 +8,7 @@
 
 struct IShader {
     virtual ~IShader() {};
-    virtual Vector3 vertex(Vector3 &vertex, Matrix4 &MVP, float intensity, Vector3 &normals, Vector3 &light, int i) = 0;
+    virtual Vector3 vertex(Vector3 &vertex, Matrix4 &MVP, Vector3 &normals, Vector3 &light, int i) = 0;
     virtual bool fragment(Vector3 &bari, Vector3 &color, float &depth, Vector3 &zVerts) = 0;
 };
 
@@ -16,14 +16,15 @@ struct FlatShader : public IShader {
     float varIntensity;
     Vector3 rgb{255,255,255};
 
-     Vector3 vertex(Vector3 &vertex, Matrix4 &MVP, float intensity, Vector3 &normals, Vector3 &light, int index) override{
-        varIntensity = intensity;
+     Vector3 vertex(Vector3 &vertex, Matrix4 &MVP, Vector3 &normals, Vector3 &light, int index) override{
+        varIntensity = std::max(0.0f,normals.dotProduct(light));
         return MVP.matMultVec(vertex);
     }
 
      bool fragment(Vector3 &bari, Vector3 &color, float &depth, Vector3 &zVerts) override{
-        color = rgb*varIntensity;
         depth = bari.dotProduct(zVerts);
+        color = rgb*varIntensity;
+        //color.print();
         return false;
     }
 
@@ -33,7 +34,7 @@ struct GouraudShader : public IShader {
     Vector3 varying_intensity;
     Vector3 rgb{255,255,255};
 
-    Vector3 vertex(Vector3 &vertex, Matrix4 &MVP,float intensity, Vector3 &normals, Vector3 &light, int index) override{
+    Vector3 vertex(Vector3 &vertex, Matrix4 &MVP, Vector3 &normals, Vector3 &light, int index) override{
         //normals.print();
         varying_intensity.data[index] = std::max(0.0f, normals.dotProduct(light));
         return MVP.matMultVec(vertex);

+ 1 - 1
include/softwareRenderer.h

@@ -35,7 +35,7 @@ class SoftwareRenderer {
         void buildTri(Vector3 &f, Vector3 *trianglePrim, std::vector<Vector3> &verts);
 
         //Culling methods
-        bool backFaceCulling(Vector3 *trianglePrim, float &intensity);
+        bool backFaceCulling(Vector3 *trianglePrim);
 
         
         //Pointer to the scene's target camera

BIN
models/.elephant.obj.swp


+ 44 - 30
models/cube.obj

@@ -1,33 +1,47 @@
 # cube.obj
 #
- 
-g cube
- 
-v  0.0  0.0  0.0
-v  0.0  0.0  1.0
-v  0.0  1.0  0.0
-v  0.0  1.0  1.0
-v  1.0  0.0  0.0
-v  1.0  0.0  1.0
-v  1.0  1.0  0.0
-v  1.0  1.0  1.0
 
-vn  0.0  0.0  1.0
-vn  0.0  0.0 -1.0
-vn  0.0  1.0  0.0
-vn  0.0 -1.0  0.0
-vn  1.0  0.0  0.0
-vn -1.0  0.0  0.0
- 
-f  1//2  7//2  5//2
-f  1//2  3//2  7//2 
-f  1//6  4//6  3//6 
-f  1//6  2//6  4//6 
-f  3//3  8//3  7//3 
-f  3//3  4//3  8//3 
-f  5//5  7//5  8//5 
-f  5//5  8//5  6//5 
-f  1//4  5//4  6//4 
-f  1//4  6//4  2//4 
-f  2//1  6//1  8//1 
-f  2//1  8//1  4//1 
+o cube
+mtllib cube.mtl
+
+v -0.500000 -0.500000 0.500000
+v 0.500000 -0.500000 0.500000
+v -0.500000 0.500000 0.500000
+v 0.500000 0.500000 0.500000
+v -0.500000 0.500000 -0.500000
+v 0.500000 0.500000 -0.500000
+v -0.500000 -0.500000 -0.500000
+v 0.500000 -0.500000 -0.500000
+
+vt 0.000000 0.000000
+vt 1.000000 0.000000
+vt 0.000000 1.000000
+vt 1.000000 1.000000
+
+vn 0.000000 0.000000 1.000000
+vn 0.000000 1.000000 0.000000
+vn 0.000000 0.000000 -1.000000
+vn 0.000000 -1.000000 0.000000
+vn 1.000000 0.000000 0.000000
+vn -1.000000 0.000000 0.000000
+
+g cube
+usemtl cube
+s 1
+f 1/1/1 2/2/1 3/3/1
+f 3/3/1 2/2/1 4/4/1
+s 2
+f 3/1/2 4/2/2 5/3/2
+f 5/3/2 4/2/2 6/4/2
+s 3
+f 5/4/3 6/3/3 7/2/3
+f 7/2/3 6/3/3 8/1/3
+s 4
+f 7/1/4 8/2/4 1/3/4
+f 1/3/4 8/2/4 2/4/4
+s 5
+f 2/1/5 8/2/5 4/3/5
+f 4/3/5 8/2/5 6/4/5
+s 6
+f 7/1/6 1/2/6 5/3/6
+f 5/3/6 1/2/6 3/4/6

+ 2 - 0
src/objParser.cpp

@@ -43,8 +43,10 @@ void OBJ::loadFileData(Mesh &mesh, std::ifstream &file){
             std::vector<std::string> splitY = split(y,delimeter);
             std::vector<std::string> splitZ = split(z,delimeter);
             for(int i = 0; i < splitX.size(); ++i){
+                //printf("%s\n",splitX[i].c_str());
                 indices[i] = Vector3(std::stof(splitX[i])-1,std::stof(splitY[i])-1,std::stof(splitZ[i])-1);
             }
+            printf("\n");
             mesh.vertexIndices.push_back(indices[0]);
             mesh.textureIndices.push_back(indices[1]);
             mesh.normalsIndices.push_back(indices[2]);

+ 1 - 2
src/rasterizer.cpp

@@ -101,8 +101,7 @@ void Rasterizer::drawTriangles(Vector3 *vertices, IShader &shader, Buffer<Uint32
 
     //Find triangle area
     float invArea = 1/(float)((xVerts[2]-xVerts[0])*(yVerts[1]-yVerts[0]) 
-                            - (xVerts[1]-xVerts[0])*(yVerts[2]-yVerts[0]));
-                            
+                            - (xVerts[1]-xVerts[0])*(yVerts[2]-yVerts[0]));                 
     float depth = 0;
     Vector3 lambdas;
     Vector3 rgbVals;

+ 1 - 1
src/scene.cpp

@@ -36,7 +36,7 @@ bool Scene::loadSceneModels(std::string &path){
     }
     else{
         modelsInScene.push_back(new Model(fullPath));
-
+        
         //We also initialize the model position here position here
         TransformParameters initParameters;
         //initParameters.scaling = Vector3(1, 60, 60);

+ 25 - 11
src/softwareRenderer.cpp

@@ -59,37 +59,48 @@ void SoftwareRenderer::drawTriangularMesh(Mesh* triMesh){
     std::vector<Vector3> * vertices = &triMesh->vertices;
     std::vector<Vector3> * normals = &triMesh->normals;
     int numFaces = triMesh->numFaces;
-
+    //printf("nFaces: %d nNormals: %lu \n", numFaces, (*normals).size());
+    //(*normals)[numFaces-1].print();
     //Array grouping vertices together into triangle
     Vector3 trianglePrimitive[3];
     Vector3 normalPrim[3];
 
     //Initializing shader
-    GouraudShader shader;
+    FlatShader shader;
 
     //Basic light direction
 
-    Vector3 lightDir{0,0,1};
-
+    Vector3 lightDir{1,0,0};
     //Building ModelViewProjection matrix
     Matrix4 MVP = (mCamera->projectionMatrix)*(mCamera->viewMatrix);
 
     float intensity = 0;
     for (int j = 0; j < numFaces; ++j){
+        //printf("\nCurrent Face: %d\n",j);
         Vector3 f = (*vIndices)[j];
         Vector3 n = (*nIndices)[j];
-
+        //printf("Vertices\n");
+        //f.print();
+        //printf("Normals\n");
+        //n.print();
         //Pack vertices together into an array
         buildTri(f,trianglePrimitive, *vertices);
         buildTri(n,normalPrim, *normals);
 
         //Skip faces that are pointing away from us
-        if (backFaceCulling(trianglePrimitive, intensity)) continue;
+        if (backFaceCulling(trianglePrimitive)) continue;
+
+        Vector3 N1 = trianglePrimitive[1] - trianglePrimitive[0];
+        Vector3 N2 = trianglePrimitive[2] - trianglePrimitive[0];
+        Vector3 N  = (N2.crossProduct(N1)).normalized();
 
         //Apply vertex shader
         for(int i = 0; i < 3; ++i){
-            //Vector3 normal = (mCamera->viewMatrix).matMultVec(normalPrim[i]);
-            trianglePrimitive[i] = shader.vertex(trianglePrimitive[i], MVP, intensity, normalPrim[i], lightDir, i);
+            //trianglePrimitive[i].print();
+            //normalPrim[i].print();
+            //printf("\n");
+            Vector3 normal = (mCamera->viewMatrix).matMultVec(N);
+            trianglePrimitive[i] = shader.vertex(trianglePrimitive[i], MVP, N, lightDir.normalized(), i);
         }
 
         //Clipping should occur here
@@ -106,15 +117,18 @@ Buffer<Uint32>* SoftwareRenderer::getRenderTarget(){
 
 void SoftwareRenderer::buildTri(Vector3 &index, Vector3 *primitive, std::vector<Vector3> &vals){
     for(int i = 0; i < 3; ++i){
-        primitive[i] = vals[index.data[i]];
+        //printf("%d\t",(int)index.data[i]);
+        primitive[i] = vals[(int)index.data[i]];
+        
     }
+    //printf("\n");
 }
 
 void SoftwareRenderer::setCameraToRenderFrom(Camera * camera){
     mCamera = camera;
 }
 
-bool SoftwareRenderer::backFaceCulling(Vector3 *trianglePrim, float &intensity){
+bool SoftwareRenderer::backFaceCulling(Vector3 *trianglePrim){
         //Triangle surface normal 
         //Should probably be calculated on load next time
         Vector3 N1 = trianglePrim[1] - trianglePrim[0];
@@ -125,6 +139,6 @@ bool SoftwareRenderer::backFaceCulling(Vector3 *trianglePrim, float &intensity){
         Vector3 view_dir =  trianglePrim[0] - mCamera->position;
         view_dir = view_dir.normalized();
 
-        intensity =  N.dotProduct(view_dir);
+        float intensity =  N.dotProduct(view_dir);
         return intensity <= 0.0;
 }