Browse Source

Stupid Gouraudshader

angel 7 years ago
parent
commit
e10217ec67
7 changed files with 37 additions and 19 deletions
  1. 13 8
      include/shader.h
  2. 3 3
      src/camera.cpp
  3. 3 1
      src/engine.cpp
  4. 2 1
      src/rasterizer.cpp
  5. 1 1
      src/scene.cpp
  6. 1 1
      src/sceneManager.cpp
  7. 14 4
      src/softwareRenderer.cpp

+ 13 - 8
include/shader.h

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

+ 3 - 3
src/camera.cpp

@@ -10,11 +10,11 @@ Camera::Camera(){
 
 
 void Camera::update(){
 void Camera::update(){
     float t = static_cast<float>(SDL_GetTicks());
     float t = static_cast<float>(SDL_GetTicks());
-    float radius = 12;
+    float radius = 5;
     float camX   = std::sin(t/4000) * radius;
     float camX   = std::sin(t/4000) * radius;
     float camZ   = std::cos(t/4000) * radius;
     float camZ   = std::cos(t/4000) * radius;
-    position.x = camX;
+    position.x = 0;
     position.y = 0;
     position.y = 0;
-    position.z = camZ;
+    position.z = radius;
     viewMatrix = Matrix4::lookAt(position,target,up);
     viewMatrix = Matrix4::lookAt(position,target,up);
 }
 }

+ 3 - 1
src/engine.cpp

@@ -77,7 +77,9 @@ void Engine::run(){
                 printf("Failed to switch scene! Quitting.\n");
                 printf("Failed to switch scene! Quitting.\n");
                 break;
                 break;
             }
             }
-            else switchScene = false;
+            else{
+                switchScene = false;
+            } 
         }
         }
 
 
         //Handle all user input
         //Handle all user input

+ 2 - 1
src/rasterizer.cpp

@@ -121,7 +121,8 @@ void Rasterizer::drawTriangles(Vector3 *vertices, IShader &shader, Buffer<Uint32
                 if(depth <= 1.0){
                 if(depth <= 1.0){
                     (*zBuffer)(x,y) = depth;
                     (*zBuffer)(x,y) = depth;
                     (*pixelBuffer)(x,y) = SDL_MapRGBA(mappingFormat,
                     (*pixelBuffer)(x,y) = SDL_MapRGBA(mappingFormat,
-                rgbVals.data[0],rgbVals.data[1],rgbVals.data[2],0xFF);;
+                rgbVals.data[0],rgbVals.data[1],rgbVals.data[2],0xFF);
+                    //rgbVals.print();
                 }   
                 }   
             } 
             } 
         }
         }

+ 1 - 1
src/scene.cpp

@@ -41,7 +41,7 @@ bool Scene::loadSceneModels(std::string &path){
         TransformParameters initParameters;
         TransformParameters initParameters;
         //initParameters.scaling = Vector3(1, 60, 60);
         //initParameters.scaling = Vector3(1, 60, 60);
         //initParameters.rotation = Vector3(0,0,0);
         //initParameters.rotation = Vector3(0,0,0);
-        initParameters.translation = Vector3(0, -3, 0);
+        initParameters.translation = Vector3(0, -1.5, 0);
         modelsInScene[0]->initPosition(initParameters);
         modelsInScene[0]->initPosition(initParameters);
 
 
         //sceneModel->describeMesh();
         //sceneModel->describeMesh();

+ 1 - 1
src/sceneManager.cpp

@@ -27,7 +27,7 @@ bool SceneManager::switchScene(){
 
 
 //for now just loads the teapot.obj
 //for now just loads the teapot.obj
 bool SceneManager::loadScene(){
 bool SceneManager::loadScene(){
-    currentScene = new Scene("teapot.obj");
+    currentScene = new Scene("elephant.obj");
     return  !currentScene->checkIfEmpty();
     return  !currentScene->checkIfEmpty();
 }
 }
 
 

+ 14 - 4
src/softwareRenderer.cpp

@@ -58,28 +58,38 @@ void SoftwareRenderer::drawTriangularMesh(Mesh* triMesh){
     std::vector<Vector3> * nIndices = &triMesh->normalsIndices;
     std::vector<Vector3> * nIndices = &triMesh->normalsIndices;
     std::vector<Vector3> * vertices = &triMesh->vertices;
     std::vector<Vector3> * vertices = &triMesh->vertices;
     std::vector<Vector3> * normals = &triMesh->normals;
     std::vector<Vector3> * normals = &triMesh->normals;
+    int numFaces = triMesh->numFaces;
 
 
     //Array grouping vertices together into triangle
     //Array grouping vertices together into triangle
     Vector3 trianglePrimitive[3];
     Vector3 trianglePrimitive[3];
+    Vector3 normalPrim[3];
 
 
     //Initializing shader
     //Initializing shader
     GouraudShader shader;
     GouraudShader shader;
 
 
+    //Basic light direction
+
+    Vector3 lightDir{0,0,1};
+
     //Building ModelViewProjection matrix
     //Building ModelViewProjection matrix
     Matrix4 MVP = (mCamera->projectionMatrix)*(mCamera->viewMatrix);
     Matrix4 MVP = (mCamera->projectionMatrix)*(mCamera->viewMatrix);
 
 
     float intensity = 0;
     float intensity = 0;
-    for (Vector3 &f : *vIndices){
+    for (int j = 0; j < numFaces; ++j){
+        Vector3 f = (*vIndices)[j];
+        Vector3 n = (*nIndices)[j];
 
 
         //Pack vertices together into an array
         //Pack vertices together into an array
         buildTri(f,trianglePrimitive, *vertices);
         buildTri(f,trianglePrimitive, *vertices);
+        buildTri(n,normalPrim, *normals);
 
 
         //Skip faces that are pointing away from us
         //Skip faces that are pointing away from us
         if (backFaceCulling(trianglePrimitive, intensity)) continue;
         if (backFaceCulling(trianglePrimitive, intensity)) continue;
 
 
         //Apply vertex shader
         //Apply vertex shader
         for(int i = 0; i < 3; ++i){
         for(int i = 0; i < 3; ++i){
-            trianglePrimitive[i] = shader.vertex(trianglePrimitive[i], MVP, intensity);
+            //Vector3 normal = (mCamera->viewMatrix).matMultVec(normalPrim[i]);
+            trianglePrimitive[i] = shader.vertex(trianglePrimitive[i], MVP, intensity, normalPrim[i], lightDir, i);
         }
         }
 
 
         //Clipping should occur here
         //Clipping should occur here
@@ -94,9 +104,9 @@ Buffer<Uint32>* SoftwareRenderer::getRenderTarget(){
     return pixelBuffer;
     return pixelBuffer;
 }
 }
 
 
-void SoftwareRenderer::buildTri(Vector3 &f, Vector3 *trianglePrim, std::vector<Vector3> &verts){
+void SoftwareRenderer::buildTri(Vector3 &index, Vector3 *primitive, std::vector<Vector3> &vals){
     for(int i = 0; i < 3; ++i){
     for(int i = 0; i < 3; ++i){
-        trianglePrim[i] = verts[f.data[i]];
+        primitive[i] = vals[index.data[i]];
     }
     }
 }
 }