Draw a chess board using open_gl & free glut

Free glut is an open source free library which is used for graphics and game designing.You can draw the chess board using free glut. Code is below.

Step_1:

include open gl library but prior configure open gl with your  compiler

#include <GL\freeglut.h> 


Step_2:

write function for initial conditions

void init(void)
{
glClearColor(0.0, 0.5, 1.0, 0.0); // Set display-window color to white.
glMatrixMode(GL_PROJECTION); // Set projection parameters.
gluOrtho2D(0.0, 600.0, 0.0, 600.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}


step_3:

write code of things you want to draw in a separate function.

void Draw()
{
int x1=20,y1=20,x3=70,y3=70;
glClear(GL_COLOR_BUFFER_BIT); // Clear display window.
int change=1;
for(int i=0;i<8;i++)
{
for(int j=0;j<8;j++)
{
change=change*(-1);

if(change==-1)
glColor3f(1.0, 1.0, 1.0);
else
glColor3f(0.0, 0.0, 0.0);
x1+=50;x3+=50;
glRecti(x1,y1,x3,y3);
}
y1+=50;y3+=50;x1=20;x3=70;
change=change*(-1);//un comment in case of even no of boxes in a ro
}

glFlush();
}


step_4:

finally go for main function.

void main(int argc, char** argv)
{
glutInit(&argc, argv); // Initialize GLUT.
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); // Set display mode.
glutInitWindowPosition(50, 50); // Set top-left display-window position.
glutInitWindowSize(700, 700); // Set display-window width and height.
glutCreateWindow(“chess board”); // Create display window.
init();
glutDisplayFunc(Draw);
glutMainLoop();

}


Download source code

DOWNLOAD MADE

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