PDA

View Full Version : Drawing Cricles



kurt
10-23-2006, 12:13 PM
Could anyone please help me on how to draw a circle in opengl?

Thanks

Zengar
10-23-2006, 01:29 PM
You could use the circle equation using the trigonometrical functions, that is, given an angle a, the corresponding circle coordinates would be (r*cos a, r*sin a). One iterates through a (from 0 to pi*2) and draws the corresponding lines (from one point to next one and so on). More iterations will result in more smooth circle. You should setup a suitable projection for this though.
The best idea is to use line strips, like


Begin(GL_LINE_STRIP)
a := 0
while a < pi*2:
Vertex2(r*cos a, r*sin a)
a := a + pi*2/N
Vertex2(r, 0) // this one to avoid the gap
End

kurt
10-23-2006, 01:42 PM
how would this be coded?
im new to opengl

thanks

Zengar
10-23-2006, 01:55 PM
Err... do you have any programming skills? If no, you should get a book or read some tutorials first...

I suggest that you visit the nehe tutorial site (google is your friend). There are also plenty of good opengl tutorials floating around in the net. You could also search the forums.

You can use the pseudocode I used above as a basic template.

The rest is for you to discover ^^ Feel free to ask if you have questions though. Hoverer, I personally (and many other people here) would not write the program for you. The members of this boards (and I am very happy about it) seem to value individual work and effort the most.

ForrestFox
03-01-2007, 05:54 AM
I'd like to know this one too.
I only have little knowledge in C programming (studied it in 3 months), and now I'm studying computer graphics and the teacher has given us a lab to create a circle and an elipse, but I have no idea where to start, how to do it, and where to find help.

Been searching the web for the past two weeks, but dont find any tutorial that explains it on my low level.
(God knows what the teacher is thinking about that gives a laboration like that to someone like me).
Anyhow, if anyone finds a good tutorial how to create circles then please post them.

RigidBody
03-01-2007, 06:01 AM
zengar,

if you use GL_LINE_LOOP instead of GL_LINE_STRIP, there will be no gap :p

Zengar
03-01-2007, 01:04 PM
Hey, it was almost half a year ago :)

Jan
03-01-2007, 04:14 PM
void RenderCircleXY (float fRadius)
{
int div = MATHS::limit<int> (MATHS::trunc (fRadius * 20.0f), 8, 180);

float step = 360.0f / div;

float f1 = 0.0f;
float f2 = step;

VECTOR v1;
VECTOR v2;

glBegin (GL_LINES);
for (int i=0; i<div; i++)
{
v1.setVector (MATHS::fCosDeg (f1) * fRadius, MATHS::fSinDeg (f1) * fRadius, 0.0f);
v2.setVector (MATHS::fCosDeg (f2) * fRadius, MATHS::fSinDeg (f2) * fRadius, 0.0f);

glVertex3f (v1.x, v1.y, v1.z);
glVertex3f (v2.x, v2.y, v2.z);

f1 = f2;
f2 += step;
}
glEnd ();
}

void RenderSphere (float fRadius)
{
RenderCircleXY (fRadius);
glRotatef (90.0f, 0, 1, 0);
RenderCircleXY (fRadius);
glRotatef (90.0f, 1, 0, 0);
RenderCircleXY (fRadius);
}Have fun with it.
Jan.

RigidBody
03-01-2007, 10:59 PM
this is probably the longest circle-drawing code i've ever seen.

i think this should only be used for very big circles. for small circles i would use zengar's version ;)

Jan
03-02-2007, 01:13 AM
If you implement what Zengar outlined, it will be nearly as long as mine.
Also, i like spaces in my code, makes it more readable.

Jan.

RigidBody
03-02-2007, 02:00 AM
void Circle(float cx, float cy, float r, int steps) {

float phi = 0.0, dphi = 2.*M_PI/(float)steps;

glBegin(GL_LINE_LOOP);

for(int i = 0; i < steps; i ++, phi += dphi) {
glVertex2f(cx+r*cos(phi), cy+r*sin(phi)); }

glEnd(); }half as long as yours, including some spaces (btw you forgot on blank line after glBegin(GL_LINES) :p ).

Jan
03-02-2007, 05:41 AM
Ok, you won. Next time i'm in Munich, i'll invite you to a beer and worship your programming-skills ;-)