From 145ba8e157f79db64203db9684af2e6ed33af075 Mon Sep 17 00:00:00 2001 From: LaG1924 <12997935+LaG1924@users.noreply.github.com> Date: Sun, 3 Sep 2017 20:45:52 +0500 Subject: 2017-09-03 --- src/AssetManager.cpp | 157 +++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 153 insertions(+), 4 deletions(-) (limited to 'src/AssetManager.cpp') diff --git a/src/AssetManager.cpp b/src/AssetManager.cpp index 22dac8d..6e35160 100644 --- a/src/AssetManager.cpp +++ b/src/AssetManager.cpp @@ -1,5 +1,8 @@ #include -#include "AssetManager.hpp" +#include "AssetManager.hpp" +#include + +namespace fs = std::experimental::filesystem::v1; //const fs::path pathToAssets = "./assets/"; //const fs::path pathToAssetsList = "./items.json"; @@ -7,9 +10,12 @@ const std::string pathToAssetsList = "./items.json"; const std::string pathToTextureIndex = "./textures.json"; +const fs::path pathToModels = "./assets/minecraft/models/"; + AssetManager::AssetManager() { LoadIds(); LoadTextureResources(); + //LoadBlockModels(); } void AssetManager::LoadIds() { @@ -17,10 +23,10 @@ void AssetManager::LoadIds() { nlohmann::json index; in >> index; for (auto &it:index) { - int id = it["type"].get(); - int state = it["meta"].get(); + unsigned short id = it["type"].get(); + unsigned char state = it["meta"].get(); std::string blockName = it["text_type"].get(); - assetIds[blockName] = Block(id, state, 0, 0); + assetIds[blockName] = BlockId{ id, state }; } LOG(INFO) << "Loaded " << assetIds.size() << " ids"; } @@ -186,3 +192,146 @@ AssetManager &AssetManager::Instance() { static AssetManager assetManager; return assetManager; } + +const BlockModel *AssetManager::GetBlockModelByBlockId(BlockId block) { + std::string blockName = ""; + for (const auto& it : assetIds) { + if (BlockId{ it.second.id,0 } == BlockId{ block.id,0 }) { + blockName = it.first; + break; + } + } + + blockName = "block/" + blockName; + + if (blockName == "" || models.find(blockName) == models.end()) + return nullptr; + + return &models[blockName]; +} + +void AssetManager::LoadBlockModels() { + + std::function parseModel = [&](std::string ModelName) { + fs::path ModelPath = pathToModels / fs::path(ModelName + ".json"); + std::ifstream in(ModelPath); + if (!in.is_open()) + throw std::runtime_error("Trying to load unknown model \"" + ModelName + "\" at " + ModelPath.generic_string()); + nlohmann::json modelData; + in >> modelData; + BlockModel model; + + if (modelData.find("parent") != modelData.end()) { + if (models.find(modelData["parent"]) == models.end()) + parseModel(modelData["parent"].get()); + + model = models.find(modelData["parent"])->second; + } + + if (modelData.find("ambientocclusion") != modelData.end()) + model.AmbientOcclusion = modelData["ambientocclusion"].get(); + + //models.Display + + if (modelData.find("textures") != modelData.end()) { + for (nlohmann::json::iterator texture = modelData["textures"].begin(); texture != modelData["textures"].end(); ++texture) { + model.Textures[texture.key()] = texture.value().get(); + } + } + + if (modelData.find("elements") != modelData.end()) { + for (auto& it : modelData["elements"]) { + BlockModel::ElementData element; + + auto vec = it["from"]; + Vector from (vec[0].get(), vec[1].get(), vec[2].get()); + vec = it["to"]; + Vector to(vec[0].get(), vec[1].get(), vec[2].get()); + + element.from = from; + element.to = to; + + if (it.find("rotation") != it.end()) { + vec = it["rotation"]["origin"]; + Vector rotOrig(vec[0].get(), vec[1].get(), vec[2].get()); + + element.rotationOrigin = rotOrig; + element.rotationAxis = (it["rotation"]["axis"].get() == "x") ? BlockModel::ElementData::Axis::x : ((it["rotation"]["axis"].get() == "y") ? BlockModel::ElementData::Axis::y : BlockModel::ElementData::Axis::z); + element.rotationAngle = it["rotation"]["angle"].get(); + element.rotationRescale = it["rotation"]["recale"].get(); + } + + if (it.find("shade") != it.end()) + element.shade = it["shade"].get(); + + for (nlohmann::json::iterator faceIt = it["faces"].begin(); faceIt != it["faces"].end(); ++faceIt) { + auto face = faceIt.value(); + BlockModel::ElementData::FaceData faceData; + + BlockModel::ElementData::FaceDirection faceDir; + if (faceIt.key() == "down") + faceDir = BlockModel::ElementData::FaceDirection::down; + else if (faceIt.key() == "up") + faceDir = BlockModel::ElementData::FaceDirection::up; + else if (faceIt.key() == "north") + faceDir = BlockModel::ElementData::FaceDirection::north; + else if (faceIt.key() == "south") + faceDir = BlockModel::ElementData::FaceDirection::south; + else if (faceIt.key() == "west") + faceDir = BlockModel::ElementData::FaceDirection::west; + else if (faceIt.key() == "east") + faceDir = BlockModel::ElementData::FaceDirection::east; + + if (face.find("uv") != face.end()) { + BlockModel::ElementData::FaceData::Uv uv; + uv.x1 = face["uv"][0]; + uv.y1 = face["uv"][1]; + uv.x2 = face["uv"][2]; + uv.y2 = face["uv"][3]; + faceData.uv = uv; + } + + BlockModel::ElementData::FaceDirection cullface; + if (face.find("cullface") != face.end()) { + if (face["cullface"] == "down") + cullface = BlockModel::ElementData::FaceDirection::down; + else if (face["cullface"] == "up") + cullface = BlockModel::ElementData::FaceDirection::up; + else if (face["cullface"] == "north") + cullface = BlockModel::ElementData::FaceDirection::north; + else if (face["cullface"] == "south") + cullface = BlockModel::ElementData::FaceDirection::south; + else if (face["cullface"] == "west") + cullface = BlockModel::ElementData::FaceDirection::west; + else if (face["cullface"] == "east") + cullface = BlockModel::ElementData::FaceDirection::east; + faceData.cullface = cullface; + } + + faceData.texture = face["texture"].get(); + + if (face.find("rotation") != face.end()) + faceData.rotation = face["rotation"].get(); + + if (face.find("tintindex") != face.end()) + faceData.tintIndex = face["tintindex"]; + + element.faces[faceDir] = faceData; + } + + model.Elements.push_back(element); + } + } + + models.insert(std::make_pair(ModelName, model)); + }; + + parseModel("block/stone"); + + /*for (auto& dirEntry : fs::directory_iterator(pathToBlockModels)) { + if (dirEntry.path().extension() != ".json") + continue; + + parseModel(dirEntry.path().generic_string()); + }*/ +} \ No newline at end of file -- cgit v1.2.3