surfpoints.c revision 32001f49
132001f49Smrg/*
232001f49Smrg * Copyright (c) 1993-2003, Silicon Graphics, Inc.
332001f49Smrg * All Rights Reserved
432001f49Smrg *
532001f49Smrg * Permission to use, copy, modify, and distribute this software for any
632001f49Smrg * purpose and without fee is hereby granted, provided that the above
732001f49Smrg * copyright notice appear in all copies and that both the copyright
832001f49Smrg * notice and this permission notice appear in supporting documentation,
932001f49Smrg * and that the name of Silicon Graphics, Inc. not be used in
1032001f49Smrg * advertising or publicity pertaining to distribution of the software
1132001f49Smrg * without specific, written prior permission.
1232001f49Smrg *
1332001f49Smrg * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS" AND
1432001f49Smrg * WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
1532001f49Smrg * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
1632001f49Smrg * FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL SILICON
1732001f49Smrg * GRAPHICS, INC.  BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
1832001f49Smrg * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
1932001f49Smrg * OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION, LOSS OF
2032001f49Smrg * PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF THIRD
2132001f49Smrg * PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC.  HAS BEEN ADVISED OF
2232001f49Smrg * THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON ANY THEORY OF
2332001f49Smrg * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE POSSESSION, USE
2432001f49Smrg * OR PERFORMANCE OF THIS SOFTWARE.
2532001f49Smrg *
2632001f49Smrg * US Government Users Restricted Rights
2732001f49Smrg * Use, duplication, or disclosure by the Government is subject to
2832001f49Smrg * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
2932001f49Smrg * (c)(1)(ii) of the Rights in Technical Data and Computer Software
3032001f49Smrg * clause at DFARS 252.227-7013 and/or in similar or successor clauses
3132001f49Smrg * in the FAR or the DOD or NASA FAR Supplement.  Unpublished - rights
3232001f49Smrg * reserved under the copyright laws of the United States.
3332001f49Smrg *
3432001f49Smrg * Contractor/manufacturer is:
3532001f49Smrg *	Silicon Graphics, Inc.
3632001f49Smrg *	1500 Crittenden Lane
3732001f49Smrg *	Mountain View, CA  94043
3832001f49Smrg *	United State of America
3932001f49Smrg *
4032001f49Smrg * OpenGL(R) is a registered trademark of Silicon Graphics, Inc.
4132001f49Smrg */
4232001f49Smrg
4332001f49Smrg/*
4432001f49Smrg *  surfpoints.c
4532001f49Smrg *  This program is a modification of the earlier surface.c
4632001f49Smrg *  program.  The vertex data are not directly rendered,
4732001f49Smrg *  but are instead passed to the callback function.
4832001f49Smrg *  The values of the tessellated vertices are printed
4932001f49Smrg *  out there.
5032001f49Smrg *
5132001f49Smrg *  This program draws a NURBS surface in the shape of a
5232001f49Smrg *  symmetrical hill.  The 'c' keyboard key allows you to
5332001f49Smrg *  toggle the visibility of the control points themselves.
5432001f49Smrg *  Note that some of the control points are hidden by the
5532001f49Smrg *  surface itself.
5632001f49Smrg */
5732001f49Smrg#include "glut_wrap.h"
5832001f49Smrg#include <stdlib.h>
5932001f49Smrg#include <stdio.h>
6032001f49Smrg
6132001f49Smrg#ifdef GLU_VERSION_1_3
6232001f49Smrg
6332001f49SmrgGLfloat ctlpoints[4][4][3];
6432001f49Smrgint showPoints = 0;
6532001f49Smrg
6632001f49SmrgGLUnurbsObj *theNurb;
6732001f49Smrg
6832001f49Smrg/*
6932001f49Smrg *  Initializes the control points of the surface to a small hill.
7032001f49Smrg *  The control points range from -3 to +3 in x, y, and z
7132001f49Smrg */
7232001f49Smrgstatic void init_surface(void)
7332001f49Smrg{
7432001f49Smrg   int u, v;
7532001f49Smrg   for (u = 0; u < 4; u++) {
7632001f49Smrg      for (v = 0; v < 4; v++) {
7732001f49Smrg         ctlpoints[u][v][0] = 2.0*((GLfloat)u - 1.5);
7832001f49Smrg         ctlpoints[u][v][1] = 2.0*((GLfloat)v - 1.5);
7932001f49Smrg
8032001f49Smrg         if ( (u == 1 || u == 2) && (v == 1 || v == 2))
8132001f49Smrg            ctlpoints[u][v][2] = 3.0;
8232001f49Smrg         else
8332001f49Smrg            ctlpoints[u][v][2] = -3.0;
8432001f49Smrg      }
8532001f49Smrg   }
8632001f49Smrg}
8732001f49Smrg
8832001f49Smrgstatic void GLAPIENTRY nurbsError(GLenum errorCode)
8932001f49Smrg{
9032001f49Smrg   const GLubyte *estring;
9132001f49Smrg
9232001f49Smrg   estring = gluErrorString(errorCode);
9332001f49Smrg   fprintf (stderr, "Nurbs Error: %s\n", estring);
9432001f49Smrg   exit (0);
9532001f49Smrg}
9632001f49Smrg
9732001f49Smrgstatic void GLAPIENTRY beginCallback(GLenum whichType)
9832001f49Smrg{
9932001f49Smrg   glBegin (whichType);  /*  resubmit rendering directive  */
10032001f49Smrg   printf ("glBegin(");
10132001f49Smrg   switch (whichType) {  /*  print diagnostic message  */
10232001f49Smrg      case GL_LINES:
10332001f49Smrg	 printf ("GL_LINES)\n");
10432001f49Smrg	 break;
10532001f49Smrg      case GL_LINE_LOOP:
10632001f49Smrg	 printf ("GL_LINE_LOOP)\n");
10732001f49Smrg	 break;
10832001f49Smrg      case GL_LINE_STRIP:
10932001f49Smrg	 printf ("GL_LINE_STRIP)\n");
11032001f49Smrg	 break;
11132001f49Smrg      case GL_TRIANGLES:
11232001f49Smrg	 printf ("GL_TRIANGLES)\n");
11332001f49Smrg	 break;
11432001f49Smrg      case GL_TRIANGLE_STRIP:
11532001f49Smrg	 printf ("GL_TRIANGLE_STRIP)\n");
11632001f49Smrg	 break;
11732001f49Smrg      case GL_TRIANGLE_FAN:
11832001f49Smrg	 printf ("GL_TRIANGLE_FAN)\n");
11932001f49Smrg	 break;
12032001f49Smrg      case GL_QUADS:
12132001f49Smrg	 printf ("GL_QUADS)\n");
12232001f49Smrg	 break;
12332001f49Smrg      case GL_QUAD_STRIP:
12432001f49Smrg	 printf ("GL_QUAD_STRIP)\n");
12532001f49Smrg	 break;
12632001f49Smrg      case GL_POLYGON:
12732001f49Smrg	 printf ("GL_POLYGON)\n");
12832001f49Smrg	 break;
12932001f49Smrg      default:
13032001f49Smrg	 break;
13132001f49Smrg   }
13232001f49Smrg}
13332001f49Smrg
13432001f49Smrgstatic void GLAPIENTRY endCallback()
13532001f49Smrg{
13632001f49Smrg   glEnd();  /*  resubmit rendering directive  */
13732001f49Smrg   printf ("glEnd()\n");
13832001f49Smrg}
13932001f49Smrg
14032001f49Smrgstatic void GLAPIENTRY vertexCallback(GLfloat *vertex)
14132001f49Smrg{
14232001f49Smrg   glVertex3fv(vertex);  /*  resubmit rendering directive  */
14332001f49Smrg   printf ("glVertex3f (%5.3f, %5.3f, %5.3f)\n",
14432001f49Smrg	   vertex[0], vertex[1], vertex[2]);
14532001f49Smrg}
14632001f49Smrg
14732001f49Smrgstatic void GLAPIENTRY normalCallback(GLfloat *normal)
14832001f49Smrg{
14932001f49Smrg   glNormal3fv(normal);  /*  resubmit rendering directive  */
15032001f49Smrg   printf ("glNormal3f (%5.3f, %5.3f, %5.3f)\n",
15132001f49Smrg           normal[0], normal[1], normal[2]);
15232001f49Smrg}
15332001f49Smrg
15432001f49Smrg/*  Initialize material property and depth buffer.
15532001f49Smrg */
15632001f49Smrgstatic void init(void)
15732001f49Smrg{
15832001f49Smrg   GLfloat mat_diffuse[] = { 0.7, 0.7, 0.7, 1.0 };
15932001f49Smrg   GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
16032001f49Smrg   GLfloat mat_shininess[] = { 100.0 };
16132001f49Smrg
16232001f49Smrg   glClearColor (0.0, 0.0, 0.0, 0.0);
16332001f49Smrg   glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
16432001f49Smrg   glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
16532001f49Smrg   glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
16632001f49Smrg
16732001f49Smrg   glEnable(GL_LIGHTING);
16832001f49Smrg   glEnable(GL_LIGHT0);
16932001f49Smrg   glEnable(GL_DEPTH_TEST);
17032001f49Smrg   glEnable(GL_AUTO_NORMAL);
17132001f49Smrg   glEnable(GL_NORMALIZE);
17232001f49Smrg
17332001f49Smrg   init_surface();
17432001f49Smrg
17532001f49Smrg   theNurb = gluNewNurbsRenderer();
17632001f49Smrg   gluNurbsProperty(theNurb, GLU_NURBS_MODE,
17732001f49Smrg		    GLU_NURBS_TESSELLATOR);
17832001f49Smrg   gluNurbsProperty(theNurb, GLU_SAMPLING_TOLERANCE, 25.0);
17932001f49Smrg   gluNurbsProperty(theNurb, GLU_DISPLAY_MODE, GLU_FILL);
18032001f49Smrg   gluNurbsCallback(theNurb, GLU_ERROR, nurbsError);
18132001f49Smrg   gluNurbsCallback(theNurb, GLU_NURBS_BEGIN, beginCallback);
18232001f49Smrg   gluNurbsCallback(theNurb, GLU_NURBS_VERTEX, vertexCallback);
18332001f49Smrg   gluNurbsCallback(theNurb, GLU_NURBS_NORMAL, normalCallback);
18432001f49Smrg   gluNurbsCallback(theNurb, GLU_NURBS_END, endCallback);
18532001f49Smrg
18632001f49Smrg}
18732001f49Smrg
18832001f49Smrgstatic void display(void)
18932001f49Smrg{
19032001f49Smrg   GLfloat knots[8] = {0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0};
19132001f49Smrg   int i, j;
19232001f49Smrg
19332001f49Smrg   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
19432001f49Smrg
19532001f49Smrg   glPushMatrix();
19632001f49Smrg   glRotatef(330.0, 1.,0.,0.);
19732001f49Smrg   glScalef (0.5, 0.5, 0.5);
19832001f49Smrg
19932001f49Smrg   gluBeginSurface(theNurb);
20032001f49Smrg   gluNurbsSurface(theNurb,
20132001f49Smrg                   8, knots, 8, knots,
20232001f49Smrg                   4 * 3, 3, &ctlpoints[0][0][0],
20332001f49Smrg                   4, 4, GL_MAP2_VERTEX_3);
20432001f49Smrg   gluEndSurface(theNurb);
20532001f49Smrg
20632001f49Smrg   if (showPoints) {
20732001f49Smrg      glPointSize(5.0);
20832001f49Smrg      glDisable(GL_LIGHTING);
20932001f49Smrg      glColor3f(1.0, 1.0, 0.0);
21032001f49Smrg      glBegin(GL_POINTS);
21132001f49Smrg      for (i = 0; i < 4; i++) {
21232001f49Smrg         for (j = 0; j < 4; j++) {
21332001f49Smrg	    glVertex3f(ctlpoints[i][j][0],
21432001f49Smrg               ctlpoints[i][j][1], ctlpoints[i][j][2]);
21532001f49Smrg         }
21632001f49Smrg      }
21732001f49Smrg      glEnd();
21832001f49Smrg      glEnable(GL_LIGHTING);
21932001f49Smrg   }
22032001f49Smrg   glPopMatrix();
22132001f49Smrg   glFlush();
22232001f49Smrg}
22332001f49Smrg
22432001f49Smrgstatic void reshape(int w, int h)
22532001f49Smrg{
22632001f49Smrg   glViewport(0, 0, (GLsizei) w, (GLsizei) h);
22732001f49Smrg   glMatrixMode(GL_PROJECTION);
22832001f49Smrg   glLoadIdentity();
22932001f49Smrg   gluPerspective (45.0, (GLdouble)w/(GLdouble)h, 3.0, 8.0);
23032001f49Smrg   glMatrixMode(GL_MODELVIEW);
23132001f49Smrg   glLoadIdentity();
23232001f49Smrg   glTranslatef (0.0, 0.0, -5.0);
23332001f49Smrg}
23432001f49Smrg
23532001f49Smrgstatic void keyboard(unsigned char key, int x, int y)
23632001f49Smrg{
23732001f49Smrg   switch (key) {
23832001f49Smrg      case 'c':
23932001f49Smrg      case 'C':
24032001f49Smrg         showPoints = !showPoints;
24132001f49Smrg         glutPostRedisplay();
24232001f49Smrg         break;
24332001f49Smrg      case 27:
24432001f49Smrg         exit(0);
24532001f49Smrg         break;
24632001f49Smrg      default:
24732001f49Smrg         break;
24832001f49Smrg   }
24932001f49Smrg}
25032001f49Smrg
25132001f49Smrgint main(int argc, char** argv)
25232001f49Smrg{
25332001f49Smrg   glutInit(&argc, argv);
25432001f49Smrg   glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
25532001f49Smrg   glutInitWindowSize (500, 500);
25632001f49Smrg   glutInitWindowPosition (100, 100);
25732001f49Smrg   glutCreateWindow(argv[0]);
25832001f49Smrg   init();
25932001f49Smrg   glutReshapeFunc(reshape);
26032001f49Smrg   glutDisplayFunc(display);
26132001f49Smrg   glutKeyboardFunc (keyboard);
26232001f49Smrg   glutMainLoop();
26332001f49Smrg   return 0;
26432001f49Smrg}
26532001f49Smrg
26632001f49Smrg#else
26732001f49Smrgint main(int argc, char** argv)
26832001f49Smrg{
26932001f49Smrg    fprintf (stderr, "This program demonstrates a feature which is introduced in the\n");
27032001f49Smrg    fprintf (stderr, "OpenGL Utility Library (GLU) Version 1.3.\n");
27132001f49Smrg    fprintf (stderr, "If your implementation of GLU has the right extensions,\n");
27232001f49Smrg    fprintf (stderr, "you may be able to modify this program to make it run.\n");
27332001f49Smrg    return 0;
27432001f49Smrg}
27532001f49Smrg#endif
27632001f49Smrg
277