Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++: Drawing a 2D disk in OpenGL

Tags:

c++

opengl

I've tried to write a proper function for drawing a 2D disk on the screen with OpenGL for a few days now, and I simply can't seem to get it right :(

This is my current code:

void Disk( Float x, Float y, Float r, const Color& vColor )
{
    glBegin( GL_TRIANGLE_FAN );
        glVertex2f( x, y );
        for( Float i = 0; i <= 2 * PI + 0.1; i += 0.1 )
        {
            glVertex2f( x + sin( i ) * r, y + cos( i ) * r );
        }
    glEnd();
}

When zooming in, the resulting disk shows spikes, not as in edges but really spikes pointing out.

Also the function doesn't draw one disk only, but always a bit more than one - which means that if alpha is enabled, the results look wrong.

  • What do I need to change in my function so it properly draws a disk?
like image 875
Janx Avatar asked Feb 23 '11 18:02

Janx


1 Answers

void circle(float x, float y, float r, int segments)
{
    glBegin( GL_TRIANGLE_FAN );
        glVertex2f(x, y);
        for( int n = 0; n <= segments; ++n ) {
            float const t = 2 * M_PI * (float)n / (float)segments;
            glVertex2f(x + sin(t) * r, y + cos(t) * r);
        }
    glEnd();
}

that should get you rid of the overdraw. About the spikes... a picture could tell a thousand words.

like image 54
datenwolf Avatar answered Nov 15 '22 00:11

datenwolf