Search Projects

Sunday, February 24, 2013

Draw Crab Pong Game using OpenGL

Games are integral part of one's life, there are many games developed for mobile phones, smart phones, tablets, Pc and off-course for high experience Xbox, Play stations have some great varieties of games. These games are not just made for children but elder also use to have some hands on them.



Abstract : In this tutorial we are going to talk about a very interesting but quite simple game. This game is called Pong. Since the player here is shaped as crab so the project is named as Crab Pong Game. The game consist of 4 player one on each corner. 3 player are programmed to control by computer while one will be handled by the player. The rule are as stated  -
A player is scored (lose a point) on when a ball crosses the player's side, but the crab isn't there to block it from crossing. The time when player reaches 0 points, the player is got  eliminated, and the position of the player is turn as wall.  There are four circles at each diagonally placed  from the corners to limit balls' and crabs' positions.  There may be multiple balls in play at once.  New balls are added to play by fading in at the center of the board.  As in the original Pong,the direction of a ball after hitting a crab depends on the place where the ball hit the crab.
Description  and Downloads :  This project is quite a large to describe but a wonderful description of this available atVideotutorialsrock, there the interactive video tutorial is also available. The Project is quite good and presented very well. One this need to note that instead of crab you can change the player as different character as well by modification of code. Get the full source code.

Want to modify this project and make your own, put your comment in here. Also tell if you face some problem or things are not getting in.

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();
}