-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
401 lines (327 loc) · 13.4 KB
/
main.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
#define WIN32_LEAN_AND_MEAN
#pragma comment(lib, "glaux.lib")
#pragma comment(lib, "opengl32.lib")
#pragma comment(lib, "glu32.lib")
#pragma comment(linker, "/subsystem:console")
#include "stdlib.h"
#include "stdio.h"
#include "windows.h"
#include <mmsystem.h>
#include "myvector.h"
#include "mymatrix.h"
#include "objloader.h"
#include "particleSystem.h"
#include <gl/gl.h> // standard OpenGL include
#include <gl/glu.h> // OpenGL utilties
#include "glut.h" // OpenGL utilties
//define the particle systems
int g_nActiveSystem = 2;
CParticleSystem *g_pParticleSystems[7];
void initParticles( void );
float g_fElpasedTime;
double g_dCurTime;
double g_dLastTime;
//prototypes for our callback functions
void draw(void); //our drawing routine
void idle(void); //what to do when nothing is happening
void key(unsigned char k, int x, int y); //handle key presses
void reshape(int width, int height); //when the window is resized
void init_drawing(void); //drawing intialisation
//our main routine
int main(int argc, char *argv[])
{
//Initialise Glut and create a window
glutInit(&argc, argv);
//sets up our display mode
//here we've selected an RGBA display with depth testing
//and double buffering enabled
glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE);
//create a window and pass through the windows title
glutCreateWindow("Basic Glut Particle Systems");
//run our own drawing initialisation routine
init_drawing();
initParticles();
//tell glut the names of our callback functions point to our
//functions that we defined at the start of this file
glutReshapeFunc(reshape);
glutKeyboardFunc(key);
glutIdleFunc(idle);
glutDisplayFunc(draw);
//request a window size of 600 x 600
glutInitWindowSize(600,600);
glutReshapeWindow(600,600);
//go into the main loop
//this loop won't terminate until the user exits the
//application
glutMainLoop();
return 0;
}
//draw callback function - this is called by glut whenever the
//window needs to be redrawn
void draw(void)
{
//clear the current window
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//make changes to the modelview matrix
glMatrixMode(GL_MODELVIEW);
//initialise the modelview matrix to the identity matrix
glLoadIdentity();
glPushMatrix();
glTranslatef(0.0,0.0,-2.0); //move the camera back to view the scene
//
// Enabling GL_DEPTH_TEST and setting glDepthMask to GL_FALSE makes the
// Z-Buffer read-only, which helps remove graphical artifacts generated
// from rendering a list of particles that haven't been sorted by
// distance to the eye.
//
// Enabling GL_BLEND and setting glBlendFunc to GL_DST_ALPHA with GL_ONE
// allows particles, which overlap, to alpha blend with each other
// correctly.
//
glEnable( GL_DEPTH_TEST );
glDepthMask( GL_FALSE );
//
// Render particle system
//
//RENDER THE PARTICLES
//better to try a test render first - this does not use more advanced
//features and helps verify that the basic system is working
//It is possible that Point Sprites may not be supported by your
//graphics card, particularly if it is an older type
bool doTestRender = false;
if (doTestRender)
{
glDisable(GL_TEXTURE_2D);
glDisable(GL_BLEND);
g_pParticleSystems[g_nActiveSystem]->RenderSimple();
}
else
{
glEnable(GL_DEPTH_TEST);
glDepthMask(GL_FALSE);
glEnable(GL_TEXTURE_2D);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE);
//specify blending function here using glBlendFunc
glBindTexture( GL_TEXTURE_2D, g_pParticleSystems[g_nActiveSystem]->GetTextureID() );
g_pParticleSystems[g_nActiveSystem]->Render();
}
//
// Reset OpenGL states...
//
glDepthMask( GL_TRUE );
glDisable( GL_BLEND );
glPopMatrix();
//flush what we've drawn to the buffer
glFlush();
//swap the back buffer with the front buffer
glutSwapBuffers();
}
//idle callback function - this is called when there is nothing
//else to do
void idle(void)
{
//this is a good place to do animation
//since there are no animations in this test, we can leave
//idle() empty
g_dCurTime = timeGetTime();
g_fElpasedTime = (float)((g_dCurTime - g_dLastTime) * 0.001);
g_dLastTime = g_dCurTime;
g_pParticleSystems[g_nActiveSystem]->Update((float)g_fElpasedTime);
glutPostRedisplay();
}
//key callback function - called whenever the user presses a
//key
void key(unsigned char k, int x, int y)
{
switch(k)
{
case 'r':
break;
case 's':
break;
case 'R':
break;
case 27: //27 is the ASCII code for the ESCAPE key
exit(0);
break;
}
if (k >= '1' && k <= '6')
g_nActiveSystem = (int)(k - '1');
glutPostRedisplay();
}
//reshape callback function - called when the window size changed
void reshape(int width, int height)
{
//set the viewport to be the same width and height as the window
glViewport(0,0,width, height);
//make changes to the projection matrix
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//set up our projection type
//gluPerspective(45.0, (float) width / (float) height, 1.0, 100.0);
//here we use an orthogonal projection
glOrtho(-5.0, 5.0, -5.0, 5.0, 0.1, 5.0);
//redraw the view during resizing
draw();
}
//set up OpenGL before we do any drawing
//this function is only called once at the start of the program
void init_drawing(void)
{
//blend colours across the surface of the polygons
//another mode to try is GL_FLAT which is flat shading
glShadeModel(GL_SMOOTH);
//turn lighting off
glDisable(GL_LIGHTING);
//enable OpenGL hidden surface removal
glDepthFunc(GL_LEQUAL);
glEnable(GL_DEPTH_TEST);
GLfloat specular[] = {0.2,0.2,0.2,1.0};
GLfloat ambient[] = {1.0,1.0,1.0,1.0};
GLfloat diffuse[] = {1.0,1.0,1.0,1.0};
GLfloat position[] = {0.0,30.0,0.0,1.0};
glLightfv(GL_LIGHT0, GL_SPECULAR, specular);
glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
glLightfv(GL_LIGHT0, GL_POSITION, position);
GLfloat position1[] = {10.0,30.0,0.0,1.0};
glLightfv(GL_LIGHT1, GL_SPECULAR, specular);
glLightfv(GL_LIGHT1, GL_AMBIENT, ambient);
glLightfv(GL_LIGHT1, GL_DIFFUSE, diffuse);
glLightfv(GL_LIGHT1, GL_POSITION, position1);
glEnable(GL_LIGHT1);
//glEnable(GL_LIGHTING);
//glEnable(GL_COLOR_MATERIAL);
glEnable(GL_TEXTURE_2D);
}
void initParticles( void )
{
g_dCurTime = timeGetTime();
g_fElpasedTime = (float)((g_dCurTime - g_dLastTime) * 0.001);
g_dLastTime = g_dCurTime;
//
// Exploding burst
//
g_pParticleSystems[0] = new CParticleSystem();
//g_pParticleSystems[0]->SetTexture( "..\\particle.bmp" );
g_pParticleSystems[0]->SetTexture( "particle.bmp" );
g_pParticleSystems[0]->SetMaxParticles( 100 );
g_pParticleSystems[0]->SetNumToRelease( 100 );
g_pParticleSystems[0]->SetReleaseInterval( 0.05f );
g_pParticleSystems[0]->SetLifeCycle( 0.5f );
g_pParticleSystems[0]->SetSize( 30.0f );
g_pParticleSystems[0]->SetColor( MyVector( 1.0f, 0.0f, 0.0f ));
g_pParticleSystems[0]->SetPosition( MyVector( 0.0f, 5.0f, 0.0f) );
g_pParticleSystems[0]->SetVelocity( MyVector( 0.0f, 0.0f, 0.0f) );
g_pParticleSystems[0]->SetGravity( MyVector( 0.0f, 0.0f, 0.0f) );
g_pParticleSystems[0]->SetWind( MyVector( 0.0f, 0.0f, 0.0f) );
g_pParticleSystems[0]->SetVelocityVar( 10.0f );
g_pParticleSystems[0]->Init();
//
// Wind blown fountain
//
g_pParticleSystems[1] = new CParticleSystem();
// g_pParticleSystems[1]->SetTexture( "..\\particle.bmp" );
g_pParticleSystems[1]->SetTexture( "particle.bmp" );
g_pParticleSystems[1]->SetMaxParticles( 500 );
g_pParticleSystems[1]->SetNumToRelease( 5 );
g_pParticleSystems[1]->SetReleaseInterval( 0.05f );
g_pParticleSystems[1]->SetLifeCycle( 4.0f );
g_pParticleSystems[1]->SetSize( 30.0f );
g_pParticleSystems[1]->SetColor( MyVector( 1.0f, 1.0f, 1.0f ));
g_pParticleSystems[1]->SetPosition( MyVector( 0.0f, 0.0f, 0.0f ) );
g_pParticleSystems[1]->SetVelocity( MyVector( 0.0f, 5.0f, 0.0f ) );
g_pParticleSystems[1]->SetGravity( MyVector( 0.0f, 0.0f, 0.0f ) );
g_pParticleSystems[1]->SetWind( MyVector( 2.0f, 0.0f, 0.0f ) );
g_pParticleSystems[1]->SetVelocityVar( 1.5f );
g_pParticleSystems[1]->Init();
//
// Omni-directiional emission expanding into space with no air resistence
//
g_pParticleSystems[2] = new CParticleSystem();
g_pParticleSystems[2]->SetTexture( "particle.bmp" );
g_pParticleSystems[2]->SetMaxParticles( 2048 );
g_pParticleSystems[2]->SetNumToRelease( 10 );
g_pParticleSystems[2]->SetReleaseInterval( 0.05f );
g_pParticleSystems[2]->SetLifeCycle( 5.0f );
g_pParticleSystems[2]->SetSize( 30.0f );
g_pParticleSystems[2]->SetColor( MyVector( 1.0f, 1.0f, 1.0f ));
g_pParticleSystems[2]->SetPosition( MyVector( 0.0f, 0.0f, 0.0f) );
g_pParticleSystems[2]->SetVelocity( MyVector( 0.0f, 0.0f, 0.0f) );
g_pParticleSystems[2]->SetGravity( MyVector( 0.0f, 0.0f, 0.0f) );
g_pParticleSystems[2]->SetWind( MyVector( 0.0f, 0.0f, 0.0f) );
g_pParticleSystems[2]->SetAirResistence( false );
g_pParticleSystems[2]->SetVelocityVar(2.0f);
g_pParticleSystems[2]->Init();
//
// Fountain particles behave like paint spots when striking a plane as
// the wind blowing them towards us
//
g_pParticleSystems[3] = new CParticleSystem();
g_pParticleSystems[3]->SetTexture( "particle.bmp" );
g_pParticleSystems[3]->SetMaxParticles( 100 );
g_pParticleSystems[3]->SetNumToRelease( 10 );
g_pParticleSystems[3]->SetReleaseInterval( 0.05f );
g_pParticleSystems[3]->SetLifeCycle( 3.0f );
g_pParticleSystems[3]->SetSize( 30.0f );
g_pParticleSystems[3]->SetColor( MyVector( 1.0f, 1.0f, 1.0f ));
g_pParticleSystems[3]->SetPosition( MyVector( 0.0f, 0.0f, 0.0f ) );
g_pParticleSystems[3]->SetVelocity( MyVector( 0.0f, 5.0f, 0.0f ) );
g_pParticleSystems[3]->SetGravity( MyVector( 0.0f, 0.0f, 0.0f ) );
g_pParticleSystems[3]->SetWind( MyVector( 0.0f, 0.0f, -20.0f ) );
g_pParticleSystems[3]->SetVelocityVar( 2.5f );
g_pParticleSystems[3]->SetCollisionPlane( MyVector( 0.0f, 0.0f,1.0f ),
MyVector( 0.0f, 0.0f, -5.0f ),
1.0f, CR_STICK );
g_pParticleSystems[3]->Init();
//
// Fountain using a single collision plane acting as a floor
//
g_pParticleSystems[4] = new CParticleSystem();
g_pParticleSystems[4]->SetTexture( "particle.bmp" );
g_pParticleSystems[4]->SetMaxParticles( 200 );
g_pParticleSystems[4]->SetNumToRelease( 10 );
g_pParticleSystems[4]->SetReleaseInterval( 0.05f );
g_pParticleSystems[4]->SetLifeCycle( 5.0f );
g_pParticleSystems[4]->SetSize( 30.0f );
g_pParticleSystems[4]->SetColor( MyVector( 1.0f, 1.0f, 1.0f ));
g_pParticleSystems[4]->SetPosition( MyVector( 0.0f, 0.0f, 0.0f ) );
g_pParticleSystems[4]->SetVelocity( MyVector( 0.0f, 0.0f, 0.0f ) );
g_pParticleSystems[4]->SetGravity( MyVector( 0.0f, -9.8f, 0.0f ) );
g_pParticleSystems[4]->SetWind( MyVector( 0.0f, 0.0f, 0.0f ) );
g_pParticleSystems[4]->SetVelocityVar( 20.0f );
g_pParticleSystems[4]->SetCollisionPlane( MyVector( 0.0f, 1.0f, 0.0f ),
MyVector( 0.0f, 0.0f, 0.0f ) );
g_pParticleSystems[4]->Init();
//
// Fountain boxed-in by 6 collision planes
//
g_pParticleSystems[5] = new CParticleSystem();
g_pParticleSystems[5]->SetTexture( "particle.bmp" );
g_pParticleSystems[5]->SetMaxParticles( 100 );
g_pParticleSystems[5]->SetNumToRelease( 5 );
g_pParticleSystems[5]->SetReleaseInterval( 0.05f );
g_pParticleSystems[5]->SetLifeCycle( 5.0f );
g_pParticleSystems[5]->SetSize( 30.0f );
g_pParticleSystems[5]->SetColor( MyVector( 1.0f, 1.0f, 1.0f ));
g_pParticleSystems[5]->SetPosition( MyVector( 0.0f, 0.0f, 0.0f ) );
g_pParticleSystems[5]->SetVelocity( MyVector( 0.0f, 0.0f, 0.0f ) );
g_pParticleSystems[5]->SetGravity( MyVector( 0.0f, -9.8f, 0.0f ) );
g_pParticleSystems[5]->SetWind( MyVector( 0.0f, 0.0f, 0.0f ) );
g_pParticleSystems[5]->SetVelocityVar( 20.0f );
// Create a series of planes to collide with
g_pParticleSystems[5]->SetCollisionPlane( MyVector( 0.0f, 1.0f, 0.0f ),
MyVector( 0.0f, 0.0f, 0.0f ) ); // Floor
g_pParticleSystems[5]->SetCollisionPlane( MyVector( 1.0f, 0.0f, 0.0f ),
MyVector(-3.0f, 0.0f, 0.0f ) ); // Left Wall
g_pParticleSystems[5]->SetCollisionPlane( MyVector(-1.0f, 0.0f, 0.0f ),
MyVector( 3.0f, 0.0f, 0.0f ) ); // Right Wall
g_pParticleSystems[5]->SetCollisionPlane( MyVector( 0.0f, 0.0f, 1.0f ),
MyVector( 0.0f, 0.0f,-3.0f ) ); // Front Wall
g_pParticleSystems[5]->SetCollisionPlane( MyVector( 0.0f, 0.0f,-1.0f ),
MyVector( 0.0f, 0.0f, 3.0f ) ); // Back Wall
g_pParticleSystems[5]->SetCollisionPlane( MyVector( 0.0f,-1.0f, 0.0f ),
MyVector( 0.0f, 5.0f, 0.0f ) ); // Ceiling
g_pParticleSystems[5]->Init();
}