Commit 6825b962 authored by Dario Seyb's avatar Dario Seyb

pcf shadow maps

parent 775f7022
......@@ -4,8 +4,9 @@ uniform sampler2D uSamplerColor;
uniform sampler2D uSamplerNormal;
uniform sampler2D uSamplerDepth;
uniform sampler2D uSamplerShadowMap;
uniform sampler2DShadow uSamplerShadowMap;
uniform vec2 uOneOverShadowTexSize;
uniform vec3 uLightPosition;
uniform vec4 uLightColor;
uniform mat4 uLightProjMatrix;
......@@ -43,8 +44,17 @@ vec3 projShadowCoord(vec3 worldPosition) {
float shadowFactor(vec3 worldPosition) {
vec3 shadowCoord = projShadowCoord(worldPosition.xyz);
float shadowDepth = texture(uSamplerShadowMap, shadowCoord.xy).r;
return shadowDepth < shadowCoord.z -0.01 ? 0 : 1;
float x,y;
float shadow = 0;
for (y = -1.5 ; y <=1.5 ; y+=1.0) {
for (x = -1.5 ; x <=1.5 ; x+=1.0) {
shadow += texture(uSamplerShadowMap, shadowCoord + vec3(x * uOneOverShadowTexSize.x , y * uOneOverShadowTexSize.y, -0.005));
}
}
shadow /= 16.0 ;
return shadow;
}
void main()
......
......@@ -19,7 +19,7 @@ using namespace ACGL::OpenGL;
using namespace ACGL::Base;
using namespace ACGL::Utils;
const glm::vec2 SHADOW_MAP_RESOLUTION = { 2048, 2048 };
const glm::vec2 SHADOW_MAP_RESOLUTION = { 512, 512 };
bool RendererSystem::startup() {
RESOLVE_DEPENDENCY(m_events);
......@@ -75,14 +75,15 @@ bool RendererSystem::startup() {
for (auto &light : m_lights) {
light.shadowMap =
SharedTexture2D(new Texture2D(SHADOW_MAP_RESOLUTION));
light.shadowMap->setMinFilter(GL_NEAREST);
light.shadowMap->setMagFilter(GL_NEAREST);
SharedTexture2D(new Texture2D(SHADOW_MAP_RESOLUTION, GL_DEPTH24_STENCIL8));
light.shadowMap->setMinFilter(GL_LINEAR);
light.shadowMap->setMagFilter(GL_LINEAR);
light.shadowMap->setWrap(GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
light.shadowMap->setCompareMode(GL_COMPARE_REF_TO_TEXTURE);
light.shadowMap->setCompareFunc(GL_LEQUAL);
light.shadowFbo = SharedFrameBufferObject(new FrameBufferObject);
light.shadowFbo->attachColorTexture("oDepth", light.shadowMap);
light.shadowFbo->setDepthTexture(SharedTexture2D(new Texture2D(SHADOW_MAP_RESOLUTION, GL_DEPTH24_STENCIL8)));
light.shadowFbo->setDepthTexture(light.shadowMap);
light.shadowFbo->validate(); // always a good idea
}
......@@ -93,14 +94,11 @@ bool RendererSystem::startup() {
}
ImGui::End();
int i = 0;
ImGui::Begin("ShadowMap", 0, ImGuiWindowFlags_AlwaysAutoResize);
for (auto light : m_lights) {
ImGui::Begin("ShadowMap", 0, ImGuiWindowFlags_AlwaysAutoResize);
auto texSize = light.shadowMap->getSize();
ImGui::Image((void*)light.shadowMap->getObjectName(), { 256, 256 });
ImGui::End();
//ImGui::Image((void*)light.shadowMap->getObjectName(), { 256, 256 });
}
ImGui::End();
});
......@@ -169,8 +167,7 @@ void RendererSystem::frame(double interp, double totalTime) {
glViewport(0, 0, shadowRes.x, shadowRes.y);
auto viewMatrix = glm::lookAt<float>(light.position, light.position + glm::normalize(light.position), glm::vec3(0.0f, 1.0f, 0.0f));
auto lightVP = glm::ortho<float>(-15.0f, + 15.0f,
15.0f, -15.0f, 0.0f, -40.0f) * viewMatrix;
auto lightVP = glm::ortho<float>(-15.0f, + 15.0f, 15.0f, -15.0f, 0.0f, -40.0f) * viewMatrix;
light.projMatrix = lightVP;
......@@ -221,7 +218,11 @@ void RendererSystem::frame(double interp, double totalTime) {
for (auto light : m_lights) {
m_deferredCombineProgram->setUniform("uLightProjMatrix", biasMatrix * light.projMatrix);
m_deferredCombineProgram->setTexture("uSamplerShadowMap", light.shadowMap, 3);
m_deferredCombineProgram->setTexture("uSamplerShadowMap", light.shadowMap, 3);
auto size = light.shadowMap->getSize();
m_deferredCombineProgram->setUniform("uOneOverShadowTexSize", glm::vec2{ 1.0f/size.x, 1.0f/size.y });
m_deferredCombineProgram->setUniform("uLightPosition", light.position);
m_deferredCombineProgram->setUniform("uLightColor", light.color);
glDrawArrays(
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment