surfpoints.c revision 32001f49
1/* 2 * Copyright (c) 1993-2003, Silicon Graphics, Inc. 3 * All Rights Reserved 4 * 5 * Permission to use, copy, modify, and distribute this software for any 6 * purpose and without fee is hereby granted, provided that the above 7 * copyright notice appear in all copies and that both the copyright 8 * notice and this permission notice appear in supporting documentation, 9 * and that the name of Silicon Graphics, Inc. not be used in 10 * advertising or publicity pertaining to distribution of the software 11 * without specific, written prior permission. 12 * 13 * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS" AND 14 * WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE, 15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR 16 * FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON 17 * GRAPHICS, INC. BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT, 18 * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, 19 * OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION, LOSS OF 20 * PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF THIRD 21 * PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC. HAS BEEN ADVISED OF 22 * THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON ANY THEORY OF 23 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE POSSESSION, USE 24 * OR PERFORMANCE OF THIS SOFTWARE. 25 * 26 * US Government Users Restricted Rights 27 * Use, duplication, or disclosure by the Government is subject to 28 * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph 29 * (c)(1)(ii) of the Rights in Technical Data and Computer Software 30 * clause at DFARS 252.227-7013 and/or in similar or successor clauses 31 * in the FAR or the DOD or NASA FAR Supplement. Unpublished - rights 32 * reserved under the copyright laws of the United States. 33 * 34 * Contractor/manufacturer is: 35 * Silicon Graphics, Inc. 36 * 1500 Crittenden Lane 37 * Mountain View, CA 94043 38 * United State of America 39 * 40 * OpenGL(R) is a registered trademark of Silicon Graphics, Inc. 41 */ 42 43/* 44 * surfpoints.c 45 * This program is a modification of the earlier surface.c 46 * program. The vertex data are not directly rendered, 47 * but are instead passed to the callback function. 48 * The values of the tessellated vertices are printed 49 * out there. 50 * 51 * This program draws a NURBS surface in the shape of a 52 * symmetrical hill. The 'c' keyboard key allows you to 53 * toggle the visibility of the control points themselves. 54 * Note that some of the control points are hidden by the 55 * surface itself. 56 */ 57#include "glut_wrap.h" 58#include <stdlib.h> 59#include <stdio.h> 60 61#ifdef GLU_VERSION_1_3 62 63GLfloat ctlpoints[4][4][3]; 64int showPoints = 0; 65 66GLUnurbsObj *theNurb; 67 68/* 69 * Initializes the control points of the surface to a small hill. 70 * The control points range from -3 to +3 in x, y, and z 71 */ 72static void init_surface(void) 73{ 74 int u, v; 75 for (u = 0; u < 4; u++) { 76 for (v = 0; v < 4; v++) { 77 ctlpoints[u][v][0] = 2.0*((GLfloat)u - 1.5); 78 ctlpoints[u][v][1] = 2.0*((GLfloat)v - 1.5); 79 80 if ( (u == 1 || u == 2) && (v == 1 || v == 2)) 81 ctlpoints[u][v][2] = 3.0; 82 else 83 ctlpoints[u][v][2] = -3.0; 84 } 85 } 86} 87 88static void GLAPIENTRY nurbsError(GLenum errorCode) 89{ 90 const GLubyte *estring; 91 92 estring = gluErrorString(errorCode); 93 fprintf (stderr, "Nurbs Error: %s\n", estring); 94 exit (0); 95} 96 97static void GLAPIENTRY beginCallback(GLenum whichType) 98{ 99 glBegin (whichType); /* resubmit rendering directive */ 100 printf ("glBegin("); 101 switch (whichType) { /* print diagnostic message */ 102 case GL_LINES: 103 printf ("GL_LINES)\n"); 104 break; 105 case GL_LINE_LOOP: 106 printf ("GL_LINE_LOOP)\n"); 107 break; 108 case GL_LINE_STRIP: 109 printf ("GL_LINE_STRIP)\n"); 110 break; 111 case GL_TRIANGLES: 112 printf ("GL_TRIANGLES)\n"); 113 break; 114 case GL_TRIANGLE_STRIP: 115 printf ("GL_TRIANGLE_STRIP)\n"); 116 break; 117 case GL_TRIANGLE_FAN: 118 printf ("GL_TRIANGLE_FAN)\n"); 119 break; 120 case GL_QUADS: 121 printf ("GL_QUADS)\n"); 122 break; 123 case GL_QUAD_STRIP: 124 printf ("GL_QUAD_STRIP)\n"); 125 break; 126 case GL_POLYGON: 127 printf ("GL_POLYGON)\n"); 128 break; 129 default: 130 break; 131 } 132} 133 134static void GLAPIENTRY endCallback() 135{ 136 glEnd(); /* resubmit rendering directive */ 137 printf ("glEnd()\n"); 138} 139 140static void GLAPIENTRY vertexCallback(GLfloat *vertex) 141{ 142 glVertex3fv(vertex); /* resubmit rendering directive */ 143 printf ("glVertex3f (%5.3f, %5.3f, %5.3f)\n", 144 vertex[0], vertex[1], vertex[2]); 145} 146 147static void GLAPIENTRY normalCallback(GLfloat *normal) 148{ 149 glNormal3fv(normal); /* resubmit rendering directive */ 150 printf ("glNormal3f (%5.3f, %5.3f, %5.3f)\n", 151 normal[0], normal[1], normal[2]); 152} 153 154/* Initialize material property and depth buffer. 155 */ 156static void init(void) 157{ 158 GLfloat mat_diffuse[] = { 0.7, 0.7, 0.7, 1.0 }; 159 GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 }; 160 GLfloat mat_shininess[] = { 100.0 }; 161 162 glClearColor (0.0, 0.0, 0.0, 0.0); 163 glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse); 164 glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular); 165 glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess); 166 167 glEnable(GL_LIGHTING); 168 glEnable(GL_LIGHT0); 169 glEnable(GL_DEPTH_TEST); 170 glEnable(GL_AUTO_NORMAL); 171 glEnable(GL_NORMALIZE); 172 173 init_surface(); 174 175 theNurb = gluNewNurbsRenderer(); 176 gluNurbsProperty(theNurb, GLU_NURBS_MODE, 177 GLU_NURBS_TESSELLATOR); 178 gluNurbsProperty(theNurb, GLU_SAMPLING_TOLERANCE, 25.0); 179 gluNurbsProperty(theNurb, GLU_DISPLAY_MODE, GLU_FILL); 180 gluNurbsCallback(theNurb, GLU_ERROR, nurbsError); 181 gluNurbsCallback(theNurb, GLU_NURBS_BEGIN, beginCallback); 182 gluNurbsCallback(theNurb, GLU_NURBS_VERTEX, vertexCallback); 183 gluNurbsCallback(theNurb, GLU_NURBS_NORMAL, normalCallback); 184 gluNurbsCallback(theNurb, GLU_NURBS_END, endCallback); 185 186} 187 188static void display(void) 189{ 190 GLfloat knots[8] = {0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0}; 191 int i, j; 192 193 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 194 195 glPushMatrix(); 196 glRotatef(330.0, 1.,0.,0.); 197 glScalef (0.5, 0.5, 0.5); 198 199 gluBeginSurface(theNurb); 200 gluNurbsSurface(theNurb, 201 8, knots, 8, knots, 202 4 * 3, 3, &ctlpoints[0][0][0], 203 4, 4, GL_MAP2_VERTEX_3); 204 gluEndSurface(theNurb); 205 206 if (showPoints) { 207 glPointSize(5.0); 208 glDisable(GL_LIGHTING); 209 glColor3f(1.0, 1.0, 0.0); 210 glBegin(GL_POINTS); 211 for (i = 0; i < 4; i++) { 212 for (j = 0; j < 4; j++) { 213 glVertex3f(ctlpoints[i][j][0], 214 ctlpoints[i][j][1], ctlpoints[i][j][2]); 215 } 216 } 217 glEnd(); 218 glEnable(GL_LIGHTING); 219 } 220 glPopMatrix(); 221 glFlush(); 222} 223 224static void reshape(int w, int h) 225{ 226 glViewport(0, 0, (GLsizei) w, (GLsizei) h); 227 glMatrixMode(GL_PROJECTION); 228 glLoadIdentity(); 229 gluPerspective (45.0, (GLdouble)w/(GLdouble)h, 3.0, 8.0); 230 glMatrixMode(GL_MODELVIEW); 231 glLoadIdentity(); 232 glTranslatef (0.0, 0.0, -5.0); 233} 234 235static void keyboard(unsigned char key, int x, int y) 236{ 237 switch (key) { 238 case 'c': 239 case 'C': 240 showPoints = !showPoints; 241 glutPostRedisplay(); 242 break; 243 case 27: 244 exit(0); 245 break; 246 default: 247 break; 248 } 249} 250 251int main(int argc, char** argv) 252{ 253 glutInit(&argc, argv); 254 glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH); 255 glutInitWindowSize (500, 500); 256 glutInitWindowPosition (100, 100); 257 glutCreateWindow(argv[0]); 258 init(); 259 glutReshapeFunc(reshape); 260 glutDisplayFunc(display); 261 glutKeyboardFunc (keyboard); 262 glutMainLoop(); 263 return 0; 264} 265 266#else 267int main(int argc, char** argv) 268{ 269 fprintf (stderr, "This program demonstrates a feature which is introduced in the\n"); 270 fprintf (stderr, "OpenGL Utility Library (GLU) Version 1.3.\n"); 271 fprintf (stderr, "If your implementation of GLU has the right extensions,\n"); 272 fprintf (stderr, "you may be able to modify this program to make it run.\n"); 273 return 0; 274} 275#endif 276 277