//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
#include <math.h>

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "GLCadencer"
#pragma link "GLMisc"
#pragma link "GLObjects"
#pragma link "GLProjectedTextures"
#pragma link "GLScene"
#pragma link "GLTexture"
#pragma link "GLWin32Viewer"
#pragma link "jpeg"
#pragma link "tga"
#pragma resource "*.dfm"
TForm1 *Form1;
float  ang;
int  mx, my, mk;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------

void __fastcall TForm1::GLCadencer1Progress(TObject *Sender,
      const double deltaTime, const double newTime)
{
  ang = ang + deltaTime*20;

  Light->Position->Y = sin(DegToRad(ang));
  Light->Position->X = cos(DegToRad(ang));

  light2->Pitch(deltaTime*20);

  viewer->Invalidate();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
  SetCurrentDir("..\\..\\media");
  matLib->Materials->Items[0]->Material->Texture->Image->LoadFromFile("projector.tga");
  matLib->Materials->Items[1]->Material->Texture->Image->LoadFromFile("flare1.bmp");

  emitter1->Material->MaterialLibrary = matLib;
  emitter1->Material->LibMaterialName = "spot";

  emitter2->Material->MaterialLibrary = matLib;
  emitter2->Material->LibMaterialName = "spot2";
  emitter2->FOVy = 40;

  GLPlane1->Material->Texture->Image->LoadFromFile("cm_front.jpg");
  GLPlane2->Material->Texture->Image->LoadFromFile("cm_left.jpg");
  GLPlane3->Material->Texture->Image->LoadFromFile("cm_bottom.jpg");

  ProjLight->Emitters->AddEmitter(emitter1);
  ProjLight->Emitters->AddEmitter(emitter2);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
  Form1->Caption = Format("%f", ARRAYOFCONST((viewer->FramesPerSecond())));
  viewer->ResetPerformanceMonitor();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::viewerMouseDown(TObject *Sender,
      TMouseButton Button, TShiftState Shift, int X, int Y)
{
  mk = 1;
  mx = X;
  my = Y;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::viewerMouseUp(TObject *Sender, TMouseButton Button,
      TShiftState Shift, int X, int Y)
{
  mk = 0;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::viewerMouseMove(TObject *Sender, TShiftState Shift,
      int X, int Y)
{
  if (mk == 1)
  {
    if (Shift.Contains(ssLeft))
      camera->MoveAroundTarget(Y - my, X - mx);
    else if (Shift.Contains(ssRight))
      camera->AdjustDistanceToTarget(1.0 + (Y - my) / 100);
  }

  mx = X;
  my = Y;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormKeyDown(TObject *Sender, WORD &Key,
      TShiftState Shift)
{
  if (Key == VK_ADD)
      emitter1->FOVy = emitter1->FOVy + 5;
  else if (Key == VK_SUBTRACT)
      emitter1->FOVy = emitter1->FOVy - 5;

  if (Key == 'S')
      if (ProjLight->Style == ptsOriginal)
           ProjLight->Style = ptsInverse;
      else
           ProjLight->Style = ptsOriginal;
}
//---------------------------------------------------------------------------
