learn-opengl/14-refactor/src/scene.cpp

71 lines
1.7 KiB
C++

#include "scene.hpp"
#include "dbc.hpp"
#include <math.h>
void Scene::updateDelta() {
float currentFrame = glfwGetTime();
deltaTime = currentFrame - lastFrame;
lastFrame = currentFrame;
}
void Scene::configure() {
// REFACTOR: really this should be done somewhere else
for(auto& model : models) {
model.connect_shader(shader);
}
}
void Scene::draw_model(Model& scene_model, glm::mat4& projection, glm::mat4& view, glm::vec3& position)
{
glm::vec3 objectColor = {1.0, 0.94, 0.78};
shader.apply_material(cubeMaterial);
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();
camera.update(deltaTime);
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.pos);
light.spot.position = camera.pos;
light.spot.direction = camera.front;
shader.apply_lighting(light);
// BUG: no connection between models and positions
for(auto& position : positions) {
draw_model(models[0], projection, view, position);
}
glfwSwapBuffers(window);
glfwPollEvents();
}
void Scene::cleanup() {
shader.cleanup();
}