From: Brendan Hansen Date: Sun, 19 Dec 2021 01:31:20 +0000 (-0600) Subject: starting to work on chunking X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=266e5558eebd4956b45b80759663bcf1e42a6fe8;p=voxel-shooter.git starting to work on chunking --- diff --git a/src/build.onyx b/src/build.onyx index 1b54d7a..f2c978a 100644 --- a/src/build.onyx +++ b/src/build.onyx @@ -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 index 0000000..3cc27d2 --- /dev/null +++ b/src/chunk.onyx @@ -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 { + +} diff --git a/src/main.onyx b/src/main.onyx index 2595d0c..43a1f6f 100644 --- a/src/main.onyx +++ b/src/main.onyx @@ -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; diff --git a/src/mesh.onyx b/src/mesh.onyx index d79e1c7..6176f02 100644 --- a/src/mesh.onyx +++ b/src/mesh.onyx @@ -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); +}