feat: assignment 3

This commit is contained in:
2025-10-13 18:23:01 +02:00
parent db6e548476
commit 49219fc597
13 changed files with 364 additions and 61 deletions

61
src/material.cpp Normal file
View File

@@ -0,0 +1,61 @@
#include "material.h"
#include "../lib/glm/glm.hpp"
#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)
{
}
const std::shared_ptr<Shader>& Material::shader() const
{
return shader_ ? shader_ : state::default_shader;
}
std::shared_ptr<Shader>& Material::shader()
{
return shader_ ? shader_ : state::default_shader;
}
void Material::set_shader(const std::shared_ptr<Shader>& shader)
{
shader_ = shader;
}
void Material::prepare()
{
// Activate shader
std::shared_ptr<Shader> active_shader = shader();
if (!active_shader)
return;
active_shader->use();
// Set uniform variables
const glm::mat4 mvp =
state::projection_matrix * state::view_matrix * state::model_matrix;
const int mvp_loc = active_shader->uniform_location("mvp");
Shader::set_mat4(mvp_loc, mvp);
// Set texture-related uniforms
const int use_texture_loc =
active_shader->uniform_location("useTexture");
if (texture_) {
Shader::set_int(use_texture_loc, 1);
const int sampler_loc =
active_shader->uniform_location("texSampler");
Shader::set_int(sampler_loc, 0); // Texture unit 0
} else {
Shader::set_int(use_texture_loc, 0);
}
// Bind texture if available
if (texture_) {
texture_->bind();
}
}