From fc3806e73d1c58cc382aff35d7037b667aaaf230 Mon Sep 17 00:00:00 2001 From: Nigel Date: Sun, 13 Feb 2022 14:38:39 +0100 Subject: [PATCH] Basic Lighting Added lighting in to the shader Lighting model: Phong --- shader.fs | 32 ++++++++++++++++- shader.vs | 8 +++-- src/main.cpp | 97 ++++++++++++++++++++++++++++------------------------ 3 files changed, 89 insertions(+), 48 deletions(-) diff --git a/shader.fs b/shader.fs index 00ce963..e2d9357 100644 --- a/shader.fs +++ b/shader.fs @@ -1,11 +1,41 @@ #version 460 core out vec4 FragColor; +in vec3 Normal; +in vec3 FragPos; + uniform vec3 objectColor; uniform vec3 lightColor; +uniform vec3 lightPos; +uniform vec3 viewPos; + 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); } \ No newline at end of file diff --git a/shader.vs b/shader.vs index edc6356..c26746d 100644 --- a/shader.vs +++ b/shader.vs @@ -1,15 +1,19 @@ #version 460 core layout (location=0) in vec3 aPos; - +layout (location=1) in vec3 aNormal; uniform mat4 model; uniform mat4 view; uniform mat4 projection; +out vec3 FragPos; +out vec3 Normal; + void main(){ gl_Position = projection * view * model * vec4(aPos , 1.0); - + FragPos = vec3(model * vec4(aPos, 1.0)); + Normal = mat3(transpose(inverse(model))) * aNormal; } diff --git a/src/main.cpp b/src/main.cpp index 1827033..b680aff 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -91,51 +91,50 @@ int main() { Shader lightshader("lightsource.vs", "lightsource.fs"); Shader shader ("shader.vs", "shader.fs"); - float vertices[] = { - -0.5f, -0.5f, -0.5f, - 0.5f, -0.5f, -0.5f, - 0.5f, 0.5f, -0.5f, - 0.5f, 0.5f, -0.5f, - -0.5f, 0.5f, -0.5f, - -0.5f, -0.5f, -0.5f, +float vertices[] = { + -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, + 0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, + 0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, + 0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, + -0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, + -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, - -0.5f, -0.5f, 0.5f, - 0.5f, -0.5f, 0.5f, - 0.5f, 0.5f, 0.5f, - 0.5f, 0.5f, 0.5f, - -0.5f, 0.5f, 0.5f, - -0.5f, -0.5f, 0.5f, + -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, + 0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, + 0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f, + 0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f, + -0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f, + -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, - -0.5f, 0.5f, 0.5f, - -0.5f, 0.5f, -0.5f, - -0.5f, -0.5f, -0.5f, - -0.5f, -0.5f, -0.5f, - -0.5f, -0.5f, 0.5f, - -0.5f, 0.5f, 0.5f, + -0.5f, 0.5f, 0.5f, -1.0f, 0.0f, 0.0f, + -0.5f, 0.5f, -0.5f, -1.0f, 0.0f, 0.0f, + -0.5f, -0.5f, -0.5f, -1.0f, 0.0f, 0.0f, + -0.5f, -0.5f, -0.5f, -1.0f, 0.0f, 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0.5f, 0.5f, 0.0f, 1.0f, 0.0f, + -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f +}; -#pragma region Objects unsigned int VBO, VAO; glGenVertexArrays(1, &VAO); glGenBuffers(1, &VBO); @@ -146,22 +145,21 @@ int main() { glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); // 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); + glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*) (3*sizeof(float))); + glEnableVertexAttribArray(1); + unsigned int lightVAO; glGenVertexArrays(1, &lightVAO); glBindVertexArray(lightVAO); 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); -#pragma endregion - - - glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED); 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); + 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 shader.use(); shader.setVec3("objectColor", glm::vec3( 1.0f, 0.5, 0.31f)); 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("projection", projection);