Drawing a circle.

  • Follow


Hi all,

A very basic question I guess :
how do I draw a circle in OpenGL,
given its center and radius  ?

I mean just a circle drawn by a continuous line,
there are no filling nor texture involved here.

I know I can do this step by step by increasing
an angle and using glBegin() with GL_LINE_STRIP
or GL_LINE_LOOP, and calling glVertex3f(x,y,z)
but this needs to be done inside a loop.

So I'm wondering if there's a dedicated
function, like glCircle( center, radius, ... ) ?

TIA
0
Reply 5.d (68) 8/26/2010 9:09:04 AM

Jean-Christophe wrote:
> Hi all,
> 
> A very basic question I guess :
> how do I draw a circle in OpenGL,
> given its center and radius  ?
> 
> I mean just a circle drawn by a continuous line,
> there are no filling nor texture involved here.
> 
> I know I can do this step by step by increasing
> an angle and using glBegin() with GL_LINE_STRIP
> or GL_LINE_LOOP, and calling glVertex3f(x,y,z)
> but this needs to be done inside a loop.
> 
> So I'm wondering if there's a dedicated
> function, like glCircle( center, radius, ... ) ?

I was wondering the same some time ago, but for the ellipse (that is 
just a more general case), and the simplest solution I found was to use 
a texture.

If someone knows a better way (without using lists), I would be very 
interested to hear.
0
Reply Vladimir 8/26/2010 9:15:19 AM


"Vladimir Jovic" <vladaspams@gmail.com> wrote in message
news:i55bb7$aj6$1@news.albasani.net...
> Jean-Christophe wrote:

>> So I'm wondering if there's a dedicated
>> function, like glCircle( center, radius, ... ) ?
>
> I was wondering the same some time ago, but for the ellipse (that is just
> a more general case), and the simplest solution I found was to use a
> texture.
>
> If someone knows a better way (without using lists), I would be very
> interested to hear.

gluDisk()

-- 
Charles E Hardwidge 

0
Reply Charles 8/26/2010 9:26:29 AM

On Aug 26, 11:26=A0am, "Charles E Hardwidge"

| So I'm wondering if there's a dedicated
| function, like glCircle( center, radius, ... ) ?

> gluDisk()

Yes I know gluDisk( qobj, innerRadius, outerRadius, slices, loops );
but I want to avoid using DISK to draw a simple CIRCLE.

For now I use the following code,
but I want to avoid such a time-consuming loop.

glBegin( GL_LINE_STRIP );
 for( a =3D 0.0f; a <=3D 6.2831853f; a +=3D 1.0f )
 { y =3D d * (float)sin( a );
   x =3D d * (float)cos( a );
   glVertex3f( x, y, z );
 }
glEnd();

0
Reply Jean 8/26/2010 10:31:35 AM

On Thu, 26 Aug 2010 02:09:04 -0700, Jean-Christophe wrote:

> A very basic question I guess :
> how do I draw a circle in OpenGL,
> given its center and radius  ?
> 
> I mean just a circle drawn by a continuous line,
> there are no filling nor texture involved here.
> 
> I know I can do this step by step by increasing
> an angle and using glBegin() with GL_LINE_STRIP
> or GL_LINE_LOOP, and calling glVertex3f(x,y,z)
> but this needs to be done inside a loop.
> 
> So I'm wondering if there's a dedicated
> function, like glCircle( center, radius, ... ) ?

gluDisk() creates a filled annulus (or disc if the inner radius is zero).

Alternatively, you can use evaluators. The following code generates a
quarter of the unit circle in the X-Y plane using a quadratic rational
Bezier curve:

	#define K 0.7071067811865475	/* sqrt(1/2) */
	static const GLfloat cpts[3][4] = {
	    {0, 1, 0, 1},
	    {K, K, 0, K},
	    {1, 0, 0, 1}
	};
	#define NSEGS 50	/* number of segments */

        glEnable(GL_MAP1_VERTEX_4);
        glMap1f(GL_MAP1_VERTEX_4, 0, 1, &cpts[0][0]);
        glMapGrid1f(NSEGS, 0, 1);
        glEvalMesh1(GL_LINE, 0, NSEGS);

Reflect or rotate the control points to obtain the other quadrants.

You can do it with three 120-degree sections, but the angular spacing of
the vertices is less uniform. Or you can use two cubic curves (three
for more uniform spacing), or even a single quintic curve (6 control
points).

All polynomial or rational approaches have some nonuniformity in the
angular spacing. If you want perfectly uniform angular spacing, you have
to generate the vertices yourself with sin/cos (which is what gluDisk()
does).

0
Reply Nobody 8/26/2010 10:35:55 AM

On Aug 26, 12:31=A0pm, Jean-Christophe <5...@free.fr> wrote:
>
> Yes I know gluDisk( qobj, innerRadius, outerRadius, slices, loops );
> but I want to avoid using DISK to draw a simple CIRCLE.
>
> For now I use the following code,
> but I want to avoid such a time-consuming loop.
>
> glBegin( GL_LINE_STRIP );
> =A0for( a =3D 0.0f; a <=3D 6.2831853f; a +=3D 1.0f )
> =A0{ y =3D d * (float)sin( a );
> =A0 =A0x =3D d * (float)cos( a );
> =A0 =A0glVertex3f( x, y, z );
> =A0}
> glEnd();


If it's really a performance problem (have you
measured it or are you just imagining?) you
could put the values in an array and use
glDrawArrays().


> So I'm wondering if there's a dedicated
> function, like glCircle( center, radius, ... ) ?

There is, but it probably uses a loop just ;ike
yours.
0
Reply fungus 8/26/2010 10:41:38 AM

"Jean-Christophe" <5.d@free.fr> wrote in message
news:a8b6bdfa-c252-401f-9d10-c18a63bdcc81@l6g2000yqb.googlegroups.com...
> On Aug 26, 11:26 am, "Charles E Hardwidge"
>
> | So I'm wondering if there's a dedicated
> | function, like glCircle( center, radius, ... ) ?
>
>> gluDisk()
>
> Yes I know gluDisk( qobj, innerRadius, outerRadius, slices, loops );
> but I want to avoid using DISK to draw a simple CIRCLE.
>
> For now I use the following code,
> but I want to avoid such a time-consuming loop.
>
> glBegin( GL_LINE_STRIP );
> for( a = 0.0f; a <= 6.2831853f; a += 1.0f )
> { y = d * (float)sin( a );
>   x = d * (float)cos( a );
>   glVertex3f( x, y, z );
> }
> glEnd();
>

If your circle doesn't change much you could cache it into a texture and do
nothing more fancy than blast it out when you need it.

You can optimise in the algorithm. It's cheap and dirty but might be what
you're looking for:

http://slabode.exofire.net/circle_draw.shtml

Fragment shaders:

http://people.freedesktop.org/~idr/OpenGL_tutorials/03-fragment-intro.html

Use a "vector shader" technique:

http://www.valvesoftware.com/publications/2007/SIGGRAPH2007_AlphaTestedMagnification.pdf


-- 
Charles E Hardwidge 

0
Reply Charles 8/26/2010 10:56:33 AM

On Aug 26, 11:09=A0am, Jean-Christophe

> how do I draw a circle in OpenGL,
> given its center and radius =A0?

Ok - thanks to all of you for your answers !
0
Reply Jean 8/26/2010 11:16:32 AM

Jean-Christophe wrote:
> On Aug 26, 11:26 am, "Charles E Hardwidge"
> 
> | So I'm wondering if there's a dedicated
> | function, like glCircle( center, radius, ... ) ?
> 
>> gluDisk()
> 
> Yes I know gluDisk( qobj, innerRadius, outerRadius, slices, loops );
> but I want to avoid using DISK to draw a simple CIRCLE.
> 
> For now I use the following code,
> but I want to avoid such a time-consuming loop.
> 
> glBegin( GL_LINE_STRIP );
>  for( a = 0.0f; a <= 6.2831853f; a += 1.0f )
>  { y = d * (float)sin( a );
>    x = d * (float)cos( a );
>    glVertex3f( x, y, z );
>  }
> glEnd();

Don't think anyone's mentioned this, so I will. You can get rid of the 
costly trigonometric operations by using the midpoint circle algorithm 
(Google it).

In terms of implementing it for use with OpenGL, I guess you could write 
the points it produces to a vertex array and do it that way. There may 
be more OpenGL-friendly approaches, though, I'm not sure.

Cheers,
Stu
0
Reply Stuart 8/26/2010 8:58:12 PM

8 Replies
379 Views

(page loaded in 0.071 seconds)

Similiar Articles:













7/23/2012 6:39:54 AM


Reply: