|
-
Jul 2nd, 2006, 01:17 AM
#1
Thread Starter
Lively Member
How to draw an approximate circle in OpenGL?
Hi,
I am doing an exercise from the book "Beginning OpenGL Game Programming" by Dave Astle and Kevin Hawkins.
The question asked to write a function to draw a 2D circle approximation. The origin starts at 0,0,0. The function has to accept the radius and the number of edges.
Below is how I wrote the function. But when I ran it, there's only a straight white line appearing across the window, instead of an approximate circle.
void DrawCircleApproximation(float radius, int numberOfSides, bool edgeOnly)
{
int i;
double angle;
if(edgeOnly)
glBegin(GL_LINE_STRIP);
else
glBegin(GL_POLYGON);
for(i = 0;i < numberOfSides;i++)
{
angle = (i * 2 * 3.142) / numberOfSides;
glVertex3f(cosf(angle)*radius,0.0, sinf(angle)*radius);
}
if(edgeOnly)
glVertex3f(radius,0.0,0.0);
glEnd();
}
What is wrong with the code above? I've checked with the book's answer and they are similar.
-
Jul 2nd, 2006, 05:06 PM
#2
Lively Member
Re: How to draw an approximate circle in OpenGL?
Code:
glVertex3f(cosf(angle)*radius,0.0, sinf(angle)*radius);
That line is drawing all the points into the screen (on the XZ plane). Try this:
Code:
glVertex3f(cosf(angle)*radius, sinf(angle)*radius, 0.0);
Last edited by fartman_900; Jul 2nd, 2006 at 10:02 PM.
-
Jul 2nd, 2006, 10:25 PM
#3
Thread Starter
Lively Member
Re: How to draw an approximate circle in OpenGL?
Hi,
I used glVertex3f(cosf(angle)*radius,sinf(angle)*radius,0.0); but nothing appears on the openGL window now.
I also tried casting the integer variables to floats and the same thing happened.
I've also tried using glVertex2f instead of 3f but the problem remained the same. The windows displayed nothing.
Below is how I called the DrawCircleApproximationFunction.
int DrawGLScene(GLvoid) // Here's Where We Do All The Drawing
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity(); DrawCircleApproximation(5,5,false); //calling the function
return TRUE;
}
The first argument of the function is the radius. The second argument is the number of sides while the third argument determines whether to use GL_POLYGONS or GL_LINE_STRIP.
Could the problem lie with the calling function?
-
Jul 2nd, 2006, 11:48 PM
#4
Addicted Member
Re: How to draw an approximate circle in OpenGL?
You said that you have the answer from the book. How does the book suggest that you do it ? What is the difference between your code and their code ?
Keith_VB6
If you have any further questions, just ask.
If this solves things, then please mark the thread resolved.
[Thread Tools] --> [Mark Thread Resolved]
-
Jul 3rd, 2006, 03:54 PM
#5
Lively Member
Re: How to draw an approximate circle in OpenGL?
I found the problem. I forgot to change the Z value of the glVertex3f function. So it was drawing it more towards the viewer instead of further away. I just tried it on my computer and it works good with z=-20.0f. So it should be
Code:
glVertex3f(cosf(angle)*radius, sinf(angle)*radius, -20.0f);
...
if(edgeOnly)
glVertex3f(radius,0.0,-20.0f);
You need to have that last vertex with the same z value as the others, or it messes up. I tried it and it works fine.
-
Jul 10th, 2006, 04:24 AM
#6
Thread Starter
Lively Member
Re: How to draw an approximate circle in OpenGL?
Hi,i've tried out the suggestion and it works. Thanks!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|