Page 29/36
Runtime object creation
Until now we spoke only about design time functions and features of GLScene. As you will
learn more about GLScene you will find out that creating objects runtime is inevitable. Components
are created the usual Delphi way with Create directive. But scene objects are created differently.
Those objects have to be registered in the scene clearly saying where the objects position in the
scene hierarchy is.
If we want for example create a sphere named MyGLSphere as a child of GLCube1 the correct
code is like this:
var
MyGLSphere: TGLSphere
begin
MyGLSphere := TGLSphere(GLCube1.AddNewChild(TGLSphere));
end;
This way the new sphere will be placed correctly in the scene as a last child of GLCube1. If
you want to make it a first child of GLCube1 call AddNewChildFirst instead. Any object can be
moved in the scene hierarchy with MoveUp and MoveDown functions or by changing Parent
property. To destroy the object call MyGLSphere.Free function.
Creating new class
You will also find very useful to inherit your own classes from existing ones. This process has
same rules like any other Delphi object. Lets make a new class for our spinning cube.
type
TGLSpinningCube = class(TGLCube)
private
FSpinSpeed: single;
published
property SpinSpeed read FSpinSpeed write FSpinSpeed;
public
constructor Create(AOwner : TComponent); override;
destructor Destroy; override;
procedure DoProgress(const progressTime : TProgressTimes); override;
end;
constructor TGLSpinningCube.Create(AOwner : TComponent);
begin
inherited;
FSpinSpeed := 1000;
end;
destructor TGLSpinningCube.Destroy;
begin
inherited;
end;
procedure TGLSpinningCube.DoProgress(const progressTime : TProgressTimes);
begin
GLScene beginner's guide, Jan Zizka, 2005