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, §ion.vertexbuf); section.vertexbuf = 0; }; return true; }; if (section.vertexbuf == 0) { glGenBuffers(1, §ion.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, §ion.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); }; };