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.

surface.c 6.0KB

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