AAAAAAA Removed

Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

Simple Particle System 2023-24

CHAPTER 4

REQUIREMENTS 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:

4.1 SOFTWARE REQUIREMENTS

A. Operating System: Windows XP, Windows 10


B. Language : Visual C++ with OpenGL API
C. Compiler : GNU GCC Compiler/C++ Compiler
D. Library Files : glut32.h, OpenGL32.h & glu32.h
E. Software : Codeblock 17.2 or Visual studio 2015

4.2 HARDWARE REQUIREMENTS


A. Processor: Pentium 4

B. RAM : 1 GB DDR with 512 MHz

C. Hard Disk Drive : 40 GB

D. Moniter : 16 bit Color Moniter

Dept. of CSE, RIT Hassan Page 13


1412
Simple Particle System 2023-24

CHAPTER 5

PROJECT DESCRIPTION

The simple particle system involves developing an interactive particle system to simulate natural
phenomena like fire, smoke, and water. The system initializes particles with properties such as position,
velocity, lifespan, and color, and updates these properties in real-time based on physics-based rules. Users
can interact with the system through various keyboard controls, allowing them to toggle fog effects,
change spray types, add collision spheres, adjust particle size and flow, and switch between different
rendering modes. The project aims to provide hands-on experience in graphics programming, enhance
understanding of particle dynamics, and explore optimization techniques for real-time simulation. Despite
its complexity and resource-intensive nature, this project offers valuable insights into creating dynamic
visual effects and managing performance in computer graphics applications.

5.1 GRAPHICS FUNCTIONS AND REQUIREMENT

Header Files:
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

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

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

Inbuilt Functions

• glClearColor (GLclampf r, GLclampf g, GLclampf b, GLclamp a)

• Specifies clear values to the color buffers and clears the display before redrawing it.

• glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)

• Clears buffer to preset values.

• Void glutDisplayFunc(void(*func)(void))

Dept. of CSE, RIT Hassan Page 13


1413
Simple Particle System 2023-24

• 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 beused by the application.

• void glutCreateWindow( )

• Creates a window on the display. The string title can be used to label the window.

• void glutInitDisplayMode()

• Requests a display with the properties in mode. 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 laststatement main.

• void glMatrixMode( GLenum mode)

• Specify matrix will be affected by subsequent transformation mode GL_PROJECTION.

• void glutBitmapCharacter(void *font, int character)

• void glutKeyboardFunc()

Dept. of CSE, RIT Hassan Page 13


1414
Simple Particle System 2023-24
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 space.

• 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

• void psBounce( );

• psBounce: the particle has gone past (or exactly hit) the groundplane .

• void psBounce( );

• psBounce the particle has gone past (or exactly hit) the ground
plane, so calculate the time at which the particle actually

5.2 IMPLEMENTATION
#include <windows.h>
#include <GL/glut.h>
#include "bits/stdc++.h"
#include "testlib.h" //Download this header file from
https://raw.githubusercontent.com/MikeMirzayanov/testlib/master/testlib.h
using namespace std;

string Title = "Clash of Cars";

bool collide;
bool game_over;
long long score;

const double EPS = 1e-6;

Dept. of CSE, RIT Hassan Page 13


1415
Simple Particle System 2023-24
const double Ortho_left = -24;
const double Ortho_right = 24;
const double Ortho_top = 32;
const double Ortho_bottom = 0;

const double Road_left = -12;


const double Road_right = 13;
const double Road_top = 32;
const double Road_bottom = 0;

class Point {
public:
double x, y, z;
Point() { x = y = z = 0; }
Point(double _x, double _y) { x = _x, y = _y, z = 0; }
Point(double _x, double _y, double _z) { x = _x, y = _y, z = _z; }
};

class Color {
public:
double r, g, b;
Color() { update(); }
Color(double _r, double _g, double _b) { r = _r, g = _g, b = _b; }
void update() { r = rnd.next(1.0), g = rnd.next(1.0), b = rnd.next(1.0); }
};

vector<int> Lanes({ -11, -7, -3, 1, 5, 9 });


class Car {
public:
Color color, tolor;
double height = 5.5, width = 3, speed, x, y; bool forward;
Car() { renovate(); }
Car(int _x, int _y, double s) {
x = _x, y = _y; speed = s;
color = { 1,0,0 }; tolor = { 0,0,0 }; forward = 1;
}

void go_left() { x = max(x - 4, Road_left + 1); }


void go_right() { x = min(x + 4, Road_right - width - 1); }
void go_down() {
if (y < 0 - height) renovate(), score++;
else y -= speed;
}
void go_down(int f) { { y = max(y - 1, Road_bottom); } }
void go_up() { y = min(y + 1, Road_top - width); }

void renovate() {
x = rnd.any(Lanes), y = Road_top;
speed = rnd.next(0.1, .2);

color.update(), tolor.update();}

Dept. of CSE, RIT Hassan Page 13


1416
Simple Particle System 2023-24
void update() {
glPushMatrix();
glColor3f(tolor.r, tolor.g, tolor.b);
if (!forward) {
glRectd(x + 0, y + 2, x + 3, y + 5);

glColor3f(color.r, color.g, color.b);


glRectd(.75 + x, 2.75 + y, 2.25 + x, 4.25 + y);

glBegin(GL_LINES);
glVertex2d(x + 0, y + 2);
glVertex2d(x + 1, y + 3);
glVertex2d(x + 0, y + 5);
glVertex2d(x + 1, y + 4);
glVertex2d(x + 2, y + 4);
glVertex2d(x + 3, y + 5);
glVertex2d(x + 2, y + 3);
glVertex2d(x + 3, y + 2);
glEnd();

glBegin(GL_QUADS);
glVertex2d(x + 0.25, y + 5.5);
glVertex2d(x + 2.75, y + 5.5);
glVertex2d(x + 3, y + 5);
glVertex2d(x + 0, y + 5);
glVertex2d(x + 0, y + 2);
glVertex2d(x + 3, y + 2);
glVertex2d(x + 2.6, y);
glVertex2d(x + 0.4, y);
glEnd();

glPointSize(10);
glBegin(GL_POINTS);
glVertex2d(2.75 + x, y + 1);
glVertex2d(0.25 + x, y + 1);
glEnd();
glPopMatrix();
return;
}
glRectd(x + 0, y + .5, x + 3, y + 3.5);

glColor3f(color.r, color.g, color.b);


glRectd(.75 + x, 1.25 + y, 2.25 + x, 2.75 + y);

glBegin(GL_LINES);
glVertex2d(x + 0, y + .5);
glVertex2d(x + 1, y + 1.5);
glVertex2d(x + 0, y + 3.5);
glVertex2d(x + 1, y + 2.5);
glVertex2d(x + 2, y + 2.5);
glVertex2d(x + 3, y + 3.5);
glVertex2d(x + 2, y + 1.5);

Dept. of CSE, RIT Hassan Page 13


1417
Simple Particle System 2023-24
glVertex2d(x + 3, y + .5);
glEnd();

glBegin(GL_QUADS);
glVertex2d(x + 0.25, y);
glVertex2d(x + 2.75, y);
glVertex2d(x + 3, y + 0.5);
glVertex2d(x + 0, y + 0.5);
glVertex2d(x + 0, y + 3.5);
glVertex2d(x + 3, y + 3.5);
glVertex2d(x + 2.6, y + 5.5);
glVertex2d(x + 0.4, y + 5.5);
glEnd();

glPointSize(10);
glBegin(GL_POINTS);
glVertex2d(2.75 + x, y + 4.5);
glVertex2d(0.25 + x, y + 4.5);
glEnd();
glPopMatrix();
}
}PlayerCar(1, 0, 0.1);

vector<Car>Cars(5);

void drawstring(double x, double y, double z, string s) {


glRasterPos3f(x, y, z);
for (auto c : s)glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, c);
}

class Road {
public:
vector<double> Dash;
Road() { for (int i = 0; i <= Road_top + 4; i += 4) Dash.push_back(i); }
void update() {
glColor3d(.25, .25, .25);
glRectd(Road_left, Road_bottom, Road_right, Road_top);

glColor3d(1, 1, 0.25);
glRectd(Road_left + 0.5, Road_bottom, Road_left + 1, Road_top);
glRectd(Road_left + 8, Road_bottom, Road_left + 8.5, Road_top);
glRectd(Road_left + 16, Road_bottom, Road_left + 16.5, Road_top);
glRectd(Road_left + 24, Road_bottom, Road_left + 24.5, Road_top);

glColor3d(1, 1, 1);
for (auto& d : Dash) {
d -= PlayerCar.speed; if (d < 0)d = Road_top + 4;
glRectd(Road_left + 4, d, Road_left + 4.5, d - 2);
glRectd(Road_left + 12, d, Road_left + 12.5, d - 2);
glRectd(Road_left + 20, d, Road_left + 20.5, d - 2);
}
}

Dept. of CSE, RIT Hassan Page 13


1418
Simple Particle System 2023-24

}Roads;

class Tree {
public:
double x, y;
Tree() { renovate(); }
void renovate() {
x = (rnd.next(2) ? rnd.next(Ortho_left, Road_left) : rnd.next(Road_right, Ortho_right));
y = rnd.next(Ortho_top, Ortho_top * 2);
}
void update() {
y -= PlayerCar.speed;
if (y <= -6)renovate();

glColor3d(0.44, 0.33, 0.23);


glRectd(-0.5 + x, 0 + y, 0.5 + x, 4 + y);

glColor3d(.13, 1, .13);
glBegin(GL_TRIANGLES);
glVertex2d(0 + x, 5 + y);
glVertex2d(-1 + x, 4 + y);
glVertex2d(1 + x, 4 + y);
glVertex2d(0 + x, 4.4 + y);
glVertex2d(-2 + x, 3 + y);
glVertex2d(2 + x, 3 + y);
glVertex2d(0 + x, 3.8 + y);
glVertex2d(-2.5 + x, 2 + y);
glVertex2d(2.5 + x, 2 + y);
glEnd();
}
}; vector<Tree>Trees(30);

void display() {
glClear(GL_COLOR_BUFFER_BIT);
Roads.update();
PlayerCar.update();
for (auto& car : Cars) { if (!collide)car.go_down(); car.update(); }
sort(Trees.begin(), Trees.end(), [](auto& a, auto& b) {return a.y > b.y;});
for (auto& tree : Trees)tree.update();
if (game_over) {
glColor3d(.7, .7, .7); glRectd(-7.3, 17, 7.5, 22);
glColor3d(0, 0, 1), drawstring(-4, 21, 0, "Game Over");
glColor3d(0, 0, 1), drawstring(-3, 19.5, 0, "Score: " + toString(score));
glColor3d(1, 0, 0), drawstring(-7, 18, 0, "Press R to Restart");
}
else {
glColor3d(.7, .7, .7); glRectd(Ortho_left, Ortho_top - 1, Ortho_left + 10, Ortho_top - 3);
glColor3d(0, 0, 1), drawstring(Ortho_left + 2, Ortho_top - 2, 0, "Score: " + toString(score));
}

Dept. of CSE, RIT Hassan Page 13


1419
Simple Particle System 2023-24
boolean rectRect(double r1x, double r1y, double r2x, double r2y) {
double r1w = 3, r1h = 5.5, r2w = 3, r2h = 5.5;
if (r1x + r1w >= r2x + EPS && r1x + EPS <= r2x + r2w && r1y + r1h >= r2y + EPS && r1y + EPS <=
r2y + r2h)return true;
return false;
}

bool collision() {
for (auto c : Cars)if (rectRect(c.x, c.y, PlayerCar.x, PlayerCar.y))return true;
return false;
}

void timer(int v) {
if (collision()) {
game_over = 1;
collide = 1;
glutPostRedisplay();
glutSpecialFunc(NULL);
return;
}
glutPostRedisplay();
glutTimerFunc(10, timer, v);
}

void specialKeys(int key, int x, int y) {


switch (key) {
case GLUT_KEY_LEFT:
PlayerCar.go_left();
break;
case GLUT_KEY_RIGHT:
PlayerCar.go_right();
break;
case GLUT_KEY_UP:
PlayerCar.go_up();
break;
case GLUT_KEY_DOWN:
PlayerCar.go_down(1);
break;
default:
break;
}
glutPostRedisplay();
}

void Keys(unsigned char key, int x, int y) {


if ('r' == key or 'R' == key) {
for (auto& car : Cars)car.renovate();
score = game_over = collide = 0;
glutSpecialFunc(specialKeys);
glutPostRedisplay();
glutTimerFunc(10, timer, 0);
}

Dept. of CSE, RIT Hassan Page 13


14110
Simple Particle System
else if (key == 'e' or key == 'E')exit(0); 2023-24
}

void reshape(GLsizei width, GLsizei height) {


glClearColor(0.22, .58, .20, 0);

// Reshape Window
glViewport(0, 0, width, height);
glLoadIdentity();

GLdouble aspect = (GLdouble)width / (GLdouble)max(height, 1);


if (width >= height) glOrtho(Ortho_left * aspect, Ortho_right * aspect, Ortho_bottom, Ortho_top, 0,
Ortho_right);
else glOrtho(Ortho_left, Ortho_right, Ortho_bottom / aspect, Ortho_top / aspect, 0, Ortho_right);
}

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


glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE);
glutInitWindowSize(650, 650);
glutInitWindowPosition(400, 20);
glutCreateWindow(Title.c_str());
glutDisplayFunc(display);
glutSpecialFunc(specialKeys);
glutKeyboardFunc(Keys);
glutReshapeFunc(reshape);
glutTimerFunc(10, timer, 0);
glutMainLoop();

return 0;
}

Dept. of CSE, RIT Hassan Page 13


14111
Simple Particle System 2023-24

Dept. of CSE, RIT Hassan Page 13


14115

You might also like