nvfx: expose GLSL
[mesa/mesa-lb.git] / progs / samples / nurb.c
blobdc8991295e8d6e5ec89882f8516dcbc20e10b138
1 /*
2 * Copyright (c) 1991, 1992, 1993 Silicon Graphics, Inc.
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.
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.
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.
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <math.h>
29 #include <GL/glut.h>
32 #ifndef CALLBACK
33 #define CALLBACK
34 #endif
37 #define INREAL float
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
48 typedef INREAL Point[4];
51 GLenum doubleBuffer;
53 GLenum expectedError;
54 GLint rotX = 40, rotY = 40;
55 INREAL sknots[S_NUMKNOTS] = {
56 -1.0,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SQRT_TWO*2.0, SQRT_TWO
167 SQRT_TWO*2.0, SQRT_TWO*6.0, SQRT_TWO*2.5, SQRT_TWO
170 SQRT_TWO*2.0, SQRT_TWO*6.0, SQRT_TWO*3.0, SQRT_TWO
175 2.0, 5.0, 2.0, 1.0
178 2.0, 5.0, 2.5, 1.0
181 2.0, 5.0, 3.0, 1.0
186 3.0, 4.0, 2.0, 1.0
189 3.0, 4.0, 2.5, 1.0
192 3.0, 4.0, 3.0, 1.0
197 4.0, 2.0, 2.0, 1.0
200 4.0, 1.6, 2.5, 1.0
203 4.0, 2.0, 3.0, 1.0
207 GLUnurbsObj *theNurbs;
210 static void CALLBACK ErrorCallback(GLenum which)
213 if (which != expectedError) {
214 fprintf(stderr, "Unexpected error occured (%d):\n", which);
215 fprintf(stderr, " %s\n", (char *) gluErrorString(which));
219 typedef void (GLAPIENTRY *callback_t)();
221 static void Init(void)
224 theNurbs = gluNewNurbsRenderer();
225 gluNurbsCallback(theNurbs, GLU_ERROR, (callback_t) ErrorCallback);
227 gluNurbsProperty(theNurbs, GLU_SAMPLING_TOLERANCE, 15.0);
228 gluNurbsProperty(theNurbs, GLU_DISPLAY_MODE, GLU_OUTLINE_PATCH);
230 expectedError = GLU_INVALID_ENUM;
231 gluNurbsProperty(theNurbs, ~0, 15.0);
232 expectedError = GLU_NURBS_ERROR13;
233 gluEndSurface(theNurbs);
234 expectedError = 0;
236 glColor3f(1.0, 1.0, 1.0);
239 static void Reshape(int width, int height)
242 glViewport(0, 0, (GLint)width, (GLint)height);
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);
251 static void Key2(int key, int x, int y)
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;
271 glutPostRedisplay();
274 static void Key(unsigned char key, int x, int y)
277 switch (key) {
278 case 27:
279 exit(1);
283 static void Draw(void)
286 glClear(GL_COLOR_BUFFER_BIT);
288 glPushMatrix();
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);
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);
301 glPopMatrix();
303 glFlush();
305 if (doubleBuffer) {
306 glutSwapBuffers();
310 static GLenum Args(int argc, char **argv)
312 GLint i;
314 doubleBuffer = GL_FALSE;
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;
326 return GL_TRUE;
329 int main(int argc, char **argv)
331 GLenum type;
333 glutInit(&argc, argv);
335 if (Args(argc, argv) == GL_FALSE) {
336 exit(1);
339 glutInitWindowPosition(0, 0); glutInitWindowSize( 300, 300);
341 type = GLUT_RGB;
342 type |= (doubleBuffer) ? GLUT_DOUBLE : GLUT_SINGLE;
343 glutInitDisplayMode(type);
345 if (glutCreateWindow("NURBS Test") == GL_FALSE) {
346 exit(1);
349 Init();
351 glutReshapeFunc(Reshape);
352 glutKeyboardFunc(Key);
353 glutSpecialFunc(Key2);
354 glutDisplayFunc(Draw);
355 glutMainLoop();
356 return 0;