From 16620134dbf54aaef118dfce884137c359c9bf55 Mon Sep 17 00:00:00 2001 From: Damian Tarnawski Date: Thu, 2 May 2024 00:37:45 +0200 Subject: [PATCH] Draw shapes in candy example --- example/candy.odin | 85 +++++++++++++++++++++++++++++----- example/candy.vert | 8 ++-- example/shaders_generated.odin | 15 +++--- 3 files changed, 87 insertions(+), 21 deletions(-) diff --git a/example/candy.odin b/example/candy.odin index 6eff818..6d305fa 100644 --- a/example/candy.odin +++ b/example/candy.odin @@ -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 { @@ -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 { @@ -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[:] @@ -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)) + } } - \ No newline at end of file diff --git a/example/candy.vert b/example/candy.vert index 46f5c96..9b80dc3 100644 --- a/example/candy.vert +++ b/example/candy.vert @@ -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; } diff --git a/example/shaders_generated.odin b/example/shaders_generated.odin index eb269fa..6b9812d 100644 --- a/example/shaders_generated.odin +++ b/example/shaders_generated.odin @@ -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, }