Hello,
I''m having a problem getting my surface normals on my terrain to be correct. I''m creating my vertices like this.
2--3
0--1
in this order
0-1-2
2-1-3
I''m calculating surface normals like this:
void Terrain::FindNormals()
{
int count=0;
for(int j=0;j<GetNumVertices()/3;j++)
{
D3DXVECTOR3 vertex[3];
vertex[0] = m_pVertices[count+0].position;
vertex[1] = m_pVertices[count+2].position;
vertex[2] = m_pVertices[count+1].position;
CalculateNormal(vertex, &m_pVertices[count+0].normal);
count+=3;
}
}
here''s my calculate normal function:
void CalculateNormal(D3DVECTOR v[3], D3DVECTOR *normal)
{
D3DVECTOR a, b;
a.x = v[0].x - v[1].x;
a.y = v[0].y - v[1].y;
a.z = v[0].z - v[1].z;
b.x = v[0].x - v[2].x;
b.y = v[0].y - v[2].y;
b.z = v[0].z - v[2].z;
//find cross product to get the normal
normal->x = (a.y*b.z) - (a.z*b.y);
normal->y = (a.z*b.x) - (a.x*b.z);
normal->z = (a.x*b.y) - (a.y*b.x);
D3DXVec3Normalize((D3DXVECTOR3*)normal, (D3DXVECTOR3*)v);
}
Can anyone see where I''m not getting it right? Thanks.