Search Projects

Monday, March 18, 2013

Learn how to make a Simple 3D Rotating Cube using OpenGL

In this tutorial we are going to learn about "spinning cube animation" or a '3D Rotating Cube". First we think how to animate? The answer - by allowing continuously display the picture with different frames shown, little by little and giving the appearance of motion.


First we declare the following -


#include<GL/glut.h>
#include<GL/gl.h>
static GLfloat rot;

The header are included to access the OpenGL libraries. Then there is variable for the angle of rotation of the cube. Next we call the display function which will have the matrix that holds the vertices as well the color values in them.


static GLfloat vert[][4]={
{ 1.0,  1.0,  1.0},
{-1.0,  1.0,  1.0},
{-1.0, -1.0,  1.0},
{ 1.0, -1.0,  1.0},
{ 1.0,  1.0, -1.0},
{-1.0,  1.0, -1.0},
{-1.0, -1.0, -1.0},
{ 1.0, -1.0, -1.0},
};

static GLfloat color[][4]={
{1.0, 0.0, 0.0, 0.0},
{0.0, 1.0, 0.0, 0.0},
{0.0, 0.0, 1.0, 0.0},
{0.0, 1.0, 1.0, 0.0},
{1.0, 0.0, 1.0, 0.0},
{1.0, 1.0, 0.0, 0.0},
};


Rest display function will have the transformation functions and drawing of the cubes. Store the vertices of the cube by calling the glPushMatrix(). It pushes the matrix values on the stack (different stacks for glMatrixmode modes). Move all vertices by vector called on glTranslatef() and rotate by the declared angled calling on glRotatef() toward the axis coded.


glTranslatef(0.0, 0.0, -15.0);
glRotatef(rot, 0.0, 1.0, 0.0);
glTranslatef(0.0, 0.0, -5.0);
glRotatef(rot*2, 1.0, 0.0, 0.0);

Next is to draw the cubes using the glBegin(GL_QUADS) by allocating the proper vertices and the colors that has been declared before at beginning of the display function. Then Pop the matrix that is on the stack with glPopMatrix(); 


void reshape_func(int width, int height)
{

glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-1.0, 1.0, -1.0, 1.0, 3.0, 10000.0);

glMatrixMode(GL_MODELVIEW);
}

As mentioned in the above there are many modes for  glMatrixmode one is GL_PROJECTION which is to set the projection like widening the camera lenses and GL_MODELVIEW is for the setting up the object that is to be drawn, like size and place in space. The glViewport specifies the affine transformation of x and y from normalized device coordinates to window coordinates. glLoadIdentity() replaces the current matrix with the identity matrix. glFrustum() describes a perspective matrix that produces a perspective projection.

void idle_func(void)
{
rot=0.1*(GLfloat)glutGet(GLUT_ELAPSED_TIME); 
glutPostRedisplay(); //re-draw the screen glut
}

In the idle_func we chnage the angle by time by time by calling the glutGet which returns the amount of time (in milliseconds) from the time you call the () glutInit. The glutPostRedisplay() re-draw the screen. Below is the main function is self descriptive if you have code any sample program in OpenGL.

int main(int argc, char *argv[])
{

glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(300,300);
glutCreateWindow("Sample 4");
glutDisplayFunc(display_func);
glutReshapeFunc(reshape_func);
glutIdleFunc(idle_func);
glEnable(GL_DEPTH_TEST);
glutMainLoop();
return 0;
}

Download Full Source code

Saturday, March 16, 2013

Implementation of path finding game with opengl


path finding game opengl projects


ABSTRACT

            The aim of the mini project is to implement the path finding game. The Path finding game containing a rectangular maze of any shape and size in which the horizontal and vertical lines represent the walls of the maze. Path finding game can be made difficult or easy, that is depend upon the maze. Maze can be implemented as in 2D, 3D or more higher dimensions. This game is very popular as puzzle solving. Hence it can be used as an effecting tool for logical reasoning and mental aptitude.

Overview

           Our project title is “Implementation of path finding game”. What this means is that we are building a maze of any shape and size and search for a path through it. So, our goal is to create a Perfect Maze. More specifically, the maze we are building is a 2-Dimensional of fixed shape and size, in which the horizontal and vertical walls are connected in such a way, so that the point can move from given starting point to the ending point through the spaces formed by connecting walls, but point should never cross the wall.  

Working

           The main working of path finding game is to find out the path from given place to another place by using the movement of point. We use the special key button for the movement of point. The left key button is used to movement of point along  the X-axis as the value decreases, The right key button is used to movement of point along  the X-axis as the value increases, The up key button is used to movement of point along  the y-axis as the value increases, the down key button is used to movement of point along  the y-axis as the value decreases. There is also be given the time limitation, so it is necessary to find out the path within a given time interval.

Pay Rs 250 with Bank-wire *
or
Pay with Paypal $6* only! 

Project Docs
* call 8147656011 or 9945831394 for any query!

Saturday, March 2, 2013

How to add menu to your OpenGL Projects

In OpenGL programming there many way to make the user interactive programs. One of the things that is the use of menu, which is like selecting something (off-course with mouse) to change the state of some variable to achieve the desire result with that change. In this tutorial we are going to see how to add menu in OpenGL.



Adding menu in you OpenGL Projects not only make the program more user interactive but it make it smother and more usefull. So let get started with it.

For adding menu in our OpenGL Projects we use the glutCreateMenu, which create the popup menu and return  identifier (small integral value). The code looks like -

glutCreateMenu(menu); // it can be given to variable like mainmenu=glutCreateMenu(menu)

Firstly we create sub-menus then we go for the main menu which became the current menu.

glutAddSubMenu("Operating System", os);

The string "Operating System" represent the name of sub-menu that is to be displayed, while second argument 'os' is the sub-menu that has been created.

To add the entries in the sub menu list we use glutAddMenuEntry, which a menu entry to the bottom of the current menu.

glutAddMenuEntry("Windows", 1);

Here "Windows", is the string or the name that is to be displayed and the number is for the purpose of menu's callback function, when it is selected.
Since menu are mouse driven events in OpenGL, so we can add the interaction to either the left or right mouse button or on both (separate menu's). This is done by calling glutAttachMenu with right or left mouse button as argument.

 glutAttachMenu(GLUT_RIGHT_BUTTON); //for right mouse click
 glutAttachMenu(GLUT_LEFT_BUTTON);  //for left mouse click

This is all! So easy to add menu's to the projects in OpenGL.

Here is sample program that has menu demonstrated.