Sign in to follow this  

Something wrong with my normlal calculation

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[i].x = 0.0;
		normal[i].y = 0.0;
		normal[i].z = 0.0;
	// sum all the normals for each vertex
	for(int i = 0; i < num_faces; ++i) {
		normal[face[i].v1].x += face[i].normal.x;
		normal[face[i].v1].y += face[i].normal.y;
		normal[face[i].v1].z += face[i].normal.z;
		normal[face[i].v2].x += face[i].normal.x;
		normal[face[i].v2].y += face[i].normal.y;
		normal[face[i].v2].z += face[i].normal.z;
		normal[face[i].v3].x += face[i].normal.x;
		normal[face[i].v3].y += face[i].normal.y;
		normal[face[i].v3].z += face[i].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 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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this