From f389aa2f4aaeaab18344c667bd624c4a24ab8c98 Mon Sep 17 00:00:00 2001 From: Faerbit Date: Mon, 1 Jun 2015 00:57:36 +0200 Subject: [PATCH] Fixed pointlight shadows not getting rendered correctly. --- game/graphics.cc | 4 ++-- game/level.cc | 12 ++++++------ game/level.hh | 8 +++++--- 3 files changed, 13 insertions(+), 11 deletions(-) diff --git a/game/graphics.cc b/game/graphics.cc index 9ad52e2..d84f796 100644 --- a/game/graphics.cc +++ b/game/graphics.cc @@ -354,7 +354,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, 1, &depthViewProjectionMatrix_face, &viewMatrixVector); + level->render(depthCubeShader, false, closestLights->at(i_pointlight)->getPosition(), 1, &depthViewProjectionMatrix_face, &viewMatrixVector); if (!framebuffer_cube->isFrameBufferObjectComplete()) { printf("Framebuffer incomplete, unknown error occured during shadow generation!\n"); } @@ -392,7 +392,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, -1, &depthViewProjectionMatrices.at(i)); + level->render(depthShader, false, level->getCameraCenter()->getPosition(), -1, &depthViewProjectionMatrices.at(i)); if (!framebuffer_directional.at(i)->isFrameBufferObjectComplete()) { printf("Framebuffer incomplete, unknown error occured during shadow generation!\n"); } diff --git a/game/level.cc b/game/level.cc index 4407e94..6d51bc1 100644 --- a/game/level.cc +++ b/game/level.cc @@ -52,9 +52,9 @@ void Level::load() { } void Level::render(ACGL::OpenGL::SharedShaderProgram shader, bool lightingPass, - int chunkRenderDistance, glm::mat4* viewProjectionMatrix, + glm::vec3 center, int chunkRenderDistance, glm::mat4* viewProjectionMatrix, std::vector* shadowVPs) { - std::vector nearChunks = getSurroundingChunks(chunkRenderDistance); + std::vector nearChunks = getSurroundingChunks(center, chunkRenderDistance); for(unsigned int i = 0; irender(shader, lightingPass, viewProjectionMatrix, shadowVPs); } @@ -64,14 +64,14 @@ void Level::render(ACGL::OpenGL::SharedShaderProgram shader, bool lightingPass, } void Level::enqueueObjects(Graphics* graphics) { - std::vector nearChunks = getSurroundingChunks(-1); + std::vector nearChunks = getSurroundingChunks(cameraCenter->getPosition(), -1); for(unsigned int i = 0; ienqueueObjects(nearChunks.at(i)->getSortedObjects()); } graphics->enqueueObjects(&sortedCrossChunkObjects); } -std::vector Level::getSurroundingChunks(int chunkRenderDistance) { +std::vector Level::getSurroundingChunks(glm::vec3 center, int chunkRenderDistance) { int renderDistance = 0; if (chunkRenderDistance < 0) { if ((int)farPlane % chunkSize == 0) { @@ -84,8 +84,8 @@ std::vector Level::getSurroundingChunks(int chunkRenderDistance) { else { renderDistance = chunkRenderDistance; } - int xPosition = ((int)cameraCenter->getPosition().x + (terrain.getHeightmapWidth()/2))/chunkSize; - int zPosition = ((int)cameraCenter->getPosition().z + (terrain.getHeightmapHeight()/2))/chunkSize; + int xPosition = ((int)center.x + (terrain.getHeightmapWidth()/2))/chunkSize; + int zPosition = ((int)center.z + (terrain.getHeightmapHeight()/2))/chunkSize; int xStart = xPosition - renderDistance; unsigned int xEnd = xPosition + renderDistance; int zStart = zPosition - renderDistance; diff --git a/game/level.hh b/game/level.hh index 9ce9997..8eab380 100644 --- a/game/level.hh +++ b/game/level.hh @@ -33,7 +33,7 @@ class Level { void update(float runTimeSinceLastUpdate, float runTime, glm::vec2 mouseDelta, KeyboardState* keyboardState); void render(ACGL::OpenGL::SharedShaderProgram shader, bool lightingPass, - int chunkRenderDistance, glm::mat4* viewProjectionMatrix, + glm::vec3 center, int chunkRenderDistance, glm::mat4* viewProjectionMatrix, std::vector* shadowVPs=0); glm::vec3 getAmbientLight(); Light* getDirectionalLight(); @@ -67,7 +67,9 @@ class Level { void setCameraCenter(Object* object); void addLight(Light light); void preloadLightPosition(float xPos, float yPos, float zPos); - void addLightByParameters(float redColour, float greenColour, float blueColour, float intensity, float flameYOffset, float flameHeight, float flameWidth); + void addLightByParameters(float redColour, float greenColour, + float blueColour, float intensity, float flameYOffset, + float flameHeight, float flameWidth); void deleteFourLights(); void addTrigger(Trigger trigger); lua_State* getLuaState(); @@ -86,7 +88,7 @@ class Level { void sortObjects(int textureCount); std::vector>* getClosestLights(); private: - std::vector getSurroundingChunks(int chunkRenderDistance); + std::vector getSurroundingChunks(glm::vec3 center, int chunkRenderDistance); lua_State* luaState=nullptr; std::vector crossChunkObjects; std::vector> sortedCrossChunkObjects;