Now all configurable with json.

This commit is contained in:
Zed A. Shaw 2026-08-26 19:56:01 -04:00
parent 3041ae0172
commit d3169d3eca
25 changed files with 720 additions and 244 deletions

View file

@ -60,7 +60,7 @@ unsigned int Shader::load_shader(const std::string& filename, GLenum shader_type
return shader_id;
}
Shader::Shader(const char* vertexPath, const char* fragmentPath) {
Shader::Shader(const std::string& vertexPath, const std::string& fragmentPath) {
unsigned int vertex = load_shader(vertexPath, GL_VERTEX_SHADER);
unsigned int fragment = load_shader(fragmentPath, GL_FRAGMENT_SHADER);
@ -131,27 +131,42 @@ void Shader::apply_material(const Material& material) {
}
void Shader::apply_lighting(const Lighting& lighting) {
apply_dir_light(lighting.directional);
setInt("pointLightCount", lighting.positioned.size());
setInt("dirLightCount", lighting.directional.size());
// need +1 for the camera spot light
setInt("spotLightCount", lighting.spot.size() + 1);
for(size_t i = 0; i < lighting.directional.size(); i++) {
apply_dir_light(lighting.directional[i], i);
}
for(size_t i = 0; i < lighting.positioned.size(); i++) {
apply_point_light(lighting.positioned[i], i);
}
apply_spot_light(lighting.spot);
// set the first spotlight to the camera light
apply_spot_light(lighting.camera, 0);
for(size_t i = 0; i < lighting.spot.size(); i++) {
// need to be off by one because the camera is a spot light
apply_spot_light(lighting.spot[i], i+1);
}
}
void Shader::apply_dir_light(const Light& light) {
setVec3("dirLight.direction", light.direction);
setVec3("dirLight.ambient", light.ambient);
setVec3("dirLight.diffuse", light.diffuse);
setVec3("dirLight.specular", light.specular);
void Shader::apply_dir_light(const Light& light, size_t index) {
dbc::check(index < MAX_LIGHTS, "too many directional lights");
setVec3(std::format("dirLights[{}].direction", index), light.direction);
setVec3(std::format("dirLights[{}].ambient", index), light.ambient);
setVec3(std::format("dirLights[{}].diffuse", index), light.diffuse);
setVec3(std::format("dirLights[{}].specular", index), light.specular);
}
void Shader::apply_point_light(const Light& light, size_t index) {
dbc::check(index < NR_POINT_LIGHTS, "too many positioned lights");
dbc::check(index < MAX_LIGHTS, "too many positioned lights");
// disgusting, crafting a string on every render?
setVec3(std::format("pointLights[{}].position", index), light.position);
setVec3(std::format("pointLights[{}].ambient", index), light.ambient);
setVec3(std::format("pointLights[{}].diffuse", index), light.diffuse);
@ -162,15 +177,17 @@ void Shader::apply_point_light(const Light& light, size_t index) {
setFloat(std::format("pointLights[{}].quadratic", index), light.quadratic);
}
void Shader::apply_spot_light(const Light& light) {
setVec3("spotLight.position", light.position);
setVec3("spotLight.direction", light.direction);
setVec3("spotLight.ambient", light.ambient);
setVec3("spotLight.diffuse", light.diffuse);
setVec3("spotLight.specular", light.specular);
setFloat("spotLight.constant", light.constant);
setFloat("spotLight.linear", light.linear);
setFloat("spotLight.quadratic", light.quadratic);
setFloat("spotLight.cutOff", glm::cos(glm::radians(light.cutOff)));
setFloat("spotLight.outerCutOff", glm::cos(glm::radians(light.outerCutOff)));
void Shader::apply_spot_light(const Light& light, size_t index) {
dbc::check(index < MAX_LIGHTS, "too many spot lights");
setVec3(std::format("spotLights[{}].position", index), light.position);
setVec3(std::format("spotLights[{}].direction", index), light.direction);
setVec3(std::format("spotLights[{}].ambient", index), light.ambient);
setVec3(std::format("spotLights[{}].diffuse", index), light.diffuse);
setVec3(std::format("spotLights[{}].specular", index), light.specular);
setFloat(std::format("spotLights[{}].constant", index), light.constant);
setFloat(std::format("spotLights[{}].linear", index), light.linear);
setFloat(std::format("spotLights[{}].quadratic", index), light.quadratic);
setFloat(std::format("spotLights[{}].cut_off", index), glm::cos(glm::radians(light.cut_off)));
setFloat(std::format("spotLights[{}].outer_cut_off", index), glm::cos(glm::radians(light.outer_cut_off)));
}