home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / newopg.zip / OLYMPIC.C < prev    next >
Text File  |  1995-03-04  |  8KB  |  363 lines

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <math.h>
  4. #include <sys/types.h>
  5. //#include <sys/time.h>
  6. #include "tk.h"
  7.  
  8.  
  9.  
  10. #define XSIZE    100
  11. #define YSIZE    75
  12.  
  13. #define RINGS 5
  14. #define BLUERING 0
  15. #define BLACKRING 1
  16. #define REDRING 2
  17. #define YELLOWRING 3
  18. #define GREENRING 4
  19.  
  20. #define BACKGROUND 8
  21.  
  22. enum {
  23.     BLACK = 0,
  24.     RED,
  25.     GREEN,
  26.     YELLOW,
  27.     BLUE,
  28.     MAGENTA,
  29.     CYAN,
  30.     WHITE
  31. };
  32.  
  33.  
  34. GLenum rgb, doubleBuffer, directRender;
  35.  
  36. unsigned char rgb_colors[RINGS][3];
  37. int mapped_colors[RINGS];
  38. float dests[RINGS][3];
  39. float offsets[RINGS][3];
  40. float angs[RINGS];
  41. float rotAxis[RINGS][3];
  42. int iters[RINGS];
  43. GLuint theTorus;
  44.  
  45. static int seed = 42;
  46.  
  47. void mysrand(int i)
  48. {
  49.     seed = i;
  50. }
  51.  
  52. int myrand()
  53. {
  54.     return seed = (seed*7621 + 1)&0xffff;
  55. }
  56.  
  57. void FillTorus(float rc, int numc, float rt, int numt)
  58. {
  59.     int i, j, k;
  60.     double s, t;
  61.     double x, y, z;
  62.     double pi, twopi;
  63.  
  64.     pi = 3.14159265358979323846;
  65.     twopi = 2 * pi;
  66.  
  67.     for (i = 0; i < numc; i++) {
  68.     glBegin(GL_QUAD_STRIP);
  69.         for (j = 0; j <= numt; j++) {
  70.         for (k = 1; k >= 0; k--) {
  71.         s = (i + k) % numc + 0.5;
  72.         t = j % numt;
  73.  
  74.         x = cos(t*twopi/numt) * cos(s*twopi/numc);
  75.         y = sin(t*twopi/numt) * cos(s*twopi/numc);
  76.         z = sin(s*twopi/numc);
  77.         glNormal3f(x, y, z);
  78.  
  79.         x = (rt + rc * cos(s*twopi/numc)) * cos(t*twopi/numt);
  80.         y = (rt + rc * cos(s*twopi/numc)) * sin(t*twopi/numt);
  81.         z = rc * sin(s*twopi/numc);
  82.         glVertex3f(x, y, z);
  83.         }
  84.         }
  85.     glEnd();
  86.     }
  87. }
  88.  
  89. float Clamp(int iters_left, float t)
  90. {
  91.  
  92.     if (iters_left < 3) {
  93.     return 0.0;
  94.     }
  95.     return (iters_left-2)*t/iters_left;
  96. }
  97.  
  98. void DrawScene(void)
  99. {
  100.     int i, j;
  101.     GLboolean goIdle;
  102.  
  103.     goIdle = GL_TRUE;
  104.     for (i = 0; i < RINGS; i++) {
  105.     if (iters[i]) {
  106.         for (j = 0; j < 3; j++) {
  107.         offsets[i][j] = Clamp(iters[i], offsets[i][j]);
  108.         }
  109.         angs[i] = Clamp(iters[i], angs[i]);
  110.         iters[i]--;
  111.         goIdle = GL_FALSE;
  112.     }
  113.     }
  114.     if (goIdle) {
  115.        tkIdleFunc(NULL);
  116.     }
  117.  
  118.     glPushMatrix();
  119.     
  120.     glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  121.     gluLookAt(0.,0.,10., 0.,0.,0., 0.,1.,0.);
  122.  
  123.     for (i = 0; i < RINGS; i++) {
  124.     if (rgb) {
  125.         glColor3ubv(rgb_colors[i]);
  126.     } else {
  127.         glIndexi(mapped_colors[i]);
  128.     }
  129.     glPushMatrix();
  130.     glTranslatef(dests[i][0]+offsets[i][0], dests[i][1]+offsets[i][1],
  131.              dests[i][2]+offsets[i][2]);
  132.     glRotatef(angs[i], rotAxis[i][0], rotAxis[i][1], rotAxis[i][2]);
  133.     glCallList(theTorus);
  134.     glPopMatrix();
  135.     }
  136.  
  137.     glPopMatrix();
  138.  
  139.     glFlush();
  140.     if (doubleBuffer) {
  141.     tkSwapBuffers();
  142.     }
  143. }
  144.  
  145. float MyRand(void)
  146. {
  147.    float boo;
  148.  
  149.     return 10.0 * ((float)myrand()/(float)0xffff - 0.5);
  150. //    boo = (float) (random());
  151. //    return  10.0 * boo; 
  152. }
  153.  
  154. void ReInit(void)
  155. {
  156.     int i;
  157.     float deviation;
  158.  
  159.     deviation = MyRand() / 2;
  160.     deviation = deviation * deviation;
  161.     for (i = 0; i < RINGS; i++) {
  162.     offsets[i][0] = MyRand();
  163.     offsets[i][1] = MyRand();
  164.     offsets[i][2] = MyRand();
  165.     angs[i] = 260.0 * MyRand();
  166.     rotAxis[i][0] = MyRand();
  167.     rotAxis[i][1] = MyRand();
  168.     rotAxis[i][2] = MyRand();
  169.     iters[i] = (int)(deviation * MyRand() + 60.0);
  170. //    iters[i] = (int)((int)deviation*random()+60);
  171.     }
  172.     tkIdleFunc(DrawScene);
  173. }
  174.  
  175. void Init(void)
  176. {
  177.     int gid;
  178.     float base, height;
  179.     float aspect, x, y;
  180.     int i;
  181.     float sc = 10;
  182.     float top_y = 1.0;
  183.     float bottom_y = 0.0;
  184.     float top_z = 0.15;
  185.     float bottom_z = 0.69;
  186.     float spacing = 2.5;
  187.     static float lmodel_ambient[] = {0.0, 0.0, 0.0, 0.0};
  188.     static float lmodel_twoside[] = {GL_FALSE};
  189.     static float lmodel_local[] = {GL_FALSE};
  190.     static float light0_ambient[] = {0.1, 0.1, 0.1, 1.0};
  191.     static float light0_diffuse[] = {1.0, 1.0, 1.0, 0.0};
  192.     static float light0_position[] = {0.8660254, 0.5, 1, 0};
  193.     static float light0_specular[] = {1.0, 1.0, 1.0, 0.0};
  194.     static float bevel_mat_ambient[] = {0.0, 0.0, 0.0, 1.0};
  195.     static float bevel_mat_shininess[] = {40.0};
  196.     static float bevel_mat_specular[] = {1.0, 1.0, 1.0, 0.0};
  197.     static float bevel_mat_diffuse[] = {1.0, 0.0, 0.0, 0.0};
  198.  
  199.     ReInit();
  200.     for (i = 0; i < RINGS; i++) {
  201.     rgb_colors[i][0] = rgb_colors[i][1] = rgb_colors[i][2] = 0;
  202.     }
  203.     rgb_colors[BLUERING][2] = 255;
  204.     rgb_colors[REDRING][0] = 255;
  205.     rgb_colors[GREENRING][1] = 255;
  206.     rgb_colors[YELLOWRING][0] = 255;
  207.     rgb_colors[YELLOWRING][1] = 255;
  208.     mapped_colors[BLUERING] = BLUE;
  209.     mapped_colors[REDRING] = RED;
  210.     mapped_colors[GREENRING] = GREEN;
  211.     mapped_colors[YELLOWRING] = YELLOW;
  212.     mapped_colors[BLACKRING] = BLACK;
  213.  
  214.     dests[BLUERING][0] = -spacing;
  215.     dests[BLUERING][1] = top_y;
  216.     dests[BLUERING][2] = top_z;
  217.  
  218.     dests[BLACKRING][0] = 0.0;
  219.     dests[BLACKRING][1] = top_y;
  220.     dests[BLACKRING][2] = top_z;
  221.  
  222.     dests[REDRING][0] = spacing;
  223.     dests[REDRING][1] = top_y;
  224.     dests[REDRING][2] = top_z;
  225.  
  226.     dests[YELLOWRING][0] = -spacing / 2.0;
  227.     dests[YELLOWRING][1] = bottom_y;
  228.     dests[YELLOWRING][2] = bottom_z;
  229.  
  230.     dests[GREENRING][0] = spacing / 2.0;
  231.     dests[GREENRING][1] = bottom_y;
  232.     dests[GREENRING][2] = bottom_z;
  233.  
  234.     base = 2.0; 
  235.     height = 2.0;
  236.     theTorus = glGenLists(1);
  237.     glNewList(theTorus, GL_COMPILE);
  238.     FillTorus(0.1, 8, 1.0, 25);
  239.     glEndList();
  240.  
  241.     x = (float)XSIZE;
  242.     y = (float)YSIZE;
  243.     aspect = x / y;
  244.     glEnable(GL_CULL_FACE);
  245.     glCullFace(GL_BACK);
  246.     glEnable(GL_DEPTH_TEST);
  247.     glClearDepth(1.0);
  248.  
  249.     if (rgb) {
  250.     glClearColor(0.5, 0.5, 0.5, 0.0);
  251.     glLightfv(GL_LIGHT0, GL_AMBIENT, light0_ambient);
  252.     glLightfv(GL_LIGHT0, GL_DIFFUSE, light0_diffuse);
  253.     glLightfv(GL_LIGHT0, GL_SPECULAR, light0_specular);
  254.     glLightfv(GL_LIGHT0, GL_POSITION, light0_position);
  255.     glEnable(GL_LIGHT0);
  256.  
  257.     glLightModelfv(GL_LIGHT_MODEL_LOCAL_VIEWER, lmodel_local);
  258.     glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE, lmodel_twoside);
  259.     glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
  260.     glEnable(GL_LIGHTING);
  261.  
  262.     glMaterialfv(GL_FRONT, GL_AMBIENT, bevel_mat_ambient);
  263.     glMaterialfv(GL_FRONT, GL_SHININESS, bevel_mat_shininess);
  264.     glMaterialfv(GL_FRONT, GL_SPECULAR, bevel_mat_specular);
  265.     glMaterialfv(GL_FRONT, GL_DIFFUSE, bevel_mat_diffuse);
  266.  
  267.     glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE);
  268.     glEnable(GL_COLOR_MATERIAL);
  269.     glShadeModel(GL_SMOOTH);
  270.     } else {
  271.     glClearIndex(BACKGROUND);
  272.     glShadeModel(GL_FLAT);
  273.     }
  274.  
  275.     glMatrixMode(GL_PROJECTION);
  276.     gluPerspective(45., 1.33, 0.1, 100.0);
  277.     glMatrixMode(GL_MODELVIEW);
  278.     mysrand(35);
  279. }
  280.  
  281. void Reshape(int width, int height)
  282. {
  283.  
  284.     glViewport(0, 0, width, height);
  285. }
  286.  
  287. GLenum Key(int key, GLenum mask)
  288. {
  289.  
  290.     switch (key) {
  291.       case TK_B:
  292.     tkClipBoard();
  293.     break;
  294.       case TK_ESCAPE:
  295.     tkQuit();
  296.       case TK_SPACE:
  297.     ReInit();
  298.     break;
  299.       default:
  300.     return GL_FALSE;
  301.     }
  302.     return GL_TRUE;
  303. }
  304.  
  305. GLenum Args(int argc, char **argv)
  306. {
  307.     GLint i;
  308.  
  309.     rgb = GL_TRUE;
  310.     doubleBuffer = GL_FALSE;
  311.     directRender = GL_TRUE;
  312.  
  313.     for (i = 1; i < argc; i++) {
  314.     if (strcmp(argv[i], "-ci") == 0) {
  315.         rgb = GL_FALSE;
  316.     } else if (strcmp(argv[i], "-rgb") == 0) {
  317.         rgb = GL_TRUE;
  318.     } else if (strcmp(argv[i], "-sb") == 0) {
  319.         doubleBuffer = GL_FALSE;
  320.     } else if (strcmp(argv[i], "-db") == 0) {
  321.         doubleBuffer = GL_TRUE;
  322.     } else if (strcmp(argv[i], "-dr") == 0) {
  323.         directRender = GL_TRUE;
  324.     } else if (strcmp(argv[i], "-ir") == 0) {
  325.         directRender = GL_FALSE;
  326.     } else {
  327.         printf("%s (Bad option).\n", argv[i]);
  328.         return GL_FALSE;
  329.     }
  330.     }
  331.     return GL_TRUE;
  332. }
  333.  
  334. void main(int argc, char **argv)
  335. {
  336.     GLenum type;
  337.  
  338.     if (Args(argc, argv) == GL_FALSE) {
  339.     tkQuit();
  340.     }
  341.  
  342.     tkInitPosition(0, 0, 400, 300);
  343.  
  344.     type = TK_DEPTH;
  345.     type |= (rgb) ? TK_RGB : TK_INDEX;
  346.     type |= (doubleBuffer) ? TK_DOUBLE : TK_SINGLE;
  347.     type |= (directRender) ? TK_DIRECT : TK_INDIRECT;
  348.     tkInitDisplayMode(type);
  349.  
  350.     if (tkInitWindow("Olympic") == GL_FALSE) {
  351.         tkQuit();
  352.     }
  353.  
  354.     Init();
  355.  
  356.     tkExposeFunc(Reshape);
  357.     tkReshapeFunc(Reshape);
  358.     tkKeyDownFunc(Key);
  359.     tkIdleFunc(DrawScene);
  360.  
  361.     tkExec();
  362. }
  363.