Clone of mesa.
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. /*
  2. * Copyright (c) 1991, 1992, 1993 Silicon Graphics, Inc.
  3. *
  4. * Permission to use, copy, modify, distribute, and sell this software and
  5. * its documentation for any purpose is hereby granted without fee, provided
  6. * that (i) the above copyright notices and this permission notice appear in
  7. * all copies of the software and related documentation, and (ii) the name of
  8. * Silicon Graphics may not be used in any advertising or
  9. * publicity relating to the software without the specific, prior written
  10. * permission of Silicon Graphics.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF
  13. * ANY KIND,
  14. * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
  15. * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  16. *
  17. * IN NO EVENT SHALL SILICON GRAPHICS BE LIABLE FOR
  18. * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  19. * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  20. * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
  21. * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  22. * OF THIS SOFTWARE.
  23. */
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <math.h>
  28. #include <GL/glut.h>
  29. #ifndef CALLBACK
  30. #define CALLBACK
  31. #endif
  32. #define INREAL float
  33. #define S_NUMPOINTS 13
  34. #define S_ORDER 3
  35. #define S_NUMKNOTS (S_NUMPOINTS + S_ORDER)
  36. #define T_NUMPOINTS 3
  37. #define T_ORDER 3
  38. #define T_NUMKNOTS (T_NUMPOINTS + T_ORDER)
  39. #define SQRT_TWO 1.41421356237309504880
  40. typedef INREAL Point[4];
  41. GLenum doubleBuffer;
  42. GLenum expectedError;
  43. GLint rotX = 40, rotY = 40;
  44. INREAL sknots[S_NUMKNOTS] = {
  45. -1.0, -1.0, -1.0, 0.0, 1.0, 2.0, 3.0, 4.0,
  46. 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 9.0, 9.0
  47. };
  48. INREAL tknots[T_NUMKNOTS] = {
  49. 1.0, 1.0, 1.0, 2.0, 2.0, 2.0
  50. };
  51. Point ctlpoints[S_NUMPOINTS][T_NUMPOINTS] = {
  52. {
  53. {
  54. 4.0, 2.0, 2.0, 1.0
  55. },
  56. {
  57. 4.0, 1.6, 2.5, 1.0
  58. },
  59. {
  60. 4.0, 2.0, 3.0, 1.0
  61. }
  62. },
  63. {
  64. {
  65. 5.0, 4.0, 2.0, 1.0
  66. },
  67. {
  68. 5.0, 4.0, 2.5, 1.0
  69. },
  70. {
  71. 5.0, 4.0, 3.0, 1.0
  72. }
  73. },
  74. {
  75. {
  76. 6.0, 5.0, 2.0, 1.0
  77. },
  78. {
  79. 6.0, 5.0, 2.5, 1.0
  80. },
  81. {
  82. 6.0, 5.0, 3.0, 1.0
  83. }
  84. },
  85. {
  86. {
  87. SQRT_TWO*6.0, SQRT_TWO*6.0, SQRT_TWO*2.0, SQRT_TWO
  88. },
  89. {
  90. SQRT_TWO*6.0, SQRT_TWO*6.0, SQRT_TWO*2.5, SQRT_TWO
  91. },
  92. {
  93. SQRT_TWO*6.0, SQRT_TWO*6.0, SQRT_TWO*3.0, SQRT_TWO
  94. }
  95. },
  96. {
  97. {
  98. 5.2, 6.7, 2.0, 1.0
  99. },
  100. {
  101. 5.2, 6.7, 2.5, 1.0
  102. },
  103. {
  104. 5.2, 6.7, 3.0, 1.0
  105. }
  106. },
  107. {
  108. {
  109. SQRT_TWO*4.0, SQRT_TWO*6.0, SQRT_TWO*2.0, SQRT_TWO
  110. },
  111. {
  112. SQRT_TWO*4.0, SQRT_TWO*6.0, SQRT_TWO*2.5, SQRT_TWO
  113. },
  114. {
  115. SQRT_TWO*4.0, SQRT_TWO*6.0, SQRT_TWO*3.0, SQRT_TWO
  116. }
  117. },
  118. {
  119. {
  120. 4.0, 5.2, 2.0, 1.0
  121. },
  122. {
  123. 4.0, 4.6, 2.5, 1.0
  124. },
  125. {
  126. 4.0, 5.2, 3.0, 1.0
  127. }
  128. },
  129. {
  130. {
  131. SQRT_TWO*4.0, SQRT_TWO*6.0, SQRT_TWO*2.0, SQRT_TWO
  132. },
  133. {
  134. SQRT_TWO*4.0, SQRT_TWO*6.0, SQRT_TWO*2.5, SQRT_TWO
  135. },
  136. {
  137. SQRT_TWO*4.0, SQRT_TWO*6.0, SQRT_TWO*3.0, SQRT_TWO
  138. }
  139. },
  140. {
  141. {
  142. 2.8, 6.7, 2.0, 1.0
  143. },
  144. {
  145. 2.8, 6.7, 2.5, 1.0
  146. },
  147. {
  148. 2.8, 6.7, 3.0, 1.0
  149. }
  150. },
  151. {
  152. {
  153. SQRT_TWO*2.0, SQRT_TWO*6.0, SQRT_TWO*2.0, SQRT_TWO
  154. },
  155. {
  156. SQRT_TWO*2.0, SQRT_TWO*6.0, SQRT_TWO*2.5, SQRT_TWO
  157. },
  158. {
  159. SQRT_TWO*2.0, SQRT_TWO*6.0, SQRT_TWO*3.0, SQRT_TWO
  160. }
  161. },
  162. {
  163. {
  164. 2.0, 5.0, 2.0, 1.0
  165. },
  166. {
  167. 2.0, 5.0, 2.5, 1.0
  168. },
  169. {
  170. 2.0, 5.0, 3.0, 1.0
  171. }
  172. },
  173. {
  174. {
  175. 3.0, 4.0, 2.0, 1.0
  176. },
  177. {
  178. 3.0, 4.0, 2.5, 1.0
  179. },
  180. {
  181. 3.0, 4.0, 3.0, 1.0
  182. }
  183. },
  184. {
  185. {
  186. 4.0, 2.0, 2.0, 1.0
  187. },
  188. {
  189. 4.0, 1.6, 2.5, 1.0
  190. },
  191. {
  192. 4.0, 2.0, 3.0, 1.0
  193. }
  194. }
  195. };
  196. GLUnurbsObj *theNurbs;
  197. static void CALLBACK ErrorCallback(GLenum which)
  198. {
  199. if (which != expectedError) {
  200. fprintf(stderr, "Unexpected error occured (%d):\n", which);
  201. fprintf(stderr, " %s\n", (char *) gluErrorString(which));
  202. }
  203. }
  204. static void Init(void)
  205. {
  206. theNurbs = gluNewNurbsRenderer();
  207. gluNurbsCallback(theNurbs, GLU_ERROR, ErrorCallback);
  208. gluNurbsProperty(theNurbs, GLU_SAMPLING_TOLERANCE, 15.0);
  209. gluNurbsProperty(theNurbs, GLU_DISPLAY_MODE, GLU_OUTLINE_PATCH);
  210. expectedError = GLU_INVALID_ENUM;
  211. gluNurbsProperty(theNurbs, ~0, 15.0);
  212. expectedError = GLU_NURBS_ERROR13;
  213. gluEndSurface(theNurbs);
  214. expectedError = 0;
  215. glColor3f(1.0, 1.0, 1.0);
  216. }
  217. static void Reshape(int width, int height)
  218. {
  219. glViewport(0, 0, (GLint)width, (GLint)height);
  220. glMatrixMode(GL_PROJECTION);
  221. glLoadIdentity();
  222. glFrustum(-2.0, 2.0, -2.0, 2.0, 0.8, 10.0);
  223. gluLookAt(7.0, 4.5, 4.0, 4.5, 4.5, 2.5, 6.0, -3.0, 2.0);
  224. glMatrixMode(GL_MODELVIEW);
  225. }
  226. static void Key2(int key, int x, int y)
  227. {
  228. switch (key) {
  229. case GLUT_KEY_DOWN:
  230. rotX -= 5;
  231. break;
  232. case GLUT_KEY_UP:
  233. rotX += 5;
  234. break;
  235. case GLUT_KEY_LEFT:
  236. rotY -= 5;
  237. break;
  238. case GLUT_KEY_RIGHT:
  239. rotY += 5;
  240. break;
  241. default:
  242. return;
  243. }
  244. glutPostRedisplay();
  245. }
  246. static void Key(unsigned char key, int x, int y)
  247. {
  248. switch (key) {
  249. case 27:
  250. exit(1);
  251. }
  252. }
  253. static void Draw(void)
  254. {
  255. glClear(GL_COLOR_BUFFER_BIT);
  256. glPushMatrix();
  257. glTranslatef(4.0, 4.5, 2.5);
  258. glRotatef(rotY, 1, 0, 0);
  259. glRotatef(rotX, 0, 1, 0);
  260. glTranslatef(-4.0, -4.5, -2.5);
  261. gluBeginSurface(theNurbs);
  262. gluNurbsSurface(theNurbs, S_NUMKNOTS, sknots, T_NUMKNOTS, tknots,
  263. 4*T_NUMPOINTS, 4, &ctlpoints[0][0][0], S_ORDER,
  264. T_ORDER, GL_MAP2_VERTEX_4);
  265. gluEndSurface(theNurbs);
  266. glPopMatrix();
  267. glFlush();
  268. if (doubleBuffer) {
  269. glutSwapBuffers();
  270. }
  271. }
  272. static GLenum Args(int argc, char **argv)
  273. {
  274. GLint i;
  275. doubleBuffer = GL_FALSE;
  276. for (i = 1; i < argc; i++) {
  277. if (strcmp(argv[i], "-sb") == 0) {
  278. doubleBuffer = GL_FALSE;
  279. } else if (strcmp(argv[i], "-db") == 0) {
  280. doubleBuffer = GL_TRUE;
  281. } else {
  282. printf("%s (Bad option).\n", argv[i]);
  283. return GL_FALSE;
  284. }
  285. }
  286. return GL_TRUE;
  287. }
  288. int main(int argc, char **argv)
  289. {
  290. GLenum type;
  291. glutInit(&argc, argv);
  292. if (Args(argc, argv) == GL_FALSE) {
  293. exit(1);
  294. }
  295. glutInitWindowPosition(0, 0); glutInitWindowSize( 300, 300);
  296. type = GLUT_RGB;
  297. type |= (doubleBuffer) ? GLUT_DOUBLE : GLUT_SINGLE;
  298. glutInitDisplayMode(type);
  299. if (glutCreateWindow("NURBS Test") == GL_FALSE) {
  300. exit(1);
  301. }
  302. Init();
  303. glutReshapeFunc(Reshape);
  304. glutKeyboardFunc(Key);
  305. glutSpecialFunc(Key2);
  306. glutDisplayFunc(Draw);
  307. glutMainLoop();
  308. return 0;
  309. }