?? cameraclass.pas
字號:
unit CameraClass;
interface
Uses OpenGL, Windows;
type TVector3f = Record
X, Y, Z : glFloat;
end;
type TCamera = Object
Position : TVector3f; // The camera's position
View : TVector3f; // The camera's View
UpVector : TVector3f; // The camera's UpVector
procedure PositionCamera(positionX, positionY, positionZ : glFloat;
viewX, viewY, viewZ : glFloat;
upVectorX, upVectorY, upVectorZ : glFloat);
procedure RotateView(const X, Y, Z : glFloat);
procedure MoveCameraByMouse();
procedure RotateAroundPoint(const Center : TVector3f; const X, Y, Z : glFloat);
procedure StrafeCamera(speed : glFloat);
procedure MoveCamera(speed : glFloat);
end;
var SCREEN_WIDTH, SCREEN_HEIGHT : Integer;
implementation
{ TCamera }
{------------------------------------------------------------------------}
{--- This function sets the camera's position and view and up vVector ---}
{------------------------------------------------------------------------}
procedure TCamera.PositionCamera(positionX, positionY, positionZ, viewX,
viewY, viewZ, upVectorX, upVectorY, upVectorZ: glFloat);
begin
Position.X := PositionX;
Position.Y := PositionY;
Position.Z := PositionZ;
View.X := ViewX;
View.Y := ViewY;
View.Z := ViewZ;
UpVector.X := UpVectorX;
UpVector.Y := UpVectorY;
UpVector.Z := UpVectorZ;
end;
{-----------------------------------------------------------------------------}
{--- This will move the camera forward or backward depending on the speed ---}
{-----------------------------------------------------------------------------}
procedure TCamera.MoveCamera(speed: glFloat);
var V : TVector3f;
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 * speed; // Add our acceleration to our position's X
Position.Y := Position.Y + V.Y * speed; // Add our acceleration to our position's Y
Position.Z := Position.Z + V.Z * speed; // Add our acceleration to our position's Z
View.X := View.X + V.X * speed; // Add our acceleration to our view's X
View.Y := View.Y + V.Y * speed; // Add our acceleration to our view's Y
View.Z := View.Z + V.Z * speed; // Add our acceleration to our view's Z
end;
{-----------------------------------------------------------}
{--- The mouse look function. Use mouse to look around ---}
{-----------------------------------------------------------}
procedure TCamera.MoveCameraByMouse;
var mousePos : TPoint;
middleX, middleY : Integer;
deltaY, rotateY : glFloat;
begin
middleX := SCREEN_WIDTH SHR 1; // This is a binary shift to get half the width
middleY := SCREEN_HEIGHT SHR 1; // 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;
deltaY := (middleY - mousePos.Y)/1000;
// 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 strafes the camera left and right depending on the speed ---}
{---------------------------------------------------------------------}
procedure TCamera.StrafeCamera(speed: glFloat);
var Cross, ViewVector : TVector3f;
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 * speed;
Position.Z := Position.Z + Cross.Z * speed;
// Add the resultant vVector to our view
View.X := View.X + Cross.X * speed;
View.Z := View.Z + Cross.Z * speed;
end;
{-----------------------------------------------------------}
{--- This rotates the view around the position ---}
{-----------------------------------------------------------}
procedure TCamera.RotateView(const X, Y, Z: glFloat);
var vVector : TVector3f;
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 rotates the camera position around a given point ---}
{-------------------------------------------------------------}
procedure TCamera.RotateAroundPoint(const Center: TVector3f; const X, Y, Z: glFloat);
var viewVector : TVector3f;
begin
// Get the viewVector from our position to the center we are rotating around
viewVector.X := Position.X - Center.X; // This gets the direction of the X
viewVector.Y := Position.Y - Center.Y; // This gets the direction of the Y
viewVector.Z := Position.Z - Center.Z; // This gets the direction of the Z
// Rotate the position up or down, then add it to the center point
if X <> 0 then
begin
Position.Z := Center.Z + sin(X)*viewVector.Y + cos(X)*viewVector.Z;
Position.Y := Center.Y + cos(X)*viewVector.Y - sin(X)*viewVector.Z;
end;
if Y <> 0 then
begin
Position.Z := Center.Z + sin(Y)*viewVector.X + cos(Y)*viewVector.Z;
Position.X := Center.X + cos(Y)*viewVector.X - sin(Y)*viewVector.Z;
end;
if Z <> 0 then
begin
Position.X := Center.X + sin(Z)*viewVector.Y + cos(Z)*viewVector.X;
Position.Y := Center.Y + cos(Z)*viewVector.Y - sin(Z)*viewVector.X
end;
end;
end.
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -