surface normals

Started by
2 comments, last by Chaucer 21 years, 1 month ago
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.
Thanks!
Advertisement
quote:
D3DXVec3Normalize((D3DXVECTOR3*)normal, (D3DXVECTOR3*)v);


I think you mean:
D3DXVec3Normalize(normal, normal);


emptyhead
Watch out for advice from the successfull, they don''t want company
:wq!
Thanks. That helps but now I''m only getting strips of lit terrain instead of nothing on the terrain being lit.
Thanks!
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;  

This topic is closed to new replies.

Advertisement