From 1e34058ac8c5929e9dedb544b9a1c84a6c13435a Mon Sep 17 00:00:00 2001 From: iurii Date: Sat, 31 Aug 2024 22:27:54 +0300 Subject: phong shader model --- .gitignore | 2 + build.bat | 37 +++++++ res/shaders/colors.fs | 12 ++- res/shaders/colors.vs | 2 +- src/main.odin | 261 +++++++++++++++++++++++++++++++++++++++++-------- src/shader/shader.odin | 6 +- 6 files changed, 271 insertions(+), 49 deletions(-) create mode 100644 build.bat diff --git a/.gitignore b/.gitignore index 1fcb152..37b81c5 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ +.vs out +src\.vs diff --git a/build.bat b/build.bat new file mode 100644 index 0000000..314ee0f --- /dev/null +++ b/build.bat @@ -0,0 +1,37 @@ +@echo off +setlocal + +:: Define the compiler and flags +set ODIN=odin +set SRC_DIR=src +set OUT_DIR_DEBUG=out\debug +set OUT_DIR_RELEASE=out\release +set PROGRAM_NAME=funhalla.exe + +:: Define the build targets +call :build_debug +call :build_release +goto :eof + +:build_debug +echo Building debug version... +if not exist "%OUT_DIR_DEBUG%" mkdir "%OUT_DIR_DEBUG%" +%ODIN% build %SRC_DIR% -out:%OUT_DIR_DEBUG%\%PROGRAM_NAME% --debug -define:GL_DEBUG=true +goto :eof + +:build_release +echo Building release version... +if not exist "%OUT_DIR_RELEASE%" mkdir "%OUT_DIR_RELEASE%" +%ODIN% build %SRC_DIR% -out:%OUT_DIR_RELEASE%\%PROGRAM_NAME% +goto :eof + +:: Clean up build artifacts +clean: +echo Cleaning up build artifacts... +rd /s /q "%OUT_DIR_DEBUG%" +rd /s /q "%OUT_DIR_RELEASE%" + +:: Command to execute clean (optional, uncomment if needed) +:: call :clean + +endlocal diff --git a/res/shaders/colors.fs b/res/shaders/colors.fs index d434370..2184d03 100644 --- a/res/shaders/colors.fs +++ b/res/shaders/colors.fs @@ -8,17 +8,27 @@ in vec3 FragPos; uniform vec3 object_color; uniform vec3 light_color; uniform vec3 light_position; +uniform vec3 view_position; void main() { + // ambient float ambient_stength = 0.1; vec3 ambient = ambient_stength * light_color; + // diffuse 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; + + // specular + float specular_strength = 0.5; + 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), 32); + vec3 specular = specular_strength * spec * light_color; - vec3 result = (ambient + diffuse) * object_color; + vec3 result = (ambient + diffuse + specular) * object_color; FragColor = vec4(result, 1.0); } diff --git a/res/shaders/colors.vs b/res/shaders/colors.vs index 323b2ad..15cc265 100644 --- a/res/shaders/colors.vs +++ b/res/shaders/colors.vs @@ -12,7 +12,7 @@ out vec3 FragPos; void main() { FragPos = vec3(model * vec4(a_pos, 1.0)); - Normal = a_normal; + Normal = mat3(transpose(inverse(model))) * a_normal; gl_Position = projection * view * vec4(FragPos, 1.0); } diff --git a/src/main.odin b/src/main.odin index f09ccb4..bb4803e 100644 --- a/src/main.odin +++ b/src/main.odin @@ -139,47 +139,222 @@ main :: proc() { } vertices: []f32 = { - -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 + -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, } @@ -194,7 +369,7 @@ main :: proc() { gl.STATIC_DRAW, ) - gl.GenVertexArrays(1, &cube_vao) + gl.GenVertexArrays(1, &cube_vao) gl.BindVertexArray(cube_vao) gl.VertexAttribPointer(0, 3, gl.FLOAT, gl.FALSE, 6 * size_of(f32), 0) gl.EnableVertexAttribArray(0) @@ -227,7 +402,7 @@ main :: proc() { light_color := Vec3{1.0, 1.0, 1.0} shader.set_vec3(lighting_shader, cstring("object_color"), &object_color) shader.set_vec3(lighting_shader, cstring("light_color"), &light_color) - shader.set_vec3(lighting_shader, cstring("light_position"), &light_pos) + shader.set_vec3(lighting_shader, cstring("light_position"), &light_pos) aspect: f32 = 800.0 / 600.0 projection := linalg.matrix4_perspective_f32( diff --git a/src/shader/shader.odin b/src/shader/shader.odin index cd798ff..720efde 100644 --- a/src/shader/shader.odin +++ b/src/shader/shader.odin @@ -1,7 +1,6 @@ package shader import "core:math/linalg" -import "core:os" import "core:strings" import gl "vendor:OpenGL" @@ -16,9 +15,6 @@ Vec3 :: linalg.Vector3f32 Mat4 :: linalg.Matrix4x4f32 shader_init :: proc(vsp, fsp: string) -> (^Shader, int) { - assert(os.is_file_path(vsp)) - assert(os.is_file_path(fsp)) - program_id, ok := gl.load_shaders_file(vsp, fsp) if !ok { @@ -58,5 +54,7 @@ set_mat4 :: proc(using shader: ^Shader, name: cstring, value: ^Mat4) { set_value :: proc { set_i32, set_f32, + set_vec3, + set_mat4, set_bool, } -- cgit 1.4.1-2-gfad0