2 DEMO02 - Texture mapping and palette rotation
\r
3 (c) 1994 by Alessandro Scotti
\r
5 uses Crt, Modex, Plasma, Threed;
\r
9 Trans : TPoint = ( X:0; Y:0; Z:0 );
\r
15 Desc : array[ 0..3 ] of T2DPoint;
\r
17 Data : array[ 1..64*64 ] of byte;
\r
21 Vtx : array[ 0..3 ] of word;
\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
33 Palette: array[ byte ] of record R, G, B: byte; end;
\r
35 (* Make a 64x64 plasma to be used as texture *)
\r
36 procedure MakeTexture( Idx: word );
\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
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
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
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
92 mxSetMode( MX_320x240 );
\r
94 Page := 240; (* Start with hidden page *)
\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
110 mxSetClipRegion( 0, Page, 320, 240 ); (* Set clip to new page *)
\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
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
123 mxSetMode( MX_TEXT );
\r