0% found this document useful (0 votes)
330 views

Bouncing Ball Content

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
330 views

Bouncing Ball Content

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 24

3D BOUNCING BALL

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.

Figure 1.1 Components of Computer Graphics

1.2 OPENGL TECHNOLOGY


3D BOUNCING BALL
OpenGL is the premier environment for developing portable, interactive 2D and 3D graphics
applications. Since its introduction in 1992, OpenGL has become the industry's most widely used and
supported 2D and 3D graphics application programming interface (API), bringing thousands of applications to
a wide variety of computer platforms.
OpenGL fosters innovation and speeds application development by incorporating a broad set of
rendering, texture mapping, special effects, and other powerful visualization functions. Developers can
leverage the power of OpenGL across all popular desktop and workstation platforms, ensuring wide
application deployment.
OpenGL Available Everywhere: Supported on all UNIX® workstations, and shipped standard with
every Windows 95/98/2000/NT and MacOS PC, no other graphics API operates on a wider range of hardware
platforms and software environments.
OpenGL runs on every major operating system including Mac OS, OS/2, UNIX, Windows 95/98,
Windows 2000, Windows NT, Linux, Open Step, and BeOS; it also works with every major windowing
system, including Win32, MacOS, Presentation Manager, and X-Window System. OpenGL is callable from
Ada, C, C++, Fortran, Python, Perl and Java and offers complete independence from network protocols and
topologies.

Figure 1.2 The openGL Interface


3D BOUNCING BALL

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

SYSTEMREQUIREMENTS & SPECIFICATION

3.1 HARDWARE REQUIREMENTS

Minimum hardware specification

 Microprocessor: 1.0 GHz and above CPU based on either AMD or INTEL Microprocessor
Architecture

 Main memory : 512 MB RAM

 Hard Disk : 40 GB

 Hard disk speed in RPM:5400 RPM

 Keyboard: QWERTY Keyboard

 Mouse :2 or 3 Button mouse

 Monitor : 1024 x 768 display resolution

3.2 SOFTWARE REQUIREMENTS

Minimum software specification

 Operating system : Windows (Any)

 Tool Used : Dev C++

 OPENGL Library

 Mouse Driver

 Graphics Driver

 C++ Language
3D BOUNCING BALL

CHAPTER 4

DESIGN

4.1 EXISTING SYSTEM

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++.

4.2 PROPOSED SYSTEM

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:

1.Open GL is designed as a streamlined.

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.

4.It provides double buffering which is vital in providing transformations.

5.It is event driven software.

6.It provides call back function.

4.3 WHAT WE ARE DEVELOPING?


3D BOUNCING BALL
First the floor is drawn which is a checkerboard, most likely similar to our opengl chess board program. Then
we will draw three balls, of different colors. These balls will bounce up and down on the floor. To make the
program or interesting, the user interaction has been added to it. With the help of four navigation key, the
floor as well as ball, the whole system can be zoom in/out and can also be move around - simply say moves
the camera in different directions - swinging the camera around.

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.

4.4 PROJECT MODULES

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

GLfloat WHITE[] = {1, 1, 1};

GLfloat RED[] = {1, 0, 0};

GLfloat GREEN[] = {0, 1, 0};

GLfloat MAGENTA[] = {1, 0, 1}

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).

4.5 KEY USES TO CONTROL

up Move camera up/zoom in

down Move camera down/zoom out

left Move camera left

right Move camera right

4.6 DIMENSIONAL VIEWING


3D BOUNCING BALL

Figure 4.6 3D Dimensional Viewing

4.7 LOW LEVEL DESIGN


3D BOUNCING BALL

Figure 4.7 Event Handler Map

4.8 FLOW DIAGRAM


3D BOUNCING BALL

Figure 4.8 Flow Diagram Of 3D Bouncing Ball

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.

GlLoadIdentity( ):-the current matrix with the identity matrix.

glMatrixMode(mode):-Sets the current matrix mode, mode can be GL_MODELVIEW, GL_PROJECTION


or GL_TEXTURE.

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

5.2 FUNCTIONS USED TO SET THE VIEWING VOLUME

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,

GLdouble near, GLdouble far)

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.

5.3 CALL BACK FUNCTIONS

glutDisplayFunc Function

glutDisplayFunc sets the display callback for the current window.

SYNTAX:

void glutDisplayFunc(void (*func)(void));

glutReshapeFunc Function

glutReshapeFunc sets the reshape callback for the current window.

SYNTAX:

void glutReshapeFunc(void (*func)(int width, int height));

5.4 MAIN FUNCTION

glutInit is used to initialize the GLUT library.

SYNTAX: glutInit(int *argcp, char **argv);


3D BOUNCING BALL
PARAMETERS:

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

glutInitDisplayMode sets the initial display mode.

SYNTAX:

void glutInitDisplayMode(unsigned int mode);

PARAMETERS:

 mode - Display mode, normally the bitwise OR-ing of GLUT display mode bit masks. See values below:

GLUT_RGB: An alias for GLUT_RGBA.

GLUT_DOUBLE:Bit mask to select a double buffered window. This overrides

GLUT_SINGLE.

GLUT_DEPTH: Bit mask to select a window with a depth buffer.

glutMainLoop Function

glutMainLoop enters the GLUT event processing loop.

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>

GLfloat WHITE[] = {1, 1, 1};

GLfloat RED[] = {1, 0, 0};

GLfloat GREEN[] = {0, 1, 0};

GLfloat MAGENTA[] = {1, 0, 1};

class Camera {

double theta;

double y;

double dTheta;

double dy;

public:

Camera(): theta(0), y(3), dTheta(0.04), dy(0.2) {}

double getX() {return 10 * cos(theta);}

double getY() {return y;}

double getZ() {return 10 * sin(theta);}

void moveRight() {theta += dTheta;}

void moveLeft() {theta -= dTheta;}

void moveUp() {y += dy;}

void moveDown() {if (y > dy) y -= dy;}

};

class Ball {
3D BOUNCING BALL
double radius;

GLfloat* color;

double maximumHeight;

double x;

double y;

double z;

int direction;

public:

Ball(double r, GLfloat* c, double h, double x, double z):

radius(r), color(c), maximumHeight(h), direction(-1),

y(h), x(x), z(z) {

void update() {

y += direction * 0.05;

if (y > maximumHeight) {

y = maximumHeight; direction = -1;

} else if (y < radius) {

y = radius; direction = 1;

glPushMatrix();

glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, color);

glTranslated(x, y, z);

glutSolidSphere(radius, 30, 30);

glPopMatrix();

};
3D BOUNCING BALL
class Checkerboard {

int displayListId;

int width;

int depth;

public:

Checkerboard(int width, int depth): width(width), depth(depth) {}

double centerx() {return width / 2;}

double centerz() {return depth / 2;}

void create() {

displayListId = glGenLists(1);

glNewList(displayListId, GL_COMPILE);

GLfloat lightPosition[] = {4, 3, 7, 1};

glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);

glBegin(GL_QUADS);

glNormal3d(0, 1, 0);

for (int x = 0; x < width - 1; x++) {

for (int z = 0; z < depth - 1; z++) {

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);

};

Checkerboard checkerboard(8, 8);

Camera camera;

Ball balls[] = {

Ball(1, GREEN, 7, 6, 1),

Ball(1.5, MAGENTA, 6, 3, 4),

Ball(0.4, WHITE, 5, 1, 7)

};

void init() {

glEnable(GL_DEPTH_TEST);

glLightfv(GL_LIGHT0, GL_DIFFUSE, WHITE);

glLightfv(GL_LIGHT0, GL_SPECULAR, WHITE);

glMaterialfv(GL_FRONT, GL_SPECULAR, WHITE);

glMaterialf(GL_FRONT, GL_SHININESS, 30);

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.centerx(), 0.0, checkerboard.centerz(),

0.0, 1.0, 0.0);

checkerboard.draw();

for (int i = 0; i < sizeof balls / sizeof(Ball); i++) {

balls[i].update();

glFlush();

glutSwapBuffers();

void reshape(GLint w, GLint h) {

glViewport(0, 0, w, h);

glMatrixMode(GL_PROJECTION);

glLoadIdentity();

gluPerspective(40.0, GLfloat(w) / GLfloat(h), 1.0, 150.0);

glMatrixMode(GL_MODELVIEW);

void timer(int v) {

glutPostRedisplay();

glutTimerFunc(1000/60, timer, v);

void special(int key, int, int) {

switch (key) {

case GLUT_KEY_LEFT: camera.moveLeft(); break;

case GLUT_KEY_RIGHT: camera.moveRight(); break;

case GLUT_KEY_UP: camera.moveUp(); break;


3D BOUNCING BALL
case GLUT_KEY_DOWN: camera.moveDown(); break;

glutPostRedisplay();

int main(int argc, char** argv) {

glutInit(&argc, argv);

glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);

glutInitWindowPosition(80, 80);

glutInitWindowSize(800, 600);

glutCreateWindow("3D Bouncing Balls");

glutDisplayFunc(display);

glutReshapeFunc(reshape);

glutSpecialFunc(special);

glutTimerFunc(100, timer, 0);

init();

glutMainLoop();

CHAPTER 7

SNAPSHOTS
3D BOUNCING BALL

Figure 7.1
Rotating
View of 3D
Bouncing
Ball

Figure 7.2 Side View of 3D Bouncing Ball


3D BOUNCING BALL

Figure 7.3 Zooming In 3D Bouncing Ball

Figure 7.4 Zooming Out 3D Bouncing Ball


3D BOUNCING BALL

Figure 7.5 Stopping The Balls on Air

Figure 7.6 Viewing Shining Surface in 3D Bouncing Ball


3D BOUNCING BALL
CHAPTER 8

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:

1) Getting Started with OpenGL -GeeksforGeeks

Link :- https://www.geeksforgeeks.org/opengl-program-simple-ball-game/?ref=lbp

2)Video Tutorial

Link :- https://youtu.be/jOVdMTl4mII

3)Dev C++ Video Tutorial

Link:- https://youtu.be/ARGR-rk1h24

4)OpenGL Programming Guide

Link :- https://www.glprogramming.com/red/

5)C++ Programming for OpenGL 3D Transformation

Link :- https://youtu.be/45MIykWJ-C4

You might also like