The lighting is done better where I set it up once in the shader and then only change the camera spotlight when the camera moves.

This commit is contained in:
Zed A. Shaw 2026-08-27 15:00:48 -04:00
parent 748528a31f
commit 032f10ef44
5 changed files with 22 additions and 3 deletions

View file

@ -7,18 +7,22 @@ glm::mat4 Camera::look_at() {
} }
void Camera::forward() { void Camera::forward() {
dirty = true;
position += speed * front; position += speed * front;
} }
void Camera::back() { void Camera::back() {
dirty = true;
position -= speed * front; position -= speed * front;
} }
void Camera::left() { void Camera::left() {
dirty = true;
position -= glm::normalize(glm::cross(front, up)) * speed; position -= glm::normalize(glm::cross(front, up)) * speed;
} }
void Camera::right() { void Camera::right() {
dirty = true;
position += glm::normalize(glm::cross(front, up)) * speed; 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) { void Camera::mouse_move(double xpos, double ypos) {
dirty = true;
if(firstMouse) { if(firstMouse) {
lastX = xpos; lastX = xpos;
lastY = ypos; lastY = ypos;
@ -56,6 +62,7 @@ void Camera::mouse_move(double xpos, double ypos) {
} }
void Camera::mouse_scroll(double xoffset, double yoffset) { void Camera::mouse_scroll(double xoffset, double yoffset) {
dirty = true;
fov -= (float)yoffset; fov -= (float)yoffset;
if(fov < 1.0f) fov = 1.0f; if(fov < 1.0f) fov = 1.0f;

View file

@ -15,6 +15,7 @@ struct Camera {
float lastY = 300; float lastY = 300;
float fov = 45.0f; float fov = 45.0f;
bool firstMouse = true; bool firstMouse = true;
bool dirty = true;
glm::mat4 look_at(); glm::mat4 look_at();
void forward(); void forward();

View file

@ -44,9 +44,14 @@ void Scene::render(GLFWwindow* window) {
shader.setMat4("projection", projection); shader.setMat4("projection", projection);
shader.setVec3("viewPos", camera.position); shader.setVec3("viewPos", camera.position);
light.camera.position = camera.position; // for now just detect the camera moved and do spotlight update
light.camera.direction = camera.front; if(camera.dirty) {
shader.apply_lighting(light); 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 // BUG: no connection between models and positions
for(auto& thing : things) { for(auto& thing : things) {

View file

@ -54,6 +54,9 @@ struct Scene {
thing.position, thing.position,
materials.at(thing.material)); materials.at(thing.material));
} }
shader.use();
shader.apply_lighting(light);
} }
void update(); void update();

View file

@ -154,9 +154,11 @@ void Shader::apply_lighting(const Lighting& lighting) {
} }
} }
void Shader::apply_dir_light(const Light& light, size_t index) { void Shader::apply_dir_light(const Light& light, size_t index) {
dbc::check(index < MAX_LIGHTS, "too many directional lights"); 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[{}].direction", index), light.direction);
setVec3(std::format("dirLights[{}].ambient", index), light.ambient); setVec3(std::format("dirLights[{}].ambient", index), light.ambient);
setVec3(std::format("dirLights[{}].diffuse", index), light.diffuse); 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) { void Shader::apply_spot_light(const Light& light, size_t index) {
dbc::check(index < MAX_LIGHTS, "too many spot lights"); 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[{}].position", index), light.position);
setVec3(std::format("spotLights[{}].direction", index), light.direction); setVec3(std::format("spotLights[{}].direction", index), light.direction);
setVec3(std::format("spotLights[{}].ambient", index), light.ambient); setVec3(std::format("spotLights[{}].ambient", index), light.ambient);