calc normal mat on cpu. use blinn-phong lighting. adjust lighting intensities

This commit is contained in:
2023-11-02 04:15:59 +02:00
committed by Illusion
parent 12ace9cb13
commit 1f11904e4b
5 changed files with 62 additions and 23 deletions
+28 -9
View File
@@ -81,6 +81,7 @@ struct SpotLight {
}; };
#define NR_POINT_LIGHTS 4 #define NR_POINT_LIGHTS 4
#define blinn true
uniform vec3 viewPos; uniform vec3 viewPos;
uniform Material material; uniform Material material;
@@ -92,16 +93,22 @@ vec3 CalculateDirLight(DirLight light, vec3 normal, vec3 viewDir) {
vec3 lightDir = normalize(-light.direction); vec3 lightDir = normalize(-light.direction);
float diff = max(dot(lightDir, normal), 0.0); float diff = max(dot(lightDir, normal), 0.0);
float spec = 0.0f;
if (blinn) {
vec3 halfwayDir = normalize(lightDir + viewDir);
spec = pow(max(dot(normal, halfwayDir), 0.0), material.shininess);
} else {
vec3 reflectDir = reflect(-lightDir, normal); vec3 reflectDir = reflect(-lightDir, normal);
float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess); spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);
}
//vec3 ambient = light.ambient * vec3(texture(material.texture_diffuse1, texCoords)) * material.ambient; //vec3 ambient = light.ambient * vec3(texture(material.texture_diffuse1, texCoords)) * material.ambient;
//vec3 diffuse = light.diffuse * diff * vec3(texture(material.texture_diffuse1, texCoords)) * material.diffuse; //vec3 diffuse = light.diffuse * diff * vec3(texture(material.texture_diffuse1, texCoords)) * material.diffuse;
//vec3 specular = light.specular * spec * vec3(texture(material.texture_specular1, texCoords)) * material.specular; //vec3 specular = light.specular * spec * vec3(texture(material.texture_specular1, texCoords)) * material.specular;
vec3 ambient = light.ambient * material.ambient; vec3 ambient = light.ambient * material.ambient;
vec3 diffuse = light.diffuse * diff * material.diffuse; vec3 diffuse = light.diffuse * (diff * material.diffuse);
vec3 specular = light.specular * spec * material.specular; vec3 specular = light.specular * (spec * material.specular);
return (ambient + diffuse + specular); return (ambient + diffuse + specular);
} }
@@ -110,9 +117,15 @@ vec3 CalculatePointLight(PointLight light, vec3 normal, vec3 fragPos, vec3 viewD
vec3 lightDir = normalize(light.position - fragPos); vec3 lightDir = normalize(light.position - fragPos);
float diff = max(dot(lightDir, normal), 0.0); float diff = max(dot(lightDir, normal), 0.0);
float spec = 0.0f;
if (blinn) {
vec3 halfwayDir = normalize(lightDir + viewDir);
spec = pow(max(dot(normal, halfwayDir), 0.0), material.shininess);
} else {
vec3 reflectDir = reflect(-lightDir, normal); vec3 reflectDir = reflect(-lightDir, normal);
float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess); spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);
}
// attenuation // attenuation
float distance = length(light.position - fragPos); float distance = length(light.position - fragPos);
@@ -122,8 +135,8 @@ vec3 CalculatePointLight(PointLight light, vec3 normal, vec3 fragPos, vec3 viewD
//vec3 diffuse = light.diffuse * diff * vec3(texture(material.texture_diffuse1, texCoords)) * material.diffuse; //vec3 diffuse = light.diffuse * diff * vec3(texture(material.texture_diffuse1, texCoords)) * material.diffuse;
//vec3 specular = light.specular * spec * vec3(texture(material.texture_specular1, texCoords)) * material.specular; //vec3 specular = light.specular * spec * vec3(texture(material.texture_specular1, texCoords)) * material.specular;
vec3 ambient = light.ambient * material.ambient; vec3 ambient = light.ambient * material.ambient;
vec3 diffuse = light.diffuse * diff * material.diffuse; vec3 diffuse = light.diffuse * (diff * material.diffuse);
vec3 specular = light.specular * spec * material.specular; vec3 specular = light.specular * (spec * material.specular);
ambient *= attenuation; ambient *= attenuation;
diffuse *= attenuation; diffuse *= attenuation;
@@ -136,9 +149,15 @@ vec3 CalculateSpotLight(SpotLight light, vec3 normal, vec3 fragPos, vec3 viewDir
vec3 lightDir = normalize(light.position - fragPos); vec3 lightDir = normalize(light.position - fragPos);
float diff = max(dot(lightDir, normal), 0.0); float diff = max(dot(lightDir, normal), 0.0);
float spec = 0.0f;
if (blinn) {
vec3 halfwayDir = normalize(lightDir + viewDir);
spec = pow(max(dot(normal, halfwayDir), 0.0), material.shininess);
} else {
vec3 reflectDir = reflect(-lightDir, normal); vec3 reflectDir = reflect(-lightDir, normal);
float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess); spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);
}
// attenuation // attenuation
float distance = length(light.position - fragPos); float distance = length(light.position - fragPos);
@@ -153,8 +172,8 @@ vec3 CalculateSpotLight(SpotLight light, vec3 normal, vec3 fragPos, vec3 viewDir
//vec3 diffuse = light.diffuse * diff * vec3(texture(material.texture_diffuse1, texCoords)) * material.diffuse; //vec3 diffuse = light.diffuse * diff * vec3(texture(material.texture_diffuse1, texCoords)) * material.diffuse;
//vec3 specular = light.specular * spec * vec3(texture(material.texture_specular1, texCoords)) * material.specular; //vec3 specular = light.specular * spec * vec3(texture(material.texture_specular1, texCoords)) * material.specular;
vec3 ambient = light.ambient * material.ambient; vec3 ambient = light.ambient * material.ambient;
vec3 diffuse = light.diffuse * diff * material.diffuse; vec3 diffuse = light.diffuse * (diff * material.diffuse);
vec3 specular = light.specular * spec * material.specular; vec3 specular = light.specular * (spec * material.specular);
diffuse *= intensity; diffuse *= intensity;
specular *= intensity; specular *= intensity;
+2 -1
View File
@@ -9,11 +9,12 @@ out vec2 texCoords;
uniform mat4 model; uniform mat4 model;
uniform mat4 view; uniform mat4 view;
uniform mat4 projection; uniform mat4 projection;
uniform mat3 normalMatrix;
void main() void main()
{ {
gl_Position = projection * view * model * vec4(aPos, 1.0f); gl_Position = projection * view * model * vec4(aPos, 1.0f);
fragNormals = mat3(transpose(inverse(model))) * aNormal; fragNormals = normalMatrix * aNormal;
fragPos = vec3(model * vec4(aPos, 1.0)); fragPos = vec3(model * vec4(aPos, 1.0));
texCoords = aTexCoords; texCoords = aTexCoords;
} }
+6 -6
View File
@@ -31,7 +31,7 @@ fn main() -> anyhow::Result<(), Box<dyn std::error::Error>> {
.build(); .build();
let log_level = if cfg!(debug_assertions) { let log_level = if cfg!(debug_assertions) {
LevelFilter::Debug LevelFilter::Trace
} else { } else {
LevelFilter::Info LevelFilter::Info
}; };
@@ -132,22 +132,22 @@ fn main() -> anyhow::Result<(), Box<dyn std::error::Error>> {
mesh_shader.set_float(&format!("pointLights[{}].linear", i), 0.09); mesh_shader.set_float(&format!("pointLights[{}].linear", i), 0.09);
mesh_shader.set_float(&format!("pointLights[{}].quadratic", i), 0.032); mesh_shader.set_float(&format!("pointLights[{}].quadratic", i), 0.032);
mesh_shader.set_3fv(&format!("pointLights[{}].ambient", i), glm::vec3(0.2, 0.2, 0.2)); mesh_shader.set_3fv(&format!("pointLights[{}].ambient", i), glm::vec3(0.1, 0.1, 0.1));
mesh_shader.set_3fv(&format!("pointLights[{}].diffuse", i), glm::vec3(0.5, 0.5, 0.5)); mesh_shader.set_3fv(&format!("pointLights[{}].diffuse", i), glm::vec3(0.7, 0.7, 0.7));
mesh_shader.set_3fv(&format!("pointLights[{}].specular", i), glm::vec3(1.0, 1.0, 1.0)); mesh_shader.set_3fv(&format!("pointLights[{}].specular", i), glm::vec3(1.0, 1.0, 1.0));
} }
mesh_shader.set_float("spotLight.cutOff", glm::cos(glm::radians(12.5))); mesh_shader.set_float("spotLight.cutOff", glm::cos(glm::radians(12.5)));
mesh_shader.set_float("spotLight.outerCutOff", glm::cos(glm::radians(15.0))); mesh_shader.set_float("spotLight.outerCutOff", glm::cos(glm::radians(15.0)));
mesh_shader.set_3fv("spotLight.ambient", glm::vec3(0.2, 0.2, 0.2)); mesh_shader.set_3fv("spotLight.ambient", glm::vec3(0.2, 0.2, 0.2));
mesh_shader.set_3fv("spotLight.diffuse", glm::vec3(0.2, 0.2, 0.2)); mesh_shader.set_3fv("spotLight.diffuse", glm::vec3(0.5, 0.5, 0.5));
mesh_shader.set_3fv("spotLight.specular", glm::vec3(1.0, 1.0, 1.0)); mesh_shader.set_3fv("spotLight.specular", glm::vec3(1.0, 1.0, 1.0));
mesh_shader.set_float("spotLight.constant", 1.0); mesh_shader.set_float("spotLight.constant", 1.0);
mesh_shader.set_float("spotLight.linear", 0.09); mesh_shader.set_float("spotLight.linear", 0.09);
mesh_shader.set_float("spotLight.quadratic", 0.032); mesh_shader.set_float("spotLight.quadratic", 0.032);
mesh_shader.set_3fv("dirLight.direction", glm::vec3(-0.2, -1.0, -0.3)); mesh_shader.set_3fv("dirLight.direction", glm::vec3(-0.2, -1.0, -0.3));
mesh_shader.set_3fv("dirLight.ambient", glm::vec3(0.2, 0.2, 0.2)); mesh_shader.set_3fv("dirLight.ambient", glm::vec3(0.1, 0.1, 0.1));
mesh_shader.set_3fv("dirLight.diffuse", glm::vec3(0.5, 0.5, 0.5)); mesh_shader.set_3fv("dirLight.diffuse", glm::vec3(1.0, 1.0, 1.0));
mesh_shader.set_3fv("dirLight.specular", glm::vec3(1.0, 1.0, 1.0)); mesh_shader.set_3fv("dirLight.specular", glm::vec3(1.0, 1.0, 1.0));
let scene_fb = create_scene_framebuffer(); let scene_fb = create_scene_framebuffer();
+14 -2
View File
@@ -104,6 +104,13 @@ impl Mesh {
let model_mat = apply_rotation(&model_mat, self.rotation, pivot); let model_mat = apply_rotation(&model_mat, self.rotation, pivot);
let model_mat = glm::ext::translate(&model_mat, glm::vec3(self.position.x, self.position.y, self.position.z)); let model_mat = glm::ext::translate(&model_mat, glm::vec3(self.position.x, self.position.y, self.position.z));
shader.set_mat4fv("model", &model_mat); shader.set_mat4fv("model", &model_mat);
let normal_mat = glm::transpose(&glm::inverse(&model_mat));
let normal_mat = glm::mat3(
normal_mat[0][0], normal_mat[0][1], normal_mat[0][2],
normal_mat[1][0], normal_mat[1][1], normal_mat[1][2],
normal_mat[2][0], normal_mat[2][1], normal_mat[2][2],
);
shader.set_mat3fv("normalMatrix", &normal_mat);
let mut polygon_mode = 0; let mut polygon_mode = 0;
unsafe { unsafe {
@@ -111,10 +118,15 @@ impl Mesh {
} }
let is_wireframe = polygon_mode as u32 == gl::LINE; let is_wireframe = polygon_mode as u32 == gl::LINE;
shader.set_3fv("material.ambient", if is_wireframe {glm::vec3(0.0, 0.0, 0.0)} else {self.material.ambient_color}); if !is_wireframe {
shader.set_3fv("material.diffuse", if is_wireframe {glm::vec3(0.0, 0.0, 0.0)} else {self.material.diffuse_color}); shader.set_3fv("material.ambient", self.material.ambient_color);
shader.set_3fv("material.diffuse", self.material.diffuse_color);
shader.set_3fv("material.specular", self.material.specular_color); shader.set_3fv("material.specular", self.material.specular_color);
shader.set_float("material.shininess", self.material.specular_exponent); shader.set_float("material.shininess", self.material.specular_exponent);
} else {
shader.set_3fv("material.ambient", glm::vec3(0.0, 0.0, 0.0));
shader.set_3fv("material.diffuse", glm::vec3(0.0, 0.0, 0.0));
}
// for i in 0..self.textures.len() { // for i in 0..self.textures.len() {
// unsafe { // unsafe {
+7
View File
@@ -108,6 +108,13 @@ impl Shader {
} }
} }
pub fn set_mat3fv(&self, name: &str, value: &glm::Mat3) {
let c_str = std::ffi::CString::new(name).unwrap();
unsafe {
gl::UniformMatrix3fv(gl::GetUniformLocation(self.program_id, c_str.as_ptr()), 1, gl::FALSE, value.as_array().as_ptr() as *const f32);
}
}
pub fn set_mat4fv(&self, name: &str, value: &glm::Mat4) { pub fn set_mat4fv(&self, name: &str, value: &glm::Mat4) {
let c_str = std::ffi::CString::new(name).unwrap(); let c_str = std::ffi::CString::new(name).unwrap();
unsafe { unsafe {