In the OpenGL Computer graphics there major problem in drawing circle. In this post we are going to study How to Draw Circle in OpenGL. There is no primitive functions like of lines, points, triangles and other polygons for circle, hence drawing a circle need some new method/function from our side. we are going to learn that today.
There are many methods of Drawing circle in OpenGL, we going to discuss one by one each of them.
First Method - using the indefinite points
This method used the no of of point to draw a circle. The problem with this method is you need more precession as we use the sine and cosine function with the value of pie as 3.14159, So little error may in the round circle. Put the following code in a simple display function and run the program for your circle. See the Image it's output -
glBegin(GL_POINTS);
for (int i = 0; i < 1000; ++i)
{
glVertex3f(cos(2 * 3.14159 * i / 1000.0), sin(2 * 3.14159 * i / 1000.0), 0);
}
glEnd();
Second Method - using the lines
As Like above program circles can be created using the small sets of lines. We used the bunch of lines, which are closed together to go round forming a circle. For drawing circle in opengl, this method also used the simple sine and cosine maths functions, but we have not used pie instead go for degree for angles.
typedef struct
{
float x;
float y;
}CIRCLE;
CIRCLE circle;
void createcircle (int k, int r, int h) {
glBegin(GL_LINES);
for (int i = 0; i < 180; i++)
{
circle.x = r * cos(i) - h;
circle.y = r * sin(i) + k;
glVertex3f(circle.x + k,circle.y - h,0);
circle.x = r * cos(i + 0.1) - h;
circle.y = r * sin(i + 0.1) + k;
glVertex3f(circle.x + k,circle.y - h,0);
}
glEnd();
}
C++ object oriented approach
If you know about the structures in C then fine you understand the typedef else we have more object oriented approach making use of classes for C++. Same program above in classes.
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_LINES);
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();
}
If you're using modern OpenGL with C++, the usual way to draw a circle is to approximate it with a polygon by generating points around its circumference.
Example using GL_TRIANGLE_FAN
#include
#include
void drawCircle(float cx, float cy, float radius)
{
const int segments = 100;
glBegin(GL_TRIANGLE_FAN);
// Center of the circle
glVertex2f(cx, cy);
// Points around the circumference
for (int i = 0; i <= segments; i++)
{
float angle = 2.0f * 3.1415926f * i / segments;
float x = cx + radius * cos(angle);
float y = cy + radius * sin(angle);
glVertex2f(x, y);
}
glEnd();
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0f, 0.5f, 1.0f); // Blue
drawCircle(0.0f, 0.0f, 0.5f);
glFlush();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(800, 600);
glutCreateWindow("Circle in OpenGL");
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
How it works
The circle is described mathematically by:
x=cx+rcos(θ)y=cy+rsin(θ)where:
cx,
cy
→ center of the circleradius → radius
θ → angle from 0 to 2π
With 100 segments, the individual points form a shape that looks like a smooth circle.
Example using GL_LINE_LOOP
To have only the outline rather than a filled circle, use GL_LINE_LOOP. Replace the GL_TRIANGLE_FAN in above code with mentioed below
void drawCircleOutline(float cx, float cy, float radius)
{
const int segments = 100;
glBegin(GL_LINE_LOOP);
for (int i = 0; i < segments; i++)
{
float angle = 2.0f * 3.1415926f * i / segments;
float x = cx + radius * cos(angle);
float y = cy + radius * sin(angle);
glVertex2f(x, y);
}
glEnd();
}