home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / newopg.zip / NURBS.C < prev    next >
Text File  |  1995-03-04  |  6KB  |  173 lines

  1. /*
  2.  * (c) Copyright 1993, 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. 
  11.  *
  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.
  24.  * 
  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.
  34.  *
  35.  * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
  36.  */
  37. /*
  38.  *  nurbs.c
  39.  *  This program shows a NURBS (Non-uniform rational B-splines)
  40.  *  surface, shaped like a heart.
  41.  */
  42. #include <GL/gl.h>
  43. #include <GL/glu.h>
  44. #include "aux.h"
  45.  
  46. #define S_NUMPOINTS 13
  47. #define S_ORDER     3   
  48. #define S_NUMKNOTS  (S_NUMPOINTS + S_ORDER)
  49. #define T_NUMPOINTS 3
  50. #define T_ORDER     3 
  51. #define T_NUMKNOTS  (T_NUMPOINTS + T_ORDER)
  52. #define SQRT2    1.41421356237309504880
  53.  
  54. /* initialized local data */
  55.  
  56. GLfloat sknots[S_NUMKNOTS] =
  57.     {-1.0, -1.0, -1.0, 0.0, 1.0, 2.0, 3.0, 4.0,
  58.       4.0,  5.0,  6.0, 7.0, 8.0, 9.0, 9.0, 9.0};
  59. GLfloat tknots[T_NUMKNOTS] = {1.0, 1.0, 1.0, 2.0, 2.0, 2.0};
  60.  
  61. GLfloat ctlpoints[S_NUMPOINTS][T_NUMPOINTS][4] = {
  62. {   {4.,2.,2.,1.},{4.,1.6,2.5,1.},{4.,2.,3.0,1.}    },
  63. {   {5.,4.,2.,1.},{5.,4.,2.5,1.},{5.,4.,3.0,1.} },
  64. {   {6.,5.,2.,1.},{6.,5.,2.5,1.},{6.,5.,3.0,1.} },
  65. {   {SQRT2*6.,SQRT2*6.,SQRT2*2.,SQRT2},
  66.     {SQRT2*6.,SQRT2*6.,SQRT2*2.5,SQRT2},
  67.     {SQRT2*6.,SQRT2*6.,SQRT2*3.0,SQRT2}  },
  68. {   {5.2,6.7,2.,1.},{5.2,6.7,2.5,1.},{5.2,6.7,3.0,1.}   },
  69. {   {SQRT2*4.,SQRT2*6.,SQRT2*2.,SQRT2},
  70.     {SQRT2*4.,SQRT2*6.,SQRT2*2.5,SQRT2},
  71.     {SQRT2*4.,SQRT2*6.,SQRT2*3.0,SQRT2}  },
  72. {   {4.,5.2,2.,1.},{4.,4.6,2.5,1.},{4.,5.2,3.0,1.}  },
  73. {   {SQRT2*4.,SQRT2*6.,SQRT2*2.,SQRT2},
  74.     {SQRT2*4.,SQRT2*6.,SQRT2*2.5,SQRT2},
  75.     {SQRT2*4.,SQRT2*6.,SQRT2*3.0,SQRT2}  },
  76. {   {2.8,6.7,2.,1.},{2.8,6.7,2.5,1.},{2.8,6.7,3.0,1.}   },
  77. {   {SQRT2*2.,SQRT2*6.,SQRT2*2.,SQRT2},
  78.     {SQRT2*2.,SQRT2*6.,SQRT2*2.5,SQRT2},
  79.     {SQRT2*2.,SQRT2*6.,SQRT2*3.0,SQRT2}  },
  80. {   {2.,5.,2.,1.},{2.,5.,2.5,1.},{2.,5.,3.0,1.} },
  81. {   {3.,4.,2.,1.},{3.,4.,2.5,1.},{3.,4.,3.0,1.} },
  82. {   {4.,2.,2.,1.},{4.,1.6,2.5,1.},{4.,2.,3.0,1.}    }
  83. };
  84.  
  85. GLUnurbsObj *theNurb;
  86.  
  87. /*  Initialize material property, light source, lighting model, 
  88.  *  and depth buffer.
  89.  */
  90. void myinit(void)
  91. {
  92.     GLfloat mat_ambient[] = { 1.0, 1.0, 1.0, 1.0 };
  93.     GLfloat mat_diffuse[] = { 1.0, 0.2, 1.0, 1.0 };
  94.     GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
  95.     GLfloat mat_shininess[] = { 50.0 };
  96.  
  97.     GLfloat light0_position[] = { 1.0, 0.1, 1.0, 0.0 };
  98.     GLfloat light1_position[] = { -1.0, 0.1, 1.0, 0.0 };
  99.  
  100.     GLfloat lmodel_ambient[] = { 0.3, 0.3, 0.3, 1.0 };
  101.  
  102.     glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
  103.     glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
  104.     glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
  105.     glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
  106.     glLightfv(GL_LIGHT0, GL_POSITION, light0_position);
  107.     glLightfv(GL_LIGHT1, GL_POSITION, light1_position);
  108.     glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
  109.  
  110.     glEnable(GL_LIGHTING);
  111.     glEnable(GL_LIGHT0);
  112.     glEnable(GL_LIGHT1);
  113.     glDepthFunc(GL_LESS);
  114.     glEnable(GL_DEPTH_TEST);
  115.     glEnable(GL_AUTO_NORMAL);
  116.  
  117.     theNurb = gluNewNurbsRenderer();
  118.  
  119.     gluNurbsProperty(theNurb, GLU_SAMPLING_TOLERANCE, 25.0);
  120.     gluNurbsProperty(theNurb, GLU_DISPLAY_MODE, GLU_FILL);
  121. }
  122.  
  123. void display(void)
  124. {
  125.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  126.  
  127.     glPushMatrix();
  128.     glTranslatef (4., 4.5, 2.5);
  129.     glRotatef (220.0, 1., 0., 0.);
  130.     glRotatef (115.0, 0., 1., 0.);
  131.     glTranslatef (-4., -4.5, -2.5);
  132.  
  133.     gluBeginSurface(theNurb);
  134.     gluNurbsSurface(theNurb, 
  135.         S_NUMKNOTS, sknots,
  136.         T_NUMKNOTS, tknots,
  137.         4 * T_NUMPOINTS,
  138.         4,
  139.         &ctlpoints[0][0][0], 
  140.         S_ORDER, T_ORDER,
  141.         GL_MAP2_VERTEX_4);
  142.     gluEndSurface(theNurb);
  143.  
  144.     glPopMatrix();
  145.     glFlush();
  146. }
  147.  
  148. void myReshape(GLint w, GLint h)
  149. {
  150.     glViewport(0, 0, w, h);
  151.     glMatrixMode(GL_PROJECTION);
  152.     glLoadIdentity();
  153.     glFrustum(-2.0, 2.0, -2.0, 2.0, 0.8, 10.0);
  154.  
  155.     glMatrixMode(GL_MODELVIEW);
  156.     glLoadIdentity();
  157.     gluLookAt(7.0,4.5,4.0, 4.5,4.5,2.0, 6.0,-3.0,2.0);
  158. }
  159.  
  160. /*  Main Loop
  161.  *  Open window with initial window size, title bar, 
  162.  *  RGBA display mode, and handle input events.
  163.  */
  164. int main(int argc, char** argv)
  165. {
  166.     auxInitDisplayMode (AUX_SINGLE | AUX_RGB | AUX_DEPTH);
  167.     auxInitPosition (0, 0, 500, 500);
  168.     auxInitWindow (argv[0]);
  169.     myinit();
  170.     auxReshapeFunc (myReshape);
  171.     auxMainLoop(display);
  172. }
  173.