home *** CD-ROM | disk | FTP | other *** search
- (***********************************************************)
- (* *)
- (* Program: TS_GR3d.PAS *)
- (* *)
- (* Specification: Testing program for Gr3d units *)
- (* *)
- (* Operation Date: 8/01/93 *)
- (* *)
- (* Description: *)
- (* *)
- (* Uses Gr3d_pt, Gr3d_obj, Gr3d_lst. This is a testing *)
- (* program, I wrote it to create and test simple 3d *)
- (* objects. Gr3d???? units are implemented using matrix *)
- (* (see MM_mem for detailed implementation. *)
- (* *)
- (* *)
- (* Feedback: *)
- (* GEnie: M.LEI1 *)
- (* Compuserve: 72002,157 [LEI MING] *)
- (* *)
- (***********************************************************)
- {$N+,E+,F+} { enable numeric coprocesser, enable far call }
-
- uses
- Crt, Graph, Mm_Mem, VGA256, RateTime, Gr3d_pt, Gr3d_lst, Gr3d_obj;
-
- const
-
- (* controls VGA color intensity, sateration etc. *)
-
- NH : Integer = 16;
- NI : Integer = 5;
- NS : Integer = 3;
-
- (* true if using VGA 256 colors *)
-
- UseVGA256 : Boolean = true;
-
- type
-
- (* map points on a array for DrawPoly *)
-
- PScrnType = ^TScrnType;
- TScrnType = array[1..100] of PointType;
- TPlanePtrType = set of byte;
-
- { some simple object types }
-
- PBox = ^TBox;
- TBox = object(TPointGroup)
- SideX, SideY, SideZ: integer;
- C: integer;
- P: integer;
- constructor Init(InitX, InitY, InitZ: integer;
- InitSideX, InitSideY, InitSideZ: integer;
- InitP, InitC: integer);
- procedure Draw; virtual;
- end;
-
- PPlane = ^TPlane;
- TPlane = object(TPointGroup)
- SideX, SideY: integer;
- C: integer;
- P: integer;
- constructor Init(InitX, InitY, InitZ: integer;
- InitSideX, InitSideY: integer;
- InitP, InitC: integer);
- procedure Draw; virtual;
- end;
-
- {------------------------------}
- { Supporting procedures }
-
- procedure DrawPlane(Condition: boolean; P1, P2, P3, P4: PointType; C: integer);
- var
- S: Array[1..5] of PointType;
- begin
- if condition then
- begin
- S[1] := P1;
- S[2] := P2;
- S[3] := P3;
- S[4] := P4;
- S[5] := P1;
- SetColor(C);
- SetFillStyle(SolidFill, C);
- FillPoly(5, S);
- end;
- end;
-
- function UseColor(S, P, C: Integer): integer;
- begin
- if UseVGA256 then UseColor := VGAColor(S,P-1,C-1)
- else UseColor := GetColor;
- end;
-
- {------------------------------}
- { Box methords }
- constructor TBox.Init(InitX, InitY, InitZ: integer;
- InitSideX, InitSideY, InitSideZ: integer;
- InitP, InitC: integer);
- begin
-
- TPointGroup.Init(InitX, InitY, InitZ); { init object }
- SideX := InitSideX; { initX, Y and Z is an inital point }
- SideY := InitSideY;
- SideZ := InitSideZ;
- P := InitP;
- C := InitC;
-
- InsertPoint(InitX+SideX, InitY, InitZ); { insert other points }
- InsertPoint(InitX+SideX, InitY, InitZ+SideZ);
- InsertPoint(InitX, InitY, InitZ+SideZ);
- InsertPoint(InitX, InitY+SideY, InitZ+SideZ);
- InsertPoint(InitX+SideX, InitY+SideY, InitZ+SideZ);
- InsertPoint(InitX+SideX, InitY+SideY, InitZ);
- InsertPoint(InitX, InitY+SideY, InitZ);
-
- end;
-
- procedure TBox.Draw; { draw object }
- { other methords are inhereted from TPointGroup }
- var
- SM: PMatrix;
- S: PScrnType;
- begin
- MConvertPoints(M, SM); { convert 3d points to a 2xn matrix }
- S := Pointer(SM^.Buf); { map points on array }
-
- (* Draw planes
- (* DrawPlane(GetX(1)+SideX<0, S^[5],S^[6],S^[7],S^[8], UseColor(0,P,C)); do not show back plane *)
-
- DrawPlane(GetX(1)>0, S^[1],S^[4],S^[5],S^[8], UseColor(1,P,C));
- DrawPlane(GetX(1)+SideX<0, S^[2],S^[7],S^[6],S^[3], UseColor(1,P,C));
- DrawPlane(GetZ(1)>0, S^[1],S^[2],S^[7],S^[8], UseColor(0,P,C));
- DrawPlane(GetZ(1)+SideZ<0, S^[4],S^[5],S^[6],S^[3], UseColor(NI-2,P,C));
- DrawPlane(GetY(1)>0, S^[1],S^[2],S^[3],S^[4], UseColor(NI-3,P,C));
-
- DelMatrix(SM); { delete matrix }
- end;
-
- {------------------------------}
- { Plane methords }
-
- constructor TPlane.Init(InitX, InitY, InitZ: integer;
- InitSideX, InitSideY: integer;
- InitP, InitC: integer);
- begin
-
- TPointGroup.Init(InitX, InitY, InitZ); { init object }
- SideX := InitSideX; { initX, Y and Z is an inital point }
- SideY := InitSideY;
- P := InitP;
- C := InitC;
-
- InsertPoint(InitX+SideX, InitY, InitZ); { insert other points }
- InsertPoint(InitX+SideX, InitY+SideY, InitZ);
- InsertPoint(InitX, InitY+SideY, InitZ);
-
- end;
-
- procedure TPlane.Draw; { draw object }
- { other methords are inhereted from TPointGroup }
- var
- SM: PMatrix;
- S: PScrnType;
- begin
- MConvertPoints(M, SM); { convert 3d points to a 2xn matrix }
- S := Pointer(SM^.Buf); { map points on array }
-
- (* Draw plane *)
-
- if GetZ(1)<0 then
- DrawPlane(true, S^[1],S^[2],S^[3],S^[4], UseColor(NI-1,P,C))
- else
- DrawPlane(true, S^[1],S^[2],S^[3],S^[4], UseColor(0,P,C));
-
- DelMatrix(SM); { delete matrix }
- end;
-
-
- {------------------------------}
- { additional procedures }
-
- procedure HeapStatus(Msg: string);
- var
- Temp: string;
- begin
- Str(MemAvail:6, Temp);
- Writeln(Msg+Temp);
- end;
-
- procedure StartGraph;
- var
- grDriver: integer;
- grMode: integer;
- begin
-
- DefaultColor := 0; { default background color: black }
- VGA256Init; { init VGA }
- grDriver := Detect;
- InitGraph(grDriver,grMode,'\tp\bgi');
- SetColor(DefaultColor);
- BackColor := 6; { set background color to 6}
-
- GraphMidx := (GetMaxX div 2); { set center point of screen }
- GraphMidy := (GetMaxy div 2);
-
- VidPageSize := 60*1024; { set page size to 60 k }
- VidPageSize := 0;
- if VGA256Inited then
- begin
- VGAConvertColor(NI, NS, NH);
- UseVGA256 := true;
- end;
- end;
-
- procedure Waitforkey; { continue when a key is pressed }
- var
- ch: char;
- begin
- while not(keypressed) do;
- ch := readkey;
- end;
-
- {----------- main }
- var
- Vtime: float; { returned from TestRate routine }
-
- MyView: View; { declare 4 views }
- MyView2: View;
- MyView3: View;
- MyView4: View;
-
- procedure TestIt; { Draw all views }
- var
- DeltaX, DeltaY, DeltaZ: integer;
- begin
-
- (* four views *)
-
- while MyView.GetDelta(DeltaX, DeltaY, DeltaZ) do
- begin
- MyView.Select;
- MyView.Step(DeltaX,DeltaY,DeltaZ);
- MyView2.Select;
- MyView2.Step(-DeltaX,-DeltaY,-DeltaZ);
- MyView3.Select;
- MyView3.Step(-DeltaX,-DeltaY,-DeltaZ);
- MyView4.Select;
- MyView4.Step(DeltaX,DeltaY,DeltaZ);
- end;
-
- end;
-
- procedure TestDrawRate; { test routine for TestRate }
- begin
- MyView.Select;
- MyView.Step(1,1,1);
- MyView2.Select;
- MyView2.Step(1,1,1);
- MyView3.Select;
- MyView3.Step(1,1,1);
- MyView4.Select;
- MyView4.Step(1,1,1);
- end;
-
-
- begin
-
- ClrScr; {-- heap message before allocation }
- HeapStatus('Before allocation:');
- WaitForKey;
-
- {------------ Init }
-
- StartGraph; { init graphics }
-
- { MyView.Init(0,LineWide,GetMaxX, GetMaxY-LineWide-1); select whole screen as a view }
-
-
- { init 4 graphic viewers }
-
- MyView.Init(0,0, GraphMidX, GraphMidY, 100);
- MyView2.Init(GraphMidX, 0, GraphMidX, GraphMidY, 101);
- MyView3.Init(0, GraphMidY, GraphMidX, GraphMidY, 102);
- MyView4.Init(GraphMidX, GraphMidY, GraphMidX, GraphMidY, 103);
-
- { set_up\draw background for each view }
-
- MyView.SetWindow;
- MyView2.SetWindow;
- MyView3.SetWindow;
- MyView4.SetWindow;
-
- {------------ add objects into view }
- { I add same object(s) for each view }
- { the only difference is the color. }
-
- MyView.Add(New(PPlane, Init(0, 30, -7, 10, 10, 3, 2)),1);
- MyView.Add(New(PBox, Init(0, 30, 10, 10, 10, 10, 3, 2)),1);
- MyView.Add(New(PBox, Init(-10,10,-5, 3, 50, 3, 3, 3)),2);
-
- MyView2.Add(New(PBox, Init(0, 30, 10, 10, 10, 10, 3, 4)),1);
- MyView2.Add(New(PBox, Init(-10,20,-5, 3, 50, 3, 3, 5)),2);
-
- MyView4.Add(New(PBox, Init(0, 30, 10, 10, 10, 10, 3, 6)),1);
- MyView4.Add(New(PBox, Init(-10,20,-5, 3, 50, 3, 3, 7)),2);
-
- MyView3.Add(New(PBox, Init(0, 30, 10, 10, 10, 10, 3, 8)),1);
- MyView3.Add(New(PBox, Init(-10,20,-5, 3, 50, 3, 3, 9)),2);
-
-
- MyView.Select; MyView.Show; {-- Show all views}
- MyView2.Select; MyView2.Show;
- MyView3.Select; MyView3.Show;
- MyView4.Select; MyView4.Show;
-
- Vtime := TestRate(TestDrawRate, 1); {-- test rate }
-
- (* TestViews; {-- test all views } *)
-
- MyView.Done; {-- Deallocate all views }
- MyView2.Done;
- MyView3.Done;
- MyView4.Done;
-
- CloseGraph; {-- close graphics }
-
- HeapStatus('After allocation: '); {-- heap message after deallocation }
-
- writeln('Operations/sec = ',Vtime:8:0);
- WaitForkey;
-
- end.