feat: add lights

This commit is contained in:
2025-10-14 11:45:46 +02:00
parent c34d3f803f
commit 50a4f56e75
14 changed files with 355 additions and 10 deletions

View File

@@ -1,14 +1,19 @@
#include "material.h"
#include "../lib/glm/glm.hpp"
#include "../lib/glm/gtc/matrix_transform.hpp"
#include "light.h"
#include "shader.h"
#include "state.h"
#include "texture.h"
Material::Material(const std::shared_ptr<Texture>& tex,
const std::shared_ptr<Shader>& shader)
: shader_(shader), texture_(tex)
: shader_(shader)
, texture_(tex)
, color_(1.0f, 1.0f, 1.0f, 1.0f)
, shininess_(0)
{
}
@@ -42,6 +47,39 @@ void Material::prepare()
const int mvp_loc = active_shader->uniform_location("mvp");
Shader::set_mat4(mvp_loc, mvp);
// Set ModelView matrix
const glm::mat4 model_view = state::view_matrix * state::model_matrix;
const int model_view_loc = active_shader->uniform_location("modelView");
Shader::set_mat4(model_view_loc, model_view);
// Set Normal matrix (transpose of inverse of ModelView)
const glm::mat4 normal_matrix =
glm::transpose(glm::inverse(model_view));
const int normal_matrix_loc =
active_shader->uniform_location("normalMatrix");
Shader::set_mat4(normal_matrix_loc, normal_matrix);
// Set material properties
const int material_color_loc =
active_shader->uniform_location("materialColor");
Shader::set_vec4(material_color_loc, color_);
const int shininess_loc = active_shader->uniform_location("shininess");
Shader::set_float(shininess_loc, static_cast<float>(shininess_));
// Set ambient light
const int ambient_loc = active_shader->uniform_location("ambientLight");
Shader::set_vec3(ambient_loc, state::ambient);
// Set number of lights
const int num_lights_loc = active_shader->uniform_location("numLights");
Shader::set_int(num_lights_loc, static_cast<int>(state::lights.size()));
// Prepare each light
for (size_t i = 0; i < state::lights.size(); ++i) {
state::lights[i]->prepare(static_cast<int>(i), active_shader);
}
// Set texture-related uniforms
const int use_texture_loc =
active_shader->uniform_location("useTexture");