How do i implement a circle filling algorithm in opengl?

后端 未结 2 1573
感情败类
感情败类 2020-12-21 12:41

It should be of the form circle(float xcenter, float ycenter, float radius).

相关标签:
2条回答
  • 2020-12-21 12:46

    Using GL_TRIANGLE_FAN plop down your center point and then your perimeter vertices:

    void glCircle( float x, float y, float r, bool filled = true, unsigned int subdivs = 20 ) {
        if( filled ) {
            glBegin( GL_TRIANGLE_FAN );
            glVertex2f( x, y );
        } else {
            glBegin( GL_LINE_STRIP );
        }
    
        for( unsigned int i = 0; i <= subdivs; ++i ) {
            float angle = i * ((2.0f * 3.14159f) / subdivs);
            glVertex2f( x + r * cos(angle), y + r * sin(angle) );
        }
    
        glEnd();
    }
    
    0 讨论(0)
  • 2020-12-21 13:11

    There's a gluDisk, but it has a somewhat different signature than you've given. It always centers the disk on the origin, and expects you to use glTranslate if that's not where you want your disk. It's also a bit more versatile in other ways -- the disk it draws can have a hole in the center, which you (apparently) don't care about, as well as a "loops" parameter to draw more than one disk at a time. That makes adapting it to what you've asked for just a bit more work than you'd like:

    void circle(float xcenter, float ycenter, float radius) {
        GLUquadric* quad = gluNewQuadric();
    
        glMatrixMode(GL_MODELVIEW);
        glPushMatrix();
        glLoadIdentity();
        glTranslatef(xcenter, ycenter);
    
        gluDisk(quad, 0, radius, 256, 1);
        glPopMatrix();
        gluDeleteQuadric(quad);
    }
    

    I'd say this is right at the point that it's open to question whether it's easier to just do like @genpfault said, and draw a circular triangle-fan yourself.

    0 讨论(0)
提交回复
热议问题