diff options
Diffstat (limited to '')
-rw-r--r-- | src/camera.odin (renamed from src/camera/camera.odin) | 5 | ||||
-rw-r--r-- | src/main.odin | 322 | ||||
-rw-r--r-- | src/shader.odin (renamed from src/shader/shader.odin) | 29 | ||||
-rw-r--r-- | src/texture.odin | 12 |
4 files changed, 98 insertions, 270 deletions
diff --git a/src/camera/camera.odin b/src/camera.odin index 809e3c3..d5c1201 100644 --- a/src/camera/camera.odin +++ b/src/camera.odin @@ -1,11 +1,8 @@ -package camera +package funhalla import "core:math" import la "core:math/linalg" -Vec3 :: la.Vector3f32 -Mat4 :: la.Matrix4x4f32 - CameraMovement :: enum { FORWARD, BACKWARD, 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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.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/shader.odin b/src/shader.odin index 720efde..cfee01f 100644 --- a/src/shader/shader.odin +++ b/src/shader.odin @@ -1,4 +1,4 @@ -package shader +package funhalla import "core:math/linalg" import "core:strings" @@ -11,9 +11,6 @@ Shader :: struct { 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) @@ -27,34 +24,34 @@ shader_init :: proc(vsp, fsp: string) -> (^Shader, int) { return shader, SHADER_OK } -use :: proc(using shader: ^Shader) { +shader_use :: proc(using shader: ^Shader) { gl.UseProgram(id) } -set_bool :: proc(using shader: ^Shader, name: cstring, value: bool) { +shader_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) { +shader_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) { +shader_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) { +shader_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) { +shader_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, +shader_set_value :: proc { + shader_set_i32, + shader_set_f32, + shader_set_vec3, + shader_set_mat4, + shader_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 +} |