# HG changeset patch # User Matti Hamalainen # Date 1574404767 -7200 # Node ID 097184bd34a82225b4405dead910d455efeb509a # Parent 67647ed860f0e4ee422229956d85e26c376c4607 Implement number of lights uniform for the shaders, clean up light setup and scene rendering setup. diff -r 67647ed860f0 -r 097184bd34a8 gldragon.cpp --- a/gldragon.cpp Fri Nov 22 07:37:41 2019 +0200 +++ b/gldragon.cpp Fri Nov 22 08:39:27 2019 +0200 @@ -166,23 +166,9 @@ { int maxIndices; - if (optUseShaders) - { - // Enable shader program - glUseProgram(model.id_prog); - } - else - { - // Set the color of the model - glEnable(GL_LIGHTING); - glColor3ub(0x90, 0x80, 0x90); - } - // Render the model glGetIntegerv(GL_MAX_ELEMENTS_INDICES, &maxIndices); - glPushMatrix(); - // Add transforms glScalef(model.scale.x, model.scale.y, model.scale.z); glTranslatef(model.translate.x, model.translate.y, model.translate.z); @@ -198,14 +184,6 @@ const int count = std::min(maxIndices, model.nfaces - n); glDrawElements(GL_TRIANGLES, count * 3, GL_UNSIGNED_INT, &model.faces[n * 3]); } - - glPopMatrix(); - - // Restore - if (optUseShaders) - { - glUseProgram(0); - } } @@ -250,7 +228,30 @@ // 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); + glPopMatrix(); + + // Restore + if (optUseShaders) + { + glUseProgram(0); + } + } } @@ -443,6 +444,13 @@ } } + // Define a default light if none defined in scene file + if (scene.lights.size() == 0) + { + DMLight light; // Default light + scene.lights.push_back(light); + } + // Initialize SDL + OpenGL if (!dmInitSDLGL(optWidth, optHeight, "GLDragon")) goto exit; @@ -460,25 +468,18 @@ } } + // ... { - 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); + 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); } - // Define lights - if (scene.lights.size() == 0) - { - DMLight light; // Default light - dmSetupLight(0, light); - } - else - { - for (size_t n = 0; n < scene.lights.size(); n++) - dmSetupLight(n, scene.lights[n]); - } + // Setup lights + for (size_t n = 0; n < scene.lights.size(); n++) + dmSetupLight(n, scene.lights[n]); // Define the camera gluLookAt(0, 0.12, 0.24, 0, 0.12, 0, 0, 1, 0);