57 lines
1.6 KiB
C++
57 lines
1.6 KiB
C++
#include "light.h"
|
|
|
|
#include "../lib/glm/gtc/matrix_transform.hpp"
|
|
|
|
#include "shader.h"
|
|
#include "state.h"
|
|
|
|
Light::Light()
|
|
: Entity()
|
|
, type_(Type::DIRECTIONAL)
|
|
, color_(1.0f, 1.0f, 1.0f)
|
|
, intensity_(1.0f)
|
|
, linear_attenuation_(0.0f)
|
|
{
|
|
}
|
|
|
|
void Light::prepare(int index, std::shared_ptr<Shader>& shader) const
|
|
{
|
|
if (!shader)
|
|
return;
|
|
|
|
// Build uniform names for this light index
|
|
std::string light_vector =
|
|
"lights[" + std::to_string(index) + "].vector";
|
|
std::string light_color = "lights[" + std::to_string(index) + "].color";
|
|
std::string light_intensity =
|
|
"lights[" + std::to_string(index) + "].intensity";
|
|
std::string light_linear_att =
|
|
"lights[" + std::to_string(index) + "].linear_att";
|
|
|
|
// Get uniform locations
|
|
int vector_loc = shader->uniform_location(light_vector.c_str());
|
|
int color_loc = shader->uniform_location(light_color.c_str());
|
|
int intensity_loc = shader->uniform_location(light_intensity.c_str());
|
|
int linear_att_loc = shader->uniform_location(light_linear_att.c_str());
|
|
|
|
// Calculate light vector in view space
|
|
glm::vec4 light_vector_view;
|
|
if (type_ == Type::DIRECTIONAL) {
|
|
// For directional lights, w = 0
|
|
glm::vec3 direction =
|
|
glm::normalize(position_); // Use position as direction
|
|
light_vector_view =
|
|
state::view_matrix * glm::vec4(direction, 0.0f);
|
|
} else {
|
|
// For point lights, w = 1
|
|
light_vector_view =
|
|
state::view_matrix * glm::vec4(position_, 1.0f);
|
|
}
|
|
|
|
// Set uniforms
|
|
Shader::set_vec4(vector_loc, light_vector_view);
|
|
Shader::set_vec3(color_loc, color_);
|
|
Shader::set_float(intensity_loc, intensity_);
|
|
Shader::set_float(linear_att_loc, linear_attenuation_);
|
|
}
|