@@ -62,8 +62,8 @@ std::string pwd = std::filesystem::current_path().string();
62
62
// Shader sources
63
63
std::string lightVShaderPath = pwd + " /../shaders/light.vs" ;
64
64
std::string lightFShaderPath = pwd + " /../shaders/light.fs" ;
65
- std::string modelVShaderPath = pwd + " /../shaders/mesh.vs" ;
66
- std::string modelFShaderPath = pwd + " /../shaders/mesh.fs" ;
65
+ std::string meshVShaderPath = pwd + " /../shaders/mesh.vs" ;
66
+ std::string meshFShaderPath = pwd + " /../shaders/mesh.fs" ;
67
67
std::string modelPath = pwd + " /../../assets/backpack/backpack.obj" ;
68
68
std::string cubePath = pwd + " /../../assets/cube/cube.obj" ;
69
69
@@ -110,7 +110,8 @@ float constant = CONSTANT;
110
110
float linear = LINEAR;
111
111
float quadratic = QUADRATIC;
112
112
113
- float alpha = 1 .0f ;
113
+ float lightIntensity = 0 .5f ;
114
+ float lightAlpha = 1 .0f ;
114
115
115
116
glm::vec4 backgroundColor = DEFAULT_BACKGROUND_COLOR;
116
117
glm::vec3 lightColor = DEFAULT_LIGHT_COLOR;
@@ -150,55 +151,54 @@ int main()
150
151
glfwSetScrollCallback (window.getGlWindow (), scroll_callback);
151
152
glfwSetKeyCallback (window.getGlWindow (), key_callback);
152
153
153
- // tell stb_image.h to flip loaded texture's on the y-axis (before loading model).
154
- // stbi_set_flip_vertically_on_load(true);
155
-
156
- // configure global opengl state
154
+ // Configure OpenGL to use depth buffer
157
155
// -----------------------------
158
156
glEnable (GL_DEPTH_TEST);
159
157
160
- // build and compile shaders
161
- // -------------------------
162
- // tell stb_image.h to flip loaded texture's on the y-axis (before loading model).
158
+ // Tell stb_image.h to flip loaded texture's on the y-axis (before loading model).
163
159
stbi_set_flip_vertically_on_load (true );
164
160
165
- // build and compile shaders
161
+ // Build and compile shaders
166
162
// -------------------------
167
- Shader meshShader (modelVShaderPath .c_str (), modelFShaderPath .c_str ());
163
+ Shader meshShader (meshVShaderPath .c_str (), meshFShaderPath .c_str ());
168
164
Shader lightShader (lightVShaderPath.c_str (), lightFShaderPath.c_str ());
169
165
170
166
Shader::enableDepth ();
171
167
172
- // load models
168
+ // Load models
173
169
// -----------
174
170
Model backpack (modelPath.c_str ());
175
171
Model cube (cubePath.c_str ());
176
172
177
- // draw in wireframe
178
- // glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
173
+ // Uncomment to render models in wireframe
174
+ // glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
179
175
180
- // render loop
176
+ // Render loop
181
177
// -----------
182
178
while (!window.isShouldClose ())
183
179
{
184
- // input
185
- // -----
180
+ // Process input
181
+ // -------------
186
182
processMovement (window.getGlWindow (), &camera);
187
183
188
- // render
189
- // ------
184
+ // Set background color and clear color buffer and depth buffer
185
+ // ------------------------------------------------------------
190
186
glClearColor (backgroundColor.x , backgroundColor.y , backgroundColor.z , backgroundColor.t );
191
187
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
192
188
193
- // don't forget to enable shader before setting uniforms
194
- // meshShader.use();
189
+ // Enabling our shader before using it's uniforms
190
+ // ----------------------------------------------
195
191
lightShader.use ();
196
192
197
193
lightShader.setFloat (" material.shininess" , specularIntensity);
198
194
195
+ // Directional light creation
196
+ // --------------------------
199
197
DirectionalLight directionalLight (lightDirection, directionalAmbient, directionalDiffuse, directionalSpecular);
200
198
directionalLight.load (lightShader.getID ());
201
199
200
+ // Point lights creation
201
+ // ---------------------
202
202
std::vector<PointLight> pointLights;
203
203
for (unsigned int i = 0 ; i < 5 ; i++)
204
204
{
@@ -207,21 +207,28 @@ int main()
207
207
pointLights[i].load (lightShader.getID ());
208
208
}
209
209
210
- // view/projection transformations
210
+ // Transformations for view and projection
211
+ // ---------------------------------------
211
212
glm::mat4 projection = glm::perspective (glm::radians (camera.getFov ()), width / height, 0 .1f , 100 .0f );
212
213
glm::mat4 view = camera.calculateLookAtMatrix (camera.getPosition (), camera.getPosition () + camera.getFront (), camera.getUp ());
213
214
lightShader.setMatrix4fv (" projection" , 1 , GL_FALSE, projection);
214
215
lightShader.setMatrix4fv (" view" , 1 , GL_FALSE, view);
215
216
216
- // render the loaded model
217
+ // Transformations for model
218
+ // -------------------------
217
219
glm::mat4 model = glm::mat4 (1 .0f );
218
220
model = glm::translate (model, glm::vec3 (0 .0f , 0 .0f , 0 .0f )); // translate it down so it's at the center of the scene
219
221
model = glm::scale (model, glm::vec3 (1 .0f , 1 .0f , 1 .0f )); // it's a bit too big for our scene, so scale it down
220
222
lightShader.setMatrix3fv (" normalMatrix" , 1 , GL_TRUE, glm::mat3 (glm::inverse (model)));
221
223
lightShader.setMatrix4fv (" model" , 1 , GL_FALSE, model);
224
+
225
+ // Render model
226
+ // ------------
222
227
backpack.draw (lightShader);
223
228
lightShader.unbind ();
224
229
230
+ // Light cubes creation
231
+ // --------------------
225
232
meshShader.use ();
226
233
meshShader.setVec3 (" color" , glm::value_ptr (lightColor));
227
234
meshShader.setMatrix4fv (" projection" , 1 , GL_FALSE, projection);
@@ -237,14 +244,10 @@ int main()
237
244
}
238
245
meshShader.unbind ();
239
246
240
- // glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.)
241
- // -------------------------------------------------------------------------------
242
247
window.swapBuffers ();
243
248
glfwPollEvents ();
244
249
}
245
250
246
- // glfw: terminate, clearing all previously allocated GLFW resources.
247
- // ------------------------------------------------------------------
248
251
glfwTerminate ();
249
252
return 0 ;
250
253
}
@@ -353,23 +356,24 @@ void key_callback(GLFWwindow* window, int key, int scancode, int action, int mod
353
356
// Simple Day light set-up when user presses "1" key
354
357
if (key == GLFW_KEY_1 && action == GLFW_PRESS)
355
358
{
356
- directionalAmbient = (DIR_LIGHT_AMBIENT_VEC + glm::vec3 (0 .2f )) * colors[1 ];
357
- directionalDiffuse = (DIR_LIGHT_DIFFUSE_VEC + glm::vec3 (0 .4f )) * colors[1 ];
358
- directionalSpecular = (DIR_LIGHT_SPECULAR_VEC - glm::vec3 (0 .4f )) * colors[1 ];
359
+ lightColor = colors[4 ];
360
+ lightAlpha = 0 .5f ;
361
+
362
+ directionalAmbient = (DIR_LIGHT_AMBIENT_VEC + glm::vec3 (0 .2f )) * (lightColor * lightAlpha);
363
+ directionalDiffuse = (DIR_LIGHT_DIFFUSE_VEC + glm::vec3 (0 .4f )) * (lightColor * lightAlpha);
364
+ directionalSpecular = (DIR_LIGHT_SPECULAR_VEC - glm::vec3 (0 .4f )) * (lightColor * lightAlpha);
359
365
360
366
spotLightAmbient = glm::vec3 (0 .0f );
361
367
spotLightDiffuse = glm::vec3 (0 .0f );
362
368
spotLightSpecular = glm::vec3 (0 .0f );
363
369
364
370
365
- pointLightAmbient = POINT_LIGHT_AMBIENT_VEC * colors[ 4 ] ;
366
- pointLightDiffuse = POINT_LIGHT_DIFFUSE_VEC * colors[ 4 ] ;
367
- pointLightSpecular = POINT_LIGHT_SPECULAR_VEC * colors[ 4 ] ;
371
+ pointLightAmbient = POINT_LIGHT_AMBIENT_VEC * (lightColor * lightAlpha) ;
372
+ pointLightDiffuse = POINT_LIGHT_DIFFUSE_VEC * (lightColor * lightAlpha) ;
373
+ pointLightSpecular = POINT_LIGHT_SPECULAR_VEC * (lightColor * lightAlpha) ;
368
374
369
375
backgroundColor = DESERT_BACKGROUND_COLOR;
370
376
371
- lightColor = colors[4 ];
372
-
373
377
specularIntensity = 1 ;
374
378
375
379
cutOff = 0 ;
@@ -384,18 +388,19 @@ void key_callback(GLFWwindow* window, int key, int scancode, int action, int mod
384
388
if (key == GLFW_KEY_2 && action == GLFW_PRESS)
385
389
{
386
390
lightColor = colors[6 ];
391
+ lightAlpha = 0 .5f ;
387
392
388
- directionalAmbient = ( DIR_LIGHT_AMBIENT_VEC + glm::vec3 ( 0 . 05f )) * lightColor ;
389
- directionalDiffuse = ( DIR_LIGHT_DIFFUSE_VEC + glm::vec3 ( 0 . 1f )) * lightColor ;
390
- directionalSpecular = ( DIR_LIGHT_SPECULAR_VEC - glm::vec3 ( 0 . 2f )) * lightColor ;
393
+ directionalAmbient = DIR_LIGHT_AMBIENT_VEC * lightColor * lightAlpha ;
394
+ directionalDiffuse = DIR_LIGHT_DIFFUSE_VEC * lightColor * lightAlpha ;
395
+ directionalSpecular = DIR_LIGHT_SPECULAR_VEC * lightColor * lightAlpha ;
391
396
392
397
spotLightAmbient = SPOT_LIGHT_AMBIENT_VEC * lightColor;
393
398
spotLightDiffuse = SPOT_LIGHT_DIFFUSE_VEC * lightColor;
394
399
spotLightSpecular = SPOT_LIGHT_SPECULAR_VEC * lightColor;
395
400
396
- pointLightAmbient = POINT_LIGHT_AMBIENT_VEC * lightColor;
397
- pointLightDiffuse = POINT_LIGHT_DIFFUSE_VEC * lightColor;
398
- pointLightSpecular = POINT_LIGHT_SPECULAR_VEC * lightColor;
401
+ pointLightAmbient = POINT_LIGHT_AMBIENT_VEC * lightColor * lightAlpha ;
402
+ pointLightDiffuse = POINT_LIGHT_DIFFUSE_VEC * lightColor * lightAlpha ;
403
+ pointLightSpecular = POINT_LIGHT_SPECULAR_VEC * lightColor * lightAlpha ;
399
404
400
405
backgroundColor = FACTORY_BACKGROUND_COLOR;
401
406
@@ -412,22 +417,23 @@ void key_callback(GLFWwindow* window, int key, int scancode, int action, int mod
412
417
// Simple horror light set-up when user presses "3" key
413
418
if (key == GLFW_KEY_3 && action == GLFW_PRESS)
414
419
{
415
- directionalAmbient = glm::vec3 (0 .1f );
416
- directionalDiffuse = glm::vec3 (0 .1f );
417
- directionalSpecular = glm::vec3 (0 .1f );
420
+ lightColor = colors[0 ];
421
+ lightAlpha = 0 .3f ;
422
+
423
+ directionalAmbient = (DIR_LIGHT_AMBIENT_VEC - glm::vec3 (0 .15f )) * lightColor * lightAlpha;
424
+ directionalDiffuse = (DIR_LIGHT_DIFFUSE_VEC - glm::vec3 (0 .2f )) * lightColor * lightAlpha;
425
+ directionalSpecular = (DIR_LIGHT_SPECULAR_VEC - glm::vec3 (0 .2f )) * lightColor * lightAlpha;
418
426
419
427
spotLightAmbient = SPOT_LIGHT_AMBIENT_VEC;
420
428
spotLightDiffuse = SPOT_LIGHT_DIFFUSE_VEC;
421
429
spotLightSpecular = SPOT_LIGHT_SPECULAR_VEC;
422
430
423
- pointLightAmbient = POINT_LIGHT_AMBIENT_VEC - glm::vec3 ( 0 . 1f ) ;
424
- pointLightDiffuse = POINT_LIGHT_DIFFUSE_VEC - glm::vec3 ( 0 . 1f ) ;
425
- pointLightSpecular = POINT_LIGHT_SPECULAR_VEC - glm::vec3 ( 0 . 1f ) ;
431
+ pointLightAmbient = POINT_LIGHT_AMBIENT_VEC * lightColor * lightAlpha ;
432
+ pointLightDiffuse = POINT_LIGHT_DIFFUSE_VEC * lightColor * lightAlpha ;
433
+ pointLightSpecular = POINT_LIGHT_SPECULAR_VEC * lightColor * lightAlpha ;
426
434
427
435
backgroundColor = DEFAULT_BACKGROUND_COLOR;
428
436
429
- lightColor = colors[0 ];
430
-
431
437
specularIntensity = SPECULAR_INTENSITY;
432
438
433
439
cutOff = 5 .0f ;
@@ -441,22 +447,23 @@ void key_callback(GLFWwindow* window, int key, int scancode, int action, int mod
441
447
// Simple lab light set-up when user presses "4" key
442
448
if (key == GLFW_KEY_4 && action == GLFW_PRESS)
443
449
{
444
- directionalAmbient = DIR_LIGHT_AMBIENT_VEC * colors[3 ];
445
- directionalDiffuse = DIR_LIGHT_DIFFUSE_VEC * colors[3 ];
446
- directionalSpecular = DIR_LIGHT_SPECULAR_VEC * colors[3 ];
450
+ lightColor = colors[5 ];
451
+ lightAlpha = 0 .5f ;
452
+
453
+ directionalAmbient = (DIR_LIGHT_AMBIENT_VEC + glm::vec3 (0 .1f )) * lightColor * lightAlpha;
454
+ directionalDiffuse = (DIR_LIGHT_DIFFUSE_VEC + glm::vec3 (0 .1f )) * lightColor * lightAlpha;
455
+ directionalSpecular = (DIR_LIGHT_SPECULAR_VEC + glm::vec3 (0 .1f )) * lightColor * lightAlpha;
447
456
448
457
spotLightAmbient = SPOT_LIGHT_AMBIENT_VEC;
449
458
spotLightDiffuse = SPOT_LIGHT_DIFFUSE_VEC;
450
459
spotLightSpecular = SPOT_LIGHT_SPECULAR_VEC;
451
460
452
- pointLightAmbient = POINT_LIGHT_AMBIENT_VEC * colors[ 3 ] ;
453
- pointLightDiffuse = POINT_LIGHT_DIFFUSE_VEC;
454
- pointLightSpecular = POINT_LIGHT_SPECULAR_VEC;
461
+ pointLightAmbient = ( POINT_LIGHT_AMBIENT_VEC + glm::vec3 ( 0 . 1f )) * lightColor * lightAlpha ;
462
+ pointLightDiffuse = ( POINT_LIGHT_DIFFUSE_VEC + glm::vec3 ( 0 . 1f )) * lightColor * lightAlpha ;
463
+ pointLightSpecular = ( POINT_LIGHT_SPECULAR_VEC + glm::vec3 ( 0 . 1f )) * lightColor * lightAlpha ;
455
464
456
465
backgroundColor = LAB_BACKGROUND_COLOR;
457
466
458
- lightColor = colors[3 ];
459
-
460
467
specularIntensity = SPECULAR_INTENSITY;
461
468
462
469
cutOff = CUT_OFF;
0 commit comments