Click here to Skip to main content
15,886,014 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I'm having a bit of trouble making a good, nice, round, filled 2D circle in OpenGL. I use GNU C compiler, CodeBlocks IDE and Windows XP. The following code produces an ellipse that looks more like a "tomato" in the sense that it is wide for how tall it is. So the problem is, how to "thin it out" in the middle to make it more round like an actual circle.

C#
//filled circle
float x1,y1,x2,y2;
float angle;
double radius=0.1;

x1 = 0.5,y1=0.6;
glColor3f(1.0,1.0,0.6);

glBegin(GL_TRIANGLE_FAN);
glVertex2f(x1,y1);

for (angle=1.0f;angle<361.0f;angle+=0.2)
{
    x2 = x1+sin(angle)*radius;
    y2 = y1+cos(angle)*radius;
    glVertex2f(x2,y2);
}

glEnd();
Posted
Updated 11-Mar-10 4:41am
v2

Well it occured to me this morning to change the output window to a 1:1 square shape, like 600x600 and once I did, everything was perfect, so I guess perfection has been reached, haha. Nonetheless, feel free to run something past me about gluOrtho2d and gluPerspective, because the auto-code from my IDE/compiler (codeblocks/gnu) doesn't use those. At any rate, it sounds like those are useful for 4:3 and whatnot, and thanks again for your time, Saurabh.-B
 
Share this answer
 
v3
Yes then it is definitely about aspect ratio. Which function are you using for initialize camera - gluOrtho2D or gluPerspective? You need to match the parameters to these functions with the aspect ratio of the window.

-Saurabh
 
Share this answer
 
sin and cos expects angle to be in radians and not in degrees.

bala_48225 wrote:
for (angle=1.0f;angle<361.0f;angle+=0.2)


Try:
for(angle=0.0f ; angle<2*3.14159 ; angle+=0.2)

-Saurabh
 
Share this answer
 
v2
Well, I bought the book Beginning OpenGL Game Programming, 2nd edition from Borders and used the first example project (after I finally got it to compile and everything), and then took their drawing code out and put mine in and modified things like gluLookat() bit by bit and sure enough, I can now make a 800x600 or 1024x768 screen with a nice, round planet. The auto-code from the CodeBlocks/Mingw setup was very simple and designed to set up a small 1:1 window and then do a rotating triangle, while the example from the book used all kinds of different functions like gluLookat() and gluPerspective and who knows what else. So the moral of the story for me is, buy a book. So until the next time I remain humbly yours, good people of Code Project. -B
 
Share this answer
 
If it's a filled circle:

glEnable(GL_POINT_SMOOTH);
glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);
glPointSize(radius);
glPoint(x, y, z);
 
Share this answer
 
v2
I believe this gon help more in great way with OOP concept

C++
class circle{
public : float x,y,rot;

public:
void createcircle (int k, int r, int h); 

};
void circle::createcircle (int k, int r, int h) {
  glBegin(GL_TRIANGLE_FAN);
    for (int i = 0; i < 180; i++)
    {
    x = r * cos(i) - h;
    y = r * sin(i) + k;
    glVertex3f(x + k,y - h,0);
    
    x = r * cos(i + 0.1) - h;
    y = r * sin(i + 0.1) + k;
    glVertex3f(x + k,y - h,0);
    }
    glEnd();
}

- See more at: Draw Circle in OpenGL
 
Share this answer
 
Hello Saurabh, the same thing happened, though the "circle" wasn't complete this time so I "overkilled" it like this:

for (angle=0.0f;angle<3*3.14159;angle+=0.2)

by putting a 3 instead of 2 in "angle < 2*3.14159" as you see to finish it up.

At any rate, it's still a tomato. I personally think it has something to do with the aspectratio of the setup, because if I do this:

x1=-0.1f, y1=-0.1f,x2=0.1f,y2=0.1f;

glBegin(GL_QUADS);

glVertex2f(x1,y1);
glVertex2f(x2,y1);
glVertex2f(x2,y2);
glVertex2f(x1,y2);

glEnd();

it's a rectangle instead of a square. Perhaps you or someone else knows how to fiddle with some setup variables and thanks at any rate for giving it a shot. By the way, how do you indent on here? I put some spaces to indent, but it comes out flat.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900