1 | // Copyright (C) 2014 ChaosForge Ltd
|
---|
2 | // This file is part of NV Libraries.
|
---|
3 | // For conditions of distribution and use, see copyright notice in nv.hh
|
---|
4 |
|
---|
5 | #include "nv/engine/particle_engine.hh"
|
---|
6 |
|
---|
7 | #include <nv/interface/device.hh>
|
---|
8 | #include <nv/core/random.hh>
|
---|
9 | #include <nv/lua/lua_glm.hh>
|
---|
10 | #include <nv/core/logging.hh>
|
---|
11 | #include <cmath>
|
---|
12 |
|
---|
13 | static const char *nv_particle_engine_vertex_shader_world =
|
---|
14 | "#version 120\n"
|
---|
15 | "attribute vec3 nv_position;\n"
|
---|
16 | "attribute vec2 nv_texcoord;\n"
|
---|
17 | "attribute vec4 nv_color;\n"
|
---|
18 | "varying vec4 v_color;\n"
|
---|
19 | "varying vec2 v_texcoord;\n"
|
---|
20 | "uniform mat4 nv_m_view;\n"
|
---|
21 | "uniform mat4 nv_m_projection;\n"
|
---|
22 | "void main(void)\n"
|
---|
23 | "{\n"
|
---|
24 | " gl_Position = nv_m_projection * nv_m_view * vec4 (nv_position, 1.0);\n"
|
---|
25 | " v_texcoord = nv_texcoord;\n"
|
---|
26 | " v_color = nv_color;\n"
|
---|
27 | "}\n";
|
---|
28 | static const char *nv_particle_engine_vertex_shader_local =
|
---|
29 | "#version 120\n"
|
---|
30 | "attribute vec3 nv_position;\n"
|
---|
31 | "attribute vec2 nv_texcoord;\n"
|
---|
32 | "attribute vec4 nv_color;\n"
|
---|
33 | "varying vec4 v_color;\n"
|
---|
34 | "varying vec2 v_texcoord;\n"
|
---|
35 | "uniform mat4 nv_m_mvp;\n"
|
---|
36 | "void main(void)\n"
|
---|
37 | "{\n"
|
---|
38 | " gl_Position = nv_m_mvp * vec4 (nv_position, 1.0);\n"
|
---|
39 | " v_texcoord = nv_texcoord;\n"
|
---|
40 | " v_color = nv_color;\n"
|
---|
41 | "}\n";
|
---|
42 | static const char *nv_particle_engine_fragment_shader =
|
---|
43 | "#version 120\n"
|
---|
44 | "uniform sampler2D nv_t_diffuse;\n"
|
---|
45 | "varying vec4 v_color;\n"
|
---|
46 | "varying vec2 v_texcoord;\n"
|
---|
47 | "void main(void)\n"
|
---|
48 | "{\n"
|
---|
49 | " vec4 tex_color = texture2D( nv_t_diffuse, v_texcoord );\n"
|
---|
50 | " gl_FragColor = v_color * tex_color;\n"
|
---|
51 | "}\n";
|
---|
52 |
|
---|
53 | using namespace nv;
|
---|
54 |
|
---|
55 | static void nv_particle_emmiter_point( const particle_emmiter_data*, particle* p, uint32 count )
|
---|
56 | {
|
---|
57 | for ( uint32 i = 0; i < count; ++i )
|
---|
58 | {
|
---|
59 | p[i].position = vec3();
|
---|
60 | }
|
---|
61 |
|
---|
62 | }
|
---|
63 |
|
---|
64 | static void nv_particle_emmiter_box( const particle_emmiter_data* pe, particle* p, uint32 count )
|
---|
65 | {
|
---|
66 | random& r = random::get();
|
---|
67 | for ( uint32 i = 0; i < count; ++i )
|
---|
68 | {
|
---|
69 | p[i].position =
|
---|
70 | r.frange( -pe->hextents[0], pe->hextents[0] ) * pe->cdir +
|
---|
71 | r.frange( 0.0f, pe->extents[1] ) * pe->dir +
|
---|
72 | r.frange( -pe->hextents[2], pe->hextents[2] ) * pe->odir;
|
---|
73 | }
|
---|
74 | }
|
---|
75 |
|
---|
76 | static void nv_particle_emmiter_cylinder( const particle_emmiter_data* pe, particle* p, uint32 count )
|
---|
77 | {
|
---|
78 | random& r = random::get();
|
---|
79 | for ( uint32 i = 0; i < count; ++i )
|
---|
80 | {
|
---|
81 | vec2 rellipse( r.disk_point( pe->precise ) * pe->extents[0] );
|
---|
82 | p[i].position =
|
---|
83 | rellipse.x * pe->cdir +
|
---|
84 | r.frange( 0.0f, pe->extents[1] ) * pe->dir +
|
---|
85 | rellipse.y * pe->odir;
|
---|
86 | }
|
---|
87 | }
|
---|
88 |
|
---|
89 | static void nv_particle_emmiter_sphere( const particle_emmiter_data* pe, particle* p, uint32 count )
|
---|
90 | {
|
---|
91 | random& r = random::get();
|
---|
92 | for ( uint32 i = 0; i < count; ++i )
|
---|
93 | {
|
---|
94 | vec3 rsphere = r.sphere_point( pe->precise ) * pe->extents[0];
|
---|
95 | p[i].position =
|
---|
96 | rsphere.x * pe->cdir +
|
---|
97 | rsphere.y * pe->dir +
|
---|
98 | rsphere.z * pe->odir;
|
---|
99 | }
|
---|
100 | }
|
---|
101 |
|
---|
102 | static void nv_particle_emmiter_cylindroid( const particle_emmiter_data* pe, particle* p, uint32 count )
|
---|
103 | {
|
---|
104 | random& r = random::get();
|
---|
105 | for ( uint32 i = 0; i < count; ++i )
|
---|
106 | {
|
---|
107 | vec2 rellipse = r.ellipse_point( vec2( pe->hextents[0], pe->hextents[2] ), pe->precise );
|
---|
108 | p[i].position =
|
---|
109 | rellipse.x * pe->cdir +
|
---|
110 | r.frange( 0.0f, pe->extents[1] ) * pe->dir +
|
---|
111 | rellipse.y * pe->odir;
|
---|
112 | }
|
---|
113 | }
|
---|
114 |
|
---|
115 | static void nv_particle_emmiter_ellipsoid( const particle_emmiter_data* pe, particle* p, uint32 count )
|
---|
116 | {
|
---|
117 | random& r = random::get();
|
---|
118 | for ( uint32 i = 0; i < count; ++i )
|
---|
119 | {
|
---|
120 | vec3 rsphere = r.ellipsoid_point( pe->hextents, pe->precise );
|
---|
121 | p[i].position =
|
---|
122 | rsphere.x * pe->cdir +
|
---|
123 | rsphere.y * pe->dir +
|
---|
124 | rsphere.z * pe->odir;
|
---|
125 | }
|
---|
126 | }
|
---|
127 |
|
---|
128 | static void nv_particle_emmiter_hollow_cylinder( const particle_emmiter_data* pe, particle* p, uint32 count )
|
---|
129 | {
|
---|
130 | random& r = random::get();
|
---|
131 | for ( uint32 i = 0; i < count; ++i )
|
---|
132 | {
|
---|
133 | vec2 rellipse = r.hollow_disk_point(
|
---|
134 | pe->ihextents[0],
|
---|
135 | pe->hextents[0],
|
---|
136 | pe->precise );
|
---|
137 | p[i].position =
|
---|
138 | rellipse.x * pe->cdir +
|
---|
139 | r.frange( 0.0f, pe->extents[1] ) * pe->dir +
|
---|
140 | rellipse.y * pe->odir;
|
---|
141 | }
|
---|
142 | }
|
---|
143 |
|
---|
144 | static void nv_particle_emmiter_hollow_sphere( const particle_emmiter_data* pe, particle* p, uint32 count )
|
---|
145 | {
|
---|
146 | random& r = random::get();
|
---|
147 | for ( uint32 i = 0; i < count; ++i )
|
---|
148 | {
|
---|
149 | vec3 rellipse = r.hollow_sphere_point( pe->ihextents[0], pe->hextents[0], pe->precise );
|
---|
150 | p[i].position =
|
---|
151 | rellipse.x * pe->cdir +
|
---|
152 | rellipse.y * pe->dir +
|
---|
153 | rellipse.z * pe->odir;
|
---|
154 | }
|
---|
155 | }
|
---|
156 |
|
---|
157 | static void nv_particle_emmiter_hollow_cylindroid( const particle_emmiter_data* pe, particle* p, uint32 count )
|
---|
158 | {
|
---|
159 | random& r = random::get();
|
---|
160 | for ( uint32 i = 0; i < count; ++i )
|
---|
161 | {
|
---|
162 | vec2 rellipse = r.hollow_ellipse_point(
|
---|
163 | vec2( pe->ihextents[0], pe->ihextents[2] ),
|
---|
164 | vec2( pe->hextents[0], pe->hextents[2] ),
|
---|
165 | pe->precise );
|
---|
166 | p[i].position =
|
---|
167 | rellipse.x * pe->cdir +
|
---|
168 | r.frange( 0.0f, pe->extents[1] ) * pe->dir +
|
---|
169 | rellipse.y * pe->odir;
|
---|
170 | }
|
---|
171 | }
|
---|
172 |
|
---|
173 | static void nv_particle_emmiter_hollow_ellipsoid( const particle_emmiter_data* pe, particle* p, uint32 count )
|
---|
174 | {
|
---|
175 | random& r = random::get();
|
---|
176 | for ( uint32 i = 0; i < count; ++i )
|
---|
177 | {
|
---|
178 | vec3 rellipse = r.hollow_ellipsoid_point( pe->ihextents, pe->hextents, pe->precise );
|
---|
179 | p[i].position =
|
---|
180 | rellipse.x * pe->cdir +
|
---|
181 | rellipse.y * pe->dir +
|
---|
182 | rellipse.z * pe->odir;
|
---|
183 | }
|
---|
184 | }
|
---|
185 |
|
---|
186 | struct nvpe_linear_force_data
|
---|
187 | {
|
---|
188 | nv::vec3 force_vector;
|
---|
189 | bool average;
|
---|
190 | };
|
---|
191 |
|
---|
192 | static bool nv_particle_affector_linear_force_init( lua::table_guard* table, particle_affector_data* data )
|
---|
193 | {
|
---|
194 | nvpe_linear_force_data* datap = ((nvpe_linear_force_data*)data->paramters);
|
---|
195 | datap->force_vector = table->get<vec3>("force_vector", vec3() );
|
---|
196 | datap->average = table->get<bool>("average", false );
|
---|
197 | return true;
|
---|
198 | }
|
---|
199 |
|
---|
200 | static void nv_particle_affector_linear_force( const particle_affector_data* data, particle* p, float factor, uint32 count )
|
---|
201 | {
|
---|
202 | nvpe_linear_force_data* datap = ((nvpe_linear_force_data*)data->paramters);
|
---|
203 | if ( datap->average )
|
---|
204 | {
|
---|
205 | float norm_factor = glm::min( factor, 1.0f );
|
---|
206 | for ( uint32 i = 0; i < count; ++i )
|
---|
207 | p[i].velocity = datap->force_vector * norm_factor + p[i].velocity * ( 1.0f - norm_factor );
|
---|
208 | }
|
---|
209 | else
|
---|
210 | {
|
---|
211 | vec3 scvector = datap->force_vector * factor;
|
---|
212 | for ( uint32 i = 0; i < count; ++i ) p[i].velocity += scvector;
|
---|
213 | }
|
---|
214 | }
|
---|
215 |
|
---|
216 | struct nvpe_deflector_plane_data
|
---|
217 | {
|
---|
218 | nv::vec3 plane_point;
|
---|
219 | nv::vec3 plane_normal;
|
---|
220 | float bounce;
|
---|
221 | float distance;
|
---|
222 | };
|
---|
223 |
|
---|
224 | static bool nv_particle_affector_deflector_plane_init( lua::table_guard* table, particle_affector_data* data )
|
---|
225 | {
|
---|
226 | nvpe_deflector_plane_data* datap = ((nvpe_deflector_plane_data*)data->paramters);
|
---|
227 | datap->plane_point = table->get<vec3>("plane_point", vec3() );
|
---|
228 | datap->plane_normal = table->get<vec3>("plane_normal", vec3(0.0f,1.0f,0.0f) );
|
---|
229 | datap->plane_normal = normalize_safe( datap->plane_normal, vec3(0.0f,1.0f,0.0f) );
|
---|
230 | datap->bounce = table->get<float>("bounce", 0.0f );
|
---|
231 | datap->distance = -glm::dot( datap->plane_normal, datap->plane_point ) / glm::sqrt(glm::dot( datap->plane_normal, datap->plane_normal ) );
|
---|
232 | return true;
|
---|
233 | }
|
---|
234 |
|
---|
235 | static void nv_particle_affector_deflector_plane( const particle_affector_data* data, particle* p, float factor, uint32 count )
|
---|
236 | {
|
---|
237 | nvpe_deflector_plane_data* datap = ((nvpe_deflector_plane_data*)data->paramters);
|
---|
238 | for ( uint32 i = 0; i < count; ++i )
|
---|
239 | {
|
---|
240 | particle& pt = p[i];
|
---|
241 | vec3 direction = pt.velocity * factor;
|
---|
242 | if ( glm::dot( datap->plane_normal, pt.position + direction ) + datap->distance <= 0.0f )
|
---|
243 | {
|
---|
244 | float val = glm::dot( datap->plane_normal, pt.position ) + datap->distance;
|
---|
245 | if ( val > 0.0f )
|
---|
246 | {
|
---|
247 | vec3 part_dir = direction * ( -val / glm::dot( datap->plane_normal, direction ) );
|
---|
248 | pt.position = pt.position + part_dir + ( part_dir - direction ) * datap->bounce;
|
---|
249 | pt.velocity = glm::reflect( pt.velocity, datap->plane_normal ) * datap->bounce;
|
---|
250 | }
|
---|
251 | }
|
---|
252 | }
|
---|
253 | }
|
---|
254 |
|
---|
255 | struct nvpe_color_fader_data
|
---|
256 | {
|
---|
257 | nv::vec4 adjustment;
|
---|
258 | };
|
---|
259 |
|
---|
260 | static bool nv_particle_affector_color_fader_init( lua::table_guard* table, particle_affector_data* data )
|
---|
261 | {
|
---|
262 | nvpe_color_fader_data* datap = ((nvpe_color_fader_data*)data->paramters);
|
---|
263 | datap->adjustment = table->get<vec4>("adjustment", vec4() );
|
---|
264 | return true;
|
---|
265 | }
|
---|
266 |
|
---|
267 | static void nv_particle_affector_color_fader( const particle_affector_data* data, particle* p, float factor, uint32 count )
|
---|
268 | {
|
---|
269 | nvpe_color_fader_data* datap = ((nvpe_color_fader_data*)data->paramters);
|
---|
270 | vec4 adjustment = datap->adjustment * factor;
|
---|
271 | for ( uint32 i = 0; i < count; ++i )
|
---|
272 | {
|
---|
273 | p[i].color = glm::clamp( p[i].color + adjustment, 0.0f, 1.0f );
|
---|
274 | }
|
---|
275 | }
|
---|
276 |
|
---|
277 | struct nvpe_scaler_data
|
---|
278 | {
|
---|
279 | nv::vec2 adjustment;
|
---|
280 | };
|
---|
281 |
|
---|
282 | static bool nv_particle_affector_scaler_init( lua::table_guard* table, particle_affector_data* data )
|
---|
283 | {
|
---|
284 | nvpe_scaler_data* datap = ((nvpe_scaler_data*)data->paramters);
|
---|
285 | float rate = table->get<float>("rate", 0.0f );
|
---|
286 | datap->adjustment = table->get<vec2>("adjustment", vec2(rate,rate) );
|
---|
287 | return true;
|
---|
288 | }
|
---|
289 |
|
---|
290 | static void nv_particle_affector_scaler( const particle_affector_data* data, particle* p, float factor, uint32 count )
|
---|
291 | {
|
---|
292 | nvpe_scaler_data* datap = ((nvpe_scaler_data*)data->paramters);
|
---|
293 | vec2 adjustment = datap->adjustment * factor;
|
---|
294 | for ( uint32 i = 0; i < count; ++i )
|
---|
295 | {
|
---|
296 | p[i].size = glm::max( p[i].size + adjustment, vec2() );
|
---|
297 | }
|
---|
298 | }
|
---|
299 |
|
---|
300 | void nv::particle_engine::load( lua::table_guard& table )
|
---|
301 | {
|
---|
302 | std::string id = table.get_std_string( "id" );
|
---|
303 | if ( id == "" )
|
---|
304 | {
|
---|
305 | NV_LOG_ERROR( "Bad table passed to particle_engine!" )
|
---|
306 | }
|
---|
307 | // TODO : overwrite check
|
---|
308 | m_names[ id ] = m_data.size();
|
---|
309 |
|
---|
310 | m_data.emplace_back();
|
---|
311 | auto& data = m_data.back();
|
---|
312 |
|
---|
313 | data.quota = table.get<uint32>("quota", 1024 );
|
---|
314 | data.local = table.get<bool>("local_space", false );
|
---|
315 | data.accurate_facing = table.get<bool>("accurate_facing", false );
|
---|
316 | data.emmiter_count = 0;
|
---|
317 | data.affector_count = 0;
|
---|
318 |
|
---|
319 | const_string orientation = table.get_string( "orientation", "point" );
|
---|
320 | if ( orientation == "point" ) { data.orientation = particle_orientation::POINT; }
|
---|
321 | else if ( orientation == "oriented" ) { data.orientation = particle_orientation::ORIENTED; }
|
---|
322 | else if ( orientation == "oriented_common" ) { data.orientation = particle_orientation::ORIENTED_COMMON; }
|
---|
323 | else if ( orientation == "perpendicular" ) { data.orientation = particle_orientation::PERPENDICULAR; }
|
---|
324 | else if ( orientation == "perpendicular_common" ) { data.orientation = particle_orientation::PERPENDICULAR_COMMON; }
|
---|
325 | else
|
---|
326 | {
|
---|
327 | NV_LOG_ERROR( "Unknown orientation type! (", orientation, ")!" );
|
---|
328 | data.orientation = particle_orientation::POINT;
|
---|
329 | }
|
---|
330 |
|
---|
331 | const_string origin = table.get_string( "origin", "center" );
|
---|
332 | if ( origin == "center" ) { data.origin = particle_origin::CENTER; }
|
---|
333 | else if ( origin == "top_left" ) { data.origin = particle_origin::TOP_LEFT; }
|
---|
334 | else if ( origin == "top_center" ) { data.origin = particle_origin::TOP_CENTER; }
|
---|
335 | else if ( origin == "top_right" ) { data.origin = particle_origin::TOP_RIGHT; }
|
---|
336 | else if ( origin == "center_left" ) { data.origin = particle_origin::CENTER_LEFT; }
|
---|
337 | else if ( origin == "center_right" ) { data.origin = particle_origin::CENTER_RIGHT; }
|
---|
338 | else if ( origin == "bottom_left" ) { data.origin = particle_origin::BOTTOM_LEFT; }
|
---|
339 | else if ( origin == "bottom_center" ) { data.origin = particle_origin::BOTTOM_CENTER; }
|
---|
340 | else if ( origin == "bottom_right" ) { data.origin = particle_origin::BOTTOM_RIGHT; }
|
---|
341 | else
|
---|
342 | {
|
---|
343 | NV_LOG_ERROR( "Unknown particle origin! (", origin, ")!" );
|
---|
344 | data.origin = particle_origin::CENTER;
|
---|
345 | }
|
---|
346 |
|
---|
347 | data.common_up = glm::normalize( table.get<vec3>("common_up", vec3(1,0,0) ) );
|
---|
348 | data.common_dir = glm::normalize( table.get<vec3>("common_dir", vec3(0,1,0) ) );
|
---|
349 |
|
---|
350 | vec2 def_size = table.get<vec2>("size", vec2(0.1,0.1) );
|
---|
351 | uint32 elements = table.get_size();
|
---|
352 | for ( uint32 i = 0; i < elements; ++i )
|
---|
353 | {
|
---|
354 | lua::table_guard element( table, i+1 );
|
---|
355 | const_string type = element.get_string("type");
|
---|
356 | std::string sub_type = element.get_std_string("sub_type");
|
---|
357 | if ( type == "emmiter" )
|
---|
358 | {
|
---|
359 | if ( data.emmiter_count < MAX_PARTICLE_EMMITERS )
|
---|
360 | {
|
---|
361 | particle_emmiter_data& edata = data.emmiters[ data.emmiter_count ];
|
---|
362 | auto emmiter_iter = m_emmiters.find( sub_type );
|
---|
363 | if ( emmiter_iter != m_emmiters.end() )
|
---|
364 | {
|
---|
365 | edata.emmiter_func = emmiter_iter->second;
|
---|
366 | }
|
---|
367 | else
|
---|
368 | {
|
---|
369 | edata.emmiter_func = nv_particle_emmiter_point;
|
---|
370 | NV_LOG_WARNING( "Unknown emmiter type in particle system! (", sub_type, ")" );
|
---|
371 | }
|
---|
372 |
|
---|
373 | edata.position = element.get<vec3>("position", vec3() );
|
---|
374 | edata.extents = element.get<vec3>("extents", vec3(1,1,1) );
|
---|
375 | edata.extents[0] = element.get<float>("width", edata.extents[0] );
|
---|
376 | edata.extents[1] = element.get<float>("depth", edata.extents[1] );
|
---|
377 | edata.extents[2] = element.get<float>("height", edata.extents[2] );
|
---|
378 | edata.extents[0] = element.get<float>("radius", edata.extents[0] );
|
---|
379 | edata.iextents = element.get<vec3>("inner_extents", vec3() );
|
---|
380 | edata.iextents[0] = element.get<float>("inner_width", edata.iextents[0] );
|
---|
381 | edata.iextents[1] = element.get<float>("inner_depth", edata.iextents[1] );
|
---|
382 | edata.iextents[2] = element.get<float>("inner_height", edata.iextents[2] );
|
---|
383 | edata.iextents[0] = element.get<float>("inner_radius", edata.iextents[0] );
|
---|
384 | edata.hextents = 0.5f * edata.extents;
|
---|
385 | edata.ihextents = 0.5f * edata.iextents;
|
---|
386 | edata.precise = element.get<bool>("precise", false );
|
---|
387 | edata.square = element.get<bool>("square", true );
|
---|
388 | vec4 color = element.get<vec4>("color", vec4(1,1,1,1) );
|
---|
389 | edata.color_min = element.get<vec4>("color_min", color );
|
---|
390 | edata.color_max = element.get<vec4>("color_max", color );
|
---|
391 | vec2 size = element.get<vec2>("size", def_size );
|
---|
392 | edata.size_min = element.get<vec2>("size_min", size );
|
---|
393 | edata.size_max = element.get<vec2>("size_max", size );
|
---|
394 | edata.angle = element.get<float>("angle", 0.0f );
|
---|
395 | float velocity = element.get<float>("velocity", 0.0f );
|
---|
396 | edata.velocity_min = element.get<float>("velocity_min", velocity );
|
---|
397 | edata.velocity_max = element.get<float>("velocity_max", velocity );
|
---|
398 | float lifetime = element.get<float>("lifetime", 1.0f );
|
---|
399 | edata.lifetime_min = uint32( element.get<float>("lifetime_min", lifetime ) * 1000.f );
|
---|
400 | edata.lifetime_max = uint32( element.get<float>("lifetime_max", lifetime ) * 1000.f );
|
---|
401 | float duration = element.get<float>("duration", 0.0f );
|
---|
402 | edata.duration_min = uint32( element.get<float>("duration_min", duration ) * 1000.f );
|
---|
403 | edata.duration_max = uint32( element.get<float>("duration_max", duration ) * 1000.f );
|
---|
404 | float repeat = element.get<float>("repeat_delay", 0.0f );
|
---|
405 | edata.repeat_min = uint32( element.get<float>("repeat_delay_min", repeat ) * 1000.f );
|
---|
406 | edata.repeat_max = uint32( element.get<float>("repeat_delay_max", repeat ) * 1000.f );
|
---|
407 |
|
---|
408 | edata.rate = element.get<float>("rate", 1.0f );
|
---|
409 | edata.dir = glm::normalize( element.get<vec3>("direction", vec3(0,1,0) ) );
|
---|
410 |
|
---|
411 | edata.odir = glm::vec3( 0, 0, 1 );
|
---|
412 | if ( edata.dir != vec3( 0, 1, 0 ) && edata.dir != vec3( 0, -1, 0 ) )
|
---|
413 | edata.odir = glm::normalize( glm::cross( edata.dir, vec3( 0, 1, 0 ) ) ); edata.cdir = glm::cross( edata.dir, edata.odir );
|
---|
414 |
|
---|
415 | data.emmiter_count++;
|
---|
416 | }
|
---|
417 | else
|
---|
418 | {
|
---|
419 | NV_LOG_ERROR( "Too many emmiters (", MAX_PARTICLE_EMMITERS, " is MAX)!" );
|
---|
420 | }
|
---|
421 | }
|
---|
422 | else if ( type == "affector" )
|
---|
423 | {
|
---|
424 | if ( data.affector_count < MAX_PARTICLE_AFFECTORS )
|
---|
425 | {
|
---|
426 | particle_affector_data& adata = data.affectors[ data.affector_count ];
|
---|
427 | data.affector_count++;
|
---|
428 | auto affector_iter = m_affectors.find( sub_type );
|
---|
429 | if ( affector_iter != m_affectors.end() )
|
---|
430 | {
|
---|
431 | adata.process = affector_iter->second.process;
|
---|
432 | if ( !affector_iter->second.init( &element, &adata ) )
|
---|
433 | {
|
---|
434 | data.affector_count--;
|
---|
435 | NV_LOG_WARNING( "Bad data passed to ", sub_type, " affector in particle system!" );
|
---|
436 | }
|
---|
437 | }
|
---|
438 | else
|
---|
439 | {
|
---|
440 | data.affector_count--;
|
---|
441 | NV_LOG_WARNING( "Unknown affector type in particle system! (", sub_type, ")" );
|
---|
442 | }
|
---|
443 | }
|
---|
444 | else
|
---|
445 | {
|
---|
446 | NV_LOG_ERROR( "Too many affectors (", MAX_PARTICLE_AFFECTORS, " is MAX)!" );
|
---|
447 | }
|
---|
448 | }
|
---|
449 | else
|
---|
450 | {
|
---|
451 | NV_LOG_WARNING( "Unknown element in particle system! (", type, ")" );
|
---|
452 | }
|
---|
453 | }
|
---|
454 |
|
---|
455 | }
|
---|
456 |
|
---|
457 | nv::particle_engine::particle_engine( context* a_context )
|
---|
458 | {
|
---|
459 | m_context = a_context;
|
---|
460 | m_device = a_context->get_device();
|
---|
461 | m_program_local = m_device->create_program( nv_particle_engine_vertex_shader_local, nv_particle_engine_fragment_shader );
|
---|
462 | m_program_world = m_device->create_program( nv_particle_engine_vertex_shader_world, nv_particle_engine_fragment_shader );
|
---|
463 | m_last_update = 0;
|
---|
464 |
|
---|
465 | register_standard_emmiters();
|
---|
466 | register_standard_affectors();
|
---|
467 | }
|
---|
468 |
|
---|
469 | nv::particle_system nv::particle_engine::create_system( const std::string& id )
|
---|
470 | {
|
---|
471 | auto it = m_names.find( id );
|
---|
472 | if ( it == m_names.end() )
|
---|
473 | {
|
---|
474 | return particle_system();
|
---|
475 | }
|
---|
476 | const particle_system_data* data = &(m_data[it->second]);
|
---|
477 | particle_system result = m_systems.create();
|
---|
478 | particle_system_info* info = m_systems.get( result );
|
---|
479 |
|
---|
480 | info->data = data;
|
---|
481 | uint32 ecount = data->emmiter_count;
|
---|
482 | for ( uint32 i = 0; i < ecount; ++i )
|
---|
483 | {
|
---|
484 | info->emmiters[i].active = true;
|
---|
485 | info->emmiters[i].last_create = float( m_last_update );
|
---|
486 | if ( data->emmiters[i].duration_max == 0 )
|
---|
487 | info->emmiters[i].next_toggle = 0;
|
---|
488 | else
|
---|
489 | info->emmiters[i].next_toggle = m_last_update + random::get().urange( data->emmiters[i].duration_min, data->emmiters[i].duration_max );
|
---|
490 |
|
---|
491 | }
|
---|
492 |
|
---|
493 | info->count = 0;
|
---|
494 | info->particles = new particle[ data->quota ];
|
---|
495 | info->quads = new particle_quad[ data->quota ];
|
---|
496 | info->vtx_array = m_context->create_vertex_array<particle_vtx>(
|
---|
497 | (particle_vtx*)info->quads, data->quota*6, STREAM_DRAW );
|
---|
498 | info->vtx_buffer = m_context->find_buffer( info->vtx_array, slot::POSITION );
|
---|
499 | info->last_update = m_last_update;
|
---|
500 | info->test = false;
|
---|
501 | // result->m_own_va = true;
|
---|
502 | // result->m_offset = 0;
|
---|
503 |
|
---|
504 | return result;
|
---|
505 | }
|
---|
506 |
|
---|
507 | void nv::particle_engine::draw( particle_system system, const render_state& rs, const scene_state& ss )
|
---|
508 | {
|
---|
509 | particle_system_info* info = m_systems.get( system );
|
---|
510 | if ( info )
|
---|
511 | {
|
---|
512 | m_context->draw( nv::TRIANGLES, rs, ss, info->data->local ? m_program_local : m_program_world, info->vtx_array, info->count * 6 );
|
---|
513 | }
|
---|
514 | }
|
---|
515 |
|
---|
516 | nv::particle_engine::~particle_engine()
|
---|
517 | {
|
---|
518 | clear();
|
---|
519 | m_device->release( m_program_world );
|
---|
520 | m_device->release( m_program_local );
|
---|
521 | }
|
---|
522 |
|
---|
523 | void nv::particle_engine::reset()
|
---|
524 | {
|
---|
525 | clear();
|
---|
526 | register_standard_emmiters();
|
---|
527 | register_standard_affectors();
|
---|
528 | }
|
---|
529 |
|
---|
530 | void nv::particle_engine::clear()
|
---|
531 | {
|
---|
532 | while ( m_systems.size() > 0 )
|
---|
533 | release( m_systems.get_handle( 0 ) );
|
---|
534 | m_emmiters.clear();
|
---|
535 | m_affectors.clear();
|
---|
536 | m_names.clear();
|
---|
537 | m_data.clear();
|
---|
538 | m_last_update = 0;
|
---|
539 | }
|
---|
540 |
|
---|
541 |
|
---|
542 | void nv::particle_engine::release( particle_system system )
|
---|
543 | {
|
---|
544 | particle_system_info* info = m_systems.get( system );
|
---|
545 | if ( info )
|
---|
546 | {
|
---|
547 | delete[] info->particles;
|
---|
548 | delete[] info->quads;
|
---|
549 | //if ( system->own_va )
|
---|
550 | m_context->release( info->vtx_array );
|
---|
551 | m_systems.destroy( system );
|
---|
552 | }
|
---|
553 | }
|
---|
554 |
|
---|
555 | void nv::particle_engine::update( particle_system system, const scene_state& s, uint32 ms )
|
---|
556 | {
|
---|
557 | particle_system_info* info = m_systems.get( system );
|
---|
558 | m_last_update += ms;
|
---|
559 | if ( info )
|
---|
560 | {
|
---|
561 | m_view_matrix = s.get_view();
|
---|
562 | m_model_matrix = s.get_model();
|
---|
563 | m_camera_pos = s.get_camera().get_position();
|
---|
564 | m_inv_view_dir = glm::normalize(-s.get_camera().get_direction());
|
---|
565 |
|
---|
566 | update_emmiters( info, m_last_update );
|
---|
567 | destroy_particles( info, m_last_update );
|
---|
568 | create_particles( info, m_last_update );
|
---|
569 | update_particles( info, m_last_update );
|
---|
570 |
|
---|
571 | generate_data( info );
|
---|
572 | m_context->update( info->vtx_buffer, info->quads, /*system->m_offset*sizeof(particle_quad)*/ 0, info->count*sizeof(particle_quad) );
|
---|
573 | }
|
---|
574 | }
|
---|
575 |
|
---|
576 | void nv::particle_engine::set_texcoords( particle_system system, vec2 a, vec2 b )
|
---|
577 | {
|
---|
578 | particle_system_info* info = m_systems.get( system );
|
---|
579 | if ( info )
|
---|
580 | {
|
---|
581 | vec2 texcoords[4] = { a, vec2( b.x, a.y ), vec2( a.x, b.y ), b };
|
---|
582 |
|
---|
583 | for ( uint32 i = 0; i < info->data->quota; ++i )
|
---|
584 | {
|
---|
585 | particle_quad& rdata = info->quads[i];
|
---|
586 | rdata.data[0].texcoord = texcoords[0];
|
---|
587 | rdata.data[1].texcoord = texcoords[1];
|
---|
588 | rdata.data[2].texcoord = texcoords[2];
|
---|
589 | rdata.data[3].texcoord = texcoords[3];
|
---|
590 | rdata.data[4].texcoord = texcoords[2];
|
---|
591 | rdata.data[5].texcoord = texcoords[1];
|
---|
592 | }
|
---|
593 | }
|
---|
594 | }
|
---|
595 |
|
---|
596 | void nv::particle_engine::generate_data( particle_system_info* info )
|
---|
597 | {
|
---|
598 | vec2 lb = vec2( -0.5f, -0.5f );
|
---|
599 | vec2 rt = vec2( 0.5f, 0.5f );
|
---|
600 |
|
---|
601 | switch ( info->data->origin )
|
---|
602 | {
|
---|
603 | case particle_origin::CENTER : break;
|
---|
604 | case particle_origin::TOP_LEFT : lb = vec2(0.f,-1.f); rt = vec2(1.f,0.f); break;
|
---|
605 | case particle_origin::TOP_CENTER : lb.y = -1.f; rt.y = 0.f; break;
|
---|
606 | case particle_origin::TOP_RIGHT : lb = vec2(-1.f,-1.f); rt = vec2(); break;
|
---|
607 | case particle_origin::CENTER_LEFT : lb.x = 0.f; rt.x = 1.f; break;
|
---|
608 | case particle_origin::CENTER_RIGHT : lb.x = -1.f; rt.x = 0.f; break;
|
---|
609 | case particle_origin::BOTTOM_LEFT : lb = vec2(); rt = vec2(1.f,1.f); break;
|
---|
610 | case particle_origin::BOTTOM_CENTER : lb.y = 0.f; rt.y = 1.f; break;
|
---|
611 | case particle_origin::BOTTOM_RIGHT : lb = vec2(-1.f,0.f); rt = vec2(.0f,1.f); break;
|
---|
612 | }
|
---|
613 |
|
---|
614 | const vec3 sm[4] =
|
---|
615 | {
|
---|
616 | vec3( lb.x, lb.y, 0.0f ),
|
---|
617 | vec3( rt.x, lb.y, 0.0f ),
|
---|
618 | vec3( lb.x, rt.y, 0.0f ),
|
---|
619 | vec3( rt.x, rt.y, 0.0f ),
|
---|
620 | };
|
---|
621 | vec3 z( 0.0f, 0.0f ,1.0f );
|
---|
622 |
|
---|
623 | particle_orientation orientation = info->data->orientation;
|
---|
624 | vec3 common_up ( info->data->common_up );
|
---|
625 | vec3 common_dir( info->data->common_dir );
|
---|
626 | bool accurate_facing = info->data->accurate_facing;
|
---|
627 | mat3 rot_mat;
|
---|
628 | vec3 right;
|
---|
629 | vec3 pdir( 0.0f, 1.0f, 0.0f );
|
---|
630 |
|
---|
631 | for ( uint32 i = 0; i < info->count; ++i )
|
---|
632 | {
|
---|
633 | const particle& pdata = info->particles[i];
|
---|
634 | particle_quad& rdata = info->quads[i];
|
---|
635 |
|
---|
636 | vec3 view_dir( m_inv_view_dir );
|
---|
637 | if ( accurate_facing ) view_dir = glm::normalize( m_camera_pos - pdata.position );
|
---|
638 |
|
---|
639 | switch ( orientation )
|
---|
640 | {
|
---|
641 | case particle_orientation::POINT :
|
---|
642 | right = glm::normalize( glm::cross( view_dir, vec3( 0, 1, 0 ) ) );
|
---|
643 | rot_mat = mat3( right, glm::cross( right, -view_dir ), -view_dir );
|
---|
644 | break;
|
---|
645 | case particle_orientation::ORIENTED :
|
---|
646 | pdir = normalize_safe( pdata.velocity, pdir );
|
---|
647 | right = glm::normalize( glm::cross( pdir, view_dir ) );
|
---|
648 | rot_mat = mat3( right, pdir, glm::cross( pdir, right ) );
|
---|
649 | break;
|
---|
650 | case particle_orientation::ORIENTED_COMMON :
|
---|
651 | right = glm::normalize( glm::cross( common_dir, view_dir ) );
|
---|
652 | rot_mat = mat3( right, common_dir, glm::cross( common_dir, right ) );
|
---|
653 | break;
|
---|
654 | case particle_orientation::PERPENDICULAR :
|
---|
655 | pdir = normalize_safe( pdata.velocity, pdir );
|
---|
656 | right = glm::normalize( glm::cross( common_up, pdir ) );
|
---|
657 | rot_mat = mat3( right, common_up, glm::cross( common_up, right ) );
|
---|
658 | break;
|
---|
659 | case particle_orientation::PERPENDICULAR_COMMON :
|
---|
660 | right = glm::normalize( glm::cross( common_up, common_dir ) );
|
---|
661 | rot_mat = mat3( right, common_up, glm::cross( common_up, right ) );
|
---|
662 | break;
|
---|
663 | }
|
---|
664 |
|
---|
665 | vec3 size( pdata.size.x, pdata.size.y, 0.0f );
|
---|
666 | vec3 s0 = rot_mat * ( ( size * sm[0] ) );
|
---|
667 | vec3 s1 = rot_mat * ( ( size * sm[1] ) );
|
---|
668 | vec3 s2 = rot_mat * ( ( size * sm[2] ) );
|
---|
669 | vec3 s3 = rot_mat * ( ( size * sm[3] ) );
|
---|
670 |
|
---|
671 | rdata.data[0].position = pdata.position + s0;
|
---|
672 | rdata.data[0].color = pdata.color;
|
---|
673 |
|
---|
674 | rdata.data[1].position = pdata.position + s1;
|
---|
675 | rdata.data[1].color = pdata.color;
|
---|
676 |
|
---|
677 | rdata.data[2].position = pdata.position + s2;
|
---|
678 | rdata.data[2].color = pdata.color;
|
---|
679 |
|
---|
680 | rdata.data[3].position = pdata.position + s3;
|
---|
681 | rdata.data[3].color = pdata.color;
|
---|
682 |
|
---|
683 | rdata.data[4] = rdata.data[2];
|
---|
684 | rdata.data[5] = rdata.data[1];
|
---|
685 | }
|
---|
686 | }
|
---|
687 |
|
---|
688 | void nv::particle_engine::destroy_particles( particle_system_info* info, uint32 ms )
|
---|
689 | {
|
---|
690 | if ( info->count > 0 )
|
---|
691 | for ( sint32 i = (sint32)info->count-1; i >= 0; --i )
|
---|
692 | {
|
---|
693 | particle& pinfo = info->particles[i];
|
---|
694 | if ( //pdata.position.y < 0.0f ||
|
---|
695 | ms >= pinfo.death )
|
---|
696 | {
|
---|
697 | info->count--;
|
---|
698 | std::swap( info->particles[i], info->particles[info->count] );
|
---|
699 | }
|
---|
700 | }
|
---|
701 | }
|
---|
702 |
|
---|
703 | void nv::particle_engine::create_particles( particle_system_info* info, uint32 ms )
|
---|
704 | {
|
---|
705 | uint32 ecount = info->data->emmiter_count;
|
---|
706 | if ( ecount == 0 ) return;
|
---|
707 |
|
---|
708 | random& r = random::get();
|
---|
709 | vec3 source;
|
---|
710 | mat3 orient;
|
---|
711 | bool local = info->data->local;
|
---|
712 | if ( !local )
|
---|
713 | {
|
---|
714 | source = vec3( m_model_matrix[3] );
|
---|
715 | orient = mat3( m_model_matrix );
|
---|
716 | }
|
---|
717 |
|
---|
718 | float fms = float(ms);
|
---|
719 |
|
---|
720 | for ( uint32 i = 0; i < ecount; ++i )
|
---|
721 | {
|
---|
722 | const auto& edata = info->data->emmiters[i];
|
---|
723 | auto& einfo = info->emmiters[i];
|
---|
724 | if ( einfo.active )
|
---|
725 | {
|
---|
726 | float period = 1000.f / edata.rate;
|
---|
727 | while ( fms - einfo.last_create > period )
|
---|
728 | {
|
---|
729 | if ( info->count < info->data->quota-1 )
|
---|
730 | {
|
---|
731 | particle& pinfo = info->particles[info->count];
|
---|
732 | edata.emmiter_func( &(info->data->emmiters[i]), &pinfo, 1 );
|
---|
733 |
|
---|
734 | if ( !local ) pinfo.position = orient * pinfo.position + source;
|
---|
735 | pinfo.position += edata.position;
|
---|
736 | pinfo.color = edata.color_min == edata.color_max ?
|
---|
737 | edata.color_min : r.range( edata.color_min, edata.color_max );
|
---|
738 | pinfo.size = edata.size_min == edata.size_max ?
|
---|
739 | edata.size_min : r.range( edata.size_min, edata.size_max );
|
---|
740 | if ( edata.square ) pinfo.size.y = pinfo.size.x;
|
---|
741 | float velocity = edata.velocity_min == edata.velocity_max ?
|
---|
742 | edata.velocity_min : r.frange( edata.velocity_min, edata.velocity_max );
|
---|
743 | pinfo.death = ms + ( edata.lifetime_min == edata.lifetime_max ?
|
---|
744 | edata.lifetime_min : r.urange( edata.lifetime_min, edata.lifetime_max ) );
|
---|
745 | //pinfo.rotation = r.frand( 360.0f );
|
---|
746 |
|
---|
747 | pinfo.velocity = edata.dir;
|
---|
748 | if ( edata.angle > 0.0f )
|
---|
749 | {
|
---|
750 | float emission_angle = glm::radians( edata.angle );
|
---|
751 | float cos_theta = r.frange( glm::cos( emission_angle ), 1.0f );
|
---|
752 | float sin_theta = glm::sqrt(1.0f - cos_theta * cos_theta );
|
---|
753 | float phi = r.frange( 0.0f, 2*glm::pi<float>() );
|
---|
754 | pinfo.velocity = orient *
|
---|
755 | ( edata.odir * ( glm::cos(phi) * sin_theta ) +
|
---|
756 | edata.cdir * ( glm::sin(phi)*sin_theta ) +
|
---|
757 | edata.dir * cos_theta );
|
---|
758 | }
|
---|
759 |
|
---|
760 | pinfo.velocity *= velocity;
|
---|
761 |
|
---|
762 | info->count++;
|
---|
763 | }
|
---|
764 | einfo.last_create += period;
|
---|
765 | }
|
---|
766 | }
|
---|
767 | }
|
---|
768 | }
|
---|
769 |
|
---|
770 | void nv::particle_engine::update_particles( particle_system_info* info, uint32 ms )
|
---|
771 | {
|
---|
772 | uint32 ticks = ms - info->last_update;
|
---|
773 | if ( ticks == 0 ) return;
|
---|
774 | float factor = 0.001f * ticks;
|
---|
775 |
|
---|
776 | uint32 acount = info->data->affector_count;
|
---|
777 | for ( uint32 i = 0; i < acount; ++i )
|
---|
778 | {
|
---|
779 | const particle_affector_data* padata = &(info->data->affectors[i]);
|
---|
780 | padata->process( padata, info->particles, factor, info->count );
|
---|
781 | }
|
---|
782 |
|
---|
783 |
|
---|
784 | for ( uint32 i = 0; i < info->count; ++i )
|
---|
785 | {
|
---|
786 | particle& pdata = info->particles[i];
|
---|
787 | pdata.position += pdata.velocity * factor;
|
---|
788 | }
|
---|
789 | info->last_update = ms;
|
---|
790 | }
|
---|
791 |
|
---|
792 | void nv::particle_engine::update_emmiters( particle_system_info* info, uint32 ms )
|
---|
793 | {
|
---|
794 | uint32 ecount = info->data->emmiter_count;
|
---|
795 | if ( ecount == 0 ) return;
|
---|
796 | random& r = random::get();
|
---|
797 |
|
---|
798 | for ( uint32 i = 0; i < ecount; ++i )
|
---|
799 | {
|
---|
800 | const auto& edata = info->data->emmiters[i];
|
---|
801 | auto& einfo = info->emmiters[i];
|
---|
802 |
|
---|
803 | if ( einfo.next_toggle != 0 && ms >= einfo.next_toggle )
|
---|
804 | {
|
---|
805 | if ( einfo.active )
|
---|
806 | {
|
---|
807 | einfo.active = false;
|
---|
808 | if ( edata.repeat_min > 0 )
|
---|
809 | einfo.next_toggle += r.urange( edata.repeat_min, edata.repeat_max );
|
---|
810 | else
|
---|
811 | einfo.next_toggle = 0;
|
---|
812 | }
|
---|
813 | else
|
---|
814 | {
|
---|
815 | einfo.active = true;
|
---|
816 | einfo.last_create = float( einfo.next_toggle );
|
---|
817 | einfo.next_toggle += r.urange( edata.duration_min, edata.duration_max );
|
---|
818 | }
|
---|
819 | }
|
---|
820 | }
|
---|
821 |
|
---|
822 | }
|
---|
823 |
|
---|
824 | void nv::particle_engine::register_emmiter_type( const std::string& name, particle_emmiter_func func )
|
---|
825 | {
|
---|
826 | m_emmiters[ name ] = func;
|
---|
827 | }
|
---|
828 |
|
---|
829 | void nv::particle_engine::register_standard_emmiters()
|
---|
830 | {
|
---|
831 | register_emmiter_type( "point", nv_particle_emmiter_point );
|
---|
832 | register_emmiter_type( "box", nv_particle_emmiter_box );
|
---|
833 | register_emmiter_type( "cylinder", nv_particle_emmiter_cylinder );
|
---|
834 | register_emmiter_type( "sphere", nv_particle_emmiter_sphere );
|
---|
835 | register_emmiter_type( "cylindroid", nv_particle_emmiter_cylindroid );
|
---|
836 | register_emmiter_type( "ellipsoid", nv_particle_emmiter_ellipsoid );
|
---|
837 | register_emmiter_type( "hollow_cylinder", nv_particle_emmiter_hollow_cylinder );
|
---|
838 | register_emmiter_type( "hollow_sphere", nv_particle_emmiter_hollow_sphere );
|
---|
839 | register_emmiter_type( "hollow_cylindroid", nv_particle_emmiter_hollow_cylindroid );
|
---|
840 | register_emmiter_type( "hollow_ellipsoid", nv_particle_emmiter_hollow_ellipsoid );
|
---|
841 | }
|
---|
842 |
|
---|
843 | void nv::particle_engine::register_affector_type( const std::string& name, particle_affector_init_func init, particle_affector_func process )
|
---|
844 | {
|
---|
845 | m_affectors[ name ].init = init;
|
---|
846 | m_affectors[ name ].process = process;
|
---|
847 | }
|
---|
848 |
|
---|
849 | void nv::particle_engine::register_standard_affectors()
|
---|
850 | {
|
---|
851 | register_affector_type( "linear_force", nv_particle_affector_linear_force_init, nv_particle_affector_linear_force );
|
---|
852 | register_affector_type( "deflector_plane", nv_particle_affector_deflector_plane_init, nv_particle_affector_deflector_plane );
|
---|
853 | register_affector_type( "color_fader", nv_particle_affector_color_fader_init, nv_particle_affector_color_fader );
|
---|
854 | register_affector_type( "scaler", nv_particle_affector_scaler_init, nv_particle_affector_scaler );
|
---|
855 | }
|
---|
856 |
|
---|