diff options
author | LaG1924 <12997935+LaG1924@users.noreply.github.com> | 2017-08-20 17:21:29 +0200 |
---|---|---|
committer | LaG1924 <12997935+LaG1924@users.noreply.github.com> | 2017-08-20 17:21:29 +0200 |
commit | c62e2cd49649109f0135e56ee8add0dab2254d01 (patch) | |
tree | 7e6240186a3801daf0189d1883081b93b5f1eb5c /src/RendererSection.cpp | |
parent | 2017-08-19 (diff) | |
download | AltCraft-c62e2cd49649109f0135e56ee8add0dab2254d01.tar AltCraft-c62e2cd49649109f0135e56ee8add0dab2254d01.tar.gz AltCraft-c62e2cd49649109f0135e56ee8add0dab2254d01.tar.bz2 AltCraft-c62e2cd49649109f0135e56ee8add0dab2254d01.tar.lz AltCraft-c62e2cd49649109f0135e56ee8add0dab2254d01.tar.xz AltCraft-c62e2cd49649109f0135e56ee8add0dab2254d01.tar.zst AltCraft-c62e2cd49649109f0135e56ee8add0dab2254d01.zip |
Diffstat (limited to 'src/RendererSection.cpp')
-rw-r--r-- | src/RendererSection.cpp | 119 |
1 files changed, 74 insertions, 45 deletions
diff --git a/src/RendererSection.cpp b/src/RendererSection.cpp index 609333c..0965453 100644 --- a/src/RendererSection.cpp +++ b/src/RendererSection.cpp @@ -32,15 +32,21 @@ RendererSection::~RendererSection() { refCounterVbo[VboTextures]--; refCounterVbo[VboModels]--; refCounterVbo[VboColors]--; + refCounterVbo[VboLights]--; refCounterVao[Vao]--; + if (refCounterVbo[VboTextures] <= 0) glDeleteBuffers(1, &VboTextures); if (refCounterVbo[VboModels] <= 0) glDeleteBuffers(1, &VboTextures); + if (refCounterVbo[VboColors] <= 0) glDeleteBuffers(1, &VboColors); + if (refCounterVbo[VboLights] <= 0) + glDeleteBuffers(1, &VboLights); + if (refCounterVao[Vao] <= 0) glDeleteVertexArrays(1, &Vao); } @@ -93,6 +99,11 @@ RendererSection::RendererSection(RendererSectionData data) { refCounterVbo[VboColors] = 0; refCounterVbo[VboColors]++; + glGenBuffers(1, &VboLights); + if (refCounterVbo.find(VboLights) == refCounterVbo.end()) + refCounterVbo[VboLights] = 0; + refCounterVbo[VboLights]++; + glGenVertexArrays(1, &Vao); if (refCounterVao.find(Vao) == refCounterVao.end()) refCounterVao[Vao] = 0; @@ -108,43 +119,50 @@ RendererSection::RendererSection(RendererSectionData data) { //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); + 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); + + //Light + GLuint lightAttribPos = 13; + glBindBuffer(GL_ARRAY_BUFFER, VboLights); + glVertexAttribPointer(lightAttribPos, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(GLfloat), nullptr); + glEnableVertexAttribArray(lightAttribPos); + glVertexAttribDivisor(lightAttribPos, 1); + + glBindBuffer(GL_ARRAY_BUFFER, 0); } glBindVertexArray(0); glCheckError(); @@ -160,6 +178,9 @@ glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ARRAY_BUFFER, VboColors); glBufferData(GL_ARRAY_BUFFER, data.colors.size() * sizeof(glm::vec3), data.colors.data(), GL_DYNAMIC_DRAW); + glBindBuffer(GL_ARRAY_BUFFER, VboLights); + glBufferData(GL_ARRAY_BUFFER, data.lights.size() * sizeof(glm::vec2), data.lights.data(), GL_DYNAMIC_DRAW); + glBindBuffer(GL_ARRAY_BUFFER, 0); numOfFaces = data.textures.size(); @@ -171,6 +192,7 @@ RendererSection::RendererSection(const RendererSection &other) { this->VboModels = other.VboModels; this->VboTextures = other.VboTextures; this->VboColors = other.VboColors; + this->VboLights = other.VboLights; this->sectionPos = other.sectionPos; this->Vao = other.Vao; this->numOfFaces = other.numOfFaces; @@ -179,6 +201,7 @@ RendererSection::RendererSection(const RendererSection &other) { refCounterVbo[VboTextures]++; refCounterVbo[VboModels]++; refCounterVbo[VboColors]++; + refCounterVbo[VboLights]++; refCounterVao[Vao]++; } @@ -193,7 +216,7 @@ RendererSectionData::RendererSectionData(World * world, Vector sectionPosition) for (int x = 0; x < 16; x++) { Block block = section.GetBlock(Vector(x, y, z)); if (block.id == 0) - continue; + continue; auto testBlockNonExist = [&](Vector block) -> bool { Vector offset; @@ -216,28 +239,22 @@ RendererSectionData::RendererSectionData(World * world, Vector sectionPosition) offset = Vector(0, 0, 1); block.z = 0; } - if (offset != Vector(0, 0, 0)) { + if (offset != Vector(0, 0, 0)) { if (std::find(sectionsList.begin(), sectionsList.end(), sectionPosition + offset) == sectionsList.end()) return true; const Section& blockSection = world->GetSection(sectionPosition + offset); return blockSection.GetBlock(block).id == 0 || blockSection.GetBlock(block).id == 31 || blockSection.GetBlock(block).id == 18; } return section.GetBlock(block).id == 0 || section.GetBlock(block).id == 31 || section.GetBlock(block).id == 18; - /*return block.x == -1 || block.x == 16 || block.y == -1 || block.y == 16 || block.z == -1 || block.z == 16 || - 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 |= testBlockNonExist(Vector(x - 1, y, z)) << 0; isVisible |= testBlockNonExist(Vector(x + 1, y, z)) << 1; isVisible |= testBlockNonExist(Vector(x, y + 1, z)) << 2; isVisible |= testBlockNonExist(Vector(x, y - 1, z)) << 3; isVisible |= testBlockNonExist(Vector(x, y, z - 1)) << 4; isVisible |= testBlockNonExist(Vector(x, y, z + 1)) << 5; - //} if (isVisible == 0x00) continue; @@ -255,6 +272,7 @@ RendererSectionData::RendererSectionData(World * world, Vector sectionPosition) for (int i = 0; i < 4; i++) { textures.push_back(texture->second); colors.push_back(color); + lights.push_back(glm::vec2(block.light, block.sky)); } 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)); @@ -280,6 +298,7 @@ RendererSectionData::RendererSectionData(World * world, Vector sectionPosition) textures.push_back(glm::vec4(0.0546875, 0.00442477876106194690, 0.0078125, 0.00442477876106194690)); //Fallback TNT texture colors.push_back(color); + lights.push_back(glm::vec2(block.light, block.sky)); } if (isVisible >> 1 & 0x1) { //west side X- glm::mat4 faceTransform = glm::translate(transform, glm::vec3(1, 0, 0)); @@ -294,6 +313,7 @@ RendererSectionData::RendererSectionData(World * world, Vector sectionPosition) textures.push_back(glm::vec4(0.0546875, 0.00442477876106194690, 0.0078125, 0.00442477876106194690)); //Fallback TNT texture colors.push_back(color); + lights.push_back(glm::vec2(block.light, block.sky)); } if (isVisible >> 2 & 0x1) { //Top side Y+ glm::mat4 faceTransform = glm::translate(transform, glm::vec3(0, 1, 0)); @@ -308,6 +328,7 @@ RendererSectionData::RendererSectionData(World * world, Vector sectionPosition) colors.push_back(color); else colors.push_back(biomeColor); + lights.push_back(glm::vec2(block.light, block.sky)); } if (isVisible >> 3 & 0x1) { //Bottom side Y- glm::mat4 faceTransform = glm::translate(transform, glm::vec3(0, 0, 0)); @@ -321,6 +342,7 @@ RendererSectionData::RendererSectionData(World * world, Vector sectionPosition) textures.push_back(glm::vec4(0.0546875, 0.00442477876106194690, 0.0078125, 0.00442477876106194690)); //Fallback TNT texture colors.push_back(color); + lights.push_back(glm::vec2(block.light, block.sky)); } if (isVisible >> 4 & 0x1) { //south side Z+ glm::mat4 faceTransform = glm::translate(transform, glm::vec3(1, 0, 0)); @@ -334,6 +356,7 @@ RendererSectionData::RendererSectionData(World * world, Vector sectionPosition) textures.push_back(glm::vec4(0.0546875, 0.00442477876106194690, 0.0078125, 0.00442477876106194690)); //Fallback TNT texture colors.push_back(color); + lights.push_back(glm::vec2(block.light, block.sky)); } if (isVisible >> 5 & 0x1) { //north side Z- glm::mat4 faceTransform = glm::translate(transform, glm::vec3(0, 0, 1)); @@ -350,6 +373,7 @@ RendererSectionData::RendererSectionData(World * world, Vector sectionPosition) textures.push_back(glm::vec4(0.0546875, 0.00442477876106194690, 0.0078125, 0.00442477876106194690)); //Fallback TNT texture colors.push_back(color); + lights.push_back(glm::vec2(block.light, block.sky)); } } } @@ -359,4 +383,9 @@ RendererSectionData::RendererSectionData(World * world, Vector sectionPosition) textures.shrink_to_fit(); models.shrink_to_fit(); colors.shrink_to_fit(); + + /*for (auto& it : lights) { + it.x = 8; + it.y = 16; + }*/ } |