summaryrefslogtreecommitdiffstats
path: root/src/RendererSection.cpp
diff options
context:
space:
mode:
authorLaG1924 <12997935+LaG1924@users.noreply.github.com>2017-09-20 17:54:09 +0200
committerLaG1924 <12997935+LaG1924@users.noreply.github.com>2018-01-13 03:39:32 +0100
commit6bab0ab86a72881c3e0b88284c211e01fb22435a (patch)
tree3594305ad612f0e59ee630c2b5a59d974fc8bac5 /src/RendererSection.cpp
parent2017-09-19 (diff)
downloadAltCraft-6bab0ab86a72881c3e0b88284c211e01fb22435a.tar
AltCraft-6bab0ab86a72881c3e0b88284c211e01fb22435a.tar.gz
AltCraft-6bab0ab86a72881c3e0b88284c211e01fb22435a.tar.bz2
AltCraft-6bab0ab86a72881c3e0b88284c211e01fb22435a.tar.lz
AltCraft-6bab0ab86a72881c3e0b88284c211e01fb22435a.tar.xz
AltCraft-6bab0ab86a72881c3e0b88284c211e01fb22435a.tar.zst
AltCraft-6bab0ab86a72881c3e0b88284c211e01fb22435a.zip
Diffstat (limited to 'src/RendererSection.cpp')
-rw-r--r--src/RendererSection.cpp322
1 files changed, 125 insertions, 197 deletions
diff --git a/src/RendererSection.cpp b/src/RendererSection.cpp
index 1f0a0a3..59e3558 100644
--- a/src/RendererSection.cpp
+++ b/src/RendererSection.cpp
@@ -2,106 +2,136 @@
#include <thread>
-std::vector<glm::vec3> vertArray;
-GLuint vertVbo = -1;
-std::mutex vertMutex;
-
-GLuint GetVertex(glm::vec3 vertex) {
- vertMutex.lock();
- int i = 0;
- for (; i < vertArray.size(); i++) {
- if (vertArray[i] == vertex) {
- vertMutex.unlock();
- return i;
- }
- }
- vertArray.push_back(vertex);
- vertMutex.unlock();
- return i + 1;
-}
-
-void SyncVertices() {
- vertMutex.lock();
- if (vertVbo == -1) {
- glGenBuffers(1, &vertVbo);
- }
- glBindBuffer(GL_ARRAY_BUFFER, vertVbo);
- glBufferData(GL_ARRAY_BUFFER, sizeof(glm::vec3) * vertArray.size(), vertArray.data(), GL_DYNAMIC_DRAW);
- glBindBuffer(GL_ARRAY_BUFFER, 0);
- glCheckError();
- vertMutex.unlock();
-}
-
+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;
+GLuint RendererSection::Vao;
+GLuint RendererSection::Vbo[VBOCOUNT];
+
+std::vector<glm::mat4> contentModels;
+std::vector<glm::vec4> contentTextures;
+std::vector<glm::vec3> contentColors;
+std::vector<glm::vec2> contentLights;
RendererSection::RendererSection(RendererSectionData data) {
- SyncVertices();
-
- glGenVertexArrays(1, &Vao);
- glCheckError();
-
- glGenBuffers(VBOCOUNT, Vbo);
- glCheckError();
-
- glBindVertexArray(Vao);
- {
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, Vbo[IBO]);
- glCheckError();
+ if (VboVertices == magicUniqueConstant) {
+ glGenBuffers(1, &VboVertices);
+ glGenBuffers(1, &VboUvs);
//Cube vertices
- GLuint VertAttribPos = 0;
- glBindBuffer(GL_ARRAY_BUFFER, vertVbo);
- glVertexAttribPointer(VertAttribPos, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), nullptr);
- glEnableVertexAttribArray(VertAttribPos);
-
- /*//Cube UVs
- GLuint UvAttribPos = 2;
- glBindBuffer(GL_ARRAY_BUFFER, Vbo[UV]);
- glVertexAttribPointer(UvAttribPos, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(GLfloat), nullptr);
- glEnableVertexAttribArray(UvAttribPos);
-
- //Textures
- GLuint textureAttribPos = 7;
- glBindBuffer(GL_ARRAY_BUFFER, Vbo[TEXTURES]);
- glVertexAttribPointer(textureAttribPos, 4, GL_FLOAT, GL_FALSE, 4 * sizeof(GLfloat), nullptr);
- glEnableVertexAttribArray(textureAttribPos);
-
- //Color
- GLuint colorAttribPos = 12;
- glBindBuffer(GL_ARRAY_BUFFER, Vbo[COLORS]);
- glVertexAttribPointer(colorAttribPos, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), nullptr);
- glEnableVertexAttribArray(colorAttribPos);
-
- //Light
- GLuint lightAttribPos = 13;
- glBindBuffer(GL_ARRAY_BUFFER, Vbo[LIGHTS]);
- glVertexAttribPointer(lightAttribPos, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(GLfloat), nullptr);
- glEnableVertexAttribArray(lightAttribPos);*/
+ 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 faces";
+
+ glGenVertexArrays(1, &Vao);
+ glGenBuffers(VBOCOUNT, Vbo);
+
+ 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, Vbo[TEXTURES]);
+ 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, Vbo[MODELS]);
+ 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, Vbo[COLORS]);
+ glVertexAttribPointer(colorAttribPos, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), nullptr);
+ glEnableVertexAttribArray(colorAttribPos);
+ glVertexAttribDivisor(colorAttribPos, 1);
+
+ //Light
+ GLuint lightAttribPos = 13;
+ glBindBuffer(GL_ARRAY_BUFFER, Vbo[LIGHTS]);
+ glVertexAttribPointer(lightAttribPos, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(GLfloat), nullptr);
+ glEnableVertexAttribArray(lightAttribPos);
+ glVertexAttribDivisor(lightAttribPos, 1);
+
+ glBindBuffer(GL_ARRAY_BUFFER, 0);
+ }
+ glBindVertexArray(0);
+ glCheckError();
}
- glBindVertexArray(0);
- glBindBuffer(GL_ARRAY_BUFFER, 0);
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
- glCheckError();
-
//Upload data to VRAM
- glBindBuffer(GL_ARRAY_BUFFER, Vbo[IBO]);
- glBufferData(GL_ARRAY_BUFFER, data.indices.size() * sizeof(GLuint), data.indices.data(), GL_DYNAMIC_DRAW);
-
- glBindBuffer(GL_ARRAY_BUFFER, Vbo[UV]);
- glBufferData(GL_ARRAY_BUFFER, data.uv.size() * sizeof(glm::vec2), data.uv.data(), GL_DYNAMIC_DRAW);
-
- /*glBindBuffer(GL_ARRAY_BUFFER, Vbo[TEXTURES]);
- glBufferData(GL_ARRAY_BUFFER, data.textures.size() * sizeof(glm::vec4), data.textures.data(), GL_DYNAMIC_DRAW);
+ contentModels.insert(contentModels.end(), data.models.begin(), data.models.end());
+ glBindBuffer(GL_ARRAY_BUFFER, Vbo[MODELS]);
+ glBufferData(GL_ARRAY_BUFFER, contentModels.size() * sizeof(glm::mat4), contentModels.data(), GL_DYNAMIC_DRAW);
+
+ contentTextures.insert(contentTextures.end(), data.textures.begin(), data.textures.end());
+ glBindBuffer(GL_ARRAY_BUFFER, Vbo[TEXTURES]);
+ glBufferData(GL_ARRAY_BUFFER, contentTextures.size() * sizeof(glm::vec4), contentTextures.data(), GL_DYNAMIC_DRAW);
+ contentColors.insert(contentColors.end(), data.colors.begin(), data.colors.end());
glBindBuffer(GL_ARRAY_BUFFER, Vbo[COLORS]);
- glBufferData(GL_ARRAY_BUFFER, data.colors.size() * sizeof(glm::vec3), data.colors.data(), GL_DYNAMIC_DRAW);
+ glBufferData(GL_ARRAY_BUFFER, contentColors.size() * sizeof(glm::vec3), contentColors.data(), GL_DYNAMIC_DRAW);
+ contentLights.insert(contentLights.end(), data.lights.begin(), data.lights.end());
glBindBuffer(GL_ARRAY_BUFFER, Vbo[LIGHTS]);
- glBufferData(GL_ARRAY_BUFFER, data.lights.size() * sizeof(glm::vec2), data.lights.data(), GL_DYNAMIC_DRAW);*/
+ glBufferData(GL_ARRAY_BUFFER, contentLights.size() * sizeof(glm::vec2), contentLights.data(), GL_DYNAMIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
numOfFaces = data.textures.size();
+ offset = contentModels.size() - numOfFaces;
sectionPos = data.sectionPos;
hash = data.hash;
}
@@ -112,30 +142,13 @@ RendererSection::RendererSection(RendererSection && other) {
}
RendererSection::~RendererSection() {
- if (Vao != 0)
- glDeleteVertexArrays(1, &Vao);
-
- for (int i = 0; i < VBOCOUNT; i++)
- if (Vbo[i] != 0) {
- glBindBuffer(GL_ARRAY_BUFFER, Vbo[i]);
- glBufferData(GL_ARRAY_BUFFER, 0, 0, GL_STATIC_DRAW);
- }
-
- glDeleteBuffers(VBOCOUNT, Vbo);
}
void swap(RendererSection & lhs, RendererSection & rhs) {
- std::swap(lhs.Vbo, rhs.Vbo);
- std::swap(lhs.Vao, rhs.Vao);
std::swap(lhs.hash, rhs.hash);
std::swap(lhs.numOfFaces, rhs.numOfFaces);
std::swap(lhs.sectionPos, rhs.sectionPos);
-}
-
-void RendererSection::Render(RenderState &renderState) {
- renderState.SetActiveVao(Vao);
- glDrawElements(GL_TRIANGLES, 6 * numOfFaces, GL_UNSIGNED_INT, nullptr);
- glCheckError();
+ std::swap(lhs.offset, rhs.offset);
}
Vector RendererSection::GetPosition()
@@ -148,14 +161,17 @@ size_t RendererSection::GetHash()
return hash;
}
-RendererSectionData::RendererSectionData(World * world, Vector sectionPosition) {
+GLuint RendererSection::GetVao() {
+ return Vao;
+}
+
+RendererSectionData::RendererSectionData(World * world, Vector sectionPosition) {
const std::map<BlockTextureId, glm::vec4> &textureAtlas = AssetManager::Instance().GetTextureAtlasIndexes();
const Section &section = world->GetSection(sectionPosition);
hash = section.GetHash();
sectionPos = sectionPosition;
- //glm::mat4 baseOffset = glm::translate(glm::mat4(), (section.GetPosition() * 16).glm()), transform;
- glm::mat4 transform;
+ glm::mat4 baseOffset = glm::translate(glm::mat4(), (section.GetPosition() * 16).glm()),transform;
auto sectionsList = world->GetSectionsList();
@@ -169,7 +185,7 @@ RendererSectionData::RendererSectionData(World * world, Vector sectionPosition)
const bool useNewMethod = true;
- transform = glm::translate(glm::mat4(), Vector(x, y, z).glm());
+ transform = glm::translate(baseOffset, Vector(x, y, z).glm());
const BlockModel* model = AssetManager::Instance().GetBlockModelByBlockId(block);
if (model) {
@@ -177,13 +193,12 @@ RendererSectionData::RendererSectionData(World * world, Vector sectionPosition)
}
else {
transform = glm::translate(transform, glm::vec3(0, 1, 0));
-
+
if (block.id == 8 || block.id == 9) {
textures.push_back(AssetManager::Instance().GetTextureByAssetName("minecraft/textures/blocks/water_still"));
textures.back().w /= 32.0f;
transform = glm::translate(transform, glm::vec3(0, -0.2, 0));
- }
- else
+ } else
textures.push_back(AssetManager::Instance().GetTextureByAssetName("minecraft/textures/blocks/tnt_side"));
models.push_back(transform);
@@ -194,50 +209,9 @@ RendererSectionData::RendererSectionData(World * world, Vector sectionPosition)
}
}
}
-
- CreateVertices();
-
- ReplaceVertices();
-
- models.clear();
-
- /* const int mul = 6;
-
- textures.resize(textures.size() * mul);
- for (int i = textures.size()/mul - 1; i > 0; i--) {
- textures[i * mul] = textures[i];
- }
- for (int i = 0; i < textures.size(); i += mul) {
- for (int j = 1; j < mul; j++) {
- textures[i + j] = textures[i];
- }
- }
-
- colors.resize(colors.size() * mul);
- for (int i = colors.size() / mul - 1; i > 0; i--) {
- colors[i * mul] = colors[i];
- }
- for (int i = 0; i < colors.size(); i += mul) {
- for (int j = 1; j < mul; j++) {
- colors[i + j] = colors[i];
- }
- }
-
- lights.resize(lights.size() * mul);
- for (int i = lights.size() / mul - 1; i > 0; i--) {
- lights[i * mul] = lights[i];
- }
- for (int i = 0; i < lights.size(); i += mul) {
- for (int j = 1; j < mul; j++) {
- lights[i + j] = lights[i];
- }
- }*/
-
textures.shrink_to_fit();
models.shrink_to_fit();
colors.shrink_to_fit();
- lights.shrink_to_fit();
- vertices.shrink_to_fit();
}
void RendererSectionData::AddFacesByBlockModel(const std::vector<Vector> &sectionsList, World *world, Vector blockPos, const BlockModel &model, glm::mat4 transform, unsigned char light, unsigned char skyLight) {
@@ -363,50 +337,4 @@ bool RendererSectionData::TestBlockExists(const std::vector<Vector> &sectionsLis
auto blockModel = AssetManager::Instance().GetBlockModelByBlockId(world->GetSection(section).GetBlockId(blockPos));
return blockId.id != 0 && blockModel && blockModel->IsBlock;
-}
-
-void RendererSectionData::CreateVertices() {
- const GLfloat verts[] = {
- 0, 0, 0,
- 1, 0, 1,
- 1, 0, 0,
-
- 0, 0, 0,
- 0, 0, 1,
- 1, 0, 1,
- };
-
- const GLfloat uvs[] = {
- 0.0f, 0.0f,
- 1.0f, 1.0f,
- 0.0f, 1.0f,
-
- 0.0f, 0.0f,
- 1.0f, 0.0f,
- 1.0f, 1.0f,
- };
-
- for (const auto &model : models) {
- vertices.push_back(model * glm::vec4(verts[0], verts[1], verts[2], 1.0f));
- vertices.push_back(model * glm::vec4(verts[3], verts[4], verts[5], 1.0f));
- vertices.push_back(model * glm::vec4(verts[6], verts[7], verts[8], 1.0f));
-
- vertices.push_back(model * glm::vec4(verts[9], verts[10], verts[11], 1.0f));
- vertices.push_back(model * glm::vec4(verts[12], verts[13], verts[14], 1.0f));
- vertices.push_back(model * glm::vec4(verts[15], verts[16], verts[17], 1.0f));
-
- uv.push_back(glm::vec2(uvs[0], uvs[1]));
- uv.push_back(glm::vec2(uvs[2], uvs[3]));
- uv.push_back(glm::vec2(uvs[4], uvs[5]));
-
- uv.push_back(glm::vec2(uvs[6], uvs[7]));
- uv.push_back(glm::vec2(uvs[8], uvs[9]));
- uv.push_back(glm::vec2(uvs[10], uvs[11]));
- }
-}
-
-void RendererSectionData::ReplaceVertices() {
- for (auto& it : vertices) {
- indices.push_back(GetVertex(it));
- }
} \ No newline at end of file