From: Brendan Hansen Date: Sat, 18 Dec 2021 03:44:22 +0000 (-0600) Subject: random world of cubes X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=3cd92ef863e7a9f3838b165f576b7d1dd7fe608e;p=voxel-shooter.git random world of cubes --- diff --git a/run_tree/assets/shaders/world_fragment.glsl b/run_tree/assets/shaders/world_fragment.glsl new file mode 100644 index 0000000..f1f7549 --- /dev/null +++ b/run_tree/assets/shaders/world_fragment.glsl @@ -0,0 +1,9 @@ +#version 300 es +precision mediump float; + +in vec4 v_color; + +out vec4 fragColor; +void main() { + fragColor = v_color; +} diff --git a/run_tree/assets/shaders/world_vertex.glsl b/run_tree/assets/shaders/world_vertex.glsl new file mode 100644 index 0000000..d93598a --- /dev/null +++ b/run_tree/assets/shaders/world_vertex.glsl @@ -0,0 +1,23 @@ +#version 300 es +layout(location = 0) in vec3 a_pos; +layout(location = 1) in uint a_data; + +layout(std140) uniform u_matrix_block { + mat4 u_view; + mat4 u_world; + mat4 u_model; +}; + +out vec4 v_color; + +void main() { + gl_Position = u_view * u_world * vec4(a_pos, 1); + + vec3 block_color = vec3( + float((a_data & 0x0000FU) >> 0U) / 15.0, + float((a_data & 0x000F0U) >> 4U) / 15.0, + float((a_data & 0x00F00U) >> 8U) / 15.0 + ) * (float((a_data & 0x0F000U) >> 12U) / 15.0); + + v_color = vec4(block_color, 1); +} diff --git a/src/build.onyx b/src/build.onyx index 9319bf4..1b54d7a 100644 --- a/src/build.onyx +++ b/src/build.onyx @@ -10,6 +10,7 @@ #load "config" #load "main" #load "camera" +#load "mesh" #load "vecmath" // Onyx library code diff --git a/src/main.onyx b/src/main.onyx index f6cca9f..bde1562 100644 --- a/src/main.onyx +++ b/src/main.onyx @@ -51,84 +51,12 @@ toggle_cursor_grabbed :: () { } } -Mesh :: struct { - handle: GLuint; - verticies: [] Vertex; - indicies: [] u32; -} - -mesh_draw :: (use mesh: ^Mesh) { - glBindVertexArray(handle); - glDrawElements(GL_TRIANGLES, indicies.count, GL_UNSIGNED_INT, ~~0); - glBindVertexArray(-1); -} - -Vertex :: struct { - x, y, z: f32; -} - -make_cube_mesh :: () -> Mesh { - vertex_data := memory.make_slice(Vertex, 8); - vertex_data[0] = .{0,0,0}; - vertex_data[1] = .{0,1,0}; - vertex_data[2] = .{1,1,0}; - vertex_data[3] = .{1,0,0}; - vertex_data[4] = .{0,0,1}; - vertex_data[5] = .{0,1,1}; - vertex_data[6] = .{1,1,1}; - vertex_data[7] = .{1,0,1}; - - #persist index_data := u32.[ - 0, 2, 1, // back - 0, 3, 2, - - 3, 6, 2, // right side - 3, 7, 6, - - 5, 0, 1, // left side - 5, 4, 0, - - 4, 5, 6, // front - 4, 6, 7, - - 5, 1, 2, // top - 5, 2, 6, - - 0, 4, 7, // bottom - 0, 7, 3, - ]; - - mesh: Mesh; - mesh.verticies = vertex_data; - mesh.indicies = index_data; - - glGenVertexArrays(1, ^mesh.handle); - glBindVertexArray(mesh.handle); - - vbo: GLuint; - glGenBuffers(1, ^vbo); - glBindBuffer(GL_ARRAY_BUFFER, vbo); - glBufferData(GL_ARRAY_BUFFER, sizeof Vertex * vertex_data.count, vertex_data.data, GL_STATIC_DRAW); - - glEnableVertexAttribArray(0); - glVertexAttribPointer(0, 3, GL_FLOAT, false, 3 * sizeof GLfloat, ~~0); - - ibo: GLuint; - glGenBuffers(1, ^ibo); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo); - glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof i32 * 36, ~~index_data, GL_STATIC_DRAW); - - glBindVertexArray(-1); - - return mesh; -} - make_plane_mesh :: () -> Mesh { vertex_data := memory.make_slice(Vertex, 4); - vertex_data[0] = .{-10, 0, 10}; - vertex_data[1] = .{-10, 0, -10}; - vertex_data[2] = .{ 10, 0, -10}; - vertex_data[3] = .{ 10, 0, 10}; + vertex_data[0] = .{-10, 0, 10, 0 }; + vertex_data[1] = .{-10, 0, -10, 0 }; + vertex_data[2] = .{ 10, 0, -10, 0 }; + vertex_data[3] = .{ 10, 0, 10, 0 }; #persist index_data := u32.[ 0, 1, 2, 0, 2, 3 ]; @@ -155,7 +83,7 @@ make_plane_mesh :: () -> Mesh { return mesh; } -cube_mesh: Mesh; +cubes: [..] Mesh; plane_mesh: Mesh; matrix_block_buffer: GLuint; @@ -202,8 +130,8 @@ setup_opengl :: () { return prog; } - vertex_shader := os.get_contents("assets/shaders/basic_vertex.glsl"); - fragment_shader := os.get_contents("assets/shaders/basic_fragment.glsl"); + vertex_shader := os.get_contents("assets/shaders/world_vertex.glsl"); + fragment_shader := os.get_contents("assets/shaders/world_fragment.glsl"); vs := compile_shader(vertex_shader, GL_VERTEX_SHADER); fs := compile_shader(fragment_shader, GL_FRAGMENT_SHADER); @@ -221,8 +149,12 @@ setup_opengl :: () { matrix_block_index := glGetUniformBlockIndex(prog, #cstr "u_matrix_block"); glUniformBlockBinding(prog, matrix_block_index, 0); - cube_mesh = make_cube_mesh(); - plane_mesh = make_plane_mesh(); + for 100 { + x := random.between(0, 10); + y := random.between(0, 10); + z := random.between(0, 10); + cubes << mesh_make_cube(~~x, ~~y, ~~z); + } __initialize(^camera); camera.position = .{5,5,5}; @@ -296,8 +228,9 @@ draw :: () { glClearColor(.2, .2, .2, 1); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - mesh_draw(^plane_mesh); - mesh_draw(^cube_mesh); + // mesh_draw(^plane_mesh); + //mesh_draw(^cube_mesh); + for^ cubes do mesh_draw(it); glfwSwapBuffers(window); } diff --git a/src/mesh.onyx b/src/mesh.onyx new file mode 100644 index 0000000..c38141b --- /dev/null +++ b/src/mesh.onyx @@ -0,0 +1,93 @@ +use package core +use package opengles + +Vertex :: struct { + x, y, z: f32; + + // This field is broken up by bit, but Onyx does not (nor ever will) support bit-fields. + // + // 4-bits (0..3) - red color + // 4-bits (4..7) - green color + // 4-bits (8..11) - blue color + // 4-bits (12..15) - intensity + // 12-bits (16..27) - light per face (2 bits per face) + // 16..17 - top + // 18..19 - bottom + // 20..21 - front + // 22..23 - left + // 24..25 - back + // 26..27 - right + data: u32; +} + +Mesh :: struct { + handle: GLuint; + verticies: [] Vertex; + indicies: [] u32; +} + +mesh_draw :: (use mesh: ^Mesh) { + glBindVertexArray(handle); + glDrawElements(GL_TRIANGLES, indicies.count, GL_UNSIGNED_INT, ~~0); + glBindVertexArray(-1); +} + +mesh_make_cube :: (x, y, z: f32) -> Mesh { + data := random.between(0x0000, 0xffff); + + vertex_data := memory.make_slice(Vertex, 8); + vertex_data[0] = .{x+0,y+0,z+0,data}; + vertex_data[1] = .{x+0,y+1,z+0,data}; + vertex_data[2] = .{x+1,y+1,z+0,data}; + vertex_data[3] = .{x+1,y+0,z+0,data}; + vertex_data[4] = .{x+0,y+0,z+1,data}; + vertex_data[5] = .{x+0,y+1,z+1,data}; + vertex_data[6] = .{x+1,y+1,z+1,data}; + vertex_data[7] = .{x+1,y+0,z+1,data}; + + #persist index_data := u32.[ + 0, 2, 1, // back + 0, 3, 2, + + 3, 6, 2, // right side + 3, 7, 6, + + 5, 0, 1, // left side + 5, 4, 0, + + 4, 5, 6, // front + 4, 6, 7, + + 5, 1, 2, // top + 5, 2, 6, + + 0, 4, 7, // bottom + 0, 7, 3, + ]; + + mesh: Mesh; + mesh.verticies = vertex_data; + mesh.indicies = index_data; + + glGenVertexArrays(1, ^mesh.handle); + glBindVertexArray(mesh.handle); + + vbo: GLuint; + glGenBuffers(1, ^vbo); + glBindBuffer(GL_ARRAY_BUFFER, vbo); + glBufferData(GL_ARRAY_BUFFER, sizeof Vertex * vertex_data.count, vertex_data.data, GL_STATIC_DRAW); + + glEnableVertexAttribArray(0); + glEnableVertexAttribArray(1); + glVertexAttribPointer(0, 3, GL_FLOAT, false, sizeof Vertex, ~~0); + glVertexAttribIPointer(1, 1, GL_UNSIGNED_INT, sizeof Vertex, ~~12); + + ibo: GLuint; + glGenBuffers(1, ^ibo); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo); + glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof i32 * 36, ~~index_data, GL_STATIC_DRAW); + + glBindVertexArray(-1); + + return mesh; +}