Browse Source

Fixed Phong shader

angel 7 years ago
parent
commit
d972bdd62d
6 changed files with 10 additions and 14 deletions
  1. 5 4
      include/shader.h
  2. 1 1
      src/camera.cpp
  3. 0 5
      src/rasterizer.cpp
  4. 2 2
      src/scene.cpp
  5. 1 1
      src/sceneManager.cpp
  6. 1 1
      src/softwareRenderer.cpp

+ 5 - 4
include/shader.h

@@ -67,9 +67,10 @@ struct GouraudShader : public IShader {
     }
 
 };
+
 //Even more complex shader that interpolates normals and calculates intensities per fragment instead
-//of per normals. Uses half angle optimization.BlinnPhongShader
-struct BlinnPhongShader : public IShader {
+//of per normals. 
+struct PhongShader : public IShader {
     Matrix4 MVP, MV, V, N;
     float ambientStrength = 0.05, diffStrength = 0, specularStrength = 0.9, spec = 0;
     Vector3f normals[3], viewDir[3];
@@ -93,12 +94,12 @@ struct BlinnPhongShader : public IShader {
         ambient = lightColor * ambientStrength;
 
         //Diffuse
-        diffStrength = std::max(0.0f, (interpNormal).dotProduct(light2));
+        diffStrength = std::max(0.0f, (interpNormal.normalized()).dotProduct(light2));
         diffuse = lightColor * diffStrength;
         
         //Specular
         reflectDir = Vector3f::reflect(-light2, interpNormal);
-        spec = std::pow( std::max( (-interpViewDir).dotProduct(reflectDir), 0.0f), 50.0f);
+        spec = std::pow( std::max( (-interpViewDir.normalized()).dotProduct(reflectDir), 0.0f), 50.0f);
         specular = lightColorSpec * (specularStrength * spec);
 
         color = (ambient + diffuse + specular) * rgb;

+ 1 - 1
src/camera.cpp

@@ -11,7 +11,7 @@ Camera::Camera(){
 
 void Camera::update(){
     float t = static_cast<float>(SDL_GetTicks());
-    float radius = 5;
+    float radius = 12;
     float camX   = std::sin(t/4000) * radius;
     float camZ   = std::cos(t/4000) * radius;
     position.x   = camX;

+ 0 - 5
src/rasterizer.cpp

@@ -95,13 +95,8 @@ void Rasterizer::drawTriangles(Vector3f *vertices, IShader &shader, Buffer<Uint3
     int xMax, xMin, yMax, yMin;
     Rasterizer::triBoundBox(xMax, xMin, yMax, yMin, vertices, pixelBuffer);
 
-    
-
     //Per triangle variables
     Vector3f zVals{vertices[0].z,vertices[1].z,vertices[2].z};
-
-    
-
     float A01 = vertices[0].y - vertices[1].y, B01= vertices[1].x - vertices[0].x;
     float A12 = vertices[1].y - vertices[2].y, B12= vertices[2].x - vertices[1].x;
     float A20 = vertices[2].y - vertices[0].y, B20= vertices[0].x - vertices[2].x;

+ 2 - 2
src/scene.cpp

@@ -34,8 +34,8 @@ bool Scene::loadSceneModels(std::string &path){
     }
     else{
         TransformParameters initParameters;
-        initParameters.rotation = Vector3f(90*M_PI/180.0f, 0 , 0);
-        initParameters.translation = Vector3f(0, 0, 0);
+        //initParameters.rotation = Vector3f(90*M_PI/180.0f, 0 , 0);
+        initParameters.translation = Vector3f(0, -5, 0);
         modelsInScene.push_back(new Model(fullPath, initParameters));
         return false;
     }

+ 1 - 1
src/sceneManager.cpp

@@ -23,7 +23,7 @@ bool SceneManager::switchScene(){
 
 //for now just loads a single .obj based on the  given string
 bool SceneManager::loadScene(){
-    currentScene = new Scene("teapot.obj");
+    currentScene = new Scene("dragon.obj");
     return  !currentScene->checkIfEmpty(); //True if empty, so it's negated for startup
 }
 

+ 1 - 1
src/softwareRenderer.cpp

@@ -35,7 +35,7 @@ void SoftwareRenderer::drawTriangularMesh(Model * currentModel){
     Vector3f normalPrim[3];
 
     //Initializing shader 
-    BlinnPhongShader shader;
+    PhongShader shader;
 
     //Basic light direction
     Vector3f lightDir{1, 0, 0};