SHOW:
|
|
- or go back to the newest paste.
1 | #include <stdafx.h> | |
2 | #include<stdlib.h> | |
3 | #include <time.h> | |
4 | #include <GL\glut.h> | |
5 | #include<GL\GLU.h> | |
6 | ||
7 | #define MAX_PARTICLES 100 | |
8 | #define MAX_BOUNCE_COUNT 10 | |
9 | - | #define MAX_PARTICLE_AGE 10 |
9 | + | #define MAX_PARTICLE_AGE 50 |
10 | ||
11 | ||
12 | struct PARTICLE { | |
13 | float X,Y,Z; // Current position | |
14 | float sX,sY,sZ; // Current Speed/Movement | |
15 | float tX,tY,tZ; // Target Speed/Movement | |
16 | float R,B,G; // Particle Colour | |
17 | bool Active; // Is particle Active | |
18 | int Age; // Age of the particle | |
19 | int MaxAge; // Maximum Age before particle dies | |
20 | int BounceCount; | |
21 | } Particles[MAX_PARTICLES]; | |
22 | ||
23 | void Init_Particles(); | |
24 | void Activate_Particles(); | |
25 | void Adjust_Particles(); | |
26 | void Render_Particles(); | |
27 | void prepare_GLscene(); | |
28 | void DrawGLscene(); | |
29 | void Reshape(GLsizei w, GLsizei h); | |
30 | ||
31 | ||
32 | int main(int argc, char** argv){ | |
33 | glutInit(&argc,argv); | |
34 | glutCreateWindow("Particle fountain"); | |
35 | prepare_GLscene(); | |
36 | glutDisplayFunc(DrawGLscene); | |
37 | //glutReshapeFunc(Reshape); | |
38 | glutMainLoop(); | |
39 | } | |
40 | ||
41 | ||
42 | void Init_Particles(){ | |
43 | int p; | |
44 | srand((int)time(NULL)); | |
45 | for(p=0; p<MAX_PARTICLES; p++){ | |
46 | Particles[p].Active = false; | |
47 | Particles[p].tX = 0.0f; | |
48 | Particles[p].tY = -0.1f; | |
49 | Particles[p].tZ = 0.0f; | |
50 | } | |
51 | } | |
52 | ||
53 | void Activate_Particles(){ | |
54 | int p; | |
55 | for(p=0; p<MAX_PARTICLES; p++){ | |
56 | if(!Particles[p].Active){ | |
57 | // Start the particle at 0,0,0 origin | |
58 | Particles[p].X = 0.0f; | |
59 | Particles[p].Y = 0.0f; | |
60 | Particles[p].Z = 0.0f; | |
61 | // The following lines set a random speed value | |
62 | Particles[p].sX = (((float)((rand() % 100) + 1)) / | |
63 | 1000.0f) - 0.05f; | |
64 | Particles[p].sY = (((float)((rand() % 100) + 50)) / | |
65 | 500.0f); | |
66 | Particles[p].sZ = (((float)((rand() % 100) + 1)) / | |
67 | 1000.0f) - 0.05f; | |
68 | // We also activate the particle | |
69 | Particles[p].Active = true; | |
70 | // Set it's Age to zero | |
71 | Particles[p].Age = 0; | |
72 | // We also assign a max age to the particles | |
73 | Particles[p].MaxAge = MAX_PARTICLE_AGE; | |
74 | // We Also reset the bouncecount to zero | |
75 | Particles[p].BounceCount = 0; | |
76 | return; | |
77 | } | |
78 | } | |
79 | } | |
80 | ||
81 | void Adjust_Particles(){ | |
82 | int p; | |
83 | for(p=0; p<MAX_PARTICLES; p++){ | |
84 | // We move the speed towards the target speed by 1/20 (5%) | |
85 | Particles[p].sX+= (Particles[p].tX - Particles[p].sX) / 20.0f; | |
86 | Particles[p].sY+= (Particles[p].tY - Particles[p].sY) / 20.0f; | |
87 | Particles[p].sZ+= (Particles[p].tZ - Particles[p].sZ) / 20.0f; | |
88 | // Then we adjust the position of | |
89 | // the particle by the new speed | |
90 | Particles[p].X+= Particles[p].sX; | |
91 | Particles[p].Y+= Particles[p].sY; | |
92 | Particles[p].Z+= Particles[p].sZ; | |
93 | // Now for the bounce code. | |
94 | if(Particles[p].Y < 0.0f){ | |
95 | Particles[p].Y = 0.0f; | |
96 | Particles[p].sY = -Particles[p].sY; | |
97 | Particles[p].BounceCount++; | |
98 | if(Particles[p].BounceCount > MAX_BOUNCE_COUNT){ | |
99 | Particles[p].Active = false; | |
100 | } | |
101 | } | |
102 | // And finally the age check | |
103 | Particles[p].Age++; | |
104 | if(Particles[p].Age > Particles[p].MaxAge){ | |
105 | Particles[p].Active = false; | |
106 | } | |
107 | } | |
108 | } | |
109 | ||
110 | void Render_Particles(){ | |
111 | int p; | |
112 | glBegin(GL_POINTS); | |
113 | for(p=0; p<MAX_PARTICLES; p++){ | |
114 | if(Particles[p].Active){ | |
115 | glColor4f(1.0f, 1.0f, 1.0f, 0.5f); | |
116 | glVertex3f(Particles[p].X, | |
117 | Particles[p].Y, | |
118 | Particles[p].Z); | |
119 | } | |
120 | } | |
121 | glEnd(); | |
122 | ||
123 | } | |
124 | ||
125 | void prepare_GLscene(){ | |
126 | Activate_Particles(); | |
127 | Adjust_Particles(); | |
128 | Render_Particles(); | |
129 | } | |
130 | ||
131 | void DrawGLscene(){ | |
132 | glTranslatef(0.0f, -0.05f, 0.0f); | |
133 | Render_Particles(); | |
134 | glFlush(); | |
135 | } | |
136 | ||
137 | void Reshape(GLsizei w,GLsizei h){ | |
138 | // resize viewport to new canvas size | |
139 | glViewport(0, 0, w, h); | |
140 | // change clipping region to have same w:h ratio as canvas | |
141 | glMatrixMode(GL_PROJECTION); | |
142 | glLoadIdentity(); | |
143 | ||
144 | if (w <= h) | |
145 | gluOrtho2D(-1.0, 1.0, -1.0 * h/w, 1.0 * h/w); | |
146 | else | |
147 | gluOrtho2D(-1.0 * w/h, 1.0 * w/h, -1.0, 1.0); | |
148 | } |