Shadow WIP stage 2
- fix cel-shading
This commit is contained in:
@@ -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
|
||||||
@@ -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;
|
||||||
|
|
||||||
|
|||||||
@@ -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,47 +67,51 @@ 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);
|
||||||
|
|
||||||
for (auto &job : scene.DirectionalLightJobs) {
|
if (m_ShadowOn == true)
|
||||||
auto directionalLightJob = std::dynamic_pointer_cast<DirectionalLightJob>(job);
|
{
|
||||||
|
for (auto &job : scene.DirectionalLightJobs) {
|
||||||
|
auto directionalLightJob = std::dynamic_pointer_cast<DirectionalLightJob>(job);
|
||||||
|
|
||||||
if(directionalLightJob) {
|
if(directionalLightJob) {
|
||||||
//m_LightProjection = glm::ortho(m_LRBT[Left], m_LRBT[Right], m_LRBT[Bottom], m_LRBT[Top], m_NearFarPlane[Near], m_NearFarPlane[Far]);
|
//m_LightProjection = glm::ortho(m_LRBT[Left], m_LRBT[Right], m_LRBT[Bottom], m_LRBT[Top], m_NearFarPlane[Near], m_NearFarPlane[Far]);
|
||||||
m_LightProjection = glm::ortho(m_LRBT[Left], m_LRBT[Right], m_LRBT[Bottom], m_LRBT[Top], m_NearFarPlane[Near], m_NearFarPlane[Far]);
|
m_LightProjection = glm::ortho(m_LRBT[Left], m_LRBT[Right], m_LRBT[Bottom], m_LRBT[Top], m_NearFarPlane[Near], m_NearFarPlane[Far]);
|
||||||
m_LightView = glm::lookAt(-glm::normalize(glm::vec3(directionalLightJob->Direction)), glm::vec3(0.f,0.f,0.f), glm::vec3(0.f,1.f,0.f));
|
m_LightView = glm::lookAt(-glm::normalize(glm::vec3(directionalLightJob->Direction)), glm::vec3(0.f,0.f,0.f), glm::vec3(0.f,1.f,0.f));
|
||||||
//m_LightView = glm::lookAt(glm::vec3(-20.0f, 20.0f, -20.0f), glm::vec3(0.0f), glm::vec3(1.0));
|
//m_LightView = glm::lookAt(glm::vec3(-20.0f, 20.0f, -20.0f), glm::vec3(0.0f), glm::vec3(1.0));
|
||||||
m_LightSpaceMatrix = m_LightProjection * m_LightView;
|
m_LightSpaceMatrix = m_LightProjection * m_LightView;
|
||||||
|
|
||||||
glUniformMatrix4fv(glGetUniformLocation(shaderHandle, "P"), 1, GL_FALSE, glm::value_ptr(m_LightProjection));
|
glUniformMatrix4fv(glGetUniformLocation(shaderHandle, "P"), 1, GL_FALSE, glm::value_ptr(m_LightProjection));
|
||||||
glUniformMatrix4fv(glGetUniformLocation(shaderHandle, "V"), 1, GL_FALSE, glm::value_ptr(m_LightView));
|
glUniformMatrix4fv(glGetUniformLocation(shaderHandle, "V"), 1, GL_FALSE, glm::value_ptr(m_LightView));
|
||||||
|
|
||||||
GLERROR("ShadowLight ERROR");
|
GLERROR("ShadowLight ERROR");
|
||||||
|
|
||||||
for (auto &objectJob : scene.OpaqueObjects) {
|
for (auto &objectJob : scene.OpaqueObjects) {
|
||||||
auto modelJob = std::dynamic_pointer_cast<ModelJob>(objectJob);
|
auto modelJob = std::dynamic_pointer_cast<ModelJob>(objectJob);
|
||||||
|
|
||||||
glUniformMatrix4fv(glGetUniformLocation(shaderHandle, "M"), 1, GL_FALSE, glm::value_ptr(modelJob->Matrix));
|
glUniformMatrix4fv(glGetUniformLocation(shaderHandle, "M"), 1, GL_FALSE, glm::value_ptr(modelJob->Matrix));
|
||||||
|
|
||||||
glBindVertexArray(modelJob->Model->VAO);
|
glBindVertexArray(modelJob->Model->VAO);
|
||||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, modelJob->Model->ElementBuffer);
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, modelJob->Model->ElementBuffer);
|
||||||
glDrawElements(GL_TRIANGLES, modelJob->EndIndex - modelJob->StartIndex + 1, GL_UNSIGNED_INT, (void*)(modelJob->StartIndex * sizeof(unsigned int)));
|
glDrawElements(GL_TRIANGLES, modelJob->EndIndex - modelJob->StartIndex + 1, GL_UNSIGNED_INT, (void*)(modelJob->StartIndex * sizeof(unsigned int)));
|
||||||
|
|
||||||
GLERROR("Shadow Draw ERROR");
|
GLERROR("Shadow Draw ERROR");
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
m_DepthBuffer.Unbind();
|
||||||
|
|
||||||
|
delete state;
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
m_DepthBuffer.Unbind();
|
|
||||||
|
|
||||||
delete state;
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user