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 );
}