Learn how to draw simple opengl program the Bouncing Ball in opengl in C computer graphics.
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.
Code Bouncing Ball C Program
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;
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.
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.
Future Enchantments of Program
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.