Starting day 19, blending.
This commit is contained in:
parent
dc973009fd
commit
128f522089
94 changed files with 15008 additions and 0 deletions
68
19-blending/src/scene.cpp
Normal file
68
19-blending/src/scene.cpp
Normal file
|
|
@ -0,0 +1,68 @@
|
|||
#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));
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue