changeset 32:1215fdd0a8ab

Add support for specifying specular and shininess values per model.
author Matti Hamalainen <ccr@tnsp.org>
date Fri, 22 Nov 2019 22:57:14 +0200
parents 6847715b46cd
children 2e85c180afdf
files dmmodel.cpp dmmodel.h dragon.scene gldragon.cpp
diffstat 4 files changed, 53 insertions(+), 53 deletions(-) [+]
line wrap: on
line diff
--- a/dmmodel.cpp	Fri Nov 22 19:53:55 2019 +0200
+++ b/dmmodel.cpp	Fri Nov 22 22:57:14 2019 +0200
@@ -618,34 +618,6 @@
 }
 
 
-bool dmParseColor(DMTextFileInfo &info, const std::vector<std::string> tokens, const size_t offs, DMColor &color)
-{
-    color.alpha = 0xff;
-
-    if (tokens.size() == offs + 1)
-    {
-        color.r = color.g = color.b = std::stoi(tokens[offs], 0, 0);
-    }
-    else
-    if (tokens.size() == offs + 3 || tokens.size() == offs + 4)
-    {
-        color.r = std::stoi(tokens[offs], 0, 0);
-        color.g = std::stoi(tokens[offs + 1], 0, 0);
-        color.b = std::stoi(tokens[offs + 2], 0, 0);
-
-        if (tokens.size() == offs + 4)
-            color.alpha = std::stoi(tokens[offs + 3], 0, 0);
-    }
-    else
-    {
-        return dmSyntaxError(info,
-            "Expected color values <value> or <red> <green> <blue> [<alpha>] for '"+ *info.key +"'");
-    }
-
-    return true;
-}
-
-
 bool DMSimpleScene::loadInfo(const std::string &filename)
 {
     DMTextFileInfo info;
@@ -702,12 +674,6 @@
             info.state = 1;
         }
         else
-        if (info.state == 1 && key == "color")
-        {
-            if (!dmParseColor(info, tokens, 1, model->color))
-                return false;
-        }
-        else
         if (info.state == 1 && (key == "translate" || key == "rotate" || key == "scale"))
         {
             DMVector3 vec;
@@ -728,15 +694,38 @@
                 model->scale = vec;
         }
         else
+        if (info.state == 1 && key == "specular")
+        {
+            DMVector4 val;
+            val.p.w = 1.0f;
+
+            if (!dmParseVector(info, tokens, 1, val))
+                return false;
+
+            model->specular = val;
+        }
+        else
+        if (info.state == 1 && key == "shininess")
+        {
+            if (tokens.size() != 2)
+            {
+                return dmError(info,
+                    "Expected argument for shininess");
+            }
+
+            model->shininess = std::stoi(tokens[1], 0, 0);
+        }
+        else
         if (key == "light")
         {
             DMLight newlight;
 
             if (lights.size() >= 4)
             {
-                printf("ERROR: Too many lights defined.\n");
-                return false;
+                return dmError(info,
+                "Too many lights defined (max 4)");
             }
+
             lights.push_back(newlight);
             light = &lights.back();
             ppos = &light->position;
--- a/dmmodel.h	Fri Nov 22 19:53:55 2019 +0200
+++ b/dmmodel.h	Fri Nov 22 22:57:14 2019 +0200
@@ -121,12 +121,6 @@
 };
 
 
-struct DMColor
-{
-    int r, g, b, alpha;
-};
-
-
 struct DMVector3
 {
     float x, y, z;
@@ -146,7 +140,8 @@
     std::vector<DMVector3> vertices, normals;
     std::vector<unsigned int> faces;
 
-    DMColor color;
+    DMVector4 ambient, diffuse, specular;
+    int shininess;
     DMVector3 translate, scale, rotate;
 
     unsigned int id_prog, id_fs, id_vs;
@@ -178,6 +173,9 @@
         translate.x = translate.y = translate.z = 0;
         rotate.x = rotate.y = rotate.z = 0;
         scale.x = scale.y = scale.z = 1;
+
+        specular.p.x = specular.p.y = specular.p.z = 0.8f; specular.p.w = 1.0f;
+        shininess = 96;
     }
 };
 
--- a/dragon.scene	Fri Nov 22 19:53:55 2019 +0200
+++ b/dragon.scene	Fri Nov 22 22:57:14 2019 +0200
@@ -1,8 +1,25 @@
+# Dragon model with 2 lights
+# all settings have default values which will be used
+# if they are not overridden / specified for the element
 model dragon.ply
+	specular 0.8 0.5 0.2
+	shininess 96
+	#scale 1 1 0.1
+
+#model models/cube.ply
+#	scale 0.015
+#	translate -3 7 0
+#	rotate 0 0 0
+#	specular 0 1 0
+#	shininess 3
+
 
 light
 	position 10 10 0.0
+	#ambient 0
+	#diffuse 0 0.3 0.3
 
 light
+	position -50 50 0
 	diffuse 1.0 0 0
-	position -50 50 0
+	ambient 0.1 0.1 0
--- a/gldragon.cpp	Fri Nov 22 19:53:55 2019 +0200
+++ b/gldragon.cpp	Fri Nov 22 22:57:14 2019 +0200
@@ -166,6 +166,11 @@
 {
     int maxIndices;
 
+    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);
+
     // Render the model
     glGetIntegerv(GL_MAX_ELEMENTS_INDICES, &maxIndices);
 
@@ -468,15 +473,6 @@
         }
     }
 
-    // ...
-    {
-    float specReflection[] = { 0.8f, 0.8f, 0.8f, 1.0f };
-    glEnable(GL_COLOR_MATERIAL);
-    glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
-    glMateriali(GL_FRONT, GL_SHININESS, 96);
-    glMaterialfv(GL_FRONT, GL_SPECULAR, specReflection);
-    }
-
     // Setup lights
     for (size_t n = 0; n < scene.lights.size(); n++)
         dmSetupLight(n, scene.lights[n]);