Moved calculation of shadowMVP to the CPU side.
This commit is contained in:
parent
21009d0032
commit
36f7217288
@ -2,9 +2,7 @@
|
|||||||
|
|
||||||
uniform mat4 modelMatrix;
|
uniform mat4 modelMatrix;
|
||||||
uniform mat4 modelViewProjectionMatrix;
|
uniform mat4 modelViewProjectionMatrix;
|
||||||
uniform mat4 shadowMVP_near;
|
uniform mat4 shadowMVPs[35];
|
||||||
uniform mat4 shadowMVP_middle;
|
|
||||||
uniform mat4 shadowMVP_far;
|
|
||||||
|
|
||||||
in vec3 aPosition;
|
in vec3 aPosition;
|
||||||
in vec3 aNormal;
|
in vec3 aNormal;
|
||||||
@ -22,8 +20,8 @@ void main()
|
|||||||
fragPosition = modelMatrix * vec4(aPosition, 1.0);
|
fragPosition = modelMatrix * vec4(aPosition, 1.0);
|
||||||
vNormal = inverse(transpose(mat3(modelMatrix))) * aNormal;
|
vNormal = inverse(transpose(mat3(modelMatrix))) * aNormal;
|
||||||
vTexCoord = aTexCoord;
|
vTexCoord = aTexCoord;
|
||||||
shadowCoord_near = shadowMVP_near * modelMatrix * vec4(aPosition, 1.0);
|
shadowCoord_near = shadowMVPs[0] * vec4(aPosition, 1.0);
|
||||||
shadowCoord_middle = shadowMVP_middle * modelMatrix * vec4(aPosition, 1.0);
|
shadowCoord_middle = shadowMVPs[1] * vec4(aPosition, 1.0);
|
||||||
shadowCoord_far = shadowMVP_far * modelMatrix * vec4(aPosition, 1.0);
|
shadowCoord_far = shadowMVPs[2] * vec4(aPosition, 1.0);
|
||||||
gl_Position = modelViewProjectionMatrix * vec4(aPosition, 1.0);
|
gl_Position = modelViewProjectionMatrix * vec4(aPosition, 1.0);
|
||||||
}
|
}
|
||||||
|
27
graphics.cc
27
graphics.cc
@ -110,7 +110,7 @@ void Graphics::render()
|
|||||||
glm::mat4 depthViewProjectionMatrix_face = depthProjectionMatrix_pointlights * glm::lookAt(level->getLights()->at(i_pointlight).getPosition(),
|
glm::mat4 depthViewProjectionMatrix_face = depthProjectionMatrix_pointlights * glm::lookAt(level->getLights()->at(i_pointlight).getPosition(),
|
||||||
level->getLights()->at(i_pointlight).getPosition() + looking_directions[i_face], glm::vec3(0.0f, 1.0f, 0.0f));
|
level->getLights()->at(i_pointlight).getPosition() + looking_directions[i_face], glm::vec3(0.0f, 1.0f, 0.0f));
|
||||||
depthShader->setUniform("viewProjectionMatrix", depthViewProjectionMatrix_face);
|
depthShader->setUniform("viewProjectionMatrix", depthViewProjectionMatrix_face);
|
||||||
level->render(depthShader, false, depthViewProjectionMatrix_face);
|
level->render(depthShader, false, &depthViewProjectionMatrix_face);
|
||||||
if (!framebuffer_cube->isFrameBufferObjectComplete()) {
|
if (!framebuffer_cube->isFrameBufferObjectComplete()) {
|
||||||
printf("Framebuffer incomplete, unknown error occured during shadow generation!\n");
|
printf("Framebuffer incomplete, unknown error occured during shadow generation!\n");
|
||||||
}
|
}
|
||||||
@ -124,7 +124,7 @@ void Graphics::render()
|
|||||||
glm::vec3 sunVector = (level->getCameraCenter()->getPosition() + level->getDirectionalLight()->getPosition());
|
glm::vec3 sunVector = (level->getCameraCenter()->getPosition() + level->getDirectionalLight()->getPosition());
|
||||||
glm::mat4 depthViewProjectionMatrix_near = glm::ortho<float>(-5, 5, -5, 5, -5, 5) *
|
glm::mat4 depthViewProjectionMatrix_near = glm::ortho<float>(-5, 5, -5, 5, -5, 5) *
|
||||||
glm::lookAt(sunVector, level->getCameraCenter()->getPosition(), glm::vec3(0,1,0));
|
glm::lookAt(sunVector, level->getCameraCenter()->getPosition(), glm::vec3(0,1,0));
|
||||||
level->render(depthShader, false, depthViewProjectionMatrix_near);
|
level->render(depthShader, false, &depthViewProjectionMatrix_near);
|
||||||
if (!framebuffer_near->isFrameBufferObjectComplete()) {
|
if (!framebuffer_near->isFrameBufferObjectComplete()) {
|
||||||
printf("Framebuffer incomplete, unknown error occured during shadow generation!\n");
|
printf("Framebuffer incomplete, unknown error occured during shadow generation!\n");
|
||||||
}
|
}
|
||||||
@ -134,7 +134,7 @@ void Graphics::render()
|
|||||||
glClear(GL_DEPTH_BUFFER_BIT);
|
glClear(GL_DEPTH_BUFFER_BIT);
|
||||||
glm::mat4 depthViewProjectionMatrix_middle = glm::ortho<float>(-20, 20, -20, 20, -20, 20) *
|
glm::mat4 depthViewProjectionMatrix_middle = glm::ortho<float>(-20, 20, -20, 20, -20, 20) *
|
||||||
glm::lookAt(sunVector, level->getCameraCenter()->getPosition(), glm::vec3(0,1,0));
|
glm::lookAt(sunVector, level->getCameraCenter()->getPosition(), glm::vec3(0,1,0));
|
||||||
level->render(depthShader, false, depthViewProjectionMatrix_middle);
|
level->render(depthShader, false, &depthViewProjectionMatrix_middle);
|
||||||
if (!framebuffer_middle->isFrameBufferObjectComplete()) {
|
if (!framebuffer_middle->isFrameBufferObjectComplete()) {
|
||||||
printf("Framebuffer incomplete, unknown error occured during shadow generation!\n");
|
printf("Framebuffer incomplete, unknown error occured during shadow generation!\n");
|
||||||
}
|
}
|
||||||
@ -144,7 +144,7 @@ void Graphics::render()
|
|||||||
glClear(GL_DEPTH_BUFFER_BIT);
|
glClear(GL_DEPTH_BUFFER_BIT);
|
||||||
glm::mat4 depthViewProjectionMatrix_far = glm::ortho<float>(-farPlane/2.0f, farPlane/2.0f, -farPlane/2.0f, farPlane/2.0f, -farPlane/2.0f, farPlane/2.0f) *
|
glm::mat4 depthViewProjectionMatrix_far = glm::ortho<float>(-farPlane/2.0f, farPlane/2.0f, -farPlane/2.0f, farPlane/2.0f, -farPlane/2.0f, farPlane/2.0f) *
|
||||||
glm::lookAt(sunVector, level->getCameraCenter()->getPosition(), glm::vec3(0,1,0));
|
glm::lookAt(sunVector, level->getCameraCenter()->getPosition(), glm::vec3(0,1,0));
|
||||||
level->render(depthShader, false, depthViewProjectionMatrix_far);
|
level->render(depthShader, false, &depthViewProjectionMatrix_far);
|
||||||
if (!framebuffer_far->isFrameBufferObjectComplete()) {
|
if (!framebuffer_far->isFrameBufferObjectComplete()) {
|
||||||
printf("Framebuffer incomplete, unknown error occured during shadow generation!\n");
|
printf("Framebuffer incomplete, unknown error occured during shadow generation!\n");
|
||||||
}
|
}
|
||||||
@ -200,15 +200,15 @@ void Graphics::render()
|
|||||||
0.0, 0.0, 0.5, 0.0,
|
0.0, 0.0, 0.5, 0.0,
|
||||||
0.5, 0.5, 0.5, 1.0
|
0.5, 0.5, 0.5, 1.0
|
||||||
);
|
);
|
||||||
glm::mat4 depthBiasMVP_near = biasMatrix*depthViewProjectionMatrix_near;
|
glm::mat4 depthBiasVP_near = biasMatrix*depthViewProjectionMatrix_near;
|
||||||
glm::mat4 depthBiasMVP_middle = biasMatrix*depthViewProjectionMatrix_middle;
|
glm::mat4 depthBiasVP_middle = biasMatrix*depthViewProjectionMatrix_middle;
|
||||||
glm::mat4 depthBiasMVP_far = biasMatrix*depthViewProjectionMatrix_far;
|
glm::mat4 depthBiasVP_far = biasMatrix*depthViewProjectionMatrix_far;
|
||||||
|
|
||||||
lightingShader->setUniform("shadowMVP_near", depthBiasMVP_near);
|
//lightingShader->setUniform("shadowMVP_near", depthBiasVP_near);
|
||||||
lightingShader->setTexture("shadowMap_near", depthTexture_near, 1);
|
lightingShader->setTexture("shadowMap_near", depthTexture_near, 1);
|
||||||
lightingShader->setUniform("shadowMVP_middle", depthBiasMVP_middle);
|
//lightingShader->setUniform("shadowMVP_middle", depthBiasVP_middle);
|
||||||
lightingShader->setTexture("shadowMap_middle", depthTexture_middle, 2);
|
lightingShader->setTexture("shadowMap_middle", depthTexture_middle, 2);
|
||||||
lightingShader->setUniform("shadowMVP_far", depthBiasMVP_far);
|
//lightingShader->setUniform("shadowMVP_far", depthBiasVP_far);
|
||||||
lightingShader->setTexture("shadowMap_far", depthTexture_far, 3);
|
lightingShader->setTexture("shadowMap_far", depthTexture_far, 3);
|
||||||
|
|
||||||
// set fog Parameters
|
// set fog Parameters
|
||||||
@ -223,8 +223,13 @@ void Graphics::render()
|
|||||||
//set view and projection matrix
|
//set view and projection matrix
|
||||||
glm::mat4 lightingViewProjectionMatrix = glm::perspective(1.571f, (float)windowSize.x/(float)windowSize.y, 0.1f, farPlane) * buildViewMatrix(level);
|
glm::mat4 lightingViewProjectionMatrix = glm::perspective(1.571f, (float)windowSize.x/(float)windowSize.y, 0.1f, farPlane) * buildViewMatrix(level);
|
||||||
|
|
||||||
|
std::vector<glm::mat4> shadowVPs = std::vector<glm::mat4>();
|
||||||
|
shadowVPs.push_back(depthBiasVP_near);
|
||||||
|
shadowVPs.push_back(depthBiasVP_middle);
|
||||||
|
shadowVPs.push_back(depthBiasVP_far);
|
||||||
|
|
||||||
// render the level
|
// render the level
|
||||||
level->render(lightingShader, true, lightingViewProjectionMatrix);
|
level->render(lightingShader, true, &lightingViewProjectionMatrix, &shadowVPs);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Graphics::resize(glm::uvec2 windowSize) {
|
void Graphics::resize(glm::uvec2 windowSize) {
|
||||||
|
5
level.cc
5
level.cc
@ -294,11 +294,12 @@ void Level::load() {
|
|||||||
cameraCenter = object;
|
cameraCenter = object;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Level::render(ACGL::OpenGL::SharedShaderProgram shader, bool lightingPass, glm::mat4 viewProjectionMatrix) {
|
void Level::render(ACGL::OpenGL::SharedShaderProgram shader, bool lightingPass,
|
||||||
|
glm::mat4* viewProjectionMatrix, std::vector<glm::mat4>* shadowVPs) {
|
||||||
for(unsigned int i = 0; i<objects.size(); i++) {
|
for(unsigned int i = 0; i<objects.size(); i++) {
|
||||||
// do not project shadow of skydome
|
// do not project shadow of skydome
|
||||||
if(lightingPass || objects.at(i) != skydome) {
|
if(lightingPass || objects.at(i) != skydome) {
|
||||||
objects.at(i)->render(shader, lightingPass, viewProjectionMatrix);
|
objects.at(i)->render(shader, lightingPass, viewProjectionMatrix, shadowVPs);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
3
level.hh
3
level.hh
@ -18,7 +18,8 @@ class Level {
|
|||||||
~Level();
|
~Level();
|
||||||
void load();
|
void load();
|
||||||
void update(float runTime, glm::vec2 mouseDelta,bool wPressed, bool aPressed,bool sPressed, bool dPressed);
|
void update(float runTime, glm::vec2 mouseDelta,bool wPressed, bool aPressed,bool sPressed, bool dPressed);
|
||||||
void render(ACGL::OpenGL::SharedShaderProgram shader, bool lightingPass, glm::mat4 viewProjectionMatrix);
|
void render(ACGL::OpenGL::SharedShaderProgram shader, bool lightingPass,
|
||||||
|
glm::mat4* viewProjectionMatrix, std::vector<glm::mat4>* shadowVPs=0);
|
||||||
glm::vec3 getAmbientLight();
|
glm::vec3 getAmbientLight();
|
||||||
Light* getDirectionalLight();
|
Light* getDirectionalLight();
|
||||||
std::vector<Light>* getLights();
|
std::vector<Light>* getLights();
|
||||||
|
12
object.cc
12
object.cc
@ -12,7 +12,8 @@ Object::Object() {
|
|||||||
Object::~Object() {
|
Object::~Object() {
|
||||||
}
|
}
|
||||||
|
|
||||||
void Object::render(ACGL::OpenGL::SharedShaderProgram shader, bool lightingPass, glm::mat4 viewProjectionMatrix) {
|
void Object::render(ACGL::OpenGL::SharedShaderProgram shader, bool lightingPass,
|
||||||
|
glm::mat4* viewProjectionMatrix, std::vector<glm::mat4>* shadowVPs) {
|
||||||
glm::mat4 modelMatrix = glm::translate(getPosition()) * getRotation() * glm::scale<float>(glm::vec3(model.getScale()));
|
glm::mat4 modelMatrix = glm::translate(getPosition()) * getRotation() * glm::scale<float>(glm::vec3(model.getScale()));
|
||||||
if (lightingPass) {
|
if (lightingPass) {
|
||||||
// set lightning parameters for this object
|
// set lightning parameters for this object
|
||||||
@ -23,8 +24,15 @@ void Object::render(ACGL::OpenGL::SharedShaderProgram shader, bool lightingPass,
|
|||||||
shader->setTexture("uTexture", material.getReference(), 0);
|
shader->setTexture("uTexture", material.getReference(), 0);
|
||||||
// set model matrix
|
// set model matrix
|
||||||
shader->setUniform( "modelMatrix", modelMatrix);
|
shader->setUniform( "modelMatrix", modelMatrix);
|
||||||
|
// set shadowMVPs
|
||||||
|
glm::mat4 shadowMVPs[35];
|
||||||
|
for(unsigned int i = 0; (i<shadowVPs->size() && i<35); i++) {
|
||||||
|
shadowMVPs[i] = shadowVPs->at(i) * modelMatrix;
|
||||||
|
}
|
||||||
|
glUniformMatrix4fv(shader->getUniformLocation("shadowMVPs"),
|
||||||
|
sizeof(shadowMVPs), false, (GLfloat*) shadowMVPs);
|
||||||
}
|
}
|
||||||
glm::mat4 mvp = viewProjectionMatrix * modelMatrix;
|
glm::mat4 mvp = (*viewProjectionMatrix) * modelMatrix;
|
||||||
shader->setUniform("modelViewProjectionMatrix", mvp);
|
shader->setUniform("modelViewProjectionMatrix", mvp);
|
||||||
// draw
|
// draw
|
||||||
model.getReference()->render();
|
model.getReference()->render();
|
||||||
|
@ -15,7 +15,8 @@ class Object : public Entity {
|
|||||||
glm::vec3 position, glm::vec3 rotation);
|
glm::vec3 position, glm::vec3 rotation);
|
||||||
Object();
|
Object();
|
||||||
~Object();
|
~Object();
|
||||||
void render(ACGL::OpenGL::SharedShaderProgram shader, bool lightingPass, glm::mat4 viewProjcetionMatrix);
|
void render(ACGL::OpenGL::SharedShaderProgram shader, bool lightingPass,
|
||||||
|
glm::mat4* viewProjcetionMatrix, std::vector<glm::mat4>* shadowVPs);
|
||||||
private:
|
private:
|
||||||
Model model;
|
Model model;
|
||||||
Material material;
|
Material material;
|
||||||
|
Loading…
Reference in New Issue
Block a user