From ffd24338b945ea2adf8a3c9ce217407da980b3a0 Mon Sep 17 00:00:00 2001 From: iamcheeseman <[hidden email]> Date: Fri, 6 Feb 2026 22:24:12 -0500 Subject: Fix many a issue --- src/fw/fw.odin | 5 +++ src/fw/input.odin | 4 +- src/fw/opengl.odin | 4 +- src/fw/renderer.odin | 108 ++++++++++++++++++++++++++++++++++++++++++++++----- src/fw/shader.frag | 6 +-- 5 files changed, 110 insertions(+), 17 deletions(-) (limited to 'src/fw') diff --git a/src/fw/fw.odin b/src/fw/fw.odin index 86a3e3a..556817b 100644 --- a/src/fw/fw.odin +++ b/src/fw/fw.odin @@ -1,6 +1,7 @@ package fw import "core:log" +import "core:math/linalg" import "vendor:glfw" @@ -77,7 +78,11 @@ next_frame :: proc() -> bool { new_batch(renderer.white_1x1.handle, .None, false) } + view := renderer.view + renderer.view = linalg.MATRIX4F32_IDENTITY renderer.vt.end_frame() + renderer.view = view + new_batch(renderer.white_1x1.handle, .None, false) renderer.draw_calls_count = renderer._draw_call_counter diff --git a/src/fw/input.odin b/src/fw/input.odin index a3cbe25..2c15017 100644 --- a/src/fw/input.odin +++ b/src/fw/input.odin @@ -68,9 +68,9 @@ get_mouse_pos :: proc() -> (mouse_pos: Vec2) { w_width, w_height := glfw.GetWindowSize(window) mx, my := glfw.GetCursorPos(window) - mouse_pos = Vec2{f32(mx), f32(mx)} + mouse_pos = Vec2{f32(mx), f32(my)} mouse_pos /= Vec2{f32(w_width), f32(w_height)} mouse_pos *= Vec2{f32(framebuf.color.size.x), f32(framebuf.color.size.y)} - // mouse_pos += state.camera.target + mouse_pos += renderer.camera_pos return } diff --git a/src/fw/opengl.odin b/src/fw/opengl.odin index ab2bb81..3419b48 100644 --- a/src/fw/opengl.odin +++ b/src/fw/opengl.odin @@ -128,8 +128,8 @@ gl_init :: proc() { gl.Enable(gl.DEPTH_TEST) gl.DepthFunc(gl.LEQUAL) - // gl.Enable(gl.BLEND); - // gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA); + gl.Enable(gl.BLEND); + gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA); renderer.vt.start_frame = gl_start_frame renderer.vt.end_frame = gl_end_frame diff --git a/src/fw/renderer.odin b/src/fw/renderer.odin index 082a0d3..72ec3e1 100644 --- a/src/fw/renderer.odin +++ b/src/fw/renderer.odin @@ -6,6 +6,10 @@ import "core:math" import "core:math/linalg" import "core:math/linalg/glsl" +WHITE :: Color{1, 1, 1, 1} +BLACK :: Color{0, 0, 0, 1} +TRANSPARENT :: Color{0, 0, 0, 0} + Index :: u16 Renderer_Backend :: enum { @@ -82,6 +86,8 @@ renderer: struct { projection: Mat4, view: Mat4, + camera_pos: Vec2, + draw_calls_count: i32, _draw_call_counter: i32, @@ -90,7 +96,7 @@ renderer: struct { @(private) _renderer_init :: proc(config: Config) { - renderer.bg_col = Color{0.2, 0.2, 0.2, 1} + renderer.bg_col = BLACK renderer.projection = linalg.matrix_ortho3d(f32(0), 800, 800, 0, 0, 1) renderer.view = linalg.identity(Mat4) @@ -116,6 +122,15 @@ _renderer_deinit :: proc() { } } +set_camera_pos :: proc(pos: Vec2) { + renderer.camera_pos = pos + renderer.view = linalg.matrix4_translate(-Vec3{pos.x, pos.y, 0}) +} + +get_camera_pos :: #force_inline proc() -> Vec2 { + return renderer.camera_pos +} + flush_batch :: proc(draw_call: ^Draw_Call) { renderer.vt.draw(draw_call^) renderer._draw_call_counter += 1 @@ -196,7 +211,7 @@ draw_line :: proc( start: Vec2, end: Vec2, depth: f32 = 0, - color := Color{1, 1, 1, 1}, + color := WHITE, ) { try_batch_calls(renderer.white_1x1.handle, .Lines, true) @@ -212,11 +227,14 @@ draw_line :: proc( _add_index(start_idx, 1) } -draw_rect :: proc( +draw_rect_gradient :: proc( pos: Vec2, size: Vec2, + tl_color: Color, + tr_color: Color, + bl_color: Color, + br_color: Color, depth: f32 = 0, - color := Color{1, 1, 1, 1}, tex := renderer.white_1x1, lines := false, ) { @@ -234,10 +252,10 @@ draw_rect :: proc( pos := linalg.round(pos) - tl := Vertex{{pos.x, pos.y, depth}, {0, 0}, color} - tr := Vertex{{pos.x + size.x, pos.y, depth}, {1, 0}, color} - bl := Vertex{{pos.x, pos.y + size.y, depth}, {0, 1}, color} - br := Vertex{{pos.x + size.x, pos.y + size.y, depth}, {1, 1}, color} + tl := Vertex{{pos.x, pos.y, depth}, {0, 0}, tl_color} + tr := Vertex{{pos.x + size.x, pos.y, depth}, {1, 0}, tr_color} + bl := Vertex{{pos.x, pos.y + size.y, depth}, {0, 1}, bl_color} + br := Vertex{{pos.x + size.x, pos.y + size.y, depth}, {1, 1}, br_color} start := Index(len(renderer.draw_call.vertices)) @@ -271,13 +289,83 @@ draw_rect :: proc( } } +draw_rect :: proc( + pos: Vec2, + size: Vec2, + depth: f32 = 0, + color := WHITE, + tex := renderer.white_1x1, + lines := false, +) { + draw_rect_gradient( + pos, + size, + color, + color, + color, + color, + depth, + tex, + lines, + ) + // try_batch_calls(tex.handle, .Triangles if !lines else .Lines, true) + // + // depth := depth + // + // if depth < -1 || depth > 1 { + // log.warnf( + // "Depth is out of range (%v). It will be clamped to -1..1.", + // depth + // ) + // depth = math.clamp(depth, -1, 1) + // } + // + // pos := linalg.round(pos) + // + // tl := Vertex{{pos.x, pos.y, depth}, {0, 0}, color} + // tr := Vertex{{pos.x + size.x, pos.y, depth}, {1, 0}, color} + // bl := Vertex{{pos.x, pos.y + size.y, depth}, {0, 1}, color} + // br := Vertex{{pos.x + size.x, pos.y + size.y, depth}, {1, 1}, color} + // + // start := Index(len(renderer.draw_call.vertices)) + // + // reserve(&renderer.draw_call.vertices, len(renderer.draw_call.vertices) + 4) + // _add_vertex(tl) + // _add_vertex(tr) + // _add_vertex(bl) + // _add_vertex(br) + // + // + // if lines { + // reserve(&renderer.draw_call.indices, len(renderer.draw_call.indices) + 8) + // + // _add_index(start, 0) + // _add_index(start, 1) + // _add_index(start, 1) + // _add_index(start, 3) + // _add_index(start, 3) + // _add_index(start, 2) + // _add_index(start, 2) + // _add_index(start, 0) + // } else { + // reserve(&renderer.draw_call.indices, len(renderer.draw_call.indices) + 6) + // + // _add_index(start, 0) + // _add_index(start, 1) + // _add_index(start, 2) + // _add_index(start, 2) + // _add_index(start, 1) + // _add_index(start, 3) + // } +} + draw_tex :: proc( tex: Texture, pos: Vec2, depth: f32 = 0, scale := Vec2{1, 1}, offset := Vec2{0, 0}, - color := Color{1, 1, 1, 1} + color := WHITE, ) { draw_rect( pos = pos - offset, @@ -337,7 +425,7 @@ draw_tex_ex :: proc( offset := Vec2{0, 0}, shear := Vec2{0, 0}, rect: Maybe(Rect) = nil, - color := Color{1, 1, 1, 1} + color := WHITE, ) { tex_size := Vec2{f32(tex.size.x), f32(tex.size.y)} rect := rect.? or_else Rect{{0, 0}, tex_size} diff --git a/src/fw/shader.frag b/src/fw/shader.frag index fdc8796..14abd3c 100644 --- a/src/fw/shader.frag +++ b/src/fw/shader.frag @@ -9,8 +9,8 @@ uniform sampler2D tex0; void main() { vec4 color = texture(tex0, f_uv) * f_col; - if (color.a <= 0.5) { - discard; - } + // if (color.a <= 0.5) { + // discard; + // } final_color = color; } -- cgit v1.3-2-g0d8e