home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / demos / OpenGL / stonehenge / Telescope.c++ < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  4.8 KB  |  174 lines

  1. /*
  2.  * (c) Copyright 1993, 1994, Silicon Graphics, Inc.
  3.  * ALL RIGHTS RESERVED
  4.  * Permission to use, copy, modify, and distribute this software for
  5.  * any purpose and without fee is hereby granted, provided that the above
  6.  * copyright notice appear in all copies and that both the copyright notice
  7.  * and this permission notice appear in supporting documentation, and that
  8.  * the name of Silicon Graphics, Inc. not be used in advertising
  9.  * or publicity pertaining to distribution of the software without specific,
  10.  * written prior permission.
  11.  *
  12.  * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
  13.  * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
  14.  * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
  15.  * FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL SILICON
  16.  * GRAPHICS, INC.  BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
  17.  * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
  18.  * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
  19.  * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
  20.  * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC.  HAS BEEN
  21.  * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
  22.  * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
  23.  * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
  24.  *
  25.  * US Government Users Restricted Rights
  26.  * Use, duplication, or disclosure by the Government is subject to
  27.  * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
  28.  * (c)(1)(ii) of the Rights in Technical Data and Computer Software
  29.  * clause at DFARS 252.227-7013 and/or in similar or successor
  30.  * clauses in the FAR or the DOD or NASA FAR Supplement.
  31.  * Unpublished-- rights reserved under the copyright laws of the
  32.  * United States.  Contractor/manufacturer is Silicon Graphics,
  33.  * Inc., 2011 N.  Shoreline Blvd., Mountain View, CA 94039-7311.
  34.  *
  35.  * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
  36.  */
  37. #include <GL/glu.h>
  38. #include <GL/glx.h>
  39.  
  40. #include <math.h>
  41. #include <stdio.h>
  42.  
  43. #include "Color.h"
  44. #include "Telescope.h"
  45.  
  46. inline float radians(float a) {return M_PI * a / 180.0;}
  47.  
  48. Telescope::Telescope(GLfloat x, GLfloat y)
  49. {
  50.   xpos = x;
  51.   ypos = y;
  52.  
  53.   divisions = 20;
  54.   radius = .1;
  55.   
  56.   disk = gluNewQuadric();
  57.   cylinder = gluNewQuadric();
  58.   gluQuadricNormals(disk, GLU_FLAT);
  59.   gluQuadricNormals(cylinder, GLU_SMOOTH);
  60.   gluQuadricTexture(disk, GL_TRUE);
  61.   gluQuadricTexture(cylinder, GL_TRUE);
  62.  
  63. }
  64.  
  65. Telescope::~Telescope()
  66. {
  67.   gluDeleteQuadric(disk);
  68.   gluDeleteQuadric(cylinder);
  69. }
  70.  
  71. void Telescope::draw_setup(GLfloat fov, GLfloat aspect, int perspective) 
  72. {
  73.   GLfloat near;
  74.  
  75.   /* Worry about the aspect ratio later */
  76.   near = .5 / tan(radians(fov / 2.0));
  77.  
  78.   glMatrixMode(GL_PROJECTION);
  79.   glPushMatrix();
  80.   glLoadIdentity();
  81.   if (perspective) gluPerspective(fov, aspect, near - .01, 10.0);
  82.   else 
  83.     fprintf(stderr, 
  84.         "Warning: Drawing telescope using orthographic projection.\n"); 
  85.   gluLookAt(0, 0, -near, 
  86.         0, 0, 1, 
  87.         0, 1, 0);
  88.  
  89.   glMatrixMode(GL_MODELVIEW);
  90.   glPushMatrix();
  91.   glLoadIdentity();
  92.   glTranslatef(xpos, ypos, 0); 
  93. }
  94.  
  95. void Telescope::draw_fake()
  96. {
  97.   glBegin(GL_QUADS);
  98.   glColor3f(1, 1, 1);
  99.   glVertex3f(-radius, -radius, .01);
  100.   glVertex3f(radius, -radius, .01);
  101.   glVertex3f(radius, radius, .01);
  102.   glVertex3f(-radius, radius, .01);
  103.   glEnd();
  104. }
  105.  
  106. void Telescope::draw_body() 
  107. {
  108.   Color c;
  109.   
  110.   glPushMatrix();
  111.  
  112.   glColor3f(1, 1, 0);
  113.   gluDisk(disk, radius, 1.1 * radius, divisions, 1);
  114.   gluCylinder(cylinder, 1.1 * radius, 1.1 * radius, .1 * radius, divisions, 1);
  115.   glTranslatef(0, 0, .1*radius);
  116.   gluDisk(disk, radius, 1.1 * radius, divisions, 1);
  117.  
  118.   glPushAttrib(GL_ENABLE_BIT);
  119.   glDisable(GL_TEXTURE_2D);
  120.   glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, white.c);
  121.   glColor3f(0, 0, 0);
  122.   gluCylinder(cylinder, 1.05 * radius, .95 * radius, .25, divisions, 1);
  123.   glPopAttrib();
  124.  
  125.   /* Would just do this with a push / pop, but that seems to be broken.
  126.    * glGetMaterialfv also seems to be broken, so we can't use that either. */
  127.   glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, black.c);
  128.  
  129.   glTranslatef(0, 0, .25);
  130.   glColor3f(1, 1, 0);
  131.   gluDisk(disk, .95 * radius, 1.05 * radius, divisions, 1);
  132.   gluCylinder(cylinder, 1.05 * radius, 1.05 * radius, .1 * radius, 
  133.           divisions, 1);
  134.   glTranslatef(0, 0, .1*radius);
  135.   gluDisk(disk, .95 * radius, 1.05 * radius, divisions, 1);
  136.  
  137.   glPopMatrix();
  138. }
  139.  
  140. void Telescope::draw_takedown()
  141. {
  142.   glMatrixMode(GL_PROJECTION);
  143.   glPopMatrix();
  144.   glMatrixMode(GL_MODELVIEW);
  145.   glPopMatrix();
  146. }
  147.  
  148. void Telescope::draw_lens()
  149. {
  150.   gluDisk(disk, 0, radius, divisions, 1);
  151. }
  152.  
  153. void Telescope::set_divisions(int d)
  154. {
  155.   // Someday we'll put all the quadric stuff in display lists...
  156.   divisions = d;
  157. }
  158.  
  159. int Telescope::get_divisions()
  160. {
  161.   return divisions;
  162. }
  163.  
  164. void Telescope::set_radius(GLfloat r)
  165. {
  166.   // Someday this might have to update some display lists
  167.   radius = r;
  168. }
  169.  
  170. GLfloat Telescope::get_radius()
  171. {
  172.   return radius;
  173. }
  174.