]> 4ch.mooo.com Git - 16.git/blob - 16/x/modex/DEMO02.PAS
push
[16.git] / 16 / x / modex / DEMO02.PAS
1 (*\r
2     DEMO02 - Texture mapping and palette rotation\r
3     (c) 1994 by Alessandro Scotti\r
4 *)\r
5 uses Crt, Modex, Plasma, Threed;\r
6 \r
7 const\r
8   LSIZE = 85;\r
9   Trans : TPoint = ( X:0; Y:0; Z:0 );\r
10 type\r
11   T2DPoint = record\r
12     X, Y: integer;\r
13   end;\r
14   TTexture = record\r
15     Desc   : array[ 0..3 ] of T2DPoint;\r
16     Width  : word;\r
17     Data   : array[ 1..64*64 ] of byte;\r
18   end;\r
19   TQuad = record\r
20     VtxCnt : word;\r
21     Vtx    : array[ 0..3 ] of word;\r
22     Texture: word;\r
23   end;\r
24 var\r
25   Vtx   : array[ 0..7 ] of TPoint;\r
26   XVtx  : array[ 0..7 ] of TPoint;\r
27   VVtx  : array[ 0..7 ] of T2DPoint;\r
28   Face  : array[ 0..5 ] of TQuad;\r
29   Txts  : array[ 0..5 ] of TTexture;\r
30   Nrm   : array[ 0..5 ] of TPoint;\r
31   XNrm  : array[ 0..5 ] of TPoint;\r
32   Page  : word;\r
33   Palette: array[ byte ] of record R, G, B: byte; end;\r
34 \r
35 (* Make a 64x64 plasma to be used as texture *)\r
36 procedure MakeTexture( Idx: word );\r
37 var\r
38   I: word;\r
39 begin\r
40   mxFillBox( 0, 0, 64, 64, 0, OP_SET );\r
41   MakePlasma( 0, 0, 64, 64, 96, Random(192)+1, Random(192)+1, Random(192)+1 );\r
42   mxGetImage( @Txts[Idx].Data, 0, 0, 64, 64 );\r
43   (* Texture vertexes are 8:8 fixed, add $80 (0.5) for best results *)\r
44   with Txts[Idx] do begin\r
45     Desc[0].X := $80; Desc[0].Y := $80;\r
46     Desc[1].X := $80; Desc[1].Y := $3F80;\r
47     Desc[2].X := $3F80; Desc[2].Y := $3F80;\r
48     Desc[3].X := $3F80; Desc[3].Y := $80;\r
49     Width := 64;\r
50   end;\r
51 end;\r
52 \r
53 procedure Init;\r
54 var\r
55   I: integer;\r
56 begin\r
57   (* Build vertexes for a cube *)\r
58   with Vtx[0] do begin X:=-LSIZE; Y:=-LSIZE; Z:=-LSIZE; end;\r
59   with Vtx[1] do begin X:=+LSIZE; Y:=-LSIZE; Z:=-LSIZE; end;\r
60   with Vtx[2] do begin X:=-LSIZE; Y:=+LSIZE; Z:=-LSIZE; end;\r
61   with Vtx[3] do begin X:=+LSIZE; Y:=+LSIZE; Z:=-LSIZE; end;\r
62   with Vtx[4] do begin X:=-LSIZE; Y:=-LSIZE; Z:=+LSIZE; end;\r
63   with Vtx[5] do begin X:=+LSIZE; Y:=-LSIZE; Z:=+LSIZE; end;\r
64   with Vtx[6] do begin X:=-LSIZE; Y:=+LSIZE; Z:=+LSIZE; end;\r
65   with Vtx[7] do begin X:=+LSIZE; Y:=+LSIZE; Z:=+LSIZE; end;\r
66   for I:=0 to 7 do begin          (* Make points 16:16 fixed *)\r
67     Vtx[I].X := Vtx[I].X*$10000;\r
68     Vtx[I].Y := Vtx[I].Y*$10000;\r
69     Vtx[I].Z := Vtx[I].Z*$10000;\r
70   end;\r
71   (* Build faces *)\r
72   with Face[0] do begin Vtx[0]:=0; Vtx[1]:=2; Vtx[2]:=3; Vtx[3]:=1; end;\r
73   with Face[1] do begin Vtx[0]:=4; Vtx[1]:=5; Vtx[2]:=7; Vtx[3]:=6; end;\r
74   with Face[2] do begin Vtx[0]:=0; Vtx[1]:=1; Vtx[2]:=5; Vtx[3]:=4; end;\r
75   with Face[3] do begin Vtx[0]:=1; Vtx[1]:=3; Vtx[2]:=7; Vtx[3]:=5; end;\r
76   with Face[4] do begin Vtx[0]:=2; Vtx[1]:=0; Vtx[2]:=4; Vtx[3]:=6; end;\r
77   with Face[5] do begin Vtx[0]:=7; Vtx[1]:=3; Vtx[2]:=2; Vtx[3]:=6; end;\r
78   for I:=0 to 5 do Face[I].Texture := I;\r
79   (* Build textures and palette *)\r
80   Randomize;\r
81   FillChar( Palette, SizeOf(Palette), 0 );\r
82   MakePlasmaPalette( Palette, PAL_RGB );\r
83   mxSetPalette( @Palette, 0, 193 );\r
84   for I:=0 to 5 do MakeTexture( I );\r
85 end;\r
86 \r
87 var\r
88   AX, AY, AZ: byte;\r
89   I: word;\r
90 begin\r
91   mxInit;\r
92   mxSetMode( MX_320x240 );\r
93   Init;\r
94   Page := 240;          (* Start with hidden page *)\r
95 \r
96   AX := 0;\r
97   AY := 0;\r
98   AZ := 0;\r
99   (* Init 3D transforms, perspective is intentionally exaggerated *)\r
100   tdSetTranslation( Trans );\r
101   tdSetPerspective( 400*$10000, $10000, $10000 );\r
102   (* Main loop, all magic here! *)\r
103   while( not KeyPressed ) do begin\r
104     tdSetRotation( AX, AY, AZ );                (* Set new angles *)\r
105     tdTransform( Vtx, XVtx, 8 );                (* 3D transform points *)\r
106     tdTransformToImage( XVtx, VVtx, 8, 160, 120+Page );\r
107     Inc( AX, 1 );                               (* Bump angles *)\r
108     Inc( AY, 1 );\r
109     Inc( AZ, 2 );\r
110     mxSetClipRegion( 0, Page, 320, 240 );       (* Set clip to new page *)\r
111     mxSetClip( TRUE );\r
112     mxFillBox( 0, Page, 320, 240, 0, OP_MOVE ); (* Clear screen *)\r
113     mxRotatePalette( @Palette[1], 192, 3 );     (* Rotate palette *)\r
114     (* Draw cube: backface culling is straighforward in this case, so *)\r
115     (* it can be handled by the polygon filling procedure *)\r
116     for I:=0 to 5 do\r
117       mxTexturePoly( 4, Face[I].Vtx, VVtx, Txts[I].Desc, Txts[I].Data, Txts[I].Width );\r
118     mxStartLine( Page );                        (* Flip pages *)\r
119     mxSetPalette( @Palette[1], 1, 192 );        (* Set new palette *)\r
120     Page := 240-Page;\r
121   end;\r
122 \r
123   mxSetMode( MX_TEXT );\r
124   mxTerm;\r
125 end.\r