diff --git a/Levels/ObjectSetups/Level0.xml b/Levels/ObjectSetups/Level0.xml index 6fcee82..20a74a6 100644 --- a/Levels/ObjectSetups/Level0.xml +++ b/Levels/ObjectSetups/Level0.xml @@ -3,6 +3,7 @@ 0.0 0.0 0.0 + false 3.500000 0.0 3.500000 @@ -16,6 +17,7 @@ 0.0 0.0 0.0 + false 3.500000 0.0 -0.500000 @@ -29,6 +31,7 @@ 0.0 0.0 0.0 + false 2.500000 0.0 -2.500000 @@ -42,6 +45,7 @@ 0.0 0.0 0.0 + false 1.500000 0.0 0.500000 @@ -55,6 +59,7 @@ 0.0 0.0 0.0 + false -0.500000 0.0 1.500000 @@ -68,6 +73,7 @@ 0.0 0.0 0.0 + false -0.500000 0.0 -1.500000 @@ -81,6 +87,7 @@ 0.0 0.0 0.0 + false -2.500000 0.0 -1.500000 @@ -94,6 +101,7 @@ 0.0 0.0 0.0 + false -3.500000 0.0 -3.500000 @@ -107,6 +115,7 @@ 0.0 0.0 0.0 + false -4.500000 0.0 -4.500000 diff --git a/converter/converter.cc b/converter/converter.cc index 8a62dd2..f60f039 100644 --- a/converter/converter.cc +++ b/converter/converter.cc @@ -2,6 +2,7 @@ #include #include #include +#include using namespace tinyxml2; @@ -41,9 +42,12 @@ Converter::Converter(std::string level){ }else{ XMLElement* thisComposition = doc->FirstChildElement("composition"); int idGreen, idBlue; + XMLError error=XML_NO_ERROR; for(; thisComposition; thisComposition=thisComposition->NextSiblingElement("composition")){ - thisComposition->QueryIntAttribute("idGreen", &idGreen); - thisComposition->QueryIntAttribute("idBlue", &idBlue); + error = thisComposition->FirstChildElement("idGreen")->QueryIntText(&idGreen); + errorCheck(error); + error = thisComposition->FirstChildElement("idBlue")->QueryIntText(&idBlue); + errorCheck(error); if(idGreen > nextID[0] || (idGreen == nextID[0] && idBlue > nextID[1])){ nextID[0] = idGreen; nextID[1] = idBlue; @@ -68,6 +72,7 @@ std::vector Converter::newComposition(unsigned int type, float pos XMLElement* zPos = doc->NewElement("zPos"); XMLElement* yOffset = doc->NewElement("yOffset"); XMLElement* xPos = doc->NewElement("xPos"); + XMLElement* manualPos = doc->NewElement("manualPos"); XMLElement* zRot = doc->NewElement("zRot"); XMLElement* yRot = doc->NewElement("yRot"); XMLElement* xRot = doc->NewElement("xRot"); @@ -79,6 +84,7 @@ std::vector Converter::newComposition(unsigned int type, float pos zPos->SetText(std::to_string(posZ).c_str()); yOffset->SetText("0.0"); xPos->SetText(std::to_string(posX).c_str()); + manualPos->SetText("false"); zRot->SetText("0.0"); yRot->SetText("0.0"); xRot->SetText("0.0"); @@ -90,6 +96,7 @@ std::vector Converter::newComposition(unsigned int type, float pos newComposition->InsertFirstChild(zPos); newComposition->InsertFirstChild(yOffset); newComposition->InsertFirstChild(xPos); + newComposition->InsertFirstChild(manualPos); newComposition->InsertFirstChild(zRot); newComposition->InsertFirstChild(yRot); newComposition->InsertFirstChild(xRot); @@ -106,23 +113,40 @@ std::vector Converter::newComposition(unsigned int type, float pos void Converter::updateComposition(unsigned int idG, unsigned int idB, float posX, float posZ){ XMLElement* thisComposition = doc->FirstChildElement("composition"); - int idGreen, idBlue; + int idGreen = 0, idBlue = 0; + bool compositionExists = false; for(; thisComposition; thisComposition=thisComposition->NextSiblingElement("composition")){ - thisComposition->QueryIntAttribute("idGreen", &idGreen); - thisComposition->QueryIntAttribute("idBlue", &idBlue); + XMLError error=XML_NO_ERROR; + error = thisComposition->FirstChildElement("idGreen")->QueryIntText(&idGreen); + errorCheck(error); + error = thisComposition->FirstChildElement("idBlue")->QueryIntText(&idBlue); + errorCheck(error); if(idGreen == idG && idBlue == idB){ - thisComposition->FirstChildElement("xPos")->SetText(std::to_string(posX).c_str()); - thisComposition->FirstChildElement("zPos")->SetText(std::to_string(posZ).c_str()); + bool manualPos; + error=thisComposition->FirstChildElement("manualPos")->QueryBoolText(&manualPos); + errorCheck(error); + if(!manualPos){ + thisComposition->FirstChildElement("xPos")->SetText(std::to_string(posX).c_str()); + thisComposition->FirstChildElement("zPos")->SetText(std::to_string(posZ).c_str()); + } + compositionExists = true; } } + if(!compositionExists){ + std::cout << "A composition has an ID in the png, but does'nt exist in the xml." << std::endl; + exit(-1); + } } void Converter::deleteComposition(unsigned int idG, unsigned int idB){ XMLElement* thisComposition = doc->FirstChildElement("composition"); int idGreen, idBlue; + XMLError error=XML_NO_ERROR; for(; thisComposition; thisComposition=thisComposition->NextSiblingElement("composition")){ - thisComposition->QueryIntAttribute("idGreen", &idGreen); - thisComposition->QueryIntAttribute("idBlue", &idBlue); + error = thisComposition->FirstChildElement("idGreen")->QueryIntText(&idGreen); + errorCheck(error); + error = thisComposition->FirstChildElement("idBlue")->QueryIntText(&idBlue); + errorCheck(error); if(idGreen == idG && idBlue == idB){ doc->DeleteChild(thisComposition); } @@ -138,13 +162,23 @@ std::vector Converter::getNextID(){ return nextID; } -/* finding a typeID in compositions: - XMLElement* thisComposition = compositions->FirstChildElement("composition"); - for(; thisComposition; thisComposition=thisComposition->NextSiblingElement("composition")){ - int thisType; - thisComposition->QueryIntAttribute("typeID", &thisType); - if(thisType == type){ - ... +void Converter::errorCheck(XMLError error){ + if (error) { + printf("XMLError: "); + if (error == XML_WRONG_ATTRIBUTE_TYPE) { + printf("Wrong attribute type.\n"); + } + else if (error == XML_NO_ATTRIBUTE) { + printf("No attribute.\n"); + } + else if (error == XML_CAN_NOT_CONVERT_TEXT) { + printf("Can not convert text.\n"); + } + else if (error == XML_NO_TEXT_NODE) { + printf("No text.\n"); + } + else { + printf("Unknown error.\n"); } } -*/ +} diff --git a/converter/converter.hh b/converter/converter.hh index d0d2e62..6ed83dc 100644 --- a/converter/converter.hh +++ b/converter/converter.hh @@ -19,6 +19,7 @@ class Converter { private: std::vector nextID; + void errorCheck(XMLError error); std::string xmlFile; XMLDocument* doc = new XMLDocument(); XMLDocument* compositions = new XMLDocument(); diff --git a/level.cc b/level.cc index 87b4714..82140c7 100644 --- a/level.cc +++ b/level.cc @@ -47,7 +47,22 @@ void Level::load() { // currently hard coded should later read this stuff out of a file this->camera = Camera(glm::vec2(-0.8f, 0.0f), 3.0f); - //Loading Objects via xml: + // load terrain + this->terrain.load(); + Model terrainModel = Model(this->terrain.getModel()); + // load a texture: + Material terrainMaterial = Material("seamlessTerrain.png", 0.1f, 0.8f, 0.2f, 3.0f); + //Create object + Object* terrainObject = new Object(terrainModel, terrainMaterial, + glm::vec3(-0.5f*(float)this->terrain.getHeightmapHeight(), 0.0f, -0.5f*(float)this->terrain.getHeightmapWidth()), + glm::vec3(0.0f, 0.0f, 0.0f)); + objects.push_back(terrainObject); + + //addTerrainPhysic + physics.addTerrain(terrain.getHeightmapWidth(), terrain.getHeightmapHeight(), terrain.getHeightmap()); + + + //Loading Objects via xml XMLDocument* doc = new XMLDocument(); const char* xmlFile = ("../Levels/ObjectSetups/Level" + levelNum + ".xml").c_str(); doc->LoadFile(xmlFile); @@ -80,10 +95,13 @@ void Level::load() { printf("XMLError: No modelPath found in object.\n"); } std::string modelPath = charModelPath; - float scaleObj, scaleComp; - object->FirstChildElement("scale")->QueryFloatText(&scaleObj); - thisComposition->FirstChildElement("scale")->QueryFloatText(&scaleComp); - //Model model = Model(modelPath, scaleObj * scaleComp); + float objectScale, compScale; + error = object->FirstChildElement("scale")->QueryFloatText(&objectScale); + errorCheck(error); + error = thisComposition->FirstChildElement("scale")->QueryFloatText(&compScale); + errorCheck(error); + //Model model = Model(modelPath, objectScale * compScale); + //Material material; XMLElement* objectData = compositions->FirstChildElement("objectData"); for(; objectData; objectData=objectData->NextSiblingElement("objectData")){ const char* charDataModelPath = objectData->FirstChildElement("modelPath")->GetText(); @@ -93,20 +111,49 @@ void Level::load() { std::string dataModelPath = charDataModelPath; if(dataModelPath == modelPath){ float ambientFactor, diffuseFactor, specularFactor, shininess; - objectData->FirstChildElement("ambientFactor")->QueryFloatText(&ambientFactor); - objectData->FirstChildElement("diffuseFactor")->QueryFloatText(&diffuseFactor); - objectData->FirstChildElement("specularFactor")->QueryFloatText(&specularFactor); - objectData->FirstChildElement("shininess")->QueryFloatText(&shininess); + error = objectData->FirstChildElement("ambientFactor")->QueryFloatText(&ambientFactor); + errorCheck(error); + error = objectData->FirstChildElement("diffuseFactor")->QueryFloatText(&diffuseFactor); + errorCheck(error); + error = objectData->FirstChildElement("specularFactor")->QueryFloatText(&specularFactor); + errorCheck(error); + error = objectData->FirstChildElement("shininess")->QueryFloatText(&shininess); + errorCheck(error); const char* charTexturePath = objectData->FirstChildElement("texturePath")->GetText(); if(charTexturePath == NULL){ printf("XMLError: No texturePath found in objectData.\n"); } std::string texturePath = charTexturePath; - //Material material = Material(texturePath, ambientFactor, diffuseFactor, specularFactor, shininess); + //material = Material(texturePath, ambientFactor, diffuseFactor, specularFactor, shininess); } } - //TODO calculate position and rotation - //Object* object = new Object(model, material, position, rotation); + float compXPos, compYOffset, compZPos; + glm::vec3 objectOffset, compRot, compPos; + error = object->FirstChildElement("xOffset")->QueryFloatText(&objectOffset[0]); + errorCheck(error); + error = object->FirstChildElement("yOffset")->QueryFloatText(&objectOffset[1]); + errorCheck(error); + error = object->FirstChildElement("zOffset")->QueryFloatText(&objectOffset[2]); + errorCheck(error); + error = thisComposition->FirstChildElement("xPos")->QueryFloatText(&compXPos); + errorCheck(error); + error = thisComposition->FirstChildElement("yOffset")->QueryFloatText(&compYOffset); + errorCheck(error); + error = thisComposition->FirstChildElement("zPos")->QueryFloatText(&compZPos); + errorCheck(error); + error = thisComposition->FirstChildElement("xRot")->QueryFloatText(&compRot[0]); + errorCheck(error); + error = thisComposition->FirstChildElement("yRot")->QueryFloatText(&compRot[1]); + errorCheck(error); + error = thisComposition->FirstChildElement("zRot")->QueryFloatText(&compRot[2]); + errorCheck(error); + compPos = glm::vec3(compXPos, + compYOffset+terrain.getHeightmap()[int(compXPos-0.5+0.5*terrain.getHeightmapHeight())] + [int(compZPos-0.5+0.5*terrain.getHeightmapWidth())], + compZPos); + objectOffset = objectOffset * compScale; + //TODO calculate position from objectOffset, compRot and compPos + //Object* object = new Object(model, material, position, compRot); //objects.push_back(object); //TODO if object has physics: physicObjects.push_back(object); //TODO add object to physics @@ -179,19 +226,7 @@ void Level::load() { Light light2 = Light(glm::vec3(3.0f, 7.0f, 0.0f), glm::vec3(1.0f, 1.0f, 1.0f), 10.0f); lights.push_back(light2); - // load terrain - this->terrain.load(); - Model terrainModel = Model(this->terrain.getModel()); - // load a texture: - Material terrainMaterial = Material("seamlessTerrain.png", 0.1f, 0.8f, 0.2f, 3.0f); - //Create object - Object* terrainObject = new Object(terrainModel, terrainMaterial, - glm::vec3(-0.5f*(float)this->terrain.getHeightmapHeight(), 0.0f, -0.5f*(float)this->terrain.getHeightmapWidth()), - glm::vec3(0.0f, 0.0f, 0.0f)); - objects.push_back(terrainObject); - //addTerrainPhysic - physics.addTerrain(terrain.getHeightmapWidth(), terrain.getHeightmapHeight(), terrain.getHeightmap()); } void Level::render(ACGL::OpenGL::SharedShaderProgram shader, bool lightingPass) {