unit mod1;

interface

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

type
  TForm1 = class(TForm)
    OpenGL1: TOpenGL;
    Transform1: TTransform;
    Transform2: TTransform;
    Light2: TLight;
    Material1: TMaterial;
    Material2: TMaterial;
    procedure FormCreate(Sender: TObject);
    procedure FormResize(Sender: TObject);
    procedure FormPaint(Sender: TObject);
    procedure Transform1Paint(Sender: TObject);
    procedure Transform2Paint(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

type
  TTexel3=packed record
    red,
    green,
    blue: GLubyte;
  end;
  TTexel4=packed record
    red,
    green,
    blue,
    alpha: GLubyte;
  end;
  
const
  wBrick=16;
  hBrick=8; 
  Level=0;
  Border=0;
  ColorComps=4;

type
  TBrickLine=array[0..wBrick-1] of TTexel4;
  TBrick=array[0..hBrick-1] of TBrickLine;

var
  Brick: TBrick;

procedure MakeBrick;
var
  sTex, {x}
  tTex: {y} integer;
begin
  Randomize;
  for tTex := 0 to hBrick-1 do
    for sTex := 0 to wBrick-1 do
    with Brick[tTex][sTex] do
    begin
      alpha := 255;
      if (tTex=hBrick-1) or (sTex=wBrick-1) then
      begin
        red := 220;
        green := 220;
        blue := 220;
      end else begin
        red := 150+Random(60);
        green := 60;
        blue := 0;
      end;
    end; {for with}
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  glPixelStorei(GL_UNPACK_ALIGNMENT,1);
  glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT);
  glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT);
  glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
  glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
  MakeBrick;
  glTexImage2D(GL_TEXTURE_2D,Level,ColorComps,wBrick,hBrick,Border,
    GL_RGBA,GL_UNSIGNED_BYTE,@Brick);
  glEnable(GL_TEXTURE_2D);
  glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_MODULATE);
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);
  Transform2.Apply;
  Transform1.Apply;
  glFlush;
  OpenGL1.ErrorCheck('FormPaint');
end;

procedure TForm1.Transform1Paint(Sender: TObject);
begin
  Material1.Apply;
  Material2.Apply;
  glBegin(GL_POLYGON);
    glTexCoord2f(0.0,00.0); glVertex3f(-1.0,-1.0,+0.0);
    glTexCoord2f(8.0,00.0); glVertex3f(+1.0,-1.0,+0.0);
    glTexCoord2f(8.0,16.0); glVertex3f(+1.0,+1.0,+0.0);
    glTexCoord2f(0.0,16.0); glVertex3f(-1.0,+1.0,+0.0);
  glEnd;
end;

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

end.
 
