I can see in your demo that the spheres are "lit" but the cube is not.
I suppose that you refer to the demo of reply #13... No, both are lit programmatically (when the "Light" checkbox is marked, but because lighting effects are calculated per surface element each face of the cube gets a uniform color.
I assume that the cube has no specified normals. So how do you create normals for quads or polygons? is there a way they are generated based on the creation of the quad/polygon?
Look at the cube generation code:
glBegin(GL_POLYGON);
glColor3f(1, 0, 0);
glNormal3f(0.0, 0.0, 1.0); // <--- this defines the normal
glVertex3f(1.0, 1.0, 1.0);
glVertex3f(-1.0, 1.0, 1.0);
glVertex3f(-1.0, -1.0, 1.0);
glVertex3f(1.0, -1.0, 1.0);
glEnd;
The normal is defined by the glNormal call. It is used for all vertices until the next glNormal is called. In the example the normal is valid for all four vertices; the face, therefore, appears flat. Normally you call glNormal before each vertex, then lighting calculation will interpolate between normals pointing into different directions.
In order to calculate a normal for a vertex you can calculate the cross product of the vectors to adjacent vertices and compute the average. In case of analytical functions you can also calculate the partial derivatives with respect to x and y; the normal vector is then (-dz/dx, -dz/dy, 1).
Note that normals should be normalized to unit length, otherwise their length may enter color calculation.
I am looking to show a surface in a single colour but with shading based on light direction.
As I said the surface must be divided into many patches, and normals must be given for each vertex, or at least for each patch (which will make the patches being rendered as flat). The lighting effects in OpenGL are determined by the properties of the light source and the material of the object. There are three classes of such properties: ambient (light coming from everywhere), diffuse (diffuse scattering), specular (specular scattering).
If you want to give a surface a uniform color (with shades varying according to lighting) the easiest way is to enable COLOR_MATERIAL mode, glEnable(GL_COLOR_MATERIAL). This way the surface keeps the color that you assign to the vertices, but the color shades vary depending on the lighting condition.
I modified an earlier program to show lighting effects and normal calculation - find it in the attachment. Please study the code and ask if you don't understand parts (sorry it's getting bigger and bigger).
I am planning the write a component for 3D charting with OpenGL. This should make it possible to create 3d plot without knowing details of OpenGL.
For refreshing my OpenGL knowledge I stumbled across the site
http://www.songho.ca/opengl/index.html which appears rather good to me. I ported some of his demo programs to Lazarus, unfortunately with additional files they are too large for an upload here.
Don't know how to properly insert a quote!
Select the post that you want to quote and click on the "Insert Quote" at the top/right of the post - this will copy the post into your message. You should delete the parts that are not needed.