home *** CD-ROM | disk | FTP | other *** search
/ IRIX 6.3 Development Libraries / SGI IRIX 6.3 Development Libraries.iso / dist6.3 / gl_dev.idb / usr / share / src / OpenGL / demos / atlantis / atlantis.c.z / atlantis.c
Encoding:
C/C++ Source or Header  |  1996-12-06  |  3.7 KB  |  169 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <math.h>
  5. #include <GL/gl.h>
  6. #include <GL/glut.h>
  7. #include "atlantis.h"
  8.  
  9. fishRec sharks[NUM_SHARKS];
  10. fishRec momWhale;
  11. fishRec babyWhale;
  12. fishRec dolph;
  13.  
  14.  
  15. void InitFishs(void)
  16. {
  17.     int i;
  18.  
  19.     for (i = 0; i < NUM_SHARKS; i++) {
  20.         sharks[i].x = 70000.0 + rand() % 6000;
  21.         sharks[i].y = rand() % 6000;
  22.         sharks[i].z = rand() % 6000;
  23.         sharks[i].psi = rand() % 360 - 180.0;
  24.         sharks[i].v = 1.0;
  25.     }
  26.  
  27.     dolph.x = 30000.0;
  28.     dolph.y = 0.0;
  29.     dolph.z = 6000.0;
  30.     dolph.psi = 90.0;
  31.     dolph.theta = 0.0;
  32.     dolph.v = 3.0;
  33.  
  34.     momWhale.x = 70000.0;
  35.     momWhale.y = 0.0;
  36.     momWhale.z = 0.0;
  37.     momWhale.psi = 90.0;
  38.     momWhale.theta = 0.0;
  39.     momWhale.v = 3.0;
  40.  
  41.     babyWhale.x = 60000.0;
  42.     babyWhale.y = -2000.0;
  43.     babyWhale.z = -2000.0;
  44.     babyWhale.psi = 90.0;
  45.     babyWhale.theta = 0.0;
  46.     babyWhale.v = 3.0;
  47. }
  48.  
  49. void Init(void)
  50. {
  51.     static float ambient[] = {0.1, 0.1, 0.1, 1.0};
  52.     static float diffuse[] = {1.0, 1.0, 1.0, 1.0};
  53.     static float position[] = {0.0, 1.0, 0.0, 0.0};
  54.     static float mat_shininess[] = {90.0};
  55.     static float mat_specular[] = {0.8, 0.8, 0.8, 1.0};
  56.     static float mat_diffuse[] = {0.46, 0.66, 0.795, 1.0};
  57.     static float mat_ambient[] = {0.0, 0.1, 0.2, 1.0};
  58.     static float lmodel_ambient[] = {0.4, 0.4, 0.4, 1.0};
  59.     static float lmodel_localviewer[] = {0.0};
  60.     GLfloat map1[4] = {0.0, 0.0, 0.0, 0.0};
  61.     GLfloat map2[4] = {0.0, 0.0, 0.0, 0.0};
  62.  
  63.     glFrontFace(GL_CW);
  64.  
  65.     glDepthFunc(GL_LEQUAL);
  66.     glEnable(GL_DEPTH_TEST);
  67.  
  68.     glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
  69.     glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
  70.     glLightfv(GL_LIGHT0, GL_POSITION, position);
  71.     glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
  72.     glLightModelfv(GL_LIGHT_MODEL_LOCAL_VIEWER, lmodel_localviewer);
  73.     glEnable(GL_LIGHTING);
  74.     glEnable(GL_LIGHT0);
  75.  
  76.     glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, mat_shininess);
  77.     glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat_specular);
  78.     glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, mat_diffuse);
  79.     glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, mat_ambient);
  80.  
  81.     InitFishs();
  82.  
  83.     glClearColor(0.0, 0.5, 0.9, 0.0);
  84. }
  85.  
  86. void Reshape(int width, int height)
  87. {
  88.  
  89.     glViewport(0, 0, width, height);
  90.  
  91.     glMatrixMode(GL_PROJECTION);
  92.     glLoadIdentity();
  93.     gluPerspective(400.0, 2.0, 1.0, 2000000.0);
  94.     glMatrixMode(GL_MODELVIEW);
  95. }
  96.  
  97. void Animate(void)
  98. {
  99.     int i;
  100.  
  101.     for (i = 0; i < NUM_SHARKS; i++) {
  102.         SharkPilot(&sharks[i]);
  103.         SharkMiss(i);
  104.     }
  105.     WhalePilot(&dolph);
  106.     dolph.phi++;
  107.     glutPostRedisplay();
  108.     WhalePilot(&momWhale);
  109.     momWhale.phi++;
  110.     WhalePilot(&babyWhale);
  111.     babyWhale.phi++;
  112. }
  113.  
  114. void Key(unsigned char key, int x, int y)
  115. {
  116.  
  117.     switch (key) {
  118.     case 27:
  119.         exit(1);
  120.     }
  121. }
  122.  
  123. void Display(void)
  124. {
  125.     int i;
  126.  
  127.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  128.  
  129.     for (i = 0; i < NUM_SHARKS; i++) {
  130.         glPushMatrix();
  131.         FishTransform(&sharks[i]);
  132.         DrawShark(&sharks[i]);
  133.         glPopMatrix();
  134.     }
  135.  
  136.     glPushMatrix();
  137.     FishTransform(&dolph);
  138.     DrawDolphin(&dolph);
  139.     glPopMatrix();
  140.  
  141.     glPushMatrix();
  142.     FishTransform(&momWhale);
  143.     DrawWhale(&momWhale);
  144.     glPopMatrix();
  145.  
  146.     glPushMatrix();
  147.     FishTransform(&babyWhale);
  148.     glScalef(0.45, 0.45, 0.3);
  149.     DrawWhale(&babyWhale);
  150.     glPopMatrix();
  151.  
  152.     glutSwapBuffers();
  153. }
  154.  
  155. void main(int argc, char **argv)
  156. {
  157.  
  158.     glutInitWindowSize(500, 250);
  159.     glutInit(&argc, argv);
  160.     glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE|GLUT_DEPTH);
  161.     glutCreateWindow("GLUT Atlantis Demo");
  162.     Init();
  163.     glutDisplayFunc(Display);
  164.     glutReshapeFunc(Reshape);
  165.     glutKeyboardFunc(Key);
  166.     glutIdleFunc(Animate);
  167.     glutMainLoop();
  168. }
  169.