1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
|
/* Copyright (c) Mark J. Kilgard, 1997. */
/* This program is freely distributable without licensing fees and is
provided without guarantee or warrantee expressed or implied. This
program is -not- in the public domain. */
/* This example shows how to use the GLU polygon tessellator to determine the
2D boundary of OpenGL rendered objects. The program uses OpenGL's
feedback mechanim to capture transformed polygons and then feeds them to
the GLU tesselator in GLU_TESS_WINDING_NONZERO and GLU_TESS_BOUNDARY_ONLY
mode. */
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <GL/glut.h>
#ifdef GLU_VERSION_1_2
#ifndef CALLBACK
#define CALLBACK
#endif
enum {
M_TORUS, M_CUBE, M_SPHERE, M_ICO, M_TEAPOT, M_ANGLE, M_BOUNDARY
};
struct VertexHolder {
struct VertexHolder *next;
GLfloat v[2];
};
int shape = M_TORUS;
int boundary = 1;
GLfloat angle = 0.0;
GLfloat lightDiffuse[] =
{1.0, 0.0, 0.0, 1.0};
GLfloat lightPosition[] =
{1.0, 1.0, 1.0, 0.0};
GLUtesselator *tess;
int width, height;
static void CALLBACK
begin(GLenum type)
{
assert(type == GL_LINE_LOOP);
glBegin(type);
}
static void CALLBACK
vertex(void *data)
{
GLfloat *v = data;
glVertex2fv(v);
}
static void CALLBACK
end(void)
{
glEnd();
}
static GLfloat *combineList = NULL;
static int combineListSize = 0;
static int combineNext = 0;
static struct VertexHolder *excessList = NULL;
static void
freeExcessList(void)
{
struct VertexHolder *holder, *next;
holder = excessList;
while (holder) {
next = holder->next;
free(holder);
holder = next;
}
excessList = NULL;
}
/* ARGSUSED1 */
static void CALLBACK
combine(GLdouble coords[3], void *d[4], GLfloat w[4], void **dataOut)
{
GLfloat *newCoords;
struct VertexHolder *holder;
/* XXX Careful, some systems still don't understand realloc of NULL. */
if (combineNext >= combineListSize) {
holder = (struct VertexHolder *) malloc(sizeof(struct VertexHolder));
holder->next = excessList;
excessList = holder;
newCoords = holder->v;
} else {
newCoords = &combineList[combineNext * 2];
}
newCoords[0] = coords[0];
newCoords[1] = coords[1];
*dataOut = newCoords;
combineNext++;
}
static void CALLBACK
error(GLenum errno)
{
printf("ERROR: %s\n", gluErrorString(errno));
}
void
reshape(int w, int h)
{
width = w;
height = h;
glViewport(0, 0, width, height);
}
void
processFeedback(GLint size, GLfloat * buffer)
{
GLfloat *loc, *end;
GLdouble v[3];
int token, nvertices, i;
if (combineNext > combineListSize) {
freeExcessList();
combineListSize = combineNext;
combineList = realloc(combineList, sizeof(GLfloat) * 2 * combineListSize);
}
combineNext = 0;
gluTessBeginPolygon(tess, NULL);
loc = buffer;
end = buffer + size;
while (loc < end) {
token = *loc;
loc++;
switch (token) {
case GL_POLYGON_TOKEN:
nvertices = *loc;
loc++;
assert(nvertices >= 3);
gluTessBeginContour(tess);
for (i = 0; i < nvertices; i++) {
v[0] = loc[0];
v[1] = loc[1];
v[2] = 0.0;
gluTessVertex(tess, v, loc);
loc += 2;
}
gluTessEndContour(tess);
break;
default:
/* Ignore everything but polygons. */
;
}
}
gluTessEndPolygon(tess);
}
int
determineBoundary(void (*renderFunc) (void), int probableSize)
{
static GLfloat *feedbackBuffer = NULL;
static int bufferSize = 0;
GLint returned;
if (bufferSize > probableSize) {
probableSize = bufferSize;
}
doFeedback:
/* XXX Careful, some systems still don't understand realloc of NULL. */
if (bufferSize < probableSize) {
bufferSize = probableSize;
feedbackBuffer = realloc(feedbackBuffer, bufferSize * sizeof(GLfloat));
}
glFeedbackBuffer(bufferSize, GL_2D, feedbackBuffer);
(void) glRenderMode(GL_FEEDBACK);
(*renderFunc) ();
returned = glRenderMode(GL_RENDER);
#if 0
if (returned == -1) {
#else
/* XXX RealityEngine workaround. */
if (returned == -1 || returned == probableSize) {
#endif
probableSize = probableSize + (probableSize >> 1);
goto doFeedback; /* Try again with larger feedback buffer. */
}
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, width, 0, height);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
processFeedback(returned, feedbackBuffer);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
return returned;
}
void
render(void)
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective( /* field of view in degree */ 30.0,
/* aspect ratio */ 1.0,
/* Z near */ 1.0, /* Z far */ 20.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.0, 0.0, 10.0, /* eye is at (0,0,5) */
0.0, 0.0, 0.0, /* center is at (0,0,0) */
0.0, 1.0, 0.); /* up is in postivie Y direction */
glPushMatrix();
glRotatef(angle, 1.0, 0.3, 0.0);
switch (shape) {
case M_TORUS:
glutSolidTorus(0.275, 0.85, 8, 8);
break;
case M_CUBE:
glutSolidCube(1.0);
break;
case M_SPHERE:
glutSolidSphere(1.0, 10, 10);
break;
case M_ICO:
glutSolidIcosahedron();
break;
case M_TEAPOT:
glutSolidTeapot(1.0);
break;
}
glPopMatrix();
}
void
display(void)
{
if (boundary) {
glClear(GL_COLOR_BUFFER_BIT);
glDisable(GL_LIGHTING);
glDisable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
determineBoundary(render, 250);
} else {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_LIGHTING);
glEnable(GL_DEPTH_TEST);
glDisable(GL_CULL_FACE);
render();
}
glutSwapBuffers();
}
void
menu(int value)
{
switch (value) {
case M_TORUS:
case M_CUBE:
case M_SPHERE:
case M_ICO:
case M_TEAPOT:
shape = value;
break;
case M_ANGLE:
angle += 10.0;
break;
case M_BOUNDARY:
boundary = !boundary; /* Toggle. */
break;
}
glutPostRedisplay();
}
/* ARGSUSED1 */
void
special(int key, int x, int y)
{
switch (key) {
case GLUT_KEY_UP:
angle += 10.0;
glutPostRedisplay();
break;
case GLUT_KEY_DOWN:
angle -= 10.0;
glutPostRedisplay();
break;
}
}
int
main(int argc, char **argv)
{
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH | GLUT_MULTISAMPLE);
glutInit(&argc, argv);
glutCreateWindow("boundary");
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutSpecialFunc(special);
glLightfv(GL_LIGHT0, GL_DIFFUSE, lightDiffuse);
glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);
glEnable(GL_LIGHT0);
tess = gluNewTess();
gluTessProperty(tess, GLU_TESS_BOUNDARY_ONLY, GL_TRUE);
gluTessProperty(tess, GLU_TESS_WINDING_RULE, GLU_TESS_WINDING_NONZERO);
gluTessCallback(tess, GLU_TESS_BEGIN, (void (CALLBACK*)())begin);
gluTessCallback(tess, GLU_TESS_VERTEX, (void (CALLBACK*)())vertex);
gluTessCallback(tess, GLU_TESS_END, (void (CALLBACK*)())end);
gluTessCallback(tess, GLU_TESS_ERROR, (void (CALLBACK*)())error);
gluTessCallback(tess, GLU_TESS_COMBINE, (void (CALLBACK*)())combine);
glutCreateMenu(menu);
glutAddMenuEntry("Torus", M_TORUS);
glutAddMenuEntry("Cube", M_CUBE);
glutAddMenuEntry("Sphere", M_SPHERE);
glutAddMenuEntry("Icosahedron", M_ICO);
glutAddMenuEntry("Teapot", M_TEAPOT);
glutAddMenuEntry("Angle", M_ANGLE);
glutAddMenuEntry("Toggle boundary", M_BOUNDARY);
glutAttachMenu(GLUT_RIGHT_BUTTON);
glutMainLoop();
return 0; /* ANSI C requires main to return int. */
}
#else
int main(int argc, char** argv)
{
fprintf(stderr, "This program demonstrates the new tesselator API in GLU 1.2.\n");
fprintf(stderr, "Your GLU library does not support this new interface, sorry.\n");
return 0;
}
#endif /* GLU_VERSION_1_2 */
|