Basic Lighting

Added lighting in to the shader
Lighting model: Phong
This commit is contained in:
Nigel Barink 2022-02-13 14:38:39 +01:00
parent 06b979dfaa
commit fc3806e73d
3 changed files with 89 additions and 48 deletions

View File

@ -1,11 +1,41 @@
#version 460 core #version 460 core
out vec4 FragColor; out vec4 FragColor;
in vec3 Normal;
in vec3 FragPos;
uniform vec3 objectColor; uniform vec3 objectColor;
uniform vec3 lightColor; uniform vec3 lightColor;
uniform vec3 lightPos;
uniform vec3 viewPos;
void main() void main()
{ {
FragColor = vec4(lightColor * objectColor, 1.0); // ambient lighting calculation
float ambientStrength = 0.1;
vec3 ambient = ambientStrength * lightColor;
// diffuse lighting calculation
vec3 norm = normalize(Normal);
vec3 lightDir = normalize(lightPos - FragPos);
float diff = max(dot(norm, lightDir), 0.0);
vec3 diffuse = diff * lightColor;
// specular lighting
float specularStrength = 0.5;
float shininess = 32;
vec3 viewDir = normalize(viewPos - FragPos);
vec3 reflectDir = reflect(-lightDir, norm);
float spec = pow(max(dot(viewDir, reflectDir), 0.0), shininess);
vec3 specular = specularStrength * spec * lightColor;
// Calculate the result;
vec3 result = (ambient + diffuse + specular) * objectColor;
FragColor = vec4(result, 1.0);
} }

View File

@ -1,15 +1,19 @@
#version 460 core #version 460 core
layout (location=0) in vec3 aPos; layout (location=0) in vec3 aPos;
layout (location=1) in vec3 aNormal;
uniform mat4 model; uniform mat4 model;
uniform mat4 view; uniform mat4 view;
uniform mat4 projection; uniform mat4 projection;
out vec3 FragPos;
out vec3 Normal;
void main(){ void main(){
gl_Position = projection * view * model * vec4(aPos , 1.0); gl_Position = projection * view * model * vec4(aPos , 1.0);
FragPos = vec3(model * vec4(aPos, 1.0));
Normal = mat3(transpose(inverse(model))) * aNormal;
} }

View File

@ -92,50 +92,49 @@ int main() {
Shader shader ("shader.vs", "shader.fs"); Shader shader ("shader.vs", "shader.fs");
float vertices[] = { float vertices[] = {
-0.5f, -0.5f, -0.5f, -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -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 1.0f, 0.0f, 0.0f,
0.5f, -0.5f, -0.5f, 0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f,
0.5f, -0.5f, -0.5f, 0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f,
0.5f, -0.5f, 0.5f, 0.5f, -0.5f, 0.5f, 1.0f, 0.0f, 0.0f,
0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.0f,
-0.5f, -0.5f, -0.5f, -0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f,
0.5f, -0.5f, -0.5f, 0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f,
0.5f, -0.5f, 0.5f, 0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f,
0.5f, -0.5f, 0.5f, 0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f,
-0.5f, -0.5f, 0.5f, -0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f,
-0.5f, -0.5f, -0.5f, -0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f,
-0.5f, 0.5f, -0.5f, -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f,
0.5f, 0.5f, -0.5f, 0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f,
-0.5f, 0.5f, 0.5f, -0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f,
-0.5f, 0.5f, -0.5f, -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f
}; };
#pragma region Objects
unsigned int VBO, VAO; unsigned int VBO, VAO;
glGenVertexArrays(1, &VAO); glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO); glGenBuffers(1, &VBO);
@ -146,22 +145,21 @@ int main() {
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
// position attribute // position attribute
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0); glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*) (3*sizeof(float)));
glEnableVertexAttribArray(1);
unsigned int lightVAO; unsigned int lightVAO;
glGenVertexArrays(1, &lightVAO); glGenVertexArrays(1, &lightVAO);
glBindVertexArray(lightVAO); glBindVertexArray(lightVAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO); glBindBuffer(GL_ARRAY_BUFFER, VBO);
glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE, 3* sizeof(float), (void*) 0); glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE, 6* sizeof(float), (void*) 0);
glEnableVertexAttribArray(0); glEnableVertexAttribArray(0);
#pragma endregion
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED); glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
glfwSetCursorPosCallback(window, mouse_callback); glfwSetCursorPosCallback(window, mouse_callback);
@ -189,10 +187,19 @@ while(!glfwWindowShouldClose(window))
projection = glm::perspective(glm::radians(camera.Zoom), (float)800 / (float)600, 0.1f, 100.0f); projection = glm::perspective(glm::radians(camera.Zoom), (float)800 / (float)600, 0.1f, 100.0f);
float orbital_speed = .4 ;
// move light around
lightpos.x = 1.0 + sin(glfwGetTime()) * 2.0f;
lightpos.y = sin(glfwGetTime() / 2.0f) * 1.0f;
// draw cubes // draw cubes
shader.use(); shader.use();
shader.setVec3("objectColor", glm::vec3( 1.0f, 0.5, 0.31f)); shader.setVec3("objectColor", glm::vec3( 1.0f, 0.5, 0.31f));
shader.setVec3("lightColor",glm::vec3( 1.0f, 1.0f, 1.0f)); shader.setVec3("lightColor",glm::vec3( 1.0f, 1.0f, 1.0f));
shader.setVec3("lightPos", lightpos);
shader.setVec3("viewPos", camera.Position);
shader.setMat4("view", view); shader.setMat4("view", view);
shader.setMat4("projection", projection); shader.setMat4("projection", projection);