home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / programm.ing / spin3d.lzh / SPIN7.C < prev   
Encoding:
C/C++ Source or Header  |  1994-03-29  |  7.2 KB  |  266 lines

  1. /*   *** SPIN7.C ***
  2.      ***************
  3.  
  4.  
  5.     Will spin ==========>  A triangle in 3 space <===============
  6.              
  7.     
  8.       *** This will project 3 different views of the triangle
  9.           projects onto the xy,zy and xz PLANES!!!
  10.  
  11.      
  12.  
  13.  
  14.   
  15.  
  16.    March 28 ,1994  jeff bilger 
  17.    jbilger@cs.tamu.edu
  18.  
  19. */
  20.  
  21.  
  22. #include <math.h>
  23. #include <linea.h>
  24. #include <osbind.h>
  25. int pts[4][2] = { 
  26.     320, 050,
  27.     120, 150,
  28.     520, 150,
  29.     320, 050
  30. };
  31. lineaport *theport;
  32.  
  33. long mu_global_s();                     /* prototype, tells our 
  34.                                           Assembly language function
  35.                                            to return a long int  */
  36. long mu_global_c();  /* multiply 32 bit global w/ a precomputed sin or cos value */
  37.  
  38.  
  39. #define SCALE 2048L       /* so far 2048 is the upper bounds scale
  40.                               i can get W/O overflow */
  41. #define BITSH 11        /* what to shift (ie DIVIDE) by. this number
  42.                            2^11 = 2048 */
  43.  
  44.  
  45. typedef struct {long x,y,z;} point3d;   /* our faithful structure that defines a point in 3 space*/
  46.  
  47.           
  48. /* these are used to allocate size of arrays */
  49. #define NPTS   25                 /* max allowable points */
  50. #define NLINES 50                
  51. #define NFACES 50                
  52.  
  53.  
  54. #define I ( SCALE  )           /* set up a SCALE factor */
  55.  
  56. point3d point_[NPTS] =         /* define our triangle in 3d */
  57.         { 
  58.           I,I,I,     3*I,I,I, 2*I,I,-I,  2*I,-I,I/2,
  59.  },
  60.         drawpt1[NPTS],drawpt2[NPTS];     /* for fast draw/erasing look up */
  61.  
  62.  
  63.  
  64. int 
  65.     npts  = 4,         /* linefrom, lineto for drawing lines */                                                                                            
  66.     linefrom[NLINES] = {0,1,2,0,1,2},     /* set up lines to draw */
  67.     lineto[NLINES]   = {1,2,0,3,3,3}, 
  68.     nlines = 6,         /* number of lines */
  69.     x_offset=320,       /* for viewport mapping */
  70.     y_offset=100,
  71.     z_offset=1;
  72.  
  73. long cos_2=2048;    /* cos of 3 degrees * scale of 2048 */
  74. long sin_2=107;     /* sin of 3 degrees * scale of 2048 */
  75.  
  76.  
  77. main()
  78. {
  79. register int i,j;   /* use em for FOR loops */
  80. point3d pointi;     /* declare one instance of our point3d struct */
  81. int color =1;       /* color to draw triangle */
  82. char com;           /* for user input */
  83.  
  84.  
  85.  
  86.  
  87. puts("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
  88. puts("1-Spin Y  2-Spin X  3-Spin Z  9-Quit\n");
  89.  
  90.  
  91. theport = a_init();                   /* line a invokage */    
  92.     theport -> plane0 = 1;
  93.     theport -> plane1 = 0;
  94.     theport -> plane2 = 0;
  95.     theport -> plane3 = 0;
  96.  
  97.  
  98.    
  99. for(i=0;i<nlines;i++)  /* set up points-to-connect lookup table*/
  100. { drawpt1[i] = point_[linefrom[i]];
  101.   drawpt2[i] = point_[lineto[i]];  
  102. }
  103.  
  104. com = 0x32;                       /* set to spin about x */
  105.  
  106. while( com != 0x39)
  107. {
  108. if( Bconstat(2) )       /* if keypress */
  109.  com = Bconin(2);       /* get input */
  110.  
  111.  
  112.                                   /* The main loop */
  113.  for(i=0;i<npts;i++)
  114.    {
  115.    pointi = point_[i];        /* get current point data. We do this for efficiency, since we will use this value many times within one loop
  116.                                  it's more efficient to compute it's value only once */
  117.    
  118.     if(com == 0x31) {              /* spin y */
  119.                point_[i].x =( mu_global_c(pointi.x)  -
  120.                               mu_global_s(pointi.z))>>BITSH; /* since our points are scaled AND or trig angles, sin&cos values are scaled we must divide by scale once here*/
  121.                point_[i].z =( mu_global_s(pointi.x) +
  122.                               mu_global_c(pointi.z))>>BITSH;
  123.  
  124.                     }
  125.     if(com == 0x32) {              /* spin x */
  126.                 point_[i].y =( mu_global_c(pointi.y)  +
  127.                                mu_global_s(pointi.z))>>BITSH;
  128.                 point_[i].z =( -(mu_global_s(pointi.y)) +
  129.                                  mu_global_c(pointi.z))>>BITSH;
  130.                     }
  131.     if(com == 0x33) {              /* spin z */
  132.                 point_[i].x =( mu_global_c(pointi.x)  +
  133.                                mu_global_s(pointi.y))>>BITSH;
  134.                 point_[i].y =( mu_global_c(pointi.y)  -
  135.                                mu_global_s(pointi.x))>>BITSH;
  136.                     }
  137.   
  138.  
  139.    }
  140.  
  141.                               /* draw and erase triangle */
  142.   for(i=0;i<nlines;i++)
  143.    { draw3dline(drawpt1[i],drawpt2[i],0);  /*erase */
  144.      draw3dline(drawpt1[i]=point_[linefrom[i]],drawpt2[i]=point_[lineto[i]],color);
  145.      /* draw it */
  146.   }
  147.    
  148.  
  149. }/* end of while */
  150.  
  151.  
  152.  
  153. } /* end of main */
  154.  
  155.  
  156.  
  157. /*******************************/
  158.  
  159. draw3dline(p1,p2,color)
  160. point3d p1,p2;
  161. int color;
  162. {
  163.  int x1,y1,x2,y2,z1,z2;
  164.  
  165. theport -> plane0 = color;
  166.  
  167.  /* project onto the xy plane */
  168.  x1 = (p1.x>>BITSH-4) + x_offset;      
  169.  y1 = (p1.y>>BITSH-4) + y_offset;
  170.  x2 = (p2.x>>BITSH-4) + x_offset;
  171.  y2 = (p2.y>>BITSH-4) + y_offset;
  172.  
  173.  
  174. /* project onto xy plane */
  175. a_line(x1,y1,x2,y2); 
  176.  
  177.  
  178. /* project onto yz  plane */
  179. z1 = (p1.z>>BITSH-4) + x_offset;
  180. z2 = (p2.z>>BITSH-4) + x_offset;
  181. a_line(z1-100,y1,z2-100,y2);
  182.  
  183. /* project onto the xz plane */
  184. z1 = (p1.z>>BITSH-4) + y_offset;
  185. z2 = (p2.z>>BITSH-4) + y_offset;
  186. a_line(x1+100,z1,x2+100,z2);
  187.  
  188.  
  189.  
  190.  
  191. }
  192.  
  193.  
  194.  
  195. /***************************************************************/
  196. /* We will now modify the assembly mult. routine to be as efficient
  197.    as possible. We will make 2 mult. routines, one to multiply 
  198.    by a precomputed SINE value, and one to multiply by a precomputed
  199.    COSINE value
  200.    ************************************************************/
  201.  
  202. /*******************************************************/
  203. /* Multiply 1 16 bit(GLOBAL) SIGNED number b by a precomputed 
  204.    SINE value of 3 degrees (then the sine value was * SCALE where
  205.    scale was 2048 )
  206.  
  207.   and return the 32 bit result in D0
  208.  
  209.    *** BIG NOTE:: b MUST be a GLOBAL var!!!!!!!!!!!!!!!!!
  210.  
  211.    * WARNING * No test is made on the overflow bit (V) to see if 
  212.                the result in c is indeed correct. 
  213.  
  214.    Send arguments to this prodecure by: c=multiply(b) 
  215. */
  216.  
  217. long mu_global_s(b)
  218. register long b;  /* extern variables so b is placed in D7,*/          
  219. {
  220.  asm
  221.     { 
  222.   
  223.       muls   #107,b    /* generates  muls #107,d7 */
  224.       move.l b,D0  /* generates   move.l A7,(A5) */
  225.                     /* Note: You MUST specify the size (.l) cause
  226.                              if you just write 'move b,d0' Laser
  227.                              C will assume move.w as the default */
  228.      
  229.     }
  230.  
  231. }
  232.  
  233. /*************************************************
  234.  multiplies a number 'b' by a precomputed COSINE value
  235.  The Cos value is ===> cosine of 2 degrees * SCALE factor
  236.  where scale factor is defined. In our case the precomputed cos value 
  237.  is 2045 */
  238.  
  239. long mu_global_c(b)
  240. register long b;  /* extern variable so b is placed in D7*/
  241. {
  242.  asm
  243.     { 
  244.   
  245.       muls   #2045,b    /* generates  muls #2045,d7 */
  246.       move.l b,D0  /* generates   move.l A7,(A5) */
  247.                     /* Note: You MUST specify the size (.l) cause
  248.                              if you just write 'move b,d0' Laser
  249.                              C will assume move.w as the default */
  250.      
  251.     }
  252.  
  253. }
  254.  
  255.  
  256.  
  257. di(a,b)
  258. register long int a,b;
  259. {
  260.  asm
  261.     {
  262.      move.l a,D0
  263.      move.l b,D1
  264.     }
  265. }
  266.