From 592f84b548e882c3ab974561456cba3cf0084d58 Mon Sep 17 00:00:00 2001 From: Faerbit Date: Mon, 1 Jun 2015 10:47:26 +0200 Subject: [PATCH] Implemented the the initilization of the shadow render queue.(#10) --- game/graphics.cc | 43 ++++++++++++++++++++++++++++++++++++++++--- game/graphics.hh | 8 ++++++++ 2 files changed, 48 insertions(+), 3 deletions(-) diff --git a/game/graphics.cc b/game/graphics.cc index f252c60..ae4f8be 100644 --- a/game/graphics.cc +++ b/game/graphics.cc @@ -197,6 +197,7 @@ void Graphics::init(Level* level) { lightingShader->setUniform("fogColorRise", level->getFogColourRise()); lightingShader->setUniform("fogColorNight", level->getFogColourNight()); lightingShader->setUniform("ambientColor", level->getAmbientLight()); + if(level->getDirectionalLight()) { lightingShader->setUniform("directionalLightVector", level->getDirectionalLight()->getPosition()); @@ -333,13 +334,14 @@ void Graphics::render(double time) double nextLightUpdate = lastLightUpdate + lightUpdateDelay; if (time >= nextLightUpdate) { - updateLights(); + //updateLights(); lastLightUpdate = time; } // At first render shadows std::vector depthViewProjectionMatrices = std::vector(framebuffer_directional.size()); if (renderShadows) { + /*depthCubeShader->use(); // render depth textures for point lights glViewport(0, 0, cube_size, cube_size); glm::mat4 depthProjectionMatrix_pointlights = glm::perspective(1.571f, (float)cube_size/(float)cube_size, 0.1f, 50.0f); @@ -365,7 +367,7 @@ void Graphics::render(double time) printf("Framebuffer incomplete, unknown error occured during shadow generation!\n"); } } - } + }*/ glViewport(0, 0, windowSize.x, windowSize.y); @@ -599,7 +601,7 @@ void Graphics::updateLights() { if (closestLights->size() > 0) { lightingShader->use(); lightingShader->setUniform("lightCount", (int) closestLights->size()); - lightingShader->setUniform("maxShadowRenderCount", std::min((int) closestLights->size(), (int)maxShadowRenderCount)); + lightingShader->setUniform("maxShadowRenderCount", min((int)closestLights->size(), maxShadowSampleCount)); // Build light position array glm::vec3 lightSources[closestLights->size()]; @@ -778,3 +780,38 @@ bool Graphics::getRenderWorld() { void Graphics::enqueueObjects(std::vector>* queue){ renderQueue.push_back(queue); } + +void Graphics::initShadowRenderQueue() { + int maxLights = min((int)closestLights->size(), maxShadowSampleCount); + shadowRenderQueue = std::vector(maxLights); + glViewport(0, 0, cube_size, cube_size); + glm::mat4 depthProjectionMatrix_pointlights = glm::perspective(1.571f, (float)cube_size/(float)cube_size, 0.1f, 50.0f); + glm::vec3 looking_directions[6] = {glm::vec3(1.0f, 0.0f, 0.0f), glm::vec3(-1.0f, 0.0f, 0.0f), glm::vec3(0.0f, 1.0f, 0.0f), + glm::vec3(0.0f, -1.0f, 0.0f), glm::vec3(0.0f, 0.0f, 1.0f), glm::vec3(0.0f, 0.0f, -1.0f)}; + glm::vec3 upvectors[6] = {glm::vec3(0.0f, -1.0f, 0.0f),glm::vec3(0.0f, -1.0f, 0.0f),glm::vec3(0.0f, 0.0f, -1.0f), + glm::vec3(0.0f, 0.0f, -1.0f),glm::vec3(0.0f, -1.0f, 0.0f),glm::vec3(0.0f, -1.0f, 0.0f)}; + + framebuffer_cube->bind(); + + for(unsigned int i = 0; iat(i); + shadowRenderQueue.at(i).priority = farPlane - glm::distance(level->getCameraCenter()->getPosition(), closestLights->at(i)->getPosition()); + shadowRenderQueue.at(i).currentPriority = 0; + // render depth textures for point lights + depthCubeShader->use(); + // render each side of the cube + for (int i_face = 0; i_face<6; i_face++) { + glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_CUBE_MAP_POSITIVE_X + i_face, depth_cubeMaps.at(i)->getObjectName(), 0); + glClear(GL_DEPTH_BUFFER_BIT); + glm::mat4 viewMatrix = glm::lookAt(shadowRenderQueue.at(i).light->getPosition(), + shadowRenderQueue.at(i).light->getPosition() + looking_directions[i_face], upvectors[i_face]); + glm::mat4 depthViewProjectionMatrix_face = depthProjectionMatrix_pointlights * viewMatrix; + std::vector viewMatrixVector = std::vector(); + viewMatrixVector.push_back(viewMatrix); + level->render(depthCubeShader, false, shadowRenderQueue.at(i).light->getPosition(), 1, &depthViewProjectionMatrix_face, &viewMatrixVector); + if (!framebuffer_cube->isFrameBufferObjectComplete()) { + printf("Framebuffer incomplete, unknown error occured during shadow generation!\n"); + } + } + } +} diff --git a/game/graphics.hh b/game/graphics.hh index ec1c815..0e63f7b 100644 --- a/game/graphics.hh +++ b/game/graphics.hh @@ -11,6 +11,12 @@ using namespace ACGL::OpenGL; +struct ShadowRenderQueueSlot{ + shared_ptr light; + int priority; + int currentPriority; +}; + class Graphics { public: Graphics(glm::uvec2 windowSize, float nearPlane, float farPlane, int cube_size, @@ -89,6 +95,8 @@ class Graphics { SharedVertexArrayObject debug_vao; SharedShaderProgram debugShader; std::vector>*> renderQueue; + std::vector shadowRenderQueue; + void initShadowRenderQueue(); }; #endif