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 <stdlib.h>
45#include "glut_wrap.h"
46
47GLfloat ctlpoints[4][4][3];
48int showPoints = 0;
49
50GLUnurbsObj *theNurb;
51
52/*
53 *  Initializes the control points of the surface to a small hill.
54 *  The control points range from -3 to +3 in x, y, and z
55 */
56static void init_surface(void)
57{
58    int u, v;
59    for (u = 0; u < 4; u++) {
60	for (v = 0; v < 4; v++) {
61	    ctlpoints[u][v][0] = 2.0*((GLfloat)u - 1.5);
62	    ctlpoints[u][v][1] = 2.0*((GLfloat)v - 1.5);
63
64	    if ( (u == 1 || u == 2) && (v == 1 || v == 2))
65		ctlpoints[u][v][2] = 7.0;
66	    else
67		ctlpoints[u][v][2] = -3.0;
68	}
69    }
70}
71
72/*  Initialize material property and depth buffer.
73 */
74static void myinit(void)
75{
76    GLfloat mat_diffuse[] = { 0.7, 0.7, 0.7, 1.0 };
77    GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
78    GLfloat mat_shininess[] = { 100.0 };
79
80    glClearColor (0.0, 0.0, 0.0, 1.0);
81    glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
82    glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
83    glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
84
85    glEnable(GL_LIGHTING);
86    glEnable(GL_LIGHT0);
87    glDepthFunc(GL_LESS);
88    glEnable(GL_DEPTH_TEST);
89    glEnable(GL_AUTO_NORMAL);
90    glEnable(GL_NORMALIZE);
91
92    init_surface();
93
94    theNurb = gluNewNurbsRenderer();
95    gluNurbsProperty(theNurb, GLU_SAMPLING_TOLERANCE, 25.0);
96    gluNurbsProperty(theNurb, GLU_DISPLAY_MODE, GLU_FILL);
97
98    glMatrixMode(GL_MODELVIEW);
99    glLoadIdentity();
100    glTranslatef (0.0, 0.0, -5.0);
101}
102
103static void display(void)
104{
105    GLfloat knots[8] = {0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0};
106    int i, j;
107
108    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
109
110    glPushMatrix();
111    glRotatef(330.0, 1.,0.,0.);
112    glScalef (0.25, 0.25, 0.25);
113
114    gluBeginSurface(theNurb);
115    gluNurbsSurface(theNurb,
116	    8, knots,
117	    8, knots,
118	    4 * 3,
119	    3,
120	    &ctlpoints[0][0][0],
121	    4, 4,
122	    GL_MAP2_VERTEX_3);
123    gluEndSurface(theNurb);
124
125    if(showPoints) {
126    glPointSize(5.0);
127    glDisable(GL_LIGHTING);
128    glColor3f(1.0, 1.0, 0.0);
129    glBegin(GL_POINTS);
130    for(i=0;i<4;i++) {
131      for(j=0;j<4;j++) {
132	glVertex3f(ctlpoints[i][j][0], ctlpoints[i][j][1], ctlpoints[i][j][2]);
133      }
134    }
135    glEnd();
136    glEnable(GL_LIGHTING);
137    }
138
139    glPopMatrix();
140    glutSwapBuffers();
141}
142
143static void reshape(int w, int h)
144{
145    glViewport(0, 0, w, h);
146    glMatrixMode(GL_PROJECTION);
147    glLoadIdentity();
148    gluPerspective (45.0, (GLdouble)w/(GLdouble)h, 3.0, 8.0);
149
150    glMatrixMode(GL_MODELVIEW);
151}
152
153static void
154menu(int value)
155{
156    switch (value) {
157    case 0:
158    case 1:
159        showPoints = value;
160	break;
161    case 2:
162        gluNurbsProperty(theNurb, GLU_DISPLAY_MODE, GLU_FILL);
163	break;
164    case 3:
165        gluNurbsProperty(theNurb, GLU_DISPLAY_MODE, GLU_OUTLINE_POLYGON);
166	break;
167    }
168    glutPostRedisplay();
169}
170
171int down = 0, lastx;
172
173/* ARGSUSED1 */
174static void
175motion(int x, int y)
176{
177    if (down) {
178        glRotatef(lastx - x, 0, 1, 0);
179        lastx = x;
180        glutPostRedisplay();
181    }
182}
183
184/* ARGSUSED3 */
185static void
186mouse(int button, int state, int x, int y)
187{
188    if (button == GLUT_LEFT_BUTTON) {
189        if (state == GLUT_DOWN) {
190            lastx = x;
191            down = 1;
192        } else {
193            down = 0;
194        }
195    }
196}
197
198static void
199key(unsigned char k, int x, int y)
200{
201  switch (k) {
202  case 27:  /* Escape */
203    exit(0);
204    break;
205  default:
206    return;
207  }
208  glutPostRedisplay();
209}
210
211/* Main Loop */
212int
213main(int argc, char** argv)
214{
215    glutInit(&argc, argv);
216    glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGB);
217    glutCreateWindow(argv[0]);
218    myinit();
219    glutReshapeFunc(reshape);
220    glutDisplayFunc(display);
221    glutCreateMenu(menu);
222    glutAddMenuEntry("Show control points", 1);
223    glutAddMenuEntry("Hide control points", 0);
224    glutAddMenuEntry("Solid", 2);
225    glutAddMenuEntry("Wireframe", 3);
226    glutAttachMenu(GLUT_RIGHT_BUTTON);
227    glutMouseFunc(mouse);
228    glutMotionFunc(motion);
229    glutKeyboardFunc(key);
230    glutMainLoop();
231    return 0;             /* ANSI C requires main to return int. */
232}
233