unit UMaterials;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, GLCrossPlatform, {BaseClasses,} GLMaterial, GLCustomShader,
  GLSLShader, GLSLDiffuseSpecularShader,GLTexture, GLScene, GLBaseClasses;

type
  TForm2 = class(TForm)
    GLMaterialLibrary1: TGLMaterialLibrary;
    ObjectShader: TGLSLDiffuseSpecularShader;
    PlaneShader: TGLSLDiffuseSpecularShader;
    procedure loadTextures;
  private        
    procedure loadTexture(TexName,FileName:string;Shader:TGLShader);overload;
    procedure loadTexture(TexName,FileName:string;Shader:TGLShader;
    ScaleFactor:Real);overload;
  public
  end;

var
  Form2:TForm2;
  
implementation

{$R *.dfm}

uses UMyUtils;


procedure TForm2.loadTexture(TexName,FileName:string;Shader:TGLShader);
begin
  if not FileExists('Texturen\'+FileName+'.jpg') then Exit;
  GLMaterialLibrary1.AddTextureMaterial(TexName,'Texturen\'+FileName+'.jpg');
  if Shader<>nil then
  GLMaterialLibrary1.Materials[GLMaterialLibrary1.Materials.Count-
  1].Shader:=Shader;
  GLMaterialLibrary1.Materials[GLMaterialLibrary1.Materials.Count-
  1].Material.Texture.TextureMode:=tmDecal;
end;

procedure TForm2.loadTexture(TexName,FileName:string;Shader:TGLShader;
  ScaleFactor:Real);
var BM1,BM2:TBitmap;
begin                           
  if not FileExists('Texturen\'+FileName+'.jpg') then Exit;
  BM1:=JPGtoBMP('Texturen\'+FileName+'.jpg');
  BM2:=TBitmap.Create;
  BM2.Width:=Round(BM1.Width*ScaleFactor);
  BM2.Height:=Round(BM1.Height*ScaleFactor);
  BM2.Canvas.StretchDraw(Rect(0,0,BM2.Width,BM2.Height),BM1);
  GLMaterialLibrary1.AddTextureMaterial(TexName,BM2);
  if Shader<>nil then
  GLMaterialLibrary1.Materials[GLMaterialLibrary1.Materials.Count-
  1].Shader:=Shader;
  GLMaterialLibrary1.Materials[GLMaterialLibrary1.Materials.Count-
  1].Material.Texture.TextureMode:=tmDecal;
end;

procedure TForm2.loadTextures;
begin
  try
    loadTexture('GreenSpeed','GreenSpeed',PlaneShader);
    loadTexture('RedSpeed','RedSpeed',PlaneShader);
    loadTexture('BlueSpeed','BlueSpeed',PlaneShader);
    loadTexture('Explosion','Explosion',PlaneShader);
    loadTexture('PortDownUp','PortDownUp',PlaneShader);
    loadTexture('PortUpDown','PortUpDown',PlaneShader);
    loadTexture('Turner','Turner',PlaneShader);
    loadTexture('Goal','Goal',PlaneShader);
    loadTexture('PusherUp','PusherUp',PlaneShader);
    loadTexture('PusherDown','PusherDown',PlaneShader);
    loadTexture('grey','grey',nil);
    loadTexture('Muh','Muh',ObjectShader);
    loadTexture('RandomSpeed','RandomSpeed',PlaneShader);
    loadTexture('ArrowSpawner','ArrowSpawner',PlaneShader);
    loadTexture('BlackExplosion','BlackExplosion',PlaneShader);
    loadTexture('Lightning','Lightning',PlaneShader);
    loadTexture('ContaminationDown','ContaminationDown',PlaneShader);
    loadTexture('ContaminationUp','ContaminationUp',PlaneShader);
    loadTexture('InkDown','InkDown',PlaneShader);
    loadTexture('InkUp','InkUp',PlaneShader);
    loadTexture('Bonbon','Bonbon',PlaneShader);
    loadTexture('Open','Background',PlaneShader);

    loadTexture('HalfWall','HalfWall',ObjectShader);
    loadTexture('HalfWallLowRes','HalfWall',ObjectShader,0.5);
    loadTexture('HalfWallVeryLowRes','HalfWall',ObjectShader,0.25);
    loadTexture('Wall','Wall',ObjectShader);
    loadTexture('WallLowRes','Wall',ObjectShader,0.5);
    loadTexture('WallVeryLowRes','Wall',ObjectShader,0.25);

    loadTexture('Arrow','Arrow',PlaneShader);       
    loadTexture('CeilingVeryLowRes','Ceiling',PlaneShader,0.1);
    loadTexture('CeilingLowRes','Ceiling',PlaneShader,0.5);
    loadTexture('Ceiling','Ceiling',PlaneShader);

    loadTexture('GrassVeryLowRes','Grass',PlaneShader,0.1);
    loadTexture('GrassLowRes','Grass',PlaneShader,0.5);
    loadTexture('Grass','Grass',PlaneShader);

    loadTexture('GreenWall','GreenWall',nil);
    GLMaterialLibrary1.Materials[GLMaterialLibrary1.Materials.Count-
    1].TextureScale.X:=4;
    GLMaterialLibrary1.Materials[GLMaterialLibrary1.Materials.Count-
    1].TextureScale.Y:=2;
    GLMaterialLibrary1.Materials[GLMaterialLibrary1.Materials.Count-
    1].TextureScale.Z:=4;

    loadTexture('HalfGreenWall','GreenWall',nil);
    GLMaterialLibrary1.Materials[GLMaterialLibrary1.Materials.Count-
    1].TextureScale.X:=4;
    GLMaterialLibrary1.Materials[GLMaterialLibrary1.Materials.Count-
    1].TextureScale.Y:=1;
    GLMaterialLibrary1.Materials[GLMaterialLibrary1.Materials.Count-
    1].TextureScale.Z:=4;

    loadTexture('HalfWallFar','HalfWall',nil,0.25);
    GLMaterialLibrary1.Materials[GLMaterialLibrary1.Materials.Count-
    1].TextureScale.X:=4;
    GLMaterialLibrary1.Materials[GLMaterialLibrary1.Materials.Count-
    1].TextureScale.Y:=1;
    GLMaterialLibrary1.Materials[GLMaterialLibrary1.Materials.Count-
    1].TextureScale.Z:=4;
    loadTexture('YSNP','YSNP',PlaneShader);
    
    loadTexture('WallFar','Wall',nil,0.25);
    GLMaterialLibrary1.Materials[GLMaterialLibrary1.Materials.Count-
    1].TextureScale.X:=4;
    GLMaterialLibrary1.Materials[GLMaterialLibrary1.Materials.Count-
    1].TextureScale.Y:=2;
    GLMaterialLibrary1.Materials[GLMaterialLibrary1.Materials.Count-
    1].TextureScale.Z:=4;
  except Application.Terminate;
  end;
end;


end.
