unit Fog1;

interface

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

type
  TForm1 = class(TForm)
    OpenGL1: TOpenGL;
    Transform1: TTransform;
    Transform2: TTransform;
    Transform3: TTransform;
    Transform4: TTransform;
    Transform5: TTransform;
    Light1: TLight;
    Material1: TMaterial;
    Material2: TMaterial;
    Material3: TMaterial;
    Material4: TMaterial;
    procedure FormCreate(Sender: TObject);
    procedure FormResize(Sender: TObject);
    procedure FormPaint(Sender: TObject);
    procedure FormKeyPress(Sender: TObject; var Key: Char);
    procedure Transform1Paint(Sender: TObject);
    procedure Transform2Paint(Sender: TObject);
    procedure Transform3Paint(Sender: TObject);
    procedure Transform4Paint(Sender: TObject);
    procedure Transform5Paint(Sender: TObject);
  private
    fogStart,
    fogEnd: GLfloat;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

uses Shapes;

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
var
  FogColor: packed record
    red,
    green,
    blue,
    alpha: GLfloat;
  end;
begin
  CylinderBase;
  glEnable(GL_FOG);
  glFogi(GL_FOG_MODE,GL_EXP2);
  with FogColor do
  begin
    red := 0.5;
    green := 0.5;
    blue := 0.5;
    alpha := 1.0;
  end;
  glFogfv(GL_FOG_COLOR,@FogColor);
  glFogf(GL_FOG_DENSITY,0.3);
end;

procedure TForm1.FormResize(Sender: TObject);
begin
{}
end;

procedure TForm1.FormPaint(Sender: TObject);
begin
  glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);
  Transform1.Apply;
  Transform2.Apply;
  Transform3.Apply;
  Transform4.Apply;
  Transform5.Apply;
  OpenGL1.Swap;
  OpenGL1.ErrorCheck('FormPaint');
end;

procedure TForm1.FormKeyPress(Sender: TObject; var Key: Char);
var
  density: GLfloat;
begin
  case Key of
    '0'..'9': begin
      fogStart := ord(key)-48;
      glFogf(GL_FOG_START,fogStart);
    end;
    'A'..'Z': begin
      density := (ord(Key)-65)/25.0;
      glFogf(GL_FOG_DENSITY,density);
    end;
    'a': glDisable(GL_FOG);
    'b': begin
      glEnable(GL_FOG);
      glFogi(GL_FOG_MODE,GL_EXP);
    end;
    'c': begin
      glEnable(GL_FOG);
      glFogi(GL_FOG_MODE,GL_EXP2);
    end;
    'd': begin
      glEnable(GL_FOG);
      glFogi(GL_FOG_MODE,GL_LINEAR);
    end;
    'e'..'z': begin
      fogEnd := ord(Key)-ord('d')+fogStart;
      glFogf(GL_FOG_END,fogEnd);
    end;
  end; {case}
  FormPaint(nil);
end;

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

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

procedure TForm1.Transform3Paint(Sender: TObject);
begin
  Material2.Apply;
  Cube;
end;

procedure TForm1.Transform4Paint(Sender: TObject);
begin
  Material3.Apply;
  Cone;
end;

procedure TForm1.Transform5Paint(Sender: TObject);
begin
  Material4.Apply;
  glBegin(GL_POLYGON);
    glVertex3f(-1.0,-1.0,-2.0);
    glVertex3f(+1.0,-1.0,-2.0);
    glVertex3f(+1.0,-1.0,-7.0);
    glVertex3f(-1.0,-1.0,-7.0);
  glEnd;
end;

end.
