comparison gldragon.cpp @ 25:2403030a0352

"Finish" implementing multiple lights support in scene files. If scene defines no lights, a default light will be used. Light objects will have default values and can redefine only color, position etc.
author Matti Hamalainen <ccr@tnsp.org>
date Fri, 22 Nov 2019 06:47:13 +0200
parents c1897cfc8463
children 67647ed860f0
comparison
equal deleted inserted replaced
24:c1897cfc8463 25:2403030a0352
183 183
184 glPushMatrix(); 184 glPushMatrix();
185 185
186 // Add transforms 186 // Add transforms
187 glScalef(model.scale.x, model.scale.y, model.scale.z); 187 glScalef(model.scale.x, model.scale.y, model.scale.z);
188 glTranslatef(model.translate.x, model.translate.y, model.translate. z); 188 glTranslatef(model.translate.x, model.translate.y, model.translate.z);
189 glRotatef(model.rotate.x, 1.0f, 0.0f, 0.0f); 189 glRotatef(model.rotate.x, 1.0f, 0.0f, 0.0f);
190 glRotatef(model.rotate.y, 0.0f, 1.0f, 0.0f); 190 glRotatef(model.rotate.y, 0.0f, 1.0f, 0.0f);
191 glRotatef(model.rotate.z, 0.0f, 0.0f, 1.0f); 191 glRotatef(model.rotate.z, 0.0f, 0.0f, 1.0f);
192 192
193 glVertexPointer(3, GL_FLOAT, 0, &model.vertices[0]); 193 glVertexPointer(3, GL_FLOAT, 0, &model.vertices[0]);
281 { 281 {
282 model.id_prog = glCreateProgram(); 282 model.id_prog = glCreateProgram();
283 glAttachShader(model.id_prog, model.id_fs); 283 glAttachShader(model.id_prog, model.id_fs);
284 glAttachShader(model.id_prog, model.id_vs); 284 glAttachShader(model.id_prog, model.id_vs);
285 glLinkProgram(model.id_prog); 285 glLinkProgram(model.id_prog);
286 }
287
288
289 void dmSetupLight(const int n, const DMLight &light)
290 {
291 glEnable(GL_LIGHT0 + n);
292 glLightfv(GL_LIGHT0 + n, GL_AMBIENT, light.ambient.values);
293 glLightfv(GL_LIGHT0 + n, GL_DIFFUSE, light.diffuse.values);
294 glLightfv(GL_LIGHT0 + n, GL_SPECULAR, light.specular.values);
295 glLightfv(GL_LIGHT0 + n, GL_POSITION, light.position.values);
286 } 296 }
287 297
288 298
289 int main(int argc, char *argv[]) 299 int main(int argc, char *argv[])
290 { 300 {
436 goto exit; 446 goto exit;
437 447
438 dmLinkModelShaders(model); 448 dmLinkModelShaders(model);
439 } 449 }
440 } 450 }
441 else 451
442 { 452 {
443 float specReflection[] = { 0.8f, 0.8f, 0.8f, 1.0f }; 453 float specReflection[] = { 0.8f, 0.8f, 0.8f, 1.0f };
444
445 glEnable(GL_COLOR_MATERIAL); 454 glEnable(GL_COLOR_MATERIAL);
446
447 glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE); 455 glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
448 glMateriali(GL_FRONT, GL_SHININESS, 96); 456 glMateriali(GL_FRONT, GL_SHININESS, 96);
449 glMaterialfv(GL_FRONT, GL_SPECULAR, specReflection); 457 glMaterialfv(GL_FRONT, GL_SPECULAR, specReflection);
450 458 }
451 glEnable(GL_LIGHT0); 459
452 460 // Define lights
453 // Define the light components and position 461 if (scene.lights.size() == 0)
454 GLfloat ambient[] = { 0.2f, 0.2f, 0.2f, 1.0f }; 462 {
455 GLfloat diffuse[] = { 0.8f, 0.8f, 0.8f, 1.0f }; 463 DMLight light; // Default light
456 GLfloat specular[] = { 0.5f, 0.5f, 0.5f, 1.0f }; 464 dmSetupLight(0, light);
457 GLfloat position[] = { 10.0f, 10.0f, 0.0f, 0.0f }; 465 }
458 466 else
459 // Define the light components and position 467 {
460 glLightfv(GL_LIGHT0, GL_AMBIENT, ambient); 468 for (size_t n = 0; n < scene.lights.size(); n++)
461 glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse); 469 dmSetupLight(n, scene.lights[n]);
462 glLightfv(GL_LIGHT0, GL_SPECULAR, specular);
463 glLightfv(GL_LIGHT0, GL_POSITION, position);
464 } 470 }
465 471
466 // Define the camera 472 // Define the camera
467 gluLookAt(0, 0.12, 0.24, 0, 0.12, 0, 0, 1, 0); 473 gluLookAt(0, 0.12, 0.24, 0, 0.12, 0, 0, 1, 0);
468 474