The following are the steps for drawing a circle:
- Initialize GLUT, define the size of a top-level window, and create it. Also, set the initial position of the window for displaying our circle.
- Define a callback function that is auto-invoked after creating the window.
- In the callback function, color buffers are cleared and the color for displaying the circle is set.
- The statements for drawing a circle are grouped within a pair of glBegin and glEnd functions along with the GL_LINE_LOOP keyword.
- Use a for loop to draw small lines from 0 to 360 to give the shape of a circle.
The program for drawing a circle is as follows:
//opengldrawshapes2.c
#include <GL/glut.h>
#include<math.h>
#define pi 3.142857
void drawshapes() {
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0f, 1.0f, 0.0f);
glBegin(GL_LINE_LOOP);
for (int i=0; i <360; i++)
{
float angle = i*pi/180;
glVertex2f(cos(angle)*0.5,sin(angle)*0.5); }
glEnd();
glFlush();
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutCreateWindow("Drawing some shapes");
glutInitWindowSize(1500, 1500);
glutInitWindowPosition(0, 0);
glutDisplayFunc(drawshapes);
glutMainLoop();
return 0;
}
Now, let's go behind the scenes to understand the code better.