summaryrefslogtreecommitdiff
path: root/render_chunks.ha
diff options
context:
space:
mode:
authorLassi Pulkkinen <lassi@pulk.fi>2024-10-31 03:11:21 +0200
committerLassi Pulkkinen <lassi@pulk.fi>2024-10-31 03:51:35 +0200
commitae44478b30d890fe0fb04022f44d474dcdcc3f9d (patch)
tree5f462459ae4b47d22114eed717d1382d08cf4dfe /render_chunks.ha
Initial commit (import old repo)HEADmain
Diffstat (limited to 'render_chunks.ha')
-rw-r--r--render_chunks.ha113
1 files changed, 113 insertions, 0 deletions
diff --git a/render_chunks.ha b/render_chunks.ha
new file mode 100644
index 0000000..d56d912
--- /dev/null
+++ b/render_chunks.ha
@@ -0,0 +1,113 @@
+use gl::*;
+use glm;
+use glw;
+use time;
+use trace;
+
+fn render_chunks_frame() void = {
+ let nprep = 0;
+ for :prepare (let z = 0i32; z < CHUNKS_SIZE; z += 1)
+ for (let x = 0i32; x < CHUNKS_SIZE; x += 1)
+ for (let y = 0u8; y < CHUNKS_HEIGHT; y += 1) {
+ const pos = SectionPos {
+ x = CHUNKS_POS.x + x,
+ y = CHUNKS_MIN_Y + y: i8,
+ z = CHUNKS_POS.z + z,
+ };
+
+ if (render_chunks_prepare_section(pos)) {
+ nprep += 1;
+ if (nprep >= 80) break :prepare;
+ };
+ };
+};
+
+fn render_chunks_prepare_section(pos: SectionPos) bool = {
+ const section = match (getsection(pos)) {
+ case let section: *Section =>
+ yield section;
+ case null =>
+ return false;
+ };
+
+ if (!section.dirty) return false;
+ section.dirty = false;
+
+ const t0 = time::now(time::clock::MONOTONIC);
+ const vertices = section_build_geometry(pos);
+ const t = time::diff(t0, time::now(time::clock::MONOTONIC));
+ defer free(vertices);
+
+// trace::debug(&trace::root,
+// "[ {} {} {} ]: {} vertices / {} bytes / took {} µs / palette {} items",
+// pos.x, pos.y, pos.z,
+// len(vertices), len(vertices) * size(Vertex),
+// t / 1000, section.bstates.palette_size);
+
+ if (len(vertices) == 0) {
+ if (section.vertexbuf != 0) {
+ glDeleteBuffers(1, &section.vertexbuf);
+ section.vertexbuf = 0;
+ };
+ return true;
+ };
+
+ if (section.vertexbuf == 0) {
+ glGenBuffers(1, &section.vertexbuf);
+ };
+
+ glBindBuffer(GL_ARRAY_BUFFER, section.vertexbuf);
+ glw::buffer_data(
+ GL_ARRAY_BUFFER,
+ vertices, size(Vertex),
+ GL_STATIC_DRAW,
+ );
+
+ section.vertexcount = len(vertices): i32;
+
+ return true;
+};
+
+fn render_chunks_section_destroy(section: *Section) void = {
+ glDeleteBuffers(1, &section.vertexbuf);
+};
+
+fn render_chunks_render_section(pos: SectionPos) void = {
+ const section = match (getsection(pos)) {
+ case let section: *Section =>
+ yield section;
+ case null =>
+ return;
+ };
+
+ if (section.vertexbuf == 0) return;
+
+ const origin = [
+ (pos.x - CHUNKS_POS.x): f32 * 16.0,
+ pos.y: f32 * 16.0,
+ (pos.z - CHUNKS_POS.z): f32 * 16.0,
+ ];
+ glUniform3fv(1, 1, &origin: *f32);
+
+ glBindVertexBuffer(0, section.vertexbuf, 0, size(Vertex): i32);
+ glDrawArrays(GL_TRIANGLES, 0, section.vertexcount);
+};
+
+fn render_chunks_render(trans: *glm::m4) void = {
+ glUseProgram(SHADER_BLOCKS);
+ glBindTexture(GL_TEXTURE_2D, ATLAS_BLOCKS.gl_texture);
+ glUniformMatrix4fv(0, 1, 0, trans: *f32);
+ glBindVertexArray(VAO_BLOCKS);
+
+ for (let z = 0i32; z < CHUNKS_SIZE; z += 1)
+ for (let x = 0i32; x < CHUNKS_SIZE; x += 1)
+ for (let y = 0u8; y < CHUNKS_HEIGHT; y += 1) {
+ const pos = SectionPos {
+ x = CHUNKS_POS.x + x,
+ y = CHUNKS_MIN_Y + y: i8,
+ z = CHUNKS_POS.z + z,
+ };
+
+ render_chunks_render_section(pos);
+ };
+};