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.

tess.c 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*
  2. * Copyright (c) 1993-1997, 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(R) is a registered trademark of Silicon Graphics, Inc.
  36. */
  37. /*
  38. * tess.c
  39. * This program demonstrates polygon tessellation.
  40. * Two tesselated objects are drawn. The first is a
  41. * rectangle with a triangular hole. The second is a
  42. * smooth shaded, self-intersecting star.
  43. *
  44. * Note the exterior rectangle is drawn with its vertices
  45. * in counter-clockwise order, but its interior clockwise.
  46. * Note the combineCallback is needed for the self-intersecting
  47. * star. Also note that removing the TessProperty for the
  48. * star will make the interior unshaded (WINDING_ODD).
  49. */
  50. #include <GL/glut.h>
  51. #include <stdlib.h>
  52. #include <stdio.h>
  53. #ifdef GLU_VERSION_1_2
  54. /* Win32 calling conventions. */
  55. #ifndef CALLBACK
  56. #define CALLBACK
  57. #endif
  58. GLuint startList;
  59. void display (void) {
  60. glClear(GL_COLOR_BUFFER_BIT);
  61. glColor3f(1.0, 1.0, 1.0);
  62. glCallList(startList);
  63. glCallList(startList + 1);
  64. glFlush();
  65. }
  66. void CALLBACK beginCallback(GLenum which)
  67. {
  68. glBegin(which);
  69. }
  70. void CALLBACK errorCallback(GLenum errorCode)
  71. {
  72. const GLubyte *estring;
  73. estring = gluErrorString(errorCode);
  74. fprintf(stderr, "Tessellation Error: %s\n", estring);
  75. exit(0);
  76. }
  77. void CALLBACK endCallback(void)
  78. {
  79. glEnd();
  80. }
  81. void CALLBACK vertexCallback(GLvoid *vertex)
  82. {
  83. const GLdouble *pointer;
  84. pointer = (GLdouble *) vertex;
  85. glColor3dv(pointer+3);
  86. glVertex3dv(vertex);
  87. }
  88. /* combineCallback is used to create a new vertex when edges
  89. * intersect. coordinate location is trivial to calculate,
  90. * but weight[4] may be used to average color, normal, or texture
  91. * coordinate data. In this program, color is weighted.
  92. */
  93. void CALLBACK combineCallback(GLdouble coords[3],
  94. GLdouble *vertex_data[4],
  95. GLfloat weight[4], GLdouble **dataOut )
  96. {
  97. GLdouble *vertex;
  98. int i;
  99. vertex = (GLdouble *) malloc(6 * sizeof(GLdouble));
  100. vertex[0] = coords[0];
  101. vertex[1] = coords[1];
  102. vertex[2] = coords[2];
  103. for (i = 3; i < 6; i++)
  104. vertex[i] = weight[0] * vertex_data[0][i]
  105. + weight[1] * vertex_data[1][i]
  106. + weight[2] * vertex_data[2][i]
  107. + weight[3] * vertex_data[3][i];
  108. *dataOut = vertex;
  109. }
  110. void init (void)
  111. {
  112. GLUtesselator *tobj;
  113. GLdouble rect[4][3] = {{50.0, 50.0, 0.0},
  114. {200.0, 50.0, 0.0},
  115. {200.0, 200.0, 0.0},
  116. {50.0, 200.0, 0.0}};
  117. GLdouble tri[3][3] = {{75.0, 75.0, 0.0},
  118. {125.0, 175.0, 0.0},
  119. {175.0, 75.0, 0.0}};
  120. GLdouble star[5][6] = {{250.0, 50.0, 0.0, 1.0, 0.0, 1.0},
  121. {325.0, 200.0, 0.0, 1.0, 1.0, 0.0},
  122. {400.0, 50.0, 0.0, 0.0, 1.0, 1.0},
  123. {250.0, 150.0, 0.0, 1.0, 0.0, 0.0},
  124. {400.0, 150.0, 0.0, 0.0, 1.0, 0.0}};
  125. glClearColor(0.0, 0.0, 0.0, 0.0);
  126. startList = glGenLists(2);
  127. tobj = gluNewTess();
  128. gluTessCallback(tobj, GLU_TESS_VERTEX,
  129. (GLvoid (CALLBACK*) ()) &glVertex3dv);
  130. gluTessCallback(tobj, GLU_TESS_BEGIN,
  131. (GLvoid (CALLBACK*) ()) &beginCallback);
  132. gluTessCallback(tobj, GLU_TESS_END,
  133. (GLvoid (CALLBACK*) ()) &endCallback);
  134. gluTessCallback(tobj, GLU_TESS_ERROR,
  135. (GLvoid (CALLBACK*) ()) &errorCallback);
  136. /* rectangle with triangular hole inside */
  137. glNewList(startList, GL_COMPILE);
  138. glShadeModel(GL_FLAT);
  139. gluTessBeginPolygon(tobj, NULL);
  140. gluTessBeginContour(tobj);
  141. gluTessVertex(tobj, rect[0], rect[0]);
  142. gluTessVertex(tobj, rect[1], rect[1]);
  143. gluTessVertex(tobj, rect[2], rect[2]);
  144. gluTessVertex(tobj, rect[3], rect[3]);
  145. gluTessEndContour(tobj);
  146. gluTessBeginContour(tobj);
  147. gluTessVertex(tobj, tri[0], tri[0]);
  148. gluTessVertex(tobj, tri[1], tri[1]);
  149. gluTessVertex(tobj, tri[2], tri[2]);
  150. gluTessEndContour(tobj);
  151. gluTessEndPolygon(tobj);
  152. glEndList();
  153. gluTessCallback(tobj, GLU_TESS_VERTEX,
  154. (GLvoid (CALLBACK*) ()) &vertexCallback);
  155. gluTessCallback(tobj, GLU_TESS_BEGIN,
  156. (GLvoid (CALLBACK*) ()) &beginCallback);
  157. gluTessCallback(tobj, GLU_TESS_END,
  158. (GLvoid (CALLBACK*) ()) &endCallback);
  159. gluTessCallback(tobj, GLU_TESS_ERROR,
  160. (GLvoid (CALLBACK*) ()) &errorCallback);
  161. gluTessCallback(tobj, GLU_TESS_COMBINE,
  162. (GLvoid (CALLBACK*) ()) &combineCallback);
  163. /* smooth shaded, self-intersecting star */
  164. glNewList(startList + 1, GL_COMPILE);
  165. glShadeModel(GL_SMOOTH);
  166. gluTessProperty(tobj, GLU_TESS_WINDING_RULE,
  167. GLU_TESS_WINDING_POSITIVE);
  168. gluTessBeginPolygon(tobj, NULL);
  169. gluTessBeginContour(tobj);
  170. gluTessVertex(tobj, star[0], star[0]);
  171. gluTessVertex(tobj, star[1], star[1]);
  172. gluTessVertex(tobj, star[2], star[2]);
  173. gluTessVertex(tobj, star[3], star[3]);
  174. gluTessVertex(tobj, star[4], star[4]);
  175. gluTessEndContour(tobj);
  176. gluTessEndPolygon(tobj);
  177. glEndList();
  178. gluDeleteTess(tobj);
  179. }
  180. void reshape (int w, int h)
  181. {
  182. glViewport(0, 0, (GLsizei) w, (GLsizei) h);
  183. glMatrixMode(GL_PROJECTION);
  184. glLoadIdentity();
  185. gluOrtho2D(0.0, (GLdouble) w, 0.0, (GLdouble) h);
  186. }
  187. /* ARGSUSED1 */
  188. void keyboard(unsigned char key, int x, int y)
  189. {
  190. switch (key) {
  191. case 27:
  192. exit(0);
  193. break;
  194. }
  195. }
  196. int main(int argc, char** argv)
  197. {
  198. glutInit(&argc, argv);
  199. glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
  200. glutInitWindowSize(500, 500);
  201. glutCreateWindow(argv[0]);
  202. init();
  203. glutDisplayFunc(display);
  204. glutReshapeFunc(reshape);
  205. glutKeyboardFunc(keyboard);
  206. glutMainLoop();
  207. return 0;
  208. }
  209. #else
  210. int main(int argc, char** argv)
  211. {
  212. fprintf (stderr, "This program demonstrates the new tesselator API in GLU 1.2.\n");
  213. fprintf (stderr, "Your GLU library does not support this new interface, sorry.\n");
  214. return 0;
  215. }
  216. #endif