0% found this document useful (0 votes)
32 views44 pages

Simple Particle

Uploaded by

gowdarachanam21
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)
32 views44 pages

Simple Particle

Uploaded by

gowdarachanam21
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

Simple Particle System 2021-22

CHAPTER 1

INTRODUCTION

1.1 About Computer Graphics


Computer Graphics are graphics created using computer and more generally the
representation and manipulation of image data by computer with the help of specialized
hardware and software.
Computer Graphics is concerned with all aspects of producing pictures or images
using a computer. We can picturize all the real world objects using computer graphics.
We can model all real world objects, render them, give various material properties for
them, and create animations using computer graphics. Computer graphics today is
largely interactive: the user controls the contents, structure and appearance of the objects
and their displayed images by using input devices such as keyboard, mouse. User can
select command by pull down menu, pop-up screen etc. There are many graphics
software and APIs and one such API is OpenGL.

1.1.1 Applications of Computer Graphics


The development of computer graphics has been driven both by the needs of the
user community and by advances in hardware and software. The applications of
computer graphics are many and varied. However, they are divide them into four major
areas:
a. Display of information
b. Design
c. Simulation and Animations
d. User Interfaces

Dept of CSE, GEC- Page 1


Simple Particle System 2021-22

1.2 About OpenGL


OpenGL is graphics software system which has become widely accepted for
developing graphics application. OpenGL enables top down approach. It provides ease to
the programmer as most of the functions are inbuilt. OpenGL has most widely used and
supported 2D and 3D graphics application programming interface (API).
OpenGL is easy to learn and it possesses most of the characteristics of other popular
graphical systems. The libraries provided by OpenGL interface are OpenGL core,
OpenGL utility library, OpenGL utility toolkit. We want to start writing, as quickly as
possible, application program that will generate graphical output.

1.3 About the Project


This mini project “A SIMPLE PARTICLE SYSTEM” shows particles bounce on the
spheres and then bounce down on the rotatable bottom .The particles fall on the spheres,
the number of spheres can be increased up to three spheres .we can change the active
spheres ,and few transformations are applied on the active sphere. It has a maximum of
10,000 particles flowing ,and they either fall on the spheres in waterfall pattern or fountain
pattern, and also increase or decrease the flow of the particle ,and increase or decrease the
size of the particle. Keyboard event and mouse event has been implemented in this
project. We included additional operations like translations, scaling and other basic
geometric primitive. Primitives may have many options like dots, lines, triangles,
polygons etc.. Thus the project designed here gives a graphical representation of three
spheres and particles bouncing on it. The project has been designed using C++ as language
with OpenGL API.

Dept of CSE, GEC- Page 2


Simple Particle System 2021-22

CHAPTER 2
REQUIREMENT SPECIFICATION

To perform this project in graphics using OpenGL, certain hardware and


software requirements are required by the system. These hardware and software
requirements are listed as below:

2.1 Hardware Requirements


Processor : Pentium 4
RAM : 1 GB DDR with 512 MHz
Hard Disk Drive : 40 GB
Moniter : 16 bit Color Moniter

2.2 Software Requirements


Operating System : Windows XP, Windows 7 Ultimate
Language : Visual C++ with OpenGL API
Compiler : GNU GCC Compiler/C++ Compiler
Library Files : glut32.h, OpenGL32.h & glu32.h
Software : Codeblock 17.2 or Visual studio 2015

Dept of CSE, GEC- Page 3


Simple Particle System 2021-22

CHAPTER 3

DESIGN
3.1 Design Phase
In the design phase the software requirements are transformed into definitions of
software components and their interfaces, to establish the framework of the software. The
system design is about the system’s behavioral design affecting the design of system
components.
The essence of software design is making decisions about the logical organization of
the software. Sometimes we represent this logical organization as a model in a defined
modeling language. Sometimes we simply use informal notations and sketches to represent
the design.
The purpose of this phase is to develop the concept and basic framework for the
design of the project. This is the most existing and creative part of the project. This phase
starts with the requirement document delivered by the requirement phase and maps the
requirements into architecture.
The design which represents the working of the nuclear power plant is shown. The
complete overview of the project is shown in the block diagram; it shows how it will perform
the actions through user interaction.

Dept of CSE, GEC- Page 4


Simple Particle System 2021-22

The flow if execution id depicted in the flowing block diagram.

Simple particle system

User Interface

Key board

Mouse

‘[-]’ ‘[+]’ ‘p’ ‘P’ ‘1’ ‘#’ ‘~’


pressed pressed pressed pressed presse pressd pressed

‘t’
spray ‘s’
colli less sma larg tog togg toggl
more
pressed
type(wa pressed
sio n le e full
flow flow ll er er gle
ter fall/ sphe of Figureof3.1 Designpart
of the project
par poi fra scree
fountai m n
res ticl nts
parti ic
spray type(water fall/

fountain) collision spheres.

less flow of

particles. more flow

of particles. smaller
Right click

particles. larger

particles.

toggle points/line
toggle frame rate ON/OFF toggle

Dept of CSE, GEC- Page 5


Simple Particle System 2021-22

The Figure 3.1 shows the working of the project. The input is given with the help of
keyboard. By pressing various keys we come to know how the woman is crossing the river.
The design depicted above specifies the initial model of the project. It is well defined
and uses motion functions to provide movement. Also it is made interactive with the user
through the input device, i.e. keyboard and mouse. The various keys and menu provide
different options to the user. The other key functions are:
‘f’=fog on/off
‘t’=spray type(water fall/ fountain)
‘s’=collision spheres.
‘[-]’=less flow of particles.
‘[+]’=more flow of particles.
‘p’=smaller particles.
‘P’=larger particles.
‘l’=toggle points/line
‘#’=toggle framerate ON/OFF.
‘~’=toggle fullscreen ON/OFF.
‘[!]’=change active spheres.
[Esc]=quit.
Use these numeric keys to move the active sphere
‘1’=move the sphere back.
‘2’=move the sphere down.
‘3’=moves the sphere front.
‘4’=move the sphere right.
‘6’=move the sphere left.
‘7’=decreases the size of the active sphere.
‘8’=move the sphere up.
‘9’=increases the size of active sphere.

Dept of CSE, GEC- Page 6


Simple Particle System 2021-22

CHAPTER
4
IMPLEMENTATION

Implementation is third phase of the project. In this phase the project inception
review is done and the activity schedule is prepared. The pseudo code is a basic idea of a
program and the flowchart provides a pictorial representation of the program. The
pseudo code, flowchart and the functions that are used in the program are discussed
below:

4.1 Algorithm
Algorithm is a step by step procedure to solve the problem.
Step1: start ()
Step2: display();
Step3: Alphabetical functions
‘f’=fog on/off
‘t’=spray type(water fall/ fountain)
‘s’=collision spheres.
‘[-]’=less flow of particles.
‘[+]’=more flow of particles.
‘p’=smaller particles.
‘P’=larger particles.
‘l’=toggle points/line
‘#’=toggle framerate ON/OFF.
‘~’=toggle fullscreen ON/OFF.
‘[!]’=change active spheres.
[Esc]=quit.

Dept of CSE, GEC- Page 7


Simple Particle System 2021-22

Step4: Use these numeric keys to move the active sphere


‘1’=move the sphere back.
‘2’=move the sphere down.
‘3’=moves the sphere front.
‘4’=move the sphere right.
‘6’=move the sphere left.
‘7’=decreases the size of the active sphere.
‘8’=move the sphere up.
‘9’=increases the size of active sphere.

4.2 Pseudo Code

Pseudo code is a compact and informal high level description of computer


programming algorithm. The execution of the program starts from the main function. It calls
various inbuilt and user defined functions, the pseudo code is as follows:

Void main ()
{
The program execution begins from the first statement of this function.
It also calls various other functions defined in the program and do the
following: Initializes the argument counter argc and argument vector array argv.
Sets the parameter required for window.
Calls display function.
Calls init function.
Calls Keyboard function.
Calls Mouse function.
}
Void init ()
{
Performs init functions
}
Void display ()

Dept of CSE, GEC- Page 8


Simple Particle System 2021-22

{
Initially, the buffers are cleared using glClear ();
Displays rasterized text
}
Void mouse ()
{
When right mouse button is clicked, it gives the menu options
}
Void keys ()
{
If ‘1’ is pressed, it moves the sphere back.
If ‘2’ is pressed, it moves the sphere down.
If ‘3’ is pressed, it moves the sphere front.
If ‘4’ is pressed, it moves the sphere right.
If ‘6’ is pressed, it moves the sphere left.
If ‘7’ is pressed, it decreases the size of the active sphere.
If ‘8’ is pressed moves the sphere up.
If ‘9’ is pressed increases the size of active sphere.
}

4.3 FUNCTIONS
The implementation of the project requires the usage of different kinds of
functions. Functions can either be inbuilt or defined by the user. The functions that are
used in the program are discovered below. This section contains brief description of all
the headers and functions. These functions are as follows:

4.3.1 Headers Defined


The in-built functions are defined in the OpenGL library. Some of the headers
that are used are as follows:
 #include<stdio.h> : to take input from standard input and
write to standard output

Dept of CSE, GEC- Page 9


Simple Particle System 2021-22

 #include<stdlib.h> : to include standard library functions


 #include<GL/glut.h>: to include glut library files

4.3.2 Inbuilt Functions


OpenGL functions used in the code are as follows:
 glClearColor (GLclampf r, GLclampf g, GLclampf b, GLclamp a)
Specifies clear values to the color buffers and clears the display
before redrawing it. It specifies the red, green, blue and alpha values used
to clear the buffers. Values specified is in the range of [0, 1].
 glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
Clears buffer to preset values. Specifies BITWISE OR of masks that
indicate the buffers to be cleared.
 Void glutDisplayFunc(void(*func)(void))
It is the function that displays the primitives onto the screen by calling
the user defined functions. It registers the display function callback be
executed after the current callback when window needs to be redrawn
 glFlush()
Forces any buffered OpenGL commands to execute. Sets keyboard call
back to current window.
 glBegin (glEnum mode)
Initiates a new primitive of type mode and starts collection of vertices.
Values of mode include GL_POINTS, GL_LINES and GL_POLYGON.
 void glEnd()
Terminates a list of vertices.

 void glutInit ( )
All Initializes the GLUT. The arguments from main are passed in and can
be used by the application.
 void glutCreateWindow( )
Creates a window on the display. The string title can be used to label the
window.
 void glutInitDisplayMode()

Dept of CSE, GEC- Page


Simple Particle System 2021-22

Requests a display with the properties in mode. The mode is determined


by the logical OR of options including the color model.
 Void glutInitWindowSize( )
Specifies the initial height and width of the window in pixels.
 void glutInitwindowPosition(int x, int y)
Specifies the initial position of the top-left corner of the window in pixels.
 void glutMainLoop()
Causes the program to enter an event-processing loop. It should be the last
statement main.
 void glMatrixMode( GLenum mode)
Specify matrix will be affected by subsequent transformation mode can be
GL_MODELVIEW, GL_PROJECTION.
 void glutBitmapCharacter(void *font, int character)
Without using any display lists, glutBitmapCharacter renders the character
in the named bitmap font. Font: Bitmap font to use. Character: Character to
render (not confined to 8 bits). It is used to display the rasterized text on
screen.
 void glutKeyboardFunc()
It is a user interactive function which displays snapshots on hitting the
appropriate keyboard keys onto the display screen.
 void glutMouseFunc ()
It is a user interactive function which displays snapshots on clicking
the appropriate mouse button onto the display screen.
 void glLoadIdentity()
Sets the current transformations matrix to an identity matrix.

4.3.3 User defined functions

 timedelta(void);
This function returns the number of seconds that have elapsed since the
previous call to the function.
 void text( );
This function draws a string of text with an 18 point Helvetica bitmap font at
position(x,y) in window space.

Dept of CSE, GEC- Page


Simple Particle System 2021-22

 int fequal( );
Customized equals ("==") operation for floating point value.
 void psTimestep( );
Updates velocity and position of particles by a small value delta.
 void psNewparticle( );
Initializes the velocity, position,dampening and related information of
particles based on the particle type.
 void psBounce( );
psBounce: the particle has gone past (or exactly hit) the ground
plane, so calculate the time at which the particle actually
intersected the ground plane (s). essentially, this just rolls
back time to when the particle hit the ground plane, then starts
time again from then.
- - o A (previous position)
| | \
| s \ o (position it _should_ be at) -
t | \/ |t-s
| - ------X-------- -
| \
- o B (new position)
A + V*s = 0 or s = -A/V
to calculate where the particle should be:

A + V*t + V*(t-s)*d
where d is a damping factor which accounts for the loss
of energy due to the bounce.

 void visible(int state)


It is the callback function for GLUT_VISIBLE_FUNC
sets the visibility callback for the current window. The visibility callback for a
window is called when the visibility of a window changes. The state callback
parameter is either GLUT_NOT_VISIBLE or GLUT_VISIBLE depending on the
current visibility of the window. GLUT_VISIBLE does not distinguish a window

Dept of CSE, GEC- Page


Simple Particle System 2021-22

being totally versus partially visible. GLUT_NOT_VISIBLE means no part of the


window is visible, i.e., until the window's visibility changes, all further rendering
to the window is discarded.

 void bail(int code)


De-allocation of memory assigned to particles.
 void ss_mouse( )
Frees memory on mouse click by calling bail().

 Void mydisplay();
This function displays the front screen.
 void key(unsigned char key,int x,int y);
This function specifies various keys for different movements.

 void display();
This function displays various objects on the screen.

Dept of CSE, GEC- Page


Simple Particle System 2021-22

4.4 FLOW CHART

Sta

Main
()
Init ()

Display ()

User Functions

Keyboard Functions

If ‘f’ is If ‘t’ is If ‘[s]’ is If ‘-’ is pressed


pressed
pressed pressed pressed
press pressed

Spray type Adds the Decrease ^


collision the flow of
(waterfall
spheres ,m particles
/fountain)
ax up to
‘3’

Fog ON/OF

Dept of CSE, GEC- Page


Simple Particle System 2021-22

If ‘p’ is If ‘P’ is If ‘l’ is If ‘#’ is If ‘~’ is


pressed. pressed
pressed pressed pressed

It reduces It increases Toggle Toggle Toggle


the size of the size of points/lin frame rate full
the particle. the particle. es. ON/OFF. screen
ON/OFF.

If ‘!’ is If ‘esc’ is If ‘1’ is If ‘2’ is


pressed
pressed pressed pressed
^

Change Quit .
The active The active
active
sphere sphere
sphere.
moves back moves
down.

Dept of CSE, GEC- Page


Simple Particle System 2021-22

If ‘3’ is If ‘6’ is
If ‘4’ is If ‘7’ is

pressed pressed
If ‘f’ pressed pressed

The active sphere


The active sphere moves front. moves
The activeright.(system)
sphere movesDecreases the size of the active sphere.
left.(system)

If ‘8’ is
If ‘9’ is
pressed
pressed

The active sphere is moved up. the size of the active sphere.
Increases

Figure 4.4 Flow chart

Dept of CSE, GEC- Page


Simple Particle System 2021-22

CHAPTER 5

TESTING
5.1 Introduction to testing
Verification and validation is a generic name given to checking process which
ensures that the software confirms to its specifications and meets the demands of users.
 Validation
Are we building the right product?
Validation involves checking the program that the program has implanted
meets the requirement of the users.

 Verification
Are we building the right product?
Verification involves checking that program confirms to its specification.

5.2 Stages in the Implementation of Testing

 Unit testing
Each individual unit is tested for correctness. These individual components will
be tested to ensure that they operate correctly.

 System testing
The Sub-systems are integrated to make up the entire system. The errors that
result from unanticipated interaction between sub-systems and system components are
removed.

Dept of CSE, GEC- Page


Simple Particle System 2021-22

 Integration testing
This phase in software testing in which individual software modules are
combined and tested as a group. It occurs after unit testing and before validation
testing. Integration testing takes as its input modules that have been unit tested,
groups them in larger aggregates, applies tests defined in an integration test plan to
those aggregates, and delivers as its output the integrated system ready for system
testing.

 User acceptance testing


This is the final stage in the testing process before the system is tested for
operational use. Any requirement problem or requirement definition problem revealed
from acceptance testing are considered and made error free.

 Test plan
Careful planning is needed to the most of testing and controlled testing cost.

Dept of CSE, GEC- Page


Simple Particle System 2021-22

5.3 Test case

Test Expected
case Test case description Input Actual output output Remark
Id
A simple particle system Press key Waterfall flow Waterfall
1 (with spray type ‘t’ of particles. flow of Pass
(waterfall)) [Figure 5.1(a)] particles has
to happen
A simple particle system Press key Fountain flow Fountain flow
2 (with spray ‘t’ of particles. of particles Pass
type(fountain) [Figure 5.1(b)] has to happen
Shows the “toggle Press key Particles will be Particles has
points or lines” ‘l’ in points or to be in points
3 lines. [Figure or lines Pass
5.2(a), 5.2(b)]

Shows the increase in Press key More flow of More flow of


4 flow ‘+’ particles. particles has Pass
[Figure 5.3(b)] to happen
Shows the decrease in Press key Less flow of Less flow of
5 flow ‘-’ particles. particles has Pass
[Figure 5.3(a)] to happen
shows the toggle frame Press key Frame rate Frame rate
rate OFF/ON ‘[#]’ displayed at the has to be
6 left bottom of displayed at Pass
the screen. the left
[Figure 5.4] bottom of the
screen

Dept of CSE, GEC- Page


Simple Particle System 2021-22

7 Enable full screen Press key Full screen will Full screen
ON/OFF ‘[~]’ be enabled. has to be Pass
[Figure 5.5] enabled

Shows a simple particle Press left Simple particle Simple


8
system with a rotatable button system can be particle

bottom, which helps us and move viewed in any system must

to view the system from the cursor direction. be viewed in Pass


any direction. (mouse [Figure 5.6] any direction.

function)

Table 5.3 Test cases

Dept of CSE, GEC- Page


Simple Particle System 2021-22

CHAPTER 6
PROJECT CODE AND SNAPSHOT

Project Code:
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <GL/glut.h>
#ifdef _WIN32
#define drand48() ((float)rand()/RAND_MAX)
#endif
/* #define SCREEN_SAVER_MODE */

#define PS_GRAVITY -9.8


#define PS_WATERFALL 0
#define PS_FOUNTAIN 1

typedef struct {
float x, y, z;
float radius;
} PSsphere;

typedef struct {
float position[3]; /* current position */
float previous[3]; /* previous position */
float velocity[3]; /* velocity (magnitude & direction) */
float dampening; /* % of energy lost on collision
*/ int alive; /* is this particle alive? */
} PSparticle;

PSparticle* particles = NULL;


#define NUM_SPHERES 3
PSsphere spheres[NUM_SPHERES] = { /* position of spheres */
{ -0.1, 0.6, 0, 0.4 },
{ -0.7, 1.4, 0, 0.25 },

Dept of CSE, GEC- Page


Simple Particle System 2021-22

{ 0.1, 1.5, 0.1, 0.3 },


};
int num_particles = 10000;
int type = PS_WATERFALL;
int points = 1;
int draw_spheres = 1;
int frame_rate = 1;
float frame_time = 0;
float flow = 500;
float slow_down = 1;

float spin_x = 0;
float spin_y = 0;
int point_size = 4;
/* timedelta: returns the number of seconds that have elapsed since
the previous call to the function. */
#if defined(_WIN32)
#include <sys/timeb.h>
#else
#include <limits.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/times.h>
#endif
#ifndef CLK_TCK
#define CLK_TCK 1000
#endif
float
timedelta(void)
{
static long begin = 0;
static long finish, difference;

#if defined(_WIN32)
static struct timeb tb;
ftime(&tb);
finish = tb.time*1000+tb.millitm;
#else
static struct tms tb;

Dept of CSE, GEC- Page


Simple Particle System 2021-22

finish = times(&tb);
#endif

difference = finish - begin;


begin = finish;

return (float)difference/(float)CLK_TCK;
}
/* text: draws a string of text with an 18 point helvetica bitmap font
at position (x,y) in window space (bottom left corner is (0,0). */
void
text(int x, int y, char* s)
{
int lines;
char* p;

glDisable(GL_DEPTH_TEST);
glDisable(GL_FOG);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glOrtho(0, glutGet(GLUT_WINDOW_WIDTH),
0, glutGet(GLUT_WINDOW_HEIGHT), -1, 1);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glColor3ub(0, 0, 0);
glRasterPos2i(x+1, y-1);
for(p = s, lines = 0; *p; p++) {
if (*p == '\n') {
lines++;
glRasterPos2i(x+1, y-1-(lines*18));
}
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, *p);
}
glColor3ub(128, 0, 255);
glRasterPos2i(x, y);
for(p = s, lines = 0; *p; p++) {
if (*p == '\n') {

Dept of CSE, GEC- Page


Simple Particle System 2021-22

lines++;
glRasterPos2i(x, y-(lines*18));
}
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, *p);
}
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
glEnable(GL_FOG);
glEnable(GL_DEPTH_TEST);
}
int
fequal(float a, float b)
{
float epsilon = 0.1;
float f = a - b;
if (f < epsilon && f > -epsilon)
return 1;
else
return 0;
}
void
psTimeStep(PSparticle* p, float dt)
{
if (p->alive == 0)
return;

p->velocity[0] += 0;
p->velocity[1] += PS_GRAVITY*dt;
p->velocity[2] += 0;

p->previous[0] = p->position[0];
p->previous[1] = p->position[1];
p->previous[2] = p->position[2];

p->position[0] += p->velocity[0]*dt;
p->position[1] += p->velocity[1]*dt;
p->position[2] += p->velocity[2]*dt;

Dept of CSE, GEC- Page


Simple Particle System 2021-22

}
void
psNewParticle(PSparticle* p, float dt)
{
if (type == PS_WATERFALL) {
p->velocity[0] = -2*(drand48()-0.0);
p->velocity[1] = 0;
p->velocity[2] = 0.5*(drand48()-0.0);
p->position[0] = 0;
p->position[1] = 2;
p->position[2] = 0;
p->previous[0] = p->position[0];
p->previous[1] = p->position[1];
p->previous[2] = p->position[2];
p->dampening = 0.45*drand48();
p->alive = 1;
} else if (type == PS_FOUNTAIN) {
p->velocity[0] = 2*(drand48()-0.5);
p->velocity[1] = 5;
p->velocity[2] = 2*(drand48()-0.5);
p->position[0] = -0.1;
p->position[1] = 0.9;
p->position[2] = 0;
p->previous[0] = p->position[0];
p->previous[1] = p->position[1];
p->previous[2] = p->position[2];
p->dampening = 0.35*drand48();
p->alive = 1;
}
psTimeStep(p, 2*dt*drand48());
}
/* psBounce: the particle has gone past (or exactly hit) the ground
plane, so calculate the time at which the particle actually
intersected the ground plane (s). essentially, this just rolls
back time to when the particle hit the ground plane, then starts
time again from then.
- - o A (previous position)
| | \
| s \ o (position it _should_ be at) -

Dept of CSE, GEC- Page


Simple Particle System 2021-22

t | \/ |t-s
| - ------X-------- -
| \
- o B (new position)

A + V*s = 0 or s = -A/V

to calculate where the particle should be:

A + V*t + V*(t-s)*d

where d is a damping factor which accounts for the loss


of energy due to the bounce. */
void
psBounce(PSparticle* p, float dt)
{
float s;

if (p->alive == 0)
return;
/* since we know it is the ground plane, we only need to
calculate s for a single dimension. */
s = -p->previous[1]/p->velocity[1];

p->position[0] = (p->previous[0] + p->velocity[0] * s +


p->velocity[0] * (dt-s) * p->dampening);
p->position[1] = -p->velocity[1] * (dt-s) * p->dampening; /* reflect */
p->position[2] = (p->previous[2] + p->velocity[2] * s +
p->velocity[2] * (dt-s) * p->dampening);
/* damp the reflected velocity (since the particle hit something,
it lost some energy) */
p->velocity[0] *= p->dampening;
p->velocity[1] *= -p->dampening; /* reflect */
p->velocity[2] *= p->dampening;
}
void
psCollideSphere(PSparticle* p, PSsphere* s)
{
float vx = p->position[0] - s->x;

Dept of CSE, GEC- Page


Simple Particle System 2021-22

float vy = p->position[1] - s->y;


float vz = p->position[2] - s->z;
float distance;
if (p->alive == 0)
return;
distance = sqrt(vx*vx + vy*vy + vz*vz);
if (distance < s->radius) {
#if 0
vx /= distance; vy /= distance; vz /= distance;
d = 2*(-vx*p->velocity[0] + -vy*p->velocity[1] + -vz*p-
>velocity[2]);
p->velocity[0] += vx*d*2;
p->velocity[1] += vy*d*2;
p->velocity[2] += vz*d*2;
d = sqrt(p->velocity[0]*p->velocity[0] +
p->velocity[1]*p->velocity[1] +
p->velocity[2]*p->velocity[2]);
p->velocity[0] /= d;
p->velocity[1] /= d;
p->velocity[2] /= d;
#else
p->position[0] = s->x+(vx/distance)*s->radius;
p->position[1] = s->y+(vy/distance)*s->radius;
p->position[2] = s->z+(vz/distance)*s->radius;
p->previous[0] = p->position[0];
p->previous[1] = p->position[1];
p->previous[2] = p->position[2];
p->velocity[0] = vx/distance;
p->velocity[1] =
vy/distance; p->velocity[2]
#endif= vz/distance;
}
}
void
reshape(int width, int height)
{
float black[] = { 0, 0, 0, 0 };

glViewport(0, 0, width, height);

Dept of CSE, GEC- Page


Simple Particle System 2021-22

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60, (float)width/height, 0.1, 1000);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0, 1, 3, 0, 1, 0, 0, 1, 0);
glFogfv(GL_FOG_COLOR, black);
glFogf(GL_FOG_START, 2.5);
glFogf(GL_FOG_END, 4);
glEnable(GL_FOG);
glFogi(GL_FOG_MODE, GL_LINEAR);
glPointSize(point_size);
glEnable(GL_POINT_SMOOTH);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHT0);

timedelta();
}
void
display(void)
{
static int i;
static float c;
static int j = 0;
static char s[32];
static int frames = 0;
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(spin_y, 1, 0, 0);
glRotatef(spin_x, 0, 1, 0);
glEnable(GL_LIGHTING);
if (draw_spheres) {
for (i = 0; i < draw_spheres; i++) {
glPushMatrix();
glTranslatef(spheres[i].x, spheres[i].y, spheres[i].z);
glColor3ub(0, 255, 128);

Dept of CSE, GEC- Page


Simple Particle System 2021-22

glutSolidSphere(spheres[i].radius, 16, 16);


glPopMatrix();
}
}
glDisable(GL_LIGHTING);
glBegin(GL_QUADS);
glColor3ub(0, 128, 255);
glVertex3f(-2, 0, -2);
glVertex3f(-2, 0, 2);
glVertex3f(2, 0, 2);
glVertex3f(2, 0, -2);
glEnd();

if (points) {
glBegin(GL_POINTS);
for (i = 0; i < num_particles; i++) {
if (particles[i].alive == 0)
continue;
c = particles[i].position[1]/2.1*255;
glColor3ub(c, 128+c*0.5, 255);
glVertex3fv(particles[i].position);
}
glEnd();
} else {
glBegin(GL_LINES);
for (i = 0; i < num_particles; i++) {
if (particles[i].alive == 0)
continue;
c = particles[i].previous[1]/2.1*255;
glColor4ub(c, 128+c*0.5, 255, 32);
glVertex3fv(particles[i].previous);
c = particles[i].position[1]/2.1*255;
glColor4ub(c, 128+c*0.5, 255, 196);
glVertex3fv(particles[i].position);
}
glEnd();
}
/* spit out frame rate. */
if (frame_rate) {

Dept of CSE, GEC- Page


Simple Particle System 2021-22

frames++;
if (frames > 7) {
sprintf(s, "%g fps", (float)7/frame_time);
frame_time = 0;
frames = 0;
}
text(5, 5, s);
}

glPopMatrix();
glutSwapBuffers();
}
void
idle(void)
{
static int i, j;
static int living = 0; /* index to end of live particles
*/ static float dt;
static float last = 0;

dt = timedelta();
frame_time += dt;
#if 1
/* slow the simulation if we can't keep the frame rate up around
10 fps */
if (dt > 0.1) {
slow_down = 1.0/(100*dt);
} else if (dt < 0.1) {
slow_down = 1;
}
#endif
dt *= slow_down;
/* resurrect a few particles */
for (i = 0; i < flow*dt; i++) {
psNewParticle(&particles[living], dt);
living++;
if (living >= num_particles)
living = 0;
}

Dept of CSE, GEC- Page


Simple Particle System 2021-22

for (i = 0; i < num_particles; i++) {


psTimeStep(&particles[i], dt);

/* collision with sphere? */


if (draw_spheres) {
for (j = 0; j < draw_spheres; j++) {
psCollideSphere(&particles[i], &spheres[j]);
}
}
/* collision with ground? */
if (particles[i].position[1] <= 0) {
psBounce(&particles[i], dt);
}
/* dead particle? */
if (particles[i].position[1] < 0.1 &&
fequal(particles[i].velocity[1], 0)) {
particles[i].alive = 0;
}
}
glutPostRedisplay();
}
void
visible(int state)
{
if (state == GLUT_VISIBLE)
glutIdleFunc(idle);
else
glutIdleFunc(NULL);
}
void
bail(int code)
{
free(particles);
exit(code);
}
#ifdef SCREEN_SAVER_MODE
void
ss_keyboard(char key, int x, int y)
{

Dept of CSE, GEC- Page


Simple Particle System 2021-22

bail(0);
}
void
ss_mouse(int button, int state, int x, int y)
{
bail(0);
}
void
ss_passive(int x, int y)
{
static int been_here = 0;
/* for some reason, GLUT sends an initial passive motion callback
when a window is initialized, so this would immediately
terminate the program. to get around this, see if we've been
here before. (actually if we've been here twice.) */
if (been_here > 1)
bail(0);
been_here++;
}
#else
void
keyboard(unsigned char key, int x, int y)
{
static int fullscreen = 0;
static int old_x = 50;
static int old_y = 50;
static int old_width = 512;
static int old_height = 512;
static int s = 0;
switch (key) {
case 27:
bail(0);
break;

case 't':
if (type == PS_WATERFALL)
type = PS_FOUNTAIN;
else if (type == PS_FOUNTAIN)
type = PS_WATERFALL;

Dept of CSE, GEC- Page


Simple Particle System 2021-22

break;
case 's':
draw_spheres++;
if (draw_spheres > NUM_SPHERES)
draw_spheres = 0;
break;
case 'S':
printf("PSsphere spheres[NUM_SPHERES] = {/* position of spheres
*/\n");
for (s = 0; s < NUM_SPHERES; s++) {
printf(" { %g, %g, %g, %g },\n",
spheres[s].x, spheres[s].y,
spheres[s].z, spheres[s].radius);
}
printf("};\n");
break;
case 'l':
points = !points;
break;

case 'P':
point_size++;
glPointSize(point_size);
break;

case 'p':
point_size--;
if (point_size < 1)
point_size = 1;
glPointSize(point_size);
break;

case '+':
flow += 100;
if (flow > num_particles)
flow = num_particles;
printf("%g particles/second\n", flow);
break;

Dept of CSE, GEC- Page


Simple Particle System 2021-22

case '-':
flow -= 100;
if (flow < 0)
flow = 0;
printf("%g particles/second\n", flow);
break;

case '#':
frame_rate = !frame_rate;
break;

case '~':
fullscreen = !fullscreen;
if (fullscreen) {
old_x = glutGet(GLUT_WINDOW_X);
old_y = glutGet(GLUT_WINDOW_Y);
old_width = glutGet(GLUT_WINDOW_WIDTH);
old_height = glutGet(GLUT_WINDOW_HEIGHT);
glutFullScreen();
} else {
glutReshapeWindow(old_width, old_height);
glutPositionWindow(old_x, old_y);
}
break;

case '!':
s++;
if (s >= NUM_SPHERES)
s = 0;
break;

case '4':
spheres[s].x -= 0.05;
break;

case '6':
spheres[s].x += 0.05;
break;

Dept of CSE, GEC- Page


Simple Particle System 2021-22

case '2':
spheres[s].y -= 0.05;
break;

case '8':
spheres[s].y += 0.05;
break;

case '7':
spheres[s].z -= 0.05;
break;

case '3':
spheres[s].z += 0.05;
break;

case '9':
spheres[s].radius += 0.05;
break;

case '1':
spheres[s].radius -= 0.05;
break;
}
}
void
menu(int item)
{
keyboard((unsigned char)item, 0, 0);
}
void
menustate(int state)
{
/* hook up a fake time delta to avoid jumping when menu comes up */
if (state == GLUT_MENU_NOT_IN_USE)
timedelta();
}
#endif
int old_x, old_y;

Dept of CSE, GEC- Page


Simple Particle System 2021-22

void
mouse(int button, int state, int x, int y)
{
old_x = x;
old_y = y;
glutPostRedisplay();
}
void
motion(int x, int y)
{
spin_x = x - old_x;
spin_y = y - old_y;
glutPostRedisplay();
}
int
main(int argc, char** argv)
{
glutInitDisplayMode(GLUT_RGB|GLUT_DEPTH|GLUT_DOUBLE);
glutInitWindowPosition(0, 0);
glutInitWindowSize(640, 480);
glutInit(&argc, argv);

glutCreateWindow("Particles");
glutDisplayFunc(display);
glutReshapeFunc(reshape);

#ifdef SCREEN_SAVER_MODE
glutPassiveMotionFunc(ss_passive);
glutKeyboardFunc(ss_keyboard);
glutMouseFunc(ss_mouse);
glutSetCursor(GLUT_CURSOR_NONE);
glutFullScreen();
#else
glutMotionFunc(motion);
glutMouseFunc(mouse);
glutKeyboardFunc(keyboard);
#endif

glutMenuStateFunc(menustate);

Dept of CSE, GEC- Page


Simple Particle System 2021-22

glutCreateMenu(menu);
glutAddMenuEntry("Particle", 0);
glutAddMenuEntry("", 0);
glutAddMenuEntry("[f] Fog on/off", 'f');
glutAddMenuEntry("[t] Spray type", 't');
glutAddMenuEntry("[s] Collision spheres", 's');
glutAddMenuEntry("[-] Less flow", '-');
glutAddMenuEntry("[+] More flow", '+');
glutAddMenuEntry("[p] Smaller points", 'p');
glutAddMenuEntry("[P] Larger points", 'P');
glutAddMenuEntry("[l] Toggle points/lines", 'l');
glutAddMenuEntry("[#] Toggle framerate on/off", '#');
glutAddMenuEntry("[~] Toggle fullscreen on/off", '~');
glutAddMenuEntry("", 0);
glutAddMenuEntry("Use the numeric keypad to move the spheres", 0);
glutAddMenuEntry("[!] Change active sphere", 0);
glutAddMenuEntry("", 0);
glutAddMenuEntry("[Esc] Quit", 27);
glutAttachMenu(GLUT_RIGHT_BUTTON);

if (argc > 1) {
if (strcmp(argv[1], "-h") == 0) {
fprintf(stderr, "%s [particles] [flow] [speed%]\n", argv[0]);
exit(0);
}

sscanf(argv[1], "%d", &num_particles);


if (argc > 2)
sscanf(argv[2], "%f", &flow);
if (argc > 3)
sscanf(argv[3], "%f", &slow_down);
}

particles = (PSparticle*)malloc(sizeof(PSparticle) * num_particles);

glutVisibilityFunc(visible);
glutMainLoop();
return 0;
}

Dept of CSE, GEC- Page


Simple Particle System 2021-22

SNAPSHOTS:

Figure 5.1(a) A simple particle system(with spray type (waterfall) on pressing ‘t’.

Figure 5.1(b) A simple particle system with spray type(fountain) on pressing ‘t’.

Dept of CSE, GEC- Page


Simple Particle System 2021-22

Figure 5.2(a) Shows the “toggle points or lines” on pressing ‘l’(lines) .

Figure 5.2(b) Shows the “toggle points or lines” on pressing ‘l’(points).

Dept of CSE, GEC- Page


Simple Particle System 2021-22

Figure 5.3(a) Shows the decrease in particle flow on pressing ‘-’.

Figure 5.3(b) Shows the increase in particle flow on pressing ‘+’.

Dept of CSE, GEC- Page


Simple Particle System 2021-22

Figure 5.4 Shows the toggle frame rate OFF/ON on pressing [#].

Figure 5.5 Enable full screen ON/OFF on pressing [~].

Dept of CSE, GEC- Page


Simple Particle System 2021-22

Figure 5.6 Shows a simple particle system with a rotatable bottom,which helps us to view the
system from any direction (using mouse).

Dept of CSE, GEC- Page


Simple Particle System 2021-22

CHAPTER 7

CONCLUSION AND FUTURE ENHANCEMENTS

CONCLUSION:
The project has involved the designing of “SIMPLE PARTICLE SYSTEM”. It is
implemented using some in-built functions which are provided in the standards graphics
package. It even has a number of user defines functions.

The graphics system allows the programmer to decline pictures that include variety of
transformations. The project shows three spheres and particles bouncing on it. The
movements are done by using various keyboard functions and mouse functions which have
been successfully implemented.

FUTURE ENHANCEMENTS:
Further development in the project can be done by illustrating graphically some other
conditions other than what we have illustrated and some more advanced techniques that are
recently used can be implemented. It can be made still more attractive using 3D
implementation. The project is widely open to anyone who wishes to improve it further on
graphical way. We can modify the project by adding some features in the future. The project
can further be implemented by changing the colors, by adding more operations and functions.
Many other keyboard and mouse functions could be given.

Dept of CSE, GEC- Page


Simple Particle System 2021-22

BIBLIOGRAPHY

BOOKS
 Edward Angel, 2009, Interactive computer graphics A Top-Down approach with
OpenGL, 5TH EDITION, Addison-Wesley.

 Blinn, J.F., and M.E. Newell, 1996, Texture and Reflection in Computer
Generated Images, CACM.

WEBSITES

 https://www.opengl.org
 http://www.glprogramming.com
 http://bluelotusrajeev.hubpages.com/hub

Dept of CSE, GEC- Page

You might also like