unit Camera;

interface
uses Windows,Controls,Types,OpenGL;


type TVertex3f     = record
 x,y,z : GLFloat;
end;

type TCamera = Object
    Position : TVertex3f;      // The camera's position
    View     : TVertex3f;      // The camera's View
    UpVector : TVertex3f;      // The camera's UpVector

    procedure GoToCamera(positionX, positionY, positionZ : glFloat; viewX, viewY, viewZ : glFloat);
    procedure MoveCameraByMouse;
    procedure RotateView(const X, Y, Z: glFloat);
    procedure StrafeCamera(speed : glFloat);
    procedure SetViewport;
    procedure MoveCamera(speed: glFloat);
end;


var
 cam : TCamera;
 TimeCorrection : real;
 vPort:array [0..3] of integer;
 app_left,app_top : integer;

const
 MOUSE_SPEED = 1;

implementation



procedure TCamera.GoToCamera(positionX, positionY, positionZ : glFloat;
			     viewX, viewY, viewZ : glFloat);
 var mousePos:TPoint;
begin
  mousePos.X:=vPort[2] shr 1;
  mousePos.Y:=vPort[3] shr 1;
  mouse.CursorPos:=mousePos;

  Position.X := PositionX;
  Position.Y := PositionY;
  Position.Z := PositionZ;

  View.X     := viewX;
  View.Y     := viewY;
  View.Z     := viewZ;

  UpVector.X := 0;
  UpVector.Y := 1;
  UpVector.Z := 0;
end;

{-----------------------------------------------------------}
{--- The mouse look function. Use mouse to look around   ---}
{-----------------------------------------------------------}
procedure TCamera.MoveCameraByMouse;
var mousePos : TPoint;
    middleX, middleY : Integer;
    deltaY, rotateY : glFloat;
begin
  middleX := (vPort[2] SHR 1)+app_left;       // This is a binary shift to get half the width
  middleY := (vPort[3] SHR 1)+app_top;      // This is a binary shift to get half the height

  // Get the mouse's current X,Y position
  GetCursorPos(mousePos);

  // If our cursor is still in the middle, we never moved... so don't update the screen
  if (mousePos.X = middleX) AND (mousePos.Y = middleY) then
    exit;

  // Set the mouse position to the middle of our window
  SetCursorPos(middleX, middleY);

  // Get the direction the mouse moved in, but bring the number down to a reasonable amount
  rotateY := ((middleX - mousePos.X)/500)*MOUSE_SPEED;
  deltaY  := ((middleY - mousePos.Y)/1000)*MOUSE_SPEED;


  // Multiply the direction vVector for Y by an acceleration (The higher the faster is goes).
  View.Y := View.Y + deltaY*5;

  // Check if the distance of our view exceeds 60 from our position, if so, stop it. (UP)
  if View.Y - Position.Y > 10 then
     View.Y := Position.Y + 10;

  // Check if the distance of our view exceeds -60 from our position, if so, stop it. (DOWN)
  if View.Y - Position.Y < -10 then
     View.Y := Position.Y - 10;

  // Here we rotate the view along the X avis depending on the direction (Left of Right)
  RotateView(0, -rotateY, 0);
end;


{-----------------------------------------------------------}
{--- This rotates the view around the position           ---}
{-----------------------------------------------------------}
procedure TCamera.RotateView(const X, Y, Z: glFloat);
var vVector : TVertex3f;
begin
  // Get our view vVector (The direction we are facing)
  vVector.X := View.X - Position.X;          // This gets the direction of the X
  vVector.Y := View.Y - Position.Y;          // This gets the direction of the Y
  vVector.Z := View.Z - Position.Z;          // This gets the direction of the Z

  // If we pass in a negative X Y or Z, it will rotate the opposite way,
  // so we only need one function for a left and right, up or down rotation.
  if X <> 0 then
  begin
    View.Z := Position.Z + sin(X)*vVector.Y + cos(X)*vVector.Z;
    View.Y := Position.Y + cos(X)*vVector.Y - sin(X)*vVector.Z;
  end;

  if Y <> 0 then
  begin
    View.Z := Position.Z + sin(Y)*vVector.X + cos(Y)*vVector.Z;
    View.X := Position.X + cos(Y)*vVector.X - sin(Y)*vVector.Z;
  end;

  if Z <> 0 then
  begin
    View.X := Position.X + sin(Z)*vVector.Y + cos(Z)*vVector.X;
    View.Y := Position.Y + cos(Z)*vVector.Y - sin(Z)*vVector.X
  end;
end;

{---------------------------------------------------------------------}
{--- This strafes the camera left and right depending on the speed ---}
{---------------------------------------------------------------------}
procedure TCamera.StrafeCamera(speed: glFloat);
var Cross, ViewVector : TVertex3f;
begin
  // Initialize a variable for the cross product result
  Cross.X :=0;
  Cross.Y :=0;
  Cross.Z :=0;

  // Get the view vVector of our camera and store it in a local variable
  ViewVector.X := View.X - Position.X;
  ViewVector.Y := View.Y - Position.Y;
  ViewVector.Z := View.Z - Position.Z;

  // Calculate the cross product of our up vVector and view vVector
  Cross.X := (UpVector.Y * ViewVector.Z) - (UpVector.Z * ViewVector.Y);   // (V1.Y * V2.Z) - (V1.Z * V2.Y)
  Cross.Y := (UpVector.Z * ViewVector.X) - (UpVector.X * ViewVector.Z);   // (V1.Z * V2.X) - (V1.X * V2.Z)
  Cross.Z := (UpVector.X * ViewVector.Y) - (UpVector.Y * ViewVector.X);   // (V1.X * V2.Y) - (V1.Y * V2.X)

  // Add the resultant vVector to our position
  Position.X := Position.X + Cross.X * TimeCorrection/speed;
  Position.Z := Position.Z + Cross.Z * TimeCorrection/speed;

  // Add the resultant vVector to our view
  View.X := View.X + Cross.X * TimeCorrection/speed;
  View.Z := View.Z + Cross.Z * TimeCorrection/speed;
end;


procedure TCamera.SetViewport;
begin
(*
glrotate(View.x, 1,0,0);
glrotate(View.y, 0,1,0);
glrotate(View.z, 0,0,1);
gltranslate(Position.X, Position.Y, Position.Z);*)


gluLookAt(Position.X, Position.Y, Position.Z,
          View.x,     View.y,     View.Z,
          UpVector.x, UpVector.y, UpVector.Z);

end;


procedure TCamera.MoveCamera(speed: glFloat);
var V : TVertex3f;
begin
  // Get our view vVector (The direciton we are facing)
  V.X := View.X - Position.X;              // This gets the direction of the X
  V.Y := View.Y - Position.Y;              // This gets the direction of the Y
  V.Z := View.Z - Position.Z;              // This gets the direction of the Z

  Position.X := Position.X + V.X * TimeCorrection/speed;  // Add our acceleration to our position's X
  Position.Y := Position.Y + V.Y * TimeCorrection/speed;  // Add our acceleration to our position's Y
  Position.Z := Position.Z + V.Z * TimeCorrection/speed;  // Add our acceleration to our position's Z
  View.X := View.X + V.X * TimeCorrection/speed;          // Add our acceleration to our view's X
  View.Y := View.Y + V.Y * TimeCorrection/speed;          // Add our acceleration to our view's Y
  View.Z := View.Z + V.Z * TimeCorrection/speed;          // Add our acceleration to our view's Z
end;






end.
