Made heightmap path configurable from level.

This commit is contained in:
Faerbit 2015-03-14 13:00:47 +01:00
parent 7e0394df00
commit 370135fca8
6 changed files with 25 additions and 11 deletions

View File

@ -13208,6 +13208,7 @@
</directionalLight> </directionalLight>
<terrain> <terrain>
<heightmap>heightmapLvl1.png</heightmap>
<texture>sand.png</texture> <texture>sand.png</texture>
<ambientFactor>0.13</ambientFactor> <ambientFactor>0.13</ambientFactor>
<diffuseFactor>0.8</diffuseFactor> <diffuseFactor>0.8</diffuseFactor>

View File

@ -19,13 +19,11 @@ void Application::init()
graphics.renderLoadingScreen(); graphics.renderLoadingScreen();
// init random generator // init random generator
std::srand(std::time(NULL)); std::srand(std::time(NULL));
// choose Level TODO: Choose this in a menu
} }
void Application::initLevel() { void Application::initLevel() {
std::string heightmapFilePath = heightmapPath + "heightmapLvl1.png";
std::string levelXmlFilePath = levelXmlPath + "Level1.xml"; std::string levelXmlFilePath = levelXmlPath + "Level1.xml";
this->level = Level(heightmapFilePath, levelXmlFilePath); this->level = Level(levelXmlFilePath);
level.getPhysics()->init(geometryPath); level.getPhysics()->init(geometryPath);
// Don't change this! // Don't change this!
ignoredMouseUpdates = 0; ignoredMouseUpdates = 0;
@ -38,7 +36,7 @@ void Application::initLevel() {
level.load(); level.load();
Loader loader = Loader(); Loader loader = Loader();
loader.load(levelXmlFilePath, &level, compositionsPath, scriptPath, geometryPath, texturePath); loader.load(levelXmlFilePath, &level, compositionsPath, scriptPath, geometryPath, texturePath, heightmapPath);
graphics.init(&level); graphics.init(&level);
// just in case: check for errors // just in case: check for errors

View File

@ -2,8 +2,7 @@
#include "loader.hh" #include "loader.hh"
#include <string> #include <string>
Level::Level(std::string heightmapFilePath, std::string xmlFilePath){ Level::Level(std::string xmlFilePath){
this->terrain = Terrain(heightmapFilePath);
// default value // default value
skydomeSize = 50.0f; skydomeSize = 50.0f;
physics = Physics(); physics = Physics();
@ -341,3 +340,7 @@ void Level::activateEndgame(){
void Level::setWaterPlane(Object* water) { void Level::setWaterPlane(Object* water) {
this->waterPlane = water; this->waterPlane = water;
} }
void Level::setTerrain(Terrain terrain) {
this->terrain = terrain;
}

View File

@ -22,7 +22,7 @@ extern "C" {
class Level { class Level {
public: public:
Level(std::string heightmapFilePath, std::string xmlFilePath); Level(std::string xmlFilePath);
Level(); Level();
~Level(); ~Level();
void load(); void load();
@ -73,6 +73,7 @@ class Level {
void setPlayerIndex(int index); void setPlayerIndex(int index);
void forceMove(float x, float y, float z, unsigned indice); void forceMove(float x, float y, float z, unsigned indice);
void activateEndgame(); void activateEndgame();
void setTerrain(Terrain terrain);
private: private:
lua_State* luaState=nullptr; lua_State* luaState=nullptr;
std::vector<Object*> objects; std::vector<Object*> objects;

View File

@ -52,7 +52,9 @@ void Loader::loadConfig(Application* application) {
} }
} }
void Loader::load(std::string filePath, Level* level, std::string compositionsPath, std::string scriptPath, std::string globalGeometryPath, std::string globalTexturePath) { void Loader::load(std::string filePath, Level* level, std::string compositionsPath, std::string scriptPath,
std::string globalGeometryPath, std::string globalTexturePath, std::string heightmapPath) {
struct stat buf;
//Loading from xml: //Loading from xml:
XMLDocument* doc = new XMLDocument(); XMLDocument* doc = new XMLDocument();
const char* xmlFile = filePath.c_str(); const char* xmlFile = filePath.c_str();
@ -68,15 +70,22 @@ void Loader::load(std::string filePath, Level* level, std::string compositionsPa
level->setStrength(strength); level->setStrength(strength);
// load the terrain // load the terrain
XMLElement* terrainElement = doc->FirstChildElement("terrain");
std::string levelHeightmapPath = heightmapPath + queryString(terrainElement, "heightmap");
if(stat(levelHeightmapPath.c_str(), &buf) != 0){
std::cout << "The heightmap file " << levelHeightmapPath << " does not exist." << std::endl;
exit(-1);
}
Terrain terrain = Terrain(levelHeightmapPath);
level->setTerrain(terrain);
// Because object gets copied a lot load it when it has reached it's final destination
level->getTerrain()->load(); level->getTerrain()->load();
Model terrainModel = Model(level->getTerrain()->getModel()); Model terrainModel = Model(level->getTerrain()->getModel());
XMLElement* terrainElement = doc->FirstChildElement("terrain");
std::string terrainTexture = queryString(terrainElement, "texture"); std::string terrainTexture = queryString(terrainElement, "texture");
float terrainAmbientFactor = queryFloat(terrainElement, "ambientFactor"); float terrainAmbientFactor = queryFloat(terrainElement, "ambientFactor");
float terrainDiffuseFactor = queryFloat(terrainElement, "diffuseFactor"); float terrainDiffuseFactor = queryFloat(terrainElement, "diffuseFactor");
float terrainSpecularFactor = queryFloat(terrainElement, "specularFactor"); float terrainSpecularFactor = queryFloat(terrainElement, "specularFactor");
float terrainShininess = queryFloat(terrainElement, "shininess"); float terrainShininess = queryFloat(terrainElement, "shininess");
struct stat buf;
std::string terrainTexturePath = "../" + globalTexturePath + terrainTexture; std::string terrainTexturePath = "../" + globalTexturePath + terrainTexture;
if(stat(terrainTexturePath.c_str(), &buf) != 0){ if(stat(terrainTexturePath.c_str(), &buf) != 0){
std::cout << "The texture file " << terrainTexturePath << " does not exist." << std::endl; std::cout << "The texture file " << terrainTexturePath << " does not exist." << std::endl;

View File

@ -10,7 +10,9 @@ class Loader {
public: public:
Loader(); Loader();
void loadConfig(Application* application); void loadConfig(Application* application);
void load(std::string filePath, Level* level, std::string compositionsPath, std::string scriptPath, std::string geometryPath, std::string texturePath); void load(std::string filePath, Level* level, std::string compositionsPath,
std::string scriptPath, std::string geometryPath, std::string texturePath,
std::string heightmapPath);
glm::vec3 reloadPlayerPosition(std::string filePath, Level* level); glm::vec3 reloadPlayerPosition(std::string filePath, Level* level);
private: private:
float queryFloat(XMLElement* element, const char* attribute); float queryFloat(XMLElement* element, const char* attribute);