Implemented updating of shadow render queue. Doesn't work satisfactory right now. (#10)

This commit is contained in:
Faerbit 2015-06-02 00:18:22 +02:00
parent ea194309ef
commit 819584ef15
3 changed files with 83 additions and 39 deletions

View File

@ -56,8 +56,8 @@ ADD_DEFINITIONS(-DNO_SPACE_NAVIGATOR_SUPPORT)
SET(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS} -DSAXUM_DEBUG -g") SET(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS} -DSAXUM_DEBUG -g")
SET(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS} -DSAXUM_DEBUG -g") SET(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS} -DSAXUM_DEBUG -g")
SET(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS} -O2") SET(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS} -O2 -DNDEBUG")
SET(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS} -O2") SET(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS} -O2 -DNDEBUG")
set(dir ${CMAKE_CURRENT_SOURCE_DIR}/binaries) set(dir ${CMAKE_CURRENT_SOURCE_DIR}/binaries)
set(EXECUTABLE_OUTPUT_PATH ${dir} CACHE PATH "Build directory" FORCE) set(EXECUTABLE_OUTPUT_PATH ${dir} CACHE PATH "Build directory" FORCE)

View File

@ -180,8 +180,6 @@ void Graphics::init(Level* level) {
bindTextureUnits(); bindTextureUnits();
updateLights();
// set shader variables that stay the same across the runtime of the application // set shader variables that stay the same across the runtime of the application
skydomeShader->use(); skydomeShader->use();
skydomeShader->setUniform("farPlane", farPlane); skydomeShader->setUniform("farPlane", farPlane);
@ -211,12 +209,14 @@ void Graphics::init(Level* level) {
depthCubeShader->setUniform("farPlane", farPlane); depthCubeShader->setUniform("farPlane", farPlane);
level->sortObjects(Material::getAllTextures()->size()); level->sortObjects(Material::getAllTextures()->size());
#ifdef SAXUM_DEBUG #ifdef SAXUM_DEBUG
std::cout << "There were " << Material::getAllTextures()->size() std::cout << "There were " << Material::getAllTextures()->size()
<< " materials used in this level." << std::endl; << " materials used in this level." << std::endl;
#endif #endif
initShadowRenderQueue(); initShadowRenderQueue();
updateLights();
} }
void Graphics::bindTextureUnits(){ void Graphics::bindTextureUnits(){
@ -334,7 +334,7 @@ void Graphics::render(double time)
double nextLightUpdate = lastLightUpdate + lightUpdateDelay; double nextLightUpdate = lastLightUpdate + lightUpdateDelay;
if (time >= nextLightUpdate) if (time >= nextLightUpdate)
{ {
//updateLights(); updateLights();
lastLightUpdate = time; lastLightUpdate = time;
} }
@ -375,26 +375,29 @@ void Graphics::render(double time)
framebuffer_cube->bind(); framebuffer_cube->bind();
for (unsigned int i_pointlight = 0; i_pointlight < renderQueue.size(); i_pointlight++) { for (unsigned int i_pointlight = 0; i_pointlight < renderQueue.size(); i_pointlight++) {
// render each side of the cube // check if queue points to a existing light
glm::vec3 position = glm::vec3(0.0f); if (std::get<0>(renderQueue.at(i_pointlight))) {
if (std::get<0>(renderQueue.at(i_pointlight))->isFlame()) { // render each side of the cube
position = std::get<0>(renderQueue.at(i_pointlight))->getPosition(); glm::vec3 position = glm::vec3(0.0f);
position = glm::vec3(position.x + 0.75f*wind.x, position.y, position.z + 0.75f*wind.y); if (std::get<0>(renderQueue.at(i_pointlight))->isFlame()) {
} position = std::get<0>(renderQueue.at(i_pointlight))->getPosition();
else { position = glm::vec3(position.x + 0.75f*wind.x, position.y, position.z + 0.75f*wind.y);
position = std::get<0>(renderQueue.at(i_pointlight))->getPosition(); }
} else {
for (int i_face = 0; i_face<6; i_face++) { position = std::get<0>(renderQueue.at(i_pointlight))->getPosition();
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_CUBE_MAP_POSITIVE_X + i_face, }
depth_cubeMaps.at(std::get<2>(renderQueue.at(i_pointlight)))->getObjectName(), 0); for (int i_face = 0; i_face<6; i_face++) {
glClear(GL_DEPTH_BUFFER_BIT); glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_CUBE_MAP_POSITIVE_X + i_face,
glm::mat4 viewMatrix = glm::lookAt(position, position + looking_directions[i_face], upvectors[i_face]); depth_cubeMaps.at(std::get<2>(renderQueue.at(i_pointlight)))->getObjectName(), 0);
glm::mat4 depthViewProjectionMatrix_face = depthProjectionMatrix_pointlights * viewMatrix; glClear(GL_DEPTH_BUFFER_BIT);
std::vector<glm::mat4> viewMatrixVector = std::vector<glm::mat4>(1); glm::mat4 viewMatrix = glm::lookAt(position, position + looking_directions[i_face], upvectors[i_face]);
viewMatrixVector.at(0) = viewMatrix; glm::mat4 depthViewProjectionMatrix_face = depthProjectionMatrix_pointlights * viewMatrix;
level->render(depthCubeShader, false, std::get<0>(renderQueue.at(i_pointlight))->getPosition(), 1, &depthViewProjectionMatrix_face, &viewMatrixVector); std::vector<glm::mat4> viewMatrixVector = std::vector<glm::mat4>(1);
if (!framebuffer_cube->isFrameBufferObjectComplete()) { viewMatrixVector.at(0) = viewMatrix;
printf("Framebuffer incomplete, unknown error occured during shadow generation!\n"); level->render(depthCubeShader, false, std::get<0>(renderQueue.at(i_pointlight))->getPosition(), 1, &depthViewProjectionMatrix_face, &viewMatrixVector);
if (!framebuffer_cube->isFrameBufferObjectComplete()) {
printf("Framebuffer incomplete, unknown error occured during shadow generation!\n");
}
} }
} }
} }
@ -627,12 +630,52 @@ void Graphics::render(double time)
} }
void Graphics::updateLights() { void Graphics::updateLights() {
std::vector<std::shared_ptr<Light>> oldClosestLights = std::vector<std::shared_ptr<Light>>(*closestLights);
closestLights = level->getClosestLights(); closestLights = level->getClosestLights();
if (closestLights->size() > 0) { if (closestLights->size() > 0) {
lightingShader->use(); lightingShader->use();
lightingShader->setUniform("lightCount", (int) closestLights->size()); lightingShader->setUniform("lightCount", (int) closestLights->size());
lightingShader->setUniform("maxShadowRenderCount", min((int)closestLights->size(), maxShadowSampleCount)); lightingShader->setUniform("maxShadowRenderCount", min((int)closestLights->size(), maxShadowSampleCount));
// find new closest lights for the shadow render queue
unsigned int i = 0;
std::vector<std::shared_ptr<Light>> compareClosestLights = std::vector<std::shared_ptr<Light>>(*closestLights);
while(i<oldClosestLights.size()) {
bool found = false;
for(unsigned int j = 0; j<compareClosestLights.size(); j++) {
if (oldClosestLights.at(i) == compareClosestLights.at(j)){
found = true;
compareClosestLights.erase(compareClosestLights.begin() + j);
break;
}
}
if (found) {
oldClosestLights.erase(oldClosestLights.begin() + i);
}
else {
i++;
}
}
assert(oldClosestLights.size() == compareClosestLights.size());
// replace old lights with the new ones in the shadow render queue
for(unsigned int i = 0; i<oldClosestLights.size(); i++) {
for(unsigned int j = 0; j<shadowRenderQueue.size(); j++) {
if(oldClosestLights.at(i) == shadowRenderQueue.at(j).light) {
shadowRenderQueue.at(j).light = compareClosestLights.at(i);
// 15000 is larger priority than any light can get during one tick
shadowRenderQueue.at(j).currentPriority = 15000;
}
}
}
// update priority of the shadow render queue
for(unsigned int i = 0; i<shadowRenderQueue.size(); i++) {
float distance = glm::distance(level->getCameraCenter()->getPosition(), shadowRenderQueue.at(i).light->getPosition());
shadowRenderQueue.at(i).priority = (int) 100*std::exp(5.0f - 0.1f * distance);
}
// Build light position array // Build light position array
glm::vec3 lightSources[closestLights->size()]; glm::vec3 lightSources[closestLights->size()];
for(unsigned int i = 0; i<closestLights->size(); i++) { for(unsigned int i = 0; i<closestLights->size(); i++) {
@ -654,20 +697,20 @@ void Graphics::updateLights() {
} }
glUniform1fv(lightingShader->getUniformLocation("lightIntensities"), glUniform1fv(lightingShader->getUniformLocation("lightIntensities"),
sizeof(lightIntensities), (GLfloat*) lightIntensities); sizeof(lightIntensities), (GLfloat*) lightIntensities);
}
// set directional Light bool isFlame[closestLights->size()];
bool isFlame[closestLights->size()]; closestFlames = std::vector<Flame*>();
closestFlames = std::vector<Flame*>(); for (unsigned int i = 0; i<closestLights->size(); i++) {
for (unsigned int i = 0; i<closestLights->size(); i++) { if (closestLights->at(i)->isFlame()) {
if (closestLights->at(i)->isFlame()) { closestFlames.push_back(closestLights->at(i)->getFlame());
closestFlames.push_back(closestLights->at(i)->getFlame()); isFlame[i] = true;
isFlame[i] = true; }
} else {
else { isFlame[i] = false;
isFlame[i] = false; }
} }
glUniform1iv(lightingShader->getUniformLocation("isFlame"), sizeof(isFlame), (GLint*) isFlame);
} }
glUniform1iv(lightingShader->getUniformLocation("isFlame"), sizeof(isFlame), (GLint*) isFlame);
} }
void Graphics::saveWindowSize(glm::uvec2 windowSize) { void Graphics::saveWindowSize(glm::uvec2 windowSize) {
@ -812,6 +855,7 @@ void Graphics::enqueueObjects(std::vector<std::vector<Object*>>* queue){
} }
void Graphics::initShadowRenderQueue() { void Graphics::initShadowRenderQueue() {
closestLights = level->getClosestLights();
int maxLights = min((int)closestLights->size(), maxShadowSampleCount); int maxLights = min((int)closestLights->size(), maxShadowSampleCount);
shadowRenderQueue = std::vector<ShadowRenderQueueSlot>(maxLights); shadowRenderQueue = std::vector<ShadowRenderQueueSlot>(maxLights);
glViewport(0, 0, cube_size, cube_size); glViewport(0, 0, cube_size, cube_size);
@ -825,8 +869,6 @@ void Graphics::initShadowRenderQueue() {
for(unsigned int i = 0; i<shadowRenderQueue.size(); i++){ for(unsigned int i = 0; i<shadowRenderQueue.size(); i++){
shadowRenderQueue.at(i).light = closestLights->at(i); shadowRenderQueue.at(i).light = closestLights->at(i);
float distance = glm::distance(level->getCameraCenter()->getPosition(), closestLights->at(i)->getPosition());
shadowRenderQueue.at(i).priority = (int) 100*std::exp(5.0f - 0.1f * distance);
shadowRenderQueue.at(i).currentPriority = 0; shadowRenderQueue.at(i).currentPriority = 0;
// render depth textures for point lights // render depth textures for point lights
depthCubeShader->use(); depthCubeShader->use();

View File

@ -462,5 +462,7 @@ std::vector<shared_ptr<Light>>* Level::getClosestLights() {
closestLights = std::vector<shared_ptr<Light>>(&closestLights[0], closestLights = std::vector<shared_ptr<Light>>(&closestLights[0],
&closestLights[15]); &closestLights[15]);
} }
// sort pointers for faster comparisons
std::sort(closestLights.begin(), closestLights.end());
return &closestLights; return &closestLights;
} }