How to Draw Circle in OpenGL

Rajeev
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();

draw circle opengl - using points

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();
}

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();
}


39 comments

  1. soumya sona
    ya sir i got it il just go through it if any modifications il let you know bt i have a another project same like a story as u heard of ant and bird an ant wil be stuckd in river it seeks help and bird helps it by dropping a leaf so in turn of exchange can i have any algorithm project using opengl
  2. soumya sona
    im not getting any algorithm sir
  3. soumya sona
    sir you said you wil try to make changes if i ger thus project so in turn of buying i exchanged it so ca n you stil do any modifications
  4. soumya sona
    of real appearance of animals
  5. Rajeev Kumar Singh
    offcourse i am trying to do that
  6. soumya sona
    ok sir if you modify any real appearance of animals let me know and may i know that hanoi of tower program is been executed successfully
  7. Rajeev Kumar Singh
    Tower of Hanoi Project is working fine here in mysystem so do it will in your's
  8. soumya sona
    rajeev sir i need source codw of algorithm if u have any algorithm suggest me
  9. soumya sona
    rajeev sir i need a source code of any algorithm except lru sstf
  10. Rajeev Kumar Singh
    which algorithm there are many here
  11. soumya sona
    Gudnght our lecturer said he wil give a topic algorithm tomorrow to do a mini project using opengl c++ so i will let you know the topic tomorrow
  12. soumya sona
    hi rajeev sir do have any project based on freedom fighters like image of chitradurga fort and onaki obavva hits soldiers who come way hidden in forts of rocks and can say which project you have based on algorithm of os and any other as i am not gettng any idea about that rply me as soon you can get the message
  13. Anoosha
    In visual c++ it is not giving output. its showing as 0 succeeded, 0 failed, 1 up-to-date,
    how to get a output?
  14. VISHAL BINIT
    sir the speed of transfer of rings from 1 to 3 is very high...how to reduce the transfer speed
  15. soumya sona
    sir rajeev sir kindly help me in mini project on algorithm
  16. soumya sona
    can i have any 3d project
  17. soumya sona
    k fine i have shooting of objects in exchange can i have any 3d mini project using opengl
  18. Rajeev Kumar Singh
    there are many algorithm projects in here


    why you not trying them or just mention one
  19. soumya sona
    can i have any 3d a wonderfull project
  20. Rajeev Kumar Singh
    first decide exactly what you want?
  21. soumya sona
    i need one amazing worthy 3d mini project based on some tag line
  22. Rajeev Kumar Singh
    do you have any idea as i have not much time
  23. soumya sona
    hi rajeev sir actually i don hav any idea on 3d some of my friend doing 3d home with furnitured so if you have any3d projects let me knw
  24. soumya sona
    hi rajeev sir am trying out from long days and texting you that can i have one wonderful and interesting cg project using c++ opengl if its cost also i have a some other projects can exchange it as i did it with hare and tortoise project with lru page replacement algorithm sourcecode
  25. soumya sona
    if it is 3d also no problem
  26. soumya sona
    rajeev sir rply to my message let me know
  27. Rajeev Kumar Singh
    i am a bit busy with my work so not able to concentrate on the things........
  28. soumya sona
    k sir fine but if you're free send me one interesting project if it cost i have source code of some wil exchange
  29. Addy Kolkar
    the movement of the rings is too fast how to slow it
  30. Nikhil Depolkar
    Sir plz send me code link...available link is not working
  31. Nikhil Depolkar
    Depolkarnikhil@gmail.com
  32. Sinchana
    Code link not working.Plz help Sir.tylorabcd@gmail.com
  33. soumya sona
    sir is this hanoi project of using opengl functions
  34. OpenGL Projects
    I have sent to you the mail with source cod links
  35. OpenGL Projects
    check your mail from openglprojects@gmail.com it has link for the hare tortoise projects
  36. OpenGL Projects
    Get free project in exchange of any one that not published here
  37. soumya sona
    sir i asked you for a algorithm sourcecode
  38. akshata
    Pls can I get source code for tower of Hanoi pls pls
  39. Preethu Gowda
    source code for hare and the tortoise story

Join the conversation