2 * Copyright (c) 1993-2003, Silicon Graphics, Inc.
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.
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.
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.
34 * Contractor/manufacturer is:
35 * Silicon Graphics, Inc.
36 * 1500 Crittenden Lane
37 * Mountain View, CA 94043
38 * United State of America
40 * OpenGL(R) is a registered trademark of Silicon Graphics, Inc.
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
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
61 #ifdef GLU_VERSION_1_3
67 GLfloat ctlpoints
[4][4][3];
73 * Initializes the control points of the surface to a small hill.
74 * The control points range from -3 to +3 in x, y, and z
76 static void init_surface(void)
79 for (u
= 0; u
< 4; u
++) {
80 for (v
= 0; v
< 4; v
++) {
81 ctlpoints
[u
][v
][0] = 2.0*((GLfloat
)u
- 1.5);
82 ctlpoints
[u
][v
][1] = 2.0*((GLfloat
)v
- 1.5);
84 if ( (u
== 1 || u
== 2) && (v
== 1 || v
== 2))
85 ctlpoints
[u
][v
][2] = 3.0;
87 ctlpoints
[u
][v
][2] = -3.0;
92 static void CALLBACK
nurbsError(GLenum errorCode
)
94 const GLubyte
*estring
;
96 estring
= gluErrorString(errorCode
);
97 fprintf (stderr
, "Nurbs Error: %s\n", estring
);
101 static void CALLBACK
beginCallback(GLenum whichType
)
103 glBegin (whichType
); /* resubmit rendering directive */
105 switch (whichType
) { /* print diagnostic message */
107 printf ("GL_LINES)\n");
110 printf ("GL_LINE_LOOP)\n");
113 printf ("GL_LINE_STRIP)\n");
116 printf ("GL_TRIANGLES)\n");
118 case GL_TRIANGLE_STRIP
:
119 printf ("GL_TRIANGLE_STRIP)\n");
121 case GL_TRIANGLE_FAN
:
122 printf ("GL_TRIANGLE_FAN)\n");
125 printf ("GL_QUADS)\n");
128 printf ("GL_QUAD_STRIP)\n");
131 printf ("GL_POLYGON)\n");
138 static void CALLBACK
endCallback()
140 glEnd(); /* resubmit rendering directive */
141 printf ("glEnd()\n");
144 static void CALLBACK
vertexCallback(GLfloat
*vertex
)
146 glVertex3fv(vertex
); /* resubmit rendering directive */
147 printf ("glVertex3f (%5.3f, %5.3f, %5.3f)\n",
148 vertex
[0], vertex
[1], vertex
[2]);
151 static void CALLBACK
normalCallback(GLfloat
*normal
)
153 glNormal3fv(normal
); /* resubmit rendering directive */
154 printf ("glNormal3f (%5.3f, %5.3f, %5.3f)\n",
155 normal
[0], normal
[1], normal
[2]);
158 /* Initialize material property and depth buffer.
160 static void init(void)
162 GLfloat mat_diffuse
[] = { 0.7, 0.7, 0.7, 1.0 };
163 GLfloat mat_specular
[] = { 1.0, 1.0, 1.0, 1.0 };
164 GLfloat mat_shininess
[] = { 100.0 };
166 glClearColor (0.0, 0.0, 0.0, 0.0);
167 glMaterialfv(GL_FRONT
, GL_DIFFUSE
, mat_diffuse
);
168 glMaterialfv(GL_FRONT
, GL_SPECULAR
, mat_specular
);
169 glMaterialfv(GL_FRONT
, GL_SHININESS
, mat_shininess
);
171 glEnable(GL_LIGHTING
);
173 glEnable(GL_DEPTH_TEST
);
174 glEnable(GL_AUTO_NORMAL
);
175 glEnable(GL_NORMALIZE
);
179 theNurb
= gluNewNurbsRenderer();
180 gluNurbsProperty(theNurb
, GLU_NURBS_MODE
,
181 GLU_NURBS_TESSELLATOR
);
182 gluNurbsProperty(theNurb
, GLU_SAMPLING_TOLERANCE
, 25.0);
183 gluNurbsProperty(theNurb
, GLU_DISPLAY_MODE
, GLU_FILL
);
184 gluNurbsCallback(theNurb
, GLU_ERROR
, nurbsError
);
185 gluNurbsCallback(theNurb
, GLU_NURBS_BEGIN
, beginCallback
);
186 gluNurbsCallback(theNurb
, GLU_NURBS_VERTEX
, vertexCallback
);
187 gluNurbsCallback(theNurb
, GLU_NURBS_NORMAL
, normalCallback
);
188 gluNurbsCallback(theNurb
, GLU_NURBS_END
, endCallback
);
192 static void display(void)
194 GLfloat knots
[8] = {0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0};
197 glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
200 glRotatef(330.0, 1.,0.,0.);
201 glScalef (0.5, 0.5, 0.5);
203 gluBeginSurface(theNurb
);
204 gluNurbsSurface(theNurb
,
206 4 * 3, 3, &ctlpoints
[0][0][0],
207 4, 4, GL_MAP2_VERTEX_3
);
208 gluEndSurface(theNurb
);
212 glDisable(GL_LIGHTING
);
213 glColor3f(1.0, 1.0, 0.0);
215 for (i
= 0; i
< 4; i
++) {
216 for (j
= 0; j
< 4; j
++) {
217 glVertex3f(ctlpoints
[i
][j
][0],
218 ctlpoints
[i
][j
][1], ctlpoints
[i
][j
][2]);
222 glEnable(GL_LIGHTING
);
228 static void reshape(int w
, int h
)
230 glViewport(0, 0, (GLsizei
) w
, (GLsizei
) h
);
231 glMatrixMode(GL_PROJECTION
);
233 gluPerspective (45.0, (GLdouble
)w
/(GLdouble
)h
, 3.0, 8.0);
234 glMatrixMode(GL_MODELVIEW
);
236 glTranslatef (0.0, 0.0, -5.0);
239 static void keyboard(unsigned char key
, int x
, int y
)
244 showPoints
= !showPoints
;
255 int main(int argc
, char** argv
)
257 glutInit(&argc
, argv
);
258 glutInitDisplayMode(GLUT_SINGLE
| GLUT_RGB
| GLUT_DEPTH
);
259 glutInitWindowSize (500, 500);
260 glutInitWindowPosition (100, 100);
261 glutCreateWindow(argv
[0]);
263 glutReshapeFunc(reshape
);
264 glutDisplayFunc(display
);
265 glutKeyboardFunc (keyboard
);
271 int main(int argc
, char** argv
)
273 fprintf (stderr
, "This program demonstrates a feature which is introduced in the\n");
274 fprintf (stderr
, "OpenGL Utility Library (GLU) Version 1.3.\n");
275 fprintf (stderr
, "If your implementation of GLU has the right extensions,\n");
276 fprintf (stderr
, "you may be able to modify this program to make it run.\n");