All, I’m trying to add a single directional light source to my scene, the idea is that this will mimic the Sun and illuminate selected objects to give them some concept of depth and 3D context. My problem is that when the scene is rendered the output is not what I expect. It’s tough to describe so follow this URL for a screen shot (small jpeg) http://www.thecompass.co.uk/light1.jpg I’ve put two sections of code below, the first is how I’ve set up my lighting, the second is how each face (triangle) that makes up the 3D model is drawn.
The end effect I want to achieve is really just to light the model, in balance with other non-lit objects in my scene hence the background ambient light, and pick-out or highlight parts of the model to give a good perception of 3D. I’m not sure about the positioning of my light source “GL_LIGHT0” and this may well be the problem, truth is I’m not really sure where to put it or how to position to achieve my desired result ?
Thanks in advance for any help/pointers.
Ewan
= set up lighting ==============================================
GLfloat background_light[] = {0.43, 0.43, 0.43, 1.0};
GLfloat ambient_light[] = {0.2, 0.2, 0.2, 1.0};
GLfloat diffuse_light[] = {1.0, 1.0, 1.0, 1.0};
GLfloat specular_light[] = {1.0, 1.0, 1.0, 1.0};
/* directional light pointing down negative Z ? */
GLfloat light_position[] = {0.0, 0.0, 1.0, 0.0};
GLfloat shininess_material[] = {50.0};
GLfloat specular_material[] = {1.0, 1.0, 1.0, 1.0};
/* this is just a default value, this material property is
- overwritten when the polygon model is generated, see below */
GLfloat diffuse_material[] = {1.0, 1.0, 1.0, 1.0};
/* only do this once */
if (lighting() && !LightingGlSet)
{
glMaterialfv(GL_FRONT, GL_SHININESS, shininess_material);
glMaterialfv(GL_FRONT, GL_SPECULAR, specular_material);
glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, diffuse_material);
/* use some background ambient light */
glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_FALSE);
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, background_light);
glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);
glEnable(GL_LIGHT0);
glLightfv(GL_LIGHT0, GL_AMBIENT, ambient_light);
glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse_light);
glLightfv(GL_LIGHT0, GL_SPECULAR, specular_light);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
LightingGlSet = TRUE;
}
================================================================
In my scene only some models require lighting effects to be applied
so GL_LIGHTING is turned off and on as required.
= draw each face of the model ==================================
glEnable(GL_LIGHTING);
/* now for each triangle in my model, draw vertices and the normal */
glBegin(GL_POLYGON);
glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE,
my_material_colour);
glVertex3f(x1, y1, z1);
glVertex3f(x2, y2, z2);
glVertex3f(x3, y3, z3);
nx = (x2-x1)*(x2-x3);
ny = (y2-y1)*(y2-y3);
nz = (z2-z1)*(z2-z3);
nl = sqrt((nx*nx)+(ny*ny)+(nz*nz));
nx = (nl>0.0) ? (nx/nl) : nx;
ny = (nl>0.0) ? (ny/nl) : ny;
nz = (nl>0.0) ? (nz/nl) : nz;
glNormal3f(nx,ny,nz);
glEnd();
/* when all faces processed turn of lighting */
glDisable(GL_LIGHTING);
================================================================
[This message has been edited by Ewan (edited 12-01-2000).]
[This message has been edited by Ewan (edited 12-01-2000).]