this tutorial will work with borland kylix
It's time to add some sort of lighting to our scene to make things look a little nicer and clearer. Adding a light is just as easy as adding a Camera or Sphere. Open the GLScene Editor if it isn't already open, move to 'Scene objects' and select 'LightSource' from the menu that drops down when you click on the 'Add object' button. Like the camera before it, our light isn't positioned correctly so at the moment it won't be having any noticeable effect on our scene. We can move the light so that it is in a better place by first altering its Z coordinate. Anything from 1 to 3 here will be fine for this example. You should straight away see the lights effect on the sphere. Changing the light's X and Y coordinates will have the effect of moving the light to the left/right or up/down respectively. Play around with it until you get the light where you want it. When we move our object with the arrow keys shortly, we'll be leaving the lightsource in its current position, so you'll see the light move over the sphere as we press the keys.
But we're getting a head of ourselves here. We want to make our sphere a bit more interesting still by giving it a nice texture. This, like everything else so far, is very simple. Select Sphere1 from the GLScene Editor and open the list of options available with the Material property in the Object Inspector. Clicking the 'three-dots' button here will open a dialog box from which we can change the colour of our sphere, or select and apply a texture.
Click on the Texture button at the top and a list of options become available. The top one, Image, allows you to select an image from your hard drive to use as a texture. There are some example textures which you could use in the media directory in the demos directory of GLScene. If you decide to use a Jpeg texture instead of a bitmap, you should add the word Jpeg to the uses section of your code up at the top, or you won't be able to see the texture on your sphere when you execute your code. Once you've chosen your image, you'll need to untick the 'Disabled' box. At that point you'll be able to see your texture on a Cube on the right. The cube just gives you an idea of how your texture will look. You can change the example object to a sphere from the drop-down menu above the image to get a better idea for this example if you like.
All of the options found in this dialog box can be chosen from the Object Inspector if you prefer. There are additional options there too, so it's worth a look, particularly if you're having problems viewing the texture on the sphere from within the dialog box. If you want your lighting to still have an effect on the sphere, you'll want to set the 'Texture Mode' property to 'tmModulate'. We'll not go into any more detail here about textures as this is just an introduction. Most of the changes you make to the various properties will have an affect there and then on your sphere in the GLSceneViewer, so you can see what things do what without compiling any code.
Another thing we'll do is change the background colour of our scene to something a little nicer. We can do this by selecting the GLSceneViewer component, and opening up the Buffer properties in the Object Inspector. The 'BackgroundColor' property is funnily enough, what you're after, although us non-American types would probably spell it Colour ;) . There are lots of other properties here which you may not be able to resist trying. As before, any changes you make will most likely show up on your form, so have fun playing around with them.
So, we have a sphere, we've given it some lighting and a texture, and everything is looking just fine. Of course, now we want to be able to move our sphere around, and like everything else in this tutorial, it's not too hard to do. If you've done some of the general tutorials on this site, you'll be familiar with using the keyboard to control moving an image around. Getting a 3D object to move around is very similar, with the only addition being a third axis on which we can move things around. If you add an OnKeyDown procedure for our form, then the following code placed inside it will do everything you need to in a case like this.
procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
case Key of
37: Sphere1.Position.X := Sphere1.Position.X - 0.1;
39: Sphere1.Position.X := Sphere1.Position.X + 0.1;
38: Sphere1.Position.Y := Sphere1.Position.Y + 0.1;
40: Sphere1.Position.Y := Sphere1.Position.Y - 0.1;
107: Sphere1.Position.Z := Sphere1.Position.Z + 0.1;
109: Sphere1.Position.Z := Sphere1.Position.Z - 0.1;
This code just checks the Left (37) and Right (39) arrow keys and adjusts the X-coordinate accordingly. The same applies for the Up (38) and Down (40) arrow keys, which adjusts the Y-coordinate, and the + (107) and - (109) keys on the num pad. The property that we're using to change these coordinates is called Position. By specifying which object and which coordinate we want to change, we can move our object anywhere in the scene.
If you're having problems visualising the position of things in the scene, you may want to get your sphere to show its own axes. If you select your Sphere, the property you want is called 'ShowAxes', which when set to True, will give you an idea of which axis you're currently moving the sphere along.
That's it for this introductory tutorial. You can find many more well commented examples of how to use GLScene's many features in the Demos directory included with GLScene. If you head to glscene.org you'll also find details of their newsgroups where you can ask any questions you may have about using GLScene. Enjoy.