How do i implement a circle filling algorithm in opengl?
It s开发者_开发知识库hould be of the form circle(float xcenter, float ycenter, float radius)
.
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();
}
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.
精彩评论