components used: GLScene GLSceneViewer

this tutorial will work with borland kylix

Part 1Part 2 download source code

The purpose of this tutorial is to introduce you to GLScene and hopefully give you a glimpse of what can be achieved with it when you put your mind to it. GLScene will have you up and running using OpenGL to create 3D (or 2D if you prefer) games in no time.

When you first come across GLScene it may not seem obvious how to begin. There's lots of example code which demonstrates to you how much you can do, but when you start a project from scratch you may be puzzled as to why you can't quite get things to work. So, what we'll do is go through how to get things going by adding a sphere and taking control of it with the keyboard keys.

Assuming you've installed GLScene, move to the GLScene tab of the Component Palette and add the component called (see if you can spot the pattern here) GLScene. This component is the hub of everything you do and keeps control of all the other GLScene components you add, so it should always be the first one you place on your form.

Next add a GLSceneViewer component which will create the canvas on which our 3D scenes will be seen. You can drag this out to be as big or little as you want, but bare in mind that if you don't drag it out to be bigger than it defaults to, things will be rather small.

We'll come back to this component in a minute, but for now let's go back and double-click on the GLScene component that we just added to the form. A dialog box should pop up showing you what is called the 'GLScene Editor'. Within this editor you'll see two tree entries, 'Cameras' and 'Scene Objects'. Any cameras we use will be created in the first entry and any objects within our scene will be created in the second entry, hence their names.

The camera is what we'll be viewing our scene through so we'd better create one of them first. If you click on the 'Cameras' entry you should notice that the 'Add camera' button along the top of the dialog box has been enabled. Click on it and you'll see that our first camera has been created, named GLCamera1.

We'll need to add an object to our scene next, so this time click on the 'Scene Objects' entry and you'll see that the 'Add object' button beside the camera one has been enabled now. Clicking this button opens a drop-down menu. Move to the 'Basic geometry' entry and select 'Sphere'. A new Sphere object called Sphere1 will now appear on the list. The Editor dialog should now look similar to that shown below. Close it for the moment and we'll continue.

At the moment we still can't see anything on our GLSceneViewer canvas. The reason for this is that we haven't yet told it what camera to look through. Click on the GLSceneViewer's canvas to select it and move to its Camera property in the Object Inspector. Now select the GLCamera1 from the drop-down menu. If you look at the GLSceneViewer's canvas now you should probably not see any difference and you could be forgiven for thinking that I'm wasting your time. I'm not wasting your time though (you'll be glad to hear), because there's a perfectly valid reason for you still not seeing anything.

The reason is that our camera is not positioned correctly. To move it, open the GLScene editor again as before and select the GLCamera1 we created earlier. You'll see its properties in the Object Inspector. The property we're interested in just now is the 'Position' property, and more specifically, the Z coordinate. At the moment it will be set at 0. This means our camera is positioned pretty much exactly where our Sphere is. We can't see the Sphere because it is behind the camera. Let's move it back a little from the Sphere by setting its Z property at 8. You should finally see a result, a black sphere which admittedly doesn't look very 3D just now.

In the next part we'll set about adding a light to our scene so that we can actually see our Sphere a bit better (rather than as a black blob), we'll give it a texture, and we'll also take control of its position in our scene with the keyboard.

Part 1Part 2 download source code

Subscribe to the feed NEWS Feed
archived news | mailing list

[ all tutorials found at are Copyright © 2008 Ben Watt ]