|

OpenGL/ FPC - Chapter 14 - by Delax
As we use diffuse light now, you really should understand the theory presented in chapter 12. If not, print it out and take a look at it whenever you run into a problem.
We created an array for out ambient light. Now we need 2 new arrays. One for the values of the diffuse light and one for the position. Instead of creating the array in the OpenGL_Init procedure, we'll define them as globals.
AmbientLight : array[0..3] of glFloat = (0.3,0.3,0.3,1.0);
DiffuseLight : array[0..3] of glFloat = (1.0,1.0,1.0,1.0);
LightPosition : array[0..3] of glFloat = (0.0,0.0,0.0,1.0);
Also, our OpenGL_Init changes in several ways. Here are the new definitions.
glEnable(GL_LIGHTING); // Enable Lighting
glLightfv(GL_LIGHT0,GL_AMBIENT, @AmbientLight);
glLightfv(GL_LIGHT0,GL_DIFFUSE, @DiffuseLight);
glEnable(GL_LIGHT0);
glLightfv(GL_LIGHT0,GL_POSITION,@LightPosition);
This time we define a light with the id "LIGHT0". First we set the ambient part of the light with glLightfv(GL_LIGHT0,GL_AMBIENT, @AmbientLight);. Then we set the diffuse part the same way and activate the light. After that we set the position of it.
Now following is the Draw_OpenGL procedure.
PROCEDURE Draw_OpenGL;
begin
glClear(GL_COLOR_BUFFER_BIT OR GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef( 0.0,-5.0,-50.0);
glRotatef( 80,-1.0, 0.0, 0.0);
glRotatef( rotation, 0.0, 0.0, 1.0);
LightPosition[0] := 3.0; LightPosition[1] := 5.0;
LightPosition[2] := 5.0; LightPosition[3] := 0.5;
glLightfv(GL_LIGHT0,GL_POSITION,@LightPosition);
glBegin(GL_QUADS);
glColor3f( 0.0, 0.0, 0.5 );
glNormal3f(0.0, 0.0, 1.0);
glVertex3f( 10.0,-10.0, 0.0);
glVertex3f( 10.0, 10.0, 0.0);
glVertex3f(-10.0, 10.0, 0.0);
glVertex3f(-10.0,-10.0, 0.0);
glEnd();
So we set the position of the light behind the quad. Then we tell OpenGL to use this new position.
As you can see the position of a light has 4 values. The first 3 values are the X/ Y/ Z values. The last one is the distance of the light. 0.0 is for "infinite distance" so the light reaches your surfaces parallel. 1.0 is for "right at this point" and the light is a sphere around the given point.
From now on we need a normal for every surface we draw!
Get the source code of the example here. We also draw a little marker representing our light. Play around a little. Let the light rotate around the scene and look how it behaves. Change the lighting values. Just goof around and make yourself familiar.
Delax/ Sundancer Inc.
[delax@sundancerinc.de]
Back to previous page
|