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