Archive Pages Design$type=blogging

3D spinning Cube gl programming

We are going to demonstrate the 3D spinning Cube gl programming in this blog post with free source code.

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.

COMMENTS

Name

3D Bi-Cycle 3D Game 3D Laptop 3D Objects 3D Projects 3D Zoo Algorithm Android Aquarium Bellman-Ford Algorithm Block Breaker Blockshooting Bucket Sort C# Chess Clock Code Blocks Colors Complex Project Complex Projects Crab Pong Cross Zero DSAV Escapa First Come First Serve Fluids Font Demo Games Gear Motion GlutDino Glutplane Graphics Editor iOS iPhone Java Java OpenGL Graphics Programming Light Torus Linked List Ludo Board Game Mancala board games Memory blocks game Memory Game Menu Mickey Mouse Minesweeper Miniature Steam Engine moth Motion Blur MoveLight moving car Moving Ship Multiplex Networking Based Project Nuclear Power Plant Olympic opengl c++ examples OpenGL ES OpenGL Tutorial Origami OS Based Projects Paint Paper Folding Particles Drop Path Finding ping pong Pong Game Project Report Projects Report Puzzles qix like Random Flowing lines Rigid Body Rings Robot sample c++ opengl code Screen Saver Screen Saver Ship Shadow Cube Shadowfun Ship Iceberg Simple Drawing Simple Haloed Lined Wireframe Simple Move Light Simple Project Simulation SNAKE XENZIA GAME Solar system Sorting Sphere Spot Light Swing Stars Taj Mahal The Edge Game Tic Tac Toe Tower of Hanoi Traffic Signal Transformation Triangular Animation Tutorial water ripple effects Whirlpool
false
ltr
item
OpenGL Projects: 3D spinning Cube gl programming
3D spinning Cube gl programming
We are going to demonstrate the 3D spinning Cube gl programming in this blog post with free source code.
https://2.bp.blogspot.com/-mqH0Gz9otkc/Vs3rnFp8WvI/AAAAAAAAHkA/Vb2mM8qlwkw/s320/3d%2Bspinning%2Bcube.PNG
https://2.bp.blogspot.com/-mqH0Gz9otkc/Vs3rnFp8WvI/AAAAAAAAHkA/Vb2mM8qlwkw/s72-c/3d%2Bspinning%2Bcube.PNG
OpenGL Projects
http://www.openglprojects.in/2016/02/3d-spinning-cube-gl-programming.html
http://www.openglprojects.in/
http://www.openglprojects.in/
http://www.openglprojects.in/2016/02/3d-spinning-cube-gl-programming.html
true
2020194800836807725
UTF-8
Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago