auxbuffer: Don't override $DISPLAY from the environment
[mesa-demos.git] / src / redbook / trim.c
blob412506de77a42b330acf13c0313d51b9726718ee
1 /*
2 * Copyright (c) 1993-1997, Silicon Graphics, Inc.
3 * ALL RIGHTS RESERVED
4 * Permission to use, copy, modify, and distribute this software for
5 * any purpose and without fee is hereby granted, provided that the above
6 * copyright notice appear in all copies and that both the copyright notice
7 * and this permission notice appear in supporting documentation, and that
8 * the name of Silicon Graphics, Inc. not be used in advertising
9 * or publicity pertaining to distribution of the software without specific,
10 * written prior permission.
12 * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
13 * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
14 * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
15 * FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON
16 * GRAPHICS, INC. BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
17 * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
18 * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
19 * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
20 * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC. HAS BEEN
21 * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
22 * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
23 * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
25 * US Government Users Restricted Rights
26 * Use, duplication, or disclosure by the Government is subject to
27 * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
28 * (c)(1)(ii) of the Rights in Technical Data and Computer Software
29 * clause at DFARS 252.227-7013 and/or in similar or successor
30 * clauses in the FAR or the DOD or NASA FAR Supplement.
31 * Unpublished-- rights reserved under the copyright laws of the
32 * United States. Contractor/manufacturer is Silicon Graphics,
33 * Inc., 2011 N. Shoreline Blvd., Mountain View, CA 94039-7311.
35 * OpenGL(R) is a registered trademark of Silicon Graphics, Inc.
39 * trim.c
40 * This program draws a NURBS surface in the shape of a
41 * symmetrical hill, using both a NURBS curve and pwl
42 * (piecewise linear) curve to trim part of the surface.
44 #include <stdlib.h>
45 #include "glut_wrap.h"
46 #include <stdio.h>
49 GLfloat ctlpoints[4][4][3];
51 GLUnurbsObj *theNurb;
54 * Initializes the control points of the surface to a small hill.
55 * The control points range from -3 to +3 in x, y, and z
57 static void init_surface(void)
59 int u, v;
60 for (u = 0; u < 4; u++) {
61 for (v = 0; v < 4; v++) {
62 ctlpoints[u][v][0] = 2.0*((GLfloat)u - 1.5);
63 ctlpoints[u][v][1] = 2.0*((GLfloat)v - 1.5);
65 if ( (u == 1 || u == 2) && (v == 1 || v == 2))
66 ctlpoints[u][v][2] = 3.0;
67 else
68 ctlpoints[u][v][2] = -3.0;
73 static void GLAPIENTRY nurbsError(GLenum errorCode)
75 const GLubyte *estring;
77 estring = gluErrorString(errorCode);
78 fprintf (stderr, "Nurbs Error: %s\n", (char *) estring);
79 exit (0);
82 /* Initialize material property and depth buffer.
84 static void init(void)
86 GLfloat mat_diffuse[] = { 0.7, 0.7, 0.7, 1.0 };
87 GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
88 GLfloat mat_shininess[] = { 100.0 };
90 glClearColor (0.0, 0.0, 0.0, 0.0);
91 glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
92 glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
93 glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
95 glEnable(GL_LIGHTING);
96 glEnable(GL_LIGHT0);
97 glEnable(GL_DEPTH_TEST);
98 glEnable(GL_AUTO_NORMAL);
99 glEnable(GL_NORMALIZE);
101 init_surface();
103 theNurb = gluNewNurbsRenderer();
104 gluNurbsProperty(theNurb, GLU_SAMPLING_TOLERANCE, 25.0);
105 gluNurbsProperty(theNurb, GLU_DISPLAY_MODE, GLU_FILL);
106 gluNurbsCallback(theNurb, GLU_ERROR, nurbsError);
109 static void display(void)
111 GLfloat knots[8] = {0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0};
112 GLfloat edgePt[5][2] = /* counter clockwise */
113 {{0.0, 0.0}, {1.0, 0.0}, {1.0, 1.0}, {0.0, 1.0}, {0.0, 0.0}};
114 GLfloat curvePt[4][2] = /* clockwise */
115 {{0.25, 0.5}, {0.25, 0.75}, {0.75, 0.75}, {0.75, 0.5}};
116 GLfloat curveKnots[8] =
117 {0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0};
118 GLfloat pwlPt[4][2] = /* clockwise */
119 {{0.75, 0.5}, {0.5, 0.25}, {0.25, 0.5}};
121 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
122 glPushMatrix();
123 glRotatef(330.0, 1.,0.,0.);
124 glScalef (0.5, 0.5, 0.5);
126 gluBeginSurface(theNurb);
127 gluNurbsSurface(theNurb, 8, knots, 8, knots,
128 4 * 3, 3, &ctlpoints[0][0][0],
129 4, 4, GL_MAP2_VERTEX_3);
130 gluBeginTrim (theNurb);
131 gluPwlCurve (theNurb, 5, &edgePt[0][0], 2, GLU_MAP1_TRIM_2);
132 gluEndTrim (theNurb);
133 gluBeginTrim (theNurb);
134 gluNurbsCurve (theNurb, 8, curveKnots, 2,
135 &curvePt[0][0], 4, GLU_MAP1_TRIM_2);
136 gluPwlCurve (theNurb, 3, &pwlPt[0][0], 2, GLU_MAP1_TRIM_2);
137 gluEndTrim (theNurb);
138 gluEndSurface(theNurb);
140 glPopMatrix();
141 glFlush();
144 static void reshape(int w, int h)
146 glViewport(0, 0, (GLsizei) w, (GLsizei) h);
147 glMatrixMode(GL_PROJECTION);
148 glLoadIdentity();
149 gluPerspective (45.0, (GLdouble)w/(GLdouble)h, 3.0, 8.0);
151 glMatrixMode(GL_MODELVIEW);
152 glLoadIdentity();
153 glTranslatef (0.0, 0.0, -5.0);
156 /* ARGSUSED1 */
157 static void keyboard(unsigned char key, int x, int y)
159 switch (key) {
160 case 27:
161 exit(0);
162 break;
166 /* Main Loop
168 int main(int argc, char** argv)
170 glutInit(&argc, argv);
171 glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
172 glutInitWindowSize (500, 500);
173 glutInitWindowPosition (100, 100);
174 glutCreateWindow(argv[0]);
175 init();
176 glutReshapeFunc(reshape);
177 glutDisplayFunc(display);
178 glutKeyboardFunc (keyboard);
179 glutMainLoop();
180 return 0;