From 4306d164722ca00add1414b9769dd20e11b1acb2 Mon Sep 17 00:00:00 2001 From: LaG1924 <12997935+LaG1924@users.noreply.github.com> Date: Fri, 10 Aug 2018 06:06:25 +0500 Subject: Support rotation in BlockStates --- src/AssetManager.cpp | 45 ++++++++++++++++++++++++++++++++++----------- src/AssetManager.hpp | 8 +++++++- 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 assetIds; std::map blockIdToBlockName; std::unique_ptr assetTree; std::unique_ptr atlas; -std::map blockIdToBlockModel; +std::map 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("/minecraft/models/block/error")->blockModel.parsedFaces; + blockFaces.isBlock = GetAsset("/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("/minecraft/blockstates/" + blockStateName.first); if (!asset) - return &GetAsset("/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("/minecraft/models/block/error")->blockModel; + return GetBlockModelByBlockId(BlockId{ 7788,0 }); BlockStateVariant &variant = blockState.variants[blockStateName.second]; if (variant.models.empty()) - return &GetAsset("/minecraft/models/block/error")->blockModel; + return GetBlockModelByBlockId(BlockId{ 7788,0 }); BlockStateVariant::Model &model = variant.models[0]; AssetBlockModel *assetModel = GetAsset("/minecraft/models/block/" + model.modelName); if (!assetModel) - return &GetAsset("/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 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> &idModels) { +BlockFaces *GetInternalBlockModel(const BlockId& id, std::vector> &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 GetBlockVisibilityData(const SectionsData §ions, const std::array &blockIdData, std::vector> &idModels) { +std::array GetBlockVisibilityData(const SectionsData §ions, const std::array &blockIdData, std::vector> &idModels) { std::array arr; for (int y = 0; y < 16; y++) { for (int z = 0; z < 16; z++) { @@ -142,12 +141,12 @@ std::array 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> idModels; + std::vector> idModels; std::array blockIdData = SetBlockIdData(sections); std::array 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); } } -- cgit v1.2.3