Search Projects

Sunday, February 28, 2016

Simple Opengl Program - Bouncing Ball

We have shared so many OpenGL Programs, today we are going to share a simple opengl program on Bouncing Ball. This program is simple, easy to code and understand as well, you can modify the program easily and made it into a superb project.

Simple Opengl Program - Bouncing Ball


Code Bouncing Ball C Program

Initialization

First we create a flag which will check the ball's uppermost position and lower most position. we will also define the coordinate of ball that varies as it bounce. The coordinate will be determine by a certain variable which changes the coordinate position by an amount which we fix. 

static int flag=0;
float tx=0.0,ty=0.0,tz=0.0;
float ball_x=0.5,ball_y=0.0,ball_z=0.0;

Draw and Update Ball

In this Simple Opengl Program we are going to create a procedure that will update the position of ball according to it's motion upward and downward. The function will use the variable flag as mentioned above. The update ball function will update the position of the ball of up and down. 

void updateBall()
{
    if(!flag)
    {
        ball_y+=0.05;
        if(ball_y>1.0)
            flag=1;
    }
    if(flag)
    {
        ball_y-=0.05;
        if(ball_y<-1)
            flag=0;
    }
}

In the draw function of this Simple Opengl Program we are going to draw the ball and make it bounce. Set current matrix on the stack and draw the Ball with the help of  glutSolidSphere. we call the Update function here so that the ball an change according to it's position going up or down. 

void display()
{
        glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
     
        glClearColor(0,1,0,0);
     
       glPushMatrix();
     
        glBegin(GL_POLYGON);
            glColor3f(1,0,1);
         
            glVertex3f(-0.25,0,-1);

            glVertex3f(0.25,0,-1);

            glVertex3f(0,0.25,-1);
        glEnd();
        glPopMatrix();
 

        glPushMatrix();
            glColor3f(1,1,0);
            glTranslatef(ball_x,ball_y,ball_z);
            glutSolidSphere(0.3,60,23);
        glPopMatrix();

        updateBall();


        glutSwapBuffers();
}

Future Enchantments of Program

Drawing Ball

You can change the size of the ball by modifying the glutSolidSphere, color of the ball can also be modified similarly. Background of the color can also be added and changed accordingly. You an draw any new object in the background as the ball bounces.

What you can Modify

You can modify this Simple Opengl Program by adding some user interaction that will let ball start and stop. You can add sound to the program a well. The 3D version of the program can also be made as a future enchantments.   


Download the Simple OpenGL Program on Bouncing Ball.

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.

Sunday, February 21, 2016

Block Breaker Sample C++ Opengl Code

Get started with OpenGL C++

It is easy to get stated with OpenGL C++. First you need to setup the environment for programming and then use some sample c++ opengl code. In windows system, you need to install the Microsoft Visual studio for programming environment. Install Glut on windows and start coding your computer graphics programs in C/C++.  You need to add the dependencies of glut or glu files with the new visual studio. There are many sample c++ opengl code are available on this site, with free source code download. Linux system are bit different, you have to use the Terminal for execution of program. Install opengl in Linux and then you can use some opengl c++ examples, execute the C/C++ program in OpenGL with help of some commands.

Block Breaker

We have listed so many opengl c++ examples that can help you understand OpenGL C++. Today we are sharing the sample c++ opengl code for Block Breaker.

Block Breaker Sample C++ Opengl Code

Overview

In Block Breaker we divide the whole section in two part - Block area and Hit Area. Block Area contains the blocks, while hit area have one bat that will stop the ball hitting the wall below. Ball moves and break the blocks in screen in randomly with the direction of force. While up, right and left wall is free for ball to hit, down wall is protected with bat. If ball hit the download to a specific no of time you will looses the game.

Coding

There are numerous function used in this sample c++ opengl code. Following done for coding the entire program so this Block Break game works perfectly.
  1. A function created to set the initial co-ordinates of the objects on the screen.
  2. Function for checking collision between block, walls, bat with the ball and give it the direction accordingly. Coding have been done keeping in mind the real world scenario.
  3. Yeah Most importantly the design for Block has been coded.
  4. The coordinate position of Ball as it move changes, which has been record with a reshape function.
  5. Function added for - as the ball touches the block, block should disappear.
  6. Lastly the main function, draw function and keyboard function to make UI better.
You can check the program, it has been well documented as well so it is easy to understand, which function works for what.

Download sample c++ opengl code

We are dedicate to provide you the source code for opengl c++ examples as we get one. So Download this sample c++ opengl code with the download link below. Do comment about the projects, any query you have in mind. Suggest us to improve. Do share source code (this source code is share by one of our reader) with us which is not published on this blog.


Friday, February 19, 2016

3D Racing Game in OpenGL

A Slovenian student of University of Maribor has developed 3D Racing Game in OpenGL with the use of - c++, OpenGL, Fmod for sounds, Assimp for loading models and some algorithm for track. I found it while browsing on my YouTube Channel. He has brilliantly coded the game which look like professional one while little improvement can be done on UI and Graphics part.

We already have shared you 2d car racing game in opengl computer graphics and formula one racing in opengl c++, but this project is very good and other project will able to compete with this 3D Racing Game in OpenGL.

I din't able to find the source code but only a video demo which I have embedded below. I am trying to contact the person to let him share the code with us, if does we are going to share it with our fellow students who would love to give this a their project.

I want you guys to try coding this and get something from this output. though this is too complex but with the help of codes provide by me in this blog and your teacher and friends you would be able to come up with some nice result. Do try experiment with code and create magic like this. Hope you will able to do so, and don't forget to share with us as well. Good Luck!

Watch Video Demo for 3D Racing Game in OpenGL


Wednesday, February 10, 2016

3D Bi-Cycle OpenGL Programming

Before going into OpenGL Programming of 3D Bi-Cycle, let know in brief about OpenGL.

What is OpenGL?

OpenGL (Open Graphics Library) is an api use for rendering 2D and 3D graphic. It is cross platform, supports in multiple programming languages. Silicon Graphics originally developed OpenGL in 90's, which is know most used industry standard open graphics. OpenGL uses geometric primitives - points, lines, and polygons in modeling the objects.

OpenGL Programming

As we get to know OpenGL is cross platform, hence it can run on any device. OpenGL Programming is used fro developing windows applications, mobile applications and other console applications. You can experience the OpenGL Programming in Android development or iOS Application development, almost all the mobile graphics application on these platform uses the OpenGL to render the graphical objects. The OpenGL Programming can be done in C,C++, Java and other languages.

Draw 3D Bi-Cycle in OpenGL



First we need to structures the Bi-Cycle - find different part and create a function for each of them. Finally all the functions can be used in display to show it on the screen. Different parts of Bi-Cycle are - Frame, Gears, Tyre, Chain, Pedals and Seat. Draw Frame Since a Bi-Cycle Frame consists of different part so it need to be drawn by using different primitive objects. Road of the Bi-Cycle is in cylindrical form so we have formed two function - Xcylinder and ZCylinder. These two functions take the parameter - radius and length.