1
2/* Copyright (c) Mark J. Kilgard, 1994. */
3
4/*
5 * (c) Copyright 1993, Silicon Graphics, Inc.
6 * ALL RIGHTS RESERVED
7 * Permission to use, copy, modify, and distribute this software for
8 * any purpose and without fee is hereby granted, provided that the above
9 * copyright notice appear in all copies and that both the copyright notice
10 * and this permission notice appear in supporting documentation, and that
11 * the name of Silicon Graphics, Inc. not be used in advertising
12 * or publicity pertaining to distribution of the software without specific,
13 * written prior permission.
14 *
15 * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
16 * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
18 * FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL SILICON
19 * GRAPHICS, INC.  BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
20 * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
21 * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
22 * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
23 * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC.  HAS BEEN
24 * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
25 * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
26 * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
27 *
28 * US Government Users Restricted Rights
29 * Use, duplication, or disclosure by the Government is subject to
30 * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
31 * (c)(1)(ii) of the Rights in Technical Data and Computer Software
32 * clause at DFARS 252.227-7013 and/or in similar or successor
33 * clauses in the FAR or the DOD or NASA FAR Supplement.
34 * Unpublished-- rights reserved under the copyright laws of the
35 * United States.  Contractor/manufacturer is Silicon Graphics,
36 * Inc., 2011 N.  Shoreline Blvd., Mountain View, CA 94039-7311.
37 *
38 * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
39 */
40/*
41 *  nurbs.c
42 *  This program shows a NURBS (Non-uniform rational B-splines)
43 *  surface, shaped like a heart.
44 */
45#include <stdlib.h>
46#include "glut_wrap.h"
47
48#define S_NUMPOINTS 13
49#define S_ORDER     3
50#define S_NUMKNOTS  (S_NUMPOINTS + S_ORDER)
51#define T_NUMPOINTS 3
52#define T_ORDER     3
53#define T_NUMKNOTS  (T_NUMPOINTS + T_ORDER)
54#define SQRT2    1.41421356237309504880
55
56/* initialized local data */
57
58GLfloat sknots[S_NUMKNOTS] =
59    {-1.0, -1.0, -1.0, 0.0, 1.0, 2.0, 3.0, 4.0,
60      4.0,  5.0,  6.0, 7.0, 8.0, 9.0, 9.0, 9.0};
61GLfloat tknots[T_NUMKNOTS] = {1.0, 1.0, 1.0, 2.0, 2.0, 2.0};
62
63GLfloat ctlpoints[S_NUMPOINTS][T_NUMPOINTS][4] = {
64{   {4.,2.,2.,1.},{4.,1.6,2.5,1.},{4.,2.,3.0,1.}    },
65{   {5.,4.,2.,1.},{5.,4.,2.5,1.},{5.,4.,3.0,1.} },
66{   {6.,5.,2.,1.},{6.,5.,2.5,1.},{6.,5.,3.0,1.} },
67{   {SQRT2*6.,SQRT2*6.,SQRT2*2.,SQRT2},
68    {SQRT2*6.,SQRT2*6.,SQRT2*2.5,SQRT2},
69    {SQRT2*6.,SQRT2*6.,SQRT2*3.0,SQRT2}  },
70{   {5.2,6.7,2.,1.},{5.2,6.7,2.5,1.},{5.2,6.7,3.0,1.}   },
71{   {SQRT2*4.,SQRT2*6.,SQRT2*2.,SQRT2},
72    {SQRT2*4.,SQRT2*6.,SQRT2*2.5,SQRT2},
73    {SQRT2*4.,SQRT2*6.,SQRT2*3.0,SQRT2}  },
74{   {4.,5.2,2.,1.},{4.,4.6,2.5,1.},{4.,5.2,3.0,1.}  },
75{   {SQRT2*4.,SQRT2*6.,SQRT2*2.,SQRT2},
76    {SQRT2*4.,SQRT2*6.,SQRT2*2.5,SQRT2},
77    {SQRT2*4.,SQRT2*6.,SQRT2*3.0,SQRT2}  },
78{   {2.8,6.7,2.,1.},{2.8,6.7,2.5,1.},{2.8,6.7,3.0,1.}   },
79{   {SQRT2*2.,SQRT2*6.,SQRT2*2.,SQRT2},
80    {SQRT2*2.,SQRT2*6.,SQRT2*2.5,SQRT2},
81    {SQRT2*2.,SQRT2*6.,SQRT2*3.0,SQRT2}  },
82{   {2.,5.,2.,1.},{2.,5.,2.5,1.},{2.,5.,3.0,1.} },
83{   {3.,4.,2.,1.},{3.,4.,2.5,1.},{3.,4.,3.0,1.} },
84{   {4.,2.,2.,1.},{4.,1.6,2.5,1.},{4.,2.,3.0,1.}    }
85};
86
87GLUnurbsObj *theNurb;
88
89/*  Initialize material property, light source, lighting model,
90 *  and depth buffer.
91 */
92static void myinit(void)
93{
94    GLfloat mat_ambient[] = { 1.0, 1.0, 1.0, 1.0 };
95    GLfloat mat_diffuse[] = { 1.0, 0.2, 1.0, 1.0 };
96    GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
97    GLfloat mat_shininess[] = { 50.0 };
98
99    GLfloat light0_position[] = { 1.0, 0.1, 1.0, 0.0 };
100    GLfloat light1_position[] = { -1.0, 0.1, 1.0, 0.0 };
101
102    GLfloat lmodel_ambient[] = { 0.3, 0.3, 0.3, 1.0 };
103
104    glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
105    glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
106    glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
107    glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
108    glLightfv(GL_LIGHT0, GL_POSITION, light0_position);
109    glLightfv(GL_LIGHT1, GL_POSITION, light1_position);
110    glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
111
112    glEnable(GL_LIGHTING);
113    glEnable(GL_LIGHT0);
114    glEnable(GL_LIGHT1);
115    glDepthFunc(GL_LESS);
116    glEnable(GL_DEPTH_TEST);
117    glEnable(GL_AUTO_NORMAL);
118
119    theNurb = gluNewNurbsRenderer();
120
121    gluNurbsProperty(theNurb, GLU_SAMPLING_TOLERANCE, 25.0);
122    gluNurbsProperty(theNurb, GLU_DISPLAY_MODE, GLU_FILL);
123}
124
125static void display(void)
126{
127    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
128
129    glPushMatrix();
130    glTranslatef (4., 4.5, 2.5);
131    glRotatef (220.0, 1., 0., 0.);
132    glRotatef (115.0, 0., 1., 0.);
133    glTranslatef (-4., -4.5, -2.5);
134
135    gluBeginSurface(theNurb);
136    gluNurbsSurface(theNurb,
137	    S_NUMKNOTS, sknots,
138	    T_NUMKNOTS, tknots,
139	    4 * T_NUMPOINTS,
140	    4,
141	    &ctlpoints[0][0][0],
142	    S_ORDER, T_ORDER,
143	    GL_MAP2_VERTEX_4);
144    gluEndSurface(theNurb);
145
146    glPopMatrix();
147    glFlush();
148}
149
150static void myReshape(int w, int h)
151{
152    glViewport(0, 0, w, h);
153    glMatrixMode(GL_PROJECTION);
154    glLoadIdentity();
155    glFrustum(-1.0, 1.0, -1.5, 0.5, 0.8, 10.0);
156
157    glMatrixMode(GL_MODELVIEW);
158    glLoadIdentity();
159    gluLookAt(7.0,4.5,4.0, 4.5,4.5,2.0, 6.0,-3.0,2.0);
160}
161
162static void
163key(unsigned char k, int x, int y)
164{
165  switch (k) {
166  case 27:  /* Escape */
167    exit(0);
168    break;
169  default:
170    return;
171  }
172  glutPostRedisplay();
173}
174
175/*  Main Loop
176 *  Open window with initial window size, title bar,
177 *  RGBA display mode, and handle input events.
178 */
179int main(int argc, char** argv)
180{
181    glutInit(&argc, argv);
182    glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
183    glutCreateWindow (argv[0]);
184    myinit();
185    glutReshapeFunc (myReshape);
186    glutDisplayFunc(display);
187    glutKeyboardFunc(key);
188    glutMainLoop();
189    return 0;             /* ANSI C requires main to return int. */
190}
191