AAAAAAA Removed
AAAAAAA Removed
AAAAAAA Removed
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:
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.
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
Inbuilt Functions
• Specifies clear values to the color buffers and clears the display before redrawing it.
• glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
• Void glutDisplayFunc(void(*func)(void))
• glFlush()
• Forces any buffered OpenGL commands to execute. Sets keyboard call back to current
window.
• 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()
• 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()
• Void glutInitWindowSize( )
• 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 glutKeyboardFunc()
• 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( );
• void psTimestep( );
• void psNewparticle( );
• 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;
bool collide;
bool game_over;
long long score;
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); }
};
void renovate() {
x = rnd.any(Lanes), y = Road_top;
speed = rnd.next(0.1, .2);
color.update(), tolor.update();}
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);
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);
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);
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);
}
}
}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(.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));
}
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);
}
// Reshape Window
glViewport(0, 0, width, height);
glLoadIdentity();
return 0;
}