From 6433b56c3af8721f8301255818a8dd692e75492e Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sun, 4 Dec 2022 17:16:31 +0000 Subject: Generate and use a shadow map Generated when the directional light is specified in the environment call, passed to the directional light shader pass to conditionally illuminate each pixel. --- gfx/gl/sceneRenderer.cpp | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) (limited to 'gfx/gl/sceneRenderer.cpp') diff --git a/gfx/gl/sceneRenderer.cpp b/gfx/gl/sceneRenderer.cpp index a06a163..49c73a2 100644 --- a/gfx/gl/sceneRenderer.cpp +++ b/gfx/gl/sceneRenderer.cpp @@ -78,11 +78,15 @@ SceneRenderer::render(const SceneProvider & scene) const glBindTexture(GL_TEXTURE_2D, gPosition); glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, gNormal); + glActiveTexture(GL_TEXTURE2); + glBindTexture(GL_TEXTURE_2D, shadowMapper); scene.environment(shader, *this); scene.lights(shader); // Lighting pass glBindFramebuffer(GL_FRAMEBUFFER, output); + glViewport(0, 0, size.x, size.y); + glCullFace(GL_BACK); glDisable(GL_BLEND); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glActiveTexture(GL_TEXTURE0); @@ -102,32 +106,40 @@ SceneRenderer::render(const SceneProvider & scene) const void SceneRenderer::setAmbientLight(const glm::vec3 & colour) const { + glBindFramebuffer(GL_FRAMEBUFFER, gBuffer); glClearColor(colour.r, colour.g, colour.b, 1.0F); glClear(GL_COLOR_BUFFER_BIT); } void -SceneRenderer::setDirectionalLight(const glm::vec3 & colour, const glm::vec3 & direction) const +SceneRenderer::setDirectionalLight( + const glm::vec3 & colour, const glm::vec3 & direction, const SceneProvider & scene) const { if (colour.r > 0 || colour.g > 0 || colour.b > 0) { + const auto lvp = shadowMapper.update(scene, direction); + glBindFramebuffer(GL_FRAMEBUFFER, gBuffer); + glViewport(0, 0, size.x, size.y); dirLight.use(); - dirLight.setDirectionalLight(colour, direction); + dirLight.setDirectionalLight(colour, direction, lvp); glBindVertexArray(displayVAO); glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); glBindVertexArray(0); + glEnable(GL_DEPTH_TEST); } } SceneRenderer::DirectionalLightProgram::DirectionalLightProgram() : - Program {lightingShader_vs, directionalLight_fs}, directionLoc {*this, "lightDirection"}, colourLoc {*this, - "lightColour"} + Program {lightingShader_vs, directionalLight_fs}, directionLoc {*this, "lightDirection"}, + colourLoc {*this, "lightColour"}, lightViewProjectionLoc {*this, "lightViewProjection"} { } void -SceneRenderer::DirectionalLightProgram::setDirectionalLight(const glm::vec3 & c, const glm::vec3 & d) const +SceneRenderer::DirectionalLightProgram::setDirectionalLight( + const glm::vec3 & c, const glm::vec3 & d, const glm::mat4x4 & lvp) const { glUniform3fv(colourLoc, 1, glm::value_ptr(c)); const auto nd = glm::normalize(d); glUniform3fv(directionLoc, 1, glm::value_ptr(nd)); + glUniformMatrix4fv(lightViewProjectionLoc, 1, GL_FALSE, glm::value_ptr(lvp)); } -- cgit v1.2.3