#### Archived

This topic is now archived and is closed to further replies.

# surface normals

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

## Recommended Posts

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.

##### Share on other sites
Thanks. That helps but now I''m only getting strips of lit terrain instead of nothing on the terrain being lit.

##### Share on other sites
You''re only setting the normal for one in every three vertices...

  D3DXVECTOR3 vertex[3];vertex[0] = m_pVertices[count+0].position;vertex[1] = m_pVertices[count+2].position;vertex[2] = m_pVertices[count+1].position;D3DXVECTOR3 normal;CalculateNormal(vertex, &normal);m_pVertices[count+0].normal = normal;m_pVertices[count+1].normal = normal;m_pVertices[count+2].normal = normal;

1. 1
2. 2
Rutin
19
3. 3
4. 4
5. 5

• 14
• 12
• 9
• 12
• 37
• ### Forum Statistics

• Total Topics
631435
• Total Posts
3000056
×