Tom Tetlaw
Tom Tetlaw

Reputation: 113

opengl not filling in polygon

I'm trying to draw a filled in circle, but when I draw this, it only shows in wireframe, here is the code I'm using to draw:

void render_circle(Vec2 position, float radius, Vec4 colour) {
    glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
    glColor4f(colour.x, colour.y, colour.z, colour.w);

    glBegin(GL_LINE_LOOP);
    int num_segments = 30; //@todo: make this scale for larger radius
    for(int i = 0; i < num_segments; i++) {
        float theta = 2.0f * math_pi * (float)i / (float)num_segments;
        float x = radius * cosf(theta);
        float y = radius * sinf(theta);
        glVertex2f(position.x + x, position.y + y);
    }
    glEnd();
}

Upvotes: 2

Views: 820

Answers (1)

Rabbid76
Rabbid76

Reputation: 210878

GL_LINE_LOOP is a line primitive type. If you want to draw a filled polygon, then you have to use a polygon primitive type. For instance GL_TRINAGLE_FAN.
It is only possible to correctly draw convex geometry. Concave polygons may not be represented correctly, by a primitive. A possibility to deal with this, is to split concave polygons into convex parts.

Upvotes: 4

Related Questions