Skip to content

Commit e9ab780

Browse files
committed
Updated light directory
1 parent 3e6418f commit e9ab780

File tree

3 files changed

+112
-85
lines changed

3 files changed

+112
-85
lines changed

8-light/main.cpp

Lines changed: 83 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ float lastY = HEIGHT / 2.0f;
6060
float ambientStrength = 0.1f;
6161
float lightStrength = 0.1f;
6262
float specularStrength = 0.1f;
63-
float specularIntensity = 32;
63+
float specularIntensity = 2;
6464

6565
// Float array for cube, each point has 3 coordinated (x,y,z)
6666
float vertices[] = {
@@ -392,82 +392,88 @@ int main()
392392
unsigned int moduloVal = 6;
393393
unsigned int lightCubeIndex = 0;
394394
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+
// }
471477
// Render model
472478
mesh.render(GL_TRIANGLES, 36, GL_UNSIGNED_INT, nullptr);
473479
}

8-light/shaders/color.fs

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,34 +19,58 @@ struct Material
1919
struct Light
2020
{
2121
vec3 position;
22+
vec3 direction;
23+
float cutOff;
24+
float outerCutOff;
25+
2226
vec3 ambient;
2327
vec3 diffuse;
2428
vec3 specular;
29+
30+
float constant;
31+
float linear;
32+
float quadratic;
2533
};
2634

2735
uniform Material material;
2836
uniform Light light;
2937

3038
void main()
3139
{
40+
vec3 lightDirection = normalize(light.position - fragPosition);
41+
3242
// Ambient light calculation
3343
vec3 ambientLight = light.ambient * texture(material.diffuse, textureCoordinates).rgb;
3444

3545
// Diffuse light calculation
3646
vec3 norm = normalize(normal);
37-
vec3 lightDirection = normalize(light.position - fragPosition);
38-
float diff = max(dot(norm, lightDirection), 0.0);
47+
float diff = max(dot(norm, light.direction), 0.0);
3948
vec3 diffuseLight = light.diffuse * diff * texture(material.diffuse, textureCoordinates).rgb;
4049

4150
// Specular light calculation
42-
vec3 viewDirection = normalize(viewPosition - fragPosition);
43-
vec3 reflectedLightDirection = reflect(-lightDirection, norm);
51+
vec3 viewDirection = normalize(viewPosition -fragPosition);
52+
vec3 reflectedLightDirection = reflect(-light.direction, norm);
4453
float specular = pow(max(dot(viewDirection, reflectedLightDirection), 0.0), material.shininess);
4554
vec3 specularLight = specular * light.specular * material.shininess * texture(material.specular, textureCoordinates).rgb;
4655

4756
// Emission light from object
4857
vec3 emissionLight = texture(material.emission, textureCoordinates).rgb;
58+
59+
float theta = dot(lightDirection, normalize(-light.direction));
60+
float epsilon = (light.cutOff - light.outerCutOff);
61+
float intensity = clamp((theta - light.outerCutOff) / epsilon, 0.0, 1.0);
62+
63+
diffuseLight *= intensity;
64+
specularLight *= intensity;
65+
66+
// Calculate attenuation
67+
float distance = length(light.position - fragPosition);
68+
float attenuation = 1.0 / (light.constant + light.linear * distance + light.quadratic * (distance * distance));
4969

70+
ambientLight *= attenuation;
71+
diffuseLight *= attenuation;
72+
specularLight *= attenuation;
73+
5074
// By mixing different light components we get the Phong light model
5175
vec3 result = specularLight + diffuseLight + ambientLight + emissionLight;
5276
// Set fragment color

8-light/shaders/light.fs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@
22

33
out vec4 fragColor;
44

5-
uniform vec3 color;
6-
uniform float strength;
7-
85
void main()
96
{
10-
fragColor = vec4(color * strength, 1.0);
7+
fragColor = vec4(1.0);
118
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy