PDA

View Full Version : Drawing Circles in OpenGL

newToOpenGL
10-10-2004, 10:13 PM
Hi,

Could someone please tell me how to draw circles in OpenGL? A simple example would be greately appreciated.

bulldogs
10-18-2004, 04:22 PM
have a look here http://www.opengl.org/documentation/red_book_1.0/

Chapter 2 i think

DSB
10-25-2004, 11:02 PM
Here is an example showing how to use OpenGL and GLUT to draw a circle.

If you are not familiar with or do not have GLUT you can obtain it at this address: http://www.xmission.com/~nate/glut.html

DSB

#include <windows.h>
#include <math.h>
#include <glut.h>

#define PI 3.1415926535897932384626433832795

#define WIDTH 640
#define HEIGHT 480

void init();
void reshape(int, int);
void display();

//Specifies the radius of the circle

int main(int argc, char **argv)
{
glutInit(&amp;argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
glutInitWindowSize(WIDTH, HEIGHT);
glutInitWindowPosition(50, 50);
glutCreateWindow("OpenGL - Draw Circle");
init();
glutReshapeFunc(reshape);
glutDisplayFunc(display);
glutMainLoop();

return 0;
}

void init()
{
//Tells OpenGL to check which objects are in front of other objects
//Otherwise OpenGL would draw the last object in front regardless
//of it's position in Z space
//Note: It is not necessary to enable this for a simple 2D circle
//but is good practice
glEnable(GL_DEPTH_TEST);

//Tells OpenGL not to draw backfaces
//Backfaces are defined by vertex drawing order
//By default couter-clockwise drawing order specifies front faces
//Note: The circle is drawn counter-clockwise
//Note: It is not necessary to enable this for a simple 2D circle
//but is good practice
glCullFace(GL_BACK);
glEnable(GL_CULL_FACE);

glMatrixMode(GL_PROJECTION);
gluPerspective(45.0f, (GLfloat)WIDTH / (GLfloat)HEIGHT, 0.1f, 100000.0f);
glMatrixMode(GL_MODELVIEW);

//Assign the clear screen color
//Format (Red, Green, Blue, Alpha)
//Values should remain normalized between 0 and 1
glClearColor(1.0, 1.0, 1.0, 0.0);
}

void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei)WIDTH, (GLsizei)HEIGHT);
glMatrixMode(GL_PROJECTION);
gluPerspective(45.0f, (GLfloat)WIDTH / (GLfloat)HEIGHT, 0.1f, 100000.0f);
glMatrixMode(GL_MODELVIEW);
}

void display()
{
//Clear the screen
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glPushMatrix();

//Tells the camera where to be and where to look
//Format (camera position x,y,z, focal point x,y,z, camera orientation x,y,z)
//Remember that by default the camera points toward negative Z
gluLookAt(0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);

glPushMatrix();

//Set Drawing Color - Will Remain this color until otherwise specified
glColor3f(0.2, 0.3, 0.5); //Some type of blue

//Draw Circle
glBegin(GL_POLYGON);
//Change the 6 to 12 to increase the steps (number of drawn points) for a smoother circle
//Note that anything above 24 will have little affect on the circles appearance
//Play with the numbers till you find the result you are looking for
//Value 1.5 - Draws Triangle
//Value 2 - Draws Square
//Value 3 - Draws Hexagon
//Value 4 - Draws Octagon
//Value 5 - Draws Decagon
//Notice the correlation between the value and the number of sides
//The number of sides is always twice the value given this range
for(double i = 0; i < 2 * PI; i += PI / 6) //<-- Change this Value
glEnd();
//Draw Circle

glPopMatrix();

glPopMatrix();

glFlush();
glutSwapBuffers();
}

Sumedh
10-28-2004, 01:21 AM
Thats a good code for starters, but note that it will give you jagged edges when the circle is bigger. I mean there will be an inconsistancy with the circles drawn as the radius varies.
So here's a tweak.
Firstly, there is nothing like a cicle in Discreet Graphics. Its always a polygon. The smaller the edges of the polygon, the smoother its going to look.
Hence for uniform smoothness, if the radius increases, increase the edges of polygon. Or in other words, you can fix the length of edges to be very small & accordingly calculate the number of edges required to render the polygon. For eg if you fix edge length Len = 0.01 units then
No_Edges = (2* PI * rad) / Len;
Then you already have DSB's solution that works good.