Shadow WIP stage 2

- fix cel-shading
This commit is contained in:
Ayumiwii
2016-02-10 11:48:44 +01:00
parent ecd823cb9f
commit 6af167675d
3 changed files with 57 additions and 45 deletions
+5 -4
View File
@@ -51,16 +51,17 @@ private:
GLuint m_DepthFBO; GLuint m_DepthFBO;
GLfloat m_NearFarPlane[2] = { -40.f, 30.f }; GLfloat m_NearFarPlane[2] = { -84.f, 28.f };
GLfloat m_LRBT[4] = { -40.f, 100.f, -50.f, 50.f }; GLfloat m_LRBT[4] = { -77.f, 75.f, -89.f, 89.f };
glm::mat4 m_LightProjection; glm::mat4 m_LightProjection;
glm::mat4 m_LightView; glm::mat4 m_LightView;
glm::mat4 m_LightSpaceMatrix; glm::mat4 m_LightSpaceMatrix;
GLuint resolutionSizeWidth = 2048; GLuint resolutionSizeWidth = 2048 * 4;
GLuint resolutionSizeHeigth = 2048; GLuint resolutionSizeHeigth = 2048 * 4;
bool m_ShadowOn = true;
}; };
#endif #endif
+19 -13
View File
@@ -124,7 +124,8 @@ vec2 poissonDisk[4] = vec2[](
float CalcShadowValue(vec4 positionLightSpace, vec4 normal, vec4 lightDir, sampler2D depthTexture) float CalcShadowValue(vec4 positionLightSpace, vec4 normal, vec4 lightDir, sampler2D depthTexture)
{ {
float bias = max(0.05 * (1.0 - dot(normal, -lightDir)), 0.005); float bias = 0.005;
//float bias = max(0.05 * (1.0 - dot(normal, -lightDir)), 0.005);
// perform perspective divide // perform perspective divide
vec3 projCoords = positionLightSpace.xyz / positionLightSpace.w; vec3 projCoords = positionLightSpace.xyz / positionLightSpace.w;
// Transform to [0,1] range // Transform to [0,1] range
@@ -136,21 +137,26 @@ float CalcShadowValue(vec4 positionLightSpace, vec4 normal, vec4 lightDir, sampl
//float currentDepth = positionLightSpace.z; //float currentDepth = positionLightSpace.z;
float currentDepth = projCoords.z; float currentDepth = projCoords.z;
// Check whether current frag pos is in shadow // Check whether current frag pos is in shadow
//float shadow = currentDepth - bias > closestDepth ? 1.0 : 0.0; float shadow = currentDepth - bias > closestDepth ? 1.0 : 0.0;
// float shadow = currentDepth > closestDepth ? 1.0 : 0.0; //float shadow = currentDepth > closestDepth ? 1.0 : 0.0;
float shadow = 0.0; //float shadow = 0.0;
//soft shadow - using percentage-closer filtering (PCF) is to simply sample the surrounding texels of the depth map and average the results: ////soft shadow - using percentage-closer filtering (PCF) is to simply sample the surrounding texels of the depth map and average the results:
vec2 texelSize = 1.0 / textureSize(depthTexture, 0); //vec2 texelSize = 1.0 / textureSize(depthTexture, 0);
for(int x = -1; x <= 1; ++x) //for(int x = -1; x <= 1; ++x)
//{
// for(int y = -1; y <= 1; ++y)
// {
// float pcfDepth = texture(depthTexture, projCoords.xy + vec2(x, y) * texelSize).r;
// shadow += currentDepth - bias > pcfDepth ? 1.0 : 0.0;
// }
//}
//shadow /= 9.0;
//
if(projCoords.z > 0.9)
{ {
for(int y = -1; y <= 1; ++y) shadow = 1.0;
{
float pcfDepth = texture(depthTexture, projCoords.xy + vec2(x, y) * texelSize).r;
shadow += currentDepth - bias > pcfDepth ? 1.0 : 0.0;
} }
}
shadow /= 9.0;
return shadow; return shadow;
+8 -3
View File
@@ -25,13 +25,14 @@ void ShadowPass::InitializeFrameBuffers()
// Depth texture // Depth texture
glGenTextures(1, &m_DepthMap); glGenTextures(1, &m_DepthMap);
glBindTexture(GL_TEXTURE_2D, m_DepthMap); glBindTexture(GL_TEXTURE_2D, m_DepthMap);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, resolutionSizeWidth, resolutionSizeHeigth, 0, GL_DEPTH_COMPONENT, GL_FLOAT, nullptr); glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT32, resolutionSizeWidth, resolutionSizeHeigth, 0, GL_DEPTH_COMPONENT, GL_FLOAT, nullptr);
//glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB16F, m_Renderer->GetViewportSize().Width, m_Renderer->GetViewportSize().Height, 0, GL_RGB, GL_FLOAT, 0); //glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB16F, m_Renderer->GetViewportSize().Width, m_Renderer->GetViewportSize().Height, 0, GL_RGB, GL_FLOAT, 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
m_DepthBuffer.AddResource(std::shared_ptr<BufferResource>(new Texture2D(&m_DepthMap, GL_DEPTH_ATTACHMENT))); m_DepthBuffer.AddResource(std::shared_ptr<BufferResource>(new Texture2D(&m_DepthMap, GL_DEPTH_ATTACHMENT)));
//m_DepthBuffer.AddResource(std::shared_ptr<BufferResource>(new Texture2D(&m_DepthMap, GL_COLOR_ATTACHMENT0))); //m_DepthBuffer.AddResource(std::shared_ptr<BufferResource>(new Texture2D(&m_DepthMap, GL_COLOR_ATTACHMENT0)));
m_DepthBuffer.Generate(); m_DepthBuffer.Generate();
@@ -66,12 +67,15 @@ void ShadowPass::Draw(RenderScene & scene)
m_ShadowProgram->Bind(); m_ShadowProgram->Bind();
glViewport(0, 0, resolutionSizeWidth, resolutionSizeHeigth); glViewport(0, 0, resolutionSizeWidth, resolutionSizeHeigth);
glCullFace(GL_FRONT); glCullFace(GL_BACK);
//state->Disable(GL_CULL_FACE);
ImGui::DragFloat4("ShadowMapCam", m_LRBT, 1.f, -1000.f, 1000.f); ImGui::DragFloat4("ShadowMapCam", m_LRBT, 1.f, -1000.f, 1000.f);
ImGui::DragFloat2("ShadowMapNearFar", m_NearFarPlane, 1.f, -1000.f, 1000.f); ImGui::DragFloat2("ShadowMapNearFar", m_NearFarPlane, 1.f, -1000.f, 1000.f);
ImGui::Checkbox("EnableShadow", &m_ShadowOn);
if (m_ShadowOn == true)
{
for (auto &job : scene.DirectionalLightJobs) { for (auto &job : scene.DirectionalLightJobs) {
auto directionalLightJob = std::dynamic_pointer_cast<DirectionalLightJob>(job); auto directionalLightJob = std::dynamic_pointer_cast<DirectionalLightJob>(job);
@@ -108,6 +112,7 @@ void ShadowPass::Draw(RenderScene & scene)
} }
}