From 30c8a4d7c2160c174392aa2dccf60178f9cd02c2 Mon Sep 17 00:00:00 2001 From: iurii plugatarov Date: Sun, 18 Aug 2024 21:54:42 +0300 Subject: diffuse --- res/shaders/colors.fs | 15 ++++- res/shaders/colors.vs | 9 ++- src/main.odin | 157 +++++++++++++++----------------------------------- 3 files changed, 68 insertions(+), 113 deletions(-) diff --git a/res/shaders/colors.fs b/res/shaders/colors.fs index a2b8ff4..d434370 100644 --- a/res/shaders/colors.fs +++ b/res/shaders/colors.fs @@ -2,10 +2,23 @@ out vec4 FragColor; +in vec3 Normal; +in vec3 FragPos; + uniform vec3 object_color; uniform vec3 light_color; +uniform vec3 light_position; void main() { - FragColor = vec4(light_color * object_color, 1.0); + float ambient_stength = 0.1; + vec3 ambient = ambient_stength * light_color; + + vec3 norm = normalize(Normal); + vec3 light_dir = normalize(light_position - FragPos); + float diff = max(dot(norm, light_dir), 0.0); + vec3 diffuse = diff * light_color; + + vec3 result = (ambient + diffuse) * object_color; + FragColor = vec4(result, 1.0); } diff --git a/res/shaders/colors.vs b/res/shaders/colors.vs index 63a9758..323b2ad 100644 --- a/res/shaders/colors.vs +++ b/res/shaders/colors.vs @@ -1,11 +1,18 @@ #version 330 core layout (location = 0) in vec3 a_pos; +layout (location = 1) in vec3 a_normal; uniform mat4 model; uniform mat4 view; uniform mat4 projection; +out vec3 Normal; +out vec3 FragPos; + void main() { - gl_Position = projection * view * model * vec4(a_pos, 1.0); + FragPos = vec3(model * vec4(a_pos, 1.0)); + Normal = a_normal; + + gl_Position = projection * view * vec4(FragPos, 1.0); } diff --git a/src/main.odin b/src/main.odin index c2c54ef..43cd5fb 100644 --- a/src/main.odin +++ b/src/main.odin @@ -139,114 +139,47 @@ main :: proc() { } vertices: []f32 = { - -0.5, - -0.5, - -0.5, - 0.5, - -0.5, - -0.5, - 0.5, - 0.5, - -0.5, - 0.5, - 0.5, - -0.5, - -0.5, - 0.5, - -0.5, - -0.5, - -0.5, - -0.5, - -0.5, - -0.5, - 0.5, - 0.5, - -0.5, - 0.5, - 0.5, - 0.5, - 0.5, - 0.5, - 0.5, - 0.5, - -0.5, - 0.5, - 0.5, - -0.5, - -0.5, - 0.5, - -0.5, - 0.5, - 0.5, - -0.5, - 0.5, - -0.5, - -0.5, - -0.5, - -0.5, - -0.5, - -0.5, - -0.5, - -0.5, - -0.5, - 0.5, - -0.5, - 0.5, - 0.5, - 0.5, - 0.5, - 0.5, - 0.5, - 0.5, - -0.5, - 0.5, - -0.5, - -0.5, - 0.5, - -0.5, - -0.5, - 0.5, - -0.5, - 0.5, - 0.5, - 0.5, - 0.5, - -0.5, - -0.5, - -0.5, - 0.5, - -0.5, - -0.5, - 0.5, - -0.5, - 0.5, - 0.5, - -0.5, - 0.5, - -0.5, - -0.5, - 0.5, - -0.5, - -0.5, - -0.5, - -0.5, - 0.5, - -0.5, - 0.5, - 0.5, - -0.5, - 0.5, - 0.5, - 0.5, - 0.5, - 0.5, - 0.5, - -0.5, - 0.5, - 0.5, - -0.5, - 0.5, - -0.5, + -0.5, -0.5, -0.5, 0.0, 0.0, -1.0, + 0.5, -0.5, -0.5, 0.0, 0.0, -1.0, + 0.5, 0.5, -0.5, 0.0, 0.0, -1.0, + 0.5, 0.5, -0.5, 0.0, 0.0, -1.0, + -0.5, 0.5, -0.5, 0.0, 0.0, -1.0, + -0.5, -0.5, -0.5, 0.0, 0.0, -1.0, + + -0.5, -0.5, 0.5, 0.0, 0.0, 1.0, + 0.5, -0.5, 0.5, 0.0, 0.0, 1.0, + 0.5, 0.5, 0.5, 0.0, 0.0, 1.0, + 0.5, 0.5, 0.5, 0.0, 0.0, 1.0, + -0.5, 0.5, 0.5, 0.0, 0.0, 1.0, + -0.5, -0.5, 0.5, 0.0, 0.0, 1.0, + + -0.5, 0.5, 0.5, -1.0, 0.0, 0.0, + -0.5, 0.5, -0.5, -1.0, 0.0, 0.0, + -0.5, -0.5, -0.5, -1.0, 0.0, 0.0, + -0.5, -0.5, -0.5, -1.0, 0.0, 0.0, + -0.5, -0.5, 0.5, -1.0, 0.0, 0.0, + -0.5, 0.5, 0.5, -1.0, 0.0, 0.0, + + 0.5, 0.5, 0.5, 1.0, 0.0, 0.0, + 0.5, 0.5, -0.5, 1.0, 0.0, 0.0, + 0.5, -0.5, -0.5, 1.0, 0.0, 0.0, + 0.5, -0.5, -0.5, 1.0, 0.0, 0.0, + 0.5, -0.5, 0.5, 1.0, 0.0, 0.0, + 0.5, 0.5, 0.5, 1.0, 0.0, 0.0, + + -0.5, -0.5, -0.5, 0.0, -1.0, 0.0, + 0.5, -0.5, -0.5, 0.0, -1.0, 0.0, + 0.5, -0.5, 0.5, 0.0, -1.0, 0.0, + 0.5, -0.5, 0.5, 0.0, -1.0, 0.0, + -0.5, -0.5, 0.5, 0.0, -1.0, 0.0, + -0.5, -0.5, -0.5, 0.0, -1.0, 0.0, + + -0.5, 0.5, -0.5, 0.0, 1.0, 0.0, + 0.5, 0.5, -0.5, 0.0, 1.0, 0.0, + 0.5, 0.5, 0.5, 0.0, 1.0, 0.0, + 0.5, 0.5, 0.5, 0.0, 1.0, 0.0, + -0.5, 0.5, 0.5, 0.0, 1.0, 0.0, + -0.5, 0.5, -0.5, 0.0, 1.0, 0.0 } @@ -263,14 +196,15 @@ main :: proc() { gl.GenVertexArrays(1, &cube_vao) gl.BindVertexArray(cube_vao) - gl.VertexAttribPointer(0, 3, gl.FLOAT, gl.FALSE, 3 * size_of(f32), 0) + gl.VertexAttribPointer(0, 3, gl.FLOAT, gl.FALSE, 6 * size_of(f32), 0) gl.EnableVertexAttribArray(0) - + gl.VertexAttribPointer(1, 3, gl.FLOAT, gl.FALSE, 6 * size_of(f32), 3 * size_of(f32)) + gl.EnableVertexAttribArray(1) gl.GenVertexArrays(1, &light_cube_vao) gl.BindVertexArray(light_cube_vao) gl.BindBuffer(gl.ARRAY_BUFFER, vbo) - gl.VertexAttribPointer(0, 3, gl.FLOAT, gl.FALSE, 3 * size_of(f32), 0) + gl.VertexAttribPointer(0, 3, gl.FLOAT, gl.FALSE, 6 * size_of(f32), 0) gl.EnableVertexAttribArray(0) //gl.PolygonMode(gl.FRONT_AND_BACK, gl.LINE) @@ -293,6 +227,7 @@ main :: proc() { light_color := Vec3{1.0, 1.0, 1.0} shader.set_vec3(lighting_shader, cstring("object_color"), &object_color[0]) shader.set_vec3(lighting_shader, cstring("light_color"), &light_color[0]) + shader.set_vec3(lighting_shader, cstring("light_position"), &light_pos[0]) aspect: f32 = 800.0 / 600.0 projection := linalg.matrix4_perspective_f32( -- cgit 1.4.1-2-gfad0