Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Page 1 of 2 12 LastLast
Results 1 to 10 of 18

Thread: Polygon 2D

  1. #1
    Junior Member Newbie
    Join Date
    May 2013
    Posts
    12

    Polygon 2D

    Hi. Can anyone tell me why my polygon is drawing with extensions...

    Like the picture:



    There is my code:

    Code :
                    glPushMatrix(); 
     
                    glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); 
     
                    CMTObject::Draw(); 
     
                    glBegin(GL_POLYGON); 
                    for(CMTPolygon::iterator it = begin(); it != end(); it++) 
                    { 
                            glColor3f(1,0,0); 
                            glVertex2d(it->x,it->y); 
                    } 
                    glEnd(); 
     
                    glVertexPointer(2, GL_DOUBLE, 0, data()); 
     
                    glColor3f(0,0,0); 
                    glDrawArrays(GL_LINE_LOOP,0,size());

    How I can paint my polygon interior (inside lineloop)?

  2. #2
    Advanced Member Frequent Contributor
    Join Date
    Aug 2004
    Location
    munich, germany
    Posts
    659
    looks to me like a GL_POLYGON is internally drawn as a GL_TRIANGLE_FAN.
    if you switch to glPolygonMode(GL_FRONT_AND_BACK, GL_LINE), you should see that all triangles start at one single point.
    and since your polygon is not konvex, some triangles overlap.

    btw, which country is that?

  3. #3
    Junior Member Newbie
    Join Date
    May 2013
    Posts
    12

    Points - Blumenau

    Quote Originally Posted by RigidBody View Post
    looks to me like a GL_POLYGON is internally drawn as a GL_TRIANGLE_FAN.
    if you switch to glPolygonMode(GL_FRONT_AND_BACK, GL_LINE), you should see that all triangles start at one single point.
    and since your polygon is not konvex, some triangles overlap.

    btw, which country is that?
    Its a city limits from Blumenau - Santa Catarina - Brasil...

    This is a non convex polygon...

  4. #4
    Member Regular Contributor
    Join Date
    Aug 2008
    Posts
    433
    OpenGL only guarantees drawing convex polygons correctly with glBegin/glEnd. It states this here: http://www.opengl.org/sdk/docs/man2/xhtml/glBegin.xml
    The bit that covers in in the compatibility spec is:
    Quote Originally Posted by OpenGL_4_3_Compatibility
    Only convex polygons are guaranteed to be drawn correctly by the GL. If aspecified polygon is nonconvex when projected onto the window, then the rendered
    polygon need only lie within the convex hull of the projected vertices defining its
    boundary

  5. #5
    Junior Member Newbie
    Join Date
    May 2013
    Posts
    12

    How paint an non convex polygon?

    What is the best way to do this "painting" inside the non convex polygon?

  6. #6
    Member Regular Contributor trinitrotoluene's Avatar
    Join Date
    Sep 2008
    Location
    Montérégie,Québec
    Posts
    362
    Quote Originally Posted by opessoa View Post
    What is the best way to do this "painting" inside the non convex polygon?
    You can draw filled concave polygon with the stencil buffer. See this thread and this link.

    Edit: with chromium on linux , open the link in a new tab otherwise the link bring me at the bottom of the web page.
    Last edited by trinitrotoluene; 05-29-2013 at 09:55 AM.

  7. #7
    Junior Member Newbie
    Join Date
    May 2013
    Posts
    12

    Tess

    Sorry trinitrotoluene... It's no work. :P

    Im trying to use Tess, but steal not working, can you help me?

    Code :
    GLvoid tcbBegin(GLenum type)
    	{
    		glBegin(type);
    	}
     
    	GLvoid tcbVertex(GLvoid *vertex)
    	{
    		glVertex3dv((GLdouble*)vertex);
    	}
     
    	GLvoid tcbEnd()
    	{
    		glEnd();
    	}
     
    	GLvoid combineCallback(GLdouble coords[3], GLdouble *vertex_data[4], GLfloat weight[4], GLdouble **dataOut )
    	{
    		GLdouble *vertex;
    		vertex = (GLdouble *) malloc(3 * sizeof(GLdouble));
    		vertex[0] = coords[0];
    		vertex[1] = coords[1];
    		vertex[2] = coords[2];
    		*dataOut = vertex;
    	}
     
    	GLvoid errorCallback(GLenum errorCode)
    	{
    		const GLubyte *estring;
    		estring = gluErrorString(errorCode);
    		fprintf(stderr, "Tessellation Error: %s\n", estring);
    	}
     
    void draw()
    {
    double* dData = reinterpret_cast<double*>(data());
     
    GLuint startList = glGenLists(2);
     
    GLUtesselator* wall1 = gluNewTess();
    gluTessCallback(wall1, GLU_TESS_VERTEX, (GLvoid (APIENTRY *) ()) &tcbVertex);
    gluTessCallback(wall1, GLU_TESS_BEGIN, (GLvoid (APIENTRY *) ()) &tcbBegin);
    gluTessCallback(wall1, GLU_TESS_END, (GLvoid (APIENTRY *) ()) &tcbEnd);
    gluTessCallback(wall1, GLU_TESS_COMBINE, (GLvoid (APIENTRY *) ()) &combineCallback);
    gluTessCallback(wall1, GLU_TESS_ERROR, (GLvoid (APIENTRY *) ()) &errorCallback);
     
    glNewList(startList, GL_COMPILE);
    glShadeModel(GL_SMOOTH);
    gluTessProperty(wall1, GLU_TESS_WINDING_RULE, GLU_TESS_WINDING_POSITIVE);
     
    gluTessBeginPolygon(wall1, NULL);
    	gluTessBeginContour(wall1);
    	for (int i = 0; i < size(); i+=3)
    	{
    		gluTessVertex(wall1, &dData[i], &dData[i]);
    	}
    	gluTessEndContour(wall1);
    gluTessEndPolygon(wall1);
     
    glEndList();
     
    gluDeleteTess(wall1);
    }

  8. #8
    Member Regular Contributor trinitrotoluene's Avatar
    Join Date
    Sep 2008
    Location
    Montérégie,Québec
    Posts
    362
    Try that with a simple concave polygon first:
    Code :
     
    //INIT CODE
    glEnable(GL_DEPTH_TEST);
     
     
    //DRAW CODE
    glClear(GL_COLOR_BUFFER_BIT|GL_STENCIL_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
     
    glEnable(GL_STENCIL_TEST);
    glBindVertexArray(m_VAO);
      //Dont write in the color buffer
      //Dont write in the depth buffer
      glColorMask(GL_FALSE,GL_FALSE,GL_FALSE,GL_FALSE);
      glDepthMask(GL_FALSE);
      glEnable(GL_STENCIL_TEST);
      glStencilFunc(GL_ALWAYS,0x1,0x1);
      glStencilOp(GL_KEEP,GL_KEEP,GL_INVERT);
      glDrawArrays(GL_TRIANGLE_FAN,0,m_Vertices.size()); 
     
     
    //Draw the triangle strip a second time but only where the stencil is one
      glDepthMask(GL_TRUE);
      glColorMask(GL_TRUE,GL_TRUE,GL_TRUE,GL_TRUE);
     
      glStencilFunc(GL_EQUAL,0x1,0x1);
    //NOT TESTED:  using GL_INVERT as the last parameter permit to clear stencil value to 0 where 
    //stencil value is one and to draw multiple concave polygon in the same frame.   
      glStencilOp(GL_KEEP,GL_KEEP,GL_KEEP);
      glDrawArrays(GL_TRIANGLE_FAN,0,m_Vertices.size()); 
     
    glDisable(GL_STENCIL_TEST);

    It work fine for me.

    Also,when you create your rendering context with GLUT for example, you must ask for a depth buffer with a stencil buffer to be present when you call glutInitDisplayMode.

  9. #9
    Junior Member Newbie
    Join Date
    May 2013
    Posts
    12

    Pixel errors

    It's work, but with some pixels error.



    How to solve this?

  10. #10
    Member Regular Contributor trinitrotoluene's Avatar
    Join Date
    Sep 2008
    Location
    Montérégie,Québec
    Posts
    362
    Which algorithm are you using, the one with stencil buffer or the one with GLU tessellation (a deprecated library) ?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •