diff --git a/17-refactor-bug-fix/src/camera.cpp b/17-refactor-bug-fix/src/camera.cpp index 0d619b8..0d45d05 100644 --- a/17-refactor-bug-fix/src/camera.cpp +++ b/17-refactor-bug-fix/src/camera.cpp @@ -7,18 +7,22 @@ glm::mat4 Camera::look_at() { } void Camera::forward() { + dirty = true; position += speed * front; } void Camera::back() { + dirty = true; position -= speed * front; } void Camera::left() { + dirty = true; position -= glm::normalize(glm::cross(front, up)) * speed; } void Camera::right() { + dirty = true; position += glm::normalize(glm::cross(front, up)) * speed; } @@ -27,6 +31,8 @@ void Camera::update(float deltaTime) { } void Camera::mouse_move(double xpos, double ypos) { + dirty = true; + if(firstMouse) { lastX = xpos; lastY = ypos; @@ -56,6 +62,7 @@ void Camera::mouse_move(double xpos, double ypos) { } void Camera::mouse_scroll(double xoffset, double yoffset) { + dirty = true; fov -= (float)yoffset; if(fov < 1.0f) fov = 1.0f; diff --git a/17-refactor-bug-fix/src/camera.hpp b/17-refactor-bug-fix/src/camera.hpp index 2603e2d..e97eeef 100644 --- a/17-refactor-bug-fix/src/camera.hpp +++ b/17-refactor-bug-fix/src/camera.hpp @@ -15,6 +15,7 @@ struct Camera { float lastY = 300; float fov = 45.0f; bool firstMouse = true; + bool dirty = true; glm::mat4 look_at(); void forward(); diff --git a/17-refactor-bug-fix/src/scene.cpp b/17-refactor-bug-fix/src/scene.cpp index d39db47..d02f505 100644 --- a/17-refactor-bug-fix/src/scene.cpp +++ b/17-refactor-bug-fix/src/scene.cpp @@ -44,9 +44,14 @@ void Scene::render(GLFWwindow* window) { shader.setMat4("projection", projection); shader.setVec3("viewPos", camera.position); - light.camera.position = camera.position; - light.camera.direction = camera.front; - shader.apply_lighting(light); + // for now just detect the camera moved and do spotlight update + if(camera.dirty) { + light.camera.position = camera.position; + light.camera.direction = camera.front; + // just update the camera's light + shader.apply_spot_light(light.camera, 0); + camera.dirty = false; + } // BUG: no connection between models and positions for(auto& thing : things) { diff --git a/17-refactor-bug-fix/src/scene.hpp b/17-refactor-bug-fix/src/scene.hpp index 601c8c4..96f9443 100644 --- a/17-refactor-bug-fix/src/scene.hpp +++ b/17-refactor-bug-fix/src/scene.hpp @@ -54,6 +54,9 @@ struct Scene { thing.position, materials.at(thing.material)); } + + shader.use(); + shader.apply_lighting(light); } void update(); diff --git a/17-refactor-bug-fix/src/shader.cpp b/17-refactor-bug-fix/src/shader.cpp index 0937d7d..a0d86ef 100644 --- a/17-refactor-bug-fix/src/shader.cpp +++ b/17-refactor-bug-fix/src/shader.cpp @@ -154,9 +154,11 @@ void Shader::apply_lighting(const Lighting& lighting) { } } + void Shader::apply_dir_light(const Light& light, size_t index) { dbc::check(index < MAX_LIGHTS, "too many directional lights"); + // nasty, this needs to go setVec3(std::format("dirLights[{}].direction", index), light.direction); setVec3(std::format("dirLights[{}].ambient", index), light.ambient); setVec3(std::format("dirLights[{}].diffuse", index), light.diffuse); @@ -180,6 +182,7 @@ void Shader::apply_point_light(const Light& light, size_t index) { void Shader::apply_spot_light(const Light& light, size_t index) { dbc::check(index < MAX_LIGHTS, "too many spot lights"); + // find a way to not make strings all the time setVec3(std::format("spotLights[{}].position", index), light.position); setVec3(std::format("spotLights[{}].direction", index), light.direction); setVec3(std::format("spotLights[{}].ambient", index), light.ambient);