Use correct PV when clipping.
[mesa.git] / progs / redbook / surface.c
1 /* aux2glut conversion Copyright (c) Mark J. Kilgard, 1994, 1995 */
2
3 /**
4 * (c) Copyright 1993, Silicon Graphics, Inc.
5 * ALL RIGHTS RESERVED
6 * Permission to use, copy, modify, and distribute this software for
7 * any purpose and without fee is hereby granted, provided that the above
8 * copyright notice appear in all copies and that both the copyright notice
9 * and this permission notice appear in supporting documentation, and that
10 * the name of Silicon Graphics, Inc. not be used in advertising
11 * or publicity pertaining to distribution of the software without specific,
12 * written prior permission.
13 *
14 * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
15 * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
17 * FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON
18 * GRAPHICS, INC. BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
19 * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
20 * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
21 * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
22 * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC. HAS BEEN
23 * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
24 * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
25 * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
26 *
27 * US Government Users Restricted Rights
28 * Use, duplication, or disclosure by the Government is subject to
29 * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
30 * (c)(1)(ii) of the Rights in Technical Data and Computer Software
31 * clause at DFARS 252.227-7013 and/or in similar or successor
32 * clauses in the FAR or the DOD or NASA FAR Supplement.
33 * Unpublished-- rights reserved under the copyright laws of the
34 * United States. Contractor/manufacturer is Silicon Graphics,
35 * Inc., 2011 N. Shoreline Blvd., Mountain View, CA 94039-7311.
36 *
37 * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
38 */
39 /**
40 * surface.c
41 * This program draws a NURBS surface in the shape of a
42 * symmetrical hill.
43 */
44 #include <GL/glut.h>
45
46 GLfloat ctlpoints[4][4][3];
47 int showPoints = 0;
48
49 GLUnurbsObj *theNurb;
50
51 /*
52 * Initializes the control points of the surface to a small hill.
53 * The control points range from -3 to +3 in x, y, and z
54 */
55 void init_surface(void)
56 {
57 int u, v;
58 for (u = 0; u < 4; u++) {
59 for (v = 0; v < 4; v++) {
60 ctlpoints[u][v][0] = 2.0*((GLfloat)u - 1.5);
61 ctlpoints[u][v][1] = 2.0*((GLfloat)v - 1.5);
62
63 if ( (u == 1 || u == 2) && (v == 1 || v == 2))
64 ctlpoints[u][v][2] = 7.0;
65 else
66 ctlpoints[u][v][2] = -3.0;
67 }
68 }
69 }
70
71 /* Initialize material property and depth buffer.
72 */
73 void myinit(void)
74 {
75 GLfloat mat_diffuse[] = { 0.7, 0.7, 0.7, 1.0 };
76 GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
77 GLfloat mat_shininess[] = { 100.0 };
78
79 glClearColor (0.0, 0.0, 0.0, 1.0);
80 glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
81 glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
82 glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
83
84 glEnable(GL_LIGHTING);
85 glEnable(GL_LIGHT0);
86 glDepthFunc(GL_LESS);
87 glEnable(GL_DEPTH_TEST);
88 glEnable(GL_AUTO_NORMAL);
89 glEnable(GL_NORMALIZE);
90
91 init_surface();
92
93 theNurb = gluNewNurbsRenderer();
94 gluNurbsProperty(theNurb, GLU_SAMPLING_TOLERANCE, 25.0);
95 gluNurbsProperty(theNurb, GLU_DISPLAY_MODE, GLU_FILL);
96
97 glMatrixMode(GL_MODELVIEW);
98 glLoadIdentity();
99 glTranslatef (0.0, 0.0, -5.0);
100 }
101
102 void display(void)
103 {
104 GLfloat knots[8] = {0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0};
105 int i, j;
106
107 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
108
109 glPushMatrix();
110 glRotatef(330.0, 1.,0.,0.);
111 glScalef (0.25, 0.25, 0.25);
112
113 gluBeginSurface(theNurb);
114 gluNurbsSurface(theNurb,
115 8, knots,
116 8, knots,
117 4 * 3,
118 3,
119 &ctlpoints[0][0][0],
120 4, 4,
121 GL_MAP2_VERTEX_3);
122 gluEndSurface(theNurb);
123
124 if(showPoints) {
125 glPointSize(5.0);
126 glDisable(GL_LIGHTING);
127 glColor3f(1.0, 1.0, 0.0);
128 glBegin(GL_POINTS);
129 for(i=0;i<4;i++) {
130 for(j=0;j<4;j++) {
131 glVertex3f(ctlpoints[i][j][0], ctlpoints[i][j][1], ctlpoints[i][j][2]);
132 }
133 }
134 glEnd();
135 glEnable(GL_LIGHTING);
136 }
137
138 glPopMatrix();
139 glutSwapBuffers();
140 }
141
142 void reshape(int w, int h)
143 {
144 glViewport(0, 0, w, h);
145 glMatrixMode(GL_PROJECTION);
146 glLoadIdentity();
147 gluPerspective (45.0, (GLdouble)w/(GLdouble)h, 3.0, 8.0);
148
149 glMatrixMode(GL_MODELVIEW);
150 }
151
152 void
153 menu(int value)
154 {
155 switch (value) {
156 case 0:
157 case 1:
158 showPoints = value;
159 break;
160 case 2:
161 gluNurbsProperty(theNurb, GLU_DISPLAY_MODE, GLU_FILL);
162 break;
163 case 3:
164 gluNurbsProperty(theNurb, GLU_DISPLAY_MODE, GLU_OUTLINE_POLYGON);
165 break;
166 }
167 glutPostRedisplay();
168 }
169
170 int down = 0, lastx;
171
172 /* ARGSUSED1 */
173 void
174 motion(int x, int y)
175 {
176 if (down) {
177 glRotatef(lastx - x, 0, 1, 0);
178 lastx = x;
179 glutPostRedisplay();
180 }
181 }
182
183 /* ARGSUSED3 */
184 void
185 mouse(int button, int state, int x, int y)
186 {
187 if (button == GLUT_LEFT_BUTTON) {
188 if (state == GLUT_DOWN) {
189 lastx = x;
190 down = 1;
191 } else {
192 down = 0;
193 }
194 }
195 }
196
197 static void
198 key(unsigned char k, int x, int y)
199 {
200 switch (k) {
201 case 27: /* Escape */
202 exit(0);
203 break;
204 default:
205 return;
206 }
207 glutPostRedisplay();
208 }
209
210 /* Main Loop */
211 int
212 main(int argc, char** argv)
213 {
214 glutInit(&argc, argv);
215 glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGB);
216 glutCreateWindow(argv[0]);
217 myinit();
218 glutReshapeFunc(reshape);
219 glutDisplayFunc(display);
220 glutCreateMenu(menu);
221 glutAddMenuEntry("Show control points", 1);
222 glutAddMenuEntry("Hide control points", 0);
223 glutAddMenuEntry("Solid", 2);
224 glutAddMenuEntry("Wireframe", 3);
225 glutAttachMenu(GLUT_RIGHT_BUTTON);
226 glutMouseFunc(mouse);
227 glutMotionFunc(motion);
228 glutKeyboardFunc(key);
229 glutMainLoop();
230 return 0; /* ANSI C requires main to return int. */
231 }