Search Projects

Wednesday, February 24, 2016

3D spinning Cube gl programming

OpenGL programming is also know as gl programming. OpenGL is not a programming language rather it is API which allow other programming languages ton exploit and develop computer graphics objects.

Overview of  gl programming

Before going to talk about the main project we are first talk the following question. This question comes in mind of all opengl learners.
What is  gl programming?
gl programming is the use of opengl api to develop games, 3 objects etc via computer graphics. The  gl programming is to create the program with the help existing language and rendering the objects with api provide in OpenGL. The base of  gl programming is Opengl - open computer graphics library. With the help of  gl programming we can do things like developing a games for pc, mobiles and other devices. It also helps in different sectors for research and implementation like scientific research, weather, medicines, card and architecture designs.

3D spinning Cube

In this post we will cover the 3D spinning cube. As we all know that a cube has 6 faces, we will color the different faces in different colors. The 3d spinning cube will be fully colorful. The cube will look good and it will make full use of computer graphics.
Note - VTU students have two programs on Cubes in the syllabus.

3D spinning Cube gl programming

Design the Cube and Spinning

To design the cube in opengl we need to make use of simple functions. Let go to code a cube in OpenGL. First we need to define the vertices of the cube and then color for each of it sides.

/*Matrix for the vertices of Cubes*/
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},
};
/*Matrix for the color of sides of Cubes*/
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},
};
Next is to give translate and rotation to the cube. We code that like -
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();//push on stack
glTranslatef(0.0, 0.0, -15.0); //move
glRotatef(rot, 0.0, 1.0, 0.0); //rotation
glTranslatef(0.0, 0.0, -5.0); //move
glRotatef(rot*2, 1.0, 0.0, 0.0); //rotation
Till know cube is not designed yet for that we have used the GL_QUADS and glVertex. We utilized the vertices and color matrix as defined above to render the 3d cube.
glBegin(GL_QUADS);
glColor3fv(color[0]);
glVertex3fv(vert[0]);
glVertex3fv(vert[1]);
glVertex3fv(vert[2]);
glVertex3fv(vert[3]);
glColor3fv(color[1]);
glVertex3fv(vert[4]);
glVertex3fv(vert[5]);
glVertex3fv(vert[6]);
glVertex3fv(vert[7]);
glColor3fv(color[2]);
glVertex3fv(vert[0]);
glVertex3fv(vert[1]);
glVertex3fv(vert[5]);
glVertex3fv(vert[4]);
glColor3fv(color[3]);
glVertex3fv(vert[2]);
glVertex3fv(vert[3]);
glVertex3fv(vert[7]);
glVertex3fv(vert[6]);
glColor3fv(color[4]);
glVertex3fv(vert[3]);
glVertex3fv(vert[0]);
glVertex3fv(vert[4]);
glVertex3fv(vert[7]);
glColor3fv(color[5]);
glVertex3fv(vert[1]);
glVertex3fv(vert[2]);
glVertex3fv(vert[6]);
glVertex3fv(vert[5]);
glEnd();
In this gl programming example we are not using any keyboard function, nor do we used the mouse function. On execution of the program we will get the colorful cube which rotates or spins around in a path on screen.
Download  the free Source code
Give suggestion or any query to us via email to openglprojects@gmail.com.

No comments:

Post a Comment