Skip to content

Commit

Permalink
Fix calculating aspect ratio
Browse files Browse the repository at this point in the history
  • Loading branch information
thetarnav committed Apr 6, 2024
1 parent 86f1279 commit 035ba56
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 14 deletions.
16 changes: 7 additions & 9 deletions example/boxes.odin
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,21 @@ import gl "../wasm/webgl"
@(private="file") BOXES_ROWS :: 3
@(private="file") BOXES_AMOUNT :: BOXES_ROWS * BOXES_ROWS * BOXES_ROWS

@(private="file") state: struct {
@(private="file") boxes_state: struct {
rotation: [2]f32,
a_position: i32,
a_color: i32,
u_matrix: i32,
vao: VAO,
}

boxes_start :: proc(program: gl.Program) {
using state
using boxes_state

vao = gl.CreateVertexArray()
gl.BindVertexArray(vao)

a_position = gl.GetAttribLocation (program, "a_position")
a_color = gl.GetAttribLocation (program, "a_color")
u_matrix = gl.GetUniformLocation(program, "u_matrix")
a_position := gl.GetAttribLocation (program, "a_position")
a_color := gl.GetAttribLocation (program, "a_color")
u_matrix = gl.GetUniformLocation(program, "u_matrix")

gl.EnableVertexAttribArray(a_position)
gl.EnableVertexAttribArray(a_color)
Expand Down Expand Up @@ -59,7 +57,7 @@ boxes_start :: proc(program: gl.Program) {
}

boxes_frame :: proc(delta: f32) {
using state
using boxes_state

gl.BindVertexArray(vao)

Expand All @@ -73,7 +71,7 @@ boxes_frame :: proc(delta: f32) {
mat: Mat4 = 1
mat *= glm.mat4PerspectiveInfinite(
fovy = glm.radians_f32(80),
aspect = f32(canvas_res.x / canvas_res.y),
aspect = aspect_ratio,
near = 1,
)
mat *= glm.mat4Translate({0, 0, -1000 + scale * 800})
Expand Down
2 changes: 1 addition & 1 deletion example/look_at.odin
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ look_at_frame :: proc(delta: f32) {

mat := glm.mat4PerspectiveInfinite(
fovy = radians(80),
aspect = f32(canvas_res.x / canvas_res.y),
aspect = aspect_ratio,
near = 1,
)
mat *= camera_mat
Expand Down
11 changes: 7 additions & 4 deletions example/setup.odin
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ canvas_size: [2]f32
window_size: [2]f32
mouse_pos: [2]f32
dpr: f32
aspect_ratio: f32

scale: f32 = 0.5

Example_Kind :: enum {
Expand Down Expand Up @@ -118,10 +120,11 @@ on_wheel :: proc(e: dom.Event) {
}
@(export)
on_window_resize :: proc "contextless" (vw, vh, cw, ch, cx, cy: f32) {
window_size = {vw, vh}
canvas_size = {cw, ch}
canvas_pos = {cx, cy}
canvas_res = cast_vec2(i32, canvas_size * dpr)
window_size = {vw, vh}
canvas_size = {cw, ch}
canvas_pos = {cx, cy}
canvas_res = cast_vec2(i32, canvas_size * dpr)
aspect_ratio = canvas_size.x / canvas_size.y
}

@(export)
Expand Down

0 comments on commit 035ba56

Please sign in to comment.