Added loading from config.xml, then debuged it for more than three hours...

This commit is contained in:
Steffen Fündgens 2015-02-06 18:00:14 +01:00
parent f29678867f
commit 2bfad25510
12 changed files with 71 additions and 76 deletions

View File

@ -5,7 +5,7 @@ using namespace tinyxml2;
Application::Application() {
//load the config.xml
loadConfig();
graphics = Graphics(glm::uvec2(1024, 786), 0.1f, 150.0f);
graphics = Graphics(glm::uvec2(windowWidth, windowHeight), 0.1f, farPlane);
}
void Application::init()
@ -13,24 +13,26 @@ void Application::init()
// init random generator
std::srand(std::time(NULL));
// choose Level TODO: Choose this in a menu
this->level = Level("1");
std::string heightmapFilePath = heightmapPath + "heightmapLvl1.png";
this->level = Level(heightmapFilePath);
level.getPhysics()->init(geometryPath);
// Don't change this!
ignoredMouseUpdates = 0;
cameraLock = true;
// set Skybox size
level.setSkydomeSize((graphics.getFarPlane())-31.0f);
// define where shaders and textures can be found:
ACGL::Base::Settings::the()->setResourcePath("../");
ACGL::Base::Settings::the()->setShaderPath("Shader/");
ACGL::Base::Settings::the()->setTexturePath("Levels/Textures/");
ACGL::Base::Settings::the()->setGeometryPath("Levels/Geometry/");
ACGL::Base::Settings::the()->setShaderPath(shaderPath);
ACGL::Base::Settings::the()->setTexturePath(texturePath);
ACGL::Base::Settings::the()->setGeometryPath(geometryPath);
// load Level
level.load();
Loader loader = Loader();
loader.load("Level1.xml", &level);
std::string levelXmlFilePath = levelXmlPath + "Level1.xml";
loader.load(levelXmlFilePath, &level, compositionsPath, scriptPath);
graphics.init(&level);
// just in case: check for errors

View File

@ -5,7 +5,7 @@
<farPlane>150.0</farPlane>
<compositionsPath>Levels/ObjectSetups/</compositionsPath>
<compositionsPath>../Levels/ObjectSetups/Compositions.xml</compositionsPath>
<shaderPath>Shader/</shaderPath>
@ -13,8 +13,8 @@
<texturePath>Levels/Textures/</texturePath>
<scriptPath>Levels/scripts/</scriptPath>
<scriptPath>../Levels/scripts/</scriptPath>
<heightmapPath>Levels/</heightmapPath>
<heightmapPath>../Levels/</heightmapPath>
<levelXmlPath>Levels/ObjectSetups/</levelXmlPath>
<levelXmlPath>../Levels/ObjectSetups/</levelXmlPath>

View File

@ -1,11 +1,11 @@
#include "level.hh"
#include <string>
Level::Level(std::string levelNum){
this->levelNum = levelNum;
this->terrain = Terrain(levelNum);
Level::Level(std::string heightmapFilePath){
this->terrain = Terrain(heightmapFilePath);
// default value
skydomeSize = 50.0f;
physics = Physics();
}
Level::Level() {
@ -40,11 +40,7 @@ void Level::load() {
//Push the level to Lua as a global variable
luabridge::push(luaState, this);
lua_setglobal(luaState, "level");
this->physics = Physics();
this->physics.init();
this->camera = Camera(glm::vec2(-0.8f, 0.0f), 3.0f);
}

View File

@ -20,7 +20,7 @@ extern "C" {
class Level {
public:
Level(std::string levelNum);
Level(std::string heightmapFilePath);
Level();
~Level();
void load();
@ -57,7 +57,6 @@ class Level {
Terrain* getTerrain();
private:
lua_State* luaState=nullptr;
std::string levelNum;
std::vector<Object*> objects;
std::vector<Object*> physicsObjects;
std::vector<Light> lights;

View File

@ -4,16 +4,15 @@ using namespace tinyxml2;
Loader::Loader() {
}
void Loader::load(std::string filePath, Level* level) {
void Loader::load(std::string filePath, Level* level, std::string compositionsPath, std::string scriptPath) {
//Loading from xml:
XMLDocument* doc = new XMLDocument();
const char* xmlFile = ("../Levels/ObjectSetups/" + filePath).c_str();
const char* xmlFile = filePath.c_str();
doc->LoadFile(xmlFile);
if (doc->ErrorID()!=0){
printf("Could not open ObjectSetupXml!\n");
exit(-1);
}
//load global physic parameter
float friction, strength;
XMLElement* physicsElement = doc->FirstChildElement("physics");
@ -87,7 +86,7 @@ void Loader::load(std::string filePath, Level* level) {
std::vector<std::vector<int>> objectIdentifiers = std::vector<std::vector<int>>(); //The first entry is the index in objects, the second one the index in physicObjects, the others are idGreen, idBlue and objectNum.
XMLDocument* compositions = new XMLDocument();
//TODO move path to config.xml
const char* compositionsFile = "../Levels/ObjectSetups/Compositions.xml";
const char* compositionsFile = compositionsPath.c_str();
compositions->LoadFile(compositionsFile);
if (compositions->ErrorID()!=0){
printf("Could not open Compositions!\n");
@ -188,7 +187,7 @@ void Loader::load(std::string filePath, Level* level) {
objectIdentifier[3] = idBlue;
objectIdentifier[4] = objectNum;
objectIdentifiers.push_back(objectIdentifier);
//add object to physics
const char* charPhysicType = objectData->FirstChildElement("physicType")->GetText();
if(charPhysicType == NULL){
@ -232,7 +231,6 @@ void Loader::load(std::string filePath, Level* level) {
printf("XMLError: Not a valid physicType.\n");
exit(-1);
}
if(compositionType == 20){
level->setCameraCenter(object);
@ -363,7 +361,7 @@ void Loader::load(std::string filePath, Level* level) {
}
}
if (object != 0) {
Trigger trigger = Trigger(position, distance, isBigger, object, luaScript, level->getLuaState(), objectToChange);
Trigger trigger = Trigger(position, distance, isBigger, object, luaScript, level->getLuaState(), objectToChange, scriptPath);
level->addTrigger(trigger);
}
else {

View File

@ -6,7 +6,7 @@
class Loader {
public:
Loader();
void load(std::string filePath, Level* level);
void load(std::string filePath, Level* level, std::string compositionsPath, std::string scriptPath);
private:
void errorCheck(tinyxml2::XMLError error);
};

View File

@ -8,7 +8,7 @@ Physics::Physics() {
Physics::~Physics() {
}
void Physics::init() //prepares bullet by creating all initial classes
void Physics::init(std::string geometryPath) //prepares bullet by creating all initial classes
{
colConfig = new btDefaultCollisionConfiguration();
dispatcher = new btCollisionDispatcher(colConfig);
@ -16,6 +16,10 @@ void Physics::init() //prepares bullet by creating all initial classes
solver = new btSequentialImpulseConstraintSolver();
world = new btDiscreteDynamicsWorld(dispatcher,broadphase,solver,colConfig);
world->setGravity(btVector3(0,-10,-0));
if (world == NULL) {
printf("No World after init\n");
}
this->geometryPath = geometryPath;
}
void Physics::takeUpdateStep(float timeDiff)
@ -110,34 +114,33 @@ void Physics::addPlayer(float friction, float rad, Entity entity, float mass, fl
addCamera(); //now that the player exists add a camera for the player
}
void Physics::addTerrain(int width, int length, float** heightData) //The terrian adding function
void Physics::addTerrain(int width, int length, float** heightData) //The terrain adding function
{
float* heightfield = new float[width * length];//bullet only accepts data in a one dimensional array, so parse data into appropriate format
int highest = -999999, j = 0, i = 0;
for (i = 0; i < width; i++)
{
for (j = 0; j < length; j++) {
heightfield[i*length+j] = heightData[j][i]; //reverse order because they are loaded backwards
int highest = -999999, j = 0, i = 0;
for (i = 0; i < width; i++)
{
for (j = 0; j < length; j++) {
heightfield[i*length+j] = heightData[j][i]; //reverse order because they are loaded backwards
if (heightData[j][i] > highest)
if (heightData[j][i] > highest)
highest = heightData[j][i]; //bullet needs to know the highest point of the heightmap
}
}
highest++;
}
}
highest++;
btHeightfieldTerrainShape* terrianShape = new btHeightfieldTerrainShape(length,width,heightfield,highest,1,true,false);
btHeightfieldTerrainShape* terrainShape = new btHeightfieldTerrainShape(length,width,heightfield,highest,1,true,false);
btRigidBody::btRigidBodyConstructionInfo info(0,new btDefaultMotionState(),terrianShape,btVector3(0,0,0)); //next we process all data for the rigid body into info
btRigidBody::btRigidBodyConstructionInfo info(0,new btDefaultMotionState(),terrainShape,btVector3(0,0,0)); //next we process all data for the rigid body into info
info.m_restitution = 0;
btRigidBody* tBody = new btRigidBody(info);
tBody->getWorldTransform().setOrigin(btVector3(0,((float)highest)/2,0)); //we have to move the origin of our rigid body down, because bullet sets the origin (0,0,0) at (width/2, height/2, length/2) in the map the x and z are correct in our level, but y needs to be addapted
terrainBody = tBody;
if (world == NULL) {
printf("No World while adding terrain.\n");
}
world->addRigidBody(terrainBody, COL_TERRAIN, COL_TERRAIN | COL_OBJECTS); //COL_XXXX are collision masks, allowing us to ignore collisions between certain object groups (required for buttons)
}
@ -149,13 +152,11 @@ void Physics::addTriangleMeshBody(Entity entity, std::string path, float mass, f
std::vector< unsigned int > vertexIndices; //temp lists for data sets
std::vector< btVector3 > temp_vertices;
path = "../Levels/Geometry/" + path;
path = "../" + geometryPath + path;
FILE * file = fopen(path.c_str(), "r");
if( file == NULL ){
throw std::invalid_argument( "Impossible to open the file" ); //create correct filepath and report error if cannot open
}
while( 1 ){
char lineHeader[128];
// read the first word of the line
@ -437,6 +438,9 @@ void Physics::addStaticGroundPlane()
void Physics::kill()//delete dynamically allocated memory
{
if (world == NULL) {
return;
}
//btDynamimcWorld*
for(unsigned i = 0; i < bodies.size();i++)
{
@ -467,5 +471,3 @@ void Physics::kill()//delete dynamically allocated memory
//feel like a good little programmer because everything is clean
}

View File

@ -45,7 +45,7 @@ class Physics {
public:
Physics();
~Physics();
void init();
void init(std::string geometryPath);
void takeUpdateStep(float timeDiff); //must be used in level.update to proagate the physics
void rollForward(glm::vec3 camPos, float strength); //self explainitory
void rollLeft(glm::vec3 camPos, float strength);
@ -80,7 +80,7 @@ class Physics {
std::vector<positionConstraint> allPositionConstraints;
void addCamera(); //Do NOT impliment before Player has been created;
btDynamicsWorld* world; //contains physical attributes of the world.
btDynamicsWorld* world = NULL; //contains physical attributes of the world.
btDispatcher* dispatcher; //
btCollisionConfiguration* colConfig; //defines the type of collision detection.
btBroadphaseInterface* broadphase; //defines how objects are culled from collision detection.
@ -89,7 +89,7 @@ class Physics {
int specialPhysicsCollision = 2 | 4;
int terrainPhysicsCollision = 2;
int counter = 0;
std::string geometryPath;
};
enum collisionTypes{

View File

@ -1,8 +1,8 @@
#include "terrain.hh"
#include "lodepng.h"
Terrain::Terrain(std::string levelNum){
this->levelNum = levelNum;
Terrain::Terrain(std::string heightmapFilePath){
this->heightmapFilePath = heightmapFilePath;
}
Terrain::Terrain(){
@ -14,9 +14,9 @@ Terrain::~Terrain() {
void Terrain::load() {
std::vector<unsigned char> image; //the raw pixels
unsigned error = lodepng::decode(image, heightmapWidth, heightmapHeight, "../Levels/heightmapLvl" + levelNum + ".png");
unsigned error = lodepng::decode(image, heightmapWidth, heightmapHeight, heightmapFilePath);
if (error) {
std::cout << "decoder error " << error << ": " << lodepng_error_text(error) << std::endl;
std::cout << "Decoder error " << error << " from Terrain::load: " << lodepng_error_text(error) << std::endl;
}
this->heightmap = new float*[this->heightmapHeight]; //initialize the heightmap
for(unsigned int rowNum = 0; rowNum < this->heightmapHeight; rowNum++){ //read in the heightmap
@ -25,10 +25,9 @@ void Terrain::load() {
this->heightmap[rowNum][columnNum] = (float)(image[(rowNum*heightmapWidth+columnNum)*4]) / 6; //<--heightmap is scaled here
}
}
this->makeTriangleMesh();
heightmapChanged = false; //no need to make a TriangleMesh again before rendering
}
void Terrain::makeTriangleMesh(){

View File

@ -12,21 +12,20 @@ class Terrain {
~Terrain();
void load();
void render();
Model getModel();
float** getHeightmap();
unsigned int getHeightmapHeight();
unsigned int getHeightmapWidth();
Model getModel();
float** getHeightmap();
unsigned int getHeightmapHeight();
unsigned int getHeightmapWidth();
private:
Material material;
std::string levelNum;
unsigned int heightmapHeight, heightmapWidth;
float** heightmap; //can be accessed like 'float[][]'
bool heightmapChanged;
ACGL::OpenGL::SharedVertexArrayObject triangleMesh;
void makeTriangleMesh();
void set_abData(float* abData, unsigned int dataCount, unsigned int rowNum, unsigned int columnNum);
std::string heightmapFilePath;
unsigned int heightmapHeight, heightmapWidth;
float** heightmap; //can be accessed like 'float[][]'
bool heightmapChanged;
ACGL::OpenGL::SharedVertexArrayObject triangleMesh;
void makeTriangleMesh();
void set_abData(float* abData, unsigned int dataCount, unsigned int rowNum, unsigned int columnNum);
};
#endif

View File

@ -1,11 +1,11 @@
#include "trigger.hh"
Trigger::Trigger(glm::vec3 position, float distance, bool isBigger, Object* object, std::string luaScript, lua_State* L, int objectToChange) {
Trigger::Trigger(glm::vec3 position, float distance, bool isBigger, Object* object, std::string luaScript, lua_State* L, int objectToChange, std::string scriptPath) {
this->position=position;
this->distance=distance;
this->isBigger=isBigger;
this->object=object;
this->luaScript="../Levels/scripts/" + luaScript + ".lua";
this->luaScript= scriptPath + luaScript + ".lua";
this->L = L;
if(L == nullptr){
printf("L is NULL in trigger!\n");

View File

@ -14,7 +14,7 @@ class Level;
class Trigger {
public:
Trigger(glm::vec3 position, float distance, bool isBigger, Object* object, std::string luaScript, lua_State* L, int objectToChange);
Trigger(glm::vec3 position, float distance, bool isBigger, Object* object, std::string luaScript, lua_State* L, int objectToChange, std::string scriptPath);
Trigger();
~Trigger();
void triggerUpdate();