mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-07-21 14:49:19 +02:00
ubo for light settings
This commit is contained in:
110
shader.cpp
110
shader.cpp
@@ -66,6 +66,11 @@ gl_shader::operator GLuint()
|
||||
return id;
|
||||
}
|
||||
|
||||
gl_shader::~gl_shader()
|
||||
{
|
||||
//glDeleteShader(*this); //m7todo: something is broken
|
||||
}
|
||||
|
||||
gl_program::gl_program(std::vector<gl_shader> shaders)
|
||||
{
|
||||
id = glCreateProgram();
|
||||
@@ -116,6 +121,11 @@ gl_program::operator GLuint()
|
||||
return id;
|
||||
}
|
||||
|
||||
gl_program::~gl_program()
|
||||
{
|
||||
//glDeleteProgram(*this); //m7todo: something is broken
|
||||
}
|
||||
|
||||
gl_program_mvp::gl_program_mvp(std::vector<gl_shader> v) : gl_program(v)
|
||||
{
|
||||
mv_uniform = glGetUniformLocation(id, "modelview");
|
||||
@@ -151,26 +161,14 @@ gl_program_light::gl_program_light(std::vector<gl_shader> v) : gl_program_mvp(v)
|
||||
glUniform1i(glGetUniformLocation(id, "tex"), 0);
|
||||
glUniform1i(glGetUniformLocation(id, "shadowmap"), 1);
|
||||
lightview_uniform = glGetUniformLocation(id, "lightview");
|
||||
ambient_uniform = glGetUniformLocation(id, "ambient");
|
||||
emission_uniform = glGetUniformLocation(id, "emission");
|
||||
specular_uniform = glGetUniformLocation(id, "specular");
|
||||
fog_color_uniform = glGetUniformLocation(id, "fog_color");
|
||||
fog_density_uniform = glGetUniformLocation(id, "fog_density");
|
||||
lcount_uniform = glGetUniformLocation(id, "lights_count");
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < MAX_LIGHTS; i++)
|
||||
{
|
||||
lights_uniform[i].type = glGetUniformLocation(id, std::string("lights[" + std::to_string(i) + "].type").c_str());
|
||||
lights_uniform[i].pos = glGetUniformLocation(id, std::string("lights[" + std::to_string(i) + "].pos").c_str());
|
||||
lights_uniform[i].dir = glGetUniformLocation(id, std::string("lights[" + std::to_string(i) + "].dir").c_str());
|
||||
lights_uniform[i].in_cutoff = glGetUniformLocation(id, std::string("lights[" + std::to_string(i) + "].in_cutoff").c_str());
|
||||
lights_uniform[i].out_cutoff = glGetUniformLocation(id, std::string("lights[" + std::to_string(i) + "].out_cutoff").c_str());
|
||||
lights_uniform[i].color = glGetUniformLocation(id, std::string("lights[" + std::to_string(i) + "].color").c_str());
|
||||
lights_uniform[i].linear = glGetUniformLocation(id, std::string("lights[" + std::to_string(i) + "].linear").c_str());
|
||||
lights_uniform[i].quadratic = glGetUniformLocation(id, std::string("lights[" + std::to_string(i) + "].quadratic").c_str());
|
||||
}
|
||||
|
||||
glUniform1ui(lcount_uniform, 0);
|
||||
void gl_program_light::bind_ubodata(GLuint point)
|
||||
{
|
||||
GLuint index = glGetUniformBlockIndex(*this, "ubodata");
|
||||
glUniformBlockBinding(*this, index, point);
|
||||
}
|
||||
|
||||
void gl_program_light::set_lightview(const glm::mat4 &lightview)
|
||||
@@ -181,53 +179,6 @@ void gl_program_light::set_lightview(const glm::mat4 &lightview)
|
||||
glUniformMatrix4fv(lightview_uniform, 1, GL_FALSE, glm::value_ptr(lightview));
|
||||
}
|
||||
|
||||
void gl_program_light::set_ambient(const glm::vec3 &ambient)
|
||||
{
|
||||
if (current_program != this)
|
||||
return;
|
||||
|
||||
glUniform3fv(ambient_uniform, 1, glm::value_ptr(ambient));
|
||||
}
|
||||
|
||||
void gl_program_light::set_fog(float density, const glm::vec3 &color)
|
||||
{
|
||||
if (current_program != this)
|
||||
return;
|
||||
|
||||
glUniform1f(fog_density_uniform, density);
|
||||
glUniform3fv(fog_color_uniform, 1, glm::value_ptr(color));
|
||||
}
|
||||
|
||||
void gl_program_light::set_light_count(GLuint count)
|
||||
{
|
||||
if (current_program != this)
|
||||
return;
|
||||
|
||||
glUniform1ui(lcount_uniform, count);
|
||||
}
|
||||
|
||||
void gl_program_light::set_light(GLuint i, type t, const glm::vec3 &pos, const glm::vec3 &dir,
|
||||
float in_cutoff, float out_cutoff,
|
||||
const glm::vec3 &color, float linear, float quadratic)
|
||||
{
|
||||
if (current_program != this)
|
||||
return;
|
||||
|
||||
glm::mat4 mv = OpenGLMatrices.data(GL_MODELVIEW);
|
||||
|
||||
glm::vec3 trans_pos = mv * glm::vec4(pos.x, pos.y, pos.z, 1.0f);
|
||||
glm::vec3 trans_dir = mv * glm::vec4(dir.x, dir.y, dir.z, 0.0f);
|
||||
|
||||
glUniform1ui(lights_uniform[i].type, (GLuint)t);
|
||||
glUniform3fv(lights_uniform[i].pos, 1, glm::value_ptr(trans_pos));
|
||||
glUniform3fv(lights_uniform[i].dir, 1, glm::value_ptr(trans_dir));
|
||||
glUniform1f(lights_uniform[i].in_cutoff, in_cutoff);
|
||||
glUniform1f(lights_uniform[i].out_cutoff, out_cutoff);
|
||||
glUniform3fv(lights_uniform[i].color, 1, glm::value_ptr(color));
|
||||
glUniform1f(lights_uniform[i].linear, linear);
|
||||
glUniform1f(lights_uniform[i].quadratic, quadratic);
|
||||
}
|
||||
|
||||
void gl_program_light::set_material(float specular, const glm::vec3 &emission)
|
||||
{
|
||||
if (current_program != this)
|
||||
@@ -236,3 +187,34 @@ void gl_program_light::set_material(float specular, const glm::vec3 &emission)
|
||||
glUniform1f(specular_uniform, specular);
|
||||
glUniform3fv(emission_uniform, 1, glm::value_ptr(emission));
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
GLuint gl_ubo<T>::binding_point_cnt = 0;
|
||||
|
||||
template<typename T>
|
||||
void gl_ubo<T>::init()
|
||||
{
|
||||
glGenBuffers(1, &buffer_id);
|
||||
glBindBuffer(GL_UNIFORM_BUFFER, buffer_id);
|
||||
glBufferData(GL_UNIFORM_BUFFER, sizeof(T), nullptr, GL_DYNAMIC_DRAW);
|
||||
binding_point = binding_point_cnt++;
|
||||
glBindBufferBase(GL_UNIFORM_BUFFER, binding_point, buffer_id);
|
||||
glBindBuffer(GL_UNIFORM_BUFFER, 0);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void gl_ubo<T>::update()
|
||||
{
|
||||
glBindBuffer(GL_UNIFORM_BUFFER, buffer_id);
|
||||
glBufferSubData(GL_UNIFORM_BUFFER, 0, sizeof(T), &data);
|
||||
glBindBuffer(GL_UNIFORM_BUFFER, 0);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
gl_ubo<T>::~gl_ubo()
|
||||
{
|
||||
if (buffer_id)
|
||||
glDeleteBuffers(1, &buffer_id);
|
||||
}
|
||||
|
||||
template class gl_ubo<gl_ubodata_light_params>;
|
||||
Reference in New Issue
Block a user