From a94c3ad29a0069cf0def0f041208536097c6c8ca Mon Sep 17 00:00:00 2001 From: Damian Tarnawski Date: Sat, 6 Apr 2024 21:37:58 +0200 Subject: [PATCH] Draw pyramids --- example/look_at.odin | 68 +++++++++++++++++++++++++++++++------------- example/setup.js | 5 +++- example/utils.odin | 63 ++++++++++++++-------------------------- 3 files changed, 75 insertions(+), 61 deletions(-) diff --git a/example/look_at.odin b/example/look_at.odin index 525cf92..6e923c7 100644 --- a/example/look_at.odin +++ b/example/look_at.odin @@ -3,15 +3,42 @@ package example import glm "core:math/linalg/glsl" import gl "../wasm/webgl" -@(private="file") BOX_HEIGHT :: 60 +PYRAMID_TRIANGLES :: 6 +PYRAMID_VERTICES :: PYRAMID_TRIANGLES * 3 + +pyramid_colors: [PYRAMID_VERTICES]RGBA = { + BLUE, BLUE, BLUE, // 0 + BLUE, BLUE, BLUE, // 1 + YELLOW, YELLOW, YELLOW, // 2 + PURPLE, PURPLE, PURPLE, // 3 + RED, RED, RED, // 4 + ORANGE, ORANGE, ORANGE, // 5 +} + +write_pyramid_positions :: proc(dst: []Vec, x, y, z, h: f32) { + assert(len(dst) == PYRAMID_VERTICES) + + positions: [PYRAMID_VERTICES]Vec = { + {x, y, z}, {x+h, y, z}, {x, y, z+h}, + {x, y, z+h}, {x+h, y, z}, {x+h, y, z+h}, + + {x, y, z}, {x+h/2, y+h, z+h/2}, {x+h, y, z}, + {x+h, y, z}, {x+h/2, y+h, z+h/2}, {x+h, y, z+h}, + {x+h, y, z+h}, {x+h/2, y+h, z+h/2}, {x, y, z+h}, + {x, y, z+h}, {x+h/2, y+h, z+h/2}, {x, y, z}, + } + for &vec in positions { + vec -= {0.5, 0.5, 0.5} * h + } + copy(dst, positions[:]) +} -@(private="file") BOXES_ROWS :: 3 -@(private="file") BOXES_AMOUNT :: BOXES_ROWS * BOXES_ROWS * BOXES_ROWS +@(private="file") HEIGHT :: 60 +@(private="file") AMOUNT :: 10 +@(private="file") VERTICES :: AMOUNT * PYRAMID_VERTICES @(private="file") state: struct { rotation: [2]f32, - a_position: i32, - a_color: i32, u_matrix: i32, vao: VAO, } @@ -22,9 +49,9 @@ look_at_start :: proc(program: gl.Program) { vao = gl.CreateVertexArray() gl.BindVertexArray(vao) - a_position = gl.GetAttribLocation (program, "a_position") - a_color = gl.GetAttribLocation (program, "a_color") - u_matrix = gl.GetUniformLocation(program, "u_matrix") + a_position := gl.GetAttribLocation (program, "a_position") + a_color := gl.GetAttribLocation (program, "a_color") + u_matrix = gl.GetUniformLocation(program, "u_matrix") gl.EnableVertexAttribArray(a_position) gl.EnableVertexAttribArray(a_color) @@ -35,18 +62,21 @@ look_at_start :: proc(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 - colors : [BOXES_AMOUNT * CUBE_VERTICES]RGBA + positions: [VERTICES]Vec + colors : [VERTICES]RGBA + + for i in 0.. [2]D where intrinsics.type_is_numeric(S) && intrinsics.type_is_numeric(D) { @@ -109,52 +113,29 @@ mat4_perspective :: proc "contextless" (fov, aspect, near, far: f32) -> glm.mat4 } +GREEN : RGBA : {60, 210, 0, 255} +YELLOW: RGBA : {210, 210, 0, 255} +BLUE : RGBA : {0, 80, 190, 255} +RED : RGBA : {230, 20, 0, 255} +ORANGE: RGBA : {250, 160, 50, 255} +PURPLE: RGBA : {160, 100, 200, 255} CUBE_TRIANGLES :: 6 * 2 CUBE_VERTICES :: CUBE_TRIANGLES * 3 cube_colors: [CUBE_VERTICES]RGBA = { - {60, 210, 0, 255}, // 0 - {60, 210, 0, 255}, // Green - {60, 210, 0, 255}, // - {60, 210, 0, 255}, // 1 - {60, 210, 0, 255}, // Green - {60, 210, 0, 255}, // - - {210, 210, 0, 255}, // 2 - {210, 210, 0, 255}, // Yellow - {210, 210, 0, 255}, // - {210, 210, 0, 255}, // 3 - {210, 210, 0, 255}, // Yellow - {210, 210, 0, 255}, // - - {0, 80, 190, 255}, // 4 - {0, 80, 190, 255}, // Blue - {0, 80, 190, 255}, // - {0, 80, 190, 255}, // 5 - {0, 80, 190, 255}, // Blue - {0, 80, 190, 255}, // - - {230, 20, 0, 255}, // 6 - {230, 20, 0, 255}, // Red - {230, 20, 0, 255}, // - {230, 20, 0, 255}, // 7 - {230, 20, 0, 255}, // Red - {230, 20, 0, 255}, // - - {250, 160, 50, 255}, // 8 - {250, 160, 50, 255}, // Orange - {250, 160, 50, 255}, // - {250, 160, 50, 255}, // 9 - {250, 160, 50, 255}, // Orange - {250, 160, 50, 255}, // - - {160, 100, 200, 255}, // 10 - {160, 100, 200, 255}, // Purple - {160, 100, 200, 255}, // - {160, 100, 200, 255}, // 11 - {160, 100, 200, 255}, // Purple - {160, 100, 200, 255}, // + GREEN, GREEN, GREEN, // 0 + GREEN, GREEN, GREEN, // 1 + YELLOW, YELLOW, YELLOW, // 2 + YELLOW, YELLOW, YELLOW, // 3 + BLUE, BLUE, BLUE, // 4 + BLUE, BLUE, BLUE, // 5 + RED, RED, RED, // 6 + RED, RED, RED, // 7 + ORANGE, ORANGE, ORANGE, // 8 + ORANGE, ORANGE, ORANGE, // 9 + PURPLE, PURPLE, PURPLE, // 10 + PURPLE, PURPLE, PURPLE, // 11 } cube_positions: [CUBE_VERTICES]Vec = {