Example component - TGLTetra

see the page first component for the explanations.

unit GLTetra;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  OpenGL12,Geometry, GLMisc, GLScene;

type
  TGLTetra = class(TGLSceneObject)
  private
    { Private declarations }
   p1,p2,p3,p4:TVector3f;
   flength:TGLFloat;
  protected
    { Protected declarations }
   procedure setlength(q:single);
  public
    { Public declarations }
   constructor create(AOwner:TComponent); override;
   procedure buildlist; override;
   procedure Assign(Source: TPersistent); override;
  published
    { Published declarations }
   property length:TGLFloat read flength write setlength;
  end;

procedure Register;

implementation

constructor TGLTetra.create(AOwner:TComponent);
begin
 inherited create(AOwner);
 length:=1;
end;

procedure TGLTetra.setlength(q:single);
begin
 flength:=q;
 p1[0]:=0;   p1[1]:=0; p1[2]:=0;                 //(0,0,0)
 p2[0]:=q;   p2[1]:=0; p2[2]:=0;                 //(q,0,0)
 p3[0]:=q/2; p3[1]:=q*sqrt(3)/2; p3[2]:=0;       //(q/2,q*sqrt(3)/2,0)
 p4[0]:=q/2; p4[1]:=q*sqrt(3)/6; p4[2]:=q*0.81649;  // top point
 StructureChanged;
end;  

// whatever state changes were made have to be reversed upon exit
// meaning push/pop and so on.
procedure TGLTetra.buildlist;
begin
 inherited buildlist;
 //gldisable(GL_CULL_FACE);
 //glshademodel(GL_FLAT);
 glbegin(GL_Triangles); // counterclockwise
  glColor3f(1.0,0.0,0.0);
  glVertex3f(p1[0],p1[1],p1[2]);
  glVertex3f(p2[0],p2[1],p2[2]);
  glVertex3f(p4[0],p4[1],p4[2]); 
 glend;
 glbegin(GL_Triangles); // counterclockwise
  glColor3f(0.0,1.0,0.0);
  glVertex3f(p2[0],p2[1],p2[2]);
  glVertex3f(p3[0],p3[1],p3[2]);
  glVertex3f(p4[0],p4[1],p4[2]);
 glend;  
 glbegin(GL_Triangles); // counterclockwise
  glColor3f(0.0,0.0,1.0);
  glVertex3f(p3[0],p3[1],p3[2]);
  glVertex3f(p1[0],p1[1],p1[2]);
  glVertex3f(p4[0],p4[1],p4[2]);
 glend;    
 glbegin(GL_Triangles); // counterclockwise
  glColor3f(0.4,0.4,0.4);
  glVertex3f(p1[0],p1[1],p1[2]);
  glVertex3f(p3[0],p3[1],p3[2]);
  glVertex3f(p2[0],p2[1],p2[2]);  
 glend; 
end;

procedure TGLTetra.Assign(Source: TPersistent);  
begin
  if assigned(Source) and (Source is TGLTetra) then
  begin
   flength:=TGLTetra(Source).flength;
   p1:=TGLTetra(Source).p1;
   p2:=TGLTetra(Source).p2;
   p3:=TGLTetra(Source).p3;
   p4:=TGLTetra(Source).p4;
  end;
  inherited Assign(Source);
end;

procedure Register;
begin
  RegisterComponents('GLScene', [TGLTetra]);
end;

end.