How to 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.

Things You'll Need

  • Computer with OpenGL support
  • Text editor and C language compiling tools or C development environment
Show More

Instructions

    • 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 <math.h> 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.

    • 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.

Related Searches

References

Resources

You May Also Like

  • What Is an OpenGL Graphics Card?

    An OpenGL Graphics Card is one that supports the OpenGL API specification. This specification, which is maintained by representatives from prominent companies...

  • How to Start Up a New 3D Max in OpenGL or Software

    Some graphics cards perform better using the OpenGL standard within 3D Max, while others work more smoothly with Direct3D. Sometimes a card...

  • How to Draw Texture

    Texture in drawing involves creating the sense of a certain type of surface on an image, for example, fur on a cat....

  • How to Draw a Circle

    Learning how to draw a circle is a very important artistic skill. Many objects are made up of circles, including wheels, rings,...

  • Drawing and Rendering Techniques

    If you want to draw a portrait or still life, or render a perspective of a building, for example, there are a...

  • How to Install OpenGL on Windows XP

    Compatible with Windows, Linux and Mac computers, OpenGL or Open Graphics Library provides developers with a cross-platform solution for 3D graphics programming...

  • How to Draw People Walking and Standing

    Two of the most common poses for people are standing around doing nothing or walking away from something that has to be...

  • How to Render Hair in Photoshop

    Photoshop is a design program used by graphic designers to distort and modify photographs as well as produce advertising materials and images....

  • OpenGL & Visual Basic Tutorial

    Microsoft Visual Basic (VB) is a computer programming language used to create web, mobile and desktop applications. OpenGL is an application programming...

Related Ads

Featured
View Mobile Site