home *** CD-ROM | disk | FTP | other *** search
/ Collection of Hack-Phreak Scene Programs / cleanhpvac.zip / cleanhpvac / TUTORC.ZIP / TUT14.C < prev    next >
C/C++ Source or Header  |  1994-10-30  |  14KB  |  601 lines

  1. /* 
  2.   tut14.c
  3.   10/30/94
  4.   from tutprog14.pas
  5.   Adapted from Denthor's tutprog14.pas
  6.   Translated into C, from Denthor's VGA Trainer, by
  7.   Steve Pinault, scp@ohm.att.com
  8.   Compiled with Microsoft Visual C++ 1.5 (Microsoft C 8.0)
  9.   To compile:
  10.   First compile the subroutines in tutsubs.c with the batch file 
  11.   cltutsub.bat
  12.   Then compile any of the tutor programs with the batch file
  13.   cltut.bat
  14.   Example: C:>cltutsub
  15.            C:>cltut tut14.c
  16.            to compile this program.
  17. */
  18.  
  19. #include "tutheadr.h"
  20.  
  21. #define maxpolys 6
  22.  
  23. int a[maxpolys][4][3] = 
  24. {
  25.          {{-10,-10,10},{-10,10,10},{10,10,10},{10,-10,10}},
  26.          {{-10,-10,-10},{-10,10,-10},{10,10,-10},{10,-10,-10}},
  27.          {{-10,-10,-10},{-10,10,-10},{-10,10,10},{-10,-10,10}},
  28.          {{10,-10,-10},{10,10,-10},{10,10,10},{10,-10,10}},
  29.          {{10,-10,10},{10,-10,-10},{-10,-10,-10},{-10,-10,10}},
  30.          {{10,10,10},{10,10,-10},{-10,10,-10},{-10,10,10}}
  31. };         
  32.         // The 3-D coordinates of our object ... stored as (X1,Y1,Z1), }
  33.         // (X2,Y2,Z2) ... for the 4 points of a poly }
  34.  
  35. struct IPoint
  36. {
  37.   int x;
  38.   int y;
  39.   int z;
  40. };  
  41. struct IPoint lines[maxpolys][4],translated[maxpolys][4];
  42. int lookup[360][2];  // sin and cos table
  43. int poly[200][2];
  44. int ytopclip,ybotclip;
  45. int xoff,yoff,zoff;
  46.  
  47. // {DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD}
  48. // Procedure Hline (x1,x2,y:word;col:byte;where:word); assembler;
  49. //  { This draws a horizontal line from x1 to x2 on line y in color col }
  50. // Version of Hline to add to the current screen color, for "glenzing"
  51. void Hline2(int x1, int x2, int y, char col, int where)
  52. {
  53. _asm
  54.  {
  55.   mov   ax,where
  56.   mov   es,ax
  57.   mov   ax,y
  58.   mov   di,ax
  59.   shl   ax,8
  60.   shl   di,6
  61.   add   di,ax
  62.   add   di,x1
  63.  
  64.   mov   cx,x2
  65.   sub   cx,x1
  66.   cmp   cx,0
  67.   jle   End
  68. Loop1 :
  69.   mov   al,es:[di]  ;  Read current color
  70.   add   al,col      ;  Add to new color
  71.   stosb             ;  Write new pixel
  72.   loop  Loop1
  73. End:  
  74.  }
  75. }    
  76.  
  77. ///////////////////////////////////////////////////////////////////////////
  78. //Procedure doside (x1,y1,x2,y2:integer);
  79. //  { This scans the side of a polygon and updates the poly variable }
  80. void doside(int x1,int y1,int x2,int y2)
  81. {
  82.   int temp,x,xinc,loop1;
  83.   
  84.   if(y1==y2)return;
  85.   if(y2<y1)
  86.   {
  87.     temp=y2;
  88.     y2=y1;
  89.     y1=temp;
  90.     temp=x2;
  91.     x2=x1;
  92.     x1=temp;
  93.   }
  94.   xinc=((x2-x1)<<7)/(y2-y1);
  95.   x=(x1<<7);
  96.   for(loop1=y1;loop1<=y2;loop1++)
  97.   {
  98.     if((loop1>ytopclip-1)&&(loop1<ybotclip+1))
  99.     {
  100.       if((x>>7)<poly[loop1][0]) poly[loop1][0]=(x>>7); // min
  101.       if((x>>7)>poly[loop1][1]) poly[loop1][1]=(x>>7); // max
  102.     }
  103.     x+=xinc;
  104.   }
  105. }
  106.  
  107. // {DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD}
  108. // Procedure DrawPoly(x1,y1,x2,y2,x3,y3,x4,y4:integer;color:byte;where:word);
  109. // This draw a polygon with 4 points at x1,y1 , x2,y2 , x3,y3 , x4,y4
  110. // in color col }
  111. void drawpoly(int x1,int y1,int x2,int y2,int x3,int y3,int x4,int y4,char color,int where)
  112. {
  113.   int miny,maxy,loop1;
  114.  
  115.   _asm
  116.   {
  117.     mov   si,offset poly
  118.     mov   cx,200
  119. Loop1:
  120.     mov   ax,32766
  121.     mov   ds:[si],ax
  122.     inc   si
  123.     inc   si
  124.     mov   ax,-32767
  125.     mov   ds:[si],ax
  126.     inc   si
  127.     inc   si
  128.     loop  Loop1
  129.    }       // { Setting the minx and maxx values to extremes }
  130.   miny=y1;
  131.   maxy=y1;
  132.   if (y2<miny)  miny=y2;
  133.   if (y3<miny)  miny=y3;
  134.   if (y4<miny)  miny=y4;
  135.   if (y2>maxy)  maxy=y2;
  136.   if (y3>maxy)  maxy=y3;
  137.   if (y4>maxy)  maxy=y4;
  138.   if (miny<ytopclip)  miny=ytopclip;
  139.   if (maxy>ybotclip)  maxy=ybotclip;
  140.   if ((miny>199) || (maxy<0)) return;
  141.  
  142.   doside (x1,y1,x2,y2);
  143.   doside (x2,y2,x3,y3);
  144.   doside (x3,y3,x4,y4);
  145.   doside (x4,y4,x1,y1);
  146.  
  147.   for( loop1= miny;loop1<=maxy;loop1++)
  148.     Hline2 (poly[loop1][0],poly[loop1][1],loop1,color,where);
  149.     // Hline (poly[loop1][0],poly[loop1][1],loop1,color,where);
  150. }
  151.  
  152. //{DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD}
  153. //Procedure SetUpPoints;
  154. //  { This creates the lookup table }
  155. void SetUpPoints()
  156. {
  157.   int loop1;
  158.   for(loop1=0;loop1<360;loop1++)
  159.   {
  160.     lookup[loop1][0]=round((float)sin((double)rad(loop1))*(float)16384);
  161.     lookup[loop1][1]=round((float)cos((double)rad(loop1))*(float)16384);
  162.   }
  163. }
  164.  
  165.  
  166. //{DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD}
  167. //Procedure RotatePoints (x,Y,z:Integer);
  168. //  { This rotates the objecct in lines to translated }
  169. void rotatepoints(int x, int y, int z)
  170. {
  171.   int loop1,loop2;
  172.   int a,b,c;
  173.   
  174.   for(loop1=0;loop1<maxpolys;loop1++)
  175.   {
  176.     for(loop2=0;loop2<4;loop2++)
  177.     {
  178.       b=lookup[y][1];
  179.       c=lines[loop1][loop2].x;
  180.       _asm
  181.       {
  182.         mov   ax,b
  183.         imul  c
  184.         sal   ax,1
  185.         rcl   dx,1
  186.         sal   ax,1
  187.         rcl   dx,1
  188.         mov   a,dx
  189.       }
  190.       b=lookup[y][0];
  191.       c=lines[loop1][loop2].z;
  192.       _asm
  193.       {
  194.         mov   ax,b
  195.         imul  c
  196.         sal   ax,1
  197.         rcl   dx,1
  198.         sal   ax,1
  199.         rcl   dx,1
  200.         add   a,dx
  201.       }
  202.       translated[loop1][loop2].x=a;
  203.       translated[loop1][loop2].y=lines[loop1][loop2].y;
  204.       b=-lookup[y][0];
  205.       c=lines[loop1][loop2].x;
  206.       _asm
  207.       {
  208.         mov   ax,b
  209.         imul  c
  210.         sal   ax,1
  211.         rcl   dx,1
  212.         sal   ax,1
  213.         rcl   dx,1
  214.         mov   a,dx
  215.       }
  216.       b=lookup[y][1];
  217.       c=lines[loop1][loop2].z;
  218.       _asm
  219.       {
  220.         mov   ax,b
  221.         imul  c
  222.         sal   ax,1
  223.         rcl   dx,1
  224.         sal   ax,1
  225.         rcl   dx,1
  226.         add   a,dx
  227.       }
  228.       translated[loop1][loop2].z=a;
  229.  
  230.  
  231.       if(x!=0)
  232.       {
  233.         b=lookup[x][1];
  234.         c=translated[loop1][loop2].y;
  235.         _asm
  236.         {
  237.           mov   ax,b
  238.           imul  c
  239.           sal   ax,1
  240.           rcl   dx,1
  241.           sal   ax,1
  242.           rcl   dx,1
  243.           mov   a,dx
  244.         }
  245.         b=lookup[x][0];
  246.         c=translated[loop1][loop2].z;
  247.         _asm
  248.         {
  249.           mov   ax,b
  250.           imul  c
  251.           sal   ax,1
  252.           rcl   dx,1
  253.           sal   ax,1
  254.           rcl   dx,1
  255.           sub   a,dx
  256.         }
  257.         b=lookup[x][0];
  258.         c=translated[loop1][loop2].y;
  259.         translated[loop1][loop2].y=a;
  260.         _asm
  261.         {
  262.           mov   ax,b
  263.           imul  c
  264.           sal   ax,1
  265.           rcl   dx,1
  266.           sal   ax,1
  267.           rcl   dx,1
  268.           mov   a,dx
  269.         }
  270.         b=lookup[x][1];
  271.         c=translated[loop1][loop2].z;
  272.         _asm
  273.         {
  274.           mov   ax,b
  275.           imul  c
  276.           sal   ax,1
  277.           rcl   dx,1
  278.           sal   ax,1
  279.           rcl   dx,1
  280.           add   a,dx
  281.         }
  282.         translated[loop1][loop2].z=a;
  283.       }
  284.  
  285.  
  286.  
  287.       if(z!=0)
  288.       {
  289.         b=lookup[z][1];
  290.         c=translated[loop1][loop2].x;
  291.         _asm
  292.         {
  293.           mov   ax,b
  294.           imul  c
  295.           sal   ax,1
  296.           rcl   dx,1
  297.           sal   ax,1
  298.           rcl   dx,1
  299.           mov   a,dx
  300.         }
  301.         b=lookup[z][0];
  302.         c=translated[loop1][loop2].y;
  303.         _asm
  304.         {
  305.           mov   ax,b
  306.           imul  c
  307.           sal   ax,1
  308.           rcl   dx,1
  309.           sal   ax,1
  310.           rcl   dx,1
  311.           sub   a,dx
  312.         }
  313.         b=lookup[z][0];
  314.         c=translated[loop1][loop2].x;
  315.         translated[loop1][loop2].x=a;
  316.         _asm
  317.         {
  318.           mov   ax,b
  319.           imul  c
  320.           sal   ax,1
  321.           rcl   dx,1
  322.           sal   ax,1
  323.           rcl   dx,1
  324.           mov   a,dx
  325.         }
  326.         b=lookup[z][1];
  327.         c=translated[loop1][loop2].y;
  328.         _asm
  329.         {
  330.           mov   ax,b
  331.           imul  c
  332.           sal   ax,1
  333.           rcl   dx,1
  334.           sal   ax,1
  335.           rcl   dx,1
  336.           add   a,dx
  337.         }
  338.         translated[loop1][loop2].y=a;
  339.       }
  340.     }
  341.   }
  342. }
  343.  
  344.  
  345. //{DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD}
  346. //  Procedure DrawPoints;
  347. //  { This draws the translated object to the virtual screen }
  348. void drawpoints()
  349. {
  350.   int loop1;
  351.   int temp;
  352.   int nx;
  353.   int tx1,ty1,tx2,ty2,tx3,ty3,tx4,ty4;
  354.   
  355.   for(loop1=0;loop1<maxpolys;loop1++)
  356.   {
  357.     if((translated[loop1][0].z+zoff<0)&&(translated[loop1][1].z+zoff<0)
  358.      &&(translated[loop1][2].z+zoff<0)&&(translated[loop1][3].z+zoff<0))
  359.      {
  360.       temp=round (translated[loop1][0].z)+zoff;
  361.       nx=translated[loop1][0].x;
  362.       _asm
  363.       {
  364.         mov   ax,nx
  365.         mov   dx,ax
  366.         sal   ax,8
  367.         sar   dx,8
  368.         idiv  temp
  369.         add   ax,160
  370.         mov   nx,ax
  371.       }
  372.       tx1=nx;
  373.       nx=translated[loop1][0].y;
  374.       _asm
  375.       {
  376.         mov   ax,nx
  377.         mov   dx,ax
  378.         sal   ax,8
  379.         sar   dx,8
  380.         idiv  temp
  381.         add   ax,100
  382.         mov   nx,ax
  383.       }
  384.       ty1=nx;
  385.  
  386.  
  387.       temp=round (translated[loop1][1].z)+zoff;
  388.       nx=translated[loop1][1].x;
  389.       _asm
  390.       {
  391.         mov   ax,nx
  392.         mov   dx,ax
  393.         sal   ax,8
  394.         sar   dx,8
  395.         idiv  temp
  396.         add   ax,160
  397.         mov   nx,ax
  398.       }
  399.       tx2=nx;
  400.       nx=translated[loop1][1].y;
  401.       _asm
  402.       {
  403.         mov   ax,nx
  404.         mov   dx,ax
  405.         sal   ax,8
  406.         sar   dx,8
  407.         idiv  temp
  408.         add   ax,100
  409.         mov   nx,ax
  410.       }
  411.       ty2=nx;
  412.  
  413.  
  414.       temp=round (translated[loop1][2].z)+zoff;
  415.       nx=translated[loop1][2].x;
  416.       _asm
  417.       {
  418.         mov   ax,nx
  419.         mov   dx,ax
  420.         sal   ax,8
  421.         sar   dx,8
  422.         idiv  temp
  423.         add   ax,160
  424.         mov   nx,ax
  425.       }
  426.       tx3=nx;
  427.       nx=translated[loop1][2].y;
  428.       _asm
  429.       {
  430.         mov   ax,nx
  431.         mov   dx,ax
  432.         sal   ax,8
  433.         sar   dx,8
  434.         idiv  temp
  435.         add   ax,100
  436.         mov   nx,ax
  437.       }
  438.       ty3=nx;
  439.  
  440.  
  441.       temp=round (translated[loop1][3].z)+zoff;
  442.       nx=translated[loop1][3].x;
  443.       _asm
  444.       {
  445.         mov   ax,nx
  446.         mov   dx,ax
  447.         sal   ax,8
  448.         sar   dx,8
  449.         idiv  temp
  450.         add   ax,160
  451.         mov   nx,ax
  452.       }
  453.       tx4=nx;
  454.       nx=translated[loop1][3].y;
  455.       _asm
  456.       {
  457.         mov   ax,nx
  458.         mov   dx,ax
  459.         sal   ax,8
  460.         sar   dx,8
  461.         idiv  temp
  462.         add   ax,100
  463.         mov   nx,ax
  464.       }
  465.       ty4=nx;
  466.  
  467.       drawpoly (tx1,ty1,tx2,ty2,tx3,ty3,tx4,ty4,(char)(loop1),Vaddr);
  468.     }
  469.   }
  470. }      
  471.  
  472. // {DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD}
  473. // Procedure MoveAround;
  474. //{ This is the main display procedure. }
  475. void movearound()
  476. {
  477.   int deg,loop1,loop2;
  478.   int degincr=2;  // degincr must divide 360!
  479.   int clipincr=1; // clipincr must divide 100;
  480.   int ch;
  481.  
  482.   for(loop1=1;loop1<16;loop1++)
  483.     Pal((char)loop1,(char)(loop1*4+3),0,(char)(63-(loop1*4+3)));
  484.   Pal(100,50,50,50);
  485.  
  486.   deg=0;    
  487.   Cls (0,Vaddr);
  488.   for(loop1=0;loop1<maxpolys;loop1++)
  489.     for(loop2=0;loop2<4;loop2++)
  490.     {
  491.       lines[loop1][loop2].x=a[loop1][loop2][0]*8;
  492.       lines[loop1][loop2].y=a[loop1][loop2][1]*8;
  493.       lines[loop1][loop2].z=a[loop1][loop2][2]*8;
  494.     }
  495.      
  496.   Cls(0,Vaddr);
  497.   Cls(0,VGA);
  498.   xoff=160;
  499.   yoff=100;
  500.   zoff=-500;
  501.   
  502.   ytopclip=101;
  503.   ybotclip=100;
  504.   Line2(0,100,319,100,100,VGA);
  505.   delay (500);
  506.   for(loop1=0;loop1<100/clipincr;loop1++)
  507.   {
  508.     rotatepoints(deg,deg,deg);
  509.     drawpoints();
  510.     Line2(0,ytopclip,319,ytopclip,100,Vaddr);
  511.     Line2(0,ybotclip,319,ybotclip,100,Vaddr);
  512.     WaitRetrace();
  513.     Flip2(Vaddr,VGA);
  514.     Cls (0,Vaddr);
  515.     deg+=degincr; if(deg==360)deg=0; // Be sure decincr divides 360!
  516.     ytopclip=ytopclip-clipincr;
  517.     ybotclip=ybotclip+clipincr;
  518.   }
  519.   while(1)
  520.   {
  521.     rotatepoints(deg,deg,deg);
  522.     drawpoints();
  523.     Line2(0,0,319,0,100,Vaddr);
  524.     Line2(0,199,319,199,100,Vaddr);
  525.     WaitRetrace();
  526.     Flip2(Vaddr,VGA);
  527.     Cls (0,Vaddr);
  528.     deg+=degincr; if(deg==360)deg=0;
  529.     if(_bios_keybrd(_KEYBRD_READY))
  530.     {
  531.       ch=(0x00ff&getch());
  532.       if(ch==0x1b)break;
  533.     }
  534.   }
  535.   for(loop1=0;loop1<100/clipincr;loop1++)
  536.   {
  537.     ytopclip=ytopclip+clipincr;
  538.     ybotclip=ybotclip-clipincr;
  539.     rotatepoints(deg,deg,deg);
  540.     drawpoints();
  541.     Line2(0,ytopclip,319,ytopclip,100,Vaddr);
  542.     Line2(0,ybotclip,319,ybotclip,100,Vaddr);
  543.     WaitRetrace();
  544.     Flip2(Vaddr,VGA);
  545.     Cls (0,Vaddr);
  546.     deg+=degincr; if(deg==360)deg=0;
  547.   }
  548. }
  549.  
  550. void main()
  551. {
  552.   SetUpVirtual();
  553.   SetMCGA();
  554.   SetUpPoints();
  555.   movearound();
  556.   SetText();
  557. }
  558.  
  559.   
  560. /*
  561. BEGIN
  562.   clrscr;
  563.   writeln ('Welcome to the fourteenth trainer! This one is on glenzing, and also');
  564.   writeln ('throws in a faster poly, fixed point math and a lot more assembler.');
  565.   writeln;
  566.   Writeln ('This isn''t very interactive ... hit any key to start, and then');
  567.   writeln ('hit the [ESC] key to exit. It is a glenzed cube spinning in the');
  568.   writeln ('middle of the screen. Read the text file for more information on');
  569.   writeln ('how the fixed point etc. works ... it will also help a lot if you');
  570.   writeln ('compare it with TUTPROG9.PAS, as this is the same 3D system, just');
  571.   writeln ('speeded up.');
  572.   writeln;
  573.   writeln;
  574.   writeln;
  575.   write ('Hit any key to continue ...');
  576.   readkey;
  577.   SetUpVirtual;
  578.   SetMCGA;
  579.   SetUpPoints;
  580.   MoveAround;
  581.   SetText;
  582.   ShutDown;
  583.   Writeln ('All done. This concludes the fourteenth sample program in the ASPHYXIA');
  584.   Writeln ('Training series. You may reach DENTHOR under the names of GRANT');
  585.   Writeln ('SMITH/DENTHOR/ASPHYXIA on the ASPHYXIA BBS.I also occasinally');
  586.   Writeln ('RSAProg, comp.lang.pascal and comp.sys.ibm.pc.demos. E-mail me at :');
  587.   Writeln ('    smith9@batis.bis.und.ac.za');
  588.   Writeln ('The numbers are available in the main text. You may also write to me at:');
  589.   Writeln ('             Grant Smith');
  590.   Writeln ('             P.O. Box 270');
  591.   Writeln ('             Kloof');
  592.   Writeln ('             3640');
  593.   Writeln ('             Natal');
  594.   Writeln ('             South Africa');
  595.   Writeln ('I hope to hear from you soon!');
  596.   Writeln; Writeln;
  597.   Write   ('Hit any key to exit ...');
  598.   readkey;
  599. END.
  600. */
  601.