/************************************************************ From: Alex Gleyzer To: Chee Yap CC: g22_3033_010_fa01@cs.nyu.edu Subject: Re: [G22_3033_010_fa01] Re: hw2: opengl draws only convex polygons Date: Fri, 09 Nov 2001 18:15:48 -0500 Apparently GLUT has a special api to draw non-convex polygons: http://ask.ii.uib.no/ebt-bin/nph-dweb/dynaweb/SGI_Developer/OpenGL_PG/@ebt-link;cs=fullhtml;pt=23505?target=%25N%15_23934_START_RESTART_N%25#X 1. Attached code demonstrates how it works (version of GLUT matters - my mesa seems to have version 1.1) 2. Attached code also is leaking memory by creating new vecto for every x,yr in myVertex. The idea is that array v must stay in memory for a while, I guess GLUT stores pointer to it in some internal data structures. Ideally you have an array of vectors and you pass pointer to current point, as it is done in the manual's example. ************************************************************/ // Date: Sat, 03 Nov 2001 13:26:00 -0500 // From: Alex Gleyzer // To: yap@cs.nyu.edu // Subject: hw2: opengl draws only convex polygons // // Professor, // // We discussed this last Tuesday after class, but I had time to implement // this only today. // // Attached program demonstrates that OpenGL (I use Mesa) draws only convex // polygons with GL_POLYGON. Therefore some of the Tiger polygons will not be // displayed properly -- I can see it when using Manhattan county data. // // Thanks, // Alex // // Chee's Note: main modification is to try several alternative // drawing of rotated/flipped versions of the polygons to see // how GLUT manage it. The orignal polygon in in the first // quadrant, the rest occur in ccw order. /* * This program demostrates that opengl can draw only convex polygons. */ // #include #include // #include #include // #include #include #include /* the callback routines registered by gluTessCallback() */ void beginCallback(GLenum which) { glBegin(which); } void endCallback(void) { glEnd(); } void errorCallback(GLenum errorCode) { const GLubyte *estring; estring = gluErrorString(errorCode); fprintf (stderr, "Tessellation Error: %s\n", estring); exit (0); } GLUtriangulatorObj *to; void initTess() { glClear(GL_COLOR_BUFFER_BIT); to = gluNewTess(); gluTessCallback(to, GLU_TESS_VERTEX, (GLvoid (*) ()) &glVertex3dv); gluTessCallback(to, GLU_TESS_BEGIN, (GLvoid (*) ()) &beginCallback); gluTessCallback(to, GLU_TESS_END, (GLvoid (*) ()) &endCallback); gluTessCallback(to, GLU_TESS_ERROR, (GLvoid (*) ()) &errorCallback); } void myVertex(GLdouble x, GLdouble y) { GLdouble *v = new GLdouble[3]; v[0] = x; v[1] = y; v[2] = 0; gluTessVertex(to, v, v); } void myDisplay() { initTess(); glClear(GL_COLOR_BUFFER_BIT); // THIS IS THE ORIGINAL polygon of Gleyzer: glColor3f(0, 0, 1); gluBeginPolygon(to); myVertex(0, 0); myVertex(0, 2); myVertex(2, 2); myVertex(2, 1); myVertex(1, 1); myVertex(1, 0); gluEndPolygon(to); glColor3f(1, 0, 0); glBegin(GL_LINE_LOOP); glVertex2f(0, 0); glVertex2f(0, 2); glVertex2f(2, 2); glVertex2f(2, 1); glVertex2f(1, 1); glVertex2f(1, 0); glEnd(); // THIS IS THE REVERSE of the polygon of Gleyzer: glTranslated(-3, 0, 0); glColor3f(0, 0, 1); gluBeginPolygon(to); myVertex(0, 0); myVertex(1, 0); myVertex(1, 1); myVertex(2, 1); myVertex(2, 2); myVertex(0, 2); gluEndPolygon(to); glColor3f(1, 0, 0); glBegin(GL_LINE_LOOP); glVertex2f(0, 0); glVertex2f(1, 0); glVertex2f(1, 1); glVertex2f(2, 1); glVertex2f(2, 2); glVertex2f(0, 2); glEnd(); // THIS IS THE Horizontal Flip of the polygon of Gleyzer: glTranslated(0, -3, 0); glColor3f(0, 0, 1); gluBeginPolygon(to); myVertex(0, 0); myVertex(2, 0); myVertex(2, 1); myVertex(1, 1); myVertex(1, 2); myVertex(0, 2); gluEndPolygon(to); glColor3f(1, 0, 0); glBegin(GL_LINE_LOOP); glVertex2f(0, 0); glVertex2f(2, 0); glVertex2f(2, 1); glVertex2f(1, 1); glVertex2f(1, 2); glVertex2f(0, 2); glEnd(); // THIS IS THE Vertical Flip of the polygon of Gleyzer: glTranslated(3, 0, 0); glColor3f(0, 0, 1); gluBeginPolygon(to); myVertex(1, 0); myVertex(2, 0); myVertex(2, 2); myVertex(0, 2); myVertex(0, 1); myVertex(1, 1); gluEndPolygon(to); glColor3f(1, 0, 0); glBegin(GL_LINE_LOOP); glVertex2f(1, 0); glVertex2f(2, 0); glVertex2f(2, 2); glVertex2f(0, 2); glVertex2f(0, 1); glVertex2f(1, 1); glEnd(); // get back to original position glTranslated(0, 3, 0); glFlush(); } int main(int argc, char *argv[]) { // init GL glutInit(&argc, argv); // initialize the toolkit cerr << gluGetString(GLU_VERSION) << endl; glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); // set display mode glutInitWindowSize(640,480); // set window size glutInitWindowPosition(100, 150); // set window position on screen glutCreateWindow("Polygon test"); // open the screen window glutDisplayFunc(myDisplay); // register redraw function glClearColor(1.0, 1.0, 1.0, 0.0); // set white background color glColor3f(0.0f, 0.0f, 0.0f); // set the drawing color glLineWidth(2); glPointSize(3); // Reset the coordinate system before modifying glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(-5, 5, -5, 5); glMatrixMode(GL_MODELVIEW); glutMainLoop(); } --------------060708010403010300000104-- _______________________________________________ G22_3033_010_fa01 mailing list G22_3033_010_fa01@cs.nyu.edu http://www.cs.nyu.edu/mailman/listinfo/g22_3033_010_fa01