home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume38 / tessel / part02 / main.c < prev    next >
C/C++ Source or Header  |  1993-06-21  |  4KB  |  135 lines

  1. /*+-----------------------------------------------------------------------+
  2.  *| This file contains subroutines used to demonstrate how polygons may   |
  3.  *| may be rendered by tesselation and smooth shading.                    |
  4.  *|                                                                       |
  5.  *| Author: Michael S. A. Robb         Version: 1.2        Date: 16/06/93 |
  6.  *+-----------------------------------------------------------------------+
  7.  */
  8.  
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <conio.h>
  12. #include <math.h>
  13. #include <time.h>
  14.  
  15. #include "24bit.h"
  16.  
  17. #define POLY_WIDTH  20
  18. #define POLY_HEIGHT 20
  19.  
  20. #define POLY_NUMBER 1000
  21.  
  22. /*+-----------------------------------------------------------------------+
  23.  *| This subroutine is used to time the speed of the rendering software.  |
  24.  *+-----------------------------------------------------------------------+
  25.  */
  26.  
  27. void polygon_demo1()
  28.   {
  29.   COORD vlist[3];
  30.   int   n, m;
  31.   long  start, finish;
  32.  
  33.   GRAPHICS_OPEN();
  34.  
  35.   polygon_setproc( render_triangle );      /* Call-back routine. */
  36.  
  37.   start = time( 0L );                      /* Starting time */
  38.  
  39.   for ( n = 0; n < POLY_NUMBER; n++ )      /* For all the polygons */
  40.     {
  41.     vlist[0].c_xpos = random(screen_width -(POLY_WIDTH  <<1) ) + POLY_WIDTH;
  42.     vlist[0].c_ypos = random(screen_height-(POLY_HEIGHT <<1) ) + POLY_HEIGHT;
  43.  
  44.     vlist[1].c_xpos = vlist[0].c_xpos + random(POLY_WIDTH  <<1 ) - POLY_WIDTH;
  45.     vlist[1].c_ypos = vlist[0].c_ypos + random(POLY_HEIGHT <<1) - POLY_HEIGHT;
  46.  
  47.     vlist[2].c_xpos = vlist[0].c_xpos + random(POLY_WIDTH  <<1 ) - POLY_WIDTH;
  48.     vlist[2].c_ypos = vlist[0].c_ypos + random(POLY_HEIGHT <<1 ) - POLY_HEIGHT;
  49.  
  50.     for ( m = 0; m < 3; m++ )           /* For each vertex. */
  51.       {
  52.       vlist[m].c_zpos  = 0;
  53.       vlist[m].c_red   = random( 256 ); /* Choose random colours */
  54.       vlist[m].c_green = random( 256 );
  55.       vlist[m].c_blue  = random( 256 );
  56.       vlist[m].c_blend = 0;
  57.       }
  58.  
  59.     polygon_tesselate( 3, vlist );
  60.     }
  61.  
  62.   finish = time( 0L ) - start; /* Number of seconds that have elapsed. */
  63.  
  64.   GRAPHICS_CLOSE();
  65.  
  66.   printf( "For %d polygons, %lf seconds",     /* Display results */
  67.           POLY_NUMBER,
  68.           (double) POLY_NUMBER / finish );
  69.   }
  70.  
  71. /*+-----------------------------------------------------------------------+
  72.  *| This subroutine is used to generate a random polygon.                 |
  73.  *+-----------------------------------------------------------------------+
  74.  */
  75.  
  76. void polygon_random( num, vlist )
  77.   int num;
  78.   COORD *vlist;
  79.   {
  80.   int    n;
  81.   double angle, radius, mult = M_PI * 2.0 / num;
  82.   static int col = 0;
  83.  
  84.   randomize();
  85.  
  86.   col = ( ++col == 8 ? 1 : col );
  87.  
  88.   for ( n = 0; n < num; n++, vlist++ )
  89.     {
  90.     angle            = mult * n;
  91.     radius           = random( 80 ) + 45;
  92.  
  93.     vlist -> c_xpos  = 256 + (long)( cos( angle ) * radius * 2 );
  94.     vlist -> c_ypos  = 128 + (long)( sin( angle ) * radius );
  95.     vlist -> c_zpos  = 0;
  96.  
  97.     vlist -> c_red   = random( 256 ) * (  col     & 0x01 );
  98.     vlist -> c_green = random( 256 ) * ( (col>>1) & 0x01 );
  99.     vlist -> c_blue  = random( 256 ) * ( (col>>2) & 0x01 );
  100.  
  101.     vlist -> c_blend = 0;
  102.     }
  103.   }
  104.  
  105. #define NUM_POINTS    16
  106. #define POLY          polygon
  107.  
  108. void polygon_demo2( void )
  109.   {
  110.   COORD   polygon[NUM_POINTS];             /* Used for random polygons. */
  111.  
  112.   GRAPHICS_OPEN();
  113.  
  114.   polygon_setproc( render_triangle );      /* Call-back routine. */
  115.  
  116.   while ( !kbhit() )
  117.     {
  118.     polygon_random(    NUM_POINTS, POLY );
  119.  
  120.     polygon_tesselate( NUM_POINTS, POLY ); /* Render the polygon. */
  121.     }
  122.  
  123.   GRAPHICS_CLOSE();
  124.   }
  125.  
  126. /*+-----------------------------------------------------------------------+
  127.  *| This is the first subroutine to be executed.                          |
  128.  *+-----------------------------------------------------------------------+
  129.  */
  130.  
  131. void main( void )
  132.   {
  133.   polygon_demo1();
  134.   }
  135.