Skip to content

Commit

Permalink
Draw shapes in candy example
Browse files Browse the repository at this point in the history
  • Loading branch information
thetarnav committed May 1, 2024
1 parent 5945134 commit 1662013
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 21 deletions.
85 changes: 73 additions & 12 deletions example/candy.odin
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
//+private file
package example

// import glm "core:math/linalg/glsl"
import glm "core:math/linalg/glsl"
import "core:math/rand"
import "core:fmt"
import gl "../wasm/webgl"


@private
State_Candy :: struct {
using locations: Input_Locations_Candy,
objects: []Object,
}

Shape :: struct {
Expand All @@ -19,8 +20,9 @@ Shape :: struct {

Object :: struct {
using uniforms: Uniform_Values_Candy,
shape: Shape,
rotation_speed: vec3,
translation: vec3,
rotation_speed: [2]vec3,
}

rand_color :: proc() -> u8vec4 {
Expand All @@ -29,10 +31,19 @@ rand_color :: proc() -> u8vec4 {
return color
}


@private
setup_candy :: proc(s: ^State_Candy, program: gl.Program) {
// gl.Enable(gl.CULL_FACE)
// gl.Enable(gl.DEPTH_TEST)

input_locations_candy(s, program)

cube_shape: Shape = {vao = gl.CreateVertexArray()}

/*
Cube
*/
cube_shape: Shape

cube_positions := get_cube_positions()
cube_shape.positions = cube_positions[:]
Expand All @@ -42,20 +53,70 @@ setup_candy :: proc(s: ^State_Candy, program: gl.Program) {
color = rand_color()
}

fmt.println("cube_shape.positions: ", cube_shape.positions)
fmt.println("cube_shape.colors: ", cube_shape.colors)
cube_shape.vao = gl.CreateVertexArray()
gl.BindVertexArray(cube_shape.vao)

// s.vao = gl.CreateVertexArray()
// gl.BindVertexArray(s.vao)
attribute(s.a_position, gl.CreateBuffer(), cube_shape.positions)
attribute(s.a_color , gl.CreateBuffer(), cube_shape.colors)

// input_locations_candy(s, program)

// gl.Enable(gl.CULL_FACE)
// gl.Enable(gl.DEPTH_TEST)
/*
Objects
*/
s.objects = make([]Object, 20)
for &object in s.objects {
object.shape = cube_shape
object.translation = {
rand.float32_range(-20, 20),
rand.float32_range(-20, 20),
rand.float32_range(-20, 20),
}
object.rotation_speed = {rand.float32(), rand.float32(), rand.float32()}
object.u_color_mult = rgba_to_vec4(rand_color())
object.u_local = 1
object.u_view = 1
}
}

@private
frame_candy :: proc(s: ^State_Candy, delta: f32) {
gl.Viewport(0, 0, canvas_res.x, canvas_res.y)
gl.ClearColor(0, 0, 0, 0)
gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)


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

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

// view_mat := glm.mat4PerspectiveInfinite(
// fovy = radians(80),
// aspect = aspect_ratio,
// near = 1,
// )
// view_mat *= camera_mat


for &o, i in s.objects {
// o.u_local *= mat4_rotate_x(delta * o.rotation_speed.x)
// o.u_local *= mat4_rotate_y(delta * o.rotation_speed.y)
// o.u_local *= mat4_rotate_z(delta * o.rotation_speed.z)
// o.u_local *= mat4_translate(o.translation)

o.u_view = glm.mat4PerspectiveInfinite(
fovy = glm.radians_f32(80),
aspect = aspect_ratio,
near = 1,
)

gl.BindVertexArray(o.shape.vao)

uniform(s.u_view, o.u_view)
uniform(s.u_local, o.u_local)
uniform(s.u_color_mult, o.u_color_mult)

gl.DrawArrays(gl.TRIANGLES, i*len(o.shape.positions), len(o.shape.positions))
}
}

8 changes: 5 additions & 3 deletions example/candy.vert
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
#version 300 es

in vec4 a_position;
in vec3 a_position;
in vec4 a_color;

uniform mat4 u_matrix;
uniform mat4 u_view;
uniform mat4 u_local;

out vec4 v_color;

void main() {
gl_Position = u_matrix * a_position;
vec4 local_pos = u_local * vec4(a_position, 1.0);
gl_Position = u_view * local_pos;

v_color = a_color;
}
15 changes: 9 additions & 6 deletions example/shaders_generated.odin
Original file line number Diff line number Diff line change
Expand Up @@ -65,25 +65,28 @@ Attribute_Values_Lighting :: struct {

Input_Locations_Candy :: struct {
u_color_mult: Uniform_vec4,
a_position: Attribute_vec4,
a_position: Attribute_vec3,
a_color: Attribute_vec4,
u_matrix: Uniform_mat4,
u_view: Uniform_mat4,
u_local: Uniform_mat4,
}

input_locations_candy :: proc(s: ^Input_Locations_Candy, program: gl.Program) {
s.u_color_mult = uniform_location_vec4(program, "u_color_mult")
s.a_position = attribute_location_vec4(program, "a_position")
s.a_position = attribute_location_vec3(program, "a_position")
s.a_color = attribute_location_vec4(program, "a_color")
s.u_matrix = uniform_location_mat4(program, "u_matrix")
s.u_view = uniform_location_mat4(program, "u_view")
s.u_local = uniform_location_mat4(program, "u_local")
}

Uniform_Values_Candy :: struct {
u_color_mult: vec4,
u_matrix: mat4,
u_view: mat4,
u_local: mat4,
}

Attribute_Values_Candy :: struct {
a_position: vec4,
a_position: vec3,
a_color: vec4,
}

Expand Down

0 comments on commit 1662013

Please sign in to comment.