Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 10 of 10

Thread: How to draw a circle

  1. #1
    Junior Member Newbie
    Join Date
    Mar 2002
    Posts
    9

    How to draw a circle

    Hi,
    Does anyone know how to draw a circle in openGL? I mean a simple circle picture, where i just write circle(x,y,radius)and it'll draw it for me ... I've seen many codes but most of them it's really complicated. I really appreciate it if someone can post the actual code. Thanx's

  2. #2
    Senior Member OpenGL Guru
    Join Date
    Feb 2000
    Location
    Sweden
    Posts
    2,982

    Re: How to draw a circle

    Try this thread.

  3. #3
    Junior Member Newbie
    Join Date
    Mar 2002
    Posts
    4

    Re: How to draw a circle

    Or try NeHe's tutorial, Lesson number 18, which is on Quadratics.
    The web page is: http://nehe.gamedev.net/opengl4.asp

    I used this excellant tutorial and had no problems. hope this helps...

  4. #4
    Junior Member Newbie
    Join Date
    Mar 2002
    Posts
    9

    Re: How to draw a circle

    Hi,
    Thanx's for the help, but i need the code for drawing circle in 2D mode. I've try the thread and checked all the code, but the circle always stuck in the middle of the window. Isn't there any other way to make it dynamic so i can put it anywhere i want? Thank you

  5. #5
    Advanced Member Frequent Contributor
    Join Date
    Apr 2000
    Location
    Adelaide, South Australia, Australia
    Posts
    765

    Re: How to draw a circle

    huh? have you looked at the code to try and understand it and modify it to your own purpose?

    I realise you might not know much/anything about trigonometry (and thus actually computnig the verticies of the circle), but what can't you understand about applying glTranslate calls before drawing the circle?

  6. #6
    Junior Member Newbie
    Join Date
    Mar 2002
    Location
    Memphis, TN, USA
    Posts
    23

    Re: How to draw a circle

    Hellrage,

    Here is what I do to draw a circle in 2d or 3d It takes care of resolution of the circle also. Really nice when added to the ends of cylinders so you can see them on end.

    Public Sub DrawCircle(x1 As GLdouble, y1 As GLdouble, z1 As GLdouble, rad As GLdouble, Nsides As GLint)
    Dim INC As GLdouble
    Dim deginrad As GLdouble
    Dim i As GLint
    glDisable GL_LIGHTING
    glDisable GL_COLOR_MATERIAL
    glEnable GL_LINE_STIPPLE
    glLineStipple SYSLTSCALE, TmpLTPattern
    If Nsides < 3 Then Nsides = 3
    INC = DEG2RAD * (360# / Nsides)
    glTranslatef x1, y1, z1 ' Move to center point
    glBegin GL_LINE_LOOP
    For i = 0 To Nsides - 1
    deginrad = i * INC
    glVertex2f Cos(deginrad) * rad, Sin(deginrad) * rad
    Next i
    glEnd
    glTranslatef -x1, -y1, -z1 'Move back to origin
    End Sub

    This is in VB but you get the idea.
    And for your next question. Here is the code for an Arc.

    Public Sub DrawArc(x1 As GLdouble, y1 As GLdouble, z1 As GLdouble, rad As GLdouble, Nsides As Integer, startang As Double, arcang As Double)
    Dim INC As Double
    Dim deginrad As Double
    Dim i As Integer
    Dim Nsec As Double ' angle of each segment in radians.
    Dim Ssec As Double ' start angle based in radians
    Dim Asides As Integer
    glDisable GL_LIGHTING
    glDisable GL_COLOR_MATERIAL
    glEnable GL_LINE_STIPPLE
    glLineStipple SYSLTSCALE, TmpLTPattern
    Asides = arcang / 360# * Nsides
    If Asides < 3 Then Asides = 3
    glTranslatef x1, y1, z1 ' Move to center point
    glBegin GL_LINES
    INC = DEG2RAD * (arcang / Asides) 'get radian amount for each segment
    Ssec = (startang / arcang) * Asides ' start angle expressed as percent of total arc
    'calculate the start point
    For i = 0 To Asides
    deginrad = (i + Ssec) * INC ' add on the start angle
    glVertex2f Cos(deginrad) * rad, Sin(deginrad) * rad
    deginrad = (i + Ssec + 1) * INC ' add on the next point
    If i < Asides Then
    glVertex2f Cos(deginrad) * rad, Sin(deginrad) * rad
    End If

    Next i
    glEnd
    glTranslatef -x1, -y1, -z1 'Move back to origin

    End Sub

    Hope this helps.

  7. #7
    Senior Member OpenGL Guru
    Join Date
    Jun 2000
    Location
    Gastonia, NC, USA
    Posts
    2,070

    Re: How to draw a circle

    Here is a little routine I am working on, draw diffrent 2D shapes, triangles, squires, etc. Add about 100 points looks like a circle. Builds array of vertex points.
    You could snip the inner code and put the routine between a glBegin/glEnd.

    void buildquad( int sides, Point3F polygon[])
    {
    float angle, xangle;
    float x, y, z;
    float r;
    int i;

    r = 2.0f;
    xangle = 3.1415927f / sides;


    for (i=0; i <= sides; i++)
    {
    angle = xangle * i * 2;
    polygon[i].xyz[0] = r * cos( angle ); // X
    polygon[i].xyz[1] = r * sin( angle ); // Y
    polygon[i].xyz[2] = 0.0f; // Z
    }
    polygon[i] = polygon[0];
    }


    Originally posted by hellrage:
    Hi,
    Does anyone know how to draw a circle in openGL? I mean a simple circle picture, where i just write circle(x,y,radius)and it'll draw it for me ... I've seen many codes but most of them it's really complicated. I really appreciate it if someone can post the actual code. Thanx's

  8. #8
    Junior Member Newbie
    Join Date
    Mar 2002
    Posts
    4

    Re: How to draw a circle

    One way to draw a circle without using sin and cos: (neither very good nor fast and aweful style, but works if you just dont want to work with trig)

    -this will be a circle in the x-y-plane:
    -first you have to move your axes without rotating them to the spot where the center of you circle will be, then:

    Code :
    int points=3600;  // 3600  is the number of points your circle will consist of
    for (int n=0;n<points;i++)
    {
      glRotatef(360*n/points,0.0f,0.0f,1.0f);
      glBegin(GL_POINTS); //some of you might now scream in pain, i know
        glVertex3f(radius,0.0f,0.0f);
      glEnd();
    }

  9. #9
    Junior Member Newbie
    Join Date
    Mar 2002
    Location
    Memphis, TN, USA
    Posts
    23

    Re: How to draw a circle

    hellrage,
    I just found out something.

    On the arc if you use

    glBegin GL_LINE_STRIP

    instead of

    glBegin GL_LINE

    you don't need the second glvertex code.

    Sorry I'm still new at this.

  10. #10
    Junior Member Newbie
    Join Date
    Mar 2002
    Posts
    9

    Re: How to draw a circle

    HI,
    Thanx's everyone for posting the message. I'll try code now, hope it works. See ya

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •