diff --git a/game/level.cc b/game/level.cc index 515f14a..9499edc 100644 --- a/game/level.cc +++ b/game/level.cc @@ -229,6 +229,7 @@ void Level::setSkydomeObject(Skydome object){ void Level::addObject(Object* object) { allObjects.push_back(object); + //int xPosition = object->getPosition().x - terrain.getHeightmapHeight chunks.at(0).at(0).addObject(object); } @@ -335,12 +336,13 @@ void Level::printPosition() { } void Level::generateChunks(int chunkSize) { + this->chunkSize = chunkSize; int numberChunksX = 0; - if (terrain.getHeightmapHeight() % chunkSize == 0) { - numberChunksX = terrain.getHeightmapHeight()/chunkSize; + if (terrain.getHeightmapWidth() % chunkSize == 0) { + numberChunksX = terrain.getHeightmapWidth()/chunkSize; } else { - numberChunksX = (terrain.getHeightmapHeight()/chunkSize) + 1; + numberChunksX = (terrain.getHeightmapWidth()/chunkSize) + 1; } int numberChunksZ = 0; if (terrain.getHeightmapHeight() % chunkSize == 0) { @@ -358,3 +360,7 @@ void Level::generateChunks(int chunkSize) { chunks.at(i) = zChunks; } } + +std::vector>* Level::getChunks() { + return &chunks; +} diff --git a/game/level.hh b/game/level.hh index f966dbc..e2572f6 100644 --- a/game/level.hh +++ b/game/level.hh @@ -75,6 +75,7 @@ class Level { void setTerrain(Terrain terrain); void printPosition(); void generateChunks(int chunkSize); + std::vector>* getChunks(); private: lua_State* luaState=nullptr; std::vector crossChunkObjects; @@ -99,6 +100,7 @@ class Level { float strength; std::string xmlFilePath; glm::vec3 nextLightPosition; + float chunkSize; }; #endif diff --git a/game/loader.cc b/game/loader.cc index e773082..d99bf89 100644 --- a/game/loader.cc +++ b/game/loader.cc @@ -2,6 +2,8 @@ #include using namespace tinyxml2; +const int chunkSize = 50; + Loader::Loader() { } @@ -80,10 +82,10 @@ void Loader::load(std::string filePath, Level* level, std::string compositionsPa } Terrain terrain = Terrain(levelHeightmapPath); level->setTerrain(terrain); - // Because object gets copied a lot load it when it has reached it's final destination + // Because object gets copied a lot, load it when it has reached it's final destination level->getTerrain()->load(); - level->generateChunks(50.0f); - Model terrainModel = Model(level->getTerrain()->getModel()); + // generate appropriate amount of chunks depending on the heightmap size. + level->generateChunks(chunkSize); std::string terrainTexture = queryString(terrainElement, "texture"); float terrainAmbientFactor = queryFloat(terrainElement, "ambientFactor"); float terrainDiffuseFactor = queryFloat(terrainElement, "diffuseFactor"); @@ -95,10 +97,15 @@ void Loader::load(std::string filePath, Level* level, std::string compositionsPa exit(-1); } Material terrainMaterial = Material(terrainTexture, terrainAmbientFactor, terrainDiffuseFactor, terrainSpecularFactor, terrainShininess); - Object* terrainObject = new Object(terrainModel, terrainMaterial, - glm::vec3(-0.5*((float)level->getTerrain()->getHeightmapHeight()-1), 0.0f, -0.5f*((float)level->getTerrain()->getHeightmapWidth()-1)), - glm::vec3(0.0f, 0.0f, 0.0f), true); - level->addObject(terrainObject); + for (unsigned int i = 0; igetChunks()->size(); i++) { + for (unsigned int j = 0; jgetChunks()->at(i).size(); j++) { + Model terrainModel = Model(level->getTerrain()->makeTriangleMesh(i*chunkSize, j*chunkSize, (i+1)*chunkSize+1, (j+1)*chunkSize+1)); + Object* terrainObject = new Object(terrainModel, terrainMaterial, + glm::vec3(-0.5*((float)level->getTerrain()->getHeightmapHeight()-1), 0.0f, -0.5f*((float)level->getTerrain()->getHeightmapWidth()-1)), + glm::vec3(0.0f, 0.0f, 0.0f), true); + level->addObject(terrainObject); + } + } level->getPhysics()->addTerrain(level->getTerrain()->getHeightmapWidth(), level->getTerrain()->getHeightmapHeight(), level->getTerrain()->getHeightmap()); //load the skydome diff --git a/game/terrain.cc b/game/terrain.cc index 219a67d..bacb2c4 100644 --- a/game/terrain.cc +++ b/game/terrain.cc @@ -29,10 +29,9 @@ void Terrain::load() { this->heightmap[columnNum][rowNum] = (float)(image[(rowNum*heightmapWidth+columnNum)*4]) / 6; //<--heightmap is scaled here } } - this->makeTriangleMesh(0, 0, heightmapHeight, heightmapWidth); } -void Terrain::makeTriangleMesh(int startX, int startZ, int endX, int endZ) { +SharedVertexArrayObject Terrain::makeTriangleMesh(int startX, int startZ, int endX, int endZ) { if (startX < 0) { startX = 0; } @@ -46,7 +45,8 @@ void Terrain::makeTriangleMesh(int startX, int startZ, int endX, int endZ) { endZ = heightmapWidth; } - ACGL::OpenGL::SharedArrayBuffer ab = std::make_shared(); + SharedArrayBuffer ab = SharedArrayBuffer(new ArrayBuffer()); + SharedVertexArrayObject vao = SharedVertexArrayObject(new VertexArrayObject()); // Do NOT change the order of this! ab->defineAttribute("aPosition", GL_FLOAT, 3); ab->defineAttribute("aTexCoord", GL_FLOAT, 2); @@ -54,7 +54,7 @@ void Terrain::makeTriangleMesh(int startX, int startZ, int endX, int endZ) { int rowNum = startX, columnNum = startZ, dataCount=0, floatsPerVertex=8; //initializing: bool movingRight = true, isUp = true; - int numVertices = (this->heightmapHeight - 1) * (this->heightmapWidth * 2 + 1) + 1; + int numVertices = ((endX - startX) - 1) * ((endZ - startZ) * 2 + 1) + 1; float* abData = new float[numVertices * floatsPerVertex]; while(rowNum < endX){ //traversing the Triangle Strip! @@ -98,10 +98,10 @@ void Terrain::makeTriangleMesh(int startX, int startZ, int endX, int endZ) { ab->setDataElements(numVertices, abData); delete abData; - this->triangleMesh = std::make_shared(); - this->triangleMesh->bind(); - this->triangleMesh->setMode(GL_TRIANGLE_STRIP); - this->triangleMesh->attachAllAttributes(ab); + vao->bind(); + vao->setMode(GL_TRIANGLE_STRIP); + vao->attachAllAttributes(ab); + return vao; } @@ -150,10 +150,6 @@ void Terrain::set_abData(float* abData, int dataCount, int rowNum, int columnNum } } -Model Terrain::getModel(){ - return Model(this->triangleMesh); -} - float** Terrain::getHeightmap(){ return this->heightmap; } diff --git a/game/terrain.hh b/game/terrain.hh index 3acb8c1..f7ba781 100644 --- a/game/terrain.hh +++ b/game/terrain.hh @@ -5,6 +5,9 @@ #include "material.hh" #include #include "model.hh" + +using namespace ACGL::OpenGL; + class Terrain { public: Terrain(std::string heightmapFilePath); @@ -12,17 +15,15 @@ class Terrain { ~Terrain(); void load(); void render(); - Model getModel(); float** getHeightmap(); int getHeightmapHeight(); int getHeightmapWidth(); - void makeTriangleMesh(int startX, int startZ, int endX, int endZ); + SharedVertexArrayObject makeTriangleMesh(int startX, int startZ, int endX, int endZ); private: Material material; std::string heightmapFilePath; int heightmapHeight, heightmapWidth; float** heightmap; //can be accessed like 'float[][]' - ACGL::OpenGL::SharedVertexArrayObject triangleMesh; void set_abData(float* abData, int dataCount, int rowNum, int columnNum); };