Drawing Cricles

Could anyone please help me on how to draw a circle in opengl?

Thanks

You could use the circle equation using the trigonometrical functions, that is, given an angle a, the corresponding circle coordinates would be (rcos a, rsin 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

how would this be coded?
im new to opengl

thanks

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.

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.

zengar,

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

Hey, it was almost half a year ago :slight_smile:

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.

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 :wink:

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.

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) :stuck_out_tongue: ).

Ok, you won. Next time i’m in Munich, i’ll invite you to a beer and worship your programming-skills :wink: