Search Projects

Tuesday, February 19, 2013

Chess Board in OpenGL Computer graphics

Chess board consist of the Black and White boxes with each follows other alternatively. Let build a chess board with computer graphics using OpenGL platform.



First draw the black and the white strips for the board with the  draw_BlackArea()  and draw_whiteArea() functions. In the functions GL_QUADS is used to form the rectangular shape, while rest is easily understood in the following code. The 6 blocks of GL_QUADS is used as for each faces in the cube.


void draw_BlackArea()
{

glBegin(GL_QUADS);
glColor3f(0.05f,0.05f,0.05f);
glVertex3f(0.0f,0.0f,0.0f);
glVertex3f(1.50f,0.0f,0.0f);
glVertex3f(1.5f,0.3f,0.0f);
glVertex3f(0.0f,0.3f,0.0f);
glEnd();


glBegin(GL_QUADS);
glColor3f(0.05f,0.05f,0.05f);
glVertex3f(0.0f,0.0f,0.0f);
glVertex3f(0.0f,0.0f,-1.5f);
glVertex3f(0.0f,0.3f,-1.5f);
glVertex3f(0.0f,0.3f,0.0f);
glEnd();


glBegin(GL_QUADS);
glColor3f(0.05f,0.05f,0.05f);
glVertex3f(1.5f,0.0f,0.0f);
glVertex3f(1.5f,0.0f,-1.5f);
glVertex3f(1.5f,0.3f,-1.5f);
glVertex3f(1.5f,0.3f,0.0f);
glEnd();

glBegin(GL_QUADS);
glColor3f(0.05f,0.05f,0.05f);
glVertex3f(0.0f,0.0f,-1.5f);
glVertex3f(1.50f,0.0f,-1.5f);
glVertex3f(1.5f,0.3f,-1.5f);
glVertex3f(0.0f,0.3f,-1.5f);
glEnd();


glBegin(GL_QUADS);
glColor3f(0.05f,0.05f,0.05f);
glVertex3f(0.0f,0.0f,0.0f);
glVertex3f(1.50f,0.0f,0.0f);
glVertex3f(1.5f,0.0f,-1.5f);
glVertex3f(0.0f,0.0f,-1.5f);
glEnd();


glBegin(GL_QUADS);
glColor3f(0.0f,0.0f,0.0f);
glVertex3f(0.0f,0.3f,0.0f);
glVertex3f(1.50f,0.3f,0.0f);
glVertex3f(1.5f,0.3f,-1.5f);
glVertex3f(0.0f,0.3f,-1.5f);
glEnd();

}
void draw_whiteArea()
{

glBegin(GL_QUADS);
glColor3f(0.05f,0.05f,0.05f);
glVertex3f(0.0f,0.0f,0.0f);
glVertex3f(1.50f,0.0f,0.0f);
glVertex3f(1.5f,0.3f,0.0f);
glVertex3f(0.0f,0.3f,0.0f);
glEnd();


glBegin(GL_QUADS);
glColor3f(0.05f,0.05f,0.05f);
glVertex3f(0.0f,0.0f,0.0f);
glVertex3f(0.0f,0.0f,-1.5f);
glVertex3f(0.0f,0.3f,-1.5f);
glVertex3f(0.0f,0.3f,0.0f);
glEnd();


glBegin(GL_QUADS);
glColor3f(0.05f,0.05f,0.05f);
glVertex3f(1.5f,0.0f,0.0f);
glVertex3f(1.5f,0.0f,-1.5f);
glVertex3f(1.5f,0.3f,-1.5f);
glVertex3f(1.5f,0.3f,0.0f);
glEnd();

glBegin(GL_QUADS);
glColor3f(0.05f,0.05f,0.05f);
glVertex3f(0.0f,0.0f,-1.5f);
glVertex3f(1.50f,0.0f,-1.5f);
glVertex3f(1.5f,0.3f,-1.5f);
glVertex3f(0.0f,0.3f,-1.5f);
glEnd();


glBegin(GL_QUADS);
glColor3f(0.05f,0.05f,0.05f);
glVertex3f(0.0f,0.0f,0.0f);
glVertex3f(1.50f,0.0f,0.0f);
glVertex3f(1.5f,0.0f,-1.5f);
glVertex3f(0.0f,0.0f,-1.5f);
glEnd();


glBegin(GL_QUADS);
glColor3f(1.0f,1.0f,1.0f);
glVertex3f(0.0f,0.3f,0.0f);
glVertex3f(1.50f,0.3f,0.0f);
glVertex3f(1.5f,0.3f,-1.5f);
glVertex3f(0.0f,0.3f,-1.5f);
glEnd();

}


Next is to render the objects that has been drawn. To have this declare the initRendering() function which not the usual init() function make the note of it. GL_COLOR_MATERIAL has been enabled for materializing the cube look for the board. The list will make room for the display list show that the white and black boards are displayed. Before that list need to be declared -


GLuint _displayListId_blackArea;
GLuint _displayListId_whiteArea;

Now this list will get called in the following code.



void initRendering()
{
glEnable(GL_DEPTH_TEST);
glEnable(GL_COLOR_MATERIAL);
glClearColor(0.0f,0.0f,0.2f,1.0f);
//Set up a display list for drawing a cube
_displayListId_blackArea = glGenLists(1); //Make room for the display list
glNewList(_displayListId_blackArea, GL_COMPILE); //Begin the display list
draw_BlackArea(); //Add commands for drawing a black area to the display list
glEndList(); //End the display list

//Set up a display list for drawing a cube
_displayListId_whiteArea = glGenLists(2); //Make room for the display list
glNewList(_displayListId_whiteArea, GL_COMPILE); //Begin the display list
draw_whiteArea(); //Add commands for drawing a black to the display list
glEndList(); //End the display list
}

Now call all the functions that need to display  in the function drawScene() where all object will be called the boards indivual box will form the whole chess of 8X8 game board.

void drawScene()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
//glRotatef(-_angle, 0.0f, 1.0f, 0.0f);
glTranslatef(-4*1.5, 0.0, 4*1.5);

for(float j=0.0;j>(-8*1.5);j-=1.5)
{
k++;
for(i=0.0;i<(4*3.0);i+=3.0)
{
if(k%2==0)
{
glPushMatrix();
glTranslatef(i,0.0,j);
glCallList(_displayListId_blackArea);
glPopMatrix();

}
else
{
glPushMatrix();
glTranslatef(i+1.5,0.0,j);
glCallList(_displayListId_blackArea);
glPopMatrix();

}
}
}
for(j=0.0;j>(-8*1.5);j-=1.5)
{
k++;
for(i=0.0;i<(4*3.0);i+=3.0)
{
if(k%2!=0)
{
glPushMatrix();
glTranslatef(i,0.0,j);
glCallList(_displayListId_whiteArea);
glPopMatrix();

}
else
{
glPushMatrix();
glTranslatef(i+1.5,0.0,j);
glCallList(_displayListId_whiteArea);
glPopMatrix();

}
}
}
glutSwapBuffers();
}



7 comments:

  1. Aren't all this OpenGL functions deprecated?

    ReplyDelete
  2. wher is the main function in this?
     

    ReplyDelete
  3.  You have to code main function and put all these in asingle projects

    ReplyDelete
  4.  yes but still they are in need for learning

    ReplyDelete
  5. sir! pls upload n queens using opengl....

    ReplyDelete
  6. Sir plz send me leaky bucket and tic toe game using opengl my e mail id srvinutha50@gmail.com

    ReplyDelete