unit quod_A;

interface
uses
  Mform, GLScene, Setup, Forms, ghload, SysUtils,
  Variants, Classes,  Graphics, Controls,
  GLUtils,
  GLWin32FullScreenViewer, GLWin32Viewer, GLMisc,
  GLTexture, GLCadencer, GLObjects, GLShadowPlane,
  GLSkyBox, GLLensFlare, GLGui;

var
  FCcontine : boolean = false;

  Scene : TGLScene;
  Viewer : TGLSceneViewer;
  ML1, ML2 : TGLMATERIALlIBRARY;

  QCamera : TGLCamera;
  GLWork, GameCube, GLInterface : TGLDummyCube;

  Sun : TGLLightSource;
  LensFlare : TGLLensFlare;
  Sky : TGLSkyBox;

  Shadow : TGLShadowPlane;

procedure CreatAll;

implementation
uses
  LFiles, Material;

Procedure CreatShadows;
begin
  if Shadow=nil then
  Shadow:=TGLShadowPlane(GameCube.AddNewChild(TGLShadowPlane));
  Shadow.Material.FrontProperties.Diffuse.Alpha:=0;
  Shadow.NoZWrite:=false;
  Shadow.Scale.SetVector(30,30,1);
  Shadow.ShadowColor.SetColor(0,0,0,0.57);
  Shadow.ShadowedLight:=Sun;
  Shadow.ShadowOptions:=[spoUseStencil,spoScissor,spoTransparent];
  Shadow.Position.Z:=0.02;
end;

procedure CreatViewer;
begin
  Viewer:=TGLSceneViewer.Create(QForm);
  Viewer.Parent:=QForm;
  Viewer.Enabled:=true;
  Viewer.Align:=alClient;
  Viewer.Buffer.BackgroundColor:=clBlack;
  Viewer.Buffer.FogEnable:=true;
  Viewer.Buffer.FogEnvironment.FogColor.SetColor(1,0.3,0.3,0.1);
  if Setup.Shadows then
  Viewer.Buffer.ContextOptions:=[roDoubleBuffer,roStencilBuffer,roRenderToWindow]
  else Viewer.Buffer.ContextOptions:=[roDoubleBuffer,roRenderToWindow];
  Viewer.Camera:=QCamera;
  Viewer.OnMouseDown:=QForm.FullViewer.OnMouseDown;
  Viewer.OnMouseUp:=QForm.FullViewer.OnMouseUp;
  Viewer.OnMouseMove:=QForm.FullViewer.OnMouseMove;
end;

Procedure UseSetup;
begin
  if (Setup.Shadows) then
  CreatShadows
  else
  if Shadow<>nil then freeandnil(Shadow);

  If FullWin=true then
    begin
      QForm.FullViewer.Height:=WinH;
      QForm.FullViewer.Width:=WinW;
      if WinColors=32 then
      QForm.FullViewer.ScreenDepth:=sd32bits;
      if WinColors=16 then
      QForm.FullViewer.ScreenDepth:=sd16bits;
      QForm.FullViewer.Buffer.BackgroundColor:=clBlack;
      QForm.FullViewer.Buffer.FogEnable:=true;
      QForm.FullViewer.Buffer.FogEnvironment.FogColor.SetColor(1,0.3,0.3,0.1);
      if Setup.Shadows then
      QForm.FullViewer.Buffer.ContextOptions:=[roDoubleBuffer,roStencilBuffer,roRenderToWindow]
      else QForm.FullViewer.Buffer.ContextOptions:=[roDoubleBuffer,roRenderToWindow];

      QForm.FullViewer.Camera:=QCamera;
      QForm.BorderStyle:=bsNone;
      QForm.Width:=0;
      QForm.Height:=0;
      QForm.FullViewer.Active:=true;
    end
  else
    begin
      QForm.FullViewer.Active:=false;
      if Viewer=nil then CreatViewer;
      QForm.BorderStyle:=bsDialog;
      QForm.ClientWidth:=Setup.WinW;
      QForm.ClientHeight:=Setup.WinH;
      QForm.Position:=poDesktopCenter;
    end;
end;


procedure CreatAll;
begin
  Setup.LoadSetupFile;
  
  Scene:=TglScene.Create(QForm);

  QForm.Cadencer.Scene:=Scene;

  ML1 := TGLMaterialLibrary.Create(QForm);
  ML2 := TGLMaterialLibrary.Create(QForm);

  GLWork:=TGLDummyCube(Scene.Objects.AddNewChild(TGLDummyCube));

  Sky:=TGLSkyBox(GLWork.AddNewChild(TGLSkyBox));
  Sun:=TGLLightSource(GLWork.AddNewChild(TGLLightSource));
  LensFlare:=TGLLensFlare(Sun.AddNewChild(TGLLensFlare));
  Sun.Position.SetPoint(55,1,45);

  Sky.PitchAngle:=90;
  Material.LoadJPG;

  GameCube:=TGLDummyCube(Scene.Objects.AddNewChild(TGLDummyCube));
  GLInterface:=TGLDummyCube(Scene.Objects.AddNewChild(TGLDummyCube));

  SetCurrentDir(ExtractFilePath(Application.ExeName)+'\Data\');
  QForm.GLBitmapFont1.Glyphs.LoadFromFile('font.bmp');

  ghload.GLGLayout:= TGLGuiLayout.Create(QForm);
  ghload.GLGLayout.BitmapFont:=QForm.GLBitmapFont1;
  ghload.GLGLayout.Material.Texture.Image.LoadFromFile('Skin.bmp');
  ghload.GLGLayout.Material.Texture.Disabled:=false;
  ghload.GLGLayout.FileName:='Skin';
  ghload.GLGUIWork:=GLInterface;
  ghload.Height:=Setup.WinH;
  ghload.Width:=Setup.WinW;
  ghload.glgLoad('1.gxt');

  LFiles.work;

  QCamera:=TGLCamera(GameCube.AddNewChild(TGLCamera));
  QCamera.Position.Z:=7;
  QCamera.TargetObject:=GameCube;
  UseSetup;

  FCcontine:=true;

end;


end.
