@@ -60,7 +60,7 @@ float lastY = HEIGHT / 2.0f;
60
60
float ambientStrength = 0 .1f ;
61
61
float lightStrength = 0 .1f ;
62
62
float specularStrength = 0 .1f ;
63
- float specularIntensity = 32 ;
63
+ float specularIntensity = 2 ;
64
64
65
65
// Float array for cube, each point has 3 coordinated (x,y,z)
66
66
float vertices[] = {
@@ -392,82 +392,88 @@ int main()
392
392
unsigned int moduloVal = 6 ;
393
393
unsigned int lightCubeIndex = 0 ;
394
394
glm::vec3 lightPosition;
395
- // If index devides by 3, cube will be a light source
396
- if (i == lightCubeIndex)
397
- {
398
- lightShader.use ();
399
- // Set perspective projection matrix
400
- projection = glm::perspective (glm::radians (camera.getFov ()), WIDTH / HEIGHT, 0 .1f , 100 .0f );
401
- lightShader.setMatrix4fv (" projection" , 1 , GL_FALSE, glm::value_ptr (projection));
402
-
403
- // Set view matrix
404
- view = camera.calculateLookAtMatrix (camera.getPosition (), camera.getPosition () + camera.getFront (), camera.getUp ());
405
- lightShader.setMatrix4fv (" view" , 1 , GL_FALSE, glm::value_ptr (view));
406
-
407
- // Simple use of sin to make the light cube move around a bit
408
- lightPosition = glm::vec3 (1 .0f + sin (glfwGetTime ()) * 2 .0f , sin (glfwGetTime () / 2 .0f ), 0 .0f );
409
- // Translate (move) light cube to updated position
410
- model = glm::translate (model, lightPosition);
411
- // Make light cube a fith (1/5) of it's normal size
412
- model = glm::scale (model, glm::vec3 (0 .2f , 0 .2f , 0 .2f ));
413
- // Set all changes made to model
414
- lightShader.setMatrix4fv (" model" , 1 , GL_FALSE, glm::value_ptr (model));
415
-
416
- // Set light object's light strength
417
- lightShader.setFloat (" strength" , lightStrength);
418
- }
419
- else
420
- {
421
- // Simple use of GLFW time function with modulo to have different angles to apply on the cubes
422
- angle = glfwGetTime () * 15 .0f * (((i + 1 ) % moduloVal) + 1 );
423
- // How much the rotation affects each axis, 1 is full effect and 0 is none
424
- rotationVec = glm::vec3 (1 .0f , 0 .5f , 0 .2f );
425
- // Update uniform matrix in colorShader program
426
- colorShader.use ();
427
- // Set light properties
428
- colorShader.setVec3 (" viewPosition" , glm::value_ptr (camera.getPosition ()));
429
- colorShader.setVec3 (" light.position" , glm::value_ptr (lightPosition));
430
- colorShader.setVec3 (" light.ambient" , glm::value_ptr (glm::vec3 (1 .0f ) * lightStrength));
431
- colorShader.setVec3 (" light.diffuse" , glm::value_ptr (glm::vec3 (1 .0f ) * lightStrength));
432
- colorShader.setVec3 (" light.specular" , glm::value_ptr (glm::vec3 (1 .0f ) * lightStrength));
433
-
434
- // Set material properties
435
- colorShader.setVec3 (" material.ambient" , glm::value_ptr (glm::vec3 (0.5 ) * lightStrength));
436
- colorShader.setVec3 (" material.diffuse" , glm::value_ptr (glm::vec3 (0.8 ) * lightStrength));
437
- colorShader.setVec3 (" material.specular" , glm::value_ptr (glm::vec3 (1.0 ) * lightStrength));
438
- colorShader.setFloat (" material.shininess" , specularIntensity);
439
-
440
- // Calculate and set object normal vector so we can calculate in shader how much the light hits the object
441
- colorShader.setMatrix3fv (" normalMatrix" , 1 , GL_TRUE, glm::value_ptr (glm::mat3 (glm::inverse (model))));
442
-
443
- // Set perspective projection matrix
444
- projection = glm::perspective (glm::radians (camera.getFov ()), WIDTH / HEIGHT, 0 .1f , 100 .0f );
445
- colorShader.setMatrix4fv (" projection" , 1 , GL_FALSE, glm::value_ptr (projection));
446
-
447
- // Set view matrix
448
- view = camera.calculateLookAtMatrix (camera.getPosition (), camera.getPosition () + camera.getFront (), camera.getUp ());
449
- colorShader.setMatrix4fv (" view" , 1 , GL_FALSE, glm::value_ptr (view));
450
-
451
- // Rotate cube
452
- model = glm::rotate (model, glm::radians (angle), rotationVec);
453
- // Set changes made to model
454
- colorShader.setMatrix4fv (" model" , 1 , GL_FALSE, glm::value_ptr (model));
455
-
456
- // Set texture unit 0 active (unnecessary if we have only one texture, texture unit 0 is the default)
457
- glActiveTexture (GL_TEXTURE0);
458
- // Bind "diffuseMap" to texture unit 0
459
- glBindTexture (GL_TEXTURE_2D, diffuseMap);
460
-
461
- // Set texture unit 1 active
462
- glActiveTexture (GL_TEXTURE1);
463
- // Bind "specularMap" to texture unit 1
464
- glBindTexture (GL_TEXTURE_2D, specularMap);
465
-
466
- // Set texture unit 2 active
467
- glActiveTexture (GL_TEXTURE2);
468
- // bind "emissionMap" to texture unit 2
469
- glBindTexture (GL_TEXTURE_2D, emissionMap);
470
- }
395
+ // If cube is a light source
396
+ // if(i == lightCubeIndex)
397
+ // {
398
+ // lightShader.use();
399
+ // // Set perspective projection matrix
400
+ // projection = glm::perspective(glm::radians(camera.getFov()), WIDTH / HEIGHT, 0.1f, 100.0f);
401
+ // lightShader.setMatrix4fv("projection", 1, GL_FALSE, glm::value_ptr(projection));
402
+
403
+ // // Set view matrix
404
+ // view = camera.calculateLookAtMatrix(camera.getPosition(), camera.getPosition() + camera.getFront(), camera.getUp());
405
+ // lightShader.setMatrix4fv("view", 1, GL_FALSE, glm::value_ptr(view));
406
+
407
+ // // Simple use of sin to make the light cube move around a bit
408
+ // lightPosition = glm::vec3(1.0f + sin(glfwGetTime()) * 2.0f, sin(glfwGetTime() / 2.0f), 0.0f);
409
+ // // Translate (move) light cube to updated position
410
+ // model = glm::translate(model, lightPosition);
411
+ // // Make light cube a fith (1/5) of it's normal size
412
+ // model = glm::scale(model, glm::vec3(0.2f, 0.2f, 0.2f));
413
+ // // Set all changes made to model
414
+ // lightShader.setMatrix4fv("model", 1, GL_FALSE, glm::value_ptr(model));
415
+
416
+ // // Set light object's light strength
417
+ // lightShader.setFloat("strength", lightStrength);
418
+ // }
419
+ // else
420
+ // {
421
+ // Simple use of GLFW time function with modulo to have different angles to apply on the cubes
422
+ angle = glfwGetTime () * 15 .0f * (((i + 1 ) % moduloVal) + 1 );
423
+ // How much the rotation affects each axis, 1 is full effect and 0 is none
424
+ rotationVec = glm::vec3 (1 .0f , 0 .5f , 0 .2f );
425
+ // Update uniform matrix in colorShader program
426
+ colorShader.use ();
427
+ // Set light properties
428
+ colorShader.setVec3 (" viewPosition" , glm::value_ptr (camera.getPosition ()));
429
+ colorShader.setVec3 (" light.position" , glm::value_ptr (camera.getPosition ()));
430
+ colorShader.setVec3 (" light.direction" , glm::value_ptr (glm::vec3 (camera.getFront ().x , camera.getFront ().y , camera.getFront ().z )));
431
+ colorShader.setVec3 (" light.ambient" , glm::value_ptr (glm::vec3 (1 .0f ) * lightStrength));
432
+ colorShader.setVec3 (" light.diffuse" , glm::value_ptr (glm::vec3 (1 .0f ) * lightStrength));
433
+ colorShader.setVec3 (" light.specular" , glm::value_ptr (glm::vec3 (1 .0f ) * lightStrength));
434
+ colorShader.setFloat (" light.costant" , 1 .0f );
435
+ colorShader.setFloat (" light.linear" , 0 .09f );
436
+ colorShader.setFloat (" light.quadratic" , 0 .032f );
437
+ colorShader.setFloat (" light.cutOff" , glm::cos (glm::radians (15 .0f )));
438
+ colorShader.setFloat (" light.outerCutOff" , glm::cos (glm::radians (17 .5f )));
439
+
440
+ // Set material properties
441
+ colorShader.setVec3 (" material.ambient" , glm::value_ptr (glm::vec3 (0.5 ) * lightStrength));
442
+ colorShader.setVec3 (" material.diffuse" , glm::value_ptr (glm::vec3 (0.8 ) * lightStrength));
443
+ colorShader.setVec3 (" material.specular" , glm::value_ptr (glm::vec3 (1.0 ) * lightStrength));
444
+ colorShader.setFloat (" material.shininess" , specularIntensity);
445
+
446
+ // Calculate and set object normal vector so we can calculate in shader how much the light hits the object
447
+ colorShader.setMatrix3fv (" normalMatrix" , 1 , GL_TRUE, glm::value_ptr (glm::mat3 (glm::inverse (model))));
448
+
449
+ // Set perspective projection matrix
450
+ projection = glm::perspective (glm::radians (camera.getFov ()), WIDTH / HEIGHT, 0 .1f , 100 .0f );
451
+ colorShader.setMatrix4fv (" projection" , 1 , GL_FALSE, glm::value_ptr (projection));
452
+
453
+ // Set view matrix
454
+ view = camera.calculateLookAtMatrix (camera.getPosition (), camera.getPosition () + camera.getFront (), camera.getUp ());
455
+ colorShader.setMatrix4fv (" view" , 1 , GL_FALSE, glm::value_ptr (view));
456
+
457
+ // Rotate cube
458
+ model = glm::rotate (model, glm::radians (angle), rotationVec);
459
+ // Set changes made to model
460
+ colorShader.setMatrix4fv (" model" , 1 , GL_FALSE, glm::value_ptr (model));
461
+
462
+ // Set texture unit 0 active (unnecessary if we have only one texture, texture unit 0 is the default)
463
+ glActiveTexture (GL_TEXTURE0);
464
+ // Bind "diffuseMap" to texture unit 0
465
+ glBindTexture (GL_TEXTURE_2D, diffuseMap);
466
+
467
+ // Set texture unit 1 active
468
+ glActiveTexture (GL_TEXTURE1);
469
+ // Bind "specularMap" to texture unit 1
470
+ glBindTexture (GL_TEXTURE_2D, specularMap);
471
+
472
+ // Set texture unit 2 active
473
+ glActiveTexture (GL_TEXTURE2);
474
+ // bind "emissionMap" to texture unit 2
475
+ glBindTexture (GL_TEXTURE_2D, emissionMap);
476
+ // }
471
477
// Render model
472
478
mesh.render (GL_TRIANGLES, 36 , GL_UNSIGNED_INT, nullptr );
473
479
}
0 commit comments