Skip to main content
GameDev.net gamedev.net
🔒 Locked

surface normals

Started by Chaucer Mar 6, 2003 at 5:14 PM 2 replies 350+ views
Original Post
Chaucer
Chaucer
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!
Chaucer
Chaucer
Thanks. That helps but now I''m only getting strips of lit terrain instead of nothing on the terrain being lit.
Thanks!
Osc
Osc
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;

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.