Monday, November 21, 2011

Draw a 3D Chess Board using OpenGL utility toolkits (GLUT), a graphics project with source code

This program  may help you in graphics project of your college.I simply  tries to draw the 3D Chess Board.  For this our main goal is to draw white and black rectangular box alternately along both x and y-axis.Drawing rectangular box is simple ,it is drawn by providing all side co-ordinates with the help of glVertex3f function and only top face is assign with black or white color and other face with brown color with the help of glColor3f function. I have made two function void draw_BlackArea() and void draw_whiteArea() to draw black and white square box respectively .Inside two for loop I have call these function by creating displaylist and with the help of glTranslatef(GLfloat x,GLfloat y,GLfloat z) function I draw  alternatively by translating their position.



In this program you will also learn to create displaylist and rotate a object and also understand the use of gluLookAt(eyeX,eyeY,eyeZ,centerX,centerY,centerZ,upX,upYupZ) function.
Here is the source code written in C++ and GLUT .Copy  and compile it in codeblocks you will see nice 3D Chess Board is rotating in y-axis.
#include<windows.h>
#include <GL/glut.h>
#include <cstdlib>
#include <iostream>
#include <cmath>
using namespace std;
float _angle = 0;
float i;
int k=0;
GLuint _displayListId_blackArea; //The OpenGL id of the display list
GLuint _displayListId_whiteArea; //The OpenGL id of the display list
 
void handleKeypress(unsigned char key,int x,int y)
{
switch(key)
{
case 27:
exit(0);
}
}
 
void handleResize(int w,int h)
{
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0,(double)w/(double)h,1.0,200);
gluLookAt(0.0f,5.5f, 15.0f,
0.0f,0.0f,0.0f,
0.0f,1.0f,0.0f);
}
 
void draw_BlackArea()
{
// glPushMatrix();
//glTranslatef(1.5f,0.0f,0.0f);
glBegin(GL_QUADS);
glColor3f(0.05f,0.05f,0.05f);
glTranslatef(0.0f,0.0f,0.0f);
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();
//    glPopMatrix();
 
}
void draw_whiteArea()
{
// glPushMatrix();
glBegin(GL_QUADS);
glColor3f(0.05f,0.05f,0.05f);
glTranslatef(0.0f,0.0f,0.0f);
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();
// glPopMatrix();
 
}
 
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
}
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(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_whiteArea);
glPopMatrix();
 
}
else
{
glPushMatrix();
glTranslatef(i+1.5,0.0,j);
glCallList(_displayListId_whiteArea);
glPopMatrix();
 
}
}
}
glutSwapBuffers();
 
 
 
}
void update(int value) {
_angle += 1.0f;
if (_angle > 360) {
_angle -= 360;
}
glutPostRedisplay();
glutTimerFunc(25, update, 0);
}
int main(int argc,char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH);
glutInitWindowSize(800,800);
 
glutCreateWindow("basic shape");
initRendering();
glutFullScreen();
 
glutDisplayFunc(drawScene);
glutKeyboardFunc(handleKeypress);
glutReshapeFunc(handleResize);
glutTimerFunc(25, update, 0);
 
glutMainLoop();
 
return 0;
 
}



4 comments:

  1. the most failure code i've ever seen,
    why don't you just make a for loop ???????? just like this
    for (int i = 0; i <8; i++)
    {
    for (int j = 0; j > -8; j--)
    {
    if (i%2==0)
    {
    if (j % 2 == 0)
    {
    //Gl.glColor3d(1, 1, 1);
    glColor3ub(235, 191, 120);
    }
    else
    {
    glColor3ub(109, 75, 47);
    }

    }
    else
    {
    if (j % 2 == 0)
    {
    glColor3ub(109, 75, 47);

    }
    else
    {
    glColor3ub(235, 191, 120);

    }


    }

    glBegin(Gl.GL_POLYGON);
    glVertex3d(i, 0, j);
    glVertex3d(i, 0, j-1);
    glVertex3d(i+1, 0, j-1);
    glVertex3d(i+1, 0, j);
    glEnd();
    glFlush();
    }
    }

    ReplyDelete