|

OpenGL/ FPC - Chapter 13 - by Delax
So after all the theory, let's get some OpenGL going. We start off by looking at ambient light.
First off, we need some variables to store the values of our light. A light (as said before) is defined by 4 values, so we'll take an Array[0..3] of Real.
Now we'll do all the initialisation. Read the last chapter again, if you don't know what is happening here.
glShadeModel(GL_SMOOTH); // Set Shading Model
AmbientLight[0] := 1.0; AmbientLight[1] := 1.0;
AmbientLight[2] := 1.0; AmbientLight[3] := 1.0;
glEnable(GL_LIGHTING);
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, @AmbientLight);
glEnable(GL_COLOR_MATERIAL);
glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
First we set the shading model. That's the way the shadows will be calculated. We'll choose GL_SMOOTH.
Then we set the intensity of the ambient light. We do this by entering values from 0.0 to 1.0 in the array.
Now we activate the lighting feature of OpenGL. Right now, we only have an ambient light, so there is no need for setting other values. The lighting mode is set with glLightModelfv. We then tell OpenGL what kind of light we want to have and link to our array.
Next up is the material. We set it with glColorMaterial. It is the way our surfaces react to the light.
And basically that's it. Because we are dealing with ambient light we don't need to specify a position for our light. So we'll just draw a nice scene and rotate it.
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);
glBegin(GL_QUADS);
glColor3f( 0.0, 0.0, 0.5 );
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();
glTranslatef( 0.0, 0.0, 3.0);
glColor3f( 0.7, 0.0, 0.0 );
glBegin(GL_QUADS);
glVertex3f( 3.0,-3.0, 3.0);
glVertex3f( 3.0, 3.0, 3.0); // oben
glVertex3f(-3.0, 3.0, 3.0);
glVertex3f(-3.0,-3.0, 3.0);
glVertex3f( 3.0, 3.0,-3.0);
glVertex3f( 3.0, 3.0, 3.0); // rechts
glVertex3f( 3.0,-3.0, 3.0);
glVertex3f( 3.0,-3.0,-3.0);
glVertex3f(-3.0, 3.0, 3.0);
glVertex3f(-3.0, 3.0,-3.0); // links
glVertex3f(-3.0,-3.0,-3.0);
glVertex3f(-3.0,-3.0, 3.0);
glVertex3f( 3.0, 3.0, 3.0);
glVertex3f( 3.0, 3.0,-3.0); // hinten
glVertex3f(-3.0, 3.0,-3.0);
glVertex3f(-3.0, 3.0, 3.0);
glVertex3f( 3.0,-3.0,-3.0);
glVertex3f( 3.0,-3.0, 3.0); // vorne
glVertex3f(-3.0,-3.0, 3.0);
glVertex3f(-3.0,-3.0,-3.0);
glEnd();
end;
Granted, this is not much of an example, but it's only ambient light anyway. Get the source code here and play around with it.
Delax/ Sundancer Inc.
[delax@sundancerinc.de]
Back to previous page
|