algorithm for creating a circle.

Started by
4 comments, last by titanandrews 17 years, 9 months ago
Hi All, I am new to OpenGL and 3D graphics. I am trying to create a circle. I see lots of examples all over the internet for how to do this, but for some mysterious reason it just doesn't work for me. GLint circle_points = 100; glBegin(GL_LINE_LOOP); for (int i = 0; i < circle_points; i++) { float angle = TWO_PI*i/circle_points; glVertex2f(cos(angle), sin(angle)); } glEnd(); This is code I got directly from the OpenGL redbook, but it only draws a straight line on my screen. TWO_PI is defined as 2*M_PI. I just don't get it. I have tried several different algorithms, but I can't get any of them to work. What clues should I look for to solve this problem? many many thanks! Barry
Advertisement
Quote:Original post by titanandrews
float angle = TWO_PI*i/circle_points;

And here's your problem. i / circle_points is an operation of two integer, resulting in rounding down to zero, due to the fact that i < circle_points.
Make that float angle = TWO_PI* (i + 0.0f)/(circle_points + 0.0f); and it should work fine. The addition promotes both variables to float (just like a cast) and thus avoids type-releated errors.

Cheers,
Pat.
Did you really get that code DIRECTLY from the Red Book? You had to define i as an int to get it to compile, didn't you? The code in the Red Book doesn't say what i is actually, assuming we're taling about the same code. You assumed it was an int, but the book doesn't say [rolleyes]

The problem is if both i and circle_points are integers, then i/circle_points means integer division, which is not what you want. You should cast at least one of the values to float before dividing if you keep i as an integer.

edit: Too slow [razz]
Thanks for the replies!
Yes, I assumed it was an int ( without thinking of course ). So I changed it to a float, but I still get the same problem, a straight line in the middle of the screen. I do a cout just to make sure there is no weird rounding problem going on. What else can I check? This really has me stumped.


GLint circle_points = 100;
glBegin(GL_LINE_LOOP);
for (float i = 0; i < circle_points; i++)
{
float angle = TWO_PI*i/circle_points;
char buffer[20];
sprintf(buffer,"%f",angle);
cout << buffer << endl;
glVertex2f(cos(angle), sin(angle));
}
glEnd();
Mh. Well, here's a complete demo program that works as-is:
#include <GL/glut.h>#include <cmath>namespace {// display function that renders the scene.void display() {    // clear colour buffer (default colour is black on my machine)    glClear( GL_COLOR_BUFFER_BIT );    // set the circle colour to be white    glColor3f( 1.0f, 1.0f, 1.0f );    // make a smooth circle using 100 steps    int const steps = 100;    // instead of re-computing the angle each iteration,    // I just use the delta value    float const angleDelta = 3.141592654f * 2 / steps;    float angle = 0.0f;    // begin the loop    glBegin(GL_LINE_LOOP);    // iterate over the line segments and update the angle    for ( int i = 0; i < steps; ++i, angle += angleDelta ) {        // use the values as-is - the circle could be made smaller by        // multiplying x and y by some radius r        glVertex2f( cos(angle), sin(angle) );    }    glEnd();    // I use double-bufering, so present the rendered scene by     // swapping the buffers    glutSwapBuffers();}}int main(int argc, char * argv[]) {    // setup the display mode to use double buffering and an RGB colour buffer    glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGB );        glutInitWindowSize( 512, 512 );    glutCreateWindow( "circle" );    glutDisplayFunc( display );    // setup complete - start the rendering loop    glutMainLoop();}

Image Hosted by ImageShack.us

HTH,
Pat
Ah Ha!! I figured out my problem with the code you posted and a little trial and error. Thanks! I was making some calls like this,

gluPerspective(52.0f,(GLfloat)width/(GLfloat)height,1.0f,1000.0f);

and

gluLookAt(0.0, 10.0, 0.1, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);

These were left over from Chapter 3 examples in "Beginning OpenGL". It is unclear what these functions are actually doing. The book has not explained them yet, but just by commenting them, I can draw the circle. I did read the API docs for these functions, and I guess they will make more sense to me later. For now the light bulb is still off for me.

thanks guys!


This topic is closed to new replies.

Advertisement