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?

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:

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.

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.