Chess Board in OpenGL Source Code

OpenGL Chess Board program deals with drawing of alternative black and white boxes. For this the program uses OpenGL primitive object GL_POLYGON which draws the black and white boxes.

Draw Chess Board in OpenGL

OpenGL Chess Board Project Modules.

There are generally three function namely:

  1. init() which initialises the opengl executable window to its initial values, sets the window’s background color and also the matrix mode of the window and finally the transformation which here is 2D with the help of opengl inbuilt function glOrtho2D().

      glOrtho2D(GLdouble left,GLdouble right,GLdouble top,GLdouble bottom) takes four arguments as shown.            

The first argument decides the left boundary of the executable window, the second argument represent the right boundary of the executable and so does the other two arguments.             

This init() also contains a function named glMatrixMode(Glenum mode) defines the current matrix. The values of mode argument here can be one of the three GL_MODELVIEW, GL_PROJECTION or GL_TEXTURE.             

                           glClearColor(red,green,blue,alpha) this function is used for the window background color. If argument red to set to 1 and all  other to 0 then the window color will be of red. For Example: glClearColor(1,0,0,0); Window color will be red. B.    drawSquare(Glint x1, Glint x2, Glint x3, Glint x4, GLint y1, Glint y2, GLint y3, Glint y4) this function draws the alternative black and white boxes with the help of a conditional statement.            

Here conditional statement (if-else) uses a global variable which defines the color of the box to be set to means should be either black or white.

The color of the box is set with the help of opengl function glColor3f(red, green, blue). For example glColor3f(1,1,1) represents the white colored box where as glColor3f(0,0,0) represent the black colored box. Now we have to draw the box for this box drawing we use openGL primitive GL_POLYGON to draw square box.  The argument received in this function represent the four cordinates of the box.

(x1,y1) represents the top-left corner similarly the (x2,y2) represent the top-right corner and (x3,y3) represent the down-right corner and finally (x4,y4) down-left corner.

These all eight arguments are received by the another function available in the program named chessboard(). The drawSquare() function also uses other opengl function like glBegin(GL_POLYGON), glVertex2i() and glEnd().                         

                           glBegin(Glenum mode) is used when we have to draw any object. Like in this program we sketched polygon so we used GL_POLYGON opengl primitive data type. There are several data types available namely: GL_LINE, GL_POINTS, GL_LINE_STRIP, GL_LINE_LOOP, GL_TRIANGLE, GL_TRIANGLE_STRIP, GL_TRIANGLE_FAN, GL_QUADS, GL_QUAD_STRIP and finally GL_POLYGON.             

These data types are used for making different types of object while usiing them appropriately. glVertex2i(GLint x,GLint y ) this function used in the program is used to put a pixel at the specified two integer argument cordinates x and y.

glEnd() this function is used to end the use of the data type.

For Example:
glBegin(GL_POLYGON);
glVertex2i(x1, y1);
glVertex2i(x2, y2);
glVertex2i(x3, y3);
glVertex2i(x4, y4);
glEnd();

The following short code make a polygon because we use glBegin() function to specify the type of object we are creating.

After deciding the type of object we are making we decide the vertex of the cordinates of the polygon and finally completing the vertices we write glEnd() to make sure that we close the object construction.

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

  1.   main():   This function is common while making all the opengl programms it should made to run the program. Any opengl or any c program cannot be run/debug without using main function although you can hide main function.

First line of the main function contains a glutInit() function which calls a version of glut init() function to initialize the passed arguments.

When any program runs we can pass arguments directly from the console window and program can be made to store these passed arguments into either argc or argv for further use.

Or these function can also be initialized to other variables of the program during the calling made by the glutInit() function.

Next function used in the second line is the  glutInitDisplayMode() this function is used when we use top-level windows, subwindows and many overlays to determine the display mode.

The argument of this function with default is GLUT_RGBA | GLUT_SINGLE these both are the defult if not any of the argument is defined.

Here GLUT_SINGLE defies that we are dealing with only single level window (2D). The other argument used defines the bit masking to select and GLUT_RGBA mode window and also determines the color pattern we are using.

Next function in the main() we are using is glutInitWindowPosition() it takes two arguments distance in x and y respectively. Origin is the Top-Left corner.

When the program is compiled and run then the very second program launch at a position that position is determined by this function.

Next we have the function glutCreateWindow() this function is responsible for creating the launch window with a name on the title bar that name is sent as an argument to this function.

For example if we write glutCreateWindow(“Hello”) the a window will be created with a title bar naming Hello. Next line is call to the function init() this function calls the init body definition.

Next function used in the program is glutDisplayFunc(chessboard) this function is a calling function who call the chessboard display function body. Which further is responsible for sending the graphics to the window.

The final function used in this program is the glutMainLoop() this function works similar to the getch() function in the c program.

Application of the Program:

        The following program can also be implemented in the chess board game or even can also be used in making the floor of the house in various Autocad softwares.

Code Used:


#include<windows.h>
#include<gl\glut.h>
int c = 0;
void init()
{
// For displaying the window color
glClearColor(0, 1, 1, 0);
// Choosing the type of projection
glMatrixMode(GL_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
else
{
glColor3f(0, 0, 0); // black color value is 0 0 0
c = 0;
}

// Draw Square
glBegin(GL_POLYGON);
glVertex2i(x1, y1);
glVertex2i(x2, y2);
glVertex2i(x3, y3);
glVertex2i(x4, y4);
glEnd();
}
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
glFlush();
}

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
init();
// Send graphics to display window
glutDisplayFunc(chessboard);
// Display everything and wait.
glutMainLoop();
}

 

Leave a Reply

Your email address will not be published. Required fields are marked *