Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hi,

 

I'm creating a 3D landscape using a heightmap. The landscape is working, but I'm trying to create normals for the vertexes. Could anyone verify that I'm doing this correctly? Or maybe point me in the right direction :)

 

 

Regards,

 

Jørn

 

 

 

for( int y = 0; y < _Height; y++ )
{
for( int x = 0; x < _Width; x++ )
       {
       	try
               {
               	Vector3 p0 = _Vertexes[x + y * _Width].Position;
				Vector3 p1 = _Vertexes[-1 + x + y * _Width].Position - p0;
               	Vector3 p2 = _Vertexes[-_Width + x + y * _Width].Position - p0;
               	Vector3 p3 = _Vertexes[1 + ( -_Width ) + x + y * _Width].Position - p0;
               	Vector3 p4 = _Vertexes[1 + x + y * _Width].Position - p0;
               	Vector3 p5 = _Vertexes[_Width + x + y * _Width].Position - p0;
               	Vector3 p6 = _Vertexes[-1 + _Width + x + y * _Width].Position - p0;

               	Vector3 n1 = Vector3.Cross( p2, p1 );
               	Vector3 n2 = Vector3.Cross( p3, p2 );
               	Vector3 n3 = Vector3.Cross( p4, p3 );
               	Vector3 n4 = Vector3.Cross( p5, p4 );
               	Vector3 n5 = Vector3.Cross( p6, p5 );
               	Vector3 n6 = Vector3.Cross( p1, p6 );

               	n1.Normalize();
               	n2.Normalize();
               	n3.Normalize();
               	n4.Normalize();
               	n5.Normalize();
               	n6.Normalize();

               	Vector3 normal =
               	    ( n1 + n2 + n3 + n4 + n5 + n6 );
               	normal.X /= 6f;
               	normal.Y /= 6f;
               	normal.Z /= 6f;
               	_Vertexes[x + y * _Width].Normal = normal;
	}
	catch
	{
		// Don't care about vertexes at the borders at this point...
	}
}
}

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...