GLScene - the first component

This page indroduces into building your own component. It takes a while to dig through the source, may this page save you some time. In order to keep it simple, a simple component was choosen : TGLTetra, a tetraeder with 4 colored sides.

the reference body - TCube

TCube is found in the GLObjects unit. Definition : line 275++, Implementation : line 1457++.
The important lines are :
 TCube=class(TGLSceneObject)

 public
  constructor create(AOwner:TComponent); override;
  procedure BuildList; override;
 end;
Some of the other stuff is used for streaming.
TGLTetra is built equally.

Notice the USES ..,OpenGL12, Geometry,..

OpenGL12 defines simple stuff such as TGLFloat, and Geometry defines TVector3f and so.

make it visible

In a first step we'll be satisfied by seeing the tetra at runtime. Get a form, drop GLScene and GLSceneViewer onto.
 GLSceneViewer.align:=albottom;
 GLSceneViewer.top:=100;                   // leave space for other controls on top
 GLSceneViewer.height:=ClientHeight-105;

 Doubleclick GLScene, add a camera (Camera1)

 Camera1.position:=(0,0,10);  // move it a bit further away
 Camera1.direction:=(0,0,-1);

 GLSceneViewer.camera:=Camera1;
Our component requires a dummycube to become child
 Doubleclick GLScene, add a dummycube to the sceneobjects (dummycube1)

 add to TForm :

 public
  MyTetra:TGLTetra;
 end;


 drop a button : name='CreateTetra'
 
 procedure TForm1.CreateTetraClick(..)
 begin
  MyTetra:=TGLTetra(DummyCube1.AddNewChild(TGLTetra));
  MyTetra.showaxes:=true;
  MyTetra.Transformationmode:=tmParentNoPos;
 end;
Showing the axes tells you there is something, even there is an error, such as the size of the object being zero, the object being oriented away and culled, and so on.

Some debugging features

 Drop a TTrackbar, name='focal', max;=500, min;=50, frequency;=50
   procedure tform1.focalchange(..)
   begin
    camera1.focallength:=focal.position;
   end;

 Drop 6 buttons for rotations :

  name='px' onclick : mytetra.pitchangle:=mytetra.pitchangle+10;
  name='nx' onclick : mytetra.pitchangle:=mytetra.pitchangle-10;
  name='py' onclick : mytetra.turnangle:=mytetra.turnangle+10;
  name='ny' onclick : mytetra.turnangle:=mytetra.turnangle-10;
  name='pz' onclick : mytetra.rollangle:=mytetra.rollangle+10;
  name='nz' onclick : mytetra.rollangle:=mytetra.rollangle-10;
The focal trackbar allows zooming out and in, should the object be not at zero. The rotation buttons let you rotate the object.