Sergio Morales
Sergio Morales

Reputation: 2640

Issue with glPolygonMode - GL_FILL "skipping" vertices

I'm having a weird issue when trying to draw a polygon and filling it with a particular color:

If I set the polygon mode as:

glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);

The polygon renders just fine:

Polygon rendered after setting the polygon fill mode to GL_LINE

However, as soon as I replace that line with the following:

glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);

The polygon doesn't fill right, but it seems like most lines get projected towards its first vertex, or something along those lines:

enter image description here

I'm obviously doing something wrong. What I want to do is keep the color inside the polygon, however it seems to be ignoring several vertices. What might be wrong?

Here's some selected parts of my code that might be of interest. I'm skipping over some data structures loading and other stuff that might not be very relevant:

int main(int argc, char **argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);

    glutInitWindowSize(640, 480);
    glutInitWindowPosition(150, 100);
    glutCreateWindow("CR-View GL");

    glutDisplayFunc(display);

    glutMainLoop();
    return 0;
}

void display(void) {
    glClearColor(0, 0, 0, 0);
    glClear(GL_COLOR_BUFFER_BIT);
    /* Set drawing color */
    glColor3f(1, 0, 1);

    drawPolys(currentDrawingMode);

    /* Clear screen and draw */
    glutSwapBuffers();
}

// Draws the polygons
void drawPolys (int id) {   
    int poly, vertex;
    // set wireframe mode (if an empty polygon is required)
    if (id == 0) {
        glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
    }

    //Sets color fill mode
    if (id == 1) {
        glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
    }

    // Draw each polygon...
    for (poly = 0; poly < polyCount; poly++) {

        glBegin(GL_POLYGON);
            // Draw each vertex...
        for (vertex = 0; vertex < Polygons[poly].vertexCount; vertex++) {
            glVertex2f((float)Polygons[poly].vertices[vertex].x, (float)Polygons[poly].vertices[vertex].y);
        }
        glEnd();
    } 
}

Upvotes: 3

Views: 1704

Answers (1)

genpfault
genpfault

Reputation: 52083

If you're using GL_POLYGON be aware that it only supports convex polygons.

Upvotes: 5

Related Questions