home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / demos / OpenGL / atlantis / atlantis.c next >
Encoding:
C/C++ Source or Header  |  1996-11-11  |  6.5 KB  |  266 lines

  1.  
  2. /* Copyright (c) Mark J. Kilgard, 1994. */
  3.  
  4. /**
  5.  * (c) Copyright 1993, 1994, Silicon Graphics, Inc.
  6.  * ALL RIGHTS RESERVED
  7.  * Permission to use, copy, modify, and distribute this software for
  8.  * any purpose and without fee is hereby granted, provided that the above
  9.  * copyright notice appear in all copies and that both the copyright notice
  10.  * and this permission notice appear in supporting documentation, and that
  11.  * the name of Silicon Graphics, Inc. not be used in advertising
  12.  * or publicity pertaining to distribution of the software without specific,
  13.  * written prior permission.
  14.  *
  15.  * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
  16.  * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
  17.  * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
  18.  * FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL SILICON
  19.  * GRAPHICS, INC.  BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
  20.  * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
  21.  * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
  22.  * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
  23.  * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC.  HAS BEEN
  24.  * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
  25.  * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
  26.  * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
  27.  *
  28.  * US Government Users Restricted Rights
  29.  * Use, duplication, or disclosure by the Government is subject to
  30.  * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
  31.  * (c)(1)(ii) of the Rights in Technical Data and Computer Software
  32.  * clause at DFARS 252.227-7013 and/or in similar or successor
  33.  * clauses in the FAR or the DOD or NASA FAR Supplement.
  34.  * Unpublished-- rights reserved under the copyright laws of the
  35.  * United States.  Contractor/manufacturer is Silicon Graphics,
  36.  * Inc., 2011 N.  Shoreline Blvd., Mountain View, CA 94039-7311.
  37.  *
  38.  * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
  39.  */
  40. #include <stdio.h>
  41. #include <stdlib.h>
  42. #include <string.h>
  43. #include <math.h>
  44. #include <GL/gl.h>
  45. #include <GL/glut.h>
  46. #include "atlantis.h"
  47.  
  48. fishRec sharks[NUM_SHARKS];
  49. fishRec momWhale;
  50. fishRec babyWhale;
  51. fishRec dolph;
  52.  
  53. GLboolean moving;
  54.  
  55. void
  56. InitFishs(void)
  57. {
  58.     int i;
  59.  
  60.     for (i = 0; i < NUM_SHARKS; i++) {
  61.         sharks[i].x = 70000.0 + rand() % 6000;
  62.         sharks[i].y = rand() % 6000;
  63.         sharks[i].z = rand() % 6000;
  64.         sharks[i].psi = rand() % 360 - 180.0;
  65.         sharks[i].v = 1.0;
  66.     }
  67.  
  68.     dolph.x = 30000.0;
  69.     dolph.y = 0.0;
  70.     dolph.z = 6000.0;
  71.     dolph.psi = 90.0;
  72.     dolph.theta = 0.0;
  73.     dolph.v = 3.0;
  74.  
  75.     momWhale.x = 70000.0;
  76.     momWhale.y = 0.0;
  77.     momWhale.z = 0.0;
  78.     momWhale.psi = 90.0;
  79.     momWhale.theta = 0.0;
  80.     momWhale.v = 3.0;
  81.  
  82.     babyWhale.x = 60000.0;
  83.     babyWhale.y = -2000.0;
  84.     babyWhale.z = -2000.0;
  85.     babyWhale.psi = 90.0;
  86.     babyWhale.theta = 0.0;
  87.     babyWhale.v = 3.0;
  88. }
  89.  
  90. void
  91. Init(void)
  92. {
  93.     static float ambient[] =
  94.     {0.1, 0.1, 0.1, 1.0};
  95.     static float diffuse[] =
  96.     {1.0, 1.0, 1.0, 1.0};
  97.     static float position[] =
  98.     {0.0, 1.0, 0.0, 0.0};
  99.     static float mat_shininess[] =
  100.     {90.0};
  101.     static float mat_specular[] =
  102.     {0.8, 0.8, 0.8, 1.0};
  103.     static float mat_diffuse[] =
  104.     {0.46, 0.66, 0.795, 1.0};
  105.     static float mat_ambient[] =
  106.     {0.0, 0.1, 0.2, 1.0};
  107.     static float lmodel_ambient[] =
  108.     {0.4, 0.4, 0.4, 1.0};
  109.     static float lmodel_localviewer[] =
  110.     {0.0};
  111.     GLfloat map1[4] =
  112.     {0.0, 0.0, 0.0, 0.0};
  113.     GLfloat map2[4] =
  114.     {0.0, 0.0, 0.0, 0.0};
  115.  
  116.     glFrontFace(GL_CW);
  117.  
  118.     glDepthFunc(GL_LEQUAL);
  119.     glEnable(GL_DEPTH_TEST);
  120.  
  121.     glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
  122.     glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
  123.     glLightfv(GL_LIGHT0, GL_POSITION, position);
  124.     glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
  125.     glLightModelfv(GL_LIGHT_MODEL_LOCAL_VIEWER, lmodel_localviewer);
  126.     glEnable(GL_LIGHTING);
  127.     glEnable(GL_LIGHT0);
  128.  
  129.     glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, mat_shininess);
  130.     glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat_specular);
  131.     glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, mat_diffuse);
  132.     glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, mat_ambient);
  133.  
  134.     InitFishs();
  135.  
  136.     glClearColor(0.0, 0.5, 0.9, 0.0);
  137. }
  138.  
  139. void
  140. Reshape(int width, int height)
  141. {
  142.     glViewport(0, 0, width, height);
  143.  
  144.     glMatrixMode(GL_PROJECTION);
  145.     glLoadIdentity();
  146.     gluPerspective(400.0, 2.0, 1.0, 2000000.0);
  147.     glMatrixMode(GL_MODELVIEW);
  148. }
  149.  
  150. void
  151. Animate(void)
  152. {
  153.     int i;
  154.  
  155.     for (i = 0; i < NUM_SHARKS; i++) {
  156.         SharkPilot(&sharks[i]);
  157.         SharkMiss(i);
  158.     }
  159.     WhalePilot(&dolph);
  160.     dolph.phi++;
  161.     glutPostRedisplay();
  162.     WhalePilot(&momWhale);
  163.     momWhale.phi++;
  164.     WhalePilot(&babyWhale);
  165.     babyWhale.phi++;
  166. }
  167.  
  168. void
  169. Key(unsigned char key, int x, int y)
  170. {
  171.     switch (key) {
  172.     case 27:           /* Esc will quit */
  173.         exit(1);
  174.         break;
  175.     case ' ':          /* space will advance frame */
  176.         if (!moving) {
  177.             Animate();
  178.         }
  179.     }
  180. }
  181.  
  182. void
  183. Display(void)
  184. {
  185.     int i;
  186.  
  187.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  188.  
  189.     for (i = 0; i < NUM_SHARKS; i++) {
  190.         glPushMatrix();
  191.         FishTransform(&sharks[i]);
  192.         DrawShark(&sharks[i]);
  193.         glPopMatrix();
  194.     }
  195.  
  196.     glPushMatrix();
  197.     FishTransform(&dolph);
  198.     DrawDolphin(&dolph);
  199.     glPopMatrix();
  200.  
  201.     glPushMatrix();
  202.     FishTransform(&momWhale);
  203.     DrawWhale(&momWhale);
  204.     glPopMatrix();
  205.  
  206.     glPushMatrix();
  207.     FishTransform(&babyWhale);
  208.     glScalef(0.45, 0.45, 0.3);
  209.     DrawWhale(&babyWhale);
  210.     glPopMatrix();
  211.  
  212.     glutSwapBuffers();
  213. }
  214.  
  215. void
  216. Visible(int state)
  217. {
  218.     if (state == GLUT_VISIBLE) {
  219.         if (moving)
  220.             glutIdleFunc(Animate);
  221.     } else {
  222.         if (moving)
  223.             glutIdleFunc(NULL);
  224.     }
  225. }
  226.  
  227. void
  228. Select(int value)
  229. {
  230.     switch (value) {
  231.     case 1:
  232.         moving = GL_TRUE;
  233.         glutIdleFunc(Animate);
  234.         break;
  235.     case 2:
  236.         moving = GL_FALSE;;
  237.         glutIdleFunc(NULL);
  238.         break;
  239.     case 3:
  240.         exit(0);
  241.         break;
  242.     }
  243. }
  244.  
  245. void
  246. main(int argc, char **argv)
  247. {
  248.     glutInitWindowSize(500, 250);
  249.     glutInit(&argc, argv);
  250.     glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
  251.     glutCreateWindow("GLUT Atlantis Demo");
  252.     Init();
  253.     glutDisplayFunc(Display);
  254.     glutReshapeFunc(Reshape);
  255.     glutKeyboardFunc(Key);
  256.     moving = GL_TRUE;
  257.     glutIdleFunc(Animate);
  258.     glutVisibilityFunc(Visible);
  259.     glutCreateMenu(Select);
  260.     glutAddMenuEntry("Start motion", 1);
  261.     glutAddMenuEntry("Stop motion", 2);
  262.     glutAddMenuEntry("Quit", 3);
  263.     glutAttachMenu(GLUT_RIGHT_BUTTON);
  264.     glutMainLoop();
  265. }
  266.