From 7019eba5e9fe2500e30b542e8a982a4edae40940 Mon Sep 17 00:00:00 2001 From: iurii Date: Sat, 7 Sep 2024 13:51:36 +0300 Subject: refactored and wip texture loader --- res/images/container.png | Bin 0 -> 467893 bytes res/shaders/colors.fs | 8 +- res/shaders/colors.vs | 2 + src/camera.odin | 132 +++++++++++++++++++ src/camera/camera.odin | 135 -------------------- src/main.odin | 322 +++++++++++------------------------------------ src/shader.odin | 57 +++++++++ src/shader/shader.odin | 60 --------- src/texture.odin | 12 ++ 9 files changed, 279 insertions(+), 449 deletions(-) create mode 100644 res/images/container.png create mode 100644 src/camera.odin delete mode 100644 src/camera/camera.odin create mode 100644 src/shader.odin delete mode 100644 src/shader/shader.odin create mode 100644 src/texture.odin diff --git a/res/images/container.png b/res/images/container.png new file mode 100644 index 0000000..596e8da Binary files /dev/null and b/res/images/container.png differ diff --git a/res/shaders/colors.fs b/res/shaders/colors.fs index 053e1c4..79e243f 100644 --- a/res/shaders/colors.fs +++ b/res/shaders/colors.fs @@ -1,7 +1,6 @@ #version 330 core struct Material { - vec3 ambient; - vec3 diffuse; + Sampler2D diffuse; vec3 specular; float shininess; }; @@ -17,6 +16,7 @@ out vec4 FragColor; in vec3 Normal; in vec3 FragPos; +in vec3 TexCoords; uniform vec3 light_position; uniform vec3 view_position; @@ -26,13 +26,13 @@ uniform Light light; void main() { // ambient - vec3 ambient = light.ambient * material.ambient; + vec3 ambient = light.ambient * vec3(texture(material.diffuse, TexCoords)); // diffuse vec3 norm = normalize(Normal); vec3 light_dir = normalize(light_position - FragPos); float diff = max(dot(norm, light_dir), 0.0); - vec3 diffuse = light.diffuse * (diff * material.diffuse); + vec3 diffuse = light.diffuse * diff * vec3(texture(material.diffuse, TexCoords)); // specular vec3 view_dir = normalize(view_position - FragPos); diff --git a/res/shaders/colors.vs b/res/shaders/colors.vs index 15cc265..9128abc 100644 --- a/res/shaders/colors.vs +++ b/res/shaders/colors.vs @@ -2,6 +2,7 @@ layout (location = 0) in vec3 a_pos; layout (location = 1) in vec3 a_normal; +layout (location = 2) in vec2 a_tex_coords; uniform mat4 model; uniform mat4 view; @@ -9,6 +10,7 @@ uniform mat4 projection; out vec3 Normal; out vec3 FragPos; +out vec2 TexCoords; void main() { FragPos = vec3(model * vec4(a_pos, 1.0)); diff --git a/src/camera.odin b/src/camera.odin new file mode 100644 index 0000000..d5c1201 --- /dev/null +++ b/src/camera.odin @@ -0,0 +1,132 @@ +package funhalla + +import "core:math" +import la "core:math/linalg" + +CameraMovement :: enum { + FORWARD, + BACKWARD, + LEFT, + RIGHT, +} + +YAW: f32 : -90 +PITCH: f32 : 0.0 +SPEED: f32 : 2.5 +SENSITIVITY: f32 : 0.1 +ZOOM: f32 : 45.0 + + +Camera :: struct { + position: Vec3, + front: Vec3, + up: Vec3, + right: Vec3, + world_up: Vec3, + yaw: f32, + pitch: f32, + move_speed: f32, + mouse_sensitivity: f32, + zoom: f32, +} + + +DEFAULT_POSITION :: Vec3{0.0, 0.0, 0.0} +DEFAULT_UP :: Vec3{0.0, 1.0, 0.0} +DEFAULT_FRONT :: Vec3{0.0, 0.0, -1.0} + +camera_init :: proc( + position: Vec3 = Vec3{0.0, 0.0, 0.0}, + up: Vec3 = DEFAULT_UP, + yaw: f32 = YAW, + pitch: f32 = PITCH, + front: Vec3 = DEFAULT_FRONT, + move_speed: f32 = SPEED, + mouse_sensitivity: f32 = SENSITIVITY, + zoom: f32 = ZOOM, +) -> ^Camera { + c := new(Camera) + c.front = front + c.position = position + c.world_up = up + c.yaw = yaw + c.pitch = pitch + c.up = up + c.mouse_sensitivity = mouse_sensitivity + c.zoom = zoom + c.move_speed = move_speed + _update_camera_vectors(c) + return c +} + +get_view_matrix :: proc(using camera: ^Camera) -> Mat4 { + return la.matrix4_look_at(position, position + front, up) +} + +process_keyboard :: proc(using camera: ^Camera, direction: CameraMovement, dt: f32) { + velocity: f32 = move_speed * dt + + if direction == .FORWARD { + position += front * velocity + } + if direction == .BACKWARD { + position -= front * velocity + } + + if direction == .LEFT { + position -= right * velocity + } + + if direction == .RIGHT { + position += right * velocity + } +} + +process_mouse_move :: proc( + using camera: ^Camera, + xoffset_in, yoffset_in: f32, + constraint_pitch: bool = true, +) { + xoffset: f32 = xoffset_in * mouse_sensitivity + yoffset: f32 = yoffset_in * mouse_sensitivity + + yaw += xoffset + pitch += yoffset + + if constraint_pitch { + if pitch > 89.0 { + pitch = 89.0 + } + + if pitch < -89.0 { + pitch = -89.0 + } + } + + _update_camera_vectors(camera) +} + +process_mouse_scroll :: proc(using camera: ^Camera, yoffset: f32) { + zoom -= yoffset + + if zoom < 1.0 { + zoom = 1.0 + } + + if zoom > 45.0 { + zoom = 45.0 + } +} + + +@(private) +_update_camera_vectors :: proc(using camera: ^Camera) { + + front_x: f32 = math.cos(la.to_radians(yaw)) * math.cos(la.to_radians(pitch)) + front_y: f32 = math.sin(la.to_radians(pitch)) + front_z: f32 = math.sin(la.to_radians(yaw)) * math.cos(la.to_radians(pitch)) + + front = la.vector_normalize(Vec3{front_x, front_y, front_z}) + right = la.vector_normalize(la.vector_cross3(camera.front, camera.world_up)) + up = la.vector_normalize(la.vector_cross3(camera.right, camera.front)) +} diff --git a/src/camera/camera.odin b/src/camera/camera.odin deleted file mode 100644 index 809e3c3..0000000 --- a/src/camera/camera.odin +++ /dev/null @@ -1,135 +0,0 @@ -package camera - -import "core:math" -import la "core:math/linalg" - -Vec3 :: la.Vector3f32 -Mat4 :: la.Matrix4x4f32 - -CameraMovement :: enum { - FORWARD, - BACKWARD, - LEFT, - RIGHT, -} - -YAW: f32 : -90 -PITCH: f32 : 0.0 -SPEED: f32 : 2.5 -SENSITIVITY: f32 : 0.1 -ZOOM: f32 : 45.0 - - -Camera :: struct { - position: Vec3, - front: Vec3, - up: Vec3, - right: Vec3, - world_up: Vec3, - yaw: f32, - pitch: f32, - move_speed: f32, - mouse_sensitivity: f32, - zoom: f32, -} - - -DEFAULT_POSITION :: Vec3{0.0, 0.0, 0.0} -DEFAULT_UP :: Vec3{0.0, 1.0, 0.0} -DEFAULT_FRONT :: Vec3{0.0, 0.0, -1.0} - -camera_init :: proc( - position: Vec3 = Vec3{0.0, 0.0, 0.0}, - up: Vec3 = DEFAULT_UP, - yaw: f32 = YAW, - pitch: f32 = PITCH, - front: Vec3 = DEFAULT_FRONT, - move_speed: f32 = SPEED, - mouse_sensitivity: f32 = SENSITIVITY, - zoom: f32 = ZOOM, -) -> ^Camera { - c := new(Camera) - c.front = front - c.position = position - c.world_up = up - c.yaw = yaw - c.pitch = pitch - c.up = up - c.mouse_sensitivity = mouse_sensitivity - c.zoom = zoom - c.move_speed = move_speed - _update_camera_vectors(c) - return c -} - -get_view_matrix :: proc(using camera: ^Camera) -> Mat4 { - return la.matrix4_look_at(position, position + front, up) -} - -process_keyboard :: proc(using camera: ^Camera, direction: CameraMovement, dt: f32) { - velocity: f32 = move_speed * dt - - if direction == .FORWARD { - position += front * velocity - } - if direction == .BACKWARD { - position -= front * velocity - } - - if direction == .LEFT { - position -= right * velocity - } - - if direction == .RIGHT { - position += right * velocity - } -} - -process_mouse_move :: proc( - using camera: ^Camera, - xoffset_in, yoffset_in: f32, - constraint_pitch: bool = true, -) { - xoffset: f32 = xoffset_in * mouse_sensitivity - yoffset: f32 = yoffset_in * mouse_sensitivity - - yaw += xoffset - pitch += yoffset - - if constraint_pitch { - if pitch > 89.0 { - pitch = 89.0 - } - - if pitch < -89.0 { - pitch = -89.0 - } - } - - _update_camera_vectors(camera) -} - -process_mouse_scroll :: proc(using camera: ^Camera, yoffset: f32) { - zoom -= yoffset - - if zoom < 1.0 { - zoom = 1.0 - } - - if zoom > 45.0 { - zoom = 45.0 - } -} - - -@(private) -_update_camera_vectors :: proc(using camera: ^Camera) { - - front_x: f32 = math.cos(la.to_radians(yaw)) * math.cos(la.to_radians(pitch)) - front_y: f32 = math.sin(la.to_radians(pitch)) - front_z: f32 = math.sin(la.to_radians(yaw)) * math.cos(la.to_radians(pitch)) - - front = la.vector_normalize(Vec3{front_x, front_y, front_z}) - right = la.vector_normalize(la.vector_cross3(camera.front, camera.world_up)) - up = la.vector_normalize(la.vector_cross3(camera.right, camera.front)) -} diff --git a/src/main.odin b/src/main.odin index 67f4c4c..8aa1844 100644 --- a/src/main.odin +++ b/src/main.odin @@ -1,7 +1,6 @@ -package main +package funhalla import gl "vendor:OpenGL" import "vendor:glfw" -import "vendor:stb/image" import "base:intrinsics" import "base:runtime" @@ -11,14 +10,12 @@ import "core:math" import "core:math/linalg" import "core:os" -import cam "camera" -import "shader" +Vec3 :: linalg.Vector3f32 +Mat4 :: linalg.Matrix4x4f32 GL_MAJOR_VERSION :: 3 GL_MINOR_VERSION :: 3 -Vec3 :: linalg.Vector3f32 - delta_time: f32 = 0.0 last_frame: f32 = 0.0 @@ -27,7 +24,7 @@ SCREEN_WIDTH: f32 : 800.0 SCREEN_HEIGTH: f32 : 600.0 -camera := cam.camera_init(Vec3{0.0, 0.0, 3.0}) +camera := camera_init(Vec3{0.0, 0.0, 3.0}) first_mouse := true last_x: f32 = 800.0 / 2.0 @@ -41,7 +38,6 @@ framebuffer_size_callback :: proc "cdecl" (window: glfw.WindowHandle, width, hei mouse_callback :: proc "cdecl" (window: glfw.WindowHandle, xpos_in, ypos_in: f64) { context = runtime.default_context() - using cam xpos: f32 = f32(xpos_in) ypos: f32 = f32(ypos_in) @@ -60,13 +56,11 @@ mouse_callback :: proc "cdecl" (window: glfw.WindowHandle, xpos_in, ypos_in: f64 } mouse_scroll_callback :: proc "cdecl" (window: glfw.WindowHandle, xoffset, yoffset: f64) { - using cam context = runtime.default_context() process_mouse_scroll(camera, f32(yoffset)) } process_input :: proc(window: ^glfw.WindowHandle) { - using cam if glfw.GetKey(window^, glfw.KEY_ESCAPE) == glfw.PRESS { glfw.SetWindowShouldClose(window^, true) } @@ -121,240 +115,66 @@ main :: proc() { glfw.SetScrollCallback(window, mouse_scroll_callback) glfw.SetInputMode(window, glfw.CURSOR, glfw.CURSOR_DISABLED) - light_cube_shader, err := shader.shader_init( + light_cube_shader, err := shader_init( "res/shaders/light_cube.vs", "res/shaders/light_cube.fs", ) - if err == shader.SHADER_LOAD_ERROR { + if err == SHADER_LOAD_ERROR { fmt.eprintln("Could not initialize shader") return } - lighting_shader: ^shader.Shader - lighting_shader, err = shader.shader_init("res/shaders/colors.vs", "res/shaders/colors.fs") + lighting_shader: ^Shader + lighting_shader, err = shader_init("res/shaders/colors.vs", "res/shaders/colors.fs") - if err == shader.SHADER_LOAD_ERROR { + if err == SHADER_LOAD_ERROR { fmt.eprintln("Could not initialize shader") return } 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, + // positions // normals // texture coords + -0.5, -0.5, -0.5, 0.0, 0.0, -1.0, 0.0, 0.0, + 0.5, -0.5, -0.5, 0.0, 0.0, -1.0, 1.0, 0.0, + 0.5, 0.5, -0.5, 0.0, 0.0, -1.0, 1.0, 1.0, + 0.5, 0.5, -0.5, 0.0, 0.0, -1.0, 1.0, 1.0, + -0.5, 0.5, -0.5, 0.0, 0.0, -1.0, 0.0, 1.0, + -0.5, -0.5, -0.5, 0.0, 0.0, -1.0, 0.0, 0.0, + + -0.5, -0.5, 0.5, 0.0, 0.0, 1.0, 0.0, 0.0, + 0.5, -0.5, 0.5, 0.0, 0.0, 1.0, 1.0, 0.0, + 0.5, 0.5, 0.5, 0.0, 0.0, 1.0, 1.0, 1.0, + 0.5, 0.5, 0.5, 0.0, 0.0, 1.0, 1.0, 1.0, + -0.5, 0.5, 0.5, 0.0, 0.0, 1.0, 0.0, 1.0, + -0.5, -0.5, 0.5, 0.0, 0.0, 1.0, 0.0, 0.0, + + -0.5, 0.5, 0.5, -1.0, 0.0, 0.0, 1.0, 0.0, + -0.5, 0.5, -0.5, -1.0, 0.0, 0.0, 1.0, 1.0, + -0.5, -0.5, -0.5, -1.0, 0.0, 0.0, 0.0, 1.0, + -0.5, -0.5, -0.5, -1.0, 0.0, 0.0, 0.0, 1.0, + -0.5, -0.5, 0.5, -1.0, 0.0, 0.0, 0.0, 0.0, + -0.5, 0.5, 0.5, -1.0, 0.0, 0.0, 1.0, 0.0, + + 0.5, 0.5, 0.5, 1.0, 0.0, 0.0, 1.0, 0.0, + 0.5, 0.5, -0.5, 1.0, 0.0, 0.0, 1.0, 1.0, + 0.5, -0.5, -0.5, 1.0, 0.0, 0.0, 0.0, 1.0, + 0.5, -0.5, -0.5, 1.0, 0.0, 0.0, 0.0, 1.0, + 0.5, -0.5, 0.5, 1.0, 0.0, 0.0, 0.0, 0.0, + 0.5, 0.5, 0.5, 1.0, 0.0, 0.0, 1.0, 0.0, + + -0.5, -0.5, -0.5, 0.0, -1.0, 0.0, 0.0, 1.0, + 0.5, -0.5, -0.5, 0.0, -1.0, 0.0, 1.0, 1.0, + 0.5, -0.5, 0.5, 0.0, -1.0, 0.0, 1.0, 0.0, + 0.5, -0.5, 0.5, 0.0, -1.0, 0.0, 1.0, 0.0, + -0.5, -0.5, 0.5, 0.0, -1.0, 0.0, 0.0, 0.0, + -0.5, -0.5, -0.5, 0.0, -1.0, 0.0, 0.0, 1.0, + + -0.5, 0.5, -0.5, 0.0, 1.0, 0.0, 0.0, 1.0, + 0.5, 0.5, -0.5, 0.0, 1.0, 0.0, 1.0, 1.0, + 0.5, 0.5, 0.5, 0.0, 1.0, 0.0, 1.0, 0.0, + 0.5, 0.5, 0.5, 0.0, 1.0, 0.0, 1.0, 0.0, + -0.5, 0.5, 0.5, 0.0, 1.0, 0.0, 0.0, 0.0, + -0.5, 0.5, -0.5, 0.0, 1.0, 0.0, 0.0, 1.0 } @@ -371,15 +191,17 @@ main :: proc() { gl.GenVertexArrays(1, &cube_vao) gl.BindVertexArray(cube_vao) - gl.VertexAttribPointer(0, 3, gl.FLOAT, gl.FALSE, 6 * size_of(f32), 0) + gl.VertexAttribPointer(0, 3, gl.FLOAT, gl.FALSE, 8 * size_of(f32), 0) gl.EnableVertexAttribArray(0) - gl.VertexAttribPointer(1, 3, gl.FLOAT, gl.FALSE, 6 * size_of(f32), 3 * size_of(f32)) + gl.VertexAttribPointer(1, 3, gl.FLOAT, gl.FALSE, 8 * size_of(f32), 3 * size_of(f32)) gl.EnableVertexAttribArray(1) + gl.VertexAttribPointer(2, 2, gl.FLOAT, gl.FALSE, 8 * size_of(f32), 6 * size_of(f32)); + gl.EnableVertexAttribArray(2) 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, 6 * size_of(f32), 0) + gl.VertexAttribPointer(0, 3, gl.FLOAT, gl.FALSE, 8 * size_of(f32), 0) gl.EnableVertexAttribArray(0) //gl.PolygonMode(gl.FRONT_AND_BACK, gl.LINE) @@ -397,7 +219,7 @@ main :: proc() { gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT) - shader.use(lighting_shader) + shader_use(lighting_shader) light_color := Vec3 { cast(f32)math.sin(glfw.GetTime() * 2.0), @@ -407,20 +229,20 @@ main :: proc() { material_ambient := Vec3{1.0, 0.5, 0.31} material_diffuse := 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.diffuse"), &material_diffuse) - shader.set_vec3(lighting_shader, cstring("material.specular"), &material_specular) - shader.set_f32(lighting_shader, cstring("material.shininess"), 32.0) + shader_set_vec3(lighting_shader, cstring("material.ambient"), &material_ambient) + shader_set_vec3(lighting_shader, cstring("material.diffuse"), &material_diffuse) + 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_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) - shader.set_vec3(lighting_shader, cstring("light.specular"), &light_specular) + shader_set_vec3(lighting_shader, cstring("light.ambient"), &light_ambient) + shader_set_vec3(lighting_shader, cstring("light.diffuse"), &light_diffuse) + shader_set_vec3(lighting_shader, cstring("light.specular"), &light_specular) - shader.set_vec3(lighting_shader, cstring("light_position"), &light_pos) - shader.set_vec3(lighting_shader, cstring("view_position"), &camera.position) + shader_set_vec3(lighting_shader, cstring("light_position"), &light_pos) + shader_set_vec3(lighting_shader, cstring("view_position"), &camera.position) aspect: f32 = 800.0 / 600.0 projection := linalg.matrix4_perspective_f32( @@ -430,7 +252,7 @@ main :: proc() { 100.0, ) - view := cam.get_view_matrix(camera) + view := get_view_matrix(camera) view_location := gl.GetUniformLocation(lighting_shader.id, "view") gl.UniformMatrix4fv(view_location, 1, gl.FALSE, &view[0][0]) @@ -439,19 +261,19 @@ main :: proc() { gl.UniformMatrix4fv(projection_location, 1, gl.FALSE, &projection[0][0]) model := linalg.MATRIX4F32_IDENTITY - shader.set_mat4(lighting_shader, cstring("model"), &model) + shader_set_mat4(lighting_shader, cstring("model"), &model) gl.BindVertexArray(cube_vao) gl.DrawArrays(gl.TRIANGLES, 0, 36) // lamp cube object drawing - shader.use(light_cube_shader) - shader.set_mat4(light_cube_shader, cstring("projection"), &projection) - shader.set_mat4(light_cube_shader, cstring("view"), &view) + shader_use(light_cube_shader) + shader_set_mat4(light_cube_shader, cstring("projection"), &projection) + shader_set_mat4(light_cube_shader, cstring("view"), &view) model = linalg.matrix4_translate_f32(light_pos) model *= linalg.matrix4_scale_f32(Vec3{0.2, 0.2, 0.2}) - shader.set_mat4(light_cube_shader, cstring("model"), &model) + shader_set_mat4(light_cube_shader, cstring("model"), &model) gl.BindVertexArray(light_cube_vao) gl.DrawArrays(gl.TRIANGLES, 0, 36) diff --git a/src/shader.odin b/src/shader.odin new file mode 100644 index 0000000..cfee01f --- /dev/null +++ b/src/shader.odin @@ -0,0 +1,57 @@ +package funhalla + +import "core:math/linalg" +import "core:strings" +import gl "vendor:OpenGL" + +Shader :: struct { + id: u32, +} + +SHADER_LOAD_ERROR :: -1 +SHADER_OK :: 0 + +shader_init :: proc(vsp, fsp: string) -> (^Shader, int) { + program_id, ok := gl.load_shaders_file(vsp, fsp) + + if !ok { + return nil, SHADER_LOAD_ERROR + } + + shader := new(Shader) + shader.id = program_id + + return shader, SHADER_OK +} + +shader_use :: proc(using shader: ^Shader) { + gl.UseProgram(id) +} + +shader_set_bool :: proc(using shader: ^Shader, name: cstring, value: bool) { + gl.Uniform1i(gl.GetUniformLocation(id, name), i32(value)) +} + +shader_set_i32 :: proc(using shader: ^Shader, name: cstring, value: i32) { + gl.Uniform1i(gl.GetUniformLocation(id, name), value) +} + +shader_set_f32 :: proc(using shader: ^Shader, name: cstring, value: f32) { + gl.Uniform1f(gl.GetUniformLocation(id, name), value) +} + +shader_set_vec3 :: proc(using shader: ^Shader, name: cstring, value: ^Vec3) { + gl.Uniform3fv(gl.GetUniformLocation(id, name), 1, &value[0]) +} + +shader_set_mat4 :: proc(using shader: ^Shader, name: cstring, value: ^Mat4) { + gl.UniformMatrix4fv(gl.GetUniformLocation(id, name), 1, gl.FALSE, &value[0][0]) +} + +shader_set_value :: proc { + shader_set_i32, + shader_set_f32, + shader_set_vec3, + shader_set_mat4, + shader_set_bool, +} diff --git a/src/shader/shader.odin b/src/shader/shader.odin deleted file mode 100644 index 720efde..0000000 --- a/src/shader/shader.odin +++ /dev/null @@ -1,60 +0,0 @@ -package shader - -import "core:math/linalg" -import "core:strings" -import gl "vendor:OpenGL" - -Shader :: struct { - id: u32, -} - -SHADER_LOAD_ERROR :: -1 -SHADER_OK :: 0 - -Vec3 :: linalg.Vector3f32 -Mat4 :: linalg.Matrix4x4f32 - -shader_init :: proc(vsp, fsp: string) -> (^Shader, int) { - program_id, ok := gl.load_shaders_file(vsp, fsp) - - if !ok { - return nil, SHADER_LOAD_ERROR - } - - shader := new(Shader) - shader.id = program_id - - return shader, SHADER_OK -} - -use :: proc(using shader: ^Shader) { - gl.UseProgram(id) -} - -set_bool :: proc(using shader: ^Shader, name: cstring, value: bool) { - gl.Uniform1i(gl.GetUniformLocation(id, name), i32(value)) -} - -set_i32 :: proc(using shader: ^Shader, name: cstring, value: i32) { - gl.Uniform1i(gl.GetUniformLocation(id, name), value) -} - -set_f32 :: proc(using shader: ^Shader, name: cstring, value: f32) { - gl.Uniform1f(gl.GetUniformLocation(id, name), value) -} - -set_vec3 :: proc(using shader: ^Shader, name: cstring, value: ^Vec3) { - gl.Uniform3fv(gl.GetUniformLocation(id, name), 1, &value[0]) -} - -set_mat4 :: proc(using shader: ^Shader, name: cstring, value: ^Mat4) { - gl.UniformMatrix4fv(gl.GetUniformLocation(id, name), 1, gl.FALSE, &value[0][0]) -} - -set_value :: proc { - set_i32, - set_f32, - set_vec3, - set_mat4, - set_bool, -} diff --git a/src/texture.odin b/src/texture.odin new file mode 100644 index 0000000..45edf56 --- /dev/null +++ b/src/texture.odin @@ -0,0 +1,12 @@ +package funhalla + +import gl "vendor:OpenGL" +import "vendor:stb/image" + +load_texture :: proc(path: cstring) { + texture_id : u32 + + gl.GenTextures(1, &texture_id) + + width, height, nr_components : u32 +} -- cgit 1.4.1-2-gfad0