Bouncing Ball Content
Bouncing Ball Content
CHAPTER 1
INTRODUCTION
1.1 COMPUTER GRAPHICS
• Graphics provides one of the most natural means of communicating with a computer, since our highly
developed 2D Or 3D pattern-recognition abilities allow us to perceive and process pictorial data rapidly.
• Computers have become a powerful medium for the rapid and economical production of pictures.
• Graphics provide a so natural means of communicating with the computer that they have become
widespread.
• Interactive graphics is the most important means of producing pictures since the invention of photography
and television .
• We can make pictures of not only the real world objects but also of abstract objects such as mathematical
surfaces on 4D and of data that have no inherent geometry.
• A computer graphics system is a computer system with all the components of the general purpose computer
system. There are five major elements in system: input devices, processor, memory, frame buffer, output
devices.
CHAPTER 2
LITERATURE SURVEY
Computer graphics started with the display of data on hardcopy plotters and cathode ray tube (CRT)
screens soon after the introduction of computers.
Computer graphics today largely interactive, the user controls the contents, structure, and appearance
of objects and of displayed images by using input devices, such as keyboard, mouse, or touch-sensitive panel
on the screen. Graphics based user interfaces allow millions of new users to control simple, low-cost
application programs, such as spreadsheets, word processors, and drawing programs.
OpenGL (Open Graphics Library) is a standard specification defining a cross language, cross-
platform API for writing applications that produce 2D and 3D computer graphics. The interface consists of
over 250 different function calls which can be used to draw complex three-dimensional scenes from simple
primitives. OpenGL was developed by Silicon Graphics Inc. (SGI) in 1992 and is widely used in CAD, virtual
reality, scientific visualization, information visualization, and flight simulation. It is also used in video games,
where it competes with Direct3D on Microsoft Windows platforms (see Direct3D vs. OpenGL). OpenGL is
managed by the non-profit technology consortium, the Khronos Group.
In the 1980s, developing software that could function with a wide range of graphics hardware was a
real challenge. By the early 1990s, Silicon Graphics (SGI) was a leader in 3D graphics for workstations. SGI's
competitors (including Sun Microsystems, Hewlett-Packard and IBM) were also able. In addition, SGI had a
large number of software customers; by changing to the OpenGL API they planned to keep their customers
locked onto SGI (and IBM) hardware for a few years while market support for OpenGL matured to bring to
market 3D hardware, supported by extensions made to the PHIGS standard. In 1992, SGI led the creation of
the OpenGL architectural review board (OpenGL ARB), the group of companies that would maintain and
expand the .
OpenGL specification took for years to come. On 17 December 1997, Microsoft and SGI initiated
the Fahrenheit project, which was a joint effort with the goal of unifying the OpenGL and Direct3D interfaces
(and adding a scene-graph API too). In 1998 HewlettPackard joined the project.[4] It initially showed some
promise of bringing order to the world of interactive 3D.
3D BOUNCING BALL
CHAPTER 3
Microprocessor: 1.0 GHz and above CPU based on either AMD or INTEL Microprocessor
Architecture
Hard Disk : 40 GB
OPENGL Library
Mouse Driver
Graphics Driver
C++ Language
3D BOUNCING BALL
CHAPTER 4
DESIGN
Existing system for a graphics is the TC++. This system will support only the 2D graphics. 2D
graphics package being designed should be easy to use and understand. It should provide various options such
as free hand drawing, line drawing, polygon drawing, filled polygons, flood fill, translation, rotation, scaling,
clipping etc. Even though these properties were supported, it was difficult to render 2D graphics cannot be
very difficult to get a 3 Dimensional object. Even the effects like lighting, shading cannot be provided. So we
go for Dev C++.
To achieve three dimensional effects, open GL software is proposed. It is software which provides a
graphical interface. It is a interface between application program and graphics hardware. The advantages are:
2.It’s a hardware independent interface i.e it can be implemented on many different hardware
platforms.
3.With openGL we can draw a small set of geometric primitives such as points, lines and polygons etc.
Basically, we are creating an application which shows the balls bouncing on a floor. The animation which
make application more interesting, with arrow keys move the camera.
Apart from the basic modules of a gl programming i.e header files, main function this application has
basically in 5 modules. We have defined global array for Colors to be used for balls and floor.
// Colors
1. CAMERA CLASS - This class will define the methods (functions) that are to be used for movement of
camera. The camera moves horizontally in a circle centered at the origin of radius 10. It moves vertically
straight up and down.
Here some use of trigonometry comes as we apply the formula with sine and cosine functions from opengl.
The variables are defined which will store the position of the camera, it's x-y-z positions. The swing or motion
of camera done via the angle, which determine x,z position. Angle will help movement of camera in and
around. While the value of y coordinate helps in moving camera up and down.
The functions prototype here will get the information and determine the position of x, y, z coordinate of
camera. Also prototype will define animation, movement in four direction - up, down, left and right.
2. BALL CLASS - All the variable and prototype function declared for the balls. A ball has a radius, a color,
and bounces up and down between a maximum height and the xz plane. Therefore the x and z coordinates of
the balls are fixed. It uses a lame bouncing algorithm, simply moving up or down by a mere 0.05 units at each
frame.
3D BOUNCING BALL
The prototype of function that will draw the ball will have following parameters - radius, color, x, y, z. Here
x-y-z are coordinate positions. The ball is given a good shape with call of glutSolidSphere .
3. CHECKERBOARD CLASS - We all know a checkerboard is one with alternate square of two different
color. This class will define methods and variable for checkerboard. The number of squares in the checker
board is set via constructor. Each of the square in checkerboard is 1 x 1 unit. The one of it's corner is (0, 0)
and the board stretches out along positive x and positive z directions. It rests on the xz plane.
We have used the concept of Display list in this class to draw the checkerboard. Other like giving the
checkerboard lighting, material, ambient opengl function has been called.
4. DISPLAY - Though display is necessary for every application in gl programming, we had called it a
separate module. Like all other gl programs, here we define to draw the objects on the screen.
Here we draws one frame, first the checkerboard and then the balls, from the current camera position.
5. USER INTERACTION - Without user interaction the program look dull, hence we have added user
interaction to this program with special function (just named so).
CHAPTER 5
IMPLEMENTATION
3D BOUNCING BALL
5.1 FUNCTIONS
The glColor3f (float, float, float) :- This function will set the current drawing color
gluOrtho2D (GLdouble left, GLdouble right, GLdouble bottom, GLdouble top):- which defines a two
dimensional viewing rectangle in the plane z=0.
glClear( ):-Takes a single argument that is the bitwise OR of several values indicating which buffer is to be
cleared.
glClearColor ():-Specifies the red, green, blue, and alpha values used by glClear to clear the color buffers.
Void glutInit (int *argc, char**argv):-Initializes GLUT, the arguments from main are passed in and can be
used by the application.
Void glutInitDisplayMode (unsigned int mode):-Requests a display with the properties in mode. The value of
mode is determined by the logical OR of options including the color model and buffering.
Void glutInitWindowSize (int width, int height):- Specifies the initial position of the topleft corner of the
window in pixels
Int glutCreateWindow (char *title):-A window on the display. The string title can be used to label the
window. The return value provides references to the window that can be used when there are multiple
windows.
Void glutMouseFunc(void *f(int button, int state, int x, int y):-Register the mouse callback function f. The
callback function returns the button, the state of button after the event and the position of the mouse relative to
the top-left corner of the window.
Void glutKeyboardFunc(void(*func) (void)):-This function is called every time when you press enter key to
resume the game or when you press ‘b’ or ‘B’ key to go back to the initial screen or when you press esc key
to exit from the application.
Void glutDisplayFunc (void (*func) (void)):-Register the display function func that is executed when the
window needs to be redrawn.
Void glutSpecialFunc(void(*func)( void)):-This function is called when you press the special keys in the
keyboard like arrow keys, function keys etc. In our program.
3D BOUNCING BALL
glOrtho
This function defines orthographic viewing volume with all parameters measured from the centre of
projection. multiply the current matrix by a perspective matrix.
SYNTAX:
void glOrtho( GLdouble left, GLdouble right, GLdouble bottom, GLdouble top,
PARAMETERES:
left, right - Specify the coordinates for the left and right vertical clipping planes.
bottom, top - Specify the coordinates for the bottom and top horizontal clipping planes.
nearVal, farVal - Specify the distances to the nearer and farther depth clipping planes. These values are
negative if the plane is to be behind the viewer.
glutDisplayFunc Function
SYNTAX:
glutReshapeFunc Function
SYNTAX:
argcp - A pointer to the program's unmodified argc variable from main. Upon return, the value pointed to by
argcp will be updated, because glutInit extracts any command line options intended for the GLUT library.
Argv - The program's unmodified argv variable from main. Like argcp, the data for argv will be updated
because glutInit extracts any command line options understood by the GLUT library.
• glutInit(&argc,argv);
glutInitDisplayMode Function
SYNTAX:
PARAMETERS:
mode - Display mode, normally the bitwise OR-ing of GLUT display mode bit masks. See values below:
GLUT_SINGLE.
glutMainLoop Function
SYNTAX:
void glutMainLoop(void);
CHAPTER 6
SOURCE CODE
#include <iostream>
3D BOUNCING BALL
#ifdef __APPLE_CC__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
#include <cmath>
class Camera {
double theta;
double y;
double dTheta;
double dy;
public:
};
class Ball {
3D BOUNCING BALL
double radius;
GLfloat* color;
double maximumHeight;
double x;
double y;
double z;
int direction;
public:
void update() {
y += direction * 0.05;
if (y > maximumHeight) {
y = radius; direction = 1;
glPushMatrix();
glTranslated(x, y, z);
glPopMatrix();
};
3D BOUNCING BALL
class Checkerboard {
int displayListId;
int width;
int depth;
public:
void create() {
displayListId = glGenLists(1);
glNewList(displayListId, GL_COMPILE);
glBegin(GL_QUADS);
glNormal3d(0, 1, 0);
glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE,
(x + z) % 2 == 0 ? RED : WHITE);
glVertex3d(x, 0, z);
glVertex3d(x+1, 0, z);
glVertex3d(x+1, 0, z+1);
glVertex3d(x, 0, z+1);
glEnd();
3D BOUNCING BALL
glEndList();
void draw() {
glCallList(displayListId);
};
Camera camera;
Ball balls[] = {
Ball(0.4, WHITE, 5, 1, 7)
};
void init() {
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
checkerboard.create();
void display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
3D BOUNCING BALL
gluLookAt(camera.getX(), camera.getY(), camera.getZ(),
checkerboard.draw();
balls[i].update();
glFlush();
glutSwapBuffers();
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
void timer(int v) {
glutPostRedisplay();
switch (key) {
glutPostRedisplay();
glutInit(&argc, argv);
glutInitWindowPosition(80, 80);
glutInitWindowSize(800, 600);
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutSpecialFunc(special);
init();
glutMainLoop();
CHAPTER 7
SNAPSHOTS
3D BOUNCING BALL
Figure 7.1
Rotating
View of 3D
Bouncing
Ball
CONCLUSION
In this 3D Bouncing Ball openGL Programming, a timer is also there which requests to draw the next frame.
Also the size of the ball and colors are hard-coded which can be change with global array. Similarly size and
color of checkerboard is hard-code which also can be changed via program. We have used navigation key for
user interaction. GL Programming is programming with use of opengl graphics library. OpenGL API contains
several functions, which helps rendering 2D/3D image as graphics on computer. It is designed to streamline
the process of rendering the different objects on computer screen. One of the best part that OpenGL has over
DirectX is that, OpenGL is hardware/OS independent. First the floor is drawn which is a checkerboard, most
likely similar to our opengl chess board program. we are created an application which shows the balls
bouncing on a Chess Board.
3D BOUNCING BALL
BIBLIOGRAPHY
Website Referred:
Link :- https://www.geeksforgeeks.org/opengl-program-simple-ball-game/?ref=lbp
2)Video Tutorial
Link :- https://youtu.be/jOVdMTl4mII
Link:- https://youtu.be/ARGR-rk1h24
Link :- https://www.glprogramming.com/red/
Link :- https://youtu.be/45MIykWJ-C4