Clone of mesa.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

nurbs.c 6.1KB

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