docs: detect when too many varying vars are used
[mesa/nouveau-pmpeg.git] / progs / demos / particles.cxx
blobb88c318e0bcfa8505878d84f5ec9f15c4432dbf1
1 /*
2 * This program is under the GNU GPL.
3 * Use at your own risk.
5 * written by David Bucciarelli (humanware@plus.it)
6 * Humanware s.r.l.
7 */
9 #include <stdlib.h>
11 #include "particles.h"
13 #define vinit(a,i,j,k) {\
14 (a)[0]=i;\
15 (a)[1]=j;\
16 (a)[2]=k;\
19 #define vadds(a,dt,b) {\
20 (a)[0]+=(dt)*(b)[0];\
21 (a)[1]+=(dt)*(b)[1];\
22 (a)[2]+=(dt)*(b)[2];\
25 #define vequ(a,b) {\
26 (a)[0]=(b)[0];\
27 (a)[1]=(b)[1];\
28 (a)[2]=(b)[2];\
31 #define vinter(a,dt,b,c) {\
32 (a)[0]=(dt)*(b)[0]+(1.0-dt)*(c)[0];\
33 (a)[1]=(dt)*(b)[1]+(1.0-dt)*(c)[1];\
34 (a)[2]=(dt)*(b)[2]+(1.0-dt)*(c)[2];\
37 #define clamp(a) ((a) < 0.0 ? 0.0 : ((a) < 1.0 ? (a) : 1.0))
39 #define vclamp(v) {\
40 (v)[0]=clamp((v)[0]);\
41 (v)[1]=clamp((v)[1]);\
42 (v)[2]=clamp((v)[2]);\
46 float rainParticle::min[3];
47 float rainParticle::max[3];
48 float rainParticle::partLength=0.2f;
51 static float vrnd(void)
53 return(((float)rand())/RAND_MAX);
57 particle::particle()
59 age=0.0f;
61 vinit(acc,0.0f,0.0f,0.0f);
62 vinit(vel,0.0f,0.0f,0.0f);
63 vinit(pos,0.0f,0.0f,0.0f);
66 void particle::elapsedTime(float dt)
68 age+=dt;
70 vadds(vel,dt,acc);
72 vadds(pos,dt,vel);
75 /////////////////////////////////////////
76 // Particle System
77 /////////////////////////////////////////
79 particleSystem::particleSystem()
81 t=0.0f;
83 part=NULL;
85 particleNum=0;
88 particleSystem::~particleSystem()
90 if(part)
91 free(part);
94 void particleSystem::addParticle(particle *p)
96 if(!part) {
97 part=(particle **)calloc(1,sizeof(particle *));
98 part[0]=p;
99 particleNum=1;
100 } else {
101 particleNum++;
102 part=(particle **)realloc(part,sizeof(particle *)*particleNum);
103 part[particleNum-1]=p;
107 void particleSystem::reset(void)
109 if(part)
110 free(part);
112 t=0.0f;
114 part=NULL;
116 particleNum=0;
119 void particleSystem::draw(void)
121 if(!part)
122 return;
124 part[0]->beginDraw();
125 for(unsigned int i=0;i<particleNum;i++)
126 part[i]->draw();
127 part[0]->endDraw();
130 void particleSystem::addTime(float dt)
132 if(!part)
133 return;
135 for(unsigned int i=0;i<particleNum;i++) {
136 part[i]->elapsedTime(dt);
137 part[i]->checkAge();
141 /////////////////////////////////////////
142 // Rain
143 /////////////////////////////////////////
145 void rainParticle::init(void)
147 age=0.0f;
149 acc[0]=0.0f;
150 acc[1]=-0.98f;
151 acc[2]=0.0f;
153 vel[0]=0.0f;
154 vel[1]=0.0f;
155 vel[2]=0.0f;
157 oldpos[0]=pos[0]=min[0]+(max[0]-min[0])*vrnd();
158 oldpos[1]=pos[1]=max[1]+0.2f*max[1]*vrnd();
159 oldpos[2]=pos[2]=min[2]+(max[2]-min[2])*vrnd();
161 vadds(oldpos,-partLength,vel);
164 rainParticle::rainParticle()
166 init();
169 void rainParticle::setRainingArea(float minx, float miny, float minz,
170 float maxx, float maxy, float maxz)
172 vinit(min,minx,miny,minz);
173 vinit(max,maxx,maxy,maxz);
176 void rainParticle::setLength(float l)
178 partLength=l;
181 void rainParticle::draw(void)
183 glColor4f(0.7f,0.95f,1.0f,0.0f);
184 glVertex3fv(oldpos);
186 glColor4f(0.3f,0.7f,1.0f,1.0f);
187 glVertex3fv(pos);
190 void rainParticle::checkAge(void)
192 if(pos[1]<min[1])
193 init();
196 void rainParticle::elapsedTime(float dt)
198 particle::elapsedTime(dt);
200 if(pos[0]<min[0])
201 pos[0]=max[0]-(min[0]-pos[0]);
202 if(pos[2]<min[2])
203 pos[2]=max[2]-(min[2]-pos[2]);
205 if(pos[0]>max[0])
206 pos[0]=min[0]+(pos[0]-max[0]);
207 if(pos[2]>max[2])
208 pos[2]=min[2]+(pos[2]-max[2]);
210 vequ(oldpos,pos);
211 vadds(oldpos,-partLength,vel);
214 void rainParticle::randomHeight(void)
216 pos[1]=(max[1]-min[1])*vrnd()+min[1];
218 oldpos[1]=pos[1]-partLength*vel[1];