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