blob: 8ff15741889fc37ba8a2680961234089d7bbd849 [file] [log] [blame]
jtgafb833d1999-08-19 00:55:39 +00001/* aux2glut conversion Copyright (c) Mark J. Kilgard, 1994, 1995 */
2
3/**
4 * (c) Copyright 1993, Silicon Graphics, Inc.
Ted Jump83c02ef1999-09-17 02:40:51 +00005 * ALL RIGHTS RESERVED
6 * Permission to use, copy, modify, and distribute this software for
jtgafb833d1999-08-19 00:55:39 +00007 * 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
Ted Jump83c02ef1999-09-17 02:40:51 +00009 * and this permission notice appear in supporting documentation, and that
jtgafb833d1999-08-19 00:55:39 +000010 * the name of Silicon Graphics, Inc. not be used in advertising
11 * or publicity pertaining to distribution of the software without specific,
Ted Jump83c02ef1999-09-17 02:40:51 +000012 * written prior permission.
jtgafb833d1999-08-19 00:55:39 +000013 *
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.
Ted Jump83c02ef1999-09-17 02:40:51 +000026 *
27 * US Government Users Restricted Rights
jtgafb833d1999-08-19 00:55:39 +000028 * 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
Ted Jump83c02ef1999-09-17 02:40:51 +000041 * This program draws a NURBS surface in the shape of a
jtgafb833d1999-08-19 00:55:39 +000042 * symmetrical hill.
43 */
44#include <GL/glut.h>
45
46GLfloat ctlpoints[4][4][3];
47int showPoints = 0;
48
49GLUnurbsObj *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 */
55void 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 }
Ted Jump83c02ef1999-09-17 02:40:51 +000068 }
69}
70
jtgafb833d1999-08-19 00:55:39 +000071/* Initialize material property and depth buffer.
72 */
73void 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
102void 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);
Ted Jump83c02ef1999-09-17 02:40:51 +0000114 gluNurbsSurface(theNurb,
jtgafb833d1999-08-19 00:55:39 +0000115 8, knots,
116 8, knots,
117 4 * 3,
118 3,
Ted Jump83c02ef1999-09-17 02:40:51 +0000119 &ctlpoints[0][0][0],
jtgafb833d1999-08-19 00:55:39 +0000120 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 }
Ted Jump83c02ef1999-09-17 02:40:51 +0000137
jtgafb833d1999-08-19 00:55:39 +0000138 glPopMatrix();
139 glutSwapBuffers();
140}
141
142void 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
152void
153menu(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
170int down = 0, lastx;
171
172/* ARGSUSED1 */
173void
174motion(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 */
184void
185mouse(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
Ted Jump83c02ef1999-09-17 02:40:51 +0000197static void
198key(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
jtgafb833d1999-08-19 00:55:39 +0000210/* Main Loop */
211int
212main(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);
Ted Jump83c02ef1999-09-17 02:40:51 +0000228 glutKeyboardFunc(key);
jtgafb833d1999-08-19 00:55:39 +0000229 glutMainLoop();
230 return 0; /* ANSI C requires main to return int. */
231}