Advertisement

Need help with circles in OpenGL

Started by December 16, 2001 01:32 AM
5 comments, last by TheNewGuy 23 years, 2 months ago
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.
What you mean? Just get a texture of a circle, slap it on a quad, and done.

Advertisement
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.
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.
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.
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);// 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
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
Advertisement
Anon''s idea is the better alternative. i''d do it his way.

This topic is closed to new replies.

Advertisement