Skip to content

Commit

Permalink
Use a raw_union for example state
Browse files Browse the repository at this point in the history
  • Loading branch information
thetarnav committed Apr 19, 2024
1 parent dc9d3f7 commit 5fe97cc
Show file tree
Hide file tree
Showing 10 changed files with 246 additions and 247 deletions.
31 changes: 15 additions & 16 deletions example/boxes.odin
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,22 @@ BOX_HEIGHT :: 60
BOXES_ROWS :: 3
BOXES_AMOUNT :: BOXES_ROWS * BOXES_ROWS * BOXES_ROWS

boxes_state: struct {
@private
State_Boxes :: struct {
rotation: [2]f32,
u_matrix: i32,
vao: VAO,
}

@(private="package")
boxes_start :: proc(program: gl.Program) {
using boxes_state

vao = gl.CreateVertexArray()
gl.BindVertexArray(vao)
@private
setup_boxes :: proc(s: ^State_Boxes, program: gl.Program) {
s.vao = gl.CreateVertexArray()
gl.BindVertexArray(s.vao)

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

s.u_matrix = gl.GetUniformLocation(program, "u_matrix")

gl.EnableVertexAttribArray(a_position)
gl.EnableVertexAttribArray(a_color)
Expand Down Expand Up @@ -75,18 +75,17 @@ boxes_start :: proc(program: gl.Program) {
gl.VertexAttribPointer(a_color, 4, gl.UNSIGNED_BYTE, true, 0, 0)
}

@(private="package")
boxes_frame :: proc(delta: f32) {
using boxes_state
@private
frame_boxes :: proc(s: ^State_Boxes, delta: f32) {

gl.BindVertexArray(vao)
gl.BindVertexArray(s.vao)

gl.Viewport(0, 0, canvas_res.x, canvas_res.y)
gl.ClearColor(0, 0, 0, 0)
// Clear the canvas AND the depth buffer.
gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)

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

mat: Mat4 = 1
mat *= glm.mat4PerspectiveInfinite(
Expand All @@ -95,10 +94,10 @@ boxes_frame :: proc(delta: f32) {
near = 1,
)
mat *= glm.mat4Translate({0, 0, -900 + scale * 720})
mat *= mat4_rotate_x(rotation.x)
mat *= mat4_rotate_y(rotation.y)
mat *= mat4_rotate_x(s.rotation.x)
mat *= mat4_rotate_y(s.rotation.y)

gl.UniformMatrix4fv(u_matrix, mat)
gl.UniformMatrix4fv(s.u_matrix, mat)

gl.DrawArrays(gl.TRIANGLES, 0, CUBE_VERTICES * BOXES_AMOUNT)
}
37 changes: 18 additions & 19 deletions example/camera.odin
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ PYRAMID_COLORS: [PYRAMID_VERTICES]RGBA : {
ORANGE, ORANGE, ORANGE, // 5
}

camera_state: struct {
@private
State_Camera :: struct {
rotation: f32,
u_matrix: i32,
vao: VAO,
Expand All @@ -28,16 +29,15 @@ RING_RADIUS :: 260
CUBE_RADIUS :: 220
AMOUNT :: 10

@(private="package")
camera_start :: proc(program: gl.Program) {
using camera_state

vao = gl.CreateVertexArray()
gl.BindVertexArray(vao)
@private
setup_camera :: proc(s: ^State_Camera, program: gl.Program) {
s.vao = gl.CreateVertexArray()
gl.BindVertexArray(s.vao)

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

s.u_matrix = gl.GetUniformLocation(program, "u_matrix")

gl.EnableVertexAttribArray(a_position)
gl.EnableVertexAttribArray(a_color)
Expand Down Expand Up @@ -72,11 +72,10 @@ camera_start :: proc(program: gl.Program) {
gl.VertexAttribPointer(a_color, 4, gl.UNSIGNED_BYTE, true, 0, 0)
}

@(private="package")
camera_frame :: proc(delta: f32) {
using camera_state
@private
frame_camera :: proc(s: ^State_Camera, delta: f32) {

gl.BindVertexArray(vao)
gl.BindVertexArray(s.vao)

gl.Viewport(0, 0, canvas_res.x, canvas_res.y)
gl.ClearColor(0, 0, 0, 0)
Expand All @@ -94,13 +93,13 @@ camera_frame :: proc(delta: f32) {
)
view_mat *= camera_mat

rotation += 0.01 * delta * mouse_rel.x
elevation := 300 * -(mouse_rel.y - 0.5)
s.rotation += 0.01 * delta * mouse_rel.x
elevation := 300 * -(mouse_rel.y - 0.5)

cube_pos: Vec
cube_pos.y = elevation
cube_pos.x = CUBE_RADIUS * cos(rotation)
cube_pos.z = CUBE_RADIUS * sin(rotation)
cube_pos.x = CUBE_RADIUS * cos(s.rotation)
cube_pos.z = CUBE_RADIUS * sin(s.rotation)

for i in 0..<AMOUNT {
/* Draw pyramid looking at the cube */
Expand All @@ -118,15 +117,15 @@ camera_frame :: proc(delta: f32) {
)
mat *= mat4_rotate_x(PI/2)

gl.UniformMatrix4fv(u_matrix, mat)
gl.UniformMatrix4fv(s.u_matrix, mat)
gl.DrawArrays(gl.TRIANGLES, i*PYRAMID_VERTICES, PYRAMID_VERTICES)
}

{ /* Draw cube */
mat := view_mat
mat *= mat4_translate(cube_pos)
mat *= mat4_rotate_y(rotation)
gl.UniformMatrix4fv(u_matrix, mat)
mat *= mat4_rotate_y(s.rotation)
gl.UniformMatrix4fv(s.u_matrix, mat)
gl.DrawArrays(gl.TRIANGLES, ALL_PYRAMID_VERTICES, CUBE_VERTICES)
}
}
90 changes: 46 additions & 44 deletions example/lighting.odin
Original file line number Diff line number Diff line change
Expand Up @@ -20,31 +20,33 @@ RING_HEIGHT :: 30
RING_LENGTH :: 40
RING_SPACE :: 30

cube_angle: f32
ring_angle: f32
u_view: i32
u_local: i32
u_light_dir: i32
u_light_color: i32
vao: VAO
positions: [ALL_VERTICES]Vec
normals: [ALL_VERTICES]Vec
colors: [ALL_VERTICES]RGBA

@(private="package")
lighting_start :: proc(program: gl.Program) {

vao = gl.CreateVertexArray()
gl.BindVertexArray(vao)
@private
State_Lighting :: struct {
cube_angle: f32,
ring_angle: f32,
u_view: i32,
u_local: i32,
u_light_dir: i32,
u_light_color: i32,
vao: VAO,
positions: [ALL_VERTICES]Vec,
normals: [ALL_VERTICES]Vec,
colors: [ALL_VERTICES]RGBA,
}

@private
setup_lighting :: proc(s: ^State_Lighting, program: gl.Program) {
s.vao = gl.CreateVertexArray()
gl.BindVertexArray(s.vao)

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

u_view = gl.GetUniformLocation(program, "u_view")
u_local = gl.GetUniformLocation(program, "u_local")
u_light_dir = gl.GetUniformLocation(program, "u_light_dir")
u_light_color = gl.GetUniformLocation(program, "u_light_color")
s.u_view = gl.GetUniformLocation(program, "u_view")
s.u_local = gl.GetUniformLocation(program, "u_local")
s.u_light_dir = gl.GetUniformLocation(program, "u_light_dir")
s.u_light_color = gl.GetUniformLocation(program, "u_light_color")

gl.EnableVertexAttribArray(a_position)
gl.EnableVertexAttribArray(a_normal)
Expand All @@ -58,10 +60,10 @@ lighting_start :: proc(program: gl.Program) {
gl.Enable(gl.DEPTH_TEST) // draw only closest faces

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

/* Ring
Expand All @@ -77,10 +79,10 @@ lighting_start :: proc(program: gl.Program) {
ramp bottom
*/

rings_normals := normals [CUBE_VERTICES:]
rings_positions := positions[CUBE_VERTICES:]
rings_normals := s.normals [CUBE_VERTICES:]
rings_positions := s.positions[CUBE_VERTICES:]

for &color in colors[CUBE_VERTICES:] {
for &color in s.colors[CUBE_VERTICES:] {
color = PURPLE_DARK
}

Expand Down Expand Up @@ -137,23 +139,23 @@ lighting_start :: proc(program: gl.Program) {
}

gl.BindBuffer(gl.ARRAY_BUFFER, positions_buffer)
gl.BufferDataSlice(gl.ARRAY_BUFFER, positions[:], gl.STATIC_DRAW)
gl.BufferDataSlice(gl.ARRAY_BUFFER, s.positions[:], gl.STATIC_DRAW)
gl.VertexAttribPointer(a_position, 3, gl.FLOAT, false, 0, 0)

gl.BindBuffer(gl.ARRAY_BUFFER, normals_buffer)
gl.BufferDataSlice(gl.ARRAY_BUFFER, normals[:], gl.STATIC_DRAW)
gl.BufferDataSlice(gl.ARRAY_BUFFER, s.normals[:], gl.STATIC_DRAW)
gl.VertexAttribPointer(a_normal, 3, gl.FLOAT, false, 0, 0)

gl.BindBuffer(gl.ARRAY_BUFFER, colors_buffer)
gl.BufferDataSlice(gl.ARRAY_BUFFER, colors[:], gl.STATIC_DRAW)
gl.BufferDataSlice(gl.ARRAY_BUFFER, s.colors[:], gl.STATIC_DRAW)
gl.VertexAttribPointer(a_color, 4, gl.UNSIGNED_BYTE, true, 0, 0)

gl.Uniform4fv(u_light_color, rgba_to_vec4(ORANGE))
gl.Uniform4fv(s.u_light_color, rgba_to_vec4(ORANGE))
}

@(private="package")
lighting_frame :: proc(delta: f32) {
gl.BindVertexArray(vao)
@private
frame_lighting :: proc(s: ^State_Lighting, delta: f32) {
gl.BindVertexArray(s.vao)

gl.Viewport(0, 0, canvas_res.x, canvas_res.y)
gl.ClearColor(0, 0, 0, 0)
Expand All @@ -171,36 +173,36 @@ lighting_frame :: proc(delta: f32) {
)
view_mat *= camera_mat

gl.UniformMatrix4fv(u_view, view_mat)
gl.UniformMatrix4fv(s.u_view, view_mat)

/* Draw cube */
cube_angle += 0.01 * delta * mouse_rel.x
s.cube_angle += 0.01 * delta * mouse_rel.x

cube_pos: Vec
cube_pos.y = 500 * -mouse_rel.y
cube_pos.x = CUBE_RADIUS * cos(cube_angle)
cube_pos.z = CUBE_RADIUS * sin(cube_angle)
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_translate(cube_pos)
cube_mat *= mat4_rotate_y(cube_angle)
cube_mat *= mat4_rotate_y(s.cube_angle)

gl.UniformMatrix4fv(u_local, cube_mat)
gl.UniformMatrix4fv(s.u_local, cube_mat)
gl.DrawArrays(gl.TRIANGLES, 0, CUBE_VERTICES)

/* Draw light from cube */
light_dir := glm.normalize(cube_pos)
gl.Uniform3fv(u_light_dir, light_dir)
gl.Uniform3fv(s.u_light_dir, light_dir)

/* Draw rings */
ring_angle += 0.002 * delta
s.ring_angle += 0.002 * delta

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

gl.UniformMatrix4fv(u_local, ring_mat)
gl.UniformMatrix4fv(s.u_local, ring_mat)
gl.DrawArrays(gl.TRIANGLES, CUBE_VERTICES + i*RING_VERTICES, RING_VERTICES)
}
}
27 changes: 13 additions & 14 deletions example/pyramid.odin
Original file line number Diff line number Diff line change
Expand Up @@ -50,22 +50,22 @@ positions: [VERTICES*3]f32 = {
0, 0, SIDE/2,
}

pyramid_state: struct {
@(private="package")
State_Pyramid :: struct {
rotation: [2]f32,
u_matrix: i32,
vao: VAO,
}

@(private="package")
pyramid_start :: proc(program: gl.Program) {
using pyramid_state

vao = gl.CreateVertexArray()
gl.BindVertexArray(vao)
setup_pyramid :: proc(s: ^State_Pyramid, program: gl.Program) {
s.vao = gl.CreateVertexArray()
gl.BindVertexArray(s.vao)

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

s.u_matrix = gl.GetUniformLocation(program, "u_matrix")

gl.EnableVertexAttribArray(a_position)
gl.EnableVertexAttribArray(a_color)
Expand All @@ -85,16 +85,15 @@ pyramid_start :: proc(program: gl.Program) {
}

@(private="package")
pyramid_frame :: proc(delta: f32) {
using pyramid_state
frame_pyramid :: proc(s: ^State_Pyramid, delta: f32) {

gl.BindVertexArray(vao)
gl.BindVertexArray(s.vao)

gl.Viewport(0, 0, canvas_res.x, canvas_res.y)
gl.ClearColor(0, 0, 0, 0)
gl.Clear(gl.COLOR_BUFFER_BIT)

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

mat := glm.mat4Ortho3d(
left = 0,
Expand All @@ -106,11 +105,11 @@ pyramid_frame :: proc(delta: f32) {
)
mat *= glm.mat4Translate(vec2_to_vec3(mouse_pos - canvas_pos))
mat *= glm.mat4Scale(scale*2 + 0.4)
mat *= mat4_rotate_y(-rotation.y)
mat *= mat4_rotate_x(rotation.x)
mat *= mat4_rotate_y(-s.rotation.y)
mat *= mat4_rotate_x( s.rotation.x)
mat *= glm.mat4Translate({0, -H / 2, 0})

gl.UniformMatrix4fv(u_matrix, mat)
gl.UniformMatrix4fv(s.u_matrix, mat)

gl.DrawArrays(gl.TRIANGLES, 0, VERTICES)
}
Loading

0 comments on commit 5fe97cc

Please sign in to comment.