Render A Circle In Opengl

OpenGL, or Open Graphics Library, is a computer graphics API (application programming interface) for writing 2D and 3D computer graphics applications across programming languages and platforms. It can be used to render anything from simple, primitive objects to complex 3D scenes. OpenGL is widely used in professional computer-assisted design, scientific visualization, flight simulation and video games. While OpenGL does not have any methods to directly draw a circle, a circle can be constructed using a series of lines. You will need a basic knowledge of OpenGL to draw a circle.


1. Set up a basic program that displays an OpenGL viewport window. If you are unfamiliar with this process, there is a tutorial on the basics of OpenGL.

2. Add the math.h header file to your program by entering the following code at the beginning of the program: #include This will provide you with the cos() and sin() functions (cosine and sine) needed to calculate the circle.

3. Begin your circle method by adding the following code before your display callback function: void drawCircle(GLint x, GLint y, GLint radius, int num_lines) { } The variables “x” and “y” will be used to set the center of the circle in your window, while “radius”, of course, sets radius of the circle. The integer “num_lines” is the number of lines used to compose the circle. The higher the number, the smoother the circle will appear to be.

4. Add the following set-up code inside the brackets: float angle GO int num_lines GO glColor3f(0.0, 0.0, 0.0) GO The variable “angle” will contain the angle of the lines used to construct the circle. The call to the glColor3f() function sets the color of the lines to black, where the series of three 0s correspond to the intensity of red, green, and blue on a scale of 0 to 1.

READ  Radeon 7000 Specs

5. Add the following code below the glColor3f() call from Step 4: glBegin(GL_LINE_LOOP) GO This tells OpenGL that any following lines involve drawing lines. Specifically, this draws a “GL_LINE_LOOP,” which draws a series of connected lines.

6. Enter the following for-loop code after the glBegin() call: for(int i = 0; i < num_lines; i++) { angle = i * 2 * M_PI / num_lines; glVertex2f(x + (cos(angle) * radius), y + (sin(angle) * radius)); } This loop accomplishes three things. First, it iterates num_lines times, where num_lines is an integer, drawing a line in each iteration and resulting in a circle composed of num_lines straight lines. Second, it computes the angle of the lines relative to the center of the circle in radians. "M_PI" is a constant variable defined in math.h representing pi. Finally, the glVertex2f() call calculates the (x, y) coordinates of the endpoints of the lines.

7. After the for-loop, enter the following line of code: glEnd() GO This tells OpenGL you are done drawing lines.

8. To draw the circle in your scene, add a call to the drawCircle() method to your program’s display callback function, which you should have prepared in Step 1. Specify values for x, y, radius, and num_lines, where x and y represents a point within your scene, radius is a the radius of the circle, and num_lines is the number of lines making up the circle. Again, if you are unfamiliar with OpenGL and the display callback, refer to the OpenGL tutorial.

9. Compile and run your program. If you have entered the function correctly, it should compile without error. If you have specified valid values for x, y and radius, you should see a black circle displayed on a white background.

READ  Types Of Computer Animation