--- /dev/null
+(*\r
+ DEMO02 - Texture mapping and palette rotation\r
+ (c) 1994 by Alessandro Scotti\r
+*)\r
+uses Crt, Modex, Plasma, Threed;\r
+\r
+const\r
+ LSIZE = 85;\r
+ Trans : TPoint = ( X:0; Y:0; Z:0 );\r
+type\r
+ T2DPoint = record\r
+ X, Y: integer;\r
+ end;\r
+ TTexture = record\r
+ Desc : array[ 0..3 ] of T2DPoint;\r
+ Width : word;\r
+ Data : array[ 1..64*64 ] of byte;\r
+ end;\r
+ TQuad = record\r
+ VtxCnt : word;\r
+ Vtx : array[ 0..3 ] of word;\r
+ Texture: word;\r
+ end;\r
+var\r
+ Vtx : array[ 0..7 ] of TPoint;\r
+ XVtx : array[ 0..7 ] of TPoint;\r
+ VVtx : array[ 0..7 ] of T2DPoint;\r
+ Face : array[ 0..5 ] of TQuad;\r
+ Txts : array[ 0..5 ] of TTexture;\r
+ Nrm : array[ 0..5 ] of TPoint;\r
+ XNrm : array[ 0..5 ] of TPoint;\r
+ Page : word;\r
+ Palette: array[ byte ] of record R, G, B: byte; end;\r
+\r
+(* Make a 64x64 plasma to be used as texture *)\r
+procedure MakeTexture( Idx: word );\r
+var\r
+ I: word;\r
+begin\r
+ mxFillBox( 0, 0, 64, 64, 0, OP_SET );\r
+ MakePlasma( 0, 0, 64, 64, 96, Random(192)+1, Random(192)+1, Random(192)+1 );\r
+ mxGetImage( @Txts[Idx].Data, 0, 0, 64, 64 );\r
+ (* Texture vertexes are 8:8 fixed, add $80 (0.5) for best results *)\r
+ with Txts[Idx] do begin\r
+ Desc[0].X := $80; Desc[0].Y := $80;\r
+ Desc[1].X := $80; Desc[1].Y := $3F80;\r
+ Desc[2].X := $3F80; Desc[2].Y := $3F80;\r
+ Desc[3].X := $3F80; Desc[3].Y := $80;\r
+ Width := 64;\r
+ end;\r
+end;\r
+\r
+procedure Init;\r
+var\r
+ I: integer;\r
+begin\r
+ (* Build vertexes for a cube *)\r
+ with Vtx[0] do begin X:=-LSIZE; Y:=-LSIZE; Z:=-LSIZE; end;\r
+ with Vtx[1] do begin X:=+LSIZE; Y:=-LSIZE; Z:=-LSIZE; end;\r
+ with Vtx[2] do begin X:=-LSIZE; Y:=+LSIZE; Z:=-LSIZE; end;\r
+ with Vtx[3] do begin X:=+LSIZE; Y:=+LSIZE; Z:=-LSIZE; end;\r
+ with Vtx[4] do begin X:=-LSIZE; Y:=-LSIZE; Z:=+LSIZE; end;\r
+ with Vtx[5] do begin X:=+LSIZE; Y:=-LSIZE; Z:=+LSIZE; end;\r
+ with Vtx[6] do begin X:=-LSIZE; Y:=+LSIZE; Z:=+LSIZE; end;\r
+ with Vtx[7] do begin X:=+LSIZE; Y:=+LSIZE; Z:=+LSIZE; end;\r
+ for I:=0 to 7 do begin (* Make points 16:16 fixed *)\r
+ Vtx[I].X := Vtx[I].X*$10000;\r
+ Vtx[I].Y := Vtx[I].Y*$10000;\r
+ Vtx[I].Z := Vtx[I].Z*$10000;\r
+ end;\r
+ (* Build faces *)\r
+ with Face[0] do begin Vtx[0]:=0; Vtx[1]:=2; Vtx[2]:=3; Vtx[3]:=1; end;\r
+ with Face[1] do begin Vtx[0]:=4; Vtx[1]:=5; Vtx[2]:=7; Vtx[3]:=6; end;\r
+ with Face[2] do begin Vtx[0]:=0; Vtx[1]:=1; Vtx[2]:=5; Vtx[3]:=4; end;\r
+ with Face[3] do begin Vtx[0]:=1; Vtx[1]:=3; Vtx[2]:=7; Vtx[3]:=5; end;\r
+ with Face[4] do begin Vtx[0]:=2; Vtx[1]:=0; Vtx[2]:=4; Vtx[3]:=6; end;\r
+ with Face[5] do begin Vtx[0]:=7; Vtx[1]:=3; Vtx[2]:=2; Vtx[3]:=6; end;\r
+ for I:=0 to 5 do Face[I].Texture := I;\r
+ (* Build textures and palette *)\r
+ Randomize;\r
+ FillChar( Palette, SizeOf(Palette), 0 );\r
+ MakePlasmaPalette( Palette, PAL_RGB );\r
+ mxSetPalette( @Palette, 0, 193 );\r
+ for I:=0 to 5 do MakeTexture( I );\r
+end;\r
+\r
+var\r
+ AX, AY, AZ: byte;\r
+ I: word;\r
+begin\r
+ mxInit;\r
+ mxSetMode( MX_320x240 );\r
+ Init;\r
+ Page := 240; (* Start with hidden page *)\r
+\r
+ AX := 0;\r
+ AY := 0;\r
+ AZ := 0;\r
+ (* Init 3D transforms, perspective is intentionally exaggerated *)\r
+ tdSetTranslation( Trans );\r
+ tdSetPerspective( 400*$10000, $10000, $10000 );\r
+ (* Main loop, all magic here! *)\r
+ while( not KeyPressed ) do begin\r
+ tdSetRotation( AX, AY, AZ ); (* Set new angles *)\r
+ tdTransform( Vtx, XVtx, 8 ); (* 3D transform points *)\r
+ tdTransformToImage( XVtx, VVtx, 8, 160, 120+Page );\r
+ Inc( AX, 1 ); (* Bump angles *)\r
+ Inc( AY, 1 );\r
+ Inc( AZ, 2 );\r
+ mxSetClipRegion( 0, Page, 320, 240 ); (* Set clip to new page *)\r
+ mxSetClip( TRUE );\r
+ mxFillBox( 0, Page, 320, 240, 0, OP_MOVE ); (* Clear screen *)\r
+ mxRotatePalette( @Palette[1], 192, 3 ); (* Rotate palette *)\r
+ (* Draw cube: backface culling is straighforward in this case, so *)\r
+ (* it can be handled by the polygon filling procedure *)\r
+ for I:=0 to 5 do\r
+ mxTexturePoly( 4, Face[I].Vtx, VVtx, Txts[I].Desc, Txts[I].Data, Txts[I].Width );\r
+ mxStartLine( Page ); (* Flip pages *)\r
+ mxSetPalette( @Palette[1], 1, 192 ); (* Set new palette *)\r
+ Page := 240-Page;\r
+ end;\r
+\r
+ mxSetMode( MX_TEXT );\r
+ mxTerm;\r
+end.\r