Help with deferred shading

Started by
2 comments, last by Chod2906 10 years, 7 months ago

I'm looking for a bit of help getting deferred rendering using Direct3D9 and HLSL. I am using a custom rendering engine and using the following code as a guide to help me get it working: http://www.codesampler.com/usersrc/usersrc_7.htm#dx9u_deferred_shading

It seems to be nearly right as the lights are displaying where they should be, except the scene seems to be a strange mix or rgb values rather than any defined colour.

Being rendered in the corner are the following render targets (in order):

diffuse (A8R8G8B8)

normals (X8R8G8B8)

position (R16G16F)

depth (R16G16F)

I will post the shader code just in-case that helps

Interestingly there are a few lines in the shader I had to modify to get anything to render at all, which were:


PS_OUTPUT_BUILD psBuild(VS_OUTPUT_BUILD i)// : COLOR0

Here I had to remove the COLOR0 semantic otherwise it would throw a warning.


color.rgb = vDiffuseIntensity * c_vLightDiffuse.xyz /** vDiffuseMaterial*/	+ 
				vSpecularIntensity * c_vLightSpecular.xyz /** vSpecularMaterial*/;

Also I've had to remove the diffuse and specular material components here.

Any ideas on this would be really appreciated.

Here's an image of the scene being rendered, for reference: http://i.imgur.com/Fhm08bv.jpg

My Blog: http://chod-is.blogspot.com
Current Project: http://square-space.net
Advertisement

EDIT: Well I solved my first problem, and apparently that was caused by not mapping a texture to anything so the diffuse channel was empty.

The current output is this: http://i.imgur.com/g8kyNiH.jpg

Just need to solve this funny red/green problem then i'm good. The background should be clearing to pure black but it seems like what red/green colour is bleeding into the lighting calculation somehow.

Thanks again for any help

EDITEDIT: Well I fixed the problem, turns out there was a line in the shader code that had no place existing whatsoever.... All fixed and looking nice now: http://i.imgur.com/nYigx0U.jpg

The line of code was this: color.rgb += i.vTex0.rgr * 0.5; right at the end of the end of the lightpass pixel shader.

My Blog: http://chod-is.blogspot.com
Current Project: http://square-space.net

Sorry to bump this again but I'm still having problems with getting lighting to work 100%.

I'll start with the images..

http://i.imgur.com/NfltQJf.jpg

http://i.imgur.com/pAjYful.jpg

The first image is of a scene lit by regular old d3d lighting. The second is of the same scene, but lit with deferred shading.

The problem I'm having is that in some places the faces just aren't being lit properly. I know it's not my normals because both in both images the normals are drawing perfectly and in both images the lighting is incorrect (except in different places).

There are faces appearing unlit right infront of a light source, or where an adjacent face is lit. Take the floor for example in the first picture, the floor is completely unlit, but the step up from it is completely lit. This doesn't make any sense to me. And in the second picture, the wall where the purple light is totally unlit even though the light is right infront of it.

I can't work out the problem and it's been driving me mad so I had to resort to asking for help, so if anybody can shed some light that'd be great.

Thanks

EDIT: After debugging with PIX I can see that my normals are infact wrong. The problem is that they're being exported correctly and are correct for the vertices, but they're getting assigned to the wrong vertex somehow...

Here's my mesh loading code, if anybody can see the problem PLEASE tell me!


////////////////////////////////////////////////////////////////////////////////
// CWorldNode::LoadMesh
//! Create geometry from an imported mesh
//! 
//! \param pNode - 
//! \return void - 
////////////////////////////////////////////////////////////////////////////////
void CWorldNode::LoadMesh( CImportMesh* pNode )
{
	// read the number of vertices and faces
	pNode->num_verts		= Read<unsigned int>();
	pNode->num_faces		= Read<unsigned int>();
	pNode->num_texcoords	= Read<unsigned int>();

	// now allocate the new data
	pNode->verts = new CVertex[ pNode->num_verts ];
	pNode->faces = new CFace[ pNode->num_faces ];
	if( pNode->num_texcoords > 0 )
		pNode->texcoords = new CTexCoord[pNode->num_texcoords];

	// now create the vertex buffer
	pNode->vbuffer.Create( sizeof(Vertex_s)*pNode->num_verts, D3DFVF_XYZ|D3DFVF_NORMAL|D3DFVF_TEX1 );
	pNode->ibuffer.Create( sizeof(int)*pNode->num_faces*3 );

	pNode->vertarray = new Vertex_s[pNode->num_verts];
	pNode->indexarray = new int[pNode->num_faces*3];
	int iIndexCount = 0;

	// now read in all the faces one by one
	for( unsigned int v = 0; v < pNode->num_verts; ++v )
	{
		memcpy( &pNode->verts[v].v, ReadBytes( sizeof( pNode->verts[v].v ) ), sizeof( pNode->verts[v].v ) );
		pNode->vertarray[v].ve = pNode->verts[v].v;
	}

	for( unsigned int f = 0; f < pNode->num_faces; ++f )
	{
		memcpy( &pNode->faces[f], ReadBytes( sizeof( pNode->faces[f] ) ), sizeof( pNode->faces[f] ) );
	}

	for( unsigned int t = 0; t < pNode->num_texcoords; ++t )
	{
		memcpy( &pNode->texcoords[t], ReadBytes( sizeof( pNode->texcoords[t] ) ), sizeof( pNode->texcoords[t] ) );
	}

	// now build all the face data
	for( unsigned int f = 0; f < pNode->num_faces; ++f )
	{
		for( int j = 0; j < 3; ++j )
		{
			// index into the vertex array
			unsigned int index = pNode->faces[f].v[j];

			// index into the UV array
			unsigned int uvindex = pNode->faces[f].uvmap[j];

			// get the normal for the
			pNode->vertarray[index].no = pNode->faces[f].normal;
			if( pNode->texcoords )
				pNode->vertarray[index].uv = pNode->texcoords[uvindex];

			pNode->indexarray[iIndexCount++] = index;
		}
	}

	pNode->vbuffer.SetData( sizeof(Vertex_s)*pNode->num_verts, pNode->vertarray );
	pNode->ibuffer.SetData( sizeof(int)*pNode->num_faces*3, pNode->indexarray );		
}
My Blog: http://chod-is.blogspot.com
Current Project: http://square-space.net

EDIT EDIT: Nevermind it was my mesh loading code that was incorrect. I got confused with indices....

All fixed now and looking glorious!

Turns out I fixed my UV mapping at the same time!

AAnISX3.jpg

My Blog: http://chod-is.blogspot.com
Current Project: http://square-space.net

This topic is closed to new replies.

Advertisement