diff --git a/game/application.cc b/game/application.cc index f1c0884..b100894 100644 --- a/game/application.cc +++ b/game/application.cc @@ -22,7 +22,7 @@ void Application::init() } void Application::initLevel() { - this->level = Level(levelXmlPath); + this->level = Level(levelXmlPath, farPlane); level.getPhysics()->init(geometryPath); // Don't change this! ignoredMouseUpdates = 0; diff --git a/game/level.cc b/game/level.cc index 9499edc..bcb1125 100644 --- a/game/level.cc +++ b/game/level.cc @@ -2,11 +2,12 @@ #include "loader.hh" #include -Level::Level(std::string xmlFilePath){ +Level::Level(std::string xmlFilePath, float farPlane){ // default value skydomeSize = 50.0f; physics = Physics(); this->xmlFilePath = xmlFilePath; + this->farPlane = farPlane; } Level::Level() { @@ -52,8 +53,33 @@ void Level::load() { void Level::render(ACGL::OpenGL::SharedShaderProgram shader, bool lightingPass, glm::mat4* viewProjectionMatrix, std::vector* shadowVPs) { - for(unsigned int i = 0; igetPosition().x + (terrain.getHeightmapWidth()/2))/chunkSize; + int zPosition = ((int)cameraCenter->getPosition().z + (terrain.getHeightmapHeight()/2))/chunkSize; + int xStart = xPosition - renderDistance; + unsigned int xEnd = xPosition + renderDistance; + int zStart = zPosition - renderDistance; + unsigned int zEnd = zPosition + renderDistance; + if (xStart < 0) { + xStart = 0; + } + if (zStart < 0) { + zStart = 0; + } + if (xEnd >= chunks.size()) { + xEnd = chunks.size()-1; + } + if (zEnd >= chunks.at(0).size()) { + zEnd = chunks.at(0).size()-1; + } + for(unsigned int i = xStart; i<=xEnd; i++) { + for(unsigned int j = zStart; j<=zEnd; j++) { if (lightingPass) { chunks.at(i).at(j).render(shader, lightingPass, true, viewProjectionMatrix, shadowVPs); } @@ -229,8 +255,13 @@ 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); + int xPosition = ((int)object->getPosition().x + (terrain.getHeightmapWidth()/2))/chunkSize; + int zPosition = ((int)object->getPosition().z + (terrain.getHeightmapHeight()/2))/chunkSize; + chunks.at(xPosition).at(zPosition).addObject(object); +} + +void Level::addToSpecificChunk(Object* object, int xPosition, int zPosition) { + chunks.at(xPosition).at(zPosition).addObject(object); } void Level::addPhysicsObject(Object* object) { diff --git a/game/level.hh b/game/level.hh index e2572f6..8d99bd9 100644 --- a/game/level.hh +++ b/game/level.hh @@ -23,7 +23,7 @@ extern "C" { class Level { public: - Level(std::string xmlFilePath); + Level(std::string xmlFilePath, float farPlane); Level(); ~Level(); void load(); @@ -76,6 +76,7 @@ class Level { void printPosition(); void generateChunks(int chunkSize); std::vector>* getChunks(); + void addToSpecificChunk(Object* object, int xPosition, int zPosition); private: lua_State* luaState=nullptr; std::vector crossChunkObjects; @@ -100,7 +101,8 @@ class Level { float strength; std::string xmlFilePath; glm::vec3 nextLightPosition; - float chunkSize; + int chunkSize; + float farPlane; }; #endif diff --git a/game/loader.cc b/game/loader.cc index d99bf89..7bad3bb 100644 --- a/game/loader.cc +++ b/game/loader.cc @@ -103,7 +103,7 @@ void Loader::load(std::string filePath, Level* level, std::string compositionsPa 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->addToSpecificChunk(terrainObject, i, j); } } level->getPhysics()->addTerrain(level->getTerrain()->getHeightmapWidth(), level->getTerrain()->getHeightmapHeight(), level->getTerrain()->getHeightmap()); diff --git a/game/terrain.cc b/game/terrain.cc index bacb2c4..79e5831 100644 --- a/game/terrain.cc +++ b/game/terrain.cc @@ -38,11 +38,11 @@ SharedVertexArrayObject Terrain::makeTriangleMesh(int startX, int startZ, int en if (startZ < 0) { startZ = 0; } - if (endX > heightmapHeight) { - endX = heightmapHeight; + if (endX > heightmapWidth) { + endX = heightmapWidth; } - if (endZ > heightmapWidth) { - endZ = heightmapWidth; + if (endZ > heightmapHeight) { + endZ = heightmapHeight; } SharedArrayBuffer ab = SharedArrayBuffer(new ArrayBuffer());