home *** CD-ROM | disk | FTP | other *** search
/ Amiga MA Magazine 1998 #6 / amigamamagazinepolishissue1998.iso / coders / mesa-1.2.8 / samples / olympic.c < prev    next >
C/C++ Source or Header  |  1996-05-27  |  9KB  |  383 lines

  1. /*
  2.  * Copyright (c) 1991, 1992, 1993 Silicon Graphics, Inc.
  3.  *
  4.  * Permission to use, copy, modify, distribute, and sell this software and
  5.  * its documentation for any purpose is hereby granted without fee, provided
  6.  * that (i) the above copyright notices and this permission notice appear in
  7.  * all copies of the software and related documentation, and (ii) the name of
  8.  * Silicon Graphics may not be used in any advertising or
  9.  * publicity relating to the software without the specific, prior written
  10.  * permission of Silicon Graphics.
  11.  *
  12.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF
  13.  * ANY KIND,
  14.  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
  15.  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  16.  *
  17.  * IN NO EVENT SHALL SILICON GRAPHICS BE LIABLE FOR
  18.  * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  19.  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  20.  * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
  21.  * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  22.  * OF THIS SOFTWARE.
  23.  */
  24.  
  25. /*
  26.  * Nov 20, 1995 use stdlib's rand()/srand() instead of random()/srand48(), etc.
  27.  */
  28.  
  29.  
  30. #define _HPUX_SOURCE
  31.  
  32. #include <stdio.h>
  33. #include <stdlib.h>
  34. #include <string.h>
  35. #include <math.h>
  36. #include <sys/types.h>
  37. #include <sys/time.h>
  38. #include "gltk.h"
  39.  
  40. #ifndef RAND_MAX
  41. #  define RAND_MAX 32767
  42. #endif
  43.  
  44.  
  45. #define XSIZE    100
  46. #define YSIZE    75
  47.  
  48. #define RINGS 5
  49. #define BLUERING 0
  50. #define BLACKRING 1
  51. #define REDRING 2
  52. #define YELLOWRING 3
  53. #define GREENRING 4
  54.  
  55. #define BACKGROUND 8
  56.  
  57. enum {
  58.     BLACK = 0,
  59.     RED,
  60.     GREEN,
  61.     YELLOW,
  62.     BLUE,
  63.     MAGENTA,
  64.     CYAN,
  65.     WHITE
  66. };
  67.  
  68.  
  69. GLenum rgb, doubleBuffer, directRender;
  70.  
  71. unsigned char rgb_colors[RINGS][3];
  72. int mapped_colors[RINGS];
  73. float dests[RINGS][3];
  74. float offsets[RINGS][3];
  75. float angs[RINGS];
  76. float rotAxis[RINGS][3];
  77. int iters[RINGS];
  78. GLuint theTorus;
  79.  
  80.  
  81. void FillTorus(float rc, int numc, float rt, int numt)
  82. {
  83.     int i, j, k;
  84.     double s, t;
  85.     double x, y, z;
  86.     double pi, twopi;
  87.  
  88.     pi = 3.14159265358979323846;
  89.     twopi = 2 * pi;
  90.  
  91.     for (i = 0; i < numc; i++) {
  92.     glBegin(GL_QUAD_STRIP);
  93.         for (j = 0; j <= numt; j++) {
  94.         for (k = 1; k >= 0; k--) {
  95.         s = (i + k) % numc + 0.5;
  96.         t = j % numt;
  97.  
  98.         x = cos(t*twopi/numt) * cos(s*twopi/numc);
  99.         y = sin(t*twopi/numt) * cos(s*twopi/numc);
  100.         z = sin(s*twopi/numc);
  101.         glNormal3f(x, y, z);
  102.  
  103.         x = (rt + rc * cos(s*twopi/numc)) * cos(t*twopi/numt);
  104.         y = (rt + rc * cos(s*twopi/numc)) * sin(t*twopi/numt);
  105.         z = rc * sin(s*twopi/numc);
  106.         glVertex3f(x, y, z);
  107.         }
  108.         }
  109.     glEnd();
  110.     }
  111. }
  112.  
  113. float Clamp(int iters_left, float t)
  114. {
  115.  
  116.     if (iters_left < 3) {
  117.     return 0.0;
  118.     }
  119.     return (iters_left-2)*t/iters_left;
  120. }
  121.  
  122. void DrawScene(void)
  123. {
  124.     int i, j;
  125.     GLboolean goIdle;
  126.  
  127.     goIdle = GL_TRUE;
  128.     for (i = 0; i < RINGS; i++) {
  129.     if (iters[i]) {
  130.         for (j = 0; j < 3; j++) {
  131.         offsets[i][j] = Clamp(iters[i], offsets[i][j]);
  132.         }
  133.         angs[i] = Clamp(iters[i], angs[i]);
  134.         iters[i]--;
  135.         goIdle = GL_FALSE;
  136.     }
  137.     }
  138.     if (goIdle) {
  139.        tkIdleFunc(NULL);
  140.     }
  141.  
  142.     glPushMatrix();
  143.     
  144.     glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  145.     gluLookAt(0,0,10, 0,0,0, 0,1,0);
  146.  
  147.     for (i = 0; i < RINGS; i++) {
  148.     if (rgb) {
  149.         glColor3ubv(rgb_colors[i]);
  150.     } else {
  151.         glIndexi(mapped_colors[i]);
  152.     }
  153.     glPushMatrix();
  154.     glTranslatef(dests[i][0]+offsets[i][0], dests[i][1]+offsets[i][1],
  155.              dests[i][2]+offsets[i][2]);
  156.     glRotatef(angs[i], rotAxis[i][0], rotAxis[i][1], rotAxis[i][2]);
  157.     glCallList(theTorus);
  158.     glPopMatrix();
  159.     }
  160.  
  161.     glPopMatrix();
  162.  
  163.     glFlush();
  164.     if (doubleBuffer) {
  165.     tkSwapBuffers();
  166.     }
  167. }
  168.  
  169. float MyRand(void)
  170. {
  171.    return 10.0 * ( (float) rand() / (float) RAND_MAX - 0.5 );
  172. }
  173.  
  174. void ReInit(void)
  175. {
  176.     int i;
  177.     float deviation;
  178.  
  179.     deviation = MyRand() / 2;
  180.     deviation = deviation * deviation;
  181.     for (i = 0; i < RINGS; i++) {
  182.     offsets[i][0] = MyRand();
  183.     offsets[i][1] = MyRand();
  184.     offsets[i][2] = MyRand();
  185.     angs[i] = 260.0 * MyRand();
  186.     rotAxis[i][0] = MyRand();
  187.     rotAxis[i][1] = MyRand();
  188.     rotAxis[i][2] = MyRand();
  189.     iters[i] = (deviation * MyRand() + 60.0);
  190.     }
  191.     tkIdleFunc(DrawScene);
  192. }
  193.  
  194. void Init(void)
  195. {
  196.     int gid;
  197.     float base, height;
  198.     float aspect, x, y;
  199.     int i;
  200.     struct timeval t;
  201.     struct timezone tz;
  202.     float sc = 10;
  203.     float top_y = 1.0;
  204.     float bottom_y = 0.0;
  205.     float top_z = 0.15;
  206.     float bottom_z = 0.69;
  207.     float spacing = 2.5;
  208.     static float lmodel_ambient[] = {0.0, 0.0, 0.0, 0.0};
  209.     static float lmodel_twoside[] = {GL_FALSE};
  210.     static float lmodel_local[] = {GL_FALSE};
  211.     static float light0_ambient[] = {0.1, 0.1, 0.1, 1.0};
  212.     static float light0_diffuse[] = {1.0, 1.0, 1.0, 0.0};
  213.     static float light0_position[] = {0.8660254, 0.5, 1, 0};
  214.     static float light0_specular[] = {1.0, 1.0, 1.0, 0.0};
  215.     static float bevel_mat_ambient[] = {0.0, 0.0, 0.0, 1.0};
  216.     static float bevel_mat_shininess[] = {40.0};
  217.     static float bevel_mat_specular[] = {1.0, 1.0, 1.0, 0.0};
  218.     static float bevel_mat_diffuse[] = {1.0, 0.0, 0.0, 0.0};
  219.  
  220.     gettimeofday(&t, &tz);
  221.     srand( t.tv_usec );
  222.  
  223.     ReInit();
  224.     for (i = 0; i < RINGS; i++) {
  225.     rgb_colors[i][0] = rgb_colors[i][1] = rgb_colors[i][2] = 0;
  226.     }
  227.     rgb_colors[BLUERING][2] = 255;
  228.     rgb_colors[REDRING][0] = 255;
  229.     rgb_colors[GREENRING][1] = 255;
  230.     rgb_colors[YELLOWRING][0] = 255;
  231.     rgb_colors[YELLOWRING][1] = 255;
  232.     mapped_colors[BLUERING] = BLUE;
  233.     mapped_colors[REDRING] = RED;
  234.     mapped_colors[GREENRING] = GREEN;
  235.     mapped_colors[YELLOWRING] = YELLOW;
  236.     mapped_colors[BLACKRING] = BLACK;
  237.  
  238.     dests[BLUERING][0] = -spacing;
  239.     dests[BLUERING][1] = top_y;
  240.     dests[BLUERING][2] = top_z;
  241.  
  242.     dests[BLACKRING][0] = 0.0;
  243.     dests[BLACKRING][1] = top_y;
  244.     dests[BLACKRING][2] = top_z;
  245.  
  246.     dests[REDRING][0] = spacing;
  247.     dests[REDRING][1] = top_y;
  248.     dests[REDRING][2] = top_z;
  249.  
  250.     dests[YELLOWRING][0] = -spacing / 2.0;
  251.     dests[YELLOWRING][1] = bottom_y;
  252.     dests[YELLOWRING][2] = bottom_z;
  253.  
  254.     dests[GREENRING][0] = spacing / 2.0;
  255.     dests[GREENRING][1] = bottom_y;
  256.     dests[GREENRING][2] = bottom_z;
  257.  
  258.     base = 2.0; 
  259.     height = 2.0;
  260.     theTorus = glGenLists(1);
  261.     glNewList(theTorus, GL_COMPILE);
  262.     FillTorus(0.1, 8, 1.0, 25);
  263.     glEndList();
  264.  
  265.     x = (float)XSIZE;
  266.     y = (float)YSIZE;
  267.     aspect = x / y;
  268.     glEnable(GL_CULL_FACE);
  269.     glCullFace(GL_BACK);
  270.     glEnable(GL_DEPTH_TEST);
  271.     glClearDepth(1.0);
  272.  
  273.     if (rgb) {
  274.     glClearColor(0.5, 0.5, 0.5, 0.0);
  275.     glLightfv(GL_LIGHT0, GL_AMBIENT, light0_ambient);
  276.     glLightfv(GL_LIGHT0, GL_DIFFUSE, light0_diffuse);
  277.     glLightfv(GL_LIGHT0, GL_SPECULAR, light0_specular);
  278.     glLightfv(GL_LIGHT0, GL_POSITION, light0_position);
  279.     glEnable(GL_LIGHT0);
  280.  
  281.     glLightModelfv(GL_LIGHT_MODEL_LOCAL_VIEWER, lmodel_local);
  282.     glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE, lmodel_twoside);
  283.     glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
  284.     glEnable(GL_LIGHTING);
  285.  
  286.     glMaterialfv(GL_FRONT, GL_AMBIENT, bevel_mat_ambient);
  287.     glMaterialfv(GL_FRONT, GL_SHININESS, bevel_mat_shininess);
  288.     glMaterialfv(GL_FRONT, GL_SPECULAR, bevel_mat_specular);
  289.     glMaterialfv(GL_FRONT, GL_DIFFUSE, bevel_mat_diffuse);
  290.  
  291.     glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE);
  292.     glEnable(GL_COLOR_MATERIAL);
  293.     glShadeModel(GL_SMOOTH);
  294.     } else {
  295.     glClearIndex(BACKGROUND);
  296.     glShadeModel(GL_FLAT);
  297.     }
  298.  
  299.     glMatrixMode(GL_PROJECTION);
  300.     gluPerspective(45, 1.33, 0.1, 100.0);
  301.     glMatrixMode(GL_MODELVIEW);
  302. }
  303.  
  304. void Reshape(int width, int height)
  305. {
  306.  
  307.     glViewport(0, 0, width, height);
  308. }
  309.  
  310. GLenum Key(int key, GLenum mask)
  311. {
  312.  
  313.     switch (key) {
  314.       case TK_ESCAPE:
  315.     tkQuit();
  316.       case TK_SPACE:
  317.     ReInit();
  318.     break;
  319.       default:
  320.     return GL_FALSE;
  321.     }
  322.     return GL_TRUE;
  323. }
  324.  
  325. GLenum Args(int argc, char **argv)
  326. {
  327.     GLint i;
  328.  
  329.     rgb = GL_TRUE;
  330.     doubleBuffer = GL_FALSE;
  331.     directRender = GL_TRUE;
  332.  
  333.     for (i = 1; i < argc; i++) {
  334.     if (strcmp(argv[i], "-ci") == 0) {
  335.         rgb = GL_FALSE;
  336.     } else if (strcmp(argv[i], "-rgb") == 0) {
  337.         rgb = GL_TRUE;
  338.     } else if (strcmp(argv[i], "-sb") == 0) {
  339.         doubleBuffer = GL_FALSE;
  340.     } else if (strcmp(argv[i], "-db") == 0) {
  341.         doubleBuffer = GL_TRUE;
  342.     } else if (strcmp(argv[i], "-dr") == 0) {
  343.         directRender = GL_TRUE;
  344.     } else if (strcmp(argv[i], "-ir") == 0) {
  345.         directRender = GL_FALSE;
  346.     } else {
  347.         printf("%s (Bad option).\n", argv[i]);
  348.         return GL_FALSE;
  349.     }
  350.     }
  351.     return GL_TRUE;
  352. }
  353.  
  354. void main(int argc, char **argv)
  355. {
  356.     GLenum type;
  357.  
  358.     if (Args(argc, argv) == GL_FALSE) {
  359.     tkQuit();
  360.     }
  361.  
  362.     tkInitPosition(0, 0, 400, 300);
  363.  
  364.     type = TK_DEPTH;
  365.     type |= (rgb) ? TK_RGB : TK_INDEX;
  366.     type |= (doubleBuffer) ? TK_DOUBLE : TK_SINGLE;
  367.     type |= (directRender) ? TK_DIRECT : TK_INDIRECT;
  368.     tkInitDisplayMode(type);
  369.  
  370.     if (tkInitWindow("Olympic") == GL_FALSE) {
  371.         tkQuit();
  372.     }
  373.  
  374.     Init();
  375.  
  376.     tkExposeFunc(Reshape);
  377.     tkReshapeFunc(Reshape);
  378.     tkKeyDownFunc(Key);
  379.     tkIdleFunc(DrawScene);
  380.  
  381.     tkExec();
  382. }
  383.