Merge branch '7.8'
[mesa.git] / progs / redbook / surfpoints.c
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 <GL/glut.h>
58 #include <stdlib.h>
59 #include <stdio.h>
60
61 #ifdef GLU_VERSION_1_3
62
63 #ifndef CALLBACK
64 #define CALLBACK
65 #endif
66
67 GLfloat ctlpoints[4][4][3];
68 int showPoints = 0;
69
70 GLUnurbsObj *theNurb;
71
72 /*
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
75 */
76 static void init_surface(void)
77 {
78 int u, v;
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);
83
84 if ( (u == 1 || u == 2) && (v == 1 || v == 2))
85 ctlpoints[u][v][2] = 3.0;
86 else
87 ctlpoints[u][v][2] = -3.0;
88 }
89 }
90 }
91
92 static void CALLBACK nurbsError(GLenum errorCode)
93 {
94 const GLubyte *estring;
95
96 estring = gluErrorString(errorCode);
97 fprintf (stderr, "Nurbs Error: %s\n", estring);
98 exit (0);
99 }
100
101 static void CALLBACK beginCallback(GLenum whichType)
102 {
103 glBegin (whichType); /* resubmit rendering directive */
104 printf ("glBegin(");
105 switch (whichType) { /* print diagnostic message */
106 case GL_LINES:
107 printf ("GL_LINES)\n");
108 break;
109 case GL_LINE_LOOP:
110 printf ("GL_LINE_LOOP)\n");
111 break;
112 case GL_LINE_STRIP:
113 printf ("GL_LINE_STRIP)\n");
114 break;
115 case GL_TRIANGLES:
116 printf ("GL_TRIANGLES)\n");
117 break;
118 case GL_TRIANGLE_STRIP:
119 printf ("GL_TRIANGLE_STRIP)\n");
120 break;
121 case GL_TRIANGLE_FAN:
122 printf ("GL_TRIANGLE_FAN)\n");
123 break;
124 case GL_QUADS:
125 printf ("GL_QUADS)\n");
126 break;
127 case GL_QUAD_STRIP:
128 printf ("GL_QUAD_STRIP)\n");
129 break;
130 case GL_POLYGON:
131 printf ("GL_POLYGON)\n");
132 break;
133 default:
134 break;
135 }
136 }
137
138 static void CALLBACK endCallback()
139 {
140 glEnd(); /* resubmit rendering directive */
141 printf ("glEnd()\n");
142 }
143
144 static void CALLBACK vertexCallback(GLfloat *vertex)
145 {
146 glVertex3fv(vertex); /* resubmit rendering directive */
147 printf ("glVertex3f (%5.3f, %5.3f, %5.3f)\n",
148 vertex[0], vertex[1], vertex[2]);
149 }
150
151 static void CALLBACK normalCallback(GLfloat *normal)
152 {
153 glNormal3fv(normal); /* resubmit rendering directive */
154 printf ("glNormal3f (%5.3f, %5.3f, %5.3f)\n",
155 normal[0], normal[1], normal[2]);
156 }
157
158 /* Initialize material property and depth buffer.
159 */
160 static void init(void)
161 {
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 };
165
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);
170
171 glEnable(GL_LIGHTING);
172 glEnable(GL_LIGHT0);
173 glEnable(GL_DEPTH_TEST);
174 glEnable(GL_AUTO_NORMAL);
175 glEnable(GL_NORMALIZE);
176
177 init_surface();
178
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);
189
190 }
191
192 static void display(void)
193 {
194 GLfloat knots[8] = {0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0};
195 int i, j;
196
197 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
198
199 glPushMatrix();
200 glRotatef(330.0, 1.,0.,0.);
201 glScalef (0.5, 0.5, 0.5);
202
203 gluBeginSurface(theNurb);
204 gluNurbsSurface(theNurb,
205 8, knots, 8, knots,
206 4 * 3, 3, &ctlpoints[0][0][0],
207 4, 4, GL_MAP2_VERTEX_3);
208 gluEndSurface(theNurb);
209
210 if (showPoints) {
211 glPointSize(5.0);
212 glDisable(GL_LIGHTING);
213 glColor3f(1.0, 1.0, 0.0);
214 glBegin(GL_POINTS);
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]);
219 }
220 }
221 glEnd();
222 glEnable(GL_LIGHTING);
223 }
224 glPopMatrix();
225 glFlush();
226 }
227
228 static void reshape(int w, int h)
229 {
230 glViewport(0, 0, (GLsizei) w, (GLsizei) h);
231 glMatrixMode(GL_PROJECTION);
232 glLoadIdentity();
233 gluPerspective (45.0, (GLdouble)w/(GLdouble)h, 3.0, 8.0);
234 glMatrixMode(GL_MODELVIEW);
235 glLoadIdentity();
236 glTranslatef (0.0, 0.0, -5.0);
237 }
238
239 static void keyboard(unsigned char key, int x, int y)
240 {
241 switch (key) {
242 case 'c':
243 case 'C':
244 showPoints = !showPoints;
245 glutPostRedisplay();
246 break;
247 case 27:
248 exit(0);
249 break;
250 default:
251 break;
252 }
253 }
254
255 int main(int argc, char** argv)
256 {
257 glutInit(&argc, argv);
258 glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
259 glutInitWindowSize (500, 500);
260 glutInitWindowPosition (100, 100);
261 glutCreateWindow(argv[0]);
262 init();
263 glutReshapeFunc(reshape);
264 glutDisplayFunc(display);
265 glutKeyboardFunc (keyboard);
266 glutMainLoop();
267 return 0;
268 }
269
270 #else
271 int main(int argc, char** argv)
272 {
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");
277 return 0;
278 }
279 #endif
280