home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / tolkit45.zip / os2tk45 / samples / opengl / glutdemo / atlantis.c next >
C/C++ Source or Header  |  1999-05-11  |  7KB  |  268 lines

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