Archived

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

TheNewGuy

Need help with circles in OpenGL

Recommended Posts

Guest Anonymous Poster   
Guest Anonymous Poster
What you mean? Just get a texture of a circle, slap it on a quad, and done.

Share this post


Link to post
Share on other sites
TheNewGuy    122
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 this post


Link to post
Share on other sites
miles vignol    122
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
RADIUS = cylinder radius
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);
glVertex3f(cos(angle)*RADIUS, sin(angle)*RADIUS, HEIGHT);
}
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 this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
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 this post


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

[edit: source instead of code]

  
GLUquadricObj* myCircle;

myCircle = gluNewQuadric();

gluQuadricDrawStyle(myCircle, GLU_FILL);

gluQuadricNormals(myCircle, GLU_FLAT);
// or

gluQuadricNormals(myCircle, GLU_SMOOTH);

gluQuadricOrientation(myCircle, GLU_OUTSIDE);
// or

gluQuadricOrientation(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
outerRadius:- the disk's radius
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 this post


Link to post
Share on other sites