|
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,TV3D65_TLB ,Math;
type
D3DVECTOR=record
x ouble;
y:Double ;
z:Double ;
end;
type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
{ Private declarations }
public
{ Public declarations }
TV:TVEngine ;
Scene:TVScene ;
Input:TVInputEngine ;
Globals:TVGlobals ;
Mesh:TVMesh ;
Atmos:TVAtmosphere ;
TextureFactory :TVTextureFactory ;
Position :D3DVECTOR;
Lookat:D3DVECTOR ;
Angle:D3DVECTOR ;
Walk :Double ;
Strafe:Double ;
Procedure AppIdle(sender : TObject; var done : boolean);
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
TV := CoTVEngine.Create ;
TV.SetDebugMode(True ,true ,True ,True);
TV.SetDebugFile(ExtractFilePath(Application .ExeName ) + 'ErrLog.Log');
TV.Init3DWindowed(Form1 .Handle ,True );
TV.GetViewport.SetAutoResize(True );
TV.DisplayFPS(True,-1);
TV.SetAngleSystem(TV_ANGLE_DEGREE );
TV.SetAntialiasing(True,0);
Scene := CoTVScene.Create ;
Globals := CoTVGlobals.Create ;
Input := CoTVInputEngine.Create ;
Input.Initialize(True ,True );
TextureFactory := CoTVTextureFactory.Create ;
Atmos := CoTVAtmosphere.Create ;
Mesh := Scene.CreateMeshBuilder('MyMesh');
Scene.SetBackgroundColor(0,0,0,0);
TextureFactory.LoadTexture('.\media\top.bmp','stop',0,0,0,True);
TextureFactory.LoadTexture('.\media\bottom.bmp','sbottom',0,0,0,True);
TextureFactory.LoadTexture('.\media\left.bmp','sleft',0,0,0,True);
TextureFactory.LoadTexture('.\media\right.bmp','sright',0,0,0,True);
TextureFactory.LoadTexture('.\media\front.bmp','sfront',0,0,0,True);
TextureFactory.LoadTexture('.\media\back.bmp','sback',0,0,0,True);
Atmos.SkyBox_SetTexture(Globals.GetTex('sfront'), Globals.GetTex('sback'), Globals.GetTex('sleft'), Globals.GetTex('sright'), Globals.GetTex('stop'), Globals.GetTex('sbottom'));
Atmos.SkyBox_Enable(True);
TextureFactory.LoadTexture( '.\media\fieldstone.tga', 'floor',0,0,0,True);
TextureFactory.LoadTexture( '.\media\roof1.bmp', 'wall',0,0,0,True);
Mesh.AddWall(Globals.GetTex('wall'),500, -500, -500, -500, 100, 0, 3, 1,True);
Mesh.AddWall(Globals.GetTex('wall'),-500, -500, -500, 500, 100, 0, 3, 1,True);
Mesh.AddWall(Globals.GetTex('wall'),-500, 500, 500, 500, 100, 0, 3, 1,True);
Mesh.AddWall(Globals.GetTex('wall'),500, 500, 500, -500, 100, 0, 3, 1,True);
Mesh.AddFloor(Globals.GetTex('floor'),-500, -500, 500, 500, 0, 10, 10,true) ;
Position.x :=1;
Position.x := 0;
Position.y := 40;
Position.z := 0;
LookAt.x := 50;
LookAt.y := 20;
LookAt.z := 50;
Walk := 0;
Strafe := 0;
Scene.SetViewFrustum(60,10000);
Form1.Show;
Application.OnIdle := AppIdle;
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
TV := nil;
end;
procedure TForm1.AppIdle(sender : TObject; var done : boolean);
var
mx,my:Integer;
begin
mx:=0;
my:=0;
if Form1.Focused then begin
repeat
Application.ProcessMessages;
// if Input.IsKeyPressed(TV_KEY_W) then ShowMessage('OK');
if Input.IsKeyPressed(TV_KEY_UP) then Walk := 1;
if Input.IsKeyPressed(TV_KEY_DOWN) then Walk := -1;
if input.IsKeyPressed(TV_KEY_LEFT) then Strafe := 1;
if input.IsKeyPressed(TV_KEY_RIGHT) then Strafe := -1;
//input.GetMousePosition(mx,my);
Angle.x := Angle.x - MY div 100;
Angle.y := Angle.y - MX div 100;
If Angle.x > 1.3 Then Angle.x := 1.3;
If Angle.x < -1.3 Then Angle.x := -1.3;
if Walk >0 then begin
Walk :=Walk - 0.05;
if Walk <0 then Walk := 0;
end;
if Walk <0 then begin
Walk :=Walk + 0.05;
if Walk >0 then Walk := 0;
end;
if Strafe >0 then begin
Strafe := Strafe - 0.05;
if Strafe <0 then Strafe := 0;
end;
if Strafe <0 then begin
Strafe := Strafe + 0.05;
if Strafe >0 then Strafe := 0;
end;
Position.x := Position.x + (Cos(Angle.y) * Walk * TV.TimeElapsed) + (Cos(Angle.y + 1) * Strafe * TV.TimeElapsed);
Position.z := Position.z + (Sin(Angle.y) * Walk * TV.TimeElapsed) + (Sin(Angle.y + 1) * Strafe * TV.TimeElapsed);
LookAt.x := Position.x + Cos(Angle.y);
LookAt.y := Position.y + Tan(Angle.x);
LookAt.z := Position.z + Sin(Angle.y);
Scene.SetCamera(Position.x,Position.y, Position.z, LookAt.x, LookAt.y, LookAt.z);
TV.Clear(False);
Atmos.Atmosphere_Render;
Atmos.Fog_Enable(true);
Mesh.Render;
Scene.RenderAll(True,True );
TV.RenderToScreen ;
until Input.IsKeyPressed(TV_KEY_ESCAPE);
Close();
end;
end;
end. |
|