draw a circle??

how can i draw a simple circle using opengl ??plz help

you can use GL_LINE_LOOP computing each circle points coordinates

hello,
I’ve been looking for a solution on how to draw a circles or arcs with opengl. I found a way by myself. In fact you create a RGBA texture displaying the circle you want, can be a disk or any circular thing you like. You just need to make transparent parts of the textures that are not expected to be drawn.
To draw a full circle, it’s easy, you just have to draw a textured quad with blending activated. If you want to just draw an arc, you have to divide your quad in several triangles which have the center of the circle as a common vertex. Here is the code (I’m french, it helps to understand the names, etc…), the code is for ortho mode so only 2D in glVertex, but it’s easy to adapt to 3D:


void glTexCercle(int xc,int yc, int ray,int texID,float Angd,float Angf)
{
// (xc,yc)=center
// ray=radius
// texID=texture number
// Angd=starting angle (in degrees)
// Angf=ending angle (in degrees)
float angd=Angd,angf=Angf;
if (angf==angd) return;
// don’t forget to enable textures
// don’t forget to enable blending with
// glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
// disable face culling if needed
glBindTexture(GL_TEXTURE_2D,texID);
bool ispre=true,isder=false;
// ispre -> true=we are drawing the starting angle
// isder -> false=we are not drawing the ending angle
if (angf<angd) // we exchange values of angf and angd if
// angf<angd
{
float tpa=angd;
angd=angf;
angf=tpa;
}
if (angf-angd>=360) // if a full circle (or more)
// … we draw a full circle
{
angd=-45;
angf=315;
}
// ang=0 -> faces up
// we’re gonna divide the square into 4 triangles
// [-45°,45°]/[45°,135°]/[135°,225°]/[225°,315°]
// so now we check that angd is in [-45°,315°]
while(angd<-45)
{
angd+=360;
angf+=360;
}
while(angd>=315)
{
angd-=360;
angf-=360;
}
// angq will be the value of the start of the interval
// we are now checking, so we make it at the beginning
// of the interval where angd is
float angq=-45;
while(angd>=angq+90) angq+=90;
// we’re gonna draw up to 5 triangles that have the
// center of circle as comme vertex
glBegin(GL_TRIANGLE_FAN);
glTexCoord2f(0.5f,0.5f);
glVertex2i(xc,yc);
// these 2 vars are increment values from the center
// to reach the limit of the square according the angle
float addx,addy;
// we loop while we’ve not reached the ending angle
while(!isder)
{
if (angf<=angq+90) isder=true; //check when we
// reached the last quarter
// we calc the limit for the starting angle of
// the triangle
if (ispre) // the starting angle is angd
{
AddAngCerc(angd,&addx,&addy);
ispre=false;
}
else // the starting angle is angq
AddAngCerc(angq,&addx,&addy);
glTexCoord2f(0.5f+0.5faddx,0.5f-0.5faddy);
glVertex2i(xc+(int)(rayaddx),yc+(int) (rayaddy));
// we calc the limit for the ending angle of
// the triangle
if (isder) // the ending angle is angf
AddAngCerc(angf,&addx,&addy);
else // the ending angle is angq+90
AddAngCerc(angq+90,&addx,&addy);
glTexCoord2f(0.5f+0.5faddx,0.5f-0.5faddy);
glVertex2i(xc+(int)(rayaddx),yc+(int) (rayaddy));
angq+=90; // next interval
}
glEnd();
}

Now you say “what is this AddAngCerc function called?”.
It’s just a function calculating addx and addy, it means what is the position of the limit of the square according the angle. It just gives a number between 0 and 1 and is then multiplied by the number we like. As it is in [0,1], we just calculate it once to be used in glTexCoord and glVertex. Here is the code:


void AddAngCerc(float Ang,float *Adx,float *Ady)
{
float ang=Ang;
// we set the angle in the [-45°,315°]
while (ang<-45) ang+=360;
while (ang>360+45) ang-=360;
if (ang<=45) // in [-45,45] the formula is:
{
Adx=tan(angCST_PI/180);
*Ady=1;
}
else if (ang<=90+45) // in [45,135] the formula is:
{
*Adx=1;
*Ady=-tan((ang-90)*CST_PI/180);
}
else if (ang<=180+45) // in [135,225] the formula is:
{
*Adx=-tan((ang-180)*CST_PI/180);
*Ady=-1;
}
else if (ang<=270+45) // in [225,315] the formula is:
{
*Adx=-1;
*Ady=tan((ang-270)*CST_PI/180);
}
else // in [315,405] the formula is:
{ // angf can be more than 315
*Adx=tan((ang-360)*CST_PI/180);
*Ady=1;
}
}

So that’s the answer I found to drawing a circle in opengl (and as it’s textured, results can be really nice).
Have a look at two circles drawn there:
http://img169.imageshack.us/my.php?image=image2qg3.jpg
Hope it will help anybody.
David

If it’s a filled circle:

glEnable(GL_POINT_SMOOTH);
glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);
glPointSize(radius);
glPoint(x, y, z);

That is a 3 year old thread. The people are dead and gone.