Clone of mesa.
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

surface.c 5.9KB

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