Updated demo for new GLU 1.3 tessellation. Added optimized rendering

by saving the output of the tessellation into display lists.
This commit is contained in:
Gareth Hughes
1999-11-04 04:00:42 +00:00
parent 64e10feb25
commit eb459c6070

View File

@@ -1,4 +1,4 @@
/* $Id: tessdemo.c,v 1.2 1999/09/19 20:09:00 tanner Exp $ */ /* $Id: tessdemo.c,v 1.3 1999/11/04 04:00:42 gareth Exp $ */
/* /*
* A demo of the GLU polygon tesselation functions written by Bogdan Sikorski. * A demo of the GLU polygon tesselation functions written by Bogdan Sikorski.
@@ -8,9 +8,16 @@
cc -g -ansi -prototypes -fullwarn -float -I../include -DSHM tess_demo.c -L../lib -lglut -lMesaGLU -lMesaGL -lm -lX11 -lXext -lXmu -lfpe -lXext -o tess_demo cc -g -ansi -prototypes -fullwarn -float -I../include -DSHM tess_demo.c -L../lib -lglut -lMesaGLU -lMesaGL -lm -lX11 -lXext -lXmu -lfpe -lXext -o tess_demo
*/ */
/*
* Updated for GLU 1.3 tessellation by Gareth Hughes <garethh@bell-labs.com>
*/
/* /*
* $Log: tessdemo.c,v $ * $Log: tessdemo.c,v $
* Revision 1.3 1999/11/04 04:00:42 gareth
* Updated demo for new GLU 1.3 tessellation. Added optimized rendering
* by saving the output of the tessellation into display lists.
*
* Revision 1.2 1999/09/19 20:09:00 tanner * Revision 1.2 1999/09/19 20:09:00 tanner
* *
* lots of autoconf updates * lots of autoconf updates
@@ -44,29 +51,9 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#define MAX_POINTS 200 #define MAX_POINTS 256
#define MAX_CONTOURS 50 #define MAX_CONTOURS 32
#define MAX_TRIANGLES 256
int menu;
typedef enum{ QUIT, TESSELATE, CLEAR } menu_entries;
typedef enum{ DEFINE, TESSELATED } mode_type;
struct
{
GLint p[MAX_POINTS][2];
GLuint point_cnt;
} contours[MAX_CONTOURS];
GLuint contour_cnt;
GLsizei width,height;
mode_type mode;
struct
{
GLsizei no;
GLfloat color[3];
GLint p[3][2];
GLclampf p_color[3][3];
} triangle;
#ifndef GLCALLBACK #ifndef GLCALLBACK
#ifdef CALLBACK #ifdef CALLBACK
@@ -76,368 +63,496 @@ struct
#endif #endif
#endif #endif
typedef enum{ QUIT, TESSELATE, CLEAR } menu_entries;
typedef enum{ DEFINE, TESSELATED } mode_type;
void GLCALLBACK my_error(GLenum err) static GLsizei width, height;
static GLuint contour_cnt;
static GLuint triangle_cnt;
static mode_type mode;
static int menu;
static GLuint list_start;
static GLfloat edge_color[3];
static struct
{ {
int len,i; GLint p[MAX_POINTS][2];
char const *str; GLuint point_cnt;
} contours[MAX_CONTOURS];
glColor3f(0.9,0.9,0.9); static struct
glRasterPos2i(5,5); {
str=(const char *)gluErrorString(err); GLsizei no;
len=strlen(str); GLint p[3][2];
for(i=0;i<len;i++) GLclampf color[3][3];
glutBitmapCharacter(GLUT_BITMAP_9_BY_15,str[i]); } triangles[MAX_TRIANGLES];
void GLCALLBACK my_error( GLenum err )
{
int len, i;
char const *str;
glColor3f( 0.9, 0.9, 0.9 );
glRasterPos2i( 5, 5 );
str = (const char *) gluErrorString( err );
len = strlen( str );
for ( i = 0 ; i < len ; i++ ) {
glutBitmapCharacter( GLUT_BITMAP_9_BY_15, str[i] );
}
} }
void GLCALLBACK begin_callback(GLenum mode) void GLCALLBACK begin_callback( GLenum mode )
{ {
triangle.no=0; /* Allow multiple triangles to be output inside the begin/end pair. */
triangle_cnt = 0;
triangles[triangle_cnt].no = 0;
} }
void GLCALLBACK edge_callback(GLenum flag) void GLCALLBACK edge_callback( GLenum flag )
{ {
if(flag==GL_TRUE) /* Persist the edge flag across triangles. */
{ if ( flag == GL_TRUE )
triangle.color[0]=1.0; {
triangle.color[1]=1.0; edge_color[0] = 1.0;
triangle.color[2]=0.5; edge_color[1] = 1.0;
} edge_color[2] = 0.5;
else }
{ else
triangle.color[0]=1.0; {
triangle.color[1]=0.0; edge_color[0] = 1.0;
triangle.color[2]=0.0; edge_color[1] = 0.0;
} edge_color[2] = 0.0;
}
} }
void GLCALLBACK end_callback() void GLCALLBACK end_callback()
{ {
glBegin(GL_LINES); GLint i;
glColor3f(triangle.p_color[0][0],triangle.p_color[0][1],
triangle.p_color[0][2]); glBegin( GL_LINES );
glVertex2i(triangle.p[0][0],triangle.p[0][1]);
glVertex2i(triangle.p[1][0],triangle.p[1][1]); /* Output the three edges of each triangle as lines colored
glColor3f(triangle.p_color[1][0],triangle.p_color[1][1], according to their edge flag. */
triangle.p_color[1][2]); for ( i = 0 ; i < triangle_cnt ; i++ )
glVertex2i(triangle.p[1][0],triangle.p[1][1]); {
glVertex2i(triangle.p[2][0],triangle.p[2][1]); glColor3f( triangles[i].color[0][0],
glColor3f(triangle.p_color[2][0],triangle.p_color[2][1], triangles[i].color[0][1],
triangle.p_color[2][2]); triangles[i].color[0][2] );
glVertex2i(triangle.p[2][0],triangle.p[2][1]);
glVertex2i(triangle.p[0][0],triangle.p[0][1]); glVertex2i( triangles[i].p[0][0], triangles[i].p[0][1] );
glEnd(); glVertex2i( triangles[i].p[1][0], triangles[i].p[1][1] );
glColor3f( triangles[i].color[1][0],
triangles[i].color[1][1],
triangles[i].color[1][2] );
glVertex2i( triangles[i].p[1][0], triangles[i].p[1][1] );
glVertex2i( triangles[i].p[2][0], triangles[i].p[2][1] );
glColor3f( triangles[i].color[2][0],
triangles[i].color[2][1],
triangles[i].color[2][2] );
glVertex2i( triangles[i].p[2][0], triangles[i].p[2][1] );
glVertex2i( triangles[i].p[0][0], triangles[i].p[0][1] );
}
glEnd();
} }
void GLCALLBACK vertex_callback(void *data) void GLCALLBACK vertex_callback( void *data )
{ {
GLsizei no; GLsizei no;
GLint *p; GLint *p;
p=(GLint *)data; p = (GLint *) data;
no=triangle.no; no = triangles[triangle_cnt].no;
triangle.p[no][0]=p[0];
triangle.p[no][1]=p[1]; triangles[triangle_cnt].p[no][0] = p[0];
triangle.p_color[no][0]=triangle.color[0]; triangles[triangle_cnt].p[no][1] = p[1];
triangle.p_color[no][1]=triangle.color[1];
triangle.p_color[no][2]=triangle.color[2]; triangles[triangle_cnt].color[no][0] = edge_color[0];
++(triangle.no); triangles[triangle_cnt].color[no][1] = edge_color[1];
triangles[triangle_cnt].color[no][2] = edge_color[2];
/* After every three vertices, initialize the next triangle. */
if ( ++(triangles[triangle_cnt].no) == 3 )
{
triangle_cnt++;
triangles[triangle_cnt].no = 0;
}
} }
void set_screen_wh(GLsizei w, GLsizei h) void set_screen_wh( GLsizei w, GLsizei h )
{ {
width=w; width = w;
height=h; height = h;
} }
void tesse(void) void tesse( void )
{ {
GLUtesselator *tobj; GLUtesselator *tobj;
GLdouble data[3]; GLdouble data[3];
GLuint i,j,point_cnt; GLuint i, j, point_cnt;
tobj=gluNewTess(); list_start = glGenLists( 2 );
if(tobj!=NULL)
{ tobj = gluNewTess();
glClear(GL_COLOR_BUFFER_BIT);
glColor3f (0.7, 0.7, 0.0); if ( tobj != NULL )
gluTessCallback(tobj,GLU_BEGIN,glBegin); {
gluTessCallback(tobj,GLU_END,glEnd); gluTessCallback( tobj, GLU_BEGIN, glBegin );
gluTessCallback(tobj,GLU_ERROR,my_error); gluTessCallback( tobj, GLU_VERTEX, glVertex2iv );
gluTessCallback(tobj,GLU_VERTEX,glVertex2iv); gluTessCallback( tobj, GLU_END, glEnd );
gluBeginPolygon(tobj); gluTessCallback( tobj, GLU_ERROR, my_error );
for(j=0;j<=contour_cnt;j++)
{ glNewList( list_start, GL_COMPILE );
point_cnt=contours[j].point_cnt; gluBeginPolygon( tobj );
gluNextContour(tobj,GLU_UNKNOWN);
for(i=0;i<point_cnt;i++) for ( j = 0 ; j <= contour_cnt ; j++ )
{ {
data[0]=(GLdouble)(contours[j].p[i][0]); point_cnt = contours[j].point_cnt;
data[1]=(GLdouble)(contours[j].p[i][1]); gluNextContour( tobj, GLU_UNKNOWN );
data[2]=0.0;
gluTessVertex(tobj,data,contours[j].p[i]); for ( i = 0 ; i < point_cnt ; i++ )
} {
} data[0] = (GLdouble)( contours[j].p[i][0] );
gluEndPolygon(tobj); data[1] = (GLdouble)( contours[j].p[i][1] );
glLineWidth(2.0); data[2] = 0.0;
gluTessCallback(tobj,GLU_BEGIN,begin_callback); gluTessVertex( tobj, data, contours[j].p[i] );
gluTessCallback(tobj,GLU_END,end_callback); }
gluTessCallback(tobj,GLU_VERTEX,vertex_callback); }
gluTessCallback(tobj,GLU_EDGE_FLAG,edge_callback);
gluBeginPolygon(tobj); gluEndPolygon( tobj );
for(j=0;j<=contour_cnt;j++) glEndList();
{
point_cnt=contours[j].point_cnt; gluTessCallback( tobj, GLU_BEGIN, begin_callback );
gluNextContour(tobj,GLU_UNKNOWN); gluTessCallback( tobj, GLU_VERTEX, vertex_callback );
for(i=0;i<point_cnt;i++) gluTessCallback( tobj, GLU_END, end_callback );
{ gluTessCallback( tobj, GLU_EDGE_FLAG, edge_callback );
data[0]=(GLdouble)(contours[j].p[i][0]);
data[1]=(GLdouble)(contours[j].p[i][1]); glNewList( list_start + 1, GL_COMPILE );
data[2]=0.0; gluBeginPolygon( tobj );
gluTessVertex(tobj,data,contours[j].p[i]);
} for ( j = 0 ; j <= contour_cnt ; j++ )
} {
gluEndPolygon(tobj); point_cnt = contours[j].point_cnt;
gluDeleteTess(tobj); gluNextContour( tobj, GLU_UNKNOWN );
glutMouseFunc(NULL);
glColor3f (1.0, 1.0, 0.0); for ( i = 0 ; i < point_cnt ; i++ )
glLineWidth(1.0); {
mode=TESSELATED; data[0] = (GLdouble)( contours[j].p[i][0] );
} data[1] = (GLdouble)( contours[j].p[i][1] );
data[2] = 0.0;
gluTessVertex( tobj, data, contours[j].p[i] );
}
}
gluEndPolygon( tobj );
glEndList();
gluDeleteTess( tobj );
glutMouseFunc( NULL );
mode = TESSELATED;
}
} }
void left_down(int x1,int y1) void left_down( int x1, int y1 )
{ {
GLint P[2]; GLint P[2];
GLuint point_cnt; GLuint point_cnt;
/* translate GLUT into GL coordinates */ /* translate GLUT into GL coordinates */
P[0]=x1;
P[1]=height-y1; P[0] = x1;
point_cnt=contours[contour_cnt].point_cnt; P[1] = height - y1;
contours[contour_cnt].p[point_cnt][0]=P[0];
contours[contour_cnt].p[point_cnt][1]=P[1]; point_cnt = contours[contour_cnt].point_cnt;
glBegin(GL_LINES);
if(point_cnt) contours[contour_cnt].p[point_cnt][0] = P[0];
{ contours[contour_cnt].p[point_cnt][1] = P[1];
glVertex2iv(contours[contour_cnt].p[point_cnt-1]);
glVertex2iv(P); glBegin( GL_LINES );
}
else if ( point_cnt )
{ {
glVertex2iv(P); glVertex2iv( contours[contour_cnt].p[point_cnt-1] );
glVertex2iv(P); glVertex2iv( P );
} }
glEnd(); else
glFinish(); {
++(contours[contour_cnt].point_cnt); glVertex2iv( P );
glVertex2iv( P );
}
glEnd();
glFinish();
contours[contour_cnt].point_cnt++;
} }
void middle_down(int x1, int y1) void middle_down( int x1, int y1 )
{ {
GLuint point_cnt; GLuint point_cnt;
(void) x1; (void) x1;
(void) y1; (void) y1;
point_cnt=contours[contour_cnt].point_cnt; point_cnt = contours[contour_cnt].point_cnt;
if(point_cnt>2)
{ if ( point_cnt > 2 )
glBegin(GL_LINES); {
glVertex2iv(contours[contour_cnt].p[0]); glBegin( GL_LINES );
glVertex2iv(contours[contour_cnt].p[point_cnt-1]);
contours[contour_cnt].p[point_cnt][0]= -1; glVertex2iv( contours[contour_cnt].p[0] );
glEnd(); glVertex2iv( contours[contour_cnt].p[point_cnt-1] );
glFinish();
contour_cnt++; contours[contour_cnt].p[point_cnt][0] = -1;
contours[contour_cnt].point_cnt=0;
} glEnd();
glFinish();
contour_cnt++;
contours[contour_cnt].point_cnt = 0;
}
} }
void mouse_clicked(int button,int state,int x,int y) void mouse_clicked( int button, int state, int x, int y )
{ {
x-= x%10; x -= x%10;
y-= y%10; y -= y%10;
switch(button)
{ switch ( button )
case GLUT_LEFT_BUTTON: {
if(state==GLUT_DOWN) case GLUT_LEFT_BUTTON:
left_down(x,y); if ( state == GLUT_DOWN ) {
break; left_down( x, y );
case GLUT_MIDDLE_BUTTON: }
if(state==GLUT_DOWN) break;
middle_down(x,y); case GLUT_MIDDLE_BUTTON:
break; if ( state == GLUT_DOWN ) {
} middle_down( x, y );
}
break;
}
} }
void display(void) void display( void )
{ {
GLuint i,j; GLuint i,j;
GLuint point_cnt; GLuint point_cnt;
glClear(GL_COLOR_BUFFER_BIT); glClear( GL_COLOR_BUFFER_BIT );
switch(mode)
{
case DEFINE:
/* draw grid */
glColor3f (0.6,0.5,0.5);
glBegin(GL_LINES);
for(i=0;i<width;i+=10)
for(j=0;j<height;j+=10)
{
glVertex2i(0,j);
glVertex2i(width,j);
glVertex2i(i,height);
glVertex2i(i,0);
}
glColor3f (1.0, 1.0, 0.0);
for(i=0;i<=contour_cnt;i++)
{
point_cnt=contours[i].point_cnt;
glBegin(GL_LINES);
switch(point_cnt)
{
case 0:
break;
case 1:
glVertex2iv(contours[i].p[0]);
glVertex2iv(contours[i].p[0]);
break;
case 2:
glVertex2iv(contours[i].p[0]);
glVertex2iv(contours[i].p[1]);
break;
default:
--point_cnt;
for(j=0;j<point_cnt;j++)
{
glVertex2iv(contours[i].p[j]);
glVertex2iv(contours[i].p[j+1]);
}
if(contours[i].p[j+1][0]== -1)
{
glVertex2iv(contours[i].p[0]);
glVertex2iv(contours[i].p[j]);
}
break;
}
glEnd();
}
glFinish();
break;
case TESSELATED:
/* draw lines */
tesse();
break;
}
glColor3f (1.0, 1.0, 0.0); switch ( mode )
{
case DEFINE:
/* draw grid */
glColor3f( 0.6, 0.5, 0.5 );
glBegin( GL_LINES );
for ( i = 0 ; i < width ; i += 10 )
{
for ( j = 0 ; j < height ; j += 10 )
{
glVertex2i( 0, j );
glVertex2i( width, j );
glVertex2i( i, height );
glVertex2i( i, 0 );
}
}
glColor3f( 1.0, 1.0, 0.0 );
for ( i = 0 ; i <= contour_cnt ; i++ )
{
point_cnt = contours[i].point_cnt;
glBegin( GL_LINES );
switch ( point_cnt )
{
case 0:
break;
case 1:
glVertex2iv( contours[i].p[0] );
glVertex2iv( contours[i].p[0] );
break;
case 2:
glVertex2iv( contours[i].p[0] );
glVertex2iv( contours[i].p[1] );
break;
default:
--point_cnt;
for ( j = 0 ; j < point_cnt ; j++ )
{
glVertex2iv( contours[i].p[j] );
glVertex2iv( contours[i].p[j+1] );
}
if ( contours[i].p[j+1][0] == -1 )
{
glVertex2iv( contours[i].p[0] );
glVertex2iv( contours[i].p[j] );
}
break;
}
glEnd();
}
glFinish();
break;
case TESSELATED:
/* draw triangles */
glColor3f( 0.7, 0.7, 0.0 );
glCallList( list_start );
glLineWidth( 2.0 );
glCallList( list_start + 1 );
glLineWidth( 1.0 );
glFlush();
break;
}
glColor3f( 1.0, 1.0, 0.0 );
} }
void clear( void ) void clear( void )
{ {
contour_cnt=0; contour_cnt = 0;
contours[0].point_cnt=0; contours[0].point_cnt = 0;
glutMouseFunc(mouse_clicked); triangle_cnt = 0;
mode=DEFINE;
display(); glutMouseFunc( mouse_clicked );
mode = DEFINE;
glDeleteLists( list_start, 2 );
list_start = 0;
} }
void quit( void ) void quit( void )
{ {
exit(0); exit( 0 );
} }
void menu_selected(int entry) void menu_selected( int entry )
{ {
switch(entry) switch ( entry )
{ {
case CLEAR: case CLEAR:
clear(); clear();
break; break;
case TESSELATE: case TESSELATE:
tesse(); tesse();
break; break;
case QUIT: case QUIT:
quit(); quit();
break; break;
} }
glutPostRedisplay();
} }
void key_pressed(unsigned char key,int x,int y) void key_pressed( unsigned char key, int x, int y )
{ {
(void) x; (void) x;
(void) y; (void) y;
switch(key)
{ switch ( key )
case 't': {
case 'T': case 'c':
tesse(); case 'C':
glFinish(); clear();
break; break;
case 'q': case 't':
case 'Q': case 'T':
quit(); tesse();
break; break;
case 'c': case 'q':
case 'C': case 'Q':
clear(); quit();
break; break;
} }
glutPostRedisplay();
} }
void myinit (void) void myinit( void )
{ {
/* clear background to gray */ /* clear background to gray */
glClearColor (0.4, 0.4, 0.4, 0.0); glClearColor( 0.4, 0.4, 0.4, 0.0 );
glShadeModel (GL_FLAT); glShadeModel( GL_FLAT );
glPolygonMode( GL_FRONT, GL_FILL );
menu=glutCreateMenu(menu_selected); menu = glutCreateMenu( menu_selected );
glutAddMenuEntry("clear",CLEAR);
glutAddMenuEntry("tesselate",TESSELATE); glutAddMenuEntry( "clear", CLEAR );
glutAddMenuEntry("quit",QUIT); glutAddMenuEntry( "tesselate", TESSELATE );
glutAttachMenu(GLUT_RIGHT_BUTTON); glutAddMenuEntry( "quit", QUIT );
glutMouseFunc(mouse_clicked);
glutKeyboardFunc(key_pressed); glutAttachMenu( GLUT_RIGHT_BUTTON );
contour_cnt=0;
glPolygonMode(GL_FRONT,GL_FILL); glutMouseFunc( mouse_clicked );
mode=DEFINE; glutKeyboardFunc( key_pressed );
contour_cnt = 0;
mode = DEFINE;
} }
static void reshape(GLsizei w, GLsizei h) static void reshape( GLsizei w, GLsizei h )
{ {
glViewport(0, 0, w, h); glViewport( 0, 0, w, h );
glMatrixMode(GL_PROJECTION);
glLoadIdentity(); glMatrixMode( GL_PROJECTION );
glOrtho(0.0, (GLdouble)w, 0.0, (GLdouble)h, -1.0, 1.0); glLoadIdentity();
glMatrixMode(GL_MODELVIEW); glOrtho( 0.0, (GLdouble)w, 0.0, (GLdouble)h, -1.0, 1.0 );
glLoadIdentity();
set_screen_wh(w,h); glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
set_screen_wh( w, h );
} }
static void usage( void ) static void usage( void )
{ {
printf("Use left mouse button to place vertices.\n"); printf( "Use left mouse button to place vertices.\n" );
printf("Press middle mouse button when done.\n"); printf( "Press middle mouse button when done.\n" );
printf("Select tesselate from the pop-up menu.\n"); printf( "Select tesselate from the pop-up menu.\n" );
} }
/* Main Loop /*
* Open window with initial window size, title bar, * Main Loop
* RGBA display mode, and handle input events. * Open window with initial window size, title bar,
* RGBA display mode, and handle input events.
*/ */
int main(int argc, char** argv) int main( int argc, char **argv )
{ {
usage(); usage();
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); glutInit( &argc, argv );
glutInitWindowSize (400, 400); glutInitDisplayMode( GLUT_SINGLE | GLUT_RGB );
glutCreateWindow (argv[0]); glutInitWindowSize( 400, 400 );
myinit (); glutCreateWindow( argv[0] );
glutDisplayFunc(display);
glutReshapeFunc(reshape); myinit();
glutMainLoop();
return 0; glutDisplayFunc( display );
glutReshapeFunc( reshape );
glutMainLoop();
return 0;
} }