}
}
-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 ];
return mesh;
}
-cube_mesh: Mesh;
+cubes: [..] Mesh;
plane_mesh: Mesh;
matrix_block_buffer: GLuint;
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);
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};
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);
}
--- /dev/null
+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;
+}