forked from dom96/ParticleBench
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCPP.cpp
423 lines (380 loc) · 11.1 KB
/
CPP.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
#include <iostream>
#include <string>
#include <vector>
#include <cstdint>
#include <cmath>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
using std::cout;
using std::cerr;
using std::string;
using std::vector;
using std::endl;
constexpr bool PRINT_FRAMES = true;
constexpr const char * TITLE = "ParticleBench";
constexpr int32_t WIDTH = 800;
constexpr int32_t HEIGHT = 600;
constexpr int32_t MIN_X = -80;
constexpr int32_t MAX_X = 80;
constexpr int32_t MIN_Y = -90;
constexpr int32_t MAX_Y = 50;
constexpr int32_t MIN_DEPTH = 50;
constexpr int32_t MAX_DEPTH = 250;
constexpr int32_t START_RANGE = 15;
constexpr int32_t START_X = (MIN_X + (MIN_X + MAX_X)/2);
constexpr int32_t START_Y = MAX_Y;
constexpr int32_t START_DEPTH = (MIN_DEPTH + (MIN_DEPTH + MAX_DEPTH) / 2);
constexpr int32_t POINTS_PER_SEC = 2000;
constexpr int32_t MAX_INIT_VEL = 7;
constexpr int32_t MAX_LIFE = 5000;
constexpr int32_t MAX_SCALE = 4;
constexpr int32_t WIND_CHANGE = 2000;
constexpr int32_t MAX_WIND = 3;
constexpr double SPAWN_INTERVAL = 0.01;
constexpr int32_t RUNNING_TIME = ((MAX_LIFE / 1000) * 4);
constexpr int32_t MAX_PTS = (RUNNING_TIME * POINTS_PER_SEC);
constexpr uint32_t NUM_VERTICES = 24;
constexpr uint32_t NUM_NORMALS = NUM_VERTICES / 4;
constexpr uint32_t RAND_SEED = 1234569;
constexpr double WINDX = 0;
constexpr double WINDY = 0;
constexpr double WINDZ = 0;
constexpr double GRAV = 50;
struct Pt {
double X, Y, Z, VX, VY, VZ, R, Life;
bool is;
};
struct Vertex {
GLfloat pos[3];
GLfloat normal[3];
};
const GLfloat srcCoords[ NUM_VERTICES ][3] = {
{-1, -1, 1},
{1, -1, 1},
{1, 1, 1},
{-1, 1, 1},
{-1, -1, -1},
{-1, 1, -1},
{1, 1, -1},
{1, -1, -1},
{-1, 1, -1},
{-1, 1, 1},
{1, 1, 1},
{1, 1, -1},
{-1, -1, -1},
{1, -1, -1},
{1, -1, 1},
{-1, -1, 1},
{1, -1, -1},
{1, 1, -1},
{1, 1, 1},
{1, -1, 1},
{-1, -1, -1},
{-1, -1, 1},
{-1, 1, 1},
{-1, 1, -1}
};
const GLfloat srcNormals[ NUM_NORMALS ][3] = {
{0, 0, 1},
{0, 0, -1},
{0, 1, 0},
{0, -1, 0},
{1, 0, 0},
{-1, 0, 0}
};
struct XorRandGenerator {
uint32_t operator()( uint32_t & gen ) {
gen ^= gen << 13;
gen ^= gen >> 17;
gen ^= gen << 5;
return gen;
}
};
template<class RandGenerator>
class Particles {
int numPts_;
int minPt_;
double windX_, windY_, windZ_;
vector<Pt> particles_;
RandGenerator & randGenerator_;
public:
Particles( RandGenerator & randGenerator, const uint32_t numParticles ) :
randGenerator_( randGenerator ),
numPts_( 0 ),
minPt_( 0 ),
particles_( numParticles, Pt {0, 0, 0, 0, 0, 0, 0, 0, 0 } ),
windX_( WINDX ), windY_( WINDY ), windZ_( WINDZ ) {};
void moveParticles( const double secs ) {
for( uint32_t i = minPt_; i < numPts_; i++) {
Pt & p( particles_[i] );
if( p.is == false ) {
continue;
}
p.X += p.VX * secs;
p.Y += p.VY * secs;
p.Z += p.VZ * secs;
p.VX += windX_ * 1 / p.R;
p.VY += windY_ * 1 / p.R;
p.VY -= GRAV * secs;
p.VZ += windZ_ * 1 / p.R;
p.Life -= secs;
if (p.Life <= 0 ) {
p.is = false;
}
}
}
void doWind( const double frameDur, uint32_t & randValue ) {
windX_ += ( (double)( randGenerator_( randValue ) % WIND_CHANGE ) / WIND_CHANGE - WIND_CHANGE/2000) * frameDur;
windY_ += ( (double)( randGenerator_( randValue ) % WIND_CHANGE ) / WIND_CHANGE - WIND_CHANGE/2000) * frameDur;
windZ_ += ( (double)( randGenerator_( randValue ) % WIND_CHANGE ) / WIND_CHANGE - WIND_CHANGE/2000) * frameDur;
if (fabs(windX_) > MAX_WIND) {
windX_ *= -0.5;
}
if (fabs(windY_) > MAX_WIND) {
windY_ *= -0.5;
}
if (fabs(windZ_) > MAX_WIND) {
windZ_ *= -0.5;
}
}
void spawnParticles( const double secs, uint32_t & randValue ) {
const uint32_t num = secs * POINTS_PER_SEC;
for ( uint32_t i = 0 ; i < num; i++) {
Pt & pt = particles_[numPts_];
pt.X = 0 + (double)( randGenerator_( randValue ) % START_RANGE ) - START_RANGE/2;
pt.Y = START_Y;
pt.Z = START_DEPTH + (double)( randGenerator_( randValue ) % START_RANGE ) - START_RANGE/2;
pt.VX = (double)( randGenerator_( randValue ) % MAX_INIT_VEL );
pt.VY = (double)( randGenerator_( randValue ) % MAX_INIT_VEL );
pt.VZ = (double)( randGenerator_( randValue ) % MAX_INIT_VEL );
pt.R = (double)( randGenerator_( randValue ) % (MAX_SCALE*100) ) / 200;
pt.Life = (double)( randGenerator_( randValue ) % MAX_LIFE ) / 1000;
pt.is = true;
numPts_++;
}
}
void checkForCollisions() {
for ( uint32_t i = minPt_; i < numPts_; i++) {
Pt & p( particles_[i] );
if (p.is == false) {
continue;
}
if (p.X < MIN_X) {
p.X = MIN_X + p.R;
p.VX *= -1.1; // These particles are magic; they accelerate by 10% at every bounce off the bounding box
}
if (p.X > MAX_X) {
p.X = MAX_X - p.R;
p.VX *= -1.1;
}
if (p.Y < MIN_Y) {
p.Y = MIN_Y + p.R;
p.VY *= -1.1;
}
if (p.Y > MAX_Y) {
p.Y = MAX_Y - p.R;
p.VY *= -1.1;
}
if (p.Z < MIN_DEPTH) {
p.Z = MIN_DEPTH + p.R;
p.VZ *= -1.1;
}
if (p.Z > MAX_DEPTH) {
p.Z = MAX_DEPTH - p.R;
p.VZ *= -1.1;
}
}
}
void renderParticles() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
for (int i = minPt_; i < numPts_; i++) {
Pt & p( particles_[i] );
if (p.is == false) {
continue;
}
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
glPushMatrix();
glTranslatef(p.X, p.Y, -p.Z);
glScalef(p.R*2, p.R*2, p.R*2);
glColor4f(0.7, 0.9, 0.2, 1);
glDrawArrays( GL_QUADS, 0, 24 );
}
}
void cleanupPtPool() {
for (int i = minPt_; i < numPts_; i++) {
if (particles_[i].is == true) {
minPt_ = i;
break;
}
}
}
};
template<class RandGenerator>
class GLRenderer {
RandGenerator randGenerator_;
uint32_t randValue_;
vector<Vertex> vertices_;
GLuint gVBO_;
Particles<RandGenerator> particles_;
double spwnTmr_;
double cleanupTmr_;
public:
GLRenderer( const uint32_t randSeed ) :
randValue_( randSeed ),
gVBO_( 0 ),
particles_( randGenerator_, MAX_PTS ),
spwnTmr_( 0.0 ),
cleanupTmr_( 0.0 ) {
vertices_.reserve( NUM_VERTICES );
}
void initScene() {
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glClearColor(0.1, 0.1, 0.6, 1.0);
glClearDepth(1);
glDepthFunc(GL_LEQUAL);
float ambient[4] = {0.8, 0.05, 0.1, 1};
glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
float diffuse[4] = {1.0, 1.0, 1.0, 1};
glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
float lightPos[4] = {MIN_X + (MAX_X-MIN_X)/2, MAX_Y, MIN_DEPTH, 0};
glLightfv(GL_LIGHT0, GL_POSITION, lightPos);
glEnable(GL_LIGHT0);
glViewport(0, 0, WIDTH, HEIGHT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-1, 1, -1, 1, 1.0, 1000.0);
glRotatef(20, 1, 0, 0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glPushMatrix();
}
void setupBuffers() {
uint32_t curCoord { 0 };
for( uint32_t n = 0 ; n < NUM_NORMALS ; ++n ) {
const GLfloat *cn = srcNormals[n];
for( uint32_t p = 0 ; p < 4 ; ++p, ++curCoord ) {
const GLfloat *cv = srcCoords[curCoord];
vertices_.emplace_back( Vertex {{cv[0], cv[1], cv[2] }, {cn[0], cn[1], cn[2]}} );
}
}
glGenBuffers( 1, &gVBO_ );
glBindBuffer( GL_ARRAY_BUFFER, gVBO_ );
glBufferData( GL_ARRAY_BUFFER, NUM_VERTICES * sizeof(Vertex), &(vertices_[0]), GL_STATIC_DRAW );
glEnableClientState( GL_VERTEX_ARRAY );
glEnableClientState( GL_NORMAL_ARRAY );
glVertexPointer( 3, GL_FLOAT, sizeof( Vertex), 0 );
glNormalPointer( GL_FLOAT, sizeof( Vertex ), (const GLvoid *)offsetof( Vertex, normal ) );
}
void teardownBuffers() {
glDisableClientState( GL_NORMAL_ARRAY );
glDisableClientState( GL_VERTEX_ARRAY );
glDeleteBuffers( 1, &gVBO_ );
}
void doTimestep( const double frameDuration, double *gpuInitT ) {
particles_.moveParticles( frameDuration );
particles_.doWind( frameDuration, randValue_ );
if( spwnTmr_ >= SPAWN_INTERVAL ) {
particles_.spawnParticles( SPAWN_INTERVAL, randValue_ );
spwnTmr_ -= SPAWN_INTERVAL;
}
if( cleanupTmr_ >= (MAX_LIFE/1000.0) ) {
particles_.cleanupPtPool();
cleanupTmr_ = 0;
}
particles_.checkForCollisions();
*gpuInitT = glfwGetTime();
particles_.renderParticles();
}
void updateTimers( const double frameDuration ) {
spwnTmr_ += frameDuration;
cleanupTmr_ += frameDuration;
}
};
void error_callback(int error, const char* description) {
cerr << description << endl;
}
int main(int argc, char* argv[]) {
vector<double> frames( (RUNNING_TIME * 1000), 0.0 );
vector<double> gpuTimes( (RUNNING_TIME * 1000), 0.0 );
uint64_t curFrame( 0 );
glfwSetErrorCallback(error_callback);
if( !glfwInit() ) {
exit(EXIT_FAILURE);
}
glfwWindowHint(GLFW_SAMPLES, 2);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1);
GLFWwindow* window = glfwCreateWindow(WIDTH, HEIGHT, TITLE, NULL, NULL);
if( !window ) {
glfwTerminate();
exit(EXIT_FAILURE);
}
glfwMakeContextCurrent(window);
glfwSwapInterval(0);
GLRenderer<XorRandGenerator> glRenderer( RAND_SEED );
glRenderer.initScene();
GLenum glewError = glewInit();
if( glewError != GLEW_OK ){
cerr << "Error initializing GLEW! " << glewGetErrorString( glewError ) << endl;
return false;
}
if( !GLEW_VERSION_2_1 ){
cerr << "OpenGL 2.1 not supported!" << endl;
return false;
}
glRenderer.setupBuffers();
double initT, endT, gpuInitT, gpuEndT, frameDur, runTmr = 0;
while (!glfwWindowShouldClose(window)) {
initT = glfwGetTime();
glRenderer.doTimestep( frameDur, &gpuInitT );
glfwSwapBuffers(window);
gpuEndT = glfwGetTime();
glfwPollEvents();
endT = glfwGetTime();
frameDur = endT-initT;
glRenderer.updateTimers( frameDur );
runTmr += frameDur;
if (runTmr > MAX_LIFE/1000) {
frames[curFrame] = frameDur;
gpuTimes[curFrame] = gpuEndT - gpuInitT;
curFrame += 1;
}
if (runTmr >= RUNNING_TIME) {
double sum = 0;
uint64_t i = 0;
for (i = 0; i < curFrame; i++) {
sum += frames[i];
}
double framerateMean = sum / (double)curFrame;
cout << "Average framerate was: " << (1/framerateMean) << " frames per second." << endl;
sum = 0;
i = 0;
for (i = 0; i < curFrame; i++) {
sum += gpuTimes[i];
}
double gpuTimeMean = sum / (double)curFrame;
cout << "Average cpu time was- " << (framerateMean - gpuTimeMean) << " seconds per frame." << endl;
double sumDiffs = 0.0;
for (i = 0; i < curFrame; i++) {
sumDiffs += pow((1/frames[i])-(1/framerateMean), 2);
}
double variance = sumDiffs/ (double)curFrame;
double sd = sqrt(variance);
cout << "The standard deviation was: " << sd << " frames per second." << endl;
if( PRINT_FRAMES ) {
cout << "--:";
for( uint32_t i = 0 ; i < curFrame ; ++i ) {
cout << (1/frames[i]) << ',';
}
cout << ".--";
}
break;
}
}
glRenderer.teardownBuffers();
glfwDestroyWindow(window);
glfwTerminate();
exit(EXIT_SUCCESS);
}