Skip to content

Commit

Permalink
Typesafe uniform utils
Browse files Browse the repository at this point in the history
  • Loading branch information
thetarnav committed Apr 23, 2024
1 parent 73c0f62 commit daa3a32
Show file tree
Hide file tree
Showing 9 changed files with 207 additions and 123 deletions.
4 changes: 2 additions & 2 deletions example/boxes.odin
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ setup_boxes :: proc(s: ^State_Boxes, program: gl.Program) {
gl.Enable(gl.CULL_FACE) // don't draw back faces
gl.Enable(gl.DEPTH_TEST) // draw only closest faces

positions: [BOXES_AMOUNT * CUBE_VERTICES]Vec
positions: [BOXES_AMOUNT * CUBE_VERTICES]vec3
colors : [BOXES_AMOUNT * CUBE_VERTICES]RGBA

for i in 0..<BOXES_AMOUNT {
Expand Down Expand Up @@ -87,7 +87,7 @@ frame_boxes :: proc(s: ^State_Boxes, delta: f32) {

s.rotation -= 0.01 * delta * mouse_rel.yx

mat: Mat4 = 1
mat: mat4 = 1
mat *= glm.mat4PerspectiveInfinite(
fovy = glm.radians_f32(80),
aspect = aspect_ratio,
Expand Down
6 changes: 3 additions & 3 deletions example/camera.odin
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ setup_camera :: proc(s: ^State_Camera, program: gl.Program) {
gl.Enable(gl.CULL_FACE) // don't draw back faces
gl.Enable(gl.DEPTH_TEST) // draw only closest faces

positions: [ALL_VERTICES]Vec
positions: [ALL_VERTICES]vec3
colors : [ALL_VERTICES]RGBA

/* Pyramids */
Expand Down Expand Up @@ -82,7 +82,7 @@ frame_camera :: proc(s: ^State_Camera, delta: f32) {
// Clear the canvas AND the depth buffer.
gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)

camera_mat: Mat4 = 1
camera_mat: mat4 = 1
camera_mat *= mat4_translate({0, 0, 800 - 700 * (scale/1.2)})
camera_mat = glm.inverse_mat4(camera_mat)

Expand All @@ -96,7 +96,7 @@ frame_camera :: proc(s: ^State_Camera, delta: f32) {
s.rotation += 0.01 * delta * mouse_rel.x
elevation := 300 * -(mouse_rel.y - 0.5)

cube_pos: Vec
cube_pos: vec3
cube_pos.y = elevation
cube_pos.x = CUBE_RADIUS * cos(s.rotation)
cube_pos.z = CUBE_RADIUS * sin(s.rotation)
Expand Down
16 changes: 8 additions & 8 deletions example/lighting.odin
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ State_Lighting :: struct {
u_light_dir: i32,
u_light_color: i32,
vao: VAO,
positions: [ALL_VERTICES]Vec,
normals: [ALL_VERTICES]Vec,
positions: [ALL_VERTICES]vec3,
normals: [ALL_VERTICES]vec3,
colors: [ALL_VERTICES]RGBA,
}

Expand Down Expand Up @@ -61,7 +61,7 @@ setup_lighting :: proc(s: ^State_Lighting, program: gl.Program) {

/* Cube */
copy_array(s.positions[:], get_cube_positions(0, CUBE_HEIGHT))
cube_normals: [CUBE_VERTICES]Vec = 1
cube_normals: [CUBE_VERTICES]vec3 = 1
copy_array(s.normals[:], cube_normals)
slice.fill(s.colors[:], WHITE)

Expand Down Expand Up @@ -107,7 +107,7 @@ setup_lighting :: proc(s: ^State_Lighting, program: gl.Program) {
in_x1 := cos(b) * (radius-RING_LENGTH)
in_z1 := sin(b) * (radius-RING_LENGTH)

positions: []Vec = {
positions: []vec3 = {
/* Side */
{out_x0, -RING_HEIGHT/2, out_z0},
{out_x1, -RING_HEIGHT/2, out_z1},
Expand Down Expand Up @@ -162,7 +162,7 @@ frame_lighting :: proc(s: ^State_Lighting, delta: f32) {
// Clear the canvas AND the depth buffer.
gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)

camera_mat: Mat4 = 1
camera_mat: mat4 = 1
camera_mat *= mat4_translate({0, 0, 800 - 800 * scale})
camera_mat = glm.inverse_mat4(camera_mat)

Expand All @@ -178,12 +178,12 @@ frame_lighting :: proc(s: ^State_Lighting, delta: f32) {
/* Draw cube */
s.cube_angle += 0.01 * delta * mouse_rel.x

cube_pos: Vec
cube_pos: vec3
cube_pos.y = 500 * -mouse_rel.y
cube_pos.x = CUBE_RADIUS * cos(s.cube_angle)
cube_pos.z = CUBE_RADIUS * sin(s.cube_angle)

cube_mat: Mat4 = 1
cube_mat: mat4 = 1
cube_mat *= mat4_translate(cube_pos)
cube_mat *= mat4_rotate_y(s.cube_angle)

Expand All @@ -198,7 +198,7 @@ frame_lighting :: proc(s: ^State_Lighting, delta: f32) {
s.ring_angle += 0.002 * delta

for i in 0..<RINGS {
ring_mat: Mat4 = 1
ring_mat: mat4 = 1
ring_mat *= mat4_rotate_z(2*PI / (f32(RINGS)/f32(i)) + s.ring_angle/4)
ring_mat *= mat4_rotate_x(s.ring_angle)

Expand Down
2 changes: 1 addition & 1 deletion example/rectangle.odin
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ frame_rectangle :: proc(s: ^State_Rectangle, delta: f32) {

s.rotation -= 0.01 * delta * mouse_rel.x

mat: Mat3 = 1
mat: mat3 = 1
mat *= mat3_projection(canvas_size)
mat *= mat3_translate(mouse_pos - canvas_pos)
mat *= mat3_scale(scale*2 + 0.4)
Expand Down
60 changes: 30 additions & 30 deletions example/shapes.odin
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ BLACK :: RGBA{ 0, 0, 0, 255}
CUBE_TRIANGLES :: 6 * 2
CUBE_VERTICES :: CUBE_TRIANGLES * 3

CUBE_POSITIONS: [CUBE_VERTICES]Vec : {
CUBE_POSITIONS: [CUBE_VERTICES]vec3 : {
{0, 0, 0}, // 0
{1, 0, 0},
{0, 0, 1},
Expand Down Expand Up @@ -60,48 +60,48 @@ CUBE_POSITIONS: [CUBE_VERTICES]Vec : {
{0, 1, 0}, // 10
{0, 1, 1},
{1, 1, 1},

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

CUBE_NORMALS: [CUBE_VERTICES]Vec : {
CUBE_NORMALS: [CUBE_VERTICES]vec3 : {
{0, -1, 0}, // 0
{0, -1, 0},
{0, -1, 0},
{0, -1, 0}, // 1
{0, -1, 0},
{0, -1, 0},

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

{-1, 0, 0}, // 4
{-1, 0, 0},
{-1, 0, 0},
{-1, 0, 0}, // 5
{-1, 0, 0},
{-1, 0, 0},

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

{0, 0, -1}, // 8
{0, 0, -1},
{0, 0, -1},
{0, 0, -1}, // 9
{0, 0, -1},
{0, 0, -1},

{0, 1, 0}, // 10
{0, 1, 0},
{0, 1, 0},
Expand All @@ -110,7 +110,7 @@ CUBE_NORMALS: [CUBE_VERTICES]Vec : {
{0, 1, 0},
}

get_cube_positions :: proc(pos: Vec = 0, h: f32 = 1) -> [CUBE_VERTICES]Vec {
get_cube_positions :: proc(pos: vec3 = 0, h: f32 = 1) -> [CUBE_VERTICES]vec3 {
positions := CUBE_POSITIONS
for &vec in positions {
vec = pos + (vec - {0.5, 0.5, 0.5}) * h
Expand All @@ -122,7 +122,7 @@ get_cube_positions :: proc(pos: Vec = 0, h: f32 = 1) -> [CUBE_VERTICES]Vec {
PYRAMID_TRIANGLES :: 6
PYRAMID_VERTICES :: PYRAMID_TRIANGLES * 3

get_pyramid_positions :: proc(pos: Vec = 0, h: f32 = 1) -> [PYRAMID_VERTICES]Vec {
get_pyramid_positions :: proc(pos: vec3 = 0, h: f32 = 1) -> [PYRAMID_VERTICES]vec3 {
x := pos.x - h/2
y := pos.y - h/2
z := pos.z - h/2
Expand All @@ -138,7 +138,7 @@ get_pyramid_positions :: proc(pos: Vec = 0, h: f32 = 1) -> [PYRAMID_VERTICES]Vec
}
}

get_sphere_base_triangle :: proc(positions, normals: []Vec, radius: f32, segments: int) {
get_sphere_base_triangle :: proc(positions, normals: []vec3, radius: f32, segments: int) {
// TODO: merge top and bottom segment triangles
si := 0 // segment index
for vi in 0..<segments { // vertical
Expand All @@ -153,10 +153,10 @@ get_sphere_base_triangle :: proc(positions, normals: []Vec, radius: f32, segment
ha3 := 2*PI * (f32(hi)+1 + hmove) / f32(segments)

// Vertices
v0 := Vec{cos(ha0)*sin(va1), cos(va1), sin(ha0)*sin(va1)}
v1 := Vec{cos(ha1)*sin(va0), cos(va0), sin(ha1)*sin(va0)}
v2 := Vec{cos(ha2)*sin(va1), cos(va1), sin(ha2)*sin(va1)}
v3 := Vec{cos(ha3)*sin(va0), cos(va0), sin(ha3)*sin(va0)}
v0 := vec3{cos(ha0)*sin(va1), cos(va1), sin(ha0)*sin(va1)}
v1 := vec3{cos(ha1)*sin(va0), cos(va0), sin(ha1)*sin(va0)}
v2 := vec3{cos(ha2)*sin(va1), cos(va1), sin(ha2)*sin(va1)}
v3 := vec3{cos(ha3)*sin(va0), cos(va0), sin(ha3)*sin(va0)}

// Normals
n0 := normalize(v0)
Expand Down Expand Up @@ -187,7 +187,7 @@ get_sphere_base_triangle :: proc(positions, normals: []Vec, radius: f32, segment
}
}

get_sphere_base_rectangle :: proc(positions, normals: []Vec, radius: f32, segments: int) {
get_sphere_base_rectangle :: proc(positions, normals: []vec3, radius: f32, segments: int) {
// TODO: merge top and bottom segment triangles
si := 0 // segment index
for vi in 0..<segments { // vertical
Expand All @@ -196,12 +196,12 @@ get_sphere_base_rectangle :: proc(positions, normals: []Vec, radius: f32, segmen
va1 := PI * f32(vi+1) / f32(segments)
ha0 := 2*PI * f32(hi+0) / f32(segments)
ha1 := 2*PI * f32(hi+1) / f32(segments)

// Vertices
v0 := Vec{cos(ha0)*sin(va1), cos(va1), sin(ha0)*sin(va1)}
v1 := Vec{cos(ha0)*sin(va0), cos(va0), sin(ha0)*sin(va0)}
v2 := Vec{cos(ha1)*sin(va1), cos(va1), sin(ha1)*sin(va1)}
v3 := Vec{cos(ha1)*sin(va0), cos(va0), sin(ha1)*sin(va0)}
v0 := vec3{cos(ha0)*sin(va1), cos(va1), sin(ha0)*sin(va1)}
v1 := vec3{cos(ha0)*sin(va0), cos(va0), sin(ha0)*sin(va0)}
v2 := vec3{cos(ha1)*sin(va1), cos(va1), sin(ha1)*sin(va1)}
v3 := vec3{cos(ha1)*sin(va0), cos(va0), sin(ha1)*sin(va0)}

// Normals
n0 := normalize(v0)
Expand Down Expand Up @@ -235,18 +235,18 @@ get_sphere_base_rectangle :: proc(positions, normals: []Vec, radius: f32, segmen
JOINT_TRIANGLES :: 8
JOINT_VERTICES :: 3 * JOINT_TRIANGLES

get_joint :: proc(from, to: Vec, w: f32) -> [JOINT_VERTICES]Vec {
get_joint :: proc(from, to: vec3, w: f32) -> [JOINT_VERTICES]vec3 {

mid := from*(1.0/3.0) + to*(2.0/3.0)

from, to := from, to
if from.y > to.y {
from, to = to, from
}

normal := normalize(to - from)

cross_dir: Vec
cross_dir: vec3

if normal.x > 0.5 {
cross_dir = UP
Expand Down Expand Up @@ -274,20 +274,20 @@ get_joint :: proc(from, to: Vec, w: f32) -> [JOINT_VERTICES]Vec {

move_x := normal_x * w
move_z := normal_z * w

return {
from,
mid + move_z,
mid + move_x,

from,
mid - move_x,
mid + move_z,

from,
mid + move_x,
mid - move_z,

from,
mid - move_z,
mid - move_x,
Expand Down
14 changes: 7 additions & 7 deletions example/specular.odin
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ State_Specular :: struct {
u_light_color: i32,
u_eye_pos : i32,
vao : VAO,
positions : [ALL_VERTICES]Vec,
normals : [ALL_VERTICES]Vec,
positions : [ALL_VERTICES]vec3,
normals : [ALL_VERTICES]vec3,
colors : [ALL_VERTICES]RGBA,
}

Expand Down Expand Up @@ -59,7 +59,7 @@ setup_specular :: proc(s: ^State_Specular, program: gl.Program) {

/* Cube */
copy_array(s.positions[:], get_cube_positions(0, CUBE_HEIGHT))
cube_normals: [CUBE_VERTICES]Vec = 1
cube_normals: [CUBE_VERTICES]vec3 = 1
copy_array(s.normals[:], cube_normals)
slice.fill(s.colors[:], WHITE)

Expand Down Expand Up @@ -95,9 +95,9 @@ frame_specular :: proc(s: ^State_Specular, delta: f32) {
// Clear the canvas AND the depth buffer.
gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)

camera_pos := Vec{0, 0, 500 - 500 * (scale-0.5)}
camera_pos := vec3{0, 0, 500 - 500 * (scale-0.5)}

camera_mat: Mat4 = 1
camera_mat: mat4 = 1
camera_mat *= mat4_translate(camera_pos)
camera_mat = glm.inverse_mat4(camera_mat)

Expand All @@ -110,12 +110,12 @@ frame_specular :: proc(s: ^State_Specular, delta: f32) {

s.cube_angle += 0.01 * delta * mouse_rel.x

cube_pos: Vec
cube_pos: vec3
cube_pos.y = 500 * -mouse_rel.y
cube_pos.x = CUBE_RADIUS * cos(s.cube_angle)
cube_pos.z = CUBE_RADIUS * sin(s.cube_angle)

cube_mat: Mat4 = 1
cube_mat: mat4 = 1
cube_mat *= mat4_translate(cube_pos)
cube_mat *= mat4_rotate_y(s.cube_angle)

Expand Down
Loading

0 comments on commit daa3a32

Please sign in to comment.