#### Archived

This topic is now archived and is closed to further replies.

# Need help with circles in OpenGL

This topic is 6210 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Can anyone help show me how to create 2D circles with either quads or triangles in 3D space with C++? If you can, I''d appreciate it.

##### Share on other sites
What you mean? Just get a texture of a circle, slap it on a quad, and done.

##### Share on other sites
I don''t mean creating a circle on a square surface.
I''m trying to make a circular lid to put on a 3D cylinder, and I''m to build a circle out of triangles or quad polygons.

##### Share on other sites
If you can make the sides, why can''t you make the cap?

You could use a triangle fan and do something like:

HEIGHT = cap height in Z
SLICES = number of sections to divide cap into

float angle, section;

glBegin(GL_TRIANGLE_FAN);
glVertex3f(0.0, 0.0, HEIGHT);

for (section=0;section{
angle = 2*M_PI*((SLICES-1)/section);
}
glEnd();

Be advised: I just typed this in off the top of my head, so it may be anywhere for perfect to totally wrong.

##### Share on other sites
You can do it with simple high school trig. As you increment some angle theta from 0 to 360 degrees (or 0 to 2PI if radians) calculate X, Y coordinates as...

X = radius * cosine (theta)
Y = radius * sine (theta)

That gives you the values you are looking for. You can apply the coordinates to triangles or quads in a way that makes sense for your program.

##### Share on other sites
the easiest way (I think) would be to use GLUT.
*Reaches for OpenGL Game Programming Book*
you could do something like this:

  GLUquadricObj* myCircle;myCircle = gluNewQuadric();gluQuadricDrawStyle(myCircle, GLU_FILL);gluQuadricNormals(myCircle, GLU_FLAT);// orgluQuadricNormals(myCircle, GLU_SMOOTH);gluQuadricOrientation(myCircle, GLU_OUTSIDE);// orgluQuadricOrientation(myCircle, GLU_OUTSIDE);gluQuadricTexture(myCircle, GL_TRUE);gluDisk(myCircle, innerRadius, outerRadius, slices, loops);

Basically you create a GLUquadricObj and initialise it with gluNewQuadric(). Then set the draw style to flat (other options are GLU_LINES and GLU_POINT). Next you set the kind of shading, only use one of the above. Set the orientation, now that is basically the way it faces so try both to see which works best. finally gluQuadricTexture enables or disables texture co-ordinate generation and gluDisk creates the disk with these parameters:

myCircle:- the GLUquadricObj you spent ages setting up
innerRadius:- the disk has a hole, this is it's radius. Set 0 for no hole
slices:- the number of "slices", like a cake, it is divided into.
loops:- the number of rings that the disk is divided into

There you go. I hope that u don't mind using GLUT because I spent ages typing this reply!

------------------------------
Baldur K

"It will be happened; it shall be going to be happening; it will be was an event that could will have been taken place in the future."
--time travel explained by Rimmer (Red Dwarf)

"If you don't gosub a program loop, you'll never get a subroutine."
-- Kryten (Red Dwarf)

Edited by - baldurk on December 16, 2001 4:42:35 AM

##### Share on other sites
Anon''s idea is the better alternative. i''d do it his way.

1. 1
2. 2
Rutin
21
3. 3
A4L
15
4. 4
5. 5
khawk
14

• 13
• 26
• 10
• 11
• 44
• ### Forum Statistics

• Total Topics
633742
• Total Posts
3013639
×