Jump to content
  • Advertisement
Sign in to follow this  

Something wrong with my normlal calculation

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

For some reason all of my normals for my terrain are coming up as nan when I output them. Not really sure why :( The first thing I do is store the faces for my terrain in terms of offsets in my vertex array. I get this information from the index array:
	int face_num = 0;
	for(int i = 0; i < (size - 1); ++i) {
		for(int j = 0; j < (size_x2 - 2); ++j) {
			face[face_num].v1 = index[(j + i * size_x2)];
			face[face_num].v2 = index[(j + i * size_x2) + 1];
			face[face_num].v3 = index[(j + i * size_x2) + 2];
			compute_normal(vertex[face[face_num].v1], vertex[face[face_num].v3], vertex[face[face_num].v3], face[face_num].normal);

Once I have the vertices for the face I pass those vertices to a compute_normal function that takes the normal for that face by ref and fills it in. Here is that code:
void terrain::compute_normal(const vector3 & p0, const vector3 & p1, const vector3 & p2, vector3 & n) {
	double a[3];
	double b[3];
	double length;

	a[0] = p0.x - p1.x;
	a[1] = p0.y - p1.y;
	a[2] = p0.z - p1.z;
	b[0] = p0.x - p2.x;
	b[1] = p0.y - p2.y;
	b[2] = p0.z - p2.z;			
	n.x = a[1] * b[2] - b[1] * a[2];
	n.y = b[0] * a[2] - a[0] * b[2];
	n.z = a[0] * b[1] - b[0] * a[1];
	length = sqrt(n.x * n.x + n.y * n.y + n.z * n.z);
	n.x /= length;
	n.y /= length;
	n.z /= length;

At this point I know something has already gone wrong because outputting the vector n in that compute normal functions returns nans but for the sake of completeness, here is how I then get the normals per vertex:
	// zero out the normal array
	for(int i = 0; i < num_vertices; ++i) {
		normal.x = 0.0;
		normal.y = 0.0;
		normal.z = 0.0;
	// sum all the normals for each vertex
	for(int i = 0; i < num_faces; ++i) {
		normal[face.v1].x += face.normal.x;
		normal[face.v1].y += face.normal.y;
		normal[face.v1].z += face.normal.z;
		normal[face.v2].x += face.normal.x;
		normal[face.v2].y += face.normal.y;
		normal[face.v2].z += face.normal.z;
		normal[face.v3].x += face.normal.x;
		normal[face.v3].y += face.normal.y;
		normal[face.v3].z += face.normal.z;
	// normalize the sum
	for(int i = 0; i < num_vertices; ++i) {

Does anyone see a problem in my compute_normals function that I am not seeing?

Share this post

Link to post
Share on other sites
Ok I see that I was passing the same vertex twice to my compute normal function. Changing that gives me good numbers now I think but theres a different problem...

The lighting on my terrain looks like absolute crap. You can find a pic at binweb.binghamton.edu/~nschwar1...look at test.bmp.

Does anyone know what might be wrong?

Share this post

Link to post
Share on other sites
As a slight update it appears as though some of the normals I have are the zero vector...

I know for a fact the faces are being calculating correctly and that every vector is being "hit". I sort of looks like sum of the normals are adding up to the zero vector.

Did I miss something really obvious? I even went ahead and compared my code to other code out there and it all looks good...

any help would be really appreciated.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!