// =============================================================================
//   OpenGL1.5 - VCL Template (opengl15_vcl_template.zip)
// =============================================================================
//   Copyright © 2003 by DGL - http://www.delphigl.com
// =============================================================================
//   Contents of this file are subject to the GNU Public License (GPL) which can
//   be obtained here : http://opensource.org/licenses/gpl-license.php
// =============================================================================
//   History :
//    Version 1.0 - Initial Release                            (Sascha Willems)
// =============================================================================

unit OpenGL15_MainForm;

interface

uses
  Windows,
  Messages,
  SysUtils,
  Classes,
  Graphics,
  Controls,
  Forms,
  Dialogs,

  dglOpenGL,
  settings,
  keys,
  shader,
  lights,
  textures,
  StdCtrls, ExtCtrls;

type
  TGLForm = class(TForm)
    Panel4: TPanel;
    Panel5: TPanel;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure ApplicationEventsIdle(Sender: TObject; var Done: Boolean);
    procedure FormKeyPress(Sender: TObject; var Key: Char);
  private
    { Private-Deklarationen }
  public
    RC        : HGLRC;
    DC        : HDC;
    ShowFPS   : Boolean;
    FontBase  : GLUInt;
    StartTick : Cardinal;
    Frames    : Integer;
    FPS       : Single;
  end;

var
  GLForm: TGLForm;
  SPEEDVARIABLE: Extended;

  shader: GLHandleARB;
  three_colors, beton, sand, wall: GLuint;

implementation

{$R *.dfm}

procedure TGLForm.FormCreate(Sender: TObject);
begin
 SettingsOnInit;

 shader:= LoadFragmentandVertexShader('fs.txt', 'vs.txt');
 LoadTexture('wall.jpg', wall);
 LoadTexture('sand.jpg', sand);
 LoadTexture('beton.jpg', beton);
 LoadTexture('threecolors.jpg', three_colors);
end;
                                     
procedure TGLForm.FormDestroy(Sender: TObject);
begin
 SettingsOnDestroy;
end;

procedure TGLForm.ApplicationEventsIdle(Sender: TObject; var Done: Boolean);
begin
 SettingsOnIdle1;

 glUseProgramObjectARB(shader);

 glUniform1iARB(glGetUniformLocationARB(shader, PGLCharARB('three_colors')), 0);
 glUniform1iARB(glGetUniformLocationARB(shader, PGLCharARB('beton')), 1);
 glUniform1iARB(glGetUniformLocationARB(shader, PGLCharARB('sand')), 2);
 glUniform1iARB(glGetUniformLocationARB(shader, PGLCharARB('wall')), 3);


 glActiveTextureARB(GL_TEXTURE0_ARB);
 glEnable(GL_TEXTURE_2D);
 glBindTexture(GL_TEXTURE_2D, three_colors);
 glActiveTextureARB(GL_TEXTURE1_ARB);
 glEnable(GL_TEXTURE_2D);
 glBindTexture(GL_TEXTURE_2D, beton);
 glActiveTextureARB(GL_TEXTURE2_ARB);
 glEnable(GL_TEXTURE_2D);
 glBindTexture(GL_TEXTURE_2D, sand);
 glActiveTextureARB(GL_TEXTURE3_ARB);
 glEnable(GL_TEXTURE_2D);
 glBindTexture(GL_TEXTURE_2D, wall);
  
 
 glTranslatef(-0.5, -0.5, -1);
 glBegin(GL_QUADS);
  glMultiTexCoord2fARB(GL_TEXTURE0_ARB, 0.0, 0.0);
  glMultiTexCoord2fARB(GL_TEXTURE1_ARB, 0.0, 0.0);
  glMultiTexCoord2fARB(GL_TEXTURE2_ARB, 0.0, 0.0);
  glMultiTexCoord2fARB(GL_TEXTURE3_ARB, 0.0, 0.0);
  glVertex3f(0, 0, 0);

  glMultiTexCoord2fARB(GL_TEXTURE0_ARB, 1.0, 0.0);
  glMultiTexCoord2fARB(GL_TEXTURE1_ARB, 1.0, 0.0);
  glMultiTexCoord2fARB(GL_TEXTURE2_ARB, 1.0, 0.0);
  glMultiTexCoord2fARB(GL_TEXTURE3_ARB, 1.0, 0.0);
  glVertex3f(1, 0, 0);

  glMultiTexCoord2fARB(GL_TEXTURE0_ARB, 1.0, 1.0);
  glMultiTexCoord2fARB(GL_TEXTURE1_ARB, 1.0, 1.0);
  glMultiTexCoord2fARB(GL_TEXTURE2_ARB, 1.0, 1.0);
  glMultiTexCoord2fARB(GL_TEXTURE3_ARB, 1.0, 1.0);
  glVertex3f(1, 1, 0);

  glMultiTexCoord2fARB(GL_TEXTURE0_ARB, 0.0, 1.0);
  glMultiTexCoord2fARB(GL_TEXTURE1_ARB, 0.0, 1.0);
  glMultiTexCoord2fARB(GL_TEXTURE2_ARB, 0.0, 1.0);
  glMultiTexCoord2fARB(GL_TEXTURE3_ARB, 0.0, 1.0);
  glVertex3f(0, 1, 0);
 glEnd;

 SettingsOnIdle2;
 Done := False;
end;

procedure TGLForm.FormKeyPress(Sender: TObject; var Key: Char);
begin
 processKeys(Key);
end;

end.
