From 8616ce6a0bfd4a6c31d4f6dded4137ad9eb2ba2f Mon Sep 17 00:00:00 2001 From: LaG1924 <12997935+LaG1924@users.noreply.github.com> Date: Tue, 13 Mar 2018 21:55:58 +0500 Subject: Optimized light parsing --- src/RendererSectionData.cpp | 14 ++++++++++++-- src/World.cpp | 18 ++++++++++++++---- src/World.hpp | 4 ++++ 3 files changed, 30 insertions(+), 6 deletions(-) diff --git a/src/RendererSectionData.cpp b/src/RendererSectionData.cpp index 067728f..e3c4870 100644 --- a/src/RendererSectionData.cpp +++ b/src/RendererSectionData.cpp @@ -265,6 +265,13 @@ RendererSectionData ParseSection(World * world, Vector sectionPosition) std::array blockIdData = SetBlockIdData(world, sectionPosition); std::array blockVisibility = GetBlockVisibilityData(world, sectionPosition, blockIdData, idModels); std::string textureName; + + Section* yp = world->GetSectionPtr(sectionPosition + Vector(0, 1, 0)); + Section* yn = world->GetSectionPtr(sectionPosition + Vector(0, -1, 0)); + Section* xp = world->GetSectionPtr(sectionPosition + Vector(1, 0, 0)); + Section* xn = world->GetSectionPtr(sectionPosition + Vector(-1, 0, 0)); + Section* zp = world->GetSectionPtr(sectionPosition + Vector(0, 0, 1)); + Section* zn = world->GetSectionPtr(sectionPosition + Vector(0, 0, -1)); const std::map &textureAtlas = AssetManager::Instance().GetTextureAtlasIndexes(); const Section §ion = world->GetSection(sectionPosition); @@ -287,9 +294,12 @@ RendererSectionData ParseSection(World * world, Vector sectionPosition) transform = glm::translate(baseOffset, Vector(x, y, z).glm()); + unsigned char light = world->GetBlockLight(Vector(x, y, z), §ion, xp, xn, yp, yn, zp, zn); + unsigned char skyLight = world->GetBlockSkyLight(Vector(x, y, z), §ion, xp, xn, yp, yn, zp, zn); + const BlockModel* model = GetInternalBlockModel(block, idModels); if (model) { - AddFacesByBlockModel(sectionsList, world, Vector(x, y, z), *model, transform, world->GetBlockLight(Vector(x, y, z) + sectionPosition * 16), world->GetBlockSkyLight(Vector(x, y, z) + sectionPosition * 16), blockVisibility, textureName, data); + AddFacesByBlockModel(sectionsList, world, Vector(x, y, z), *model, transform, light, skyLight, blockVisibility, textureName, data); } else { transform = glm::translate(transform, glm::vec3(0, 1, 0)); @@ -304,7 +314,7 @@ RendererSectionData ParseSection(World * world, Vector sectionPosition) data.models.push_back(transform); data.colors.push_back(glm::vec3(0, 0, 0)); - data.lights.push_back(glm::vec2(world->GetBlockLight(Vector(x, y, z) + sectionPosition * 16), world->GetBlockSkyLight(Vector(x, y, z) + sectionPosition * 16))); + data.lights.push_back(glm::vec2(light, skyLight)); } } diff --git a/src/World.cpp b/src/World.cpp index 75e0b3f..f9eb560 100644 --- a/src/World.cpp +++ b/src/World.cpp @@ -416,7 +416,12 @@ unsigned char World::GetBlockLight(Vector pos) if (!section) return 0; - Vector directions[] = { + return GetBlockLight(blockPos, section, xp, xn, yp, yn, zp, zn); +} + +unsigned char World::GetBlockLight(const Vector &blockPos, const Section *section, const Section *xp, const Section *xn, const Section *yp, const Section *yn, const Section *zp, const Section *zn) +{ + const Vector directions[] = { Vector(0,0,0), Vector(1,0,0), Vector(-1,0,0), @@ -447,7 +452,7 @@ unsigned char World::GetBlockLight(Vector pos) dirValue = zp->GetBlockLight(Vector(vec.x, vec.y, 0)); } else dirValue = section->GetBlockLight(vec); - + value = _max(value, dirValue); } return value; @@ -472,7 +477,12 @@ unsigned char World::GetBlockSkyLight(Vector pos) if (!section) return 0; - Vector directions[] = { + return GetBlockSkyLight(blockPos, section, xp, xn, yp, yn, zp, zn); +} + +unsigned char World::GetBlockSkyLight(const Vector &blockPos, const Section *section, const Section *xp, const Section *xn, const Section *yp, const Section *yn, const Section *zp, const Section *zn) +{ + const Vector directions[] = { Vector(0,0,0), Vector(1,0,0), Vector(-1,0,0), @@ -508,4 +518,4 @@ unsigned char World::GetBlockSkyLight(Vector pos) value = _max(value, dirValue); } return value; -} \ No newline at end of file +} diff --git a/src/World.hpp b/src/World.hpp index 6800e46..165e73d 100644 --- a/src/World.hpp +++ b/src/World.hpp @@ -87,5 +87,9 @@ public: unsigned char GetBlockLight(Vector pos); + unsigned char GetBlockLight(const Vector &blockPos, const Section *section, const Section *xp, const Section *xn, const Section *yp, const Section *yn, const Section *zp, const Section *zn); + unsigned char GetBlockSkyLight(Vector pos); + + unsigned char GetBlockSkyLight(const Vector &blockPos, const Section *section, const Section *xp, const Section *xn, const Section *yp, const Section *yn, const Section *zp, const Section *zn); }; \ No newline at end of file -- cgit v1.2.3