starting to work on chunking
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Sun, 19 Dec 2021 01:31:20 +0000 (19:31 -0600)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Sun, 19 Dec 2021 01:31:20 +0000 (19:31 -0600)
src/build.onyx
src/chunk.onyx [new file with mode: 0644]
src/main.onyx
src/mesh.onyx

index 1b54d7af24bd795146610dd6bff9b73bbd031d1e..f2c978a6e6eaab0f1abf545c1007ed689ff6fa74 100644 (file)
@@ -12,6 +12,7 @@
 #load "camera"
 #load "mesh"
 #load "vecmath"
+#load "chunk"
 
 // Onyx library code
 #load "stb_truetype"
diff --git a/src/chunk.onyx b/src/chunk.onyx
new file mode 100644 (file)
index 0000000..3cc27d2
--- /dev/null
@@ -0,0 +1,49 @@
+use package core
+
+Block :: #distinct u32;
+Block_Empty :: cast(Block) 0;
+
+block_make :: (red, green, blue: f32, brightness: f32) -> Block {
+    r := cast(u32) (red * 16.0f);
+    g := cast(u32) (green * 16.0f);
+    b := cast(u32) (blue * 16.0f);
+    i := cast(u32) (brightness * 16.0f);
+
+    return ~~(i << 12 | b << 8 | g << 4 | r);
+}
+
+Chunk_Size :: 16
+
+Chunk :: struct {
+    blocks: [] Block;
+}
+
+chunk_make :: (allocator := context.allocator) -> ^Chunk {
+    chunk := new(Chunk, allocator);
+    memory.alloc_slice(^chunk.blocks, Chunk_Size * Chunk_Size * Chunk_Size);
+    memory.fill_slice(chunk.blocks, block_make(0.2, 1, 0.2, 1));
+}
+
+#local in_chunk_bounds :: macro (x, y, z: i32) -> bool {
+    if x < 0 do return false;
+    if x >= Chunk_Size do return false;
+    if y < 0 do return false;
+    if y >= Chunk_Size do return false;
+    if z < 0 do return false;
+    if z >= Chunk_Size do return false;
+    return true;
+}
+
+chunk_set :: (use chunk: ^Chunk, x, y, z: i32, block: Block) {
+    if !in_chunk_bounds(x, y, z) do return;
+    blocks[x * Chunk_Size * Chunk_Size + y * Chunk_Size + z] = block;
+}
+
+chunk_get :: (use chunk: ^Chunk, x, y, z: i32) -> Block {
+    if !in_chunk_bounds(x, y, z) do return Block_Empty;
+    return blocks[x * Chunk_Size * Chunk_Size + y * Chunk_Size + z];
+}
+
+chunk_build_mesh :: (use chunk: ^Chunk) -> ^Mesh {
+    
+}
index 2595d0c59d64a8d62ef1f5a534395b347c5ba2c2..43a1f6f3e0ecb7431aba4c90c7e7a20a4bfda541 100644 (file)
@@ -11,7 +11,7 @@ camera: Camera;
 create_window :: () => {
     glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
     glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
-    window = glfwCreateWindow(800, 600, #cstr "Voxel Shooter");
+    window = glfwCreateWindow(1200, 720, #cstr "Voxel Shooter");
 
     glfwMakeContextCurrent(window);
     glfwSwapInterval(0);
@@ -232,7 +232,7 @@ update :: (dt: f32) {
 }
 
 draw :: () {
-    glClearColor(.2, .2, .2, 1);
+    glClearColor(.7, .7, .9, 1);
     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 
     #persist forward : Vector3;
index d79e1c7eee506cde2a57a9b896e35cecf2b83ec9..6176f02ebaec260e22a5696cc14417016d9194e6 100644 (file)
@@ -15,6 +15,9 @@ Vertex :: struct {
 
 Mesh :: struct {
     handle: GLuint;
+    vertex_handle: GLuint;
+    index_handle: GLuint;
+
     verticies: [] Vertex;
     indicies:  [] u32;
 }
@@ -65,9 +68,8 @@ mesh_make_cube :: (x, y, z: f32) -> Mesh {
     glGenVertexArrays(1, ^mesh.handle);
     glBindVertexArray(mesh.handle);
 
-    vbo: GLuint;
-    glGenBuffers(1, ^vbo);
-    glBindBuffer(GL_ARRAY_BUFFER, vbo);
+    glGenBuffers(1, ^mesh.vertex_handle);
+    glBindBuffer(GL_ARRAY_BUFFER, mesh.vertex_handle);
     glBufferData(GL_ARRAY_BUFFER, sizeof Vertex * vertex_data.count, vertex_data.data, GL_STATIC_DRAW);
 
     glEnableVertexAttribArray(0);
@@ -75,12 +77,17 @@ mesh_make_cube :: (x, y, z: f32) -> Mesh {
     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);
+    glGenBuffers(1, ^mesh.index_handle);
+    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mesh.index_handle);
     glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof i32 * 36, ~~index_data, GL_STATIC_DRAW);
 
     glBindVertexArray(-1);
 
     return mesh;
 }
+
+mesh_free :: (mesh: ^Mesh) {
+    glDeleteBuffers(1, ^mesh.vertex_handle);
+    glDeleteBuffers(1, ^mesh.index_handle);
+    glDeleteVertexArrays(1, ^mesh.handle);
+}