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