changeset 37:73a785323e8a

Rendering cleanups.
author Matti Hamalainen <ccr@tnsp.org>
date Sun, 01 Dec 2019 23:52:19 +0200
parents d640f2a34031
children 372fa249ce26
files dmmodel.h gldragon.cpp
diffstat 2 files changed, 19 insertions(+), 22 deletions(-) [+]
line wrap: on
line diff
--- a/dmmodel.h	Sun Dec 01 23:12:04 2019 +0200
+++ b/dmmodel.h	Sun Dec 01 23:52:19 2019 +0200
@@ -176,6 +176,7 @@
         scale.x = scale.y = scale.z = 0;
         translateSet = rotateSet = scaleSet = false;
 
+        diffuse.p.x = diffuse.p.z = 0.56471f; diffuse.p.y = 0.5f; diffuse.p.w = 1.0f;
         specular.p.x = specular.p.y = specular.p.z = 0.8f; specular.p.w = 1.0f;
         shininess = 96;
     }
--- a/gldragon.cpp	Sun Dec 01 23:12:04 2019 +0200
+++ b/gldragon.cpp	Sun Dec 01 23:52:19 2019 +0200
@@ -162,14 +162,23 @@
 }
 
 
-void dmDrawModel(const DMModel &model)
+void dmDrawModel(const DMSimpleScene &scene, const DMModel &model)
 {
     int maxIndices;
 
+    if (optUseShaders)
+    {
+        // Enable shader program
+        glUseProgram(model.id_prog);
+        glUniform1i(glGetUniformLocation(model.id_prog, "nlights"), scene.lights.size());
+    }
+
+    // Set the material of the model
     glEnable(GL_COLOR_MATERIAL);
     glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
     glMateriali(GL_FRONT, GL_SHININESS, model.shininess);
     glMaterialfv(GL_FRONT, GL_SPECULAR, model.specular.values);
+    glColor4fv(model.diffuse.values);
 
     // Render the model
     glGetIntegerv(GL_MAX_ELEMENTS_INDICES, &maxIndices);
@@ -196,6 +205,12 @@
         const int count = std::min(maxIndices, model.nfaces - n);
         glDrawElements(GL_TRIANGLES, count * 3, GL_UNSIGNED_INT, &model.faces[n * 3]);
     }
+
+    // Restore
+    if (optUseShaders)
+    {
+        glUseProgram(0);
+    }
 }
 
 
@@ -228,7 +243,6 @@
     }
     glEnd();
 
-
     // Restore the 3D projection
     glMatrixMode(GL_PROJECTION);
     glPopMatrix();
@@ -237,32 +251,14 @@
     glPopMatrix();
 
     glEnable(GL_DEPTH_TEST);
+    glEnable(GL_LIGHTING);
 
     // Draw models
     for (const DMModel &model : scene.models)
     {
-        if (optUseShaders)
-        {
-            // Enable shader program
-            glUseProgram(model.id_prog);
-            glUniform1i(glGetUniformLocation(model.id_prog, "nlights"), scene.lights.size());
-        }
-        else
-        {
-            // Set the color of the model
-            glEnable(GL_LIGHTING);
-            glColor3ub(0x90, 0x80, 0x90);
-        }
-
         glPushMatrix();
-        dmDrawModel(model);
+        dmDrawModel(scene, model);
         glPopMatrix();
-
-        // Restore
-        if (optUseShaders)
-        {
-            glUseProgram(0);
-        }
     }
 }