Jump to content
  • Advertisement
Sign in to follow this  
pnt1614

Backface culling in geometry shader?

This topic is 1222 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I want to do backface culling in the geometry shader. First I transform vertices into view space in the vertex shader, then in the geometry shader I find face normal and check the z component of dot product between face normal and (0, 0, -1) for backface culling. But the result is weird.

 

error.jpg

 

In the VS:

struct VS_OUTPUT
{
	float4 Position     : POSITION0; // vertex position
	float4 PosWV		: POSITION1;
	float2 UV			: TEXCOORD0;
};

VS_OUTPUT RenderSceneVS( VS_INPUT input )
{
	VS_OUTPUT Output;
	
	Output.Position = mul( float4(input.Position, 1.0f), g_mWorldViewProjection );	
	Output.PosWV = mul(float4(input.Position, 1.0f), g_mWorldView);
	Output.UV = input.TextureUV;
	
	return Output;    
}

In the GS:

        float3 Pos_WV[3];

	GS_OUTPUT points[3];
	for (int i=0; i<3; i++)
	{
		points[i].Position = input[i].Position;
		points[i].UV = input[i].UV;
		
		PosWV[i] = input[i].PosWV;
	}

	
	float3 v0v1 = PosWV[1] - PosWV[0]; // position in the view space
	float3 v0v2 = PosWV[2] - PosWV[0];
	float3 normal = normalize(cross(v0v1, v0v2));

	if (dot(normal, float3(0, 0, -1)) > 0)
	{
		Output.Append(points[0]);
		Output.Append(points[1]);
		Output.Append(points[2]);
		Output.RestartStrip();
	}	
	else
	{
		Output.Append(points[0]);
		Output.Append(points[1]);
		Output.Append(points[2]);
		Output.RestartStrip();
	}

C++:

        D3DXMATRIX mView = *g_MyCamera.GetViewMatrix();
	D3DXMATRIX mProj = *g_MyCamera.GetProjMatrix();	
	D3DXMATRIX mWorldView = mWorld * mView;
	D3DXMATRIX mWorldViewProjection = mWorld * mViewProjection;

	//-------------------------Set the constant buffers--------------------------
	HRESULT hr;
	D3D11_MAPPED_SUBRESOURCE MappedResource;
	V( pd3dImmediateContext->Map( g_pcbVSPerObject11, 0, D3D11_MAP_WRITE_DISCARD, 0, &MappedResource ) );
	CB_VS_PER_OBJECT* pVSPerObject = ( CB_VS_PER_OBJECT* )MappedResource.pData;

	D3DXMatrixTranspose(&pVSPerObject->m_mWorldView, &mWorldView);
	D3DXMatrixTranspose( &pVSPerObject->m_mWorldViewProjection, &mWorldViewProjection );

	pd3dImmediateContext->Unmap( g_pcbVSPerObject11, 0 );
	pd3dImmediateContext->VSSetConstantBuffers( 0, 1, &g_pcbVSPerObject11 );

I have checked many times but I cannot find the reason why it does not work? Is there something wrong?

Edited by steven166

Share this post


Link to post
Share on other sites
Advertisement

Show your vertex-shader (paste exactly your complete shader-files without any omissions).

Probably easier to do a cross-product on the tri-vertices to get the actual face-normal in the GS, unless you measure a performance impact with it.

 

Why are you doing this?

(I'm guessing you want to cull with respect to a different view than the actual screen view .. ?)

Share this post


Link to post
Share on other sites

Thanks for the reply, Kalle_h, but I do not completely understand your answer. You mean that I need to transform into homogeneous space and find the face normal, don't you?

 

To Erik Rufelt: I added the VS in  the first post. Backface culling remove all invisible polygons which improves the performance. I just want to improve perfomance of my program.

Share this post


Link to post
Share on other sites

Thanks Kalle_h again, I did it.

 

@phantom: I need to manipulate vertices in the geometry, therefore I must do backface culling manually :(

Edited by steven166

Share this post


Link to post
Share on other sites
What are you trying to do which makes you think you need to use the GS?
Generally there is likely to be a way of doing it which will perform better.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!