From e4e90e53f02f6a431422c7790a7df34d396a3c6a Mon Sep 17 00:00:00 2001 From: iurii Date: Sat, 7 Sep 2024 15:27:01 +0300 Subject: specular mapping --- res/images/container_specular.png | Bin 0 -> 144081 bytes res/shaders/colors.fs | 4 ++-- src/main.odin | 14 +++++++------- 3 files changed, 9 insertions(+), 9 deletions(-) create mode 100644 res/images/container_specular.png diff --git a/res/images/container_specular.png b/res/images/container_specular.png new file mode 100644 index 0000000..681bf6e Binary files /dev/null and b/res/images/container_specular.png differ diff --git a/res/shaders/colors.fs b/res/shaders/colors.fs index a486a92..6a42cbd 100644 --- a/res/shaders/colors.fs +++ b/res/shaders/colors.fs @@ -4,7 +4,7 @@ out vec4 FragColor; struct Material { sampler2D diffuse; - vec3 specular; + sampler2D specular; float shininess; }; @@ -39,7 +39,7 @@ void main() { vec3 view_dir = normalize(view_position - FragPos); vec3 reflect_dir = reflect(-light_dir, norm); float spec = pow(max(dot(view_dir, reflect_dir), 0.0), material.shininess); - vec3 specular = light.specular * (spec * material.specular); + vec3 specular = light.specular * spec * texture(material.specular, TexCoords).rgb; vec3 result = ambient + diffuse + specular; FragColor = vec4(result, 1.0); diff --git a/src/main.odin b/src/main.odin index e261abf..80080e4 100644 --- a/src/main.odin +++ b/src/main.odin @@ -191,6 +191,7 @@ main :: proc() { defer gl.DeleteVertexArrays(1, &cube_vao) defer gl.DeleteBuffers(1, &vbo) diffuse_map := load_texture("res/images/container.png") + specular_map := load_texture("res/images/container_specular.png") if diffuse_map == 0 { fmt.eprintln("could not load texture: exiting...") @@ -216,6 +217,7 @@ main :: proc() { shader_use(lighting_shader) shader_set_i32(lighting_shader, "material.diffuse", 0) + shader_set_i32(lighting_shader, "material.specular", 1) //gl.PolygonMode(gl.FRONT_AND_BACK, gl.LINE) gl.Enable(gl.DEPTH_TEST) @@ -233,19 +235,14 @@ main :: proc() { shader_use(lighting_shader) - light_color := Vec3 { - cast(f32)math.sin(glfw.GetTime() * 2.0), - cast(f32)math.sin(glfw.GetTime() * 0.7), - cast(f32)math.sin(glfw.GetTime() * 1.3), - } material_ambient := Vec3{1.0, 0.5, 0.31} material_specular := Vec3{0.5, 0.5, 0.5} shader_set_vec3(lighting_shader, cstring("material.ambient"), &material_ambient) shader_set_vec3(lighting_shader, cstring("material.specular"), &material_specular) shader_set_f32(lighting_shader, cstring("material.shininess"), 32.0) - light_diffuse := light_color * Vec3{0.5, 0.5, 0.05} - light_ambient := light_diffuse * Vec3{0.2, 0.2, 0.2} + light_diffuse := Vec3{0.5, 0.5, 0.05} + light_ambient := Vec3{0.2, 0.2, 0.2} light_specular := Vec3{1.0, 1.0, 1.0} shader_set_vec3(lighting_shader, cstring("light.ambient"), &light_ambient) shader_set_vec3(lighting_shader, cstring("light.diffuse"), &light_diffuse) @@ -276,6 +273,9 @@ main :: proc() { gl.ActiveTexture(gl.TEXTURE0) gl.BindTexture(gl.TEXTURE_2D, diffuse_map) + gl.ActiveTexture(gl.TEXTURE1) + gl.BindTexture(gl.TEXTURE_2D, specular_map) + gl.BindVertexArray(cube_vao) gl.DrawArrays(gl.TRIANGLES, 0, 36) -- cgit 1.4.1-2-gfad0