st/mesa: fix incorrect RowStride computation
[mesa.git] / progs / samples / nurb.c
1 /*
2 * Copyright (c) 1991, 1992, 1993 Silicon Graphics, Inc.
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and
5 * its documentation for any purpose is hereby granted without fee, provided
6 * that (i) the above copyright notices and this permission notice appear in
7 * all copies of the software and related documentation, and (ii) the name of
8 * Silicon Graphics may not be used in any advertising or
9 * publicity relating to the software without the specific, prior written
10 * permission of Silicon Graphics.
11 *
12 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF
13 * ANY KIND,
14 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
15 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
16 *
17 * IN NO EVENT SHALL SILICON GRAPHICS BE LIABLE FOR
18 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
19 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
20 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
21 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
22 * OF THIS SOFTWARE.
23 */
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <math.h>
29 #include <GL/glut.h>
30
31
32 #ifndef CALLBACK
33 #define CALLBACK
34 #endif
35
36
37 #define INREAL float
38
39 #define S_NUMPOINTS 13
40 #define S_ORDER 3
41 #define S_NUMKNOTS (S_NUMPOINTS + S_ORDER)
42 #define T_NUMPOINTS 3
43 #define T_ORDER 3
44 #define T_NUMKNOTS (T_NUMPOINTS + T_ORDER)
45 #define SQRT_TWO 1.41421356237309504880
46
47
48 typedef INREAL Point[4];
49
50
51 GLenum doubleBuffer;
52
53 GLenum expectedError;
54 GLint rotX = 40, rotY = 40;
55 INREAL sknots[S_NUMKNOTS] = {
56 -1.0, -1.0, -1.0, 0.0, 1.0, 2.0, 3.0, 4.0,
57 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 9.0, 9.0
58 };
59 INREAL tknots[T_NUMKNOTS] = {
60 1.0, 1.0, 1.0, 2.0, 2.0, 2.0
61 };
62 Point ctlpoints[S_NUMPOINTS][T_NUMPOINTS] = {
63 {
64 {
65 4.0, 2.0, 2.0, 1.0
66 },
67 {
68 4.0, 1.6, 2.5, 1.0
69 },
70 {
71 4.0, 2.0, 3.0, 1.0
72 }
73 },
74 {
75 {
76 5.0, 4.0, 2.0, 1.0
77 },
78 {
79 5.0, 4.0, 2.5, 1.0
80 },
81 {
82 5.0, 4.0, 3.0, 1.0
83 }
84 },
85 {
86 {
87 6.0, 5.0, 2.0, 1.0
88 },
89 {
90 6.0, 5.0, 2.5, 1.0
91 },
92 {
93 6.0, 5.0, 3.0, 1.0
94 }
95 },
96 {
97 {
98 SQRT_TWO*6.0, SQRT_TWO*6.0, SQRT_TWO*2.0, SQRT_TWO
99 },
100 {
101 SQRT_TWO*6.0, SQRT_TWO*6.0, SQRT_TWO*2.5, SQRT_TWO
102 },
103 {
104 SQRT_TWO*6.0, SQRT_TWO*6.0, SQRT_TWO*3.0, SQRT_TWO
105 }
106 },
107 {
108 {
109 5.2, 6.7, 2.0, 1.0
110 },
111 {
112 5.2, 6.7, 2.5, 1.0
113 },
114 {
115 5.2, 6.7, 3.0, 1.0
116 }
117 },
118 {
119 {
120 SQRT_TWO*4.0, SQRT_TWO*6.0, SQRT_TWO*2.0, SQRT_TWO
121 },
122 {
123 SQRT_TWO*4.0, SQRT_TWO*6.0, SQRT_TWO*2.5, SQRT_TWO
124 },
125 {
126 SQRT_TWO*4.0, SQRT_TWO*6.0, SQRT_TWO*3.0, SQRT_TWO
127 }
128 },
129 {
130 {
131 4.0, 5.2, 2.0, 1.0
132 },
133 {
134 4.0, 4.6, 2.5, 1.0
135 },
136 {
137 4.0, 5.2, 3.0, 1.0
138 }
139 },
140 {
141 {
142 SQRT_TWO*4.0, SQRT_TWO*6.0, SQRT_TWO*2.0, SQRT_TWO
143 },
144 {
145 SQRT_TWO*4.0, SQRT_TWO*6.0, SQRT_TWO*2.5, SQRT_TWO
146 },
147 {
148 SQRT_TWO*4.0, SQRT_TWO*6.0, SQRT_TWO*3.0, SQRT_TWO
149 }
150 },
151 {
152 {
153 2.8, 6.7, 2.0, 1.0
154 },
155 {
156 2.8, 6.7, 2.5, 1.0
157 },
158 {
159 2.8, 6.7, 3.0, 1.0
160 }
161 },
162 {
163 {
164 SQRT_TWO*2.0, SQRT_TWO*6.0, SQRT_TWO*2.0, SQRT_TWO
165 },
166 {
167 SQRT_TWO*2.0, SQRT_TWO*6.0, SQRT_TWO*2.5, SQRT_TWO
168 },
169 {
170 SQRT_TWO*2.0, SQRT_TWO*6.0, SQRT_TWO*3.0, SQRT_TWO
171 }
172 },
173 {
174 {
175 2.0, 5.0, 2.0, 1.0
176 },
177 {
178 2.0, 5.0, 2.5, 1.0
179 },
180 {
181 2.0, 5.0, 3.0, 1.0
182 }
183 },
184 {
185 {
186 3.0, 4.0, 2.0, 1.0
187 },
188 {
189 3.0, 4.0, 2.5, 1.0
190 },
191 {
192 3.0, 4.0, 3.0, 1.0
193 }
194 },
195 {
196 {
197 4.0, 2.0, 2.0, 1.0
198 },
199 {
200 4.0, 1.6, 2.5, 1.0
201 },
202 {
203 4.0, 2.0, 3.0, 1.0
204 }
205 }
206 };
207 GLUnurbsObj *theNurbs;
208
209
210 static void CALLBACK ErrorCallback(GLenum which)
211 {
212
213 if (which != expectedError) {
214 fprintf(stderr, "Unexpected error occured (%d):\n", which);
215 fprintf(stderr, " %s\n", (char *) gluErrorString(which));
216 }
217 }
218
219 typedef void (GLAPIENTRY *callback_t)();
220
221 static void Init(void)
222 {
223
224 theNurbs = gluNewNurbsRenderer();
225 gluNurbsCallback(theNurbs, GLU_ERROR, (callback_t) ErrorCallback);
226
227 gluNurbsProperty(theNurbs, GLU_SAMPLING_TOLERANCE, 15.0);
228 gluNurbsProperty(theNurbs, GLU_DISPLAY_MODE, GLU_OUTLINE_PATCH);
229
230 expectedError = GLU_INVALID_ENUM;
231 gluNurbsProperty(theNurbs, ~0, 15.0);
232 expectedError = GLU_NURBS_ERROR13;
233 gluEndSurface(theNurbs);
234 expectedError = 0;
235
236 glColor3f(1.0, 1.0, 1.0);
237 }
238
239 static void Reshape(int width, int height)
240 {
241
242 glViewport(0, 0, (GLint)width, (GLint)height);
243
244 glMatrixMode(GL_PROJECTION);
245 glLoadIdentity();
246 glFrustum(-2.0, 2.0, -2.0, 2.0, 0.8, 10.0);
247 gluLookAt(7.0, 4.5, 4.0, 4.5, 4.5, 2.5, 6.0, -3.0, 2.0);
248 glMatrixMode(GL_MODELVIEW);
249 }
250
251 static void Key2(int key, int x, int y)
252 {
253
254 switch (key) {
255 case GLUT_KEY_DOWN:
256 rotX -= 5;
257 break;
258 case GLUT_KEY_UP:
259 rotX += 5;
260 break;
261 case GLUT_KEY_LEFT:
262 rotY -= 5;
263 break;
264 case GLUT_KEY_RIGHT:
265 rotY += 5;
266 break;
267 default:
268 return;
269 }
270
271 glutPostRedisplay();
272 }
273
274 static void Key(unsigned char key, int x, int y)
275 {
276
277 switch (key) {
278 case 27:
279 exit(1);
280 }
281 }
282
283 static void Draw(void)
284 {
285
286 glClear(GL_COLOR_BUFFER_BIT);
287
288 glPushMatrix();
289
290 glTranslatef(4.0, 4.5, 2.5);
291 glRotatef(rotY, 1, 0, 0);
292 glRotatef(rotX, 0, 1, 0);
293 glTranslatef(-4.0, -4.5, -2.5);
294
295 gluBeginSurface(theNurbs);
296 gluNurbsSurface(theNurbs, S_NUMKNOTS, sknots, T_NUMKNOTS, tknots,
297 4*T_NUMPOINTS, 4, &ctlpoints[0][0][0], S_ORDER,
298 T_ORDER, GL_MAP2_VERTEX_4);
299 gluEndSurface(theNurbs);
300
301 glPopMatrix();
302
303 glFlush();
304
305 if (doubleBuffer) {
306 glutSwapBuffers();
307 }
308 }
309
310 static GLenum Args(int argc, char **argv)
311 {
312 GLint i;
313
314 doubleBuffer = GL_FALSE;
315
316 for (i = 1; i < argc; i++) {
317 if (strcmp(argv[i], "-sb") == 0) {
318 doubleBuffer = GL_FALSE;
319 } else if (strcmp(argv[i], "-db") == 0) {
320 doubleBuffer = GL_TRUE;
321 } else {
322 printf("%s (Bad option).\n", argv[i]);
323 return GL_FALSE;
324 }
325 }
326 return GL_TRUE;
327 }
328
329 int main(int argc, char **argv)
330 {
331 GLenum type;
332
333 glutInit(&argc, argv);
334
335 if (Args(argc, argv) == GL_FALSE) {
336 exit(1);
337 }
338
339 glutInitWindowPosition(0, 0); glutInitWindowSize( 300, 300);
340
341 type = GLUT_RGB;
342 type |= (doubleBuffer) ? GLUT_DOUBLE : GLUT_SINGLE;
343 glutInitDisplayMode(type);
344
345 if (glutCreateWindow("NURBS Test") == GL_FALSE) {
346 exit(1);
347 }
348
349 Init();
350
351 glutReshapeFunc(Reshape);
352 glutKeyboardFunc(Key);
353 glutSpecialFunc(Key2);
354 glutDisplayFunc(Draw);
355 glutMainLoop();
356 return 0;
357 }