Camera and shader now fixed up.

This commit is contained in:
Zed A. Shaw 2026-08-25 14:11:14 -04:00
parent 47bd233722
commit a7beb5a92a
5 changed files with 60 additions and 53 deletions

View file

@ -2,7 +2,7 @@
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
glm::mat4 Camera::lookAt() {
glm::mat4 Camera::look_at() {
return glm::lookAt(pos, pos + front, up);
}

View file

@ -16,7 +16,7 @@ struct Camera {
float fov = 45.0f;
bool firstMouse = true;
glm::mat4 lookAt();
glm::mat4 look_at();
void forward();
void back();
void left();

View file

@ -224,7 +224,7 @@ Config setup() {
void draw_models(Config& config, size_t model_i, size_t model_pos, glm::mat4& projection, glm::mat4& view)
{
glm::vec3 objectColor = {1.0, 0.94, 0.78};
config.shader.applyMaterial(config.cubeMaterial);
config.shader.apply_material(config.cubeMaterial);
float time = glfwGetTime();
glm::mat4 model = glm::mat4(1.0f);
@ -247,7 +247,7 @@ void render(GLFWwindow* window, Config& config) {
config.camera.update(config.deltaTime);
glm::mat4 view = config.camera.lookAt();
glm::mat4 view = config.camera.look_at();
glm::mat4 projection = glm::mat4(1.0f);
// fov, aspect, near plane, far plane
@ -260,7 +260,7 @@ void render(GLFWwindow* window, Config& config) {
config.light.spot.position = config.camera.pos;
config.light.spot.direction = config.camera.front;
config.shader.applyLighting(config.light);
config.shader.apply_lighting(config.light);
for(size_t i = 0; i < CUBE_COUNT; i++) {
draw_models(config, 0, i, projection, view);

View file

@ -124,3 +124,53 @@ void Shader::setMat4(const std::string &name, const glm::mat4& mat) const {
unsigned int loc = glGetUniformLocation(ID, name.c_str());
glUniformMatrix4fv(loc, 1, GL_FALSE, glm::value_ptr(mat));
}
void Shader::apply_material(const Material& material) {
setVec3("material.ambient", material.ambient);
setFloat("material.shininess", material.shininess);
}
void Shader::apply_lighting(const Lighting& lighting) {
apply_dir_light(lighting.directional);
setInt("pointLightCount", lighting.positioned.size());
for(size_t i = 0; i < lighting.positioned.size(); i++) {
apply_point_light(lighting.positioned[i], i);
}
apply_spot_light(lighting.spot);
}
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_point_light(const Light& light, size_t index) {
dbc::check(index < NR_POINT_LIGHTS, "too many positioned lights");
setVec3(std::format("pointLights[{}].position", index), light.position);
setVec3(std::format("pointLights[{}].ambient", index), light.ambient);
setVec3(std::format("pointLights[{}].diffuse", index), light.diffuse);
setVec3(std::format("pointLights[{}].specular", index), light.specular);
setFloat(std::format("pointLights[{}].constant", index), light.constant);
setFloat(std::format("pointLights[{}].linear", index), light.linear);
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)));
}

View file

@ -61,52 +61,9 @@ public:
void setMat4(const std::string &name, const glm::mat4& what) const;
void cleanup();
void applyMaterial(const Material& material) {
setVec3("material.ambient", material.ambient);
setFloat("material.shininess", material.shininess);
}
void applyLighting(const Lighting& lighting) {
applyDirLight(lighting.directional);
setInt("pointLightCount", lighting.positioned.size());
for(size_t i = 0; i < lighting.positioned.size(); i++) {
applyPointLight(lighting.positioned[i], i);
}
applySpotLight(lighting.spot);
}
void applyDirLight(const Light& light) {
setVec3("dirLight.direction", light.direction);
setVec3("dirLight.ambient", light.ambient);
setVec3("dirLight.diffuse", light.diffuse);
setVec3("dirLight.specular", light.specular);
}
void applyPointLight(const Light& light, size_t index) {
dbc::check(index < NR_POINT_LIGHTS, "too many positioned lights");
setVec3(std::format("pointLights[{}].position", index), light.position);
setVec3(std::format("pointLights[{}].ambient", index), light.ambient);
setVec3(std::format("pointLights[{}].diffuse", index), light.diffuse);
setVec3(std::format("pointLights[{}].specular", index), light.specular);
setFloat(std::format("pointLights[{}].constant", index), light.constant);
setFloat(std::format("pointLights[{}].linear", index), light.linear);
setFloat(std::format("pointLights[{}].quadratic", index), light.quadratic);
}
void applySpotLight(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 apply_material(const Material& material);
void apply_lighting(const Lighting& lighting);
void apply_dir_light(const Light& light);
void apply_point_light(const Light& light, size_t index);
void apply_spot_light(const Light& light);
};