Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C Windows OpenGL
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.

//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 10-Mar-10 14:46pm
Edited 11-Mar-10 5:41am
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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
  Permalink  
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

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
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 6

If it's a filled circle:
 
glEnable(GL_POINT_SMOOTH);
glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);
glPointSize(radius);
glPoint(x, y, z);
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 7

I believe this gon help more in great way with OOP concept
 
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
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  
v2

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



Advertise | Privacy | Mobile
Web03 | 2.8.1411022.1 | Last Updated 25 Mar 2014
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100