learn-opengl/19-blending/src/scene.cpp
2026-08-29 13:13:41 -04:00

68 lines
1.7 KiB
C++

#include "scene.hpp"
#include "dbc.hpp"
#include <math.h>
void Scene::update() {
float currentFrame = glfwGetTime();
deltaTime = currentFrame - lastFrame;
lastFrame = currentFrame;
camera.update(deltaTime);
}
void Scene::draw_model(Shader& with_shader, Model& scene_model, Material& material, glm::vec3& position, float scale)
{
with_shader.apply_material(material);
glm::mat4 model = glm::mat4(1.0f);
model = glm::translate(model, position);
model = glm::scale(model, glm::vec3(scale, scale, scale));
with_shader.setMat4("model", model);
scene_model.draw(with_shader);
}
void Scene::render(GLFWwindow* window) {
glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glm::mat4 view = camera.look_at();
glm::mat4 projection = glm::mat4(1.0f);
// fov, aspect, near plane, far plane
projection = glm::perspective(glm::radians(camera.fov), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f);
shader.use();
shader.setMat4("view", view);
shader.setMat4("projection", projection);
// 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;
}
for(auto& thing : things) {
draw_model(shader, thing.model, thing.material, thing.position);
}
}
void Scene::cleanup() {
shader.cleanup();
}
void Scene::spawn(const std::string& name, components::Position& position) {
if(models.contains(name)) {
things.emplace_back(
name,
models.at(name),
position,
materials.at("default"));
} else {
dbc::log($F("No model named: {}", name));
}
}