Texture Array is now working. Enjoy your single Bind().

This commit is contained in:
FakeShemp
2016-02-27 15:53:28 +01:00
parent 22c391df54
commit 38d8cdded2
6 changed files with 91 additions and 76 deletions
+2 -2
View File
@@ -68,7 +68,7 @@ private:
GLuint m_DepthFBO;
GLfloat m_NearFarPlane[2] = { -34.f, 27.f };
GLfloat m_LRBT[4] = { -10.f, 10.f, -10.f, 10.f };
//GLfloat m_LRBT[4] = { -10.f, 10.f, -10.f, 10.f };
GLuint m_ResolutionSizeWidth = 1024 * 2;
GLuint m_ResolutionSizeHeigth = 1024 * 2;
@@ -77,7 +77,7 @@ private:
int m_ShadowLevel = 0;
int m_CurrentNrOfSplits = 3;
float m_SplitWeight = 0.75f;
float m_SplitWeight = 0.7f;
std::array<Frustum, MAX_SPLITS> m_shadowFrusta;
};
+45 -29
View File
@@ -17,7 +17,7 @@ layout (binding = 0) uniform sampler2D DiffuseTexture;
layout (binding = 1) uniform sampler2D NormalMapTexture;
layout (binding = 2) uniform sampler2D SpecularMapTexture;
layout (binding = 3) uniform sampler2D GlowMapTexture;
layout (binding = 4) uniform sampler2DShadow DepthMap[MAX_SPLITS];
layout (binding = 4) uniform sampler2DArrayShadow DepthMap;
#define TILE_SIZE 16
@@ -138,32 +138,59 @@ vec2 poissonDisk[16] = vec2[](
vec2( 0.14383161, -0.14100790 )
);
float random(vec3 seed, int i)
float Random(vec3 seed, int i)
{
vec4 seed4 = vec4(seed, i);
float dot_product = dot(seed4, vec4(12.9898, 78.233, 45.164, 94.673));
return fract(sin(dot_product) * 43758.5453);
}
float CalcShadowValue(vec4 positionLightSpace, vec3 normal, vec3 lightDir, sampler2DShadow depthTexture)
// Standard hardware-calculated PCF method
float PCFShadow(sampler2DArrayShadow depth_texture_array, vec3 projection_coords, int layer_index)
{
return texture(depth_texture_array, vec4(projection_coords.xy, layer_index, projection_coords.z));
}
float CalcShadowValue(vec4 light_space_pos, vec3 normal, vec3 light_dir, sampler2DArrayShadow depth_texture_array, int layer_index)
{
//float bias = 0.005;
//float bias = max(0.05 * (1.0 - dot(normal, lightDir)), 0.005);
float bias = 0.005 * tan(acos(clamp(dot(normal, -lightDir), 0.0, 1.0)));
vec3 projCoords = vec3(positionLightSpace.xy, positionLightSpace.z + bias) / positionLightSpace.w;
projCoords = projCoords * 0.5 + 0.5;
//float shadowMapDepth = texture(depthTexture, projCoords.xy).r;
float shadowMapDepth;
float bias;
// Various bias methods.
//bias = 0.005;
//bias = max(0.05 * (1.0 - dot(normal, light_dir)), 0.005);
bias = 0.005 * tan(acos(clamp(dot(normal, -light_dir), 0.0, 1.0)));
// Calculate coordinates in projection space
vec3 projCoords = vec3(light_space_pos.xy, light_space_pos.z + bias) / light_space_pos.w;
projCoords = projCoords * 0.5 + 0.5;
// Various methods for shadow calculation in fastest to slowest order.
shadowMapDepth = PCFShadow(depth_texture_array, projCoords, layer_index);
// PCF + Four-tap Poisson model method
//float SplitWeight = 0.7;
//for (int i = 0; i < 4; i++)
//{
// int index = i;
// //int index = int(16.0 * random(gl_FragCoord.xyy, i)) % 16;
// shadowMapDepth += 0.25 * texture(depthTexture, projCoords + vec3(poissonDisk[index], 0.0) / 700.0);
// int loop = i;
// vec3 newProjCoords = projCoords + vec3(poissonDisk[loop], 0.0) / (1500.0 * SplitWeight * (1.0 + layer_index));
// shadowMapDepth += 0.25 * texture(depthTexture, vec4(newProjCoords.xy, layer_index, newProjCoords.z));
//}
shadowMapDepth = texture(depthTexture, projCoords);
//
//// PCF + Four-tap Poisson model method
//float SplitWeight = 0.7;
//for (int i = 0; i < 4; i++)
//{
// int loop = i;
// vec3 newProjCoords = projCoords + vec3(poissonDisk[loop], 0.0) / (1500.0 * SplitWeight * (1.0 + layer_index));
// //int loop = int(16.0 * Random(gl_FragCoord.xyy, i)) % 16;
// shadowMapDepth += 0.25 * texture(depthTexture, vec4(newProjCoords.xy, layer_index, newProjCoords.z));
//}
//float geometryDepth = projCoords.z;
//float shadow = geometryDepth - bias > shadowMapDepth ? 1.0 : 0.0;
@@ -242,22 +269,11 @@ void main()
if(light.Type == 1) { // point
light_result = CalcPointLightSource(V * light.Position, light.Radius, light.Color, light.Intensity, viewVec, position, normal, light.Falloff);
} else if (light.Type == 2) { //Directional
//int DepthMapIndex = getShadowIndex(FarDistance);
int DepthMapIndex = getShadowIndex(FarDistance);
light_result = CalcDirectionalLightSource(V * light.Direction, light.Color, light.Intensity, viewVec, normal);
//if( DepthMapIndex == 0 )
//{
shadowFactor = CalcShadowValue(Input.PositionLightSpace[0], Input.Normal, vec3(light.Direction), DepthMap[0]);
//}
//else if( DepthMapIndex == 1 )
//{
// shadowFactor = CalcShadowValue(Input.PositionLightSpace[DepthMapIndex], Input.Normal, vec3(light.Direction), DepthMap1);
//}
//else
//{
// shadowFactor = CalcShadowValue(Input.PositionLightSpace[DepthMapIndex], Input.Normal, vec3(light.Direction), DepthMap2);
//}
shadowFactor = CalcShadowValue(Input.PositionLightSpace[DepthMapIndex], Input.Normal, vec3(light.Direction), DepthMap, DepthMapIndex);
}
totalLighting.Diffuse += light_result.Diffuse;
-1
View File
@@ -73,7 +73,6 @@ void FrameBuffer::Generate()
glFramebufferTexture(GL_FRAMEBUFFER, (*it)->m_Attachment, *(*it)->m_ResourceHandle, 0);
attachments.push_back((*it)->m_Attachment);
GLERROR("FrameBuffer generate: GL_TEXTURE_2D_ARRAY");
break;
}
+3 -3
View File
@@ -136,9 +136,9 @@ void Renderer::Draw(RenderFrame& frame)
if (m_DebugTextureToDraw == 4) {
m_DrawScreenQuadPass->Draw(m_PickingPass->PickingTexture());
}
//if (m_DebugTextureToDraw == 5) {
// m_DrawScreenQuadPass->Draw(m_ShadowPass->DepthMap());
//}
if (m_DebugTextureToDraw == 5) {
m_DrawScreenQuadPass->Draw(m_ShadowPass->DepthMap());
}
m_ImGuiRenderPass->Draw();
glfwSwapBuffers(m_Window);
+37 -41
View File
@@ -119,9 +119,6 @@ void ShadowPass::InitializeFrameBuffers()
glTexSubImage3D(GL_TEXTURE_2D_ARRAY, 0, 0, 0, 0, m_ResolutionSizeWidth, m_ResolutionSizeHeigth, m_CurrentNrOfSplits, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, nullptr);
GLERROR("depthMap failed4");
//for (int i = 0; i < m_CurrentNrOfSplits; i++) {
// glBindTexture(GL_TEXTURE_2D, m_DepthMap[i]);
// glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT16, m_ResolutionSizeWidth / (1 /*+ i*/), m_ResolutionSizeHeigth + (1 /*+ i*/), 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, nullptr);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
@@ -129,7 +126,6 @@ void ShadowPass::InitializeFrameBuffers()
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE);
glTexParameterfv(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_BORDER_COLOR, glm::vec4(1.f).data);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
//glTexParameteri(GL_TEXTURE_2D, GL_DEPTH_TEXTURE_MODE, GL_INTENSITY);
GLERROR("depthMap failed5");
m_DepthBuffer.AddResource(std::shared_ptr<BufferResource>(new Texture2DArray(&m_DepthMap, GL_DEPTH_ATTACHMENT, m_CurrentNrOfSplits)));
@@ -151,12 +147,15 @@ void ShadowPass::InitializeShaderPrograms()
void ShadowPass::ClearBuffer()
{
m_DepthBuffer.Bind();
for (int i = 0; i < m_CurrentNrOfSplits; i++) {
m_DepthBuffer.Bind();
glClearColor(0.f, 0.f, 0.f, 0.f);
glFramebufferTextureLayer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, m_DepthMap, 0, i);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
m_DepthBuffer.Unbind();
}
m_DepthBuffer.Unbind();
}
void ShadowPass::PointsToLightspace(Frustum& frustum, glm::mat4 v)
@@ -191,67 +190,64 @@ void ShadowPass::RadiusToLightspace(Frustum& frustum, glm::mat4 v)
void ShadowPass::Draw(RenderScene & scene)
{
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::Checkbox("EnableShadow", &m_ShadowOn);
ImGui::DragInt("ShadowLevel", &m_ShadowLevel, 0.05f, 0, m_CurrentNrOfSplits - 1);
//ImGui::DragInt("ShadowLevel", &m_ShadowLevel, 0.05f, 0, m_CurrentNrOfSplits - 1);
InitializeCameras(scene);
UpdateSplitDist(m_shadowFrusta, scene.Camera->NearClip(), scene.Camera->FarClip());
ShadowPassState* state = new ShadowPassState(m_DepthBuffer.GetHandle());
m_ShadowProgram->Bind();
for (int i = 0; i < m_CurrentNrOfSplits; i++) {
UpdateFrustumPoints(m_shadowFrusta[i], scene.Camera->Position(), scene.Camera->Forward(), scene.Camera->ProjectionMatrix(), scene.Camera->ViewMatrix());
//float test = FindRadius(m_shadFrusta[i]);
ShadowPassState* state = new ShadowPassState(m_DepthBuffer.GetHandle());
GLuint shaderHandle = m_ShadowProgram->GetHandle();
m_ShadowProgram->Bind();
glFramebufferTextureLayer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, m_DepthMap, 0, i);
glViewport(0, 0, m_ResolutionSizeWidth / (1/* + i*/), m_ResolutionSizeHeigth);
glDisable(GL_TEXTURE_2D);
glCullFace(GL_FRONT);
glViewport(0, 0, m_ResolutionSizeWidth, m_ResolutionSizeHeigth);
//state->Disable(GL_CULL_FACE);
if (m_ShadowOn == true)
{
for (auto &job : scene.DirectionalLightJobs) {
auto directionalLightJob = std::dynamic_pointer_cast<DirectionalLightJob>(job);
for (auto &job : scene.DirectionalLightJobs) {
auto directionalLightJob = std::dynamic_pointer_cast<DirectionalLightJob>(job);
if (directionalLightJob) {
m_LightView[i] = glm::lookAt(glm::vec3(-directionalLightJob->Direction) + m_shadowFrusta[i].MiddlePoint, m_shadowFrusta[i].MiddlePoint, glm::vec3(0.f, 1.f, 0.f));
PointsToLightspace(m_shadowFrusta[i], m_LightView[i]);
m_LightProjection[i] = glm::ortho(m_shadowFrusta[i].LRBT[Left], m_shadowFrusta[i].LRBT[Right], m_shadowFrusta[i].LRBT[Bottom], m_shadowFrusta[i].LRBT[Top], -30.f, 30.f);
if (directionalLightJob) {
m_LightView[i] = glm::lookAt(glm::vec3(-directionalLightJob->Direction) + m_shadowFrusta[i].MiddlePoint, m_shadowFrusta[i].MiddlePoint, glm::vec3(0.f, 1.f, 0.f));
glUniformMatrix4fv(glGetUniformLocation(shaderHandle, "P"), 1, GL_FALSE, glm::value_ptr(m_LightProjection[i]));
glUniformMatrix4fv(glGetUniformLocation(shaderHandle, "V"), 1, GL_FALSE, glm::value_ptr(m_LightView[i]));
PointsToLightspace(m_shadowFrusta[i], m_LightView[i]);
m_LightProjection[i] = glm::ortho(m_shadowFrusta[i].LRBT[Left], m_shadowFrusta[i].LRBT[Right], m_shadowFrusta[i].LRBT[Bottom], m_shadowFrusta[i].LRBT[Top], m_NearFarPlane[Near], m_NearFarPlane[Far]);
GLERROR("ShadowLight ERROR");
glUniformMatrix4fv(glGetUniformLocation(shaderHandle, "P"), 1, GL_FALSE, glm::value_ptr(m_LightProjection[i]));
glUniformMatrix4fv(glGetUniformLocation(shaderHandle, "V"), 1, GL_FALSE, glm::value_ptr(m_LightView[i]));
for (auto &objectJob : scene.OpaqueObjects) {
auto modelJob = std::dynamic_pointer_cast<ModelJob>(objectJob);
glUniformMatrix4fv(glGetUniformLocation(shaderHandle, "M"), 1, GL_FALSE, glm::value_ptr(modelJob->Matrix));
GLERROR("ShadowLight ERROR");
glBindVertexArray(modelJob->Model->VAO);
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)));
for (auto &objectJob : scene.OpaqueObjects) {
auto modelJob = std::dynamic_pointer_cast<ModelJob>(objectJob);
GLERROR("Shadow Draw ERROR");
}
glUniformMatrix4fv(glGetUniformLocation(shaderHandle, "M"), 1, GL_FALSE, glm::value_ptr(modelJob->Matrix));
glBindVertexArray(modelJob->Model->VAO);
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)));
GLERROR("Shadow Draw ERROR");
}
m_DepthBuffer.Unbind();
delete state;
}
}
glViewport(0, 0, m_Renderer->GetViewportSize().Width, m_Renderer->GetViewportSize().Height);
glEnable(GL_TEXTURE_2D);
glCullFace(GL_BACK);
m_ShadowProgram->Unbind();
}
m_DepthBuffer.Unbind();
delete state;
}
+4
View File
@@ -8,6 +8,10 @@ ShadowPassState::ShadowPassState(GLuint frameBuffer)
Enable(GL_DEPTH_TEST);
Enable(GL_CULL_FACE);
Disable(GL_BLEND);
Disable(GL_TEXTURE_2D);
CullFace(GL_FRONT);
ClearColor(glm::vec4(255.f, 128.f, 128.f, 128.f));
GLERROR("---4");
}
ShadowPassState::~ShadowPassState()