unit Auto1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Material, light, Transfrm, cOpenGL, OpenGL;

type
  TForm1 = class(TForm)
    OpenGL1: TOpenGL;
    Transform1: TTransform;
    Light1: TLight;
    Transform2: TTransform;
    Material1: TMaterial;
    Material2: TMaterial;
    procedure FormCreate(Sender: TObject);
    procedure FormPaint(Sender: TObject);
    procedure FormResize(Sender: TObject);
    procedure Transform1Paint(Sender: TObject);
    procedure Transform2Paint(Sender: TObject);
  private
    signX, 
    signY,
    signZ,
    deltaX,
    deltaY,
    deltaZ: GLfloat;
    Startms, 
    ms256,
    Counter256,
    lCounter: integer;
    bCounter: byte;
    procedure Idle(Sender:TObject;var Done:boolean);
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

uses Shapes, Count, MMSystem;

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
begin
  deltaX := 0.005;
  deltaY := 0.006;
  deltaZ := 0.007;
  signX := 1.0;
  signY := 1.0;
  signZ := -1.0;
  Application.OnIdle := Idle;
  CylinderBase;
  glLightModeli(GL_LIGHT_MODEL_TWO_SIDE,GL_TRUE);
  Startms := timeGetTime;
end;

procedure TForm1.FormResize(Sender: TObject);
begin
  if OpenGL1.Smaller then
    FormPaint(Sender);
end;

procedure TForm1.FormPaint(Sender: TObject);
begin
  glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);
  Transform1.Apply;
  Transform2.Apply;
  {glFlush}OpenGL1.Swap;
  OpenGL1.ErrorCheck('FormPaint');
end;

procedure TForm1.Transform1Paint(Sender: TObject);
begin
  Light1.Apply;
end;

procedure TForm1.Transform2Paint(Sender: TObject);
begin
  Material1.Apply;
  Material2.Apply;
  Cylinder;
end;

procedure TForm1.Idle(Sender:TObject;var Done:boolean);
var
  difms: integer;
  ratio: GLfloat;
begin
  inc(lCounter);
  inc(bCounter);
  difms := TimeGetTime-Startms; 
  with OpenGL1,Transform2 do
  begin
    if bCounter=0 then
    begin
      Form2.Caption := Format('%d %d',[lCounter*1000 div difms,
        (lCounter-Counter256)*1000 div (difms-ms256)]);
      Counter256 := lCounter;
      ms256 := difms;
      Form2.Show;
    end;

    if Angle<359.5 then
      Angle := Angle+0.5
    else
      Angle := 0;
    ratio := abs(tranZ/prxNear); 

    if (-TranZ<prxNear) then
      signZ := -1.0
    else
      if (-TranZ>prxFar) then
        signZ := 1.0;
    TranZ := TranZ+deltaZ*signZ;

    if (TranY<prxBottom*ratio) then
      signY := 1.0
    else
      if (TranY>prxTop*ratio) then
        signY := -1.0;
    TranY := TranY+deltaY*signY;

    if (TranX<prxLeft*ratio) then
      signX := 1.0
    else
      if (TranX>prxRight*ratio) then
        signX := -1.0;
    TranX := TranX+deltaX*signX;
      
  end; {with}
  Done := false;
  FormPaint(nil);
end;

end.
