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