From 320db41e892404d68f78cefb1e6fcfcb41c2098d Mon Sep 17 00:00:00 2001 From: Joey de Vries Date: Tue, 30 May 2017 19:23:56 +0200 Subject: [PATCH] Code re-work and content update: lighting. --- .../5.1.transformations/transformations.cpp | 2 +- .../7.4.camera_class/camera_class.cpp | 5 +- src/2.lighting/1.colors/1.colors.fs | 4 +- src/2.lighting/1.colors/1.colors.vs | 2 +- src/2.lighting/1.colors/1.lamp.fs | 4 +- src/2.lighting/1.colors/1.lamp.vs | 2 +- src/2.lighting/1.colors/colors.cpp | 20 ++++--- .../2.1.basic_lighting.fs | 8 +-- .../2.1.basic_lighting.vs | 4 +- .../2.1.basic_lighting_diffuse/2.1.lamp.fs | 7 +++ .../2.1.basic_lighting_diffuse/2.1.lamp.vs | 11 ++++ .../basic_lighting_diffuse.cpp | 22 ++++--- .../2.2.basic_lighting.fs | 10 ++-- .../2.2.basic_lighting.vs | 4 +- .../2.2.basic_lighting_specular/2.2.lamp.fs | 7 +++ .../2.2.basic_lighting_specular/2.2.lamp.vs | 11 ++++ .../basic_lighting_specular.cpp | 22 ++++--- src/2.lighting/3.1.materials/3.1.lamp.fs | 7 +++ src/2.lighting/3.1.materials/3.1.lamp.vs | 11 ++++ src/2.lighting/3.1.materials/3.1.materials.fs | 4 +- src/2.lighting/3.1.materials/3.1.materials.vs | 4 +- src/2.lighting/3.1.materials/materials.cpp | 22 ++++--- .../3.2.materials_exercise1/3.2.lamp.fs | 7 +++ .../3.2.materials_exercise1/3.2.lamp.vs | 11 ++++ .../3.2.materials_exercise1/3.2.materials.fs | 45 ++++++++++++++ .../3.2.materials_exercise1/3.2.materials.vs | 18 ++++++ .../materials_exercise1.cpp | 58 ++++++++++--------- .../lighting_maps_diffuse.cpp | 22 ++++--- .../lighting_maps_specular.cpp | 23 ++++---- .../lighting_maps_exercise4.cpp | 22 ++++--- .../light_casters_directional.cpp | 22 ++++--- .../light_casters_point.cpp | 23 ++++---- .../light_casters_spot.cpp | 23 ++++---- .../light_casters_spot_soft.cpp | 24 ++++---- .../6.multiple_lights/6.multiple_lights.cpp | 24 ++++---- 35 files changed, 347 insertions(+), 168 deletions(-) create mode 100644 src/2.lighting/2.1.basic_lighting_diffuse/2.1.lamp.fs create mode 100644 src/2.lighting/2.1.basic_lighting_diffuse/2.1.lamp.vs create mode 100644 src/2.lighting/2.2.basic_lighting_specular/2.2.lamp.fs create mode 100644 src/2.lighting/2.2.basic_lighting_specular/2.2.lamp.vs create mode 100644 src/2.lighting/3.1.materials/3.1.lamp.fs create mode 100644 src/2.lighting/3.1.materials/3.1.lamp.vs create mode 100644 src/2.lighting/3.2.materials_exercise1/3.2.lamp.fs create mode 100644 src/2.lighting/3.2.materials_exercise1/3.2.lamp.vs create mode 100644 src/2.lighting/3.2.materials_exercise1/3.2.materials.fs create mode 100644 src/2.lighting/3.2.materials_exercise1/3.2.materials.vs diff --git a/src/1.getting_started/5.1.transformations/transformations.cpp b/src/1.getting_started/5.1.transformations/transformations.cpp index 3b84dab..6f72710 100644 --- a/src/1.getting_started/5.1.transformations/transformations.cpp +++ b/src/1.getting_started/5.1.transformations/transformations.cpp @@ -169,11 +169,11 @@ int main() transform = glm::rotate(transform, (float)glfwGetTime(), glm::vec3(0.0f, 0.0f, 1.0f)); // get matrix's uniform location and set matrix + ourShader.use(); unsigned int transformLoc = glGetUniformLocation(ourShader.ID, "transform"); glUniformMatrix4fv(transformLoc, 1, GL_FALSE, glm::value_ptr(transform)); // render container - ourShader.use(); glBindVertexArray(VAO); glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); diff --git a/src/1.getting_started/7.4.camera_class/camera_class.cpp b/src/1.getting_started/7.4.camera_class/camera_class.cpp index f563cdf..ba336de 100644 --- a/src/1.getting_started/7.4.camera_class/camera_class.cpp +++ b/src/1.getting_started/7.4.camera_class/camera_class.cpp @@ -23,8 +23,8 @@ const unsigned int SCR_HEIGHT = 600; // camera Camera camera(glm::vec3(0.0f, 0.0f, 3.0f)); -float lastX = 800.0f / 2.0; -float lastY = 600.0 / 2.0; +float lastX = SCR_WIDTH / 2.0f; +float lastY = SCR_HEIGHT / 2.0f; bool firstMouse = true; // timing @@ -281,7 +281,6 @@ void processInput(GLFWwindow *window) if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) glfwSetWindowShouldClose(window, true); - float cameraSpeed = 2.5 * deltaTime; if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) camera.ProcessKeyboard(FORWARD, deltaTime); if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) diff --git a/src/2.lighting/1.colors/1.colors.fs b/src/2.lighting/1.colors/1.colors.fs index 0ba13e5..355b6ed 100644 --- a/src/2.lighting/1.colors/1.colors.fs +++ b/src/2.lighting/1.colors/1.colors.fs @@ -1,10 +1,10 @@ #version 330 core -out vec4 fragColor; +out vec4 FragColor; uniform vec3 objectColor; uniform vec3 lightColor; void main() { - fragColor = vec4(lightColor * objectColor, 1.0f); + FragColor = vec4(lightColor * objectColor, 1.0); } \ No newline at end of file diff --git a/src/2.lighting/1.colors/1.colors.vs b/src/2.lighting/1.colors/1.colors.vs index a175d42..752ce21 100644 --- a/src/2.lighting/1.colors/1.colors.vs +++ b/src/2.lighting/1.colors/1.colors.vs @@ -7,5 +7,5 @@ uniform mat4 projection; void main() { - gl_Position = projection * view * model * vec4(aPos, 1.0f); + gl_Position = projection * view * model * vec4(aPos, 1.0); } \ No newline at end of file diff --git a/src/2.lighting/1.colors/1.lamp.fs b/src/2.lighting/1.colors/1.lamp.fs index 61fb69a..76f765e 100644 --- a/src/2.lighting/1.colors/1.lamp.fs +++ b/src/2.lighting/1.colors/1.lamp.fs @@ -1,7 +1,7 @@ #version 330 core -out vec4 fragColor; +out vec4 FragColor; void main() { - fragColor = vec4(1.0f); // set alle 4 vector values to 1.0f + FragColor = vec4(1.0); // set alle 4 vector values to 1.0 } \ No newline at end of file diff --git a/src/2.lighting/1.colors/1.lamp.vs b/src/2.lighting/1.colors/1.lamp.vs index a175d42..752ce21 100644 --- a/src/2.lighting/1.colors/1.lamp.vs +++ b/src/2.lighting/1.colors/1.lamp.vs @@ -7,5 +7,5 @@ uniform mat4 projection; void main() { - gl_Position = projection * view * model * vec4(aPos, 1.0f); + gl_Position = projection * view * model * vec4(aPos, 1.0); } \ No newline at end of file diff --git a/src/2.lighting/1.colors/colors.cpp b/src/2.lighting/1.colors/colors.cpp index 4ef0b87..265f42b 100644 --- a/src/2.lighting/1.colors/colors.cpp +++ b/src/2.lighting/1.colors/colors.cpp @@ -16,13 +16,18 @@ void mouse_callback(GLFWwindow* window, double xpos, double ypos); void scroll_callback(GLFWwindow* window, double xoffset, double yoffset); void processInput(GLFWwindow *window); +// settings +const unsigned int SCR_WIDTH = 800; +const unsigned int SCR_HEIGHT = 600; + // camera Camera camera(glm::vec3(0.0f, 0.0f, 3.0f)); -float lastX = 800.0f / 2.0; -float lastY = 600.0 / 2.0; +float lastX = SCR_WIDTH / 2.0f; +float lastY = SCR_HEIGHT / 2.0f; bool firstMouse = true; -float deltaTime = 0.0f; // time between current frame and last frame +// timing +float deltaTime = 0.0f; float lastFrame = 0.0f; // lighting @@ -40,14 +45,14 @@ int main() // glfw window creation // -------------------- - GLFWwindow* window = glfwCreateWindow(800, 600, "LearnOpenGL", NULL, NULL); - glfwMakeContextCurrent(window); + GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", NULL, NULL); if (window == NULL) { std::cout << "Failed to create GLFW window" << std::endl; glfwTerminate(); return -1; } + glfwMakeContextCurrent(window); glfwSetFramebufferSizeCallback(window, framebuffer_size_callback); glfwSetCursorPosCallback(window, mouse_callback); glfwSetScrollCallback(window, scroll_callback); @@ -159,7 +164,7 @@ int main() // render // ------ - glClearColor(0.2f, 0.3f, 0.3f, 1.0f); + glClearColor(0.1f, 0.1f, 0.1f, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // be sure to activate shader when setting uniforms/drawing objects @@ -168,7 +173,7 @@ int main() lightingShader.setVec3("lightColor", 1.0f, 1.0f, 1.0f); // view/projection transformations - glm::mat4 projection = glm::perspective(glm::radians(camera.Zoom), 800.0f / 600.0f, 0.1f, 100.0f); + glm::mat4 projection = glm::perspective(glm::radians(camera.Zoom), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f); glm::mat4 view = camera.GetViewMatrix(); lightingShader.setMat4("projection", projection); lightingShader.setMat4("view", view); @@ -220,7 +225,6 @@ void processInput(GLFWwindow *window) if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) glfwSetWindowShouldClose(window, true); - float cameraSpeed = 2.5 * deltaTime; if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) camera.ProcessKeyboard(FORWARD, deltaTime); if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) diff --git a/src/2.lighting/2.1.basic_lighting_diffuse/2.1.basic_lighting.fs b/src/2.lighting/2.1.basic_lighting_diffuse/2.1.basic_lighting.fs index baa2bc9..105f994 100644 --- a/src/2.lighting/2.1.basic_lighting_diffuse/2.1.basic_lighting.fs +++ b/src/2.lighting/2.1.basic_lighting_diffuse/2.1.basic_lighting.fs @@ -1,5 +1,5 @@ #version 330 core -out vec4 fragColor; +out vec4 FragColor; in vec3 Normal; in vec3 FragPos; @@ -11,15 +11,15 @@ uniform vec3 objectColor; void main() { // ambient - float ambientStrength = 0.1f; + float ambientStrength = 0.1; vec3 ambient = ambientStrength * lightColor; // diffuse vec3 norm = normalize(Normal); vec3 lightDir = normalize(lightPos - FragPos); - float diff = max(dot(norm, lightDir), 0.0f); + float diff = max(dot(norm, lightDir), 0.0); vec3 diffuse = diff * lightColor; vec3 result = (ambient + diffuse) * objectColor; - fragColor = vec4(result, 1.0f); + FragColor = vec4(result, 1.0); } \ No newline at end of file diff --git a/src/2.lighting/2.1.basic_lighting_diffuse/2.1.basic_lighting.vs b/src/2.lighting/2.1.basic_lighting_diffuse/2.1.basic_lighting.vs index f1f39cb..d7ade7b 100644 --- a/src/2.lighting/2.1.basic_lighting_diffuse/2.1.basic_lighting.vs +++ b/src/2.lighting/2.1.basic_lighting_diffuse/2.1.basic_lighting.vs @@ -11,8 +11,8 @@ uniform mat4 projection; void main() { - FragPos = vec3(model * vec4(aPos, 1.0f)); + FragPos = vec3(model * vec4(aPos, 1.0)); Normal = aNormal; - gl_Position = projection * view * vec4(FragPos, 1.0f); + gl_Position = projection * view * vec4(FragPos, 1.0); } \ No newline at end of file diff --git a/src/2.lighting/2.1.basic_lighting_diffuse/2.1.lamp.fs b/src/2.lighting/2.1.basic_lighting_diffuse/2.1.lamp.fs new file mode 100644 index 0000000..76f765e --- /dev/null +++ b/src/2.lighting/2.1.basic_lighting_diffuse/2.1.lamp.fs @@ -0,0 +1,7 @@ +#version 330 core +out vec4 FragColor; + +void main() +{ + FragColor = vec4(1.0); // set alle 4 vector values to 1.0 +} \ No newline at end of file diff --git a/src/2.lighting/2.1.basic_lighting_diffuse/2.1.lamp.vs b/src/2.lighting/2.1.basic_lighting_diffuse/2.1.lamp.vs new file mode 100644 index 0000000..752ce21 --- /dev/null +++ b/src/2.lighting/2.1.basic_lighting_diffuse/2.1.lamp.vs @@ -0,0 +1,11 @@ +#version 330 core +layout (location = 0) in vec3 aPos; + +uniform mat4 model; +uniform mat4 view; +uniform mat4 projection; + +void main() +{ + gl_Position = projection * view * model * vec4(aPos, 1.0); +} \ No newline at end of file diff --git a/src/2.lighting/2.1.basic_lighting_diffuse/basic_lighting_diffuse.cpp b/src/2.lighting/2.1.basic_lighting_diffuse/basic_lighting_diffuse.cpp index b48407b..0b35e7c 100644 --- a/src/2.lighting/2.1.basic_lighting_diffuse/basic_lighting_diffuse.cpp +++ b/src/2.lighting/2.1.basic_lighting_diffuse/basic_lighting_diffuse.cpp @@ -16,13 +16,18 @@ void mouse_callback(GLFWwindow* window, double xpos, double ypos); void scroll_callback(GLFWwindow* window, double xoffset, double yoffset); void processInput(GLFWwindow *window); +// settings +const unsigned int SCR_WIDTH = 800; +const unsigned int SCR_HEIGHT = 600; + // camera Camera camera(glm::vec3(0.0f, 0.0f, 3.0f)); -float lastX = 800.0f / 2.0; -float lastY = 600.0 / 2.0; +float lastX = SCR_WIDTH / 2.0f; +float lastY = SCR_HEIGHT / 2.0f; bool firstMouse = true; -float deltaTime = 0.0f; // time between current frame and last frame +// timing +float deltaTime = 0.0f; float lastFrame = 0.0f; // lighting @@ -40,14 +45,14 @@ int main() // glfw window creation // -------------------- - GLFWwindow* window = glfwCreateWindow(800, 600, "LearnOpenGL", NULL, NULL); - glfwMakeContextCurrent(window); + GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", NULL, NULL); if (window == NULL) { std::cout << "Failed to create GLFW window" << std::endl; glfwTerminate(); return -1; } + glfwMakeContextCurrent(window); glfwSetFramebufferSizeCallback(window, framebuffer_size_callback); glfwSetCursorPosCallback(window, mouse_callback); glfwSetScrollCallback(window, scroll_callback); @@ -70,7 +75,7 @@ int main() // build and compile our shader zprogram // ------------------------------------ Shader lightingShader("2.1.basic_lighting.vs", "2.1.basic_lighting.fs"); - Shader lampShader("1.lamp.vs", "1.lamp.fs"); + Shader lampShader("2.1.lamp.vs", "2.1.lamp.fs"); // set up vertex data (and buffer(s)) and configure vertex attributes // ------------------------------------------------------------------ @@ -162,7 +167,7 @@ int main() // render // ------ - glClearColor(0.2f, 0.3f, 0.3f, 1.0f); + glClearColor(0.1f, 0.1f, 0.1f, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // be sure to activate shader when setting uniforms/drawing objects @@ -172,7 +177,7 @@ int main() lightingShader.setVec3("lightPos", lightPos); // view/projection transformations - glm::mat4 projection = glm::perspective(glm::radians(camera.Zoom), 800.0f / 600.0f, 0.1f, 100.0f); + glm::mat4 projection = glm::perspective(glm::radians(camera.Zoom), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f); glm::mat4 view = camera.GetViewMatrix(); lightingShader.setMat4("projection", projection); lightingShader.setMat4("view", view); @@ -224,7 +229,6 @@ void processInput(GLFWwindow *window) if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) glfwSetWindowShouldClose(window, true); - float cameraSpeed = 2.5 * deltaTime; if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) camera.ProcessKeyboard(FORWARD, deltaTime); if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) diff --git a/src/2.lighting/2.2.basic_lighting_specular/2.2.basic_lighting.fs b/src/2.lighting/2.2.basic_lighting_specular/2.2.basic_lighting.fs index 6db1777..8453bbc 100644 --- a/src/2.lighting/2.2.basic_lighting_specular/2.2.basic_lighting.fs +++ b/src/2.lighting/2.2.basic_lighting_specular/2.2.basic_lighting.fs @@ -1,5 +1,5 @@ #version 330 core -out vec4 fragColor; +out vec4 FragColor; in vec3 Normal; in vec3 FragPos; @@ -12,22 +12,22 @@ uniform vec3 objectColor; void main() { // ambient - float ambientStrength = 0.1f; + float ambientStrength = 0.1; vec3 ambient = ambientStrength * lightColor; // diffuse vec3 norm = normalize(Normal); vec3 lightDir = normalize(lightPos - FragPos); - float diff = max(dot(norm, lightDir), 0.0f); + float diff = max(dot(norm, lightDir), 0.0); vec3 diffuse = diff * lightColor; // specular - float specularStrength = 0.5f; + float specularStrength = 0.5; vec3 viewDir = normalize(viewPos - FragPos); vec3 reflectDir = reflect(-lightDir, norm); float spec = pow(max(dot(viewDir, reflectDir), 0.0), 32); vec3 specular = specularStrength * spec * lightColor; vec3 result = (ambient + diffuse + specular) * objectColor; - fragColor = vec4(result, 1.0f); + FragColor = vec4(result, 1.0); } \ No newline at end of file diff --git a/src/2.lighting/2.2.basic_lighting_specular/2.2.basic_lighting.vs b/src/2.lighting/2.2.basic_lighting_specular/2.2.basic_lighting.vs index 42fca3b..85bfc03 100644 --- a/src/2.lighting/2.2.basic_lighting_specular/2.2.basic_lighting.vs +++ b/src/2.lighting/2.2.basic_lighting_specular/2.2.basic_lighting.vs @@ -11,8 +11,8 @@ uniform mat4 projection; void main() { - FragPos = vec3(model * vec4(aPos, 1.0f)); + FragPos = vec3(model * vec4(aPos, 1.0)); Normal = mat3(transpose(inverse(model))) * aNormal; - gl_Position = projection * view * vec4(FragPos, 1.0f); + gl_Position = projection * view * vec4(FragPos, 1.0); } \ No newline at end of file diff --git a/src/2.lighting/2.2.basic_lighting_specular/2.2.lamp.fs b/src/2.lighting/2.2.basic_lighting_specular/2.2.lamp.fs new file mode 100644 index 0000000..76f765e --- /dev/null +++ b/src/2.lighting/2.2.basic_lighting_specular/2.2.lamp.fs @@ -0,0 +1,7 @@ +#version 330 core +out vec4 FragColor; + +void main() +{ + FragColor = vec4(1.0); // set alle 4 vector values to 1.0 +} \ No newline at end of file diff --git a/src/2.lighting/2.2.basic_lighting_specular/2.2.lamp.vs b/src/2.lighting/2.2.basic_lighting_specular/2.2.lamp.vs new file mode 100644 index 0000000..752ce21 --- /dev/null +++ b/src/2.lighting/2.2.basic_lighting_specular/2.2.lamp.vs @@ -0,0 +1,11 @@ +#version 330 core +layout (location = 0) in vec3 aPos; + +uniform mat4 model; +uniform mat4 view; +uniform mat4 projection; + +void main() +{ + gl_Position = projection * view * model * vec4(aPos, 1.0); +} \ No newline at end of file diff --git a/src/2.lighting/2.2.basic_lighting_specular/basic_lighting_specular.cpp b/src/2.lighting/2.2.basic_lighting_specular/basic_lighting_specular.cpp index 71b5f90..e9fb46a 100644 --- a/src/2.lighting/2.2.basic_lighting_specular/basic_lighting_specular.cpp +++ b/src/2.lighting/2.2.basic_lighting_specular/basic_lighting_specular.cpp @@ -16,13 +16,18 @@ void mouse_callback(GLFWwindow* window, double xpos, double ypos); void scroll_callback(GLFWwindow* window, double xoffset, double yoffset); void processInput(GLFWwindow *window); +// settings +const unsigned int SCR_WIDTH = 800; +const unsigned int SCR_HEIGHT = 600; + // camera Camera camera(glm::vec3(0.0f, 0.0f, 3.0f)); -float lastX = 800.0f / 2.0; -float lastY = 600.0 / 2.0; +float lastX = SCR_WIDTH / 2.0f; +float lastY = SCR_HEIGHT / 2.0f; bool firstMouse = true; -float deltaTime = 0.0f; // time between current frame and last frame +// timing +float deltaTime = 0.0f; float lastFrame = 0.0f; // lighting @@ -40,14 +45,14 @@ int main() // glfw window creation // -------------------- - GLFWwindow* window = glfwCreateWindow(800, 600, "LearnOpenGL", NULL, NULL); - glfwMakeContextCurrent(window); + GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", NULL, NULL); if (window == NULL) { std::cout << "Failed to create GLFW window" << std::endl; glfwTerminate(); return -1; } + glfwMakeContextCurrent(window); glfwSetFramebufferSizeCallback(window, framebuffer_size_callback); glfwSetCursorPosCallback(window, mouse_callback); glfwSetScrollCallback(window, scroll_callback); @@ -70,7 +75,7 @@ int main() // build and compile our shader zprogram // ------------------------------------ Shader lightingShader("2.2.basic_lighting.vs", "2.2.basic_lighting.fs"); - Shader lampShader("1.lamp.vs", "1.lamp.fs"); + Shader lampShader("2.2.lamp.vs", "2.2.lamp.fs"); // set up vertex data (and buffer(s)) and configure vertex attributes // ------------------------------------------------------------------ @@ -162,7 +167,7 @@ int main() // render // ------ - glClearColor(0.2f, 0.3f, 0.3f, 1.0f); + glClearColor(0.1f, 0.1f, 0.1f, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // be sure to activate shader when setting uniforms/drawing objects @@ -173,7 +178,7 @@ int main() lightingShader.setVec3("viewPos", camera.Position); // view/projection transformations - glm::mat4 projection = glm::perspective(glm::radians(camera.Zoom), 800.0f / 600.0f, 0.1f, 100.0f); + glm::mat4 projection = glm::perspective(glm::radians(camera.Zoom), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f); glm::mat4 view = camera.GetViewMatrix(); lightingShader.setMat4("projection", projection); lightingShader.setMat4("view", view); @@ -225,7 +230,6 @@ void processInput(GLFWwindow *window) if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) glfwSetWindowShouldClose(window, true); - float cameraSpeed = 2.5 * deltaTime; if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) camera.ProcessKeyboard(FORWARD, deltaTime); if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) diff --git a/src/2.lighting/3.1.materials/3.1.lamp.fs b/src/2.lighting/3.1.materials/3.1.lamp.fs new file mode 100644 index 0000000..76f765e --- /dev/null +++ b/src/2.lighting/3.1.materials/3.1.lamp.fs @@ -0,0 +1,7 @@ +#version 330 core +out vec4 FragColor; + +void main() +{ + FragColor = vec4(1.0); // set alle 4 vector values to 1.0 +} \ No newline at end of file diff --git a/src/2.lighting/3.1.materials/3.1.lamp.vs b/src/2.lighting/3.1.materials/3.1.lamp.vs new file mode 100644 index 0000000..752ce21 --- /dev/null +++ b/src/2.lighting/3.1.materials/3.1.lamp.vs @@ -0,0 +1,11 @@ +#version 330 core +layout (location = 0) in vec3 aPos; + +uniform mat4 model; +uniform mat4 view; +uniform mat4 projection; + +void main() +{ + gl_Position = projection * view * model * vec4(aPos, 1.0); +} \ No newline at end of file diff --git a/src/2.lighting/3.1.materials/3.1.materials.fs b/src/2.lighting/3.1.materials/3.1.materials.fs index 83e5cb3..c601bab 100644 --- a/src/2.lighting/3.1.materials/3.1.materials.fs +++ b/src/2.lighting/3.1.materials/3.1.materials.fs @@ -1,5 +1,5 @@ #version 330 core -out vec4 fragColor; +out vec4 FragColor; struct Material { vec3 ambient; @@ -41,5 +41,5 @@ void main() vec3 specular = light.specular * (spec * material.specular); vec3 result = ambient + diffuse + specular; - fragColor = vec4(result, 1.0f); + FragColor = vec4(result, 1.0); } \ No newline at end of file diff --git a/src/2.lighting/3.1.materials/3.1.materials.vs b/src/2.lighting/3.1.materials/3.1.materials.vs index 42fca3b..85bfc03 100644 --- a/src/2.lighting/3.1.materials/3.1.materials.vs +++ b/src/2.lighting/3.1.materials/3.1.materials.vs @@ -11,8 +11,8 @@ uniform mat4 projection; void main() { - FragPos = vec3(model * vec4(aPos, 1.0f)); + FragPos = vec3(model * vec4(aPos, 1.0)); Normal = mat3(transpose(inverse(model))) * aNormal; - gl_Position = projection * view * vec4(FragPos, 1.0f); + gl_Position = projection * view * vec4(FragPos, 1.0); } \ No newline at end of file diff --git a/src/2.lighting/3.1.materials/materials.cpp b/src/2.lighting/3.1.materials/materials.cpp index a232312..28719e1 100644 --- a/src/2.lighting/3.1.materials/materials.cpp +++ b/src/2.lighting/3.1.materials/materials.cpp @@ -16,13 +16,18 @@ void mouse_callback(GLFWwindow* window, double xpos, double ypos); void scroll_callback(GLFWwindow* window, double xoffset, double yoffset); void processInput(GLFWwindow *window); +// settings +const unsigned int SCR_WIDTH = 800; +const unsigned int SCR_HEIGHT = 600; + // camera Camera camera(glm::vec3(0.0f, 0.0f, 3.0f)); -float lastX = 800.0f / 2.0; -float lastY = 600.0 / 2.0; +float lastX = SCR_WIDTH / 2.0f; +float lastY = SCR_HEIGHT / 2.0f; bool firstMouse = true; -float deltaTime = 0.0f; // time between current frame and last frame +// timing +float deltaTime = 0.0f; float lastFrame = 0.0f; // lighting @@ -40,14 +45,14 @@ int main() // glfw window creation // -------------------- - GLFWwindow* window = glfwCreateWindow(800, 600, "LearnOpenGL", NULL, NULL); - glfwMakeContextCurrent(window); + GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", NULL, NULL); if (window == NULL) { std::cout << "Failed to create GLFW window" << std::endl; glfwTerminate(); return -1; } + glfwMakeContextCurrent(window); glfwSetFramebufferSizeCallback(window, framebuffer_size_callback); glfwSetCursorPosCallback(window, mouse_callback); glfwSetScrollCallback(window, scroll_callback); @@ -70,7 +75,7 @@ int main() // build and compile our shader zprogram // ------------------------------------ Shader lightingShader("3.1.materials.vs", "3.1.materials.fs"); - Shader lampShader("1.lamp.vs", "1.lamp.fs"); + Shader lampShader("3.1.lamp.vs", "3.1.lamp.fs"); // set up vertex data (and buffer(s)) and configure vertex attributes // ------------------------------------------------------------------ @@ -162,7 +167,7 @@ int main() // render // ------ - glClearColor(0.2f, 0.3f, 0.3f, 1.0f); + glClearColor(0.1f, 0.1f, 0.1f, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // be sure to activate shader when setting uniforms/drawing objects @@ -188,7 +193,7 @@ int main() lightingShader.setFloat("material.shininess", 32.0f); // view/projection transformations - glm::mat4 projection = glm::perspective(glm::radians(camera.Zoom), 800.0f / 600.0f, 0.1f, 100.0f); + glm::mat4 projection = glm::perspective(glm::radians(camera.Zoom), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f); glm::mat4 view = camera.GetViewMatrix(); lightingShader.setMat4("projection", projection); lightingShader.setMat4("view", view); @@ -240,7 +245,6 @@ void processInput(GLFWwindow *window) if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) glfwSetWindowShouldClose(window, true); - float cameraSpeed = 2.5 * deltaTime; if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) camera.ProcessKeyboard(FORWARD, deltaTime); if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) diff --git a/src/2.lighting/3.2.materials_exercise1/3.2.lamp.fs b/src/2.lighting/3.2.materials_exercise1/3.2.lamp.fs new file mode 100644 index 0000000..76f765e --- /dev/null +++ b/src/2.lighting/3.2.materials_exercise1/3.2.lamp.fs @@ -0,0 +1,7 @@ +#version 330 core +out vec4 FragColor; + +void main() +{ + FragColor = vec4(1.0); // set alle 4 vector values to 1.0 +} \ No newline at end of file diff --git a/src/2.lighting/3.2.materials_exercise1/3.2.lamp.vs b/src/2.lighting/3.2.materials_exercise1/3.2.lamp.vs new file mode 100644 index 0000000..752ce21 --- /dev/null +++ b/src/2.lighting/3.2.materials_exercise1/3.2.lamp.vs @@ -0,0 +1,11 @@ +#version 330 core +layout (location = 0) in vec3 aPos; + +uniform mat4 model; +uniform mat4 view; +uniform mat4 projection; + +void main() +{ + gl_Position = projection * view * model * vec4(aPos, 1.0); +} \ No newline at end of file diff --git a/src/2.lighting/3.2.materials_exercise1/3.2.materials.fs b/src/2.lighting/3.2.materials_exercise1/3.2.materials.fs new file mode 100644 index 0000000..c601bab --- /dev/null +++ b/src/2.lighting/3.2.materials_exercise1/3.2.materials.fs @@ -0,0 +1,45 @@ +#version 330 core +out vec4 FragColor; + +struct Material { + vec3 ambient; + vec3 diffuse; + vec3 specular; + float shininess; +}; + +struct Light { + vec3 position; + + vec3 ambient; + vec3 diffuse; + vec3 specular; +}; + +in vec3 FragPos; +in vec3 Normal; + +uniform vec3 viewPos; +uniform Material material; +uniform Light light; + +void main() +{ + // ambient + vec3 ambient = light.ambient * material.ambient; + + // diffuse + vec3 norm = normalize(Normal); + vec3 lightDir = normalize(light.position - FragPos); + float diff = max(dot(norm, lightDir), 0.0); + vec3 diffuse = light.diffuse * (diff * material.diffuse); + + // specular + vec3 viewDir = normalize(viewPos - FragPos); + vec3 reflectDir = reflect(-lightDir, norm); + float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess); + vec3 specular = light.specular * (spec * material.specular); + + vec3 result = ambient + diffuse + specular; + FragColor = vec4(result, 1.0); +} \ No newline at end of file diff --git a/src/2.lighting/3.2.materials_exercise1/3.2.materials.vs b/src/2.lighting/3.2.materials_exercise1/3.2.materials.vs new file mode 100644 index 0000000..85bfc03 --- /dev/null +++ b/src/2.lighting/3.2.materials_exercise1/3.2.materials.vs @@ -0,0 +1,18 @@ +#version 330 core +layout (location = 0) in vec3 aPos; +layout (location = 1) in vec3 aNormal; + +out vec3 FragPos; +out vec3 Normal; + +uniform mat4 model; +uniform mat4 view; +uniform mat4 projection; + +void main() +{ + FragPos = vec3(model * vec4(aPos, 1.0)); + Normal = mat3(transpose(inverse(model))) * aNormal; + + gl_Position = projection * view * vec4(FragPos, 1.0); +} \ No newline at end of file diff --git a/src/2.lighting/3.2.materials_exercise1/materials_exercise1.cpp b/src/2.lighting/3.2.materials_exercise1/materials_exercise1.cpp index baebf6c..1bf46f5 100644 --- a/src/2.lighting/3.2.materials_exercise1/materials_exercise1.cpp +++ b/src/2.lighting/3.2.materials_exercise1/materials_exercise1.cpp @@ -16,13 +16,18 @@ void mouse_callback(GLFWwindow* window, double xpos, double ypos); void scroll_callback(GLFWwindow* window, double xoffset, double yoffset); void processInput(GLFWwindow *window); +// settings +const unsigned int SCR_WIDTH = 800; +const unsigned int SCR_HEIGHT = 600; + // camera Camera camera(glm::vec3(0.0f, 0.0f, 3.0f)); -float lastX = 800.0f / 2.0; -float lastY = 600.0 / 2.0; +float lastX = SCR_WIDTH / 2.0f; +float lastY = SCR_HEIGHT / 2.0f; bool firstMouse = true; -float deltaTime = 0.0f; // time between current frame and last frame +// timing +float deltaTime = 0.0f; float lastFrame = 0.0f; // lighting @@ -40,14 +45,14 @@ int main() // glfw window creation // -------------------- - GLFWwindow* window = glfwCreateWindow(800, 600, "LearnOpenGL", NULL, NULL); - glfwMakeContextCurrent(window); + GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", NULL, NULL); if (window == NULL) { std::cout << "Failed to create GLFW window" << std::endl; glfwTerminate(); return -1; } + glfwMakeContextCurrent(window); glfwSetFramebufferSizeCallback(window, framebuffer_size_callback); glfwSetCursorPosCallback(window, mouse_callback); glfwSetScrollCallback(window, scroll_callback); @@ -70,22 +75,22 @@ int main() // build and compile our shader zprogram // ------------------------------------ Shader lightingShader("3.1.materials.vs", "3.1.materials.fs"); - Shader lampShader("1.lamp.vs", "1.lamp.fs"); + Shader lampShader("3.1.lamp.vs", "3.1.lamp.fs"); // set up vertex data (and buffer(s)) and configure vertex attributes // ------------------------------------------------------------------ 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.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.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, @@ -96,24 +101,24 @@ int main() -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, 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, 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, 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, 0.0f, + 0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.0f, -0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f, - 0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f, - 0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f, - 0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f, + 0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f, + 0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f, + 0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f, -0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f, -0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f, -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, - 0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, - 0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, - 0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, + 0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, + 0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, + 0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, -0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f }; @@ -162,7 +167,7 @@ int main() // render // ------ - glClearColor(0.2f, 0.3f, 0.3f, 1.0f); + glClearColor(0.1f, 0.1f, 0.1f, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // be sure to activate shader when setting uniforms/drawing objects @@ -182,7 +187,7 @@ int main() lightingShader.setFloat("material.shininess", 32.0f); // view/projection transformations - glm::mat4 projection = glm::perspective(glm::radians(camera.Zoom), 800.0f / 600.0f, 0.1f, 100.0f); + glm::mat4 projection = glm::perspective(glm::radians(camera.Zoom), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f); glm::mat4 view = camera.GetViewMatrix(); lightingShader.setMat4("projection", projection); lightingShader.setMat4("view", view); @@ -234,7 +239,6 @@ void processInput(GLFWwindow *window) if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) glfwSetWindowShouldClose(window, true); - float cameraSpeed = 2.5 * deltaTime; if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) camera.ProcessKeyboard(FORWARD, deltaTime); if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) diff --git a/src/2.lighting/4.1.lighting_maps_diffuse_map/lighting_maps_diffuse.cpp b/src/2.lighting/4.1.lighting_maps_diffuse_map/lighting_maps_diffuse.cpp index a5876bf..2b6c076 100644 --- a/src/2.lighting/4.1.lighting_maps_diffuse_map/lighting_maps_diffuse.cpp +++ b/src/2.lighting/4.1.lighting_maps_diffuse_map/lighting_maps_diffuse.cpp @@ -18,13 +18,18 @@ void scroll_callback(GLFWwindow* window, double xoffset, double yoffset); void processInput(GLFWwindow *window); unsigned int loadTexture(const char *path); +// settings +const unsigned int SCR_WIDTH = 800; +const unsigned int SCR_HEIGHT = 600; + // camera Camera camera(glm::vec3(0.0f, 0.0f, 3.0f)); -float lastX = 800.0f / 2.0; -float lastY = 600.0 / 2.0; +float lastX = SCR_WIDTH / 2.0f; +float lastY = SCR_HEIGHT / 2.0f; bool firstMouse = true; -float deltaTime = 0.0f; // time between current frame and last frame +// timing +float deltaTime = 0.0f; float lastFrame = 0.0f; // lighting @@ -42,14 +47,14 @@ int main() // glfw window creation // -------------------- - GLFWwindow* window = glfwCreateWindow(800, 600, "LearnOpenGL", NULL, NULL); - glfwMakeContextCurrent(window); + GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", NULL, NULL); if (window == NULL) { std::cout << "Failed to create GLFW window" << std::endl; glfwTerminate(); return -1; } + glfwMakeContextCurrent(window); glfwSetFramebufferSizeCallback(window, framebuffer_size_callback); glfwSetCursorPosCallback(window, mouse_callback); glfwSetScrollCallback(window, scroll_callback); @@ -72,7 +77,7 @@ int main() // build and compile our shader zprogram // ------------------------------------ Shader lightingShader("4.1.lighting_maps.vs", "4.1.lighting_maps.fs"); - Shader lampShader("1.lamp.vs", "1.lamp.fs"); + Shader lampShader("4.1.lamp.vs", "4.1.lamp.fs"); // set up vertex data (and buffer(s)) and configure vertex attributes // ------------------------------------------------------------------ @@ -172,7 +177,7 @@ int main() // render // ------ - glClearColor(0.2f, 0.3f, 0.3f, 1.0f); + glClearColor(0.1f, 0.1f, 0.1f, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // be sure to activate shader when setting uniforms/drawing objects @@ -190,7 +195,7 @@ int main() lightingShader.setFloat("material.shininess", 64.0f); // view/projection transformations - glm::mat4 projection = glm::perspective(glm::radians(camera.Zoom), 800.0f / 600.0f, 0.1f, 100.0f); + glm::mat4 projection = glm::perspective(glm::radians(camera.Zoom), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f); glm::mat4 view = camera.GetViewMatrix(); lightingShader.setMat4("projection", projection); lightingShader.setMat4("view", view); @@ -246,7 +251,6 @@ void processInput(GLFWwindow *window) if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) glfwSetWindowShouldClose(window, true); - float cameraSpeed = 2.5 * deltaTime; if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) camera.ProcessKeyboard(FORWARD, deltaTime); if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) diff --git a/src/2.lighting/4.2.lighting_maps_specular_map/lighting_maps_specular.cpp b/src/2.lighting/4.2.lighting_maps_specular_map/lighting_maps_specular.cpp index 245dbb1..96f1d5a 100644 --- a/src/2.lighting/4.2.lighting_maps_specular_map/lighting_maps_specular.cpp +++ b/src/2.lighting/4.2.lighting_maps_specular_map/lighting_maps_specular.cpp @@ -18,13 +18,18 @@ void scroll_callback(GLFWwindow* window, double xoffset, double yoffset); void processInput(GLFWwindow *window); unsigned int loadTexture(const char *path); +// settings +const unsigned int SCR_WIDTH = 800; +const unsigned int SCR_HEIGHT = 600; + // camera Camera camera(glm::vec3(0.0f, 0.0f, 3.0f)); -float lastX = 800.0f / 2.0; -float lastY = 600.0 / 2.0; +float lastX = SCR_WIDTH / 2.0f; +float lastY = SCR_HEIGHT / 2.0f; bool firstMouse = true; -float deltaTime = 0.0f; // time between current frame and last frame +// timing +float deltaTime = 0.0f; float lastFrame = 0.0f; // lighting @@ -42,14 +47,14 @@ int main() // glfw window creation // -------------------- - GLFWwindow* window = glfwCreateWindow(800, 600, "LearnOpenGL", NULL, NULL); - glfwMakeContextCurrent(window); + GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", NULL, NULL); if (window == NULL) { std::cout << "Failed to create GLFW window" << std::endl; glfwTerminate(); return -1; } + glfwMakeContextCurrent(window); glfwSetFramebufferSizeCallback(window, framebuffer_size_callback); glfwSetCursorPosCallback(window, mouse_callback); glfwSetScrollCallback(window, scroll_callback); @@ -72,7 +77,7 @@ int main() // build and compile our shader zprogram // ------------------------------------ Shader lightingShader("4.2.lighting_maps.vs", "4.2.lighting_maps.fs"); - Shader lampShader("1.lamp.vs", "1.lamp.fs"); + Shader lampShader("4.2.lamp.vs", "4.2.lamp.fs"); // set up vertex data (and buffer(s)) and configure vertex attributes // ------------------------------------------------------------------ @@ -174,7 +179,7 @@ int main() // render // ------ - glClearColor(0.2f, 0.3f, 0.3f, 1.0f); + glClearColor(0.1f, 0.1f, 0.1f, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // be sure to activate shader when setting uniforms/drawing objects @@ -191,7 +196,7 @@ int main() lightingShader.setFloat("material.shininess", 64.0f); // view/projection transformations - glm::mat4 projection = glm::perspective(glm::radians(camera.Zoom), 800.0f / 600.0f, 0.1f, 100.0f); + glm::mat4 projection = glm::perspective(glm::radians(camera.Zoom), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f); glm::mat4 view = camera.GetViewMatrix(); lightingShader.setMat4("projection", projection); lightingShader.setMat4("view", view); @@ -250,7 +255,6 @@ void processInput(GLFWwindow *window) if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) glfwSetWindowShouldClose(window, true); - float cameraSpeed = 2.5 * deltaTime; if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) camera.ProcessKeyboard(FORWARD, deltaTime); if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) @@ -270,7 +274,6 @@ void framebuffer_size_callback(GLFWwindow* window, int width, int height) glViewport(0, 0, width, height); } - // glfw: whenever the mouse moves, this callback is called // ------------------------------------------------------- void mouse_callback(GLFWwindow* window, double xpos, double ypos) diff --git a/src/2.lighting/4.3.lighting_maps_exercise4/lighting_maps_exercise4.cpp b/src/2.lighting/4.3.lighting_maps_exercise4/lighting_maps_exercise4.cpp index f8adb59..b55ee8c 100644 --- a/src/2.lighting/4.3.lighting_maps_exercise4/lighting_maps_exercise4.cpp +++ b/src/2.lighting/4.3.lighting_maps_exercise4/lighting_maps_exercise4.cpp @@ -18,13 +18,18 @@ void scroll_callback(GLFWwindow* window, double xoffset, double yoffset); void processInput(GLFWwindow *window); unsigned int loadTexture(const char *path); +// settings +const unsigned int SCR_WIDTH = 800; +const unsigned int SCR_HEIGHT = 600; + // camera Camera camera(glm::vec3(0.0f, 0.0f, 3.0f)); -float lastX = 800.0f / 2.0; -float lastY = 600.0 / 2.0; +float lastX = SCR_WIDTH / 2.0f; +float lastY = SCR_HEIGHT / 2.0f; bool firstMouse = true; -float deltaTime = 0.0f; // time between current frame and last frame +// timing +float deltaTime = 0.0f; float lastFrame = 0.0f; // lighting @@ -42,14 +47,14 @@ int main() // glfw window creation // -------------------- - GLFWwindow* window = glfwCreateWindow(800, 600, "LearnOpenGL", NULL, NULL); - glfwMakeContextCurrent(window); + GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", NULL, NULL); if (window == NULL) { std::cout << "Failed to create GLFW window" << std::endl; glfwTerminate(); return -1; } + glfwMakeContextCurrent(window); glfwSetFramebufferSizeCallback(window, framebuffer_size_callback); glfwSetCursorPosCallback(window, mouse_callback); glfwSetScrollCallback(window, scroll_callback); @@ -72,7 +77,7 @@ int main() // build and compile our shader zprogram // ------------------------------------ Shader lightingShader("4.3.lighting_maps.vs", "4.3.lighting_maps.fs"); - Shader lampShader("1.lamp.vs", "1.lamp.fs"); + Shader lampShader("4.3.lamp.vs", "4.3.lamp.fs"); // set up vertex data (and buffer(s)) and configure vertex attributes // ------------------------------------------------------------------ @@ -176,7 +181,7 @@ int main() // render // ------ - glClearColor(0.2f, 0.3f, 0.3f, 1.0f); + glClearColor(0.1f, 0.1f, 0.1f, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // be sure to activate shader when setting uniforms/drawing objects @@ -193,7 +198,7 @@ int main() lightingShader.setFloat("material.shininess", 64.0f); // view/projection transformations - glm::mat4 projection = glm::perspective(glm::radians(camera.Zoom), 800.0f / 600.0f, 0.1f, 100.0f); + glm::mat4 projection = glm::perspective(glm::radians(camera.Zoom), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f); glm::mat4 view = camera.GetViewMatrix(); lightingShader.setMat4("projection", projection); lightingShader.setMat4("view", view); @@ -255,7 +260,6 @@ void processInput(GLFWwindow *window) if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) glfwSetWindowShouldClose(window, true); - float cameraSpeed = 2.5 * deltaTime; if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) camera.ProcessKeyboard(FORWARD, deltaTime); if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) diff --git a/src/2.lighting/5.1.light_casters_directional/light_casters_directional.cpp b/src/2.lighting/5.1.light_casters_directional/light_casters_directional.cpp index 2fc739b..f465042 100644 --- a/src/2.lighting/5.1.light_casters_directional/light_casters_directional.cpp +++ b/src/2.lighting/5.1.light_casters_directional/light_casters_directional.cpp @@ -18,13 +18,18 @@ void scroll_callback(GLFWwindow* window, double xoffset, double yoffset); void processInput(GLFWwindow *window); unsigned int loadTexture(const char *path); +// settings +const unsigned int SCR_WIDTH = 800; +const unsigned int SCR_HEIGHT = 600; + // camera Camera camera(glm::vec3(0.0f, 0.0f, 3.0f)); -float lastX = 800.0f / 2.0; -float lastY = 600.0 / 2.0; +float lastX = SCR_WIDTH / 2.0f; +float lastY = SCR_HEIGHT / 2.0f; bool firstMouse = true; -float deltaTime = 0.0f; // time between current frame and last frame +// timing +float deltaTime = 0.0f; float lastFrame = 0.0f; int main() @@ -38,14 +43,14 @@ int main() // glfw window creation // -------------------- - GLFWwindow* window = glfwCreateWindow(800, 600, "LearnOpenGL", NULL, NULL); - glfwMakeContextCurrent(window); + GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", NULL, NULL); if (window == NULL) { std::cout << "Failed to create GLFW window" << std::endl; glfwTerminate(); return -1; } + glfwMakeContextCurrent(window); glfwSetFramebufferSizeCallback(window, framebuffer_size_callback); glfwSetCursorPosCallback(window, mouse_callback); glfwSetScrollCallback(window, scroll_callback); @@ -68,7 +73,7 @@ int main() // build and compile shaders // ------------------------- Shader lightingShader("5.1.light_casters.vs", "5.1.light_casters.fs"); - Shader lampShader("1.lamp.vs", "1.lamp.fs"); + Shader lampShader("5.1.lamp.vs", "5.1.lamp.fs"); // set up vertex data (and buffer(s)) and configure vertex attributes // ------------------------------------------------------------------ @@ -183,7 +188,7 @@ int main() // render // ------ - glClearColor(0.2f, 0.3f, 0.3f, 1.0f); + glClearColor(0.1f, 0.1f, 0.1f, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // be sure to activate shader when setting uniforms/drawing objects @@ -200,7 +205,7 @@ int main() lightingShader.setFloat("material.shininess", 32.0f); // view/projection transformations - glm::mat4 projection = glm::perspective(glm::radians(camera.Zoom), 800.0f / 600.0f, 0.1f, 100.0f); + glm::mat4 projection = glm::perspective(glm::radians(camera.Zoom), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f); glm::mat4 view = camera.GetViewMatrix(); lightingShader.setMat4("projection", projection); lightingShader.setMat4("view", view); @@ -273,7 +278,6 @@ void processInput(GLFWwindow *window) if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) glfwSetWindowShouldClose(window, true); - float cameraSpeed = 2.5 * deltaTime; if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) camera.ProcessKeyboard(FORWARD, deltaTime); if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) diff --git a/src/2.lighting/5.2.light_casters_point/light_casters_point.cpp b/src/2.lighting/5.2.light_casters_point/light_casters_point.cpp index 7ffc167..b5b1e9b 100644 --- a/src/2.lighting/5.2.light_casters_point/light_casters_point.cpp +++ b/src/2.lighting/5.2.light_casters_point/light_casters_point.cpp @@ -18,13 +18,18 @@ void scroll_callback(GLFWwindow* window, double xoffset, double yoffset); void processInput(GLFWwindow *window); unsigned int loadTexture(const char *path); +// settings +const unsigned int SCR_WIDTH = 800; +const unsigned int SCR_HEIGHT = 600; + // camera Camera camera(glm::vec3(0.0f, 0.0f, 3.0f)); -float lastX = 800.0f / 2.0; -float lastY = 600.0 / 2.0; +float lastX = SCR_WIDTH / 2.0f; +float lastY = SCR_HEIGHT / 2.0f; bool firstMouse = true; -float deltaTime = 0.0f; // time between current frame and last frame +// timing +float deltaTime = 0.0f; float lastFrame = 0.0f; // lighting @@ -41,14 +46,14 @@ int main() // glfw window creation // -------------------- - GLFWwindow* window = glfwCreateWindow(800, 600, "LearnOpenGL", NULL, NULL); - glfwMakeContextCurrent(window); + GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", NULL, NULL); if (window == NULL) { std::cout << "Failed to create GLFW window" << std::endl; glfwTerminate(); return -1; } + glfwMakeContextCurrent(window); glfwSetFramebufferSizeCallback(window, framebuffer_size_callback); glfwSetCursorPosCallback(window, mouse_callback); glfwSetScrollCallback(window, scroll_callback); @@ -71,7 +76,7 @@ int main() // build and compile our shader zprogram // ------------------------------------ Shader lightingShader("5.2.light_casters.vs", "5.2.light_casters.fs"); - Shader lampShader("1.lamp.vs", "1.lamp.fs"); + Shader lampShader("5.2.lamp.vs", "5.2.lamp.fs"); // set up vertex data (and buffer(s)) and configure vertex attributes // ------------------------------------------------------------------ @@ -186,7 +191,7 @@ int main() // render // ------ - glClearColor(0.2f, 0.3f, 0.3f, 1.0f); + glClearColor(0.1f, 0.1f, 0.1f, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // be sure to activate shader when setting uniforms/drawing objects @@ -206,7 +211,7 @@ int main() lightingShader.setFloat("material.shininess", 32.0f); // view/projection transformations - glm::mat4 projection = glm::perspective(glm::radians(camera.Zoom), 800.0f / 600.0f, 0.1f, 100.0f); + glm::mat4 projection = glm::perspective(glm::radians(camera.Zoom), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f); glm::mat4 view = camera.GetViewMatrix(); lightingShader.setMat4("projection", projection); lightingShader.setMat4("view", view); @@ -275,7 +280,6 @@ void processInput(GLFWwindow *window) if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) glfwSetWindowShouldClose(window, true); - float cameraSpeed = 2.5 * deltaTime; if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) camera.ProcessKeyboard(FORWARD, deltaTime); if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) @@ -295,7 +299,6 @@ void framebuffer_size_callback(GLFWwindow* window, int width, int height) glViewport(0, 0, width, height); } - // glfw: whenever the mouse moves, this callback is called // ------------------------------------------------------- void mouse_callback(GLFWwindow* window, double xpos, double ypos) diff --git a/src/2.lighting/5.3.light_casters_spot/light_casters_spot.cpp b/src/2.lighting/5.3.light_casters_spot/light_casters_spot.cpp index 922b4df..fa6d76c 100644 --- a/src/2.lighting/5.3.light_casters_spot/light_casters_spot.cpp +++ b/src/2.lighting/5.3.light_casters_spot/light_casters_spot.cpp @@ -18,13 +18,18 @@ void scroll_callback(GLFWwindow* window, double xoffset, double yoffset); void processInput(GLFWwindow *window); unsigned int loadTexture(const char *path); +// settings +const unsigned int SCR_WIDTH = 800; +const unsigned int SCR_HEIGHT = 600; + // camera Camera camera(glm::vec3(0.0f, 0.0f, 3.0f)); -float lastX = 800.0f / 2.0; -float lastY = 600.0 / 2.0; +float lastX = SCR_WIDTH / 2.0f; +float lastY = SCR_HEIGHT / 2.0f; bool firstMouse = true; -float deltaTime = 0.0f; // time between current frame and last frame +// timing +float deltaTime = 0.0f; float lastFrame = 0.0f; int main() @@ -38,14 +43,14 @@ int main() // glfw window creation // -------------------- - GLFWwindow* window = glfwCreateWindow(800, 600, "LearnOpenGL", NULL, NULL); - glfwMakeContextCurrent(window); + GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", NULL, NULL); if (window == NULL) { std::cout << "Failed to create GLFW window" << std::endl; glfwTerminate(); return -1; } + glfwMakeContextCurrent(window); glfwSetFramebufferSizeCallback(window, framebuffer_size_callback); glfwSetCursorPosCallback(window, mouse_callback); glfwSetScrollCallback(window, scroll_callback); @@ -68,7 +73,7 @@ int main() // build and compile our shader zprogram // ------------------------------------ Shader lightingShader("5.3.light_casters.vs", "5.3.light_casters.fs"); - Shader lampShader("1.lamp.vs", "1.lamp.fs"); + Shader lampShader("5.3.lamp.vs", "5.3.lamp.fs"); // set up vertex data (and buffer(s)) and configure vertex attributes // ------------------------------------------------------------------ @@ -183,7 +188,7 @@ int main() // render // ------ - glClearColor(0.2f, 0.3f, 0.3f, 1.0f); + glClearColor(0.1f, 0.1f, 0.1f, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // be sure to activate shader when setting uniforms/drawing objects @@ -207,7 +212,7 @@ int main() lightingShader.setFloat("material.shininess", 32.0f); // view/projection transformations - glm::mat4 projection = glm::perspective(glm::radians(camera.Zoom), 800.0f / 600.0f, 0.1f, 100.0f); + glm::mat4 projection = glm::perspective(glm::radians(camera.Zoom), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f); glm::mat4 view = camera.GetViewMatrix(); lightingShader.setMat4("projection", projection); lightingShader.setMat4("view", view); @@ -276,7 +281,6 @@ void processInput(GLFWwindow *window) if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) glfwSetWindowShouldClose(window, true); - float cameraSpeed = 2.5 * deltaTime; if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) camera.ProcessKeyboard(FORWARD, deltaTime); if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) @@ -296,7 +300,6 @@ void framebuffer_size_callback(GLFWwindow* window, int width, int height) glViewport(0, 0, width, height); } - // glfw: whenever the mouse moves, this callback is called // ------------------------------------------------------- void mouse_callback(GLFWwindow* window, double xpos, double ypos) diff --git a/src/2.lighting/5.4.light_casters_spot_soft/light_casters_spot_soft.cpp b/src/2.lighting/5.4.light_casters_spot_soft/light_casters_spot_soft.cpp index a52de33..ced5392 100644 --- a/src/2.lighting/5.4.light_casters_spot_soft/light_casters_spot_soft.cpp +++ b/src/2.lighting/5.4.light_casters_spot_soft/light_casters_spot_soft.cpp @@ -18,13 +18,18 @@ void scroll_callback(GLFWwindow* window, double xoffset, double yoffset); void processInput(GLFWwindow *window); unsigned int loadTexture(const char *path); +// settings +const unsigned int SCR_WIDTH = 800; +const unsigned int SCR_HEIGHT = 600; + // camera Camera camera(glm::vec3(0.0f, 0.0f, 3.0f)); -float lastX = 800.0f / 2.0; -float lastY = 600.0 / 2.0; +float lastX = SCR_WIDTH / 2.0f; +float lastY = SCR_HEIGHT / 2.0f; bool firstMouse = true; -float deltaTime = 0.0f; // time between current frame and last frame +// timing +float deltaTime = 0.0f; float lastFrame = 0.0f; int main() @@ -38,14 +43,14 @@ int main() // glfw window creation // -------------------- - GLFWwindow* window = glfwCreateWindow(800, 600, "LearnOpenGL", NULL, NULL); - glfwMakeContextCurrent(window); + GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", NULL, NULL); if (window == NULL) { std::cout << "Failed to create GLFW window" << std::endl; glfwTerminate(); return -1; } + glfwMakeContextCurrent(window); glfwSetFramebufferSizeCallback(window, framebuffer_size_callback); glfwSetCursorPosCallback(window, mouse_callback); glfwSetScrollCallback(window, scroll_callback); @@ -68,7 +73,7 @@ int main() // build and compile our shader zprogram // ------------------------------------ Shader lightingShader("5.4.light_casters.vs", "5.4.light_casters.fs"); - Shader lampShader("1.lamp.vs", "1.lamp.fs"); + Shader lampShader("5.4.lamp.vs", "5.4.lamp.fs"); // set up vertex data (and buffer(s)) and configure vertex attributes // ------------------------------------------------------------------ @@ -183,7 +188,7 @@ int main() // render // ------ - glClearColor(0.2f, 0.3f, 0.3f, 1.0f); + glClearColor(0.1f, 0.1f, 0.1f, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // be sure to activate shader when setting uniforms/drawing objects @@ -208,7 +213,7 @@ int main() lightingShader.setFloat("material.shininess", 32.0f); // view/projection transformations - glm::mat4 projection = glm::perspective(glm::radians(camera.Zoom), 800.0f / 600.0f, 0.1f, 100.0f); + glm::mat4 projection = glm::perspective(glm::radians(camera.Zoom), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f); glm::mat4 view = camera.GetViewMatrix(); lightingShader.setMat4("projection", projection); lightingShader.setMat4("view", view); @@ -238,7 +243,6 @@ int main() glDrawArrays(GL_TRIANGLES, 0, 36); } - // again, a lamp object is weird when we only have a spot light, don't render the light object // lampShader.use(); // lampShader.setMat4("projection", projection); @@ -277,7 +281,6 @@ void processInput(GLFWwindow *window) if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) glfwSetWindowShouldClose(window, true); - float cameraSpeed = 2.5 * deltaTime; if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) camera.ProcessKeyboard(FORWARD, deltaTime); if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) @@ -297,7 +300,6 @@ void framebuffer_size_callback(GLFWwindow* window, int width, int height) glViewport(0, 0, width, height); } - // glfw: whenever the mouse moves, this callback is called // ------------------------------------------------------- void mouse_callback(GLFWwindow* window, double xpos, double ypos) diff --git a/src/2.lighting/6.multiple_lights/6.multiple_lights.cpp b/src/2.lighting/6.multiple_lights/6.multiple_lights.cpp index e898607..22f5abe 100644 --- a/src/2.lighting/6.multiple_lights/6.multiple_lights.cpp +++ b/src/2.lighting/6.multiple_lights/6.multiple_lights.cpp @@ -18,13 +18,18 @@ void scroll_callback(GLFWwindow* window, double xoffset, double yoffset); void processInput(GLFWwindow *window); unsigned int loadTexture(const char *path); +// settings +const unsigned int SCR_WIDTH = 800; +const unsigned int SCR_HEIGHT = 600; + // camera Camera camera(glm::vec3(0.0f, 0.0f, 3.0f)); -float lastX = 800.0f / 2.0; -float lastY = 600.0 / 2.0; +float lastX = SCR_WIDTH / 2.0f; +float lastY = SCR_HEIGHT / 2.0f; bool firstMouse = true; -float deltaTime = 0.0f; // time between current frame and last frame +// timing +float deltaTime = 0.0f; float lastFrame = 0.0f; // lighting @@ -41,14 +46,14 @@ int main() // glfw window creation // -------------------- - GLFWwindow* window = glfwCreateWindow(800, 600, "LearnOpenGL", NULL, NULL); - glfwMakeContextCurrent(window); + GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", NULL, NULL); if (window == NULL) { std::cout << "Failed to create GLFW window" << std::endl; glfwTerminate(); return -1; } + glfwMakeContextCurrent(window); glfwSetFramebufferSizeCallback(window, framebuffer_size_callback); glfwSetCursorPosCallback(window, mouse_callback); glfwSetScrollCallback(window, scroll_callback); @@ -71,7 +76,7 @@ int main() // build and compile our shader zprogram // ------------------------------------ Shader lightingShader("6.multiple_lights.vs", "6.multiple_lights.fs"); - Shader lampShader("1.lamp.vs", "1.lamp.fs"); + Shader lampShader("6.lamp.vs", "6.lamp.fs"); // set up vertex data (and buffer(s)) and configure vertex attributes // ------------------------------------------------------------------ @@ -193,7 +198,7 @@ int main() // render // ------ - glClearColor(0.2f, 0.3f, 0.3f, 1.0f); + glClearColor(0.1f, 0.1f, 0.1f, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // be sure to activate shader when setting uniforms/drawing objects @@ -257,7 +262,7 @@ int main() lightingShader.setFloat("spotLight.outerCutOff", glm::cos(glm::radians(15.0f))); // view/projection transformations - glm::mat4 projection = glm::perspective(glm::radians(camera.Zoom), 800.0f / 600.0f, 0.1f, 100.0f); + glm::mat4 projection = glm::perspective(glm::radians(camera.Zoom), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f); glm::mat4 view = camera.GetViewMatrix(); lightingShader.setMat4("projection", projection); lightingShader.setMat4("view", view); @@ -287,7 +292,6 @@ int main() glDrawArrays(GL_TRIANGLES, 0, 36); } - // also draw the lamp object(s) lampShader.use(); lampShader.setMat4("projection", projection); @@ -330,7 +334,6 @@ void processInput(GLFWwindow *window) if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) glfwSetWindowShouldClose(window, true); - float cameraSpeed = 2.5 * deltaTime; if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) camera.ProcessKeyboard(FORWARD, deltaTime); if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) @@ -350,7 +353,6 @@ void framebuffer_size_callback(GLFWwindow* window, int width, int height) glViewport(0, 0, width, height); } - // glfw: whenever the mouse moves, this callback is called // ------------------------------------------------------- void mouse_callback(GLFWwindow* window, double xpos, double ypos)