From a7beb5a92ac402b523f9f0b3ef716e5432a64dea Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Tue, 25 Aug 2026 14:11:14 -0400 Subject: [PATCH] Camera and shader now fixed up. --- 14-refactor/src/camera.cpp | 2 +- 14-refactor/src/camera.hpp | 2 +- 14-refactor/src/main.cpp | 6 ++--- 14-refactor/src/shader.cpp | 50 +++++++++++++++++++++++++++++++++++ 14-refactor/src/shader.hpp | 53 ++++---------------------------------- 5 files changed, 60 insertions(+), 53 deletions(-) diff --git a/14-refactor/src/camera.cpp b/14-refactor/src/camera.cpp index 1c456a7..b82557e 100644 --- a/14-refactor/src/camera.cpp +++ b/14-refactor/src/camera.cpp @@ -2,7 +2,7 @@ #include #include -glm::mat4 Camera::lookAt() { +glm::mat4 Camera::look_at() { return glm::lookAt(pos, pos + front, up); } diff --git a/14-refactor/src/camera.hpp b/14-refactor/src/camera.hpp index 91fe1f7..0da27be 100644 --- a/14-refactor/src/camera.hpp +++ b/14-refactor/src/camera.hpp @@ -16,7 +16,7 @@ struct Camera { float fov = 45.0f; bool firstMouse = true; - glm::mat4 lookAt(); + glm::mat4 look_at(); void forward(); void back(); void left(); diff --git a/14-refactor/src/main.cpp b/14-refactor/src/main.cpp index b8bae7a..15f09cb 100644 --- a/14-refactor/src/main.cpp +++ b/14-refactor/src/main.cpp @@ -224,7 +224,7 @@ Config setup() { void draw_models(Config& config, size_t model_i, size_t model_pos, glm::mat4& projection, glm::mat4& view) { glm::vec3 objectColor = {1.0, 0.94, 0.78}; - config.shader.applyMaterial(config.cubeMaterial); + config.shader.apply_material(config.cubeMaterial); float time = glfwGetTime(); glm::mat4 model = glm::mat4(1.0f); @@ -247,7 +247,7 @@ void render(GLFWwindow* window, Config& config) { config.camera.update(config.deltaTime); - glm::mat4 view = config.camera.lookAt(); + glm::mat4 view = config.camera.look_at(); glm::mat4 projection = glm::mat4(1.0f); // fov, aspect, near plane, far plane @@ -260,7 +260,7 @@ void render(GLFWwindow* window, Config& config) { config.light.spot.position = config.camera.pos; config.light.spot.direction = config.camera.front; - config.shader.applyLighting(config.light); + config.shader.apply_lighting(config.light); for(size_t i = 0; i < CUBE_COUNT; i++) { draw_models(config, 0, i, projection, view); diff --git a/14-refactor/src/shader.cpp b/14-refactor/src/shader.cpp index a89af73..c49d36e 100644 --- a/14-refactor/src/shader.cpp +++ b/14-refactor/src/shader.cpp @@ -124,3 +124,53 @@ void Shader::setMat4(const std::string &name, const glm::mat4& mat) const { unsigned int loc = glGetUniformLocation(ID, name.c_str()); glUniformMatrix4fv(loc, 1, GL_FALSE, glm::value_ptr(mat)); } + +void Shader::apply_material(const Material& material) { + setVec3("material.ambient", material.ambient); + setFloat("material.shininess", material.shininess); +} + +void Shader::apply_lighting(const Lighting& lighting) { + apply_dir_light(lighting.directional); + + setInt("pointLightCount", lighting.positioned.size()); + + for(size_t i = 0; i < lighting.positioned.size(); i++) { + apply_point_light(lighting.positioned[i], i); + } + + apply_spot_light(lighting.spot); +} + +void Shader::apply_dir_light(const Light& light) { + setVec3("dirLight.direction", light.direction); + setVec3("dirLight.ambient", light.ambient); + setVec3("dirLight.diffuse", light.diffuse); + setVec3("dirLight.specular", light.specular); +} + +void Shader::apply_point_light(const Light& light, size_t index) { + dbc::check(index < NR_POINT_LIGHTS, "too many positioned lights"); + + setVec3(std::format("pointLights[{}].position", index), light.position); + setVec3(std::format("pointLights[{}].ambient", index), light.ambient); + setVec3(std::format("pointLights[{}].diffuse", index), light.diffuse); + setVec3(std::format("pointLights[{}].specular", index), light.specular); + + setFloat(std::format("pointLights[{}].constant", index), light.constant); + setFloat(std::format("pointLights[{}].linear", index), light.linear); + setFloat(std::format("pointLights[{}].quadratic", index), light.quadratic); +} + +void Shader::apply_spot_light(const Light& light) { + setVec3("spotLight.position", light.position); + setVec3("spotLight.direction", light.direction); + setVec3("spotLight.ambient", light.ambient); + setVec3("spotLight.diffuse", light.diffuse); + setVec3("spotLight.specular", light.specular); + setFloat("spotLight.constant", light.constant); + setFloat("spotLight.linear", light.linear); + setFloat("spotLight.quadratic", light.quadratic); + setFloat("spotLight.cutOff", glm::cos(glm::radians(light.cutOff))); + setFloat("spotLight.outerCutOff", glm::cos(glm::radians(light.outerCutOff))); +} diff --git a/14-refactor/src/shader.hpp b/14-refactor/src/shader.hpp index 4238957..87c99ce 100644 --- a/14-refactor/src/shader.hpp +++ b/14-refactor/src/shader.hpp @@ -61,52 +61,9 @@ public: void setMat4(const std::string &name, const glm::mat4& what) const; void cleanup(); - void applyMaterial(const Material& material) { - setVec3("material.ambient", material.ambient); - setFloat("material.shininess", material.shininess); - } - - void applyLighting(const Lighting& lighting) { - applyDirLight(lighting.directional); - setInt("pointLightCount", lighting.positioned.size()); - - for(size_t i = 0; i < lighting.positioned.size(); i++) { - applyPointLight(lighting.positioned[i], i); - } - - applySpotLight(lighting.spot); - } - - void applyDirLight(const Light& light) { - setVec3("dirLight.direction", light.direction); - setVec3("dirLight.ambient", light.ambient); - setVec3("dirLight.diffuse", light.diffuse); - setVec3("dirLight.specular", light.specular); - } - - void applyPointLight(const Light& light, size_t index) { - dbc::check(index < NR_POINT_LIGHTS, "too many positioned lights"); - - setVec3(std::format("pointLights[{}].position", index), light.position); - setVec3(std::format("pointLights[{}].ambient", index), light.ambient); - setVec3(std::format("pointLights[{}].diffuse", index), light.diffuse); - setVec3(std::format("pointLights[{}].specular", index), light.specular); - - setFloat(std::format("pointLights[{}].constant", index), light.constant); - setFloat(std::format("pointLights[{}].linear", index), light.linear); - setFloat(std::format("pointLights[{}].quadratic", index), light.quadratic); - } - - void applySpotLight(const Light& light) { - setVec3("spotLight.position", light.position); - setVec3("spotLight.direction", light.direction); - setVec3("spotLight.ambient", light.ambient); - setVec3("spotLight.diffuse", light.diffuse); - setVec3("spotLight.specular", light.specular); - setFloat("spotLight.constant", light.constant); - setFloat("spotLight.linear", light.linear); - setFloat("spotLight.quadratic", light.quadratic); - setFloat("spotLight.cutOff", glm::cos(glm::radians(light.cutOff))); - setFloat("spotLight.outerCutOff", glm::cos(glm::radians(light.outerCutOff))); - } + void apply_material(const Material& material); + void apply_lighting(const Lighting& lighting); + void apply_dir_light(const Light& light); + void apply_point_light(const Light& light, size_t index); + void apply_spot_light(const Light& light); };