Sign in to follow this  
Maddius

Geometry Shader Exterior Facing Triangle Culling

Recommended Posts

Maddius    146
Hi all,

I am implementing a geometry shader that culls triangles that are exterior facing but are facing in the same direction as the camera (assuming that all geometry is convex) in order to save the rendering effort. I have implemented it like so:

[code]
VS_OUT VS(VS_IN pIn)
{
VS_OUT wOut;

wOut.mPositionW = mul(float4(pIn.mPosition, 1.0f), gWorldMatrix);
wOut.mNormalW = mul(float4(pIn.mNormal, 0.0f), gWorldMatrix);
wOut.mPositionH = mul(float4(pIn.mPosition, 1.0f), gWorldViewProjMatrix);
wOut.mTextureCoords = mul(float4(pIn.mTextureCoords, 0.0f, 1.0f), gTextureMatrix);
wOut.mTangentW = mul(float4(pIn.mTangent, 0.0f), gWorldMatrix);

return wOut;
}

[maxvertexcount(3)]
void GS(triangle VS_OUT pInputTri[3], inout TriangleStream<VS_OUT> pTriStream)
{
float3 wToEye = gEyePosition - pInputTri[0].mPositionW;
float3 wV0 = normalize(pInputTri[1].mPositionW - pInputTri[0].mPositionW);
float3 wV1 = normalize(pInputTri[2].mPositionW - pInputTri[0].mPositionW);
float wNormal = normalize(cross(wV0, wV1)); // Get the un-averaged normal for this triangle

if (dot(wToEye, wNormal) >= 0) // Facing towards the camera append to stream
{
VS_OUT wPSInput;

for (uint i = 0; i < 3; i++)
{
wPSInput.mPositionW = pInputTri[i].mPositionW;
wPSInput.mNormalW = pInputTri[i].mNormalW;
wPSInput.mPositionH = pInputTri[i].mPositionH;
wPSInput.mTextureCoords = pInputTri[i].mTextureCoords;
wPSInput.mTangentW = pInputTri[i].mTangentW;

pTriStream.Append(wPSInput);
}
}

pTriStream.RestartStrip();
}

[/code]

This does not work and I'm not sure why, probably something very simple but I can't see it. I could be standing right infront of the triangles face and it would still be culled off. Any help would be much appreciated, thanks.

Share this post


Link to post
Share on other sites
Maddius    146
Yeah it doesn't matter, same result either way it just essentially tells if the cameras behind or infront of the triangles 'plane'.

Share this post


Link to post
Share on other sites
Maddius    146
As anticipated, it was something very simple. I was calculating the model space normals as opposed to the world space normals. Here is the final code for anyone who would like to see.

[code]
[maxvertexcount(3)]
void GS(triangle VS_OUT pInputTri[3], inout TriangleStream<VS_OUT> pTriStream)
{
float3 wToEye = gEyePosition - pInputTri[0].mPositionW;
float3 wV0 = pInputTri[1].mPositionW - pInputTri[0].mPositionW;
float3 wV1 = pInputTri[2].mPositionW - pInputTri[0].mPositionW;
float3 wNormal = normalize(cross(wV0, wV1));

if (dot(wToEye, wNormal) >= 0.0f)
{
pTriStream.Append(pInputTri[0]);
pTriStream.Append(pInputTri[1]);
pTriStream.Append(pInputTri[2]);
}

pTriStream.RestartStrip();
}
[/code]

Thanks.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this