1 | #include ".\particlesystem.h"
|
---|
2 | #include "includes.h"
|
---|
3 | #include <GL/glext.h>
|
---|
4 | #include <glh/glh_extensions.h>
|
---|
5 |
|
---|
6 | int CompareInc( const void *arg1, const void *arg2 )
|
---|
7 | {
|
---|
8 | Particle* particle1=(Particle*) arg1;
|
---|
9 | Particle* particle2=(Particle*) arg2;
|
---|
10 |
|
---|
11 | if(*particle1>*particle2)
|
---|
12 | return -1;
|
---|
13 | else
|
---|
14 | return 1;
|
---|
15 | }
|
---|
16 |
|
---|
17 | int CompareDec( const void *arg1, const void *arg2 )
|
---|
18 | {
|
---|
19 | Particle* particle1=(Particle*) arg1;
|
---|
20 | Particle* particle2=(Particle*) arg2;
|
---|
21 |
|
---|
22 | if(*particle1>*particle2)
|
---|
23 | return 1;
|
---|
24 | else
|
---|
25 | return -1;
|
---|
26 | }
|
---|
27 |
|
---|
28 |
|
---|
29 | void ParticleSystem::SortParticles(bool increment)
|
---|
30 | {
|
---|
31 | if(increment)
|
---|
32 | qsort( m_ParticleArray, (size_t)m_Count, sizeof(Particle), CompareInc );
|
---|
33 | else
|
---|
34 | qsort( m_ParticleArray, (size_t)m_Count, sizeof(Particle), CompareDec );
|
---|
35 | }
|
---|
36 |
|
---|
37 |
|
---|
38 | ParticleSystem::ParticleSystem(void)
|
---|
39 | {
|
---|
40 | VertexBuffer=NULL;
|
---|
41 | TexcoordBuffer=NULL;
|
---|
42 | ColorBuffer=NULL;
|
---|
43 |
|
---|
44 | m_LastParticle=NULL;
|
---|
45 | m_ParticleList=new Particle;
|
---|
46 |
|
---|
47 | m_ParticleArray=NULL;
|
---|
48 |
|
---|
49 | m_Count=0;
|
---|
50 | m_Quota=100;
|
---|
51 | m_Type=0;
|
---|
52 |
|
---|
53 | m_UpVector=Vector(0,1,0);
|
---|
54 | m_RightVector=Vector(1,0,0);
|
---|
55 |
|
---|
56 | m_refreshed=false;
|
---|
57 | m_refreshonce=false;
|
---|
58 |
|
---|
59 | m_LittleSystemRadius=1.0;
|
---|
60 | }
|
---|
61 |
|
---|
62 | ParticleSystem::~ParticleSystem(void)
|
---|
63 | {
|
---|
64 | }
|
---|
65 |
|
---|
66 | void ParticleSystem::RefreshParticles(unsigned int Dt,unsigned int TimefromSecond)
|
---|
67 | {
|
---|
68 | if(!m_refreshed)
|
---|
69 | {
|
---|
70 | //Refresh particle ages, kill dead particles; refresh particle positions
|
---|
71 | Particle* thisParticle=m_ParticleList->m_NextParticle;
|
---|
72 | Particle* parentParticle=m_ParticleList;
|
---|
73 |
|
---|
74 | while(thisParticle)
|
---|
75 | {
|
---|
76 | if(thisParticle->LiveMore(Dt))
|
---|
77 | {
|
---|
78 | //refresh particle
|
---|
79 | thisParticle->Refresh(Dt);
|
---|
80 | //thisParticle->m_Distance=(thisParticle->getPosition()-m_CameraPosition).Length();
|
---|
81 | m_LastParticle=thisParticle;
|
---|
82 | parentParticle=thisParticle;
|
---|
83 | thisParticle=thisParticle->m_NextParticle;
|
---|
84 | }
|
---|
85 | else
|
---|
86 | {
|
---|
87 | //kill particle
|
---|
88 | parentParticle->m_NextParticle=thisParticle->m_NextParticle;
|
---|
89 | delete thisParticle;
|
---|
90 | thisParticle=parentParticle->m_NextParticle;
|
---|
91 | m_LastParticle=parentParticle;
|
---|
92 | m_Count--;
|
---|
93 | }
|
---|
94 |
|
---|
95 | }
|
---|
96 |
|
---|
97 | //Ask emitter to add new particles
|
---|
98 | Particle* Addto=m_LastParticle;
|
---|
99 | if(Addto==NULL)Addto=m_ParticleList;
|
---|
100 | unsigned int AddedCount=m_Emitter.Emit(TimefromSecond,m_Quota-m_Count,Addto,m_CameraPosition);
|
---|
101 | m_Count+=AddedCount;
|
---|
102 |
|
---|
103 | //fill array ;update bounding radius
|
---|
104 | delete[] m_ParticleArray;
|
---|
105 | m_ParticleArray=new Particle[m_Count];
|
---|
106 |
|
---|
107 | //RefrBRadius_Dist();
|
---|
108 |
|
---|
109 |
|
---|
110 |
|
---|
111 | }
|
---|
112 | }
|
---|
113 |
|
---|
114 | void ParticleSystem::RefrBRadius_Dist(bool refresh_rad)
|
---|
115 | {
|
---|
116 | Particle* thisParticle=m_ParticleList->m_NextParticle;
|
---|
117 | if(refresh_rad||!m_refreshed)m_BoundingRadius=0;
|
---|
118 | double dist;
|
---|
119 | for(unsigned int i=0;i<m_Count;i++)
|
---|
120 | {
|
---|
121 | thisParticle->m_Distance=(thisParticle->getPosition()-m_CameraPosition).Length();
|
---|
122 | thisParticle->m_ID=i;
|
---|
123 |
|
---|
124 | double oldSize;
|
---|
125 | if(refresh_rad||!m_refreshed)
|
---|
126 | {
|
---|
127 | m_refreshed=true;
|
---|
128 |
|
---|
129 | oldSize=thisParticle->getSize();
|
---|
130 | thisParticle->setSize(oldSize*m_LittleSystemRadius);
|
---|
131 | dist=thisParticle->getPosition().Length()+thisParticle->getSize();
|
---|
132 | if(dist>m_BoundingRadius)
|
---|
133 | m_BoundingRadius=dist;
|
---|
134 | }
|
---|
135 | m_ParticleArray[i]=*thisParticle;
|
---|
136 |
|
---|
137 | if(refresh_rad||!m_refreshed)thisParticle->setSize(oldSize);
|
---|
138 | thisParticle=thisParticle->m_NextParticle;
|
---|
139 | }
|
---|
140 | }
|
---|
141 |
|
---|
142 | void ParticleSystem::RefreshBuffer(bool billboard)
|
---|
143 | {
|
---|
144 | /*
|
---|
145 | if(VertexBuffer!=NULL)delete VertexBuffer;
|
---|
146 | if(TexcoordBuffer!=NULL)delete TexcoordBuffer;
|
---|
147 | if(ColorBuffer!=NULL)delete ColorBuffer;
|
---|
148 | */
|
---|
149 | if(billboard)
|
---|
150 | {
|
---|
151 | double x,y,z;
|
---|
152 | Vector LeftDown=m_UpVector*-1-m_RightVector;
|
---|
153 | Vector RightDown=m_UpVector*-1+m_RightVector;
|
---|
154 | Vector RightUp=m_UpVector+m_RightVector;
|
---|
155 | Vector LeftUp=m_UpVector-m_RightVector;
|
---|
156 |
|
---|
157 | VertexBuffer=new float[12*m_Count];
|
---|
158 | ColorBuffer=new float[16*m_Count];
|
---|
159 | TexcoordBuffer=new float[16*m_Count];
|
---|
160 |
|
---|
161 | for(unsigned int i=0;i<m_Count;i++)
|
---|
162 | {
|
---|
163 | m_ParticleArray[i].getPosition(x,y,z);
|
---|
164 |
|
---|
165 | float bbsize=2*m_ParticleArray[i].getSize();
|
---|
166 | float t=m_ParticleArray[i].getSize()/m_BoundingRadius;
|
---|
167 |
|
---|
168 | TexcoordBuffer[i*16]=0;TexcoordBuffer[i*16+1]=0;
|
---|
169 | TexcoordBuffer[i*16+2]=bbsize;TexcoordBuffer[i*16+3]=t;
|
---|
170 | TexcoordBuffer[i*16+4]=1;TexcoordBuffer[i*16+5]=0;
|
---|
171 | TexcoordBuffer[i*16+6]=bbsize;TexcoordBuffer[i*16+7]=t;
|
---|
172 | TexcoordBuffer[i*16+8]=1;TexcoordBuffer[i*16+9]=1;
|
---|
173 | TexcoordBuffer[i*16+10]=bbsize;TexcoordBuffer[i*16+11]=t;
|
---|
174 | TexcoordBuffer[i*16+12]=0;TexcoordBuffer[i*16+13]=1;
|
---|
175 | TexcoordBuffer[i*16+14]=bbsize;TexcoordBuffer[i*16+15]=t;
|
---|
176 |
|
---|
177 |
|
---|
178 | ColorBuffer[i*16]=m_ParticleArray[i].m_Color.x;ColorBuffer[i*16+1]=m_ParticleArray[i].m_Color.y;
|
---|
179 | ColorBuffer[i*16+2]=m_ParticleArray[i].m_Color.z;ColorBuffer[i*16+3]=m_ParticleArray[i].m_Color.w;
|
---|
180 |
|
---|
181 | ColorBuffer[i*16+4]=m_ParticleArray[i].m_Color.x;ColorBuffer[i*16+5]=m_ParticleArray[i].m_Color.y;
|
---|
182 | ColorBuffer[i*16+6]=m_ParticleArray[i].m_Color.z;ColorBuffer[i*16+7]=m_ParticleArray[i].m_Color.w;
|
---|
183 |
|
---|
184 | ColorBuffer[i*16+8]=m_ParticleArray[i].m_Color.x;ColorBuffer[i*16+9]=m_ParticleArray[i].m_Color.y;
|
---|
185 | ColorBuffer[i*16+10]=m_ParticleArray[i].m_Color.z;ColorBuffer[i*16+11]=m_ParticleArray[i].m_Color.w;
|
---|
186 |
|
---|
187 | ColorBuffer[i*16+12]=m_ParticleArray[i].m_Color.x;ColorBuffer[i*16+13]=m_ParticleArray[i].m_Color.y;
|
---|
188 | ColorBuffer[i*16+14]=m_ParticleArray[i].m_Color.z;ColorBuffer[i*16+15]=m_ParticleArray[i].m_Color.w;
|
---|
189 |
|
---|
190 | VertexBuffer[i*12]=x+LeftDown.x*m_ParticleArray[i].getSize();
|
---|
191 | VertexBuffer[i*12+1]=y+LeftDown.y*m_ParticleArray[i].getSize();
|
---|
192 | VertexBuffer[i*12+2]=z+LeftDown.z*m_ParticleArray[i].getSize();
|
---|
193 | VertexBuffer[i*12+3]=x+RightDown.x*m_ParticleArray[i].getSize();
|
---|
194 | VertexBuffer[i*12+4]=y+RightDown.y*m_ParticleArray[i].getSize();
|
---|
195 | VertexBuffer[i*12+5]=z+RightDown.z*m_ParticleArray[i].getSize();
|
---|
196 | VertexBuffer[i*12+6]=x+RightUp.x*m_ParticleArray[i].getSize();
|
---|
197 | VertexBuffer[i*12+7]=y+RightUp.y*m_ParticleArray[i].getSize();
|
---|
198 | VertexBuffer[i*12+8]=z+RightUp.z*m_ParticleArray[i].getSize();
|
---|
199 | VertexBuffer[i*12+9]=x+LeftUp.x*m_ParticleArray[i].getSize();
|
---|
200 | VertexBuffer[i*12+10]=y+LeftUp.y*m_ParticleArray[i].getSize();
|
---|
201 | VertexBuffer[i*12+11]=z+LeftUp.z*m_ParticleArray[i].getSize();
|
---|
202 |
|
---|
203 | }
|
---|
204 | }
|
---|
205 | else
|
---|
206 | {
|
---|
207 | double x,y,z;
|
---|
208 |
|
---|
209 | if(VertexBuffer==NULL)VertexBuffer=new float[3*m_Count];
|
---|
210 | if(ColorBuffer==NULL)ColorBuffer=new float[4*m_Count];
|
---|
211 | if(TexcoordBuffer==NULL)TexcoordBuffer=new float[m_Count*2];
|
---|
212 |
|
---|
213 | for(unsigned int i=0;i<m_Count;i++)
|
---|
214 | {
|
---|
215 | m_ParticleArray[i].getPosition(x,y,z);
|
---|
216 |
|
---|
217 | ColorBuffer[i*4]=m_ParticleArray[i].m_Color.y;
|
---|
218 | ColorBuffer[4*i+1]=m_ParticleArray[i].m_Color.y;
|
---|
219 | ColorBuffer[4*i+2]=m_ParticleArray[i].m_Color.z;
|
---|
220 | ColorBuffer[4*i+3]=m_ParticleArray[i].m_Color.w;
|
---|
221 |
|
---|
222 | VertexBuffer[i*3]=x;
|
---|
223 | VertexBuffer[i*3+1]=y;
|
---|
224 | VertexBuffer[i*3+2]=z;
|
---|
225 |
|
---|
226 | TexcoordBuffer[2*i]=m_ParticleArray[i].m_ID;
|
---|
227 | TexcoordBuffer[2*i+1]=m_ParticleArray[i].getSize();
|
---|
228 |
|
---|
229 | }
|
---|
230 |
|
---|
231 | }
|
---|
232 | }
|
---|
233 |
|
---|
234 | void ParticleSystem::RenderAsPoints(bool sprites)
|
---|
235 | {
|
---|
236 |
|
---|
237 | glEnableClientState(GL_VERTEX_ARRAY);
|
---|
238 | glEnableClientState(GL_TEXTURE_COORD_ARRAY);
|
---|
239 | glEnableClientState(GL_COLOR_ARRAY);
|
---|
240 |
|
---|
241 | glVertexPointer (3, GL_FLOAT, 0, VertexBuffer);
|
---|
242 | glColorPointer(4,GL_FLOAT,0,ColorBuffer);
|
---|
243 | glTexCoordPointer(2,GL_FLOAT,0,TexcoordBuffer);
|
---|
244 |
|
---|
245 | if(sprites)
|
---|
246 | {
|
---|
247 | glEnable(GL_POINT_SPRITE_ARB);
|
---|
248 | glTexEnvi(GL_POINT_SPRITE_ARB, GL_COORD_REPLACE_ARB, GL_TRUE);
|
---|
249 | glEnable(GL_VERTEX_PROGRAM_POINT_SIZE_NV);
|
---|
250 |
|
---|
251 | }
|
---|
252 |
|
---|
253 | glDrawArrays(GL_POINTS, 0, m_Count);
|
---|
254 |
|
---|
255 | if(sprites)
|
---|
256 | {
|
---|
257 | glDisable(GL_POINT_SPRITE_ARB);
|
---|
258 | glDisable(GL_VERTEX_PROGRAM_POINT_SIZE_NV);
|
---|
259 | glTexEnvi(GL_POINT_SPRITE_ARB, GL_COORD_REPLACE_ARB, GL_FALSE);
|
---|
260 | }
|
---|
261 |
|
---|
262 | glDisableClientState( GL_VERTEX_ARRAY ); // Disable Vertex Arrays
|
---|
263 | glDisableClientState( GL_TEXTURE_COORD_ARRAY );
|
---|
264 | glDisableClientState( GL_COLOR_ARRAY );
|
---|
265 |
|
---|
266 | }
|
---|
267 |
|
---|
268 | void ParticleSystem::RenderAsBillboard()
|
---|
269 | {
|
---|
270 |
|
---|
271 | glEnableClientState(GL_VERTEX_ARRAY);
|
---|
272 | glEnableClientState(GL_TEXTURE_COORD_ARRAY);
|
---|
273 | glEnableClientState(GL_COLOR_ARRAY);
|
---|
274 |
|
---|
275 | glVertexPointer (3, GL_FLOAT, 0, VertexBuffer);
|
---|
276 | glTexCoordPointer(4,GL_FLOAT,0,TexcoordBuffer);
|
---|
277 | glColorPointer(4,GL_FLOAT,0,ColorBuffer);
|
---|
278 |
|
---|
279 | glDrawArrays(GL_QUADS, 0, m_Count*4);
|
---|
280 |
|
---|
281 | glDisableClientState( GL_VERTEX_ARRAY ); // Disable Vertex Arrays
|
---|
282 | glDisableClientState( GL_TEXTURE_COORD_ARRAY );
|
---|
283 | glDisableClientState( GL_COLOR_ARRAY );
|
---|
284 |
|
---|
285 |
|
---|
286 | }
|
---|
287 |
|
---|