From 737747a46c3068d6187fa8fa02189dc8410a16bc Mon Sep 17 00:00:00 2001 From: Derek Christ Date: Mon, 11 Jan 2021 15:26:55 +0100 Subject: [PATCH] Add support for initial values in json file --- res/models.json | 16 ++++++++++------ src/Controller.cpp | 6 +++--- src/JsonParser.cpp | 22 ++++++++++++++++++++++ 3 files changed, 35 insertions(+), 9 deletions(-) diff --git a/res/models.json b/res/models.json index b0cf78e..3cb6dd3 100644 --- a/res/models.json +++ b/res/models.json @@ -1,11 +1,12 @@ { "models": [ + { + "unique_name": "fallback", + "path": "res/models/fallback.ffo" + }, { "unique_name": "backpack", - "path": "res/models/backpack.ffo", - - "position": ["0.0", "0.0", "0.0"], - "rotation": ["0.0", "0.0", "0.0"] + "path": "res/models/backpack.ffo" }, { "unique_name": "ground", @@ -20,12 +21,15 @@ { "unique_name": "backpack", "model": "backpack", - "shaderProgram": "defaultProgram" + "shaderProgram": "defaultProgram", + "position": [0.0, 1.0, 0.0], + "rotation": [0.0, 0.0, 0.0], + "scale": 0.6 }, { "unique_name": "ground", "model": "ground", - "shaderProgram": "defaultProgram" + "shaderProgram": "defaultProgram", }, { "unique_name": "light", diff --git a/src/Controller.cpp b/src/Controller.cpp index b60ec76..29b085e 100644 --- a/src/Controller.cpp +++ b/src/Controller.cpp @@ -276,11 +276,11 @@ void Controller::renderImGui(World &world, PointLight *pointLight, glm::vec3 *li ImGui::Checkbox("Rotate Object", rotateEntity); Entity *mainObject = world.getEntityById(0); - mainObject->setPosition(glm::vec3(translation[0], translation[1], translation[2])); + //mainObject->setPosition(glm::vec3(translation[0], translation[1], translation[2])); if (!*rotateEntity) { - mainObject->setRotation(glm::vec3(0.f, 1.0f, 0.f), rotation); + //mainObject->setRotation(glm::vec3(0.f, 1.0f, 0.f), rotation); } - mainObject->setScale(scale); + //mainObject->setScale(scale); // color picker ImGui::Text("\nLight Source"); diff --git a/src/JsonParser.cpp b/src/JsonParser.cpp index 581ba64..d75f5d2 100644 --- a/src/JsonParser.cpp +++ b/src/JsonParser.cpp @@ -54,6 +54,8 @@ std::vector JsonParser::getEntities(std::vector &models, std::v std::string entity_name = entitiesJson[index]["unique_name"].asString(); std::string entity_model = entitiesJson[index]["model"].asString(); std::string entity_shaderProgram = entitiesJson[index]["shaderProgram"].asString(); + glm::vec3 entitiy_position = {}, entity_rotation = {}; + float entity_scale = 1.0f; ShaderProgram *shaderProgram = nullptr; for (auto it = shaderPrograms.begin(); it != shaderPrograms.end(); it++) { @@ -73,7 +75,27 @@ std::vector JsonParser::getEntities(std::vector &models, std::v if(!current_model) std::cout << "[Warning] Model could not be found by unique name \"" << entity_model << "\"" << std::endl; + const Json::Value positionJson = entitiesJson[index]["position"]; + const Json::Value rotationJson = entitiesJson[index]["rotation"]; + const Json::Value scaleJson = entitiesJson[index]["scale"]; + if(!positionJson.empty()) { + entitiy_position.x = positionJson[0].asFloat(); + entitiy_position.y = positionJson[1].asFloat(); + entitiy_position.z = positionJson[2].asFloat(); + } + if(!rotationJson.empty()) { + entity_rotation.s = rotationJson[0].asFloat(); + entity_rotation.t = rotationJson[1].asFloat(); + entity_rotation.p = rotationJson[2].asFloat(); + } + if(!scaleJson.empty()) { + entity_scale = scaleJson.asFloat(); + } + Entity *current_entity = new Entity(entity_name, current_model, shaderProgram); + current_entity->setPosition(entitiy_position); + current_entity->setRotation(entity_rotation); + current_entity->setScale(entity_scale); temp_entities.push_back(current_entity); std::cout << "Loaded Entity \"" << entity_name << "\" with model \"" << entity_model << "\"" << std::endl; }