Added error checking for all XML Queries, fixed bugs and continued to implement loading from xml.

This commit is contained in:
Steffen Fündgens 2014-12-08 14:10:08 +01:00
parent dbaa4f639f
commit db1092be44
4 changed files with 120 additions and 41 deletions

View File

@ -3,6 +3,7 @@
<xRot>0.0</xRot>
<yRot>0.0</yRot>
<zRot>0.0</zRot>
<manualPos>false</manualPos>
<xPos>3.500000</xPos>
<yOffset>0.0</yOffset>
<zPos>3.500000</zPos>
@ -16,6 +17,7 @@
<xRot>0.0</xRot>
<yRot>0.0</yRot>
<zRot>0.0</zRot>
<manualPos>false</manualPos>
<xPos>3.500000</xPos>
<yOffset>0.0</yOffset>
<zPos>-0.500000</zPos>
@ -29,6 +31,7 @@
<xRot>0.0</xRot>
<yRot>0.0</yRot>
<zRot>0.0</zRot>
<manualPos>false</manualPos>
<xPos>2.500000</xPos>
<yOffset>0.0</yOffset>
<zPos>-2.500000</zPos>
@ -42,6 +45,7 @@
<xRot>0.0</xRot>
<yRot>0.0</yRot>
<zRot>0.0</zRot>
<manualPos>false</manualPos>
<xPos>1.500000</xPos>
<yOffset>0.0</yOffset>
<zPos>0.500000</zPos>
@ -55,6 +59,7 @@
<xRot>0.0</xRot>
<yRot>0.0</yRot>
<zRot>0.0</zRot>
<manualPos>false</manualPos>
<xPos>-0.500000</xPos>
<yOffset>0.0</yOffset>
<zPos>1.500000</zPos>
@ -68,6 +73,7 @@
<xRot>0.0</xRot>
<yRot>0.0</yRot>
<zRot>0.0</zRot>
<manualPos>false</manualPos>
<xPos>-0.500000</xPos>
<yOffset>0.0</yOffset>
<zPos>-1.500000</zPos>
@ -81,6 +87,7 @@
<xRot>0.0</xRot>
<yRot>0.0</yRot>
<zRot>0.0</zRot>
<manualPos>false</manualPos>
<xPos>-2.500000</xPos>
<yOffset>0.0</yOffset>
<zPos>-1.500000</zPos>
@ -94,6 +101,7 @@
<xRot>0.0</xRot>
<yRot>0.0</yRot>
<zRot>0.0</zRot>
<manualPos>false</manualPos>
<xPos>-3.500000</xPos>
<yOffset>0.0</yOffset>
<zPos>-3.500000</zPos>
@ -107,6 +115,7 @@
<xRot>0.0</xRot>
<yRot>0.0</yRot>
<zRot>0.0</zRot>
<manualPos>false</manualPos>
<xPos>-4.500000</xPos>
<yOffset>0.0</yOffset>
<zPos>-4.500000</zPos>

View File

@ -2,6 +2,7 @@
#include <fstream>
#include <string>
#include <sys/stat.h>
#include <iostream>
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<unsigned int> 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<unsigned int> 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<unsigned int> 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<unsigned int> 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<unsigned int> 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");
}
}
*/
}

View File

@ -19,6 +19,7 @@ class Converter {
private:
std::vector<unsigned int> nextID;
void errorCheck(XMLError error);
std::string xmlFile;
XMLDocument* doc = new XMLDocument();
XMLDocument* compositions = new XMLDocument();

View File

@ -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) {