maxShadowRenderCount now works correctly.

This commit is contained in:
Fabian Klemp 2015-02-13 17:09:25 +01:00
parent 377902a255
commit fd1f32be6a
3 changed files with 34 additions and 47 deletions

View File

@ -26,6 +26,7 @@ uniform float specularFactor;
uniform vec3 camera; uniform vec3 camera;
uniform float shininess; uniform float shininess;
uniform int lightCount; uniform int lightCount;
uniform int maxShadowRenderCount;
uniform vec3 directionalLightVector; uniform vec3 directionalLightVector;
uniform vec3 directionalColor; uniform vec3 directionalColor;
uniform float directionalIntensity; uniform float directionalIntensity;
@ -115,45 +116,37 @@ void main()
for(int i = 0; i<lightCount; i++) { for(int i = 0; i<lightCount; i++) {
vec3 lightDirection = vec3(fragPosition) - lightSources[i]; vec3 lightDirection = vec3(fragPosition) - lightSources[i];
float distance = length(lightDirection); float distance = length(lightDirection);
float pointVisibility = 0.0f; float pointVisibility = 1.0f;
// only take lights into account with meaningful contribution // only take lights into account with meaningful contribution
if (distance < farPlane) { if (distance < farPlane) {
if (i == 0) { if (i == 0 && i<maxShadowRenderCount) {
pointVisibility = samplePointShadow(shadowMap_cube0, lightDirection); pointVisibility = samplePointShadow(shadowMap_cube0, lightDirection);
} }
if (i == 1) { if (i == 1 && i<maxShadowRenderCount) {
pointVisibility = samplePointShadow(shadowMap_cube1, lightDirection); pointVisibility = samplePointShadow(shadowMap_cube1, lightDirection);
} }
if (i == 2 && i<maxShadowRenderCount) {
if (i == 2) {
pointVisibility = samplePointShadow(shadowMap_cube2, lightDirection); pointVisibility = samplePointShadow(shadowMap_cube2, lightDirection);
} }
if (i == 3 && i<maxShadowRenderCount) {
if (i == 3) {
pointVisibility = samplePointShadow(shadowMap_cube3, lightDirection); pointVisibility = samplePointShadow(shadowMap_cube3, lightDirection);
} }
if (i == 4 && i<maxShadowRenderCount) {
if (i == 4) {
pointVisibility = samplePointShadow(shadowMap_cube4, lightDirection); pointVisibility = samplePointShadow(shadowMap_cube4, lightDirection);
} }
if (i == 5 && i<maxShadowRenderCount) {
if (i == 5) {
pointVisibility = samplePointShadow(shadowMap_cube5, lightDirection); pointVisibility = samplePointShadow(shadowMap_cube5, lightDirection);
} }
if (i == 6 && i<maxShadowRenderCount) {
if (i == 6) {
pointVisibility = samplePointShadow(shadowMap_cube6, lightDirection); pointVisibility = samplePointShadow(shadowMap_cube6, lightDirection);
} }
if (i == 7 && i<maxShadowRenderCount) {
if (i == 7) {
pointVisibility = samplePointShadow(shadowMap_cube7, lightDirection); pointVisibility = samplePointShadow(shadowMap_cube7, lightDirection);
} }
if (i == 8 && i<maxShadowRenderCount) {
if (i == 8) {
pointVisibility = samplePointShadow(shadowMap_cube8, lightDirection); pointVisibility = samplePointShadow(shadowMap_cube8, lightDirection);
} }
if (i == 9 && i<maxShadowRenderCount) {
if (i == 9) {
pointVisibility = samplePointShadow(shadowMap_cube9, lightDirection); pointVisibility = samplePointShadow(shadowMap_cube9, lightDirection);
} }
vec3 lightVector = normalize(lightSources[i]-vec3(fragPosition)); vec3 lightVector = normalize(lightSources[i]-vec3(fragPosition));

View File

@ -112,13 +112,13 @@ void Graphics::render(double time)
glm::vec3(0.0f, 0.0f, -1.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, -1.0f, 0.0f),glm::vec3(0.0f, -1.0f, 0.0f)};
framebuffer_cube->bind(); framebuffer_cube->bind();
for (unsigned int i_pointlight = 0; i_pointlight<closestLights->size() && i_pointlight < maxShadowRenderCount; i_pointlight++) { for (unsigned int i_pointlight = 0; i_pointlight<closestLights.size() && i_pointlight < maxShadowRenderCount; i_pointlight++) {
// render each side of the cube // render each side of the cube
for (int i_face = 0; i_face<6; i_face++) { 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_pointlight)->getObjectName(), 0); glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_CUBE_MAP_POSITIVE_X + i_face, depth_cubeMaps.at(i_pointlight)->getObjectName(), 0);
glClear(GL_DEPTH_BUFFER_BIT); glClear(GL_DEPTH_BUFFER_BIT);
glm::mat4 viewMatrix = glm::lookAt(closestLights->at(i_pointlight).getPosition(), glm::mat4 viewMatrix = glm::lookAt(closestLights.at(i_pointlight).getPosition(),
closestLights->at(i_pointlight).getPosition() + looking_directions[i_face], upvectors[i_face]); closestLights.at(i_pointlight).getPosition() + looking_directions[i_face], upvectors[i_face]);
glm::mat4 depthViewProjectionMatrix_face = depthProjectionMatrix_pointlights * viewMatrix; glm::mat4 depthViewProjectionMatrix_face = depthProjectionMatrix_pointlights * viewMatrix;
std::vector<glm::mat4> viewMatrixVector = std::vector<glm::mat4>(); std::vector<glm::mat4> viewMatrixVector = std::vector<glm::mat4>();
viewMatrixVector.push_back(viewMatrix); viewMatrixVector.push_back(viewMatrix);
@ -200,43 +200,38 @@ bool Graphics::compareLightDistances(Light a, Light b) {
} }
void Graphics::updateClosestLights() { void Graphics::updateClosestLights() {
if (level->getLights()->size() <= 32) { closestLights = std::vector<Light>(*level->getLights());
this->closestLights = level->getLights(); std::sort(closestLights.begin(),
} closestLights.end(),
else {
closestLightsVector = std::vector<Light>(*level->getLights());
std::sort(closestLightsVector.begin(),
closestLightsVector.end(),
[this](Light a, Light b) {return compareLightDistances(a, b); }); [this](Light a, Light b) {return compareLightDistances(a, b); });
closestLightsVector = std::vector<Light>(&closestLightsVector[0], closestLights = std::vector<Light>(&closestLights[0],
&closestLightsVector[31]); &closestLights[31]);
closestLights = &closestLightsVector;
}
} }
void Graphics::updateShaderLights() { void Graphics::updateShaderLights() {
updateClosestLights(); updateClosestLights();
if (closestLights->size() > 0) { if (closestLights.size() > 0) {
lightingShader->setUniform("lightCount", (int) closestLights->size()); lightingShader->setUniform("lightCount", (int) closestLights.size());
lightingShader->setUniform("maxShadowRenderCount", std::min((int) closestLights.size(), (int)maxShadowRenderCount));
// 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++) {
lightSources[i] = closestLights->at(i).getPosition(); lightSources[i] = closestLights.at(i).getPosition();
} }
glUniform3fv(lightingShader->getUniformLocation("lightSources"), glUniform3fv(lightingShader->getUniformLocation("lightSources"),
sizeof(lightSources), (GLfloat*) lightSources); sizeof(lightSources), (GLfloat*) lightSources);
// Build light colour array // Build light colour array
glm::vec3 lightColours[closestLights->size()]; glm::vec3 lightColours[closestLights.size()];
for(unsigned int i = 0; i<closestLights->size(); i++) { for(unsigned int i = 0; i<closestLights.size(); i++) {
lightColours[i] = closestLights->at(i).getColour(); lightColours[i] = closestLights.at(i).getColour();
} }
glUniform3fv(lightingShader->getUniformLocation("lightColors"), glUniform3fv(lightingShader->getUniformLocation("lightColors"),
sizeof(lightColours), (GLfloat*) lightColours); sizeof(lightColours), (GLfloat*) lightColours);
// Build light attenuation array // Build light attenuation array
float lightIntensities[closestLights->size()]; float lightIntensities[closestLights.size()];
for(unsigned int i = 0; i<closestLights->size(); i++) { for(unsigned int i = 0; i<closestLights.size(); i++) {
lightIntensities[i] = closestLights->at(i).getIntensity(); lightIntensities[i] = closestLights.at(i).getIntensity();
} }
glUniform1fv(lightingShader->getUniformLocation("lightIntensities"), glUniform1fv(lightingShader->getUniformLocation("lightIntensities"),
sizeof(lightIntensities), (GLfloat*) lightIntensities); sizeof(lightIntensities), (GLfloat*) lightIntensities);

View File

@ -28,8 +28,7 @@ class Graphics {
float nearPlane; float nearPlane;
float farPlane; float farPlane;
// pointer to either use the vector from the level or from here // pointer to either use the vector from the level or from here
std::vector<Light>* closestLights; std::vector<Light> closestLights;
std::vector<Light> closestLightsVector; // contains the 32 closest lights
ACGL::OpenGL::SharedShaderProgram lightingShader; ACGL::OpenGL::SharedShaderProgram lightingShader;
ACGL::OpenGL::SharedShaderProgram depthCubeShader; ACGL::OpenGL::SharedShaderProgram depthCubeShader;
ACGL::OpenGL::SharedShaderProgram depthShader; ACGL::OpenGL::SharedShaderProgram depthShader;