summaryrefslogtreecommitdiffstats
path: root/src/RendererSection.cpp
blob: fbb9c1957103120dcbe96fd71abf840cda6527bd (plain) (blame)
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
#include "RendererSection.hpp"

#include <thread>

const GLfloat vertices[] = {
		0, 0, 0,
		1, 0, 1,
		1, 0, 0,

		0, 0, 0,
		0, 0, 1,
		1, 0, 1,
};

const GLfloat uv_coords[] = {
		0.0f, 0.0f,
		1.0f, 1.0f,
		0.0f, 1.0f,

		0.0f, 0.0f,
		1.0f, 0.0f,
		1.0f, 1.0f,
};

const GLuint magicUniqueConstant = 88375;
GLuint RendererSection::VboVertices = magicUniqueConstant;
GLuint RendererSection::VboUvs = magicUniqueConstant;
std::map<GLuint, int> RendererSection::refCounterVbo;
std::map<GLuint, int> RendererSection::refCounterVao;


RendererSection::RendererSection(World *world, Vector position) : sectionPosition(position), world(world) {
	if (VboVertices == magicUniqueConstant) {
		glGenBuffers(1, &VboVertices);
		glGenBuffers(1, &VboUvs);

		//Cube vertices
		glBindBuffer(GL_ARRAY_BUFFER, VboVertices);
		glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

		//Cube UVs
		glBindBuffer(GL_ARRAY_BUFFER, VboUvs);
		glBufferData(GL_ARRAY_BUFFER, sizeof(uv_coords), uv_coords, GL_STATIC_DRAW);

		LOG(INFO) << "Created VBOs with vertices (" << VboVertices << ") and UVs (" << VboUvs
		          << ") for ordinary blocks";
	}

	glGenBuffers(1, &VboTextures);
	if (refCounterVbo.find(VboTextures) == refCounterVbo.end())
		refCounterVbo[VboTextures] = 0;
	refCounterVbo[VboTextures]++;

	glGenBuffers(1, &VboModels);
	if (refCounterVbo.find(VboModels) == refCounterVbo.end())
		refCounterVbo[VboModels] = 0;
	refCounterVbo[VboModels]++;

	glGenBuffers(1, &VboColors);
	if (refCounterVbo.find(VboColors) == refCounterVbo.end())
		refCounterVbo[VboColors] = 0;
	refCounterVbo[VboColors]++;

	glGenVertexArrays(1, &Vao);
	if (refCounterVao.find(Vao) == refCounterVao.end())
		refCounterVao[Vao] = 0;
	refCounterVao[Vao]++;

	glBindVertexArray(Vao);
	{
		//Cube vertices
		GLuint VertAttribPos = 0;
		glBindBuffer(GL_ARRAY_BUFFER, VboVertices);
		glVertexAttribPointer(VertAttribPos, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), nullptr);
		glEnableVertexAttribArray(VertAttribPos);

		//Cube UVs
		GLuint UvAttribPos = 2;
		glBindBuffer(GL_ARRAY_BUFFER, VboUvs);
		glVertexAttribPointer(UvAttribPos, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(GLfloat), nullptr);
		glEnableVertexAttribArray(UvAttribPos);

		//Textures
		GLuint textureAttribPos = 7;
		glBindBuffer(GL_ARRAY_BUFFER, VboTextures);
		glVertexAttribPointer(textureAttribPos, 4, GL_FLOAT, GL_FALSE, 4 * sizeof(GLfloat), nullptr);
		glEnableVertexAttribArray(textureAttribPos);
		glVertexAttribDivisor(textureAttribPos, 1);
		glCheckError();

		//Blocks models
		GLuint matAttribPos = 8;
		size_t sizeOfMat4 = 4 * 4 * sizeof(GLfloat);
		glBindBuffer(GL_ARRAY_BUFFER, VboModels);
		glVertexAttribPointer(matAttribPos + 0, 4, GL_FLOAT, GL_FALSE, sizeOfMat4, nullptr);
		glVertexAttribPointer(matAttribPos + 1, 4, GL_FLOAT, GL_FALSE, sizeOfMat4, (void *) (1 * 4 * sizeof(GLfloat)));
		glVertexAttribPointer(matAttribPos + 2, 4, GL_FLOAT, GL_FALSE, sizeOfMat4, (void *) (2 * 4 * sizeof(GLfloat)));
		glVertexAttribPointer(matAttribPos + 3, 4, GL_FLOAT, GL_FALSE, sizeOfMat4, (void *) (3 * 4 * sizeof(GLfloat)));
		glEnableVertexAttribArray(matAttribPos + 0);
		glEnableVertexAttribArray(matAttribPos + 1);
		glEnableVertexAttribArray(matAttribPos + 2);
		glEnableVertexAttribArray(matAttribPos + 3);
		glVertexAttribDivisor(matAttribPos + 0, 1);
		glVertexAttribDivisor(matAttribPos + 1, 1);
		glVertexAttribDivisor(matAttribPos + 2, 1);
		glVertexAttribDivisor(matAttribPos + 3, 1);

		//Color
		GLuint colorAttribPos = 12;
		glBindBuffer(GL_ARRAY_BUFFER, VboColors);
		glVertexAttribPointer(colorAttribPos, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), nullptr);
		glEnableVertexAttribArray(colorAttribPos);
		glVertexAttribDivisor(colorAttribPos, 1);

		glBindBuffer(GL_ARRAY_BUFFER, 0);
	}
	glBindVertexArray(0);
	glCheckError();
}

RendererSection::~RendererSection() {
	refCounterVbo[VboTextures]--;
	refCounterVbo[VboModels]--;
	refCounterVbo[VboColors]--;
	refCounterVao[Vao]--;
	if (refCounterVbo[VboTextures] <= 0)
		glDeleteBuffers(1, &VboTextures);

	if (refCounterVbo[VboModels] <= 0)
		glDeleteBuffers(1, &VboTextures);
	if (refCounterVbo[VboColors] <= 0)
		glDeleteBuffers(1, &VboColors);

	if (refCounterVao[Vao] <= 0)
		glDeleteVertexArrays(1, &Vao);
}

void RendererSection::Render(RenderState &renderState) {
	if (!isEnabled) return;
	if (!models.empty()) {
		PrepareRender();
	}
	renderState.SetActiveVao(Vao);
	glDrawArraysInstanced(GL_TRIANGLES, 0, 6, numOfFaces);
	glCheckError();
}

Section *RendererSection::GetSection() {
	return &world->GetSection(sectionPosition);
}

RendererSection::RendererSection(const RendererSection &other) {
	this->world = other.world;
	this->VboModels = other.VboModels;
	this->VboTextures = other.VboTextures;
	this->VboColors = other.VboColors;
	this->sectionPosition = other.sectionPosition;
	this->Vao = other.Vao;
	this->numOfFaces = other.numOfFaces;
	this->models = other.models;
	this->textures = other.textures;
	this->colors = other.colors;
	this->hash = other.hash;

	refCounterVbo[VboTextures]++;
	refCounterVbo[VboModels]++;
	refCounterVbo[VboColors]++;
	refCounterVao[Vao]++;
}

void RendererSection::SetEnabled(bool isEnabled) {
	this->isEnabled = isEnabled;
}

bool RendererSection::IsNeedResourcesPrepare() {
	size_t currentHash = world->GetSection(sectionPosition).GetHash();
	bool isNeedUpdate = currentHash != hash;
	return isNeedUpdate;
}

void RendererSection::PrepareResources() {
	const std::map<BlockTextureId,glm::vec4> &textureAtlas = AssetManager::Instance().GetTextureAtlasIndexes();
	Section &section = world->GetSection(sectionPosition);
	models.clear();
	textures.clear();
	colors.clear();
	for (int y = 0; y < 16; y++) {
		for (int z = 0; z < 16; z++) {
			for (int x = 0; x < 16; x++) {
				Vector blockPos = Vector(x, y, z) + (sectionPosition * 16);
				Block block = world->GetBlock(blockPos);
				if (block.id == 0)
					continue;

				auto checkBlockVisibility = [&](Vector block) -> bool {
					return section.GetBlock(block).id == 0 ||
					       section.GetBlock(block).id == 31 ||
					       section.GetBlock(block).id == 18;
				};

				unsigned char isVisible = 0;
				if (x == 0 || x == 15 || y == 0 || y == 15 || z == 0 || z == 15) {
					isVisible = 0b1111'1111; //All faces is visible
				} else {
					isVisible |= checkBlockVisibility(Vector(x - 1, y, z)) << 0;
					isVisible |= checkBlockVisibility(Vector(x + 1, y, z)) << 1;
					isVisible |= checkBlockVisibility(Vector(x, y + 1, z)) << 2;
					isVisible |= checkBlockVisibility(Vector(x, y - 1, z)) << 3;
					isVisible |= checkBlockVisibility(Vector(x, y, z - 1)) << 4;
					isVisible |= checkBlockVisibility(Vector(x, y, z + 1)) << 5;
				}

				if (isVisible == 0x00)
					continue;

				glm::mat4 transform;
				transform = glm::translate(transform, glm::vec3(sectionPosition.GetX() * 16,
				                                                sectionPosition.GetY() * 16,
				                                                sectionPosition.GetZ() * 16));
				transform = glm::translate(transform, glm::vec3(x, y, z));
				glm::vec3 biomeColor(0.275, 0.63, 0.1);
				glm::vec3 color(0.0f, 0.0f, 0.0f);
				if (block.id == 31 || block.id == 18)
					color = biomeColor;

				if (block.id == 31) { //X-cross like blocks rendering
					auto texture = textureAtlas.find(BlockTextureId(block.id, block.state, 2));
					for (int i = 0; i < 4; i++) {
						textures.push_back(texture->second);
						colors.push_back(color);
					}
					glm::mat4 faceTransform = glm::translate(transform, glm::vec3(0.15f, 0, 0.15f));
					faceTransform = glm::scale(faceTransform, glm::vec3(1.0f, 0.9f, 1.0f));
					faceTransform = glm::rotate(faceTransform, glm::radians(90.0f), glm::vec3(0, 0.0f, 1.0f));
					faceTransform = glm::rotate(faceTransform, glm::radians(45.0f), glm::vec3(1.0f, 0.0f, 0));
					for (int i = 0; i < 4; i++) {
						models.push_back(faceTransform);
						faceTransform = glm::translate(faceTransform, glm::vec3(0.0f, 0.0f, 0.5f));
						faceTransform = glm::rotate(faceTransform, glm::radians(90.0f), glm::vec3(1.0f, 0.0f, 0.0f));
						faceTransform = glm::translate(faceTransform, glm::vec3(0.0f, 0.0f, -0.5f));
					}
					continue;
				}

				if (isVisible >> 0 & 0x1) { //east side of block (X+)
					glm::mat4 faceTransform = glm::translate(transform, glm::vec3(0, 0, 0));
					faceTransform = glm::rotate(faceTransform, glm::radians(90.0f), glm::vec3(0, 0.0f, 1.0f));
					models.push_back(faceTransform);
					auto texture = textureAtlas.find(BlockTextureId(block.id, block.state, 2));
					if (texture != textureAtlas.end())
						textures.push_back(texture->second);
					else
						textures.push_back(glm::vec4(0.0546875, 0.00442477876106194690,
						                             0.0078125, 0.00442477876106194690)); //Fallback TNT texture
					colors.push_back(color);
				}
				if (isVisible >> 1 & 0x1) { //west side X-
					glm::mat4 faceTransform = glm::translate(transform, glm::vec3(1, 0, 0));
					faceTransform = glm::rotate(faceTransform, glm::radians(90.0f), glm::vec3(0, 0.0f, 1.0f));
					faceTransform = glm::rotate(faceTransform, glm::radians(180.0f), glm::vec3(1.0f, 0.0f, 0.0f));
					faceTransform = glm::translate(faceTransform, glm::vec3(0, 0, -1));
					models.push_back(faceTransform);
					auto texture = textureAtlas.find(BlockTextureId(block.id, block.state, 3));
					if (texture != textureAtlas.end())
						textures.push_back(texture->second);
					else
						textures.push_back(glm::vec4(0.0546875, 0.00442477876106194690,
						                             0.0078125, 0.00442477876106194690)); //Fallback TNT texture
					colors.push_back(color);
				}
				if (isVisible >> 2 & 0x1) { //Top side Y+
					glm::mat4 faceTransform = glm::translate(transform, glm::vec3(0, 1, 0));
					models.push_back(faceTransform);
					auto texture = textureAtlas.find(BlockTextureId(block.id, block.state, 1));
					if (texture != textureAtlas.end())
						textures.push_back(texture->second);
					else
						textures.push_back(glm::vec4(0.0546875, 0.00442477876106194690,
						                             0.0078125, 0.00442477876106194690)); //Fallback TNT texture
					if (block.id != 2)
						colors.push_back(color);
					else
						colors.push_back(biomeColor);
				}
				if (isVisible >> 3 & 0x1) { //Bottom side Y-
					glm::mat4 faceTransform = glm::translate(transform, glm::vec3(0, 0, 0));
					faceTransform = glm::rotate(faceTransform, glm::radians(180.0f), glm::vec3(1.0f, 0, 0));
					faceTransform = glm::translate(faceTransform, glm::vec3(0, 0, -1));
					models.push_back(faceTransform);
					auto texture = textureAtlas.find(BlockTextureId(block.id, block.state, 0));
					if (texture != textureAtlas.end())
						textures.push_back(texture->second);
					else
						textures.push_back(glm::vec4(0.0546875, 0.00442477876106194690,
						                             0.0078125, 0.00442477876106194690)); //Fallback TNT texture
					colors.push_back(color);
				}
				if (isVisible >> 4 & 0x1) { //south side Z+
					glm::mat4 faceTransform = glm::translate(transform, glm::vec3(1, 0, 0));
					faceTransform = glm::rotate(faceTransform, glm::radians(90.0f), glm::vec3(-1.0f, 0.0f, 0.0f));
					faceTransform = glm::rotate(faceTransform, glm::radians(90.0f), glm::vec3(0.0f, -1.0f, 0.0f));
					models.push_back(faceTransform);
					auto texture = textureAtlas.find(BlockTextureId(block.id, block.state, 3));
					if (texture != textureAtlas.end())
						textures.push_back(texture->second);
					else
						textures.push_back(glm::vec4(0.0546875, 0.00442477876106194690,
						                             0.0078125, 0.00442477876106194690)); //Fallback TNT texture
					colors.push_back(color);
				}
				if (isVisible >> 5 & 0x1) { //north side Z-
					glm::mat4 faceTransform = glm::translate(transform, glm::vec3(0, 0, 1));
					faceTransform = glm::rotate(faceTransform, glm::radians(90.0f), glm::vec3(-1.0f, 0.0f, 0.0f));
					faceTransform = glm::rotate(faceTransform, glm::radians(90.0f), glm::vec3(0.0f, -1.0f, 0.0f));
					faceTransform = glm::translate(faceTransform, glm::vec3(0, 0, -1));
					faceTransform = glm::rotate(faceTransform, glm::radians(180.0f), glm::vec3(1, 0, 0.0f));
					faceTransform = glm::translate(faceTransform, glm::vec3(0, 0, -1.0f));
					models.push_back(faceTransform);
					auto texture = textureAtlas.find(BlockTextureId(block.id, block.state, 4));
					if (texture != textureAtlas.end())
						textures.push_back(texture->second);
					else
						textures.push_back(glm::vec4(0.0546875, 0.00442477876106194690,
						                             0.0078125, 0.00442477876106194690)); //Fallback TNT texture
					colors.push_back(color);
				}
			}
		}
	}
	numOfFaces = textures.size();
	hash = section.GetHash();
}

void RendererSection::PrepareRender() {
	glBindBuffer(GL_ARRAY_BUFFER, VboTextures);
	glBufferData(GL_ARRAY_BUFFER, textures.size() * sizeof(glm::vec4), textures.data(), GL_DYNAMIC_DRAW);
	textures.clear();

	glBindBuffer(GL_ARRAY_BUFFER, VboModels);
	glBufferData(GL_ARRAY_BUFFER, models.size() * sizeof(glm::mat4), models.data(), GL_DYNAMIC_DRAW);
	models.clear();

	glBindBuffer(GL_ARRAY_BUFFER, VboColors);
	glBufferData(GL_ARRAY_BUFFER, colors.size() * sizeof(glm::vec3), colors.data(), GL_DYNAMIC_DRAW);
	colors.clear();

	glBindBuffer(GL_ARRAY_BUFFER, 0);
}