comparison gldragon.cpp @ 27:097184bd34a8

Implement number of lights uniform for the shaders, clean up light setup and scene rendering setup.
author Matti Hamalainen <ccr@tnsp.org>
date Fri, 22 Nov 2019 08:39:27 +0200
parents 67647ed860f0
children 6847715b46cd
comparison
equal deleted inserted replaced
26:67647ed860f0 27:097184bd34a8
164 164
165 void dmDrawModel(const DMModel &model) 165 void dmDrawModel(const DMModel &model)
166 { 166 {
167 int maxIndices; 167 int maxIndices;
168 168
169 if (optUseShaders)
170 {
171 // Enable shader program
172 glUseProgram(model.id_prog);
173 }
174 else
175 {
176 // Set the color of the model
177 glEnable(GL_LIGHTING);
178 glColor3ub(0x90, 0x80, 0x90);
179 }
180
181 // Render the model 169 // Render the model
182 glGetIntegerv(GL_MAX_ELEMENTS_INDICES, &maxIndices); 170 glGetIntegerv(GL_MAX_ELEMENTS_INDICES, &maxIndices);
183
184 glPushMatrix();
185 171
186 // Add transforms 172 // Add transforms
187 glScalef(model.scale.x, model.scale.y, model.scale.z); 173 glScalef(model.scale.x, model.scale.y, model.scale.z);
188 glTranslatef(model.translate.x, model.translate.y, model.translate.z); 174 glTranslatef(model.translate.x, model.translate.y, model.translate.z);
189 glRotatef(model.rotate.x, 1.0f, 0.0f, 0.0f); 175 glRotatef(model.rotate.x, 1.0f, 0.0f, 0.0f);
196 for (int n = 0; n < model.nfaces; n += maxIndices) 182 for (int n = 0; n < model.nfaces; n += maxIndices)
197 { 183 {
198 const int count = std::min(maxIndices, model.nfaces - n); 184 const int count = std::min(maxIndices, model.nfaces - n);
199 glDrawElements(GL_TRIANGLES, count * 3, GL_UNSIGNED_INT, &model.faces[n * 3]); 185 glDrawElements(GL_TRIANGLES, count * 3, GL_UNSIGNED_INT, &model.faces[n * 3]);
200 } 186 }
201
202 glPopMatrix();
203
204 // Restore
205 if (optUseShaders)
206 {
207 glUseProgram(0);
208 }
209 } 187 }
210 188
211 189
212 void dmDrawScene(const DMSimpleScene &scene) 190 void dmDrawScene(const DMSimpleScene &scene)
213 { 191 {
248 226
249 glEnable(GL_DEPTH_TEST); 227 glEnable(GL_DEPTH_TEST);
250 228
251 // Draw models 229 // Draw models
252 for (const DMModel &model : scene.models) 230 for (const DMModel &model : scene.models)
231 {
232 if (optUseShaders)
233 {
234 // Enable shader program
235 glUseProgram(model.id_prog);
236 glUniform1i(glGetUniformLocation(model.id_prog, "nlights"), scene.lights.size());
237 }
238 else
239 {
240 // Set the color of the model
241 glEnable(GL_LIGHTING);
242 glColor3ub(0x90, 0x80, 0x90);
243 }
244
245 glPushMatrix();
253 dmDrawModel(model); 246 dmDrawModel(model);
247 glPopMatrix();
248
249 // Restore
250 if (optUseShaders)
251 {
252 glUseProgram(0);
253 }
254 }
254 } 255 }
255 256
256 257
257 bool dmCompileShader(const GLenum stype, const std::string &src, GLuint &shader) 258 bool dmCompileShader(const GLenum stype, const std::string &src, GLuint &shader)
258 { 259 {
441 !dmReadText(vertFile, model.vertShaderStr, SET_MAX_SHADER_SIZE)) 442 !dmReadText(vertFile, model.vertShaderStr, SET_MAX_SHADER_SIZE))
442 goto exit; 443 goto exit;
443 } 444 }
444 } 445 }
445 446
447 // Define a default light if none defined in scene file
448 if (scene.lights.size() == 0)
449 {
450 DMLight light; // Default light
451 scene.lights.push_back(light);
452 }
453
446 // Initialize SDL + OpenGL 454 // Initialize SDL + OpenGL
447 if (!dmInitSDLGL(optWidth, optHeight, "GLDragon")) 455 if (!dmInitSDLGL(optWidth, optHeight, "GLDragon"))
448 goto exit; 456 goto exit;
449 457
450 // According to our mode .. 458 // According to our mode ..
458 466
459 dmLinkModelShaders(model); 467 dmLinkModelShaders(model);
460 } 468 }
461 } 469 }
462 470
463 { 471 // ...
464 float specReflection[] = { 0.8f, 0.8f, 0.8f, 1.0f }; 472 {
465 glEnable(GL_COLOR_MATERIAL); 473 float specReflection[] = { 0.8f, 0.8f, 0.8f, 1.0f };
466 glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE); 474 glEnable(GL_COLOR_MATERIAL);
467 glMateriali(GL_FRONT, GL_SHININESS, 96); 475 glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
468 glMaterialfv(GL_FRONT, GL_SPECULAR, specReflection); 476 glMateriali(GL_FRONT, GL_SHININESS, 96);
469 } 477 glMaterialfv(GL_FRONT, GL_SPECULAR, specReflection);
470 478 }
471 // Define lights 479
472 if (scene.lights.size() == 0) 480 // Setup lights
473 { 481 for (size_t n = 0; n < scene.lights.size(); n++)
474 DMLight light; // Default light 482 dmSetupLight(n, scene.lights[n]);
475 dmSetupLight(0, light);
476 }
477 else
478 {
479 for (size_t n = 0; n < scene.lights.size(); n++)
480 dmSetupLight(n, scene.lights[n]);
481 }
482 483
483 // Define the camera 484 // Define the camera
484 gluLookAt(0, 0.12, 0.24, 0, 0.12, 0, 0, 1, 0); 485 gluLookAt(0, 0.12, 0.24, 0, 0.12, 0, 0, 1, 0);
485 486
486 487