View Full Version : How to draw a circle

hellrage

03-27-2002, 01:55 AM

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

Try this (http://www.opengl.org/discussion_boards/ubb/Forum2/HTML/007879.html) thread.

Dark_Streak

03-27-2002, 07:00 AM

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

hellrage

03-27-2002, 08:40 PM

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

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?

jbarton

03-28-2002, 06:25 AM

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.

nexusone

03-28-2002, 06:39 AM

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

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:

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

}

http://www.opengl.org/discussion_boards/ubb/wink.gif

jbarton

03-28-2002, 12:07 PM

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. http://www.opengl.org/discussion_boards/ubb/redface.gif

hellrage

03-28-2002, 01:06 PM

HI,

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

Powered by vBulletin® Version 4.2.2 Copyright © 2014 vBulletin Solutions, Inc. All rights reserved.