diff options
-rw-r--r-- | src/AssetManager.cpp | 45 | ||||
-rw-r--r-- | src/AssetManager.hpp | 8 | ||||
-rw-r--r-- | src/RendererSectionData.cpp | 29 |
3 files changed, 55 insertions, 27 deletions
diff --git a/src/AssetManager.cpp b/src/AssetManager.cpp index cca31e8..482a028 100644 --- a/src/AssetManager.cpp +++ b/src/AssetManager.cpp @@ -23,7 +23,7 @@ std::map<std::string, BlockId> assetIds; std::map<BlockId, std::string> blockIdToBlockName; std::unique_ptr<AssetTreeNode> assetTree; std::unique_ptr<TextureAtlas> atlas; -std::map<BlockId, const BlockModel *> blockIdToBlockModel; +std::map<BlockId, BlockFaces> blockIdToBlockFaces; void LoadIds(); void LoadAssets(); @@ -401,7 +401,7 @@ void ParseBlockModels() { } elementTransform = glm::translate(elementTransform, elementOrigin.glm()); - elementTransform = glm::scale(elementTransform, elementSize.glm()); + elementTransform = glm::scale(elementTransform, elementSize.glm()); for (const auto& face : element.faces) { ParsedFace parsedFace; @@ -496,32 +496,55 @@ void ParseBlockModels() { AssetManager::RecursiveWalkAsset("/minecraft/models/", parseBlockModel); } -const BlockModel *AssetManager::GetBlockModelByBlockId(BlockId block) { - auto it = blockIdToBlockModel.find(block); - if (it != blockIdToBlockModel.end()) +BlockFaces &AssetManager::GetBlockModelByBlockId(BlockId block) { + auto it = blockIdToBlockFaces.find(block); + if (it != blockIdToBlockFaces.end()) return it->second; + if (block.id == 7788) { + BlockFaces blockFaces; + blockFaces.faces = GetAsset<AssetBlockModel>("/minecraft/models/block/error")->blockModel.parsedFaces; + blockFaces.isBlock = GetAsset<AssetBlockModel>("/minecraft/models/block/error")->blockModel.IsBlock; + blockIdToBlockFaces.insert(std::make_pair(block, blockFaces)); + return blockIdToBlockFaces.find(block)->second; + } + auto blockStateName = TransformBlockIdToBlockStateName(block); AssetBlockState *asset = GetAsset<AssetBlockState>("/minecraft/blockstates/" + blockStateName.first); if (!asset) - return &GetAsset<AssetBlockModel>("/minecraft/models/block/error")->blockModel; + return GetBlockModelByBlockId(BlockId{ 7788,0 }); BlockState &blockState = asset->blockState; if (blockState.variants.find(blockStateName.second) == blockState.variants.end()) - return &GetAsset<AssetBlockModel>("/minecraft/models/block/error")->blockModel; + return GetBlockModelByBlockId(BlockId{ 7788,0 }); BlockStateVariant &variant = blockState.variants[blockStateName.second]; if (variant.models.empty()) - return &GetAsset<AssetBlockModel>("/minecraft/models/block/error")->blockModel; + return GetBlockModelByBlockId(BlockId{ 7788,0 }); BlockStateVariant::Model &model = variant.models[0]; AssetBlockModel *assetModel = GetAsset<AssetBlockModel>("/minecraft/models/block/" + model.modelName); if (!assetModel) - return &GetAsset<AssetBlockModel>("/minecraft/models/block/error")->blockModel; + return GetBlockModelByBlockId(BlockId{ 7788,0 }); - blockIdToBlockModel.insert(std::make_pair(block, &assetModel->blockModel)); + BlockFaces blockFaces; + blockFaces.faces = assetModel->blockModel.parsedFaces; + blockFaces.isBlock = assetModel->blockModel.IsBlock; + + if (model.x != 0) { + blockFaces.transform = glm::translate(blockFaces.transform, glm::vec3(0.0f, 0.5f, 0.5f)); + blockFaces.transform = glm::rotate(blockFaces.transform, glm::radians((float)model.x), glm::vec3(1.0f, 0.0f, 0.0f)); + blockFaces.transform = glm::translate(blockFaces.transform, glm::vec3(0.0f, -0.5f, -0.5f)); + } + if (model.y != 0) { + blockFaces.transform = glm::translate(blockFaces.transform, glm::vec3(0.5f, 0.0f, 0.5f)); + blockFaces.transform = glm::rotate(blockFaces.transform, glm::radians((float)model.y), glm::vec3(0.0f, 1.0f, 0.0f)); + blockFaces.transform = glm::translate(blockFaces.transform, glm::vec3(-0.5f, 0.0f, -0.5f)); + } + + blockIdToBlockFaces.insert(std::make_pair(block, blockFaces)); - return &assetModel->blockModel; + return blockIdToBlockFaces.find(block)->second; } std::string AssetManager::GetAssetNameByBlockId(BlockId block) { diff --git a/src/AssetManager.hpp b/src/AssetManager.hpp index 992bd51..28cafb0 100644 --- a/src/AssetManager.hpp +++ b/src/AssetManager.hpp @@ -32,6 +32,12 @@ struct ParsedFace { glm::vec3 color; }; +struct BlockFaces { + glm::mat4 transform; + std::vector<ParsedFace> faces; + bool isBlock; +}; + struct BlockModel { bool IsBlock = false; std::string BlockName; @@ -145,7 +151,7 @@ struct AssetTexture : Asset { namespace AssetManager { void InitAssetManager(); - const BlockModel *GetBlockModelByBlockId(BlockId block); + BlockFaces &GetBlockModelByBlockId(BlockId block); std::string GetAssetNameByBlockId(BlockId block); diff --git a/src/RendererSectionData.cpp b/src/RendererSectionData.cpp index 5973dd5..6a6a7c0 100644 --- a/src/RendererSectionData.cpp +++ b/src/RendererSectionData.cpp @@ -14,8 +14,8 @@ inline const BlockId& GetBlockId(int x, int y, int z, const std::array<BlockId, return blockIdData[y * 256 + z * 16 + x]; } -void AddFacesByBlockModel(RendererSectionData &data, const BlockModel &model, const glm::mat4 &transform, unsigned char visibility, BlockLightness light, BlockLightness skyLight) { - for (const auto &face : model.parsedFaces) { +void AddFacesByBlockModel(RendererSectionData &data, const BlockFaces &model, const glm::mat4 &transform, unsigned char visibility, BlockLightness light, BlockLightness skyLight) { + for (const auto &face : model.faces) { glm::vec2 lightness; lightness.x = _max(light.face[0], light.face[1], light.face[2], light.face[3], light.face[4], light.face[5]); lightness.y = _max(skyLight.face[0], skyLight.face[1], skyLight.face[2], skyLight.face[3], skyLight.face[4], skyLight.face[5]); @@ -59,8 +59,7 @@ void AddFacesByBlockModel(RendererSectionData &data, const BlockModel &model, co } } } - - data.models.push_back(transform * face.transform); + data.models.push_back(transform * model.transform * face.transform); data.textures.push_back(face.texture); data.textureLayers.push_back(face.layer); data.lights.push_back(lightness); @@ -68,16 +67,16 @@ void AddFacesByBlockModel(RendererSectionData &data, const BlockModel &model, co } } -const BlockModel* GetInternalBlockModel(const BlockId& id, std::vector<std::pair<BlockId, const BlockModel *>> &idModels) { +BlockFaces *GetInternalBlockModel(const BlockId& id, std::vector<std::pair<BlockId, BlockFaces*>> &idModels) { for (const auto& it : idModels) { if (it.first == id) return it.second; } - idModels.push_back(std::make_pair(id, AssetManager::GetBlockModelByBlockId(id))); + idModels.push_back(std::make_pair(id, &AssetManager::GetBlockModelByBlockId(id))); return idModels.back().second; } -std::array<unsigned char, 4096> GetBlockVisibilityData(const SectionsData §ions, const std::array<BlockId, 4096> &blockIdData, std::vector<std::pair<BlockId, const BlockModel *>> &idModels) { +std::array<unsigned char, 4096> GetBlockVisibilityData(const SectionsData §ions, const std::array<BlockId, 4096> &blockIdData, std::vector<std::pair<BlockId, BlockFaces*>> &idModels) { std::array<unsigned char, 4096> arr; for (int y = 0; y < 16; y++) { for (int z = 0; z < 16; z++) { @@ -142,12 +141,12 @@ std::array<unsigned char, 4096> GetBlockVisibilityData(const SectionsData §i auto blockModelWest = GetInternalBlockModel(blockIdWest, idModels); auto blockModelEast = GetInternalBlockModel(blockIdEast, idModels); - value |= (blockIdDown.id != 0 && blockModelDown && blockModelDown->IsBlock) << 0; - value |= (blockIdUp.id != 0 && blockModelUp && blockModelUp->IsBlock) << 1; - value |= (blockIdNorth.id != 0 && blockModelNorth && blockModelNorth->IsBlock) << 2; - value |= (blockIdSouth.id != 0 && blockModelSouth && blockModelSouth->IsBlock) << 3; - value |= (blockIdWest.id != 0 && blockModelWest && blockModelWest->IsBlock) << 4; - value |= (blockIdEast.id != 0 && blockModelEast && blockModelEast->IsBlock) << 5; + value |= (blockIdDown.id != 0 && !blockModelDown->faces.empty() && blockModelDown->isBlock) << 0; + value |= (blockIdUp.id != 0 && !blockModelUp->faces.empty() && blockModelUp->isBlock) << 1; + value |= (blockIdNorth.id != 0 && !blockModelNorth->faces.empty() && blockModelNorth->isBlock) << 2; + value |= (blockIdSouth.id != 0 && !blockModelSouth->faces.empty() && blockModelSouth->isBlock) << 3; + value |= (blockIdWest.id != 0 && !blockModelWest->faces.empty() && blockModelWest->isBlock) << 4; + value |= (blockIdEast.id != 0 && !blockModelEast->faces.empty() && blockModelEast->isBlock) << 5; arr[y * 256 + z * 16 + x] = value; } @@ -172,7 +171,7 @@ RendererSectionData ParseSection(const SectionsData §ions) { RendererSectionData data; - std::vector<std::pair<BlockId, const BlockModel *>> idModels; + std::vector<std::pair<BlockId, BlockFaces*>> idModels; std::array<BlockId, 4096> blockIdData = SetBlockIdData(sections); std::array<unsigned char, 4096> blockVisibility = GetBlockVisibilityData(sections, blockIdData, idModels); std::string textureName; @@ -196,7 +195,7 @@ RendererSectionData ParseSection(const SectionsData §ions) BlockLightness light = sections.GetLight(vec); BlockLightness skyLight = sections.GetSkyLight(vec); - const BlockModel* model = GetInternalBlockModel(block, idModels); + BlockFaces *model = GetInternalBlockModel(block, idModels); AddFacesByBlockModel(data, *model, transform, blockVisibility[y * 256 + z * 16 + x], light, skyLight); } } |