diff --git a/Shader/depth.vsh b/Shader/depth.vsh index e3eba6a..f84c3cf 100644 --- a/Shader/depth.vsh +++ b/Shader/depth.vsh @@ -4,9 +4,8 @@ in vec3 aPosition; in vec3 aNormal; in vec3 aTexcoord; -uniform mat4 viewProjectionMatrix; -uniform mat4 modelMatrix; +uniform mat4 modelViewProjectionMatrix; void main() { - gl_Position = viewProjectionMatrix * modelMatrix * vec4(aPosition, 1.0); + gl_Position = modelViewProjectionMatrix * vec4(aPosition, 1.0); } diff --git a/Shader/phong.vsh b/Shader/phong.vsh index d2f840e..d2806b9 100644 --- a/Shader/phong.vsh +++ b/Shader/phong.vsh @@ -1,10 +1,8 @@ #version 150 uniform mat4 modelMatrix; -uniform mat4 lightingViewProjectionMatrix; -uniform mat4 shadowMVP_near; -uniform mat4 shadowMVP_middle; -uniform mat4 shadowMVP_far; +uniform mat4 modelViewProjectionMatrix; +uniform mat4 shadowMVPs[35]; in vec3 aPosition; in vec3 aNormal; @@ -22,8 +20,8 @@ void main() fragPosition = modelMatrix * vec4(aPosition, 1.0); vNormal = inverse(transpose(mat3(modelMatrix))) * aNormal; vTexCoord = aTexCoord; - shadowCoord_near = shadowMVP_near * modelMatrix * vec4(aPosition, 1.0); - shadowCoord_middle = shadowMVP_middle * modelMatrix * vec4(aPosition, 1.0); - shadowCoord_far = shadowMVP_far * modelMatrix * vec4(aPosition, 1.0); - gl_Position = lightingViewProjectionMatrix * modelMatrix * vec4(aPosition, 1.0); + shadowCoord_near = shadowMVPs[0] * vec4(aPosition, 1.0); + shadowCoord_middle = shadowMVPs[1] * vec4(aPosition, 1.0); + shadowCoord_far = shadowMVPs[2] * vec4(aPosition, 1.0); + gl_Position = modelViewProjectionMatrix * vec4(aPosition, 1.0); } diff --git a/graphics.cc b/graphics.cc index 9e944ee..8f35b41 100644 --- a/graphics.cc +++ b/graphics.cc @@ -97,7 +97,7 @@ void Graphics::render() depthShader->use(); // render depth textures for point lights glViewport(0, 0, cube_size, cube_size); - glm::mat4 depthProjectionMatrix_pointlights = glm::perspective(45.0f, 0.1f, farPlane, (float)cube_size/(float)cube_size); + glm::mat4 depthProjectionMatrix_pointlights = glm::perspective(1.571f, (float)cube_size/(float)cube_size, 0.1f, farPlane); 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)}; @@ -110,7 +110,7 @@ void Graphics::render() 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)); depthShader->setUniform("viewProjectionMatrix", depthViewProjectionMatrix_face); - level->render(depthShader, false); + level->render(depthShader, false, &depthViewProjectionMatrix_face); if (!framebuffer_cube->isFrameBufferObjectComplete()) { printf("Framebuffer incomplete, unknown error occured during shadow generation!\n"); } @@ -124,8 +124,7 @@ void Graphics::render() glm::vec3 sunVector = (level->getCameraCenter()->getPosition() + level->getDirectionalLight()->getPosition()); glm::mat4 depthViewProjectionMatrix_near = glm::ortho(-5, 5, -5, 5, -5, 5) * glm::lookAt(sunVector, level->getCameraCenter()->getPosition(), glm::vec3(0,1,0)); - depthShader->setUniform("viewProjectionMatrix", depthViewProjectionMatrix_near); - level->render(depthShader, false); + level->render(depthShader, false, &depthViewProjectionMatrix_near); if (!framebuffer_near->isFrameBufferObjectComplete()) { printf("Framebuffer incomplete, unknown error occured during shadow generation!\n"); } @@ -135,8 +134,7 @@ void Graphics::render() glClear(GL_DEPTH_BUFFER_BIT); glm::mat4 depthViewProjectionMatrix_middle = glm::ortho(-20, 20, -20, 20, -20, 20) * glm::lookAt(sunVector, level->getCameraCenter()->getPosition(), glm::vec3(0,1,0)); - depthShader->setUniform("viewProjectionMatrix", depthViewProjectionMatrix_middle); - level->render(depthShader, false); + level->render(depthShader, false, &depthViewProjectionMatrix_middle); if (!framebuffer_middle->isFrameBufferObjectComplete()) { printf("Framebuffer incomplete, unknown error occured during shadow generation!\n"); } @@ -146,8 +144,7 @@ void Graphics::render() glClear(GL_DEPTH_BUFFER_BIT); glm::mat4 depthViewProjectionMatrix_far = glm::ortho(-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)); - depthShader->setUniform("viewProjectionMatrix", depthViewProjectionMatrix_far); - level->render(depthShader, false); + level->render(depthShader, false, &depthViewProjectionMatrix_far); if (!framebuffer_far->isFrameBufferObjectComplete()) { printf("Framebuffer incomplete, unknown error occured during shadow generation!\n"); } @@ -158,10 +155,6 @@ void Graphics::render() lightingShader->use(); - //set view and projection matrix - glm::mat4 lightingViewProjectionMatrix = buildFrustum(75.0f, 0.1f, farPlane, (float)windowSize.x/(float)windowSize.y) * buildViewMatrix(level); - lightingShader->setUniform("lightingViewProjectionMatrix", lightingViewProjectionMatrix); - //set lighting parameters if (level->getLights()->size() > 0) { lightingShader->setUniform("lightCount", (int) level->getLights()->size()); @@ -207,15 +200,12 @@ void Graphics::render() 0.0, 0.0, 0.5, 0.0, 0.5, 0.5, 0.5, 1.0 ); - glm::mat4 depthBiasMVP_near = biasMatrix*depthViewProjectionMatrix_near; - glm::mat4 depthBiasMVP_middle = biasMatrix*depthViewProjectionMatrix_middle; - glm::mat4 depthBiasMVP_far = biasMatrix*depthViewProjectionMatrix_far; + glm::mat4 depthBiasVP_near = biasMatrix*depthViewProjectionMatrix_near; + glm::mat4 depthBiasVP_middle = biasMatrix*depthViewProjectionMatrix_middle; + glm::mat4 depthBiasVP_far = biasMatrix*depthViewProjectionMatrix_far; - lightingShader->setUniform("shadowMVP_near", depthBiasMVP_near); lightingShader->setTexture("shadowMap_near", depthTexture_near, 1); - lightingShader->setUniform("shadowMVP_middle", depthBiasMVP_middle); lightingShader->setTexture("shadowMap_middle", depthTexture_middle, 2); - lightingShader->setUniform("shadowMVP_far", depthBiasMVP_far); lightingShader->setTexture("shadowMap_far", depthTexture_far, 3); // set fog Parameters @@ -227,8 +217,16 @@ void Graphics::render() lightingShader->setUniform("ambientColor", level->getAmbientLight()); lightingShader->setUniform("camera", level->getCameraPosition()); + //set view and projection matrix + glm::mat4 lightingViewProjectionMatrix = glm::perspective(1.571f, (float)windowSize.x/(float)windowSize.y, 0.1f, farPlane) * buildViewMatrix(level); + + std::vector shadowVPs = std::vector(); + shadowVPs.push_back(depthBiasVP_near); + shadowVPs.push_back(depthBiasVP_middle); + shadowVPs.push_back(depthBiasVP_far); + // render the level - level->render(lightingShader, true); + level->render(lightingShader, true, &lightingViewProjectionMatrix, &shadowVPs); } void Graphics::resize(glm::uvec2 windowSize) { @@ -238,19 +236,8 @@ void Graphics::resize(glm::uvec2 windowSize) { depthTexture_far->resize(glm::vec2(windowSize.x, windowSize.y)); } -glm::mat4 Graphics::buildFrustum( float phiInDegree, float _near, float _far, float aspectRatio) { - - float phiHalfInRadians = 0.5*phiInDegree * (M_PI/180.0); - float top = _near * tan( phiHalfInRadians ); - float bottom = -top; - float left = bottom * aspectRatio; - float right = -left; - - return glm::frustum(left, right, bottom, top, _near, _far); -} - glm::mat4 Graphics::buildViewMatrix(Level* level) { - //construct lookAt (cameraPosition = cameraCenter + cameraVector + //construct lookAt (cameraPosition = cameraCenter + cameraVector) return glm::lookAt((level->getCameraCenter()->getPosition() + level->getCamera()->getVector()), level->getCameraCenter()->getPosition(), glm::vec3(0.0f, 1.0f, 0.0f)); } diff --git a/graphics.hh b/graphics.hh index 8dd1018..416f5ce 100644 --- a/graphics.hh +++ b/graphics.hh @@ -14,8 +14,6 @@ class Graphics { Graphics(); void init(Level* level); void render(); - // to build the projection matrix: - glm::mat4 buildFrustum( float phiInDegree, float near, float far, float aspectRatio); glm::mat4 buildViewMatrix(Level* level); glm::uvec2 getWindowSize(); bool createWindow(); diff --git a/level.cc b/level.cc index ac75d81..13e82b8 100644 --- a/level.cc +++ b/level.cc @@ -296,11 +296,12 @@ void Level::load() { cameraCenter = object; } -void Level::render(ACGL::OpenGL::SharedShaderProgram shader, bool lightingPass) { +void Level::render(ACGL::OpenGL::SharedShaderProgram shader, bool lightingPass, + glm::mat4* viewProjectionMatrix, std::vector* shadowVPs) { for(unsigned int i = 0; irender(shader, lightingPass); + objects.at(i)->render(shader, lightingPass, viewProjectionMatrix, shadowVPs); } } } diff --git a/level.hh b/level.hh index 0ceb9c5..364d392 100644 --- a/level.hh +++ b/level.hh @@ -18,7 +18,8 @@ class Level { ~Level(); void load(); void update(float runTime, glm::vec2 mouseDelta,bool wPressed, bool aPressed,bool sPressed, bool dPressed); - void render(ACGL::OpenGL::SharedShaderProgram shader, bool lightingPass); + void render(ACGL::OpenGL::SharedShaderProgram shader, bool lightingPass, + glm::mat4* viewProjectionMatrix, std::vector* shadowVPs=0); glm::vec3 getAmbientLight(); Light* getDirectionalLight(); std::vector* getLights(); diff --git a/object.cc b/object.cc index 6e4fe82..601fed0 100644 --- a/object.cc +++ b/object.cc @@ -12,7 +12,9 @@ Object::Object() { Object::~Object() { } -void Object::render(ACGL::OpenGL::SharedShaderProgram shader, bool lightingPass) { +void Object::render(ACGL::OpenGL::SharedShaderProgram shader, bool lightingPass, + glm::mat4* viewProjectionMatrix, std::vector* shadowVPs) { + glm::mat4 modelMatrix = glm::translate(getPosition()) * getRotation() * glm::scale(glm::vec3(model.getScale())); if (lightingPass) { // set lightning parameters for this object shader->setUniform("ambientFactor", material.getAmbientFactor()); @@ -20,10 +22,18 @@ void Object::render(ACGL::OpenGL::SharedShaderProgram shader, bool lightingPass) shader->setUniform("specularFactor", material.getSpecularFactor()); shader->setUniform("shininess", material.getShininess()); shader->setTexture("uTexture", material.getReference(), 0); + // set model matrix + shader->setUniform( "modelMatrix", modelMatrix); + // set shadowMVPs + glm::mat4 shadowMVPs[35]; + for(unsigned int i = 0; (isize() && i<35); i++) { + shadowMVPs[i] = shadowVPs->at(i) * modelMatrix; + } + glUniformMatrix4fv(shader->getUniformLocation("shadowMVPs"), + sizeof(shadowMVPs), false, (GLfloat*) shadowMVPs); } - // set model matrix - glm::mat4 modelMatrix = glm::translate(getPosition()) * getRotation() * glm::scale(glm::vec3(model.getScale())); - shader->setUniform( "modelMatrix", modelMatrix); + glm::mat4 mvp = (*viewProjectionMatrix) * modelMatrix; + shader->setUniform("modelViewProjectionMatrix", mvp); // draw model.getReference()->render(); } diff --git a/object.hh b/object.hh index 526b90c..321e9a0 100644 --- a/object.hh +++ b/object.hh @@ -15,7 +15,8 @@ class Object : public Entity { glm::vec3 position, glm::vec3 rotation); Object(); ~Object(); - void render(ACGL::OpenGL::SharedShaderProgram shader, bool lightingPass); + void render(ACGL::OpenGL::SharedShaderProgram shader, bool lightingPass, + glm::mat4* viewProjcetionMatrix, std::vector* shadowVPs); private: Model model; Material material;