Sandundhammika's Blog











{March 26, 2010}   Draw A circle using GL_LINE_LOOP

hi today I’m going to share my sourcecode with you how to draw a
circle using GL_LINE_LOOP.

This program simply does following.
1. define the PI constant
2. define number of steps that circle should be drawn.
3. divide 2*PI by number of steps and get the value of a one sector angle(angle).
4. within a for loop add another angle to the current angle(angle1).
5. Use the glVertex2d(sine(angle1),cos(angle1)
6. after the for loop come to the glEnd() and flush the buffers using glFlush().

here is the source code for the program.

#define GLUT_DISABLE_ATEXIT_HACK
#include <GL/gl.h>
#include <GL/glut.h>
#include <stdio.h>

#include <math.h>
#define PI 3.1415926535898
GLint circle_points =100;
// This is the draw function.
void draw()
{
	glClear(GL_COLOR_BUFFER_BIT);
	double angle = 2*  PI/circle_points ;
	glBegin(GL_LINE_LOOP);
		double angle1=0.0;
		glVertex2d( cos(0.0) , sin(0.0));
		int i;
		for ( i=0 ; i< circle_points ;i++)
		{
			printf( "angle = %f \n" , angle1);
			glVertex2d(cos(angle1),sin(angle1));
			angle1 += angle ;
		}
	glEnd();
	glFlush();
}



void init()
{
	glClearColor(0.0,0.0,0.0,0.0);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glOrtho(-1.0,1.0,-1.0,1.0,-1.0,1.0);
}

void keyboard (unsigned char key , int x, int y)
{
	exit(0);

}

void main( int argc,char **argv)
{
	glutInit(&argc,argv);
	glutInitDisplayMode(GLUT_SINGLE |GLUT_RGB);
	glutInitWindowSize(250,250);
	glutInitWindowPosition(100,100);
	glutCreateWindow("ch06");
	init();
	glutKeyboardFunc(keyboard);
	glutDisplayFunc(draw);
	glutMainLoop();
}

To compile ,Just execute following commands , note that glut32.dll should be in the folder that
you executing this.

J:\EXERCISES\opengl\glut\ch06>gcc -o ch06.exe ch06.c -lGLUT32 -lOPENGL32

and here’s how this was running.

–Happy Coding–

Advertisements


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

et cetera
%d bloggers like this: