home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / exampleCode / opengl / GLUT / progs / redbook / surface.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-11  |  5.0 KB  |  176 lines

  1. /* aux2glut conversion Copyright (c) Mark J. Kilgard, 1994, 1995 */
  2.  
  3. /**
  4.  * (c) Copyright 1993, 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. /**
  40.  *  surface.c
  41.  *  This program draws a NURBS surface in the shape of a 
  42.  *  symmetrical hill.
  43.  */
  44. #include <GL/gl.h>
  45. #include <GL/glu.h>
  46. #include <GL/glut.h>
  47.  
  48. GLfloat ctlpoints[4][4][3];
  49. int showPoints = 0;
  50.  
  51. GLUnurbsObj *theNurb;
  52.  
  53. /*
  54.  *  Initializes the control points of the surface to a small hill.
  55.  *  The control points range from -3 to +3 in x, y, and z
  56.  */
  57. void init_surface(void)
  58. {
  59.     int u, v;
  60.     for (u = 0; u < 4; u++) {
  61.     for (v = 0; v < 4; v++) {
  62.         ctlpoints[u][v][0] = 2.0*((GLfloat)u - 1.5);
  63.         ctlpoints[u][v][1] = 2.0*((GLfloat)v - 1.5);
  64.  
  65.         if ( (u == 1 || u == 2) && (v == 1 || v == 2))
  66.         ctlpoints[u][v][2] = 7.0;
  67.         else
  68.         ctlpoints[u][v][2] = -3.0;
  69.     }
  70.     }                
  71. }                
  72.             
  73. /*  Initialize material property and depth buffer.
  74.  */
  75. void myinit(void)
  76. {
  77.     GLfloat mat_diffuse[] = { 0.7, 0.7, 0.7, 1.0 };
  78.     GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
  79.     GLfloat mat_shininess[] = { 100.0 };
  80.  
  81.     glClearColor (0.0, 0.0, 0.0, 1.0);
  82.     glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
  83.     glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
  84.     glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
  85.  
  86.     glEnable(GL_LIGHTING);
  87.     glEnable(GL_LIGHT0);
  88.     glDepthFunc(GL_LESS);
  89.     glEnable(GL_DEPTH_TEST);
  90.     glEnable(GL_AUTO_NORMAL);
  91.     glEnable(GL_NORMALIZE);
  92.  
  93.     init_surface();
  94.  
  95.     theNurb = gluNewNurbsRenderer();
  96.     gluNurbsProperty(theNurb, GLU_SAMPLING_TOLERANCE, 25.0);
  97.     gluNurbsProperty(theNurb, GLU_DISPLAY_MODE, GLU_FILL);
  98. }
  99.  
  100. void display(void)
  101. {
  102.     GLfloat knots[8] = {0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0};
  103.     int i, j;
  104.  
  105.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  106.  
  107.     glPushMatrix();
  108.     glRotatef(330.0, 1.,0.,0.);
  109.     glScalef (0.25, 0.25, 0.25);
  110.  
  111.     gluBeginSurface(theNurb);
  112.     gluNurbsSurface(theNurb, 
  113.         8, knots,
  114.         8, knots,
  115.         4 * 3,
  116.         3,
  117.         &ctlpoints[0][0][0], 
  118.         4, 4,
  119.         GL_MAP2_VERTEX_3);
  120.     gluEndSurface(theNurb);
  121.  
  122.     if(showPoints) {
  123.     glPointSize(5.0);
  124.     glDisable(GL_LIGHTING);
  125.     glColor3f(1.0, 1.0, 0.0);
  126.     glBegin(GL_POINTS);
  127.     for(i=0;i<4;i++) {
  128.       for(j=0;j<4;j++) {
  129.     glVertex3f(ctlpoints[i][j][0], ctlpoints[i][j][1], ctlpoints[i][j][2]);
  130.       }
  131.     }
  132.     glEnd();
  133.     glEnable(GL_LIGHTING);
  134.     }
  135.         
  136.     glPopMatrix();
  137.     glFlush();
  138. }
  139.  
  140. void reshape(int w, int h)
  141. {
  142.     glViewport(0, 0, w, h);
  143.     glMatrixMode(GL_PROJECTION);
  144.     glLoadIdentity();
  145.     gluPerspective (45.0, (GLdouble)w/(GLdouble)h, 3.0, 8.0);
  146.  
  147.     glMatrixMode(GL_MODELVIEW);
  148.     glLoadIdentity();
  149.     glTranslatef (0.0, 0.0, -5.0);
  150. }
  151.  
  152. void
  153. menu(int value)
  154. {
  155.     showPoints = value;
  156.     glutPostRedisplay();
  157. }
  158.  
  159. /* Main Loop */
  160. int
  161. main(int argc, char** argv)
  162. {
  163.     glutInit(&argc, argv);
  164.     glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
  165.     glutCreateWindow(argv[0]);
  166.     myinit();
  167.     glutReshapeFunc(reshape);
  168.     glutDisplayFunc(display);
  169.     glutCreateMenu(menu);
  170.     glutAddMenuEntry("Show control points", 1);
  171.     glutAddMenuEntry("Hide control points", 0);
  172.     glutAttachMenu(GLUT_RIGHT_BUTTON);
  173.     glutMainLoop();
  174.     return 0;             /* ANSI C requires main to return int. */
  175. }
  176.