-
Notifications
You must be signed in to change notification settings - Fork 0
/
particleSystem.cpp
572 lines (469 loc) · 18.7 KB
/
particleSystem.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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
//-----------------------------------------------------------------------------
// Name: particlesystem.h
// Author: Kevin Harris
// Last Modified: 02/01/05
// Description: Implementation file for the CParticleSystem Class
//-----------------------------------------------------------------------------
#include <tchar.h>
#include <windows.h>
#include <mmsystem.h>
#include "stdio.h"
#include <GL/gl.h>
#include "GLAUX.H"
#include <iostream>
#include "particlesystem.h"
//-----------------------------------------------------------------------------
// FUNCTION POINTERS FOR OPENGL EXTENSIONS
//-----------------------------------------------------------------------------
// For convenience, this project ships with its own "glext.h" extension header
// file. If you have trouble running this sample, it may be that this "glext.h"
// file is defining something that your hardware doesn’t actually support.
// Try recompiling the sample using your own local, vendor-specific "glext.h"
// header file.
#include "glext.h" // Sample's header file
//#include <GL/glext.h> // Your local header file
//#ifndef GL_ARB_point_parameters
PFNGLPOINTPARAMETERFARBPROC glPointParameterfARB = NULL;
PFNGLPOINTPARAMETERFVARBPROC glPointParameterfvARB = NULL;
//-----------------------------------------------------------------------------
// Name: getRandomMinMax()
// Desc: Gets a random number between min/max boundaries
//-----------------------------------------------------------------------------
float getRandomMinMax( float fMin, float fMax )
{
float fRandNum = (float)rand () / RAND_MAX;
return fMin + (fMax - fMin) * fRandNum;
}
//-----------------------------------------------------------------------------
// Name: getRandomVector()
// Desc: Generates a random vector where X,Y, and Z components are between
// -1.0 and 1.0
//-----------------------------------------------------------------------------
MyVector getRandomVector( void )
{
MyVector vVector;
// Pick a random Z between -1.0f and 1.0f.
vVector.z = getRandomMinMax( -1.0f, 1.0f );
// Get radius of this circle
float radius = (float)sqrt(1 - vVector.z * vVector.z);
// Pick a random point on a circle.
float t = getRandomMinMax( -OGL_PI, OGL_PI );
// Compute matching X and Y for our Z.
vVector.x = (float)cosf(t) * radius;
vVector.y = (float)sinf(t) * radius;
return vVector;
}
//-----------------------------------------------------------------------------
// Name : classifyPoint()
// Desc : Classifies a point against the plane passed
//-----------------------------------------------------------------------------
int classifyPoint( MyVector *vPoint, Plane *pPlane )
{
MyVector vDirection = pPlane->m_vPoint - *vPoint;
float fResult = vDirection.getDotProduct( pPlane->m_vNormal );
if( fResult < -0.001f )
return CP_FRONT;
if( fResult > 0.001f )
return CP_BACK;
return CP_ONPLANE;
}
//-----------------------------------------------------------------------------
// Name: CParticleSystem()
// Desc:
//-----------------------------------------------------------------------------
CParticleSystem::CParticleSystem()
{
m_pActiveList = NULL; // Head node of point sprites that are active
m_pFreeList = NULL; // Head node of point sprites that are inactive and waiting to be recycled.
m_pPlanes = NULL;
m_dwActiveCount = 0;
m_fCurrentTime = 0.0f;
m_fLastUpdate = 0.0f;
m_chTexFile = NULL;
m_textureID = NULL;
m_dwMaxParticles = 1;
m_dwNumToRelease = 1;
m_fReleaseInterval = 1.0f;
m_fLifeCycle = 1.0f;
m_fSize = 1.0f;
m_clrColor = MyVector(1.0f,1.0f,1.0f);
m_vPosition = MyVector(0.0f,0.0f,0.0f);
m_vVelocity = MyVector(0.0f,0.0f,0.0f);
m_vGravity = MyVector(0.0f,0.0f,0.0f);
m_vWind = MyVector(0.0f,0.0f,0.0f);
m_bAirResistence = true;
m_fVelocityVar = 1.0f;
SetTexture("particle.bmp");
}
//-----------------------------------------------------------------------------
// Name: ~CParticleSystem()
// Desc:
//-----------------------------------------------------------------------------
CParticleSystem::~CParticleSystem()
{
glDeleteTextures( 1, &m_textureID );
while( m_pPlanes ) // Repeat till null...
{
Plane *pPlane = m_pPlanes; // Hold onto the first one
m_pPlanes = pPlane->m_pNext; // Move down to the next one
delete pPlane; // Delete the one we're holding
}
while( m_pActiveList )
{
Particle* pParticle = m_pActiveList;
m_pActiveList = pParticle->m_pNext;
delete pParticle;
}
m_pActiveList = NULL;
while( m_pFreeList )
{
Particle *pParticle = m_pFreeList;
m_pFreeList = pParticle->m_pNext;
delete pParticle;
}
m_pFreeList = NULL;
if( m_chTexFile != NULL )
{
delete [] m_chTexFile;
m_chTexFile = NULL;
}
}
//-----------------------------------------------------------------------------
// Name: SetTexture()
// Desc:
//-----------------------------------------------------------------------------
void CParticleSystem::SetTexture( char *chTexFile )
{
// Deallocate the memory that was previously reserved for this string.
if( m_chTexFile != NULL )
{
delete [] m_chTexFile;
m_chTexFile = NULL;
}
// Dynamically allocate the correct amount of memory.
m_chTexFile = new char[strlen( chTexFile ) + 1];
// If the allocation succeeds, copy the initialization string.
if( m_chTexFile != NULL )
strcpy( m_chTexFile, chTexFile );
}
//-----------------------------------------------------------------------------
// Name: GetTextureObject()
// Desc:
//-----------------------------------------------------------------------------
GLuint CParticleSystem::GetTextureID()
{
return m_textureID;
}
//-----------------------------------------------------------------------------
// Name: SetCollisionPlane()
// Desc:
//-----------------------------------------------------------------------------
void CParticleSystem::SetCollisionPlane( MyVector vPlaneNormal, MyVector vPoint,
float fBounceFactor, int nCollisionResult )
{
Plane *pPlane = new Plane;
pPlane->m_vNormal = vPlaneNormal;
pPlane->m_vPoint = vPoint;
pPlane->m_fBounceFactor = fBounceFactor;
pPlane->m_nCollisionResult = nCollisionResult;
pPlane->m_pNext = m_pPlanes; // Attach the curent list to it...
m_pPlanes = pPlane; // ... and make it the new head.
}
//-----------------------------------------------------------------------------
// Name: Init()
// Desc:
//-----------------------------------------------------------------------------
int CParticleSystem::Init( void )
{
printf("\nInitialising ParticleSystem");
//
// If the required extensions are present, get the addresses of thier
// functions that we wish to use...
//
char *ext = (char*)glGetString( GL_EXTENSIONS );
if( strstr( ext, "GL_ARB_point_parameters" ) == NULL )
{
printf("GL_ARB_point_parameters extension was not found");
MessageBox(NULL,"GL_ARB_point_parameters extension was not found",
"ERROR",MB_OK|MB_ICONEXCLAMATION);
return 0;
}
else
{
glPointParameterfARB = (PFNGLPOINTPARAMETERFARBPROC)wglGetProcAddress("glPointParameterfARB");
glPointParameterfvARB = (PFNGLPOINTPARAMETERFVARBPROC)wglGetProcAddress("glPointParameterfvARB");
if( !glPointParameterfARB || !glPointParameterfvARB )
{
printf("One or more GL_ARB_point_parameters functions were not found");
MessageBox(NULL,"One or more GL_ARB_point_parameters functions were not found",
"ERROR",MB_OK|MB_ICONEXCLAMATION);
return 0;
}
}
//
// Load up the point sprite's texture...
//
AUX_RGBImageRec *pTextureImage = auxDIBImageLoad( m_chTexFile );
if( pTextureImage != NULL )
{
printf("\nAssigning texture image %s", m_chTexFile);
glGenTextures( 1, &m_textureID );
glBindTexture(GL_TEXTURE_2D, m_textureID);
glTexParameteri( GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER, GL_LINEAR );
glTexImage2D( GL_TEXTURE_2D, 0, 3, pTextureImage->sizeX, pTextureImage->sizeY, 0,
GL_RGB, GL_UNSIGNED_BYTE, pTextureImage->data );
}
if( pTextureImage )
{
if( pTextureImage->data )
free( pTextureImage->data );
free( pTextureImage );
printf("...assigned");
}
//
// If you want to know the max size that a point sprite can be set
// to, do this.
//
glGetFloatv( GL_POINT_SIZE_MAX_ARB, &m_fMaxPointSize );
glPointSize( m_fMaxPointSize );
return 1;
}
//-----------------------------------------------------------------------------
// Name: Update()
// Desc:
//-----------------------------------------------------------------------------
int CParticleSystem::Update( FLOAT fElpasedTime )
{
Particle *pParticle;
Particle **ppParticle;
Plane *pPlane;
Plane **ppPlane;
MyVector vOldPosition;
m_fCurrentTime += fElpasedTime; // Update our particle system timer...
printf("\nUpdating particle system: current time %f", m_fCurrentTime);
ppParticle = &m_pActiveList; // Start at the head of the active list
int i = 0;
while( *ppParticle )
{
//printf("\nUpdate particle %d", i++);
pParticle = *ppParticle; // Set a pointer to the head
// Calculate new position
float fTimePassed = m_fCurrentTime - pParticle->m_fInitTime;
//printf("\nTime passed is %lf (lifecycle: %lf)", fTimePassed, m_fLifeCycle);
if( fTimePassed >= m_fLifeCycle )
{
// Time is up, put the particle back on the free list...
*ppParticle = pParticle->m_pNext;
pParticle->m_pNext = m_pFreeList;
m_pFreeList = pParticle;
--m_dwActiveCount;
}
else
{
// Update particle position and velocity
// Update velocity with respect to Gravity (Constant Accelaration)
pParticle->m_vCurVel += m_vGravity * fElpasedTime;
// Update velocity with respect to Wind (Accelaration based on
// difference of vectors)
if( m_bAirResistence == true )
pParticle->m_vCurVel += (m_vWind - pParticle->m_vCurVel) * fElpasedTime;
// Finally, update position with respect to velocity
vOldPosition = pParticle->m_vCurPos;
pParticle->m_vCurPos += pParticle->m_vCurVel * fElpasedTime;
//-----------------------------------------------------------------
// BEGIN Checking the particle against each plane that was set up
ppPlane = &m_pPlanes; // Set a pointer to the head
while( *ppPlane )
{
pPlane = *ppPlane;
int result = classifyPoint( &pParticle->m_vCurPos, pPlane );
if( result == CP_BACK /*|| result == CP_ONPLANE */ )
{
if( pPlane->m_nCollisionResult == CR_BOUNCE )
{
pParticle->m_vCurPos = vOldPosition;
//-----------------------------------------------------------------
//
// The new velocity vector of a particle that is bouncing off
// a plane is computed as follows:
//
// Vn = (N.V) * N
// Vt = V - Vn
// Vp = Vt - Kr * Vn
//
// Where:
//
// . = Dot product operation
// N = The normal of the plane from which we bounced
// V = Velocity vector prior to bounce
// Vn = Normal force
// Kr = The coefficient of restitution ( Ex. 1 = Full Bounce,
// 0 = Particle Sticks )
// Vp = New velocity vector after bounce
//
//-----------------------------------------------------------------
float Kr = pPlane->m_fBounceFactor;
MyVector Vn = pPlane->m_vNormal.getDotProduct( pParticle->m_vCurVel ) * pPlane->m_vNormal;
MyVector Vt = pParticle->m_vCurVel - Vn;
MyVector Vp = Vt - Kr * Vn;
pParticle->m_vCurVel = Vp;
}
else if( pPlane->m_nCollisionResult == CR_RECYCLE )
{
pParticle->m_fInitTime -= m_fLifeCycle;
}
else if( pPlane->m_nCollisionResult == CR_STICK )
{
pParticle->m_vCurPos = vOldPosition;
pParticle->m_vCurVel = MyVector(0.0f,0.0f,0.0f);
}
}
ppPlane = &pPlane->m_pNext;
}
// END Plane Checking
//-----------------------------------------------------------------
ppParticle = &pParticle->m_pNext;
}
}
//-------------------------------------------------------------------------
// Emit new particles in accordance to the flow rate...
//
// NOTE: The system operates with a finite number of particles.
// New particles will be created until the max amount has
// been reached, after that, only old particles that have
// been recycled can be reintialized and used again.
//-------------------------------------------------------------------------
if( m_fCurrentTime - m_fLastUpdate > m_fReleaseInterval )
{
// Reset update timing...
m_fLastUpdate = m_fCurrentTime;
// Emit new particles at specified flow rate...
for( DWORD i = 0; i < m_dwNumToRelease; ++i )
{
// Do we have any free particles to put back to work?
if( m_pFreeList )
{
// If so, hand over the first free one to be reused.
pParticle = m_pFreeList;
// Then make the next free particle in the list next to go!
m_pFreeList = pParticle->m_pNext;
}
else
{
// There are no free particles to recycle...
// We'll have to create a new one from scratch!
if( m_dwActiveCount < m_dwMaxParticles )
{
if( NULL == ( pParticle = new Particle ) )
return E_OUTOFMEMORY;
}
}
if( m_dwActiveCount < m_dwMaxParticles )
{
pParticle->m_pNext = m_pActiveList; // Make it the new head...
m_pActiveList = pParticle;
// Set the attributes for our new particle...
pParticle->m_vCurVel = m_vVelocity;
if( m_fVelocityVar != 0.0f )
{
MyVector vRandomVec = getRandomVector();
pParticle->m_vCurVel += vRandomVec * m_fVelocityVar;
}
pParticle->m_fInitTime = m_fCurrentTime;
pParticle->m_vCurPos = m_vPosition;
++m_dwActiveCount;
}
}
}
// Query for the max point size supported by the hardware
float maxSize = 0.0f;
glGetFloatv( GL_POINT_SIZE_MAX_ARB, &m_fMaxPointSize );
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: RestartParticleSystem()
// Desc:
//-----------------------------------------------------------------------------
void CParticleSystem::RestartParticleSystem( void )
{
Particle *pParticle;
Particle **ppParticle;
ppParticle = &m_pActiveList; // Start at the head of the active list
while( *ppParticle )
{
pParticle = *ppParticle; // Set a pointer to the head
// Put the particle back on the free list...
*ppParticle = pParticle->m_pNext;
pParticle->m_pNext = m_pFreeList;
m_pFreeList = pParticle;
--m_dwActiveCount;
}
}
//-----------------------------------------------------------------------------
// Name: Render()
// Desc:
//-----------------------------------------------------------------------------
void CParticleSystem::Render( void )
{
Particle *pParticle = m_pActiveList;
//Update(0.001);
//
// Set up the OpenGL state machine for using point sprites...
//
// This is how will our point sprite's size will be modified by
// distance from the viewer.
float quadratic[] = { 1.0f, 0.0f, 0.01f };
glPointParameterfvARB( GL_POINT_DISTANCE_ATTENUATION_ARB, quadratic );
// The alpha of a point is calculated to allow the fading of points
// instead of shrinking them past a defined threshold size. The threshold
// is defined by GL_POINT_FADE_THRESHOLD_SIZE_ARB and is not clamped to
// the minimum and maximum point sizes.
glPointParameterfARB( GL_POINT_FADE_THRESHOLD_SIZE_ARB, 60.0f );
glPointParameterfARB( GL_POINT_SIZE_MIN_ARB, 1.0f );
glPointParameterfARB( GL_POINT_SIZE_MAX_ARB, m_fMaxPointSize );
// Specify point sprite texture coordinate replacement mode for each texture unit
glTexEnvf( GL_POINT_SPRITE_ARB, GL_COORD_REPLACE_ARB, GL_TRUE );
//
// Render point sprites...
//
bool testRender = false;
if (testRender)//for this to work, need to disable textures in OGL
{
glDisable( GL_POINT_SPRITE_ARB );
} else glEnable( GL_POINT_SPRITE_ARB );
glPointSize( m_fSize );
glColor3f( m_clrColor.x, m_clrColor.y, m_clrColor.z );
glBegin( GL_POINTS );
{
// Render each particle...
while( pParticle )
{
glVertex3f( pParticle->m_vCurPos.x,
pParticle->m_vCurPos.y,
pParticle->m_vCurPos.z );
pParticle = pParticle->m_pNext;
}
}
glEnd();
glDisable( GL_POINT_SPRITE_ARB );
}
void CParticleSystem::RenderSimple( void )
{
Particle *pParticle = m_pActiveList;
//Use simple rendering system - GL_POINTS only
glPointSize( 2.2 );
glColor3f( m_clrColor.x, m_clrColor.y, m_clrColor.z );
glBegin( GL_POINTS );
{
// Render each particle...
while( pParticle )
{
glVertex3f( pParticle->m_vCurPos.x,
pParticle->m_vCurPos.y,
pParticle->m_vCurPos.z );
pParticle = pParticle->m_pNext;
}
}
glEnd();
}