From 28c5212a0395e122c38e657b5f8cad2aff96460c Mon Sep 17 00:00:00 2001 From: Faerbit Date: Tue, 10 Mar 2015 00:46:50 +0100 Subject: [PATCH] Only render objects which are visible. --- graphics.cc | 6 +++--- level.cc | 15 ++++++++++++--- level.hh | 4 +++- loader.cc | 2 +- 4 files changed, 19 insertions(+), 8 deletions(-) diff --git a/graphics.cc b/graphics.cc index 64173fa..6e65390 100644 --- a/graphics.cc +++ b/graphics.cc @@ -330,7 +330,7 @@ void Graphics::render(double time) glm::mat4 depthViewProjectionMatrix_face = depthProjectionMatrix_pointlights * viewMatrix; std::vector viewMatrixVector = std::vector(); viewMatrixVector.push_back(viewMatrix); - level->render(depthCubeShader, false, &depthViewProjectionMatrix_face, &viewMatrixVector); + level->render(depthCubeShader, false, &depthViewProjectionMatrix_face, farPlane, &viewMatrixVector); if (!framebuffer_cube->isFrameBufferObjectComplete()) { printf("Framebuffer incomplete, unknown error occured during shadow generation!\n"); } @@ -363,7 +363,7 @@ void Graphics::render(double time) } depthViewProjectionMatrices.at(i) = glm::ortho(-projection_size, projection_size, -projection_size, projection_size, -farPlane/1.5f, farPlane/1.5f) * glm::lookAt(sunVector, level->getCameraCenter()->getPosition(), glm::vec3(0,1,0)); - level->render(depthShader, false, &depthViewProjectionMatrices.at(i)); + level->render(depthShader, false, &depthViewProjectionMatrices.at(i), farPlane); } if (!framebuffer_directional.at(i)->isFrameBufferObjectComplete()) { printf("Framebuffer incomplete, unknown error occured during shadow generation!\n"); @@ -459,7 +459,7 @@ void Graphics::render(double time) lightingShader->setUniform("time", (float) time); // render the level - level->render(lightingShader, true, &lightingViewProjectionMatrix, &depthBiasVPs); + level->render(lightingShader, true, &lightingViewProjectionMatrix, farPlane, &depthBiasVPs); // draw flames on top flameShader->use(); diff --git a/level.cc b/level.cc index 0659e18..2e1a9aa 100644 --- a/level.cc +++ b/level.cc @@ -56,13 +56,17 @@ void Level::load() { } void Level::render(ACGL::OpenGL::SharedShaderProgram shader, bool lightingPass, - glm::mat4* viewProjectionMatrix, std::vector* shadowVPs) { + glm::mat4* viewProjectionMatrix, float farPlane, std::vector* shadowVPs) { for(unsigned int i = 0; irender(shader, lightingPass, true, viewProjectionMatrix, shadowVPs); + if (glm::distance(objects.at(i)->getPosition(), cameraCenter->getPosition()) < farPlane + 5.0f || objects.at(i) == terrainObject) { + objects.at(i)->render(shader, lightingPass, true, viewProjectionMatrix, shadowVPs); + } } else { - objects.at(i)->render(shader, lightingPass, false, viewProjectionMatrix, shadowVPs); + if (glm::distance(objects.at(i)->getPosition(), cameraCenter->getPosition()) < farPlane + 5.0f || objects.at(i) == terrainObject) { + objects.at(i)->render(shader, lightingPass, false, viewProjectionMatrix, shadowVPs); + } } } if (lightingPass) { @@ -341,3 +345,8 @@ void Level::activateEndgame(){ void Level::setWaterPlane(Object* water) { this->waterPlane = water; } + +void Level::addTerrain(Object* terrain) { + this->terrainObject = terrain; + objects.push_back(terrain); +} diff --git a/level.hh b/level.hh index 60cf960..46967af 100644 --- a/level.hh +++ b/level.hh @@ -28,7 +28,7 @@ class Level { void update(float runTimeSinceLastUpdate, float runTime, glm::vec2 mouseDelta,bool wPressed, bool aPressed,bool sPressed, bool dPressed, bool kPressed, bool lPressed, bool f1Pressed, bool f2Pressed, bool f3Pressed, bool f4Pressed ); void render(ACGL::OpenGL::SharedShaderProgram shader, bool lightingPass, - glm::mat4* viewProjectionMatrix, std::vector* shadowVPs=0); + glm::mat4* viewProjectionMatrix, float farPlane, std::vector* shadowVPs=0); glm::vec3 getAmbientLight(); Light* getDirectionalLight(); std::vector* getLights(); @@ -67,6 +67,7 @@ class Level { void addTrigger(Trigger trigger); lua_State* getLuaState(); Terrain* getTerrain(); + void addTerrain(Object* terrain); void resetPlayer(); void movePlayer(float xPosition, float yPosition, float zPosition); void setPlayerIndex(int index); @@ -94,6 +95,7 @@ class Level { float strength; std::string xmlFilePath; glm::vec3 nextLightPosition; + Object* terrainObject; }; #endif diff --git a/loader.cc b/loader.cc index 4a8ee97..aa43fef 100644 --- a/loader.cc +++ b/loader.cc @@ -86,7 +86,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->addTerrain(terrainObject); level->getPhysics()->addTerrain(level->getTerrain()->getHeightmapWidth(), level->getTerrain()->getHeightmapHeight(), level->getTerrain()->getHeightmap()); //load the skydome