Search Projects

Sunday, August 26, 2018

OpenGL Chess Board in C++ with Source Code

Chess is interesting game with 8X8 checker of black and white. Hence we are going to see a program in OpenGL that implement Chess Board in C++ with free Source Code.

This program implements with the three functions:

1. Init function - this initalise the opengl program.
2. Chess Boards - this calculate the chess and sqaure and draw the same in the screen.
3.  Main function - this is the must and common program for each and every oepngl program.

chessboard() this function is used for the calculation of the end point of the cordinates of the box to make and is also mainly responsible for calling the drawSquare()
It is also a display function which is called from the main function which is responsible for sending graphics to the display window.

The forming of boxes is made through iterating first of all from 1st row and making 1st column then 2nd and then till 8th column as a single chessbord contains 8 column.

The similiar above operation is repeated till all the 8 rows have been iterated making a total 64 boxes (8 rows and 8 column).

The finally the chessboard() function contains a opengl glFlush()  which process all opengl routine as quickly as possible means it flushes all the matter on the display window for the next display.

This similar to our earlier chess board opengl program.


        The program OpenGL Chess Board in C++ can be use in the chess board game as well as in making the floor of the house or any other structure.

Source Code : 

int c = 0;
void init()
// For displaying the window color
glClearColor(0, 1, 1, 0);
// Choosing the type of projection
// for setting the transformation which here is 2D
gluOrtho2D(0, 800, 0,600);

void drawSquare(GLint x1, GLint y1, GLint x2, GLint y2, GLint x3, GLint y3, GLint x4, GLint y4)
// if color is 0 then draw white box and change value of color = 1
if (c == 0)
glColor3f(1, 1, 1); // white color value is 1 1 1
c = 1;
// if color is 1 then draw black box and change value of color = 0
glColor3f(0, 0, 0); // black color value is 0 0 0
c = 0;

// Draw Square
glVertex2i(x1, y1);
glVertex2i(x2, y2);
glVertex2i(x3, y3);
glVertex2i(x4, y4);
void chessboard()
glClear(GL_COLOR_BUFFER_BIT); // Clear display window
GLint x, y;

for (x = 0; x <= 800; x += 100)
for (y = 0; y <= 600; y += 75)
drawSquare(x, y + 75, x + 100, y + 75, x + 100, y, x, y);
// Process all OpenGL routine s as quickly as possible

int main(int agrc, char ** argv)
// Initialize GLUT
glutInit(&agrc, argv);
// Set display mode
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
// Set top - left display window position.
glutInitWindowPosition(100, 100);
// Set display window width and height
glutInitWindowSize(800, 600);
// Create display window with the given title
glutCreateWindow("Chess Board using OpenGL in C++");
// Execute initialization procedure
// Send graphics to display window
// Display everything and wait.

No comments:

Post a Comment