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
57 #include "glut_wrap.h"
61 #ifdef GLU_VERSION_1_3
63 GLfloat ctlpoints
[4][4][3];
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
72 static void init_surface(void)
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);
80 if ( (u
== 1 || u
== 2) && (v
== 1 || v
== 2))
81 ctlpoints
[u
][v
][2] = 3.0;
83 ctlpoints
[u
][v
][2] = -3.0;
88 static void GLAPIENTRY
nurbsError(GLenum errorCode
)
90 const GLubyte
*estring
;
92 estring
= gluErrorString(errorCode
);
93 fprintf (stderr
, "Nurbs Error: %s\n", estring
);
97 static void GLAPIENTRY
beginCallback(GLenum whichType
)
99 glBegin (whichType
); /* resubmit rendering directive */
101 switch (whichType
) { /* print diagnostic message */
103 printf ("GL_LINES)\n");
106 printf ("GL_LINE_LOOP)\n");
109 printf ("GL_LINE_STRIP)\n");
112 printf ("GL_TRIANGLES)\n");
114 case GL_TRIANGLE_STRIP
:
115 printf ("GL_TRIANGLE_STRIP)\n");
117 case GL_TRIANGLE_FAN
:
118 printf ("GL_TRIANGLE_FAN)\n");
121 printf ("GL_QUADS)\n");
124 printf ("GL_QUAD_STRIP)\n");
127 printf ("GL_POLYGON)\n");
134 static void GLAPIENTRY
endCallback()
136 glEnd(); /* resubmit rendering directive */
137 printf ("glEnd()\n");
140 static void GLAPIENTRY
vertexCallback(GLfloat
*vertex
)
142 glVertex3fv(vertex
); /* resubmit rendering directive */
143 printf ("glVertex3f (%5.3f, %5.3f, %5.3f)\n",
144 vertex
[0], vertex
[1], vertex
[2]);
147 static void GLAPIENTRY
normalCallback(GLfloat
*normal
)
149 glNormal3fv(normal
); /* resubmit rendering directive */
150 printf ("glNormal3f (%5.3f, %5.3f, %5.3f)\n",
151 normal
[0], normal
[1], normal
[2]);
154 /* Initialize material property and depth buffer.
156 static void init(void)
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 };
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
);
167 glEnable(GL_LIGHTING
);
169 glEnable(GL_DEPTH_TEST
);
170 glEnable(GL_AUTO_NORMAL
);
171 glEnable(GL_NORMALIZE
);
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
);
188 static void display(void)
190 GLfloat knots
[8] = {0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0};
193 glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
196 glRotatef(330.0, 1.,0.,0.);
197 glScalef (0.5, 0.5, 0.5);
199 gluBeginSurface(theNurb
);
200 gluNurbsSurface(theNurb
,
202 4 * 3, 3, &ctlpoints
[0][0][0],
203 4, 4, GL_MAP2_VERTEX_3
);
204 gluEndSurface(theNurb
);
208 glDisable(GL_LIGHTING
);
209 glColor3f(1.0, 1.0, 0.0);
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]);
218 glEnable(GL_LIGHTING
);
224 static void reshape(int w
, int h
)
226 glViewport(0, 0, (GLsizei
) w
, (GLsizei
) h
);
227 glMatrixMode(GL_PROJECTION
);
229 gluPerspective (45.0, (GLdouble
)w
/(GLdouble
)h
, 3.0, 8.0);
230 glMatrixMode(GL_MODELVIEW
);
232 glTranslatef (0.0, 0.0, -5.0);
235 static void keyboard(unsigned char key
, int x
, int y
)
240 showPoints
= !showPoints
;
251 int main(int argc
, char** argv
)
253 glutInit(&argc
, argv
);
254 glutInitDisplayMode(GLUT_SINGLE
| GLUT_RGB
| GLUT_DEPTH
);
255 glutInitWindowSize (500, 500);
256 glutInitWindowPosition (100, 100);
257 glutCreateWindow(argv
[0]);
259 glutReshapeFunc(reshape
);
260 glutDisplayFunc(display
);
261 glutKeyboardFunc (keyboard
);
267 int main(int argc
, char** argv
)
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");