Skip to content

Commit

Permalink
The perspective actually works?
Browse files Browse the repository at this point in the history
  • Loading branch information
thetarnav committed Apr 4, 2024
1 parent 76c0681 commit f0e7df1
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 33 deletions.
34 changes: 17 additions & 17 deletions example/boxes.odin
Original file line number Diff line number Diff line change
Expand Up @@ -57,52 +57,52 @@ cube_colors: [CUBE_VERTICES]RGBA = {

cube_positions: [CUBE_VERTICES]Vec = {
{0, 0, 0}, // 0
{0, 0, 1},
{1, 0, 0},
{0, 0, 1},

{0, 0, 1}, // 1
{1, 0, 1},
{1, 0, 0},
{1, 0, 1},

{0, 0, 1}, // 2
{0, 1, 1},
{1, 0, 1},
{0, 1, 1},

{0, 1, 1}, // 3
{1, 1, 1},
{1, 0, 1},
{1, 1, 1},

{0, 0, 0}, // 4
{0, 1, 1},
{0, 0, 1},
{0, 1, 1},

{0, 0, 0}, // 5
{0, 1, 0},
{0, 1, 1},
{0, 1, 0},

{1, 0, 0}, // 6
{1, 0, 1},
{1, 1, 1},
{1, 0, 1},

{1, 0, 0}, // 7
{1, 1, 1},
{1, 1, 0},
{1, 1, 1},

{0, 0, 0}, // 8
{1, 0, 0},
{1, 1, 0},
{1, 0, 0},

{0, 0, 0}, // 9
{1, 1, 0},
{0, 1, 0},
{1, 1, 0},

{0, 1, 0}, // 10
{1, 1, 1},
{0, 1, 1},
{1, 1, 1},

{0, 1, 0}, // 11
{1, 1, 0},
{1, 1, 1},
{1, 1, 0},
}

write_cube_positions :: proc(dst: []Vec, x, y, z, h: f32) {
Expand Down Expand Up @@ -187,17 +187,17 @@ boxes_frame :: proc(delta: f32) {
// far = 2000,
// )
mat := mat4_perspective(
fov = glm.radians_f32(60),
fov = glm.radians_f32(80),
aspect = f32(canvas_res.x / canvas_res.y),
near = -400,
far = 400,
near = 1,
far = 1000,
)
// mat *= glm.mat4Translate({0, 0, 99900})
mat *= glm.mat4Translate({0, 0, -400})
// mat *= glm.mat4Translate(vec2_to_vec3(window_size / 2 - canvas_pos))
// mat *= glm.mat4Translate(vec2_to_vec3(mouse_pos - canvas_pos))
mat *= glm.mat4Scale(scale)
mat *= mat4_rotate_y(rotation_y)
mat *= mat4_rotate_x(-rotation_x)
mat *= mat4_rotate_x(rotation_x)

gl.UniformMatrix4fv(u_matrix, mat)

Expand Down
8 changes: 5 additions & 3 deletions example/boxes_fs.glsl
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
#version 300 es

precision mediump float;
precision highp float;

// the varied color passed from the vertex shader
in vec4 v_color;

// we need to declare an output for the fragment shader
out vec4 out_color;

void main() {
out_color = v_color;
}
out_color = v_color;
}
17 changes: 9 additions & 8 deletions example/boxes_vs.glsl
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
#version 300 es
// will pass vec3, but will be converted to vec4 with w = 1.0

// an attribute is an input (in) to a vertex shader.
// It will receive data from a buffer
in vec4 a_position;
in vec4 a_color;

// A matrix to transform the positions by
uniform mat4 u_matrix;

// a varying the color to the fragment shader
out vec4 v_color;

void main() {
// Multiply the position by the matrix.
vec4 position = u_matrix * a_position;

// apply "perspective"
gl_Position = vec4(position.xyz, 1.0 + position.z * 2.0);
gl_Position = u_matrix * a_position;

v_color = a_color;
}
// Pass the color to the fragment shader.
v_color = a_color;
}
8 changes: 4 additions & 4 deletions example/pyramid.odin
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,20 @@ to show the front-face.
}
@(private="file") positions: [VERTICES*3]f32 = {
0, 0, SIDE/2,
-SIDE/2, H, 0,
SIDE/2, H, 0,
-SIDE/2, H, 0,

0, 0, -SIDE/2,
-SIDE/2, H, 0,
SIDE/2, H, 0,
-SIDE/2, H, 0,

0, 0, -SIDE/2,
0, 0, SIDE/2,
0, 0, -SIDE/2,
SIDE/2, H, 0,

-SIDE/2, H, 0,
0, 0, SIDE/2,
0, 0, -SIDE/2,
0, 0, SIDE/2,
}

@(private="file") state: struct {
Expand Down
2 changes: 1 addition & 1 deletion example/utils.odin
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ mat4_rotate_z :: proc "contextless" (radians: f32) -> glm.mat4 {
}
@(require_results)
mat4_perspective :: proc "contextless" (fov, aspect, near, far: f32) -> glm.mat4 {
f : f32 = glm.tan(glm.PI*0.5 - fov*0.5)
f : f32 = glm.tan(fov*0.5)
range: f32 = 1.0 / (near - far)

return {
Expand Down

0 comments on commit f0e7df1

Please sign in to comment.