Help with rendering a 3D surface


I am physicist doing computational modeling. Over the years I have learned to code by “imitating” examples that people gave me or that I found online. I am reasonably confident using basic OpenGL, until it comes to drawing smooth surfaces, where I become quickly lost.

I have to display a 3D smooth surface for which I have 3D points describing it, arranged such that XY come in a NxM grid, and then Z is the height for each XY point.

I have been reading about polygons and face and vertex normals, but I am really at a loss as to what algorythms to use to compute vertex normals, and then how to produce a nice and smooth surface.

I have found a few examples online, but in all the normals are given, not computed. I need to compute them on the fly, as my surface changes continuously over the simulation.

Could anyone point to an example code I could again imitate?

Thanks infinitely

If I recall, the standard algorithm for computing a nice normal for a surface is to perform a weighted average the face normals for each of the faces connected to that vertex. The weight should be the area of the face.

Exactly right. Calculate the normals for each of the faces in your model first, then to get the normal for each vertex simply add up the x,y,z normals for each face that surrounds it and divide by the number of surrounding faces. This gives the ‘average’ (i.e. weighted) normal for your vertex.

So for three surrounding faces, add up normalX1+normalX2+normalX3 and divide by 3. This gives the average ‘x’ value for your vertex. And so on for your y and z values.

To calculate the normal for a face, you only need to use the first three vertices of that face:

// Calculate vectors
v1x = faceVertex1X - faceVertex2X;
v1y = faceVertex1Y - faceVertex2Y;
v1z = faceVertex1Z - faceVertex2Z;

	v2x = faceVertex2X - faceVertex3X;
	v2y = faceVertex2Y - faceVertex3Y;
	v2z = faceVertex2Z - faceVertex2Z;

// Get cross product of vectors
cx = (v1y * v2z) - (v1z * v2y);
cy = (v1z * v2x) - (v1x * v2z);
cz = (v1x * v2y) - (v1y * v2x);

// Normalise final vector
vLen = (float)sqrt((cx * cx) + (cy * cy) + (cz * cz));

	faceNormalX = (cx / vLen);
	faceNormalY = (cy / vLen);
	faceNormalZ = (cz / vLen);

Actually, that sparked a memory.

Don’t normalize the vector. Only normalize the sum of the face normals. Because the cross-product is proportional to the face area, you don’t have to compute the area manually.

OK, I didn’t realise that.

I usually normalise the face normals first, then normalise again after calculating the sum for each vertex. Still seems to work though.

Dear Opengl Experts,

Please correct me if I my understanding is wrong.

Ro calculate face normal of polygon, I will do the following
( 1 ) Calculate vector from the vertices for each edge of the ploygon
( 2 ) Calculate the crossproduct to compute normals
( 3 ) Repeat 1 and 2 for each edge of the polygon
( 4 ) Take average of the normals, This will obtain the face normal.

If I am wrong, Please correct me


(4) sum the normals
(5) normalize it.