/
particle.h
236 lines (200 loc) · 6.34 KB
/
particle.h
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
/************************************************************************/
/* */
/* This file is part of VDrift. */
/* */
/* VDrift is free software: you can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation, either version 3 of the License, or */
/* (at your option) any later version. */
/* */
/* VDrift is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
/* GNU General Public License for more details. */
/* */
/* You should have received a copy of the GNU General Public License */
/* along with VDrift. If not, see <http://www.gnu.org/licenses/>. */
/* */
/************************************************************************/
#ifndef _PARTICLE_H
#define _PARTICLE_H
#include "mathvector.h"
#include "graphics/scenenode.h"
#include "graphics/vertexarray.h"
#include <ostream>
#include <utility> // std::pair
#include <vector>
#include <list>
class ContentManager;
class PARTICLE_SYSTEM
{
private:
class PARTICLE
{
private:
typedef MATHVECTOR<float,3> VEC;
float transparency;
float longevity;
VEC start_position;
float speed;
VEC direction;
float size;
float time; ///< time since the particle was created; i.e. the particle's age
keyed_container <SCENENODE>::handle node;
keyed_container <DRAWABLE>::handle draw;
VERTEXARRAY varray;
static keyed_container <DRAWABLE> & GetDrawlist(SCENENODE & node)
{
return node.GetDrawlist().particle;
}
void Set(const PARTICLE & other)
{
transparency = other.transparency;
longevity = other.longevity;
start_position = other.start_position;
speed = other.speed;
direction = other.direction;
size = other.size;
time = other.time;
node = other.node;
draw = other.draw;
varray = other.varray;
}
public:
PARTICLE(
SCENENODE & parentnode,
const VEC & new_start_position,
const VEC & new_dir,
float newspeed,
float newtrans,
float newlong,
float newsize,
std::tr1::shared_ptr<TEXTURE> texture) :
transparency(newtrans),
longevity(newlong),
start_position(new_start_position),
speed(newspeed),
direction(new_dir),
size(newsize),
time(0)
{
node = parentnode.AddNode();
SCENENODE & noderef = parentnode.GetNode(node);
//std::cout << "Created node: " << &node.get() << endl;
draw = GetDrawlist(noderef).insert(DRAWABLE());
DRAWABLE & drawref = GetDrawlist(noderef).get(draw);
drawref.SetDrawEnable(false);
drawref.SetVertArray(&varray);
drawref.SetDiffuseMap(texture);
drawref.SetCull(false,false);
}
PARTICLE(const PARTICLE & other)
{
Set(other);
}
PARTICLE & operator=(const PARTICLE & other)
{
Set(other);
return *this;
}
keyed_container <SCENENODE>::handle & GetNode()
{
return node;
}
float lerp(float x, float y, float s)
{
float sclamp = std::max(0.f,std::min(1.0f,s));
return x + sclamp*(y-x);
}
void Update(
SCENENODE & parent,
float dt,
const QUATERNION <float> & camdir_conjugate,
const MATHVECTOR <float, 3> & campos)
{
time += dt;
SCENENODE & noderef = parent.GetNode(node);
DRAWABLE & drawref = GetDrawlist(noderef).get(draw);
drawref.SetVertArray(&varray);
MATHVECTOR <float, 3> curpos = start_position + direction * time * speed;
noderef.GetTransform().SetTranslation(curpos);
noderef.GetTransform().SetRotation(camdir_conjugate);
float sizescale = 1.0;
float trans = transparency*std::pow((double)(1.0-time/longevity),4.0);
float transmax = 1.0;
if (trans > transmax)
trans = transmax;
if (trans < 0.0)
trans = 0.0;
sizescale = 0.2*(time/longevity)+0.4;
varray.SetToBillboard(-sizescale,-sizescale,sizescale,sizescale);
drawref.SetRadius(sizescale);
bool drawenable = true;
// scale the alpha by the closeness to the camera
// if we get too close, don't draw
// this prevents major slowdown when there are a lot of particles right next to the camera
float camdist = (curpos - campos).Magnitude();
//std::cout << camdist << std::endl;
const float camdist_off = 3.0;
const float camdist_full = 4.0;
trans = lerp(0.f,trans,(camdist-camdist_off)/(camdist_full-camdist_off));
if (trans <= 0)
drawenable = false;
drawref.SetColor(1,1,1,trans);
drawref.SetDrawEnable(drawenable);
}
bool Expired() const
{
return (time > longevity);
}
};
std::vector <PARTICLE> particles;
std::list <std::tr1::shared_ptr<TEXTURE> > textures;
std::list <std::tr1::shared_ptr<TEXTURE> >::iterator cur_texture;
std::pair <float,float> transparency_range;
std::pair <float,float> longevity_range;
std::pair <float,float> speed_range;
std::pair <float,float> size_range;
MATHVECTOR <float, 3> direction;
SCENENODE node;
public:
PARTICLE_SYSTEM() :
transparency_range(0.5,1),
longevity_range(5,14),
speed_range(0.3,1),
size_range(0.5,1),
direction(0,1,0)
{
particles.reserve(128);
cur_texture = textures.end();
}
///returns true if at least one particle texture was loaded
bool Load(
const std::list <std::string> & texlist,
const std::string & texpath,
int anisotropy,
ContentManager & content);
void Update(
float dt,
const QUATERNION <float> & camdir,
const MATHVECTOR <float, 3> & campos);
/// all of the parameters are from 0.0 to 1.0 and scale to the ranges set with SetParameters. testonly should be kept false and is only used for unit testing.
void AddParticle(
const MATHVECTOR <float,3> & position,
float newspeed,
bool testonly=false);
void Clear();
void SetParameters(
float transmin,
float transmax,
float longmin,
float longmax,
float speedmin,
float speedmax,
float sizemin,
float sizemax,
MATHVECTOR <float,3> newdir);
unsigned int NumParticles() {return particles.size();}
SCENENODE & GetNode() {return node;}
};
#endif