summaryrefslogtreecommitdiff
path: root/render_chunks.ha
blob: d56d912e34df1022411bcfc869cd420749f59604 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
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);
	};
};