How do i implement a circle filling algorithm in opengl?

后端 未结 2 1586
感情败类
感情败类 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();
    }
    

提交回复
热议问题