learn-opengl/17-refactor-bug-fix/src/scene.cpp

71 lines
1.9 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(Model& scene_model, Material& material, glm::mat4& projection, glm::mat4& view, glm::vec3& position)
{
shader.apply_material(material);
float time = glfwGetTime();
glm::mat4 model = glm::mat4(1.0f);
model = glm::translate(model, position);
model = glm::rotate(model, glm::radians(time * 10.0f), glm::vec3(1.0f, 0.3f, 0.5f));
shader.setMat4("model", model);
scene_model.draw(shader);
}
void Scene::render(GLFWwindow* window) {
glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
shader.use();
// time is used for fake 3d rotation
float time = glfwGetTime();
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);
shader.setVec3("viewPos", camera.position);
// 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) {
draw_model(thing.model, thing.material, projection, view, thing.position);
}
}
void Scene::cleanup() {
shader.cleanup();
}
void Scene::spawn(const std::string& name, components::Position& position) {
things.emplace_back(
models.at(name),
position,
materials.at(name));
}