Use gl_FrontFacing to determine mirrored surface side, enable clipping (Refs #190)

This commit is contained in:
saschawillems 2016-07-06 21:13:24 +02:00
parent becdc7c9dc
commit 876f5714eb
7 changed files with 18 additions and 23 deletions

View file

@ -8,7 +8,6 @@ layout (binding = 2) uniform sampler2D samplerColorMap;
layout (location = 0) in vec2 inUV; layout (location = 0) in vec2 inUV;
layout (location = 1) in vec4 inPos; layout (location = 1) in vec4 inPos;
layout (location = 2) in vec3 inNormal;
layout (location = 0) out vec4 outFragColor; layout (location = 0) out vec4 outFragColor;
@ -28,7 +27,9 @@ void main()
vec4 color = texture(samplerColorMap, inUV); vec4 color = texture(samplerColorMap, inUV);
outFragColor = color * 0.25; outFragColor = color * 0.25;
if (inNormal.z > 0) { if (gl_FrontFacing)
{
// Only render mirrored scene on front facing (upper) side of mirror surface
vec4 reflection = vec4(0.0); vec4 reflection = vec4(0.0);
for (int x = -3; x <= 3; x++) for (int x = -3; x <= 3; x++)
{ {
@ -38,5 +39,5 @@ void main()
} }
} }
outFragColor += reflection * 1.5 * (color.r); outFragColor += reflection * 1.5 * (color.r);
} };
} }

View file

@ -5,7 +5,6 @@
layout (location = 0) in vec3 inPos; layout (location = 0) in vec3 inPos;
layout (location = 1) in vec2 inUV; layout (location = 1) in vec2 inUV;
layout (location = 3) in vec3 inNormal;
layout (binding = 0) uniform UBO layout (binding = 0) uniform UBO
{ {
@ -15,19 +14,14 @@ layout (binding = 0) uniform UBO
layout (location = 0) out vec2 outUV; layout (location = 0) out vec2 outUV;
layout (location = 1) out vec4 outPos; layout (location = 1) out vec4 outPos;
layout (location = 2) out vec3 outNormal;
mat3 mat3_emu(mat4 m4) { out gl_PerVertex
return mat3( {
m4[0][0], m4[0][1], m4[0][2], vec4 gl_Position;
m4[1][0], m4[1][1], m4[1][2], };
m4[2][0], m4[2][1], m4[2][2]);
}
void main() void main()
{ {
mat3 rotation = mat3_emu(ubo.model);
outNormal = rotation * inNormal;
outUV = inUV; outUV = inUV;
outPos = ubo.projection * ubo.model * vec4(inPos.xyz, 1.0); outPos = ubo.projection * ubo.model * vec4(inPos.xyz, 1.0);
gl_Position = outPos; gl_Position = outPos;

View file

@ -22,6 +22,7 @@ layout (location = 3) out vec3 outLightVec;
out gl_PerVertex out gl_PerVertex
{ {
vec4 gl_Position; vec4 gl_Position;
float gl_ClipDistance[];
}; };
void main() void main()
@ -33,8 +34,6 @@ void main()
outLightVec = normalize(ubo.lightPos.xyz - outEyePos); outLightVec = normalize(ubo.lightPos.xyz - outEyePos);
// Clip against reflection plane // Clip against reflection plane
//vec4 clipPlane = vec4(0.0, -1.0, 0.0, 0.0); vec4 clipPlane = vec4(0.0, -1.0, 0.0, 1.5);
gl_ClipDistance[0] = dot(inPos, clipPlane);
//gl_ClipDistance[0] = dot(inPos, clipPlane);
} }

View file

@ -789,14 +789,15 @@ public:
VK_CHECK_RESULT(vkCreateGraphicsPipelines(device, pipelineCache, 1, &pipelineCreateInfo, nullptr, &pipelines.debug)); VK_CHECK_RESULT(vkCreateGraphicsPipelines(device, pipelineCache, 1, &pipelineCreateInfo, nullptr, &pipelines.debug));
// Flip culling
rasterizationState.cullMode = VK_CULL_MODE_BACK_BIT;
// Mirror // Mirror
shaderStages[0] = loadShader(getAssetPath() + "shaders/offscreen/mirror.vert.spv", VK_SHADER_STAGE_VERTEX_BIT); shaderStages[0] = loadShader(getAssetPath() + "shaders/offscreen/mirror.vert.spv", VK_SHADER_STAGE_VERTEX_BIT);
shaderStages[1] = loadShader(getAssetPath() + "shaders/offscreen/mirror.frag.spv", VK_SHADER_STAGE_FRAGMENT_BIT); shaderStages[1] = loadShader(getAssetPath() + "shaders/offscreen/mirror.frag.spv", VK_SHADER_STAGE_FRAGMENT_BIT);
rasterizationState.cullMode = VK_CULL_MODE_NONE;
VK_CHECK_RESULT(vkCreateGraphicsPipelines(device, pipelineCache, 1, &pipelineCreateInfo, nullptr, &pipelines.mirror)); VK_CHECK_RESULT(vkCreateGraphicsPipelines(device, pipelineCache, 1, &pipelineCreateInfo, nullptr, &pipelines.mirror));
// Flip culling
rasterizationState.cullMode = VK_CULL_MODE_BACK_BIT;
// Phong shading pipelines // Phong shading pipelines
pipelineCreateInfo.layout = pipelineLayouts.shaded; pipelineCreateInfo.layout = pipelineLayouts.shaded;
// Scene // Scene