source: trunk/src/engine/particle_engine.cc @ 534

Last change on this file since 534 was 534, checked in by epyon, 8 years ago

CONTINUED:

  • getting rid of size_t
  • datatypes now restricted to uint32 size
  • 64-bit compatibility
  • copyright updates where modified
File size: 23.7 KB
Line 
1// Copyright (C) 2014-2016 ChaosForge Ltd
2// http://chaosforge.org/
3//
4// This file is part of Nova libraries.
5// For conditions of distribution and use, see copying.txt file in root folder.
6
7#include "nv/engine/particle_engine.hh"
8
9#include <nv/interface/device.hh>
10#include <nv/core/random.hh>
11#include <nv/stl/utility.hh>
12#include <nv/lua/lua_math.hh>
13#include <nv/core/logging.hh>
14
15nv::hash_store< nv::shash64, nv::particle_emitter_func >   nv::particle_engine::m_emitters;
16nv::hash_store< nv::shash64, nv::particle_affector_funcs > nv::particle_engine::m_affectors;
17
18nv::vector< nv::particle_emitter_func >*  nv::particle_engine::m_debug_emitter_list;
19nv::vector< nv::particle_affector_func >* nv::particle_engine::m_debug_affector_list;
20
21
22nv::hash_map< nv::particle_emitter_func,  nv::const_string >* nv::particle_engine::m_debug_emitter_names  = nullptr;
23nv::hash_map< nv::particle_affector_func, nv::const_string >* nv::particle_engine::m_debug_affector_names = nullptr;
24nv::hash_map< nv::particle_affector_func, nv::type_entry* >*  nv::particle_engine::m_debug_affector_types = nullptr;
25
26using namespace nv;
27
28static void nv_particle_emitter_point( random_base*, const particle_emitter_data*, particle* p, uint32 count )
29{
30        for ( uint32 i = 0; i < count; ++i )
31        {
32                p[i].position = vec3();
33        }
34}
35
36static void nv_particle_emitter_box( random_base* r, const particle_emitter_data* pe, particle* p, uint32 count )
37{
38        for ( uint32 i = 0; i < count; ++i )
39        {
40                p[i].position =
41                        r->frange( -pe->hextents[0], pe->hextents[0] ) * pe->cdir +
42                        r->frange( 0.0f, pe->extents[1] ) * pe->dir +
43                        r->frange( -pe->hextents[2], pe->hextents[2] ) * pe->odir;
44        }
45}
46
47static void nv_particle_emitter_cylinder( random_base* r, const particle_emitter_data* pe, particle* p, uint32 count )
48{
49        for ( uint32 i = 0; i < count; ++i )
50        {
51                vec2 rellipse( r->disk_point( pe->precise ) * pe->extents[0] );
52                p[i].position =
53                        rellipse.x * pe->cdir +
54                        r->frange( 0.0f, pe->extents[1] ) * pe->dir +
55                        rellipse.y * pe->odir;
56        }
57}
58
59static void nv_particle_emitter_sphere( random_base* r, const particle_emitter_data* pe, particle* p, uint32 count )
60{
61        for ( uint32 i = 0; i < count; ++i )
62        {
63                vec3 rsphere = r->sphere_point( pe->precise ) * pe->extents[0];
64                p[i].position =
65                        rsphere.x * pe->cdir +
66                        rsphere.y * pe->dir +
67                        rsphere.z * pe->odir;
68        }
69}
70
71static void nv_particle_emitter_cylindroid( random_base* r, const particle_emitter_data* pe, particle* p, uint32 count )
72{
73        for ( uint32 i = 0; i < count; ++i )
74        {
75                vec2 rellipse = r->ellipse_point( vec2( pe->hextents[0], pe->hextents[2] ), pe->precise );
76                p[i].position =
77                        rellipse.x * pe->cdir +
78                        r->frange( 0.0f, pe->extents[1] ) * pe->dir +
79                        rellipse.y * pe->odir;
80        }
81}
82
83static void nv_particle_emitter_ellipsoid( random_base* r, const particle_emitter_data* pe, particle* p, uint32 count )
84{
85        for ( uint32 i = 0; i < count; ++i )
86        {
87                vec3 rsphere = r->ellipsoid_point( pe->hextents, pe->precise );
88                p[i].position =
89                        rsphere.x * pe->cdir +
90                        rsphere.y * pe->dir +
91                        rsphere.z * pe->odir;
92        }
93}
94
95static void nv_particle_emitter_hollow_cylinder( random_base* r, const particle_emitter_data* pe, particle* p, uint32 count )
96{
97        for ( uint32 i = 0; i < count; ++i )
98        {
99                vec2 rellipse = r->hollow_disk_point(
100                        pe->ihextents[0],
101                        pe->hextents[0],
102                        pe->precise );
103                p[i].position =
104                        rellipse.x * pe->cdir +
105                        r->frange( 0.0f, pe->extents[1] ) * pe->dir +
106                        rellipse.y * pe->odir;
107        }
108}
109
110static void nv_particle_emitter_hollow_sphere( random_base* r, const particle_emitter_data* pe, particle* p, uint32 count )
111{
112        for ( uint32 i = 0; i < count; ++i )
113        {
114                vec3 rellipse = r->hollow_sphere_point( pe->ihextents[0], pe->hextents[0], pe->precise );
115                p[i].position =
116                        rellipse.x * pe->cdir +
117                        rellipse.y * pe->dir +
118                        rellipse.z * pe->odir;
119        }
120}
121
122static void nv_particle_emitter_hollow_cylindroid( random_base* r, const particle_emitter_data* pe, particle* p, uint32 count )
123{
124        for ( uint32 i = 0; i < count; ++i )
125        {
126                vec2 rellipse = r->hollow_ellipse_point(
127                        vec2( pe->ihextents[0], pe->ihextents[2] ),
128                        vec2( pe->hextents[0], pe->hextents[2] ),
129                        pe->precise );
130                p[i].position =
131                        rellipse.x * pe->cdir +
132                        r->frange( 0.0f, pe->extents[1] ) * pe->dir +
133                        rellipse.y * pe->odir;
134        }
135}
136
137static void nv_particle_emitter_hollow_ellipsoid( random_base* r, const particle_emitter_data* pe, particle* p, uint32 count )
138{
139        for ( uint32 i = 0; i < count; ++i )
140        {
141                vec3 rellipse = r->hollow_ellipsoid_point( pe->ihextents, pe->hextents, pe->precise );
142                p[i].position =
143                        rellipse.x * pe->cdir +
144                        rellipse.y * pe->dir +
145                        rellipse.z * pe->odir;
146        }
147}
148
149struct nvpe_linear_force_data
150{
151        nv::vec3 force_vector;
152        bool     average;
153};
154
155NV_RTTI_DECLARE( nvpe_linear_force_data )
156
157static bool nv_particle_affector_linear_force_init( lua::table_guard* table, particle_affector_data* data )
158{
159        nvpe_linear_force_data* datap = reinterpret_cast<nvpe_linear_force_data*>( data->paramters );
160        datap->force_vector = table->get<vec3>("force_vector", vec3() );
161        datap->average      = table->get<bool>("average", false );
162        return true;
163}
164
165static void nv_particle_affector_linear_force( const particle_affector_data* data, particle* p, float factor, uint32 count )
166{
167        const nvpe_linear_force_data* datap = reinterpret_cast<const nvpe_linear_force_data*>( data->paramters );
168        if ( datap->average )
169        {
170                float norm_factor = nv::min( factor, 1.0f, p->lifetime );
171                for ( uint32 i = 0; i < count; ++i )
172                        p[i].velocity = datap->force_vector * norm_factor + p[i].velocity * ( 1.0f - norm_factor );
173        }
174        else
175        {
176                vec3 scvector = datap->force_vector * nv::min( factor, p->lifetime );
177                for ( uint32 i = 0; i < count; ++i ) p[i].velocity += scvector;
178        }
179}
180
181struct nvpe_deflector_plane_data
182{
183        nv::vec3 plane_point;
184        nv::vec3 plane_normal;
185        float    bounce;
186        float    distance;
187};
188
189NV_RTTI_DECLARE( nvpe_deflector_plane_data )
190
191static bool nv_particle_affector_deflector_plane_init( lua::table_guard* table, particle_affector_data* data )
192{
193        nvpe_deflector_plane_data* datap = reinterpret_cast<nvpe_deflector_plane_data*>( data->paramters );
194        datap->plane_point  = table->get<vec3>("plane_point",  vec3() );
195        datap->plane_normal = table->get<vec3>("plane_normal", vec3(0.0f,1.0f,0.0f) );
196        datap->plane_normal = normalize_safe( datap->plane_normal, vec3(0.0f,1.0f,0.0f) );
197        datap->bounce       = table->get<float>("bounce", 0.0f );
198        datap->distance     = -math::dot( datap->plane_normal, datap->plane_point ) / static_cast<float>( math::sqrt( math::dot( datap->plane_normal, datap->plane_normal ) ) );
199        return true;
200}
201
202static void nv_particle_affector_deflector_plane( const particle_affector_data* data, particle* p, float factor, uint32 count )
203{
204        const nvpe_deflector_plane_data* datap = reinterpret_cast<const nvpe_deflector_plane_data*>( data->paramters );
205        for ( uint32 i = 0; i < count; ++i )
206        {
207                particle& pt = p[i];
208                vec3 direction  = pt.velocity * nv::min( factor, p->lifetime );
209                if ( math::dot( datap->plane_normal, pt.position + direction ) + datap->distance <= 0.0f )
210                {
211                        float val = math::dot( datap->plane_normal, pt.position ) + datap->distance;
212                        if ( val > 0.0f )
213                        {
214                                vec3 part_dir = direction * ( -val / math::dot( datap->plane_normal, direction ) );
215                                pt.position = pt.position + part_dir + ( part_dir - direction ) * datap->bounce;
216                                pt.velocity = math::reflect( pt.velocity, datap->plane_normal ) * datap->bounce;
217                        }
218                }
219        }
220}
221
222struct nvpe_color_fader_data
223{
224        nv::vec4 adjustment;
225};
226
227NV_RTTI_DECLARE( nvpe_color_fader_data )
228
229static bool nv_particle_affector_color_fader_init( lua::table_guard* table, particle_affector_data* data )
230{
231        nvpe_color_fader_data* datap = reinterpret_cast<nvpe_color_fader_data*>( data->paramters );
232        datap->adjustment = table->get<vec4>("adjustment",  vec4() );
233        return true;
234}
235
236static void nv_particle_affector_color_fader( const particle_affector_data* data, particle* p, float factor, uint32 count )
237{
238        const nvpe_color_fader_data* datap = reinterpret_cast<const nvpe_color_fader_data*>( data->paramters );
239        vec4 adjustment = datap->adjustment * nv::min( factor, p->lifetime );
240        for ( uint32 i = 0; i < count; ++i )
241        {
242                p[i].color = math::clamp( p[i].color + adjustment, 0.0f, 1.0f );
243        }
244}
245
246struct nvpe_scaler_data
247{
248        nv::vec2 adjustment;
249};
250
251NV_RTTI_DECLARE( nvpe_scaler_data )
252
253static bool nv_particle_affector_scaler_init( lua::table_guard* table, particle_affector_data* data )
254{
255        nvpe_scaler_data* datap = reinterpret_cast<nvpe_scaler_data*>( data->paramters );
256        float rate        = table->get<float>("rate", 0.0f );
257        datap->adjustment = table->get<vec2>("adjustment",  vec2(rate,rate) );
258        return true;
259}
260
261static void nv_particle_affector_scaler( const particle_affector_data* data, particle* p, float factor, uint32 count )
262{
263        const nvpe_scaler_data* datap = reinterpret_cast<const nvpe_scaler_data*>( data->paramters );
264        vec2 adjustment = datap->adjustment * nv::min( factor, p->lifetime );
265        for ( uint32 i = 0; i < count; ++i )
266        {
267                p[i].size = math::max( p[i].size + adjustment, vec2() );
268        }
269}
270
271nv::particle_engine::particle_engine( context* a_context, random_base* rng, particle_group_manager* groups, bool debug_data )
272{
273        m_context = a_context;
274        m_pgm     = groups;
275        m_rng     = rng;
276        if ( m_rng == nullptr )
277                m_rng = &random::get();
278
279        if ( debug_data )
280        {
281                m_debug_emitter_names  = new nv::hash_map< nv::particle_emitter_func,  nv::const_string >;
282                m_debug_affector_names = new nv::hash_map< nv::particle_affector_func, nv::const_string >;
283                m_debug_emitter_list   = new nv::vector< nv::particle_emitter_func >;
284                m_debug_affector_list  = new nv::vector< nv::particle_affector_func >;
285        }
286
287        register_standard_emitters();
288        register_standard_affectors();
289}
290
291nv::particle_system nv::particle_engine::create_system( const particle_system_data* data, particle_group group )
292{
293        if ( !m_pgm->ref( group ) )
294                return nv::particle_system();
295       
296        particle_system result = m_systems.create();
297        particle_system_info* info = m_systems.get( result );
298        info->group = group;
299        info->data = data;
300        uint32 ecount = data->emitter_count;
301        for ( uint32 i = 0; i < ecount; ++i )
302        {
303                info->emitters[i].active = true;
304                if ( data->emitters[i].duration_max == 0.0f )
305                        info->emitters[i].pause = 0;
306                else
307                        info->emitters[i].pause = m_rng->frange( data->emitters[i].duration_min, data->emitters[i].duration_max );
308        }
309
310        info->count = 0;
311        info->particles = new particle[data->quota];
312
313        return result;
314}
315
316void nv::particle_engine::set_on_collide( particle_system system, const particle_on_collide_func& f )
317{
318        particle_system_info* info = m_systems.get( system );
319        if ( info )
320                info->on_collide = f;
321}
322
323nv::particle_system nv::particle_engine::create_system( resource< nv::particle_system_data > rdata, particle_group group )
324{
325        if ( auto data = rdata.lock() )
326                return create_system( &*data, group );
327        return {};
328}
329
330void nv::particle_engine::prepare( particle_group group )
331{
332        m_pgm->prepare( group );
333}
334
335nv::particle_group nv::particle_engine::create_group( uint32 max_particles )
336{
337        return m_pgm->create_group( max_particles );
338}
339
340nv::particle_engine::~particle_engine()
341{
342        clear();
343}
344
345void nv::particle_engine::reset()
346{
347        clear();
348        register_standard_emitters( );
349        register_standard_affectors();
350}
351
352void nv::particle_engine::clear()
353{
354        while ( m_systems.size() > 0 )
355                release( m_systems.get_handle( 0 ) );
356        if ( m_pgm )
357                m_pgm->reset();
358        m_emitters.clear();
359        m_affectors.clear();
360}
361
362
363void nv::particle_engine::release( particle_system system )
364{
365        particle_system_info* info = m_systems.get( system );
366        if ( info )
367        {
368                m_pgm->unref( info->group );
369                delete[] info->particles;
370                m_systems.destroy( system );
371        }
372}
373
374bool nv::particle_engine::is_finished( particle_system system )
375{
376        particle_system_info* info = m_systems.get( system );
377        if ( info )
378        {
379                if ( info->count > 0 ) return false;
380                for ( uint32 i = 0; i < info->data->emitter_count; ++i )
381                {
382                        const auto& edata = info->emitters[i];
383                        if ( edata.active || edata.pause > 0.0f )
384                        {
385                                return false;
386                        }
387                }
388        }
389        return true;
390}
391
392void nv::particle_engine::release( particle_group group )
393{
394        m_pgm->release( group );
395}
396
397void nv::particle_engine::render( particle_system system, const scene_state& s )
398{
399        particle_system_info* info = m_systems.get( system );
400        if ( info )
401                m_pgm->generate_data( array_view< particle >( info->particles, info->count ), info->data, info->group, s );
402}
403
404void nv::particle_engine::update( particle_system system, transform model, float dtime  )
405{
406        particle_system_info* info = m_systems.get( system );
407        if ( info )
408        {
409//              while ( dtime > 0.2 )
410//              {
411//                      update_emitters( info, 0.2 );
412//                      destroy_particles( info, 0.2 );
413//                      create_particles( info, 0.2 );
414//                      update_particles( info, 0.2 );
415//                      dtime -= 0.2;
416//              }
417
418                update_emitters( info, dtime );
419                destroy_particles( info, dtime );
420                create_particles( info, model, dtime );
421                update_particles( info, dtime );
422
423//              generate_data( info );
424        }
425}
426
427void nv::particle_engine::set_texcoords( particle_system system, vec2 a, vec2 b )
428{
429        particle_system_info* info = m_systems.get( system );
430        if ( info )
431        {
432                info->texcoords[0] = a;
433                info->texcoords[1] = b;
434        }
435}
436
437void nv::particle_engine::destroy_particles( particle_system_info* info, float dtime )
438{
439        if ( info->count > 0 )
440                for ( sint32 i = sint32( info->count ) - 1; i >= 0; --i )
441                {
442                        particle& pinfo = info->particles[i];
443                        pinfo.lifetime += dtime;
444                        if ( pinfo.lifetime >= pinfo.death )
445                        {
446                                info->count--;
447                                swap( info->particles[i], info->particles[info->count] );
448                        }
449                }
450}
451
452void nv::particle_engine::create_particles( particle_system_info* info, transform model, float dtime )
453{
454        uint32 ecount = info->data->emitter_count;
455        if ( ecount == 0 ) return;
456
457        for ( uint32 i = 0; i < ecount; ++i )
458        {
459                const auto& edata = info->data->emitters[i];
460                auto& einfo = info->emitters[i];
461                if ( einfo.active )
462                {
463                        einfo.next -= dtime;
464                        float fperiod = 1.0f / edata.rate;
465                        while ( einfo.next < 0.0f )
466                        {
467                                if ( info->count < info->data->quota-1 )
468                                {
469                                        particle& pinfo = info->particles[info->count];
470                                        edata.emitter_func( m_rng, &(info->data->emitters[i]), &pinfo, 1 );
471                                        pinfo.position = vec3();
472                                        pinfo.position+= edata.position;
473//                                      if ( !local )
474                                                pinfo.position = pinfo.position * model;
475                                        pinfo.color    = edata.color_min == edata.color_max ?
476                                                edata.color_min : m_rng->range( edata.color_min, edata.color_max );
477                                        pinfo.size     = edata.size_min == edata.size_max ?
478                                                edata.size_min : m_rng->range( edata.size_min, edata.size_max );
479                                        if ( edata.square ) pinfo.size.y = pinfo.size.x;
480                                        float velocity = edata.velocity_min == edata.velocity_max ?
481                                                edata.velocity_min : m_rng->frange( edata.velocity_min, edata.velocity_max );
482                                        pinfo.lifetime = dtime + einfo.next;
483                                        pinfo.death = ( edata.lifetime_min == edata.lifetime_max ?
484                                                edata.lifetime_min : m_rng->frange( edata.lifetime_min, edata.lifetime_max ) );
485                                        pinfo.rotation = m_rng->frand( 2* math::pi<float>() );
486                                        pinfo.tcoord_a = info->texcoords[0];
487                                        pinfo.tcoord_b = info->texcoords[1];
488
489                                        pinfo.velocity = edata.dir;
490                                        if ( edata.angle > 0.0f )
491                                        {
492                                                float emission_angle = math::radians( edata.angle );
493                                                float cos_theta = m_rng->frange( cos( emission_angle ), 1.0f );
494                                                float sin_theta = sqrt(1.0f - cos_theta * cos_theta );
495                                                float phi       = m_rng->frange( 0.0f, 2* math::pi<float>() );
496                                                pinfo.velocity  = model.get_orientation() *
497                                                        ( edata.odir * ( cos(phi) * sin_theta ) +
498                                                        edata.cdir * ( sin(phi)*sin_theta ) +
499                                                        edata.dir  * cos_theta );
500                                        }
501
502                                        pinfo.velocity *= velocity;
503
504                                        info->count++;
505                                }
506                                einfo.next += fperiod;
507                        }
508
509                }
510        }
511}
512
513void nv::particle_engine::update_particles( particle_system_info* info, float dtime )
514{
515        if ( dtime <= 0.0f ) return;
516
517        uint32 acount = info->data->affector_count;
518        for ( uint32 i = 0; i < acount; ++i )
519        {
520                const particle_affector_data* padata = &(info->data->affectors[i]);
521                padata->process( padata, info->particles, dtime, info->count );
522        }
523
524
525        for ( uint32 i = 0; i < info->count; ++i )
526        {
527                particle& pdata = info->particles[i];
528                float factor = min( dtime, pdata.lifetime );
529                pdata.position += pdata.velocity * factor;
530        }
531
532        if ( info->on_collide )
533        {
534                for ( uint32 i = 0; i < info->count; ++i )
535                {
536                        particle& pdata = info->particles[i];
537                        if ( pdata.position.y <= 0.0f )
538                        {
539                                info->on_collide( pdata.position, pdata.velocity );
540                                pdata.death = pdata.lifetime;
541                        }
542                }
543        }
544
545}
546
547void nv::particle_engine::update_emitters( particle_system_info* info, float dtime )
548{
549        uint32 ecount = info->data->emitter_count;
550        if ( ecount == 0 ) return;
551
552        for ( uint32 i = 0; i < ecount; ++i )
553        {
554                const auto& edata = info->data->emitters[i];
555                auto& einfo = info->emitters[i];
556
557                if ( einfo.pause > 0.0f )
558                {
559                        einfo.pause -= dtime;
560                        if ( einfo.pause == 0.0f ) einfo.pause = -0.001f;
561                }
562
563                if ( einfo.pause < 0.0f )
564                {
565                        if ( einfo.active )
566                        {
567                                einfo.active = false;
568                                if ( edata.repeat_min > 0.0f )
569                                        einfo.pause += m_rng->frange( edata.repeat_min, edata.repeat_max );
570                                else
571                                        einfo.pause = 0.0f;
572                        }
573                        else
574                        {
575                                einfo.active = true;
576                                einfo.pause += m_rng->frange( edata.duration_min, edata.duration_max );
577                        }
578                }
579        }
580
581}
582
583void nv::particle_engine::register_emitter_type( const string_view& name, particle_emitter_func func )
584{
585        if ( m_debug_emitter_names )
586                ( *m_debug_emitter_names )[func] = name;
587        if ( m_debug_emitter_list )
588                ( *m_debug_emitter_list ).push_back( func );
589
590        m_emitters[ name ] = func;
591}
592
593void nv::particle_engine::register_standard_emitters()
594{
595        register_emitter_type( "point",             nv_particle_emitter_point );
596        register_emitter_type( "box",               nv_particle_emitter_box );
597        register_emitter_type( "cylinder",          nv_particle_emitter_cylinder );
598        register_emitter_type( "sphere",            nv_particle_emitter_sphere );
599        register_emitter_type( "cylindroid",        nv_particle_emitter_cylindroid );
600        register_emitter_type( "ellipsoid",         nv_particle_emitter_ellipsoid );
601        register_emitter_type( "hollow_cylinder",   nv_particle_emitter_hollow_cylinder );
602        register_emitter_type( "hollow_sphere",     nv_particle_emitter_hollow_sphere );
603        register_emitter_type( "hollow_cylindroid", nv_particle_emitter_hollow_cylindroid );
604        register_emitter_type( "hollow_ellipsoid",  nv_particle_emitter_hollow_ellipsoid );
605}
606
607void nv::particle_engine::register_affector_type( const string_view& name, particle_affector_init_func init, particle_affector_func process )
608{
609        if ( m_debug_affector_names )
610                ( *m_debug_affector_names )[process] = name;
611        if ( m_debug_affector_list )
612                ( *m_debug_affector_list ).push_back( process );
613
614        m_affectors[ name ].init    = init;
615        m_affectors[ name ].process = process;
616}
617
618nv::particle_emitter_func nv::particle_engine::get_emitter( shash64 emitter )
619{
620        auto emitter_iter = m_emitters.find( emitter );
621        if ( emitter_iter != m_emitters.end() )
622        {
623                return emitter_iter->second;
624        }
625        return nullptr;
626}
627
628nv::particle_affector_funcs nv::particle_engine::get_affector( shash64 affector )
629{
630        auto affector_iter = m_affectors.find( affector );
631        if ( affector_iter != m_affectors.end() )
632        {
633                return affector_iter->second;
634        }
635        return { nullptr, nullptr };
636}
637
638nv::string_view nv::particle_engine::get_debug_name( particle_emitter_func f )
639{
640        auto i = m_debug_emitter_names->find( f );
641        if ( i != m_debug_emitter_names->end() )
642                return i->second;
643        return {};
644}
645
646nv::string_view nv::particle_engine::get_debug_name( particle_affector_func f )
647{
648        auto i = m_debug_affector_names->find( f );
649        if ( i != m_debug_affector_names->end() )
650                return i->second;
651        return {};
652}
653
654nv::particle_render_data nv::particle_engine::get_render_data( particle_group group )
655{
656        return m_pgm->get_render_data( group );
657}
658
659void nv::particle_engine::register_standard_affectors()
660{
661        register_affector_type( "linear_force",    nv_particle_affector_linear_force_init, nv_particle_affector_linear_force );
662        register_affector_type( "deflector_plane", nv_particle_affector_deflector_plane_init, nv_particle_affector_deflector_plane );
663        register_affector_type( "color_fader",     nv_particle_affector_color_fader_init, nv_particle_affector_color_fader );
664        register_affector_type( "scaler",          nv_particle_affector_scaler_init, nv_particle_affector_scaler );
665}
666
667const nv::type_entry* nv::particle_engine::get_debug_type( particle_affector_func f )
668{
669        if ( m_debug_affector_types )
670        {
671                auto it = m_debug_affector_types->find( f );
672                if ( it != m_debug_affector_types->end() )
673                        return it->second;
674        }
675        return nullptr;
676}
677
678void nv::particle_engine::register_types( type_database* db, bool debug_data )
679{
680        db->create_type<particle_orientation>()
681                .value( "PS_POINT",                sint32( particle_orientation::POINT ),                "Point" )
682                .value( "PS_ORIENTED",             sint32( particle_orientation::ORIENTED ),             "Oriented" )
683                .value( "PS_ORIENTED_COMMON",      sint32( particle_orientation::ORIENTED_COMMON ),      "Oriented Common" )
684                .value( "PS_PERPENDICULAR",        sint32( particle_orientation::PERPENDICULAR ),        "Perpendicular" )
685                .value( "PS_PERPENDICULAR_COMMON", sint32( particle_orientation::PERPENDICULAR_COMMON ), "Perpendicular Common" )
686                ;
687
688        db->create_type<particle_origin>()
689                .value( "PS_CENTER",        sint32( particle_origin::CENTER ),        "Center" )
690                .value( "PS_TOP_LEFT",      sint32( particle_origin::TOP_LEFT ),      "Top left" )
691                .value( "PS_TOP_CENTER",    sint32( particle_origin::TOP_CENTER ),    "Top center" )
692                .value( "PS_TOP_RIGHT",     sint32( particle_origin::TOP_RIGHT ),     "Top right" )
693                .value( "PS_CENTER_LEFT",   sint32( particle_origin::CENTER_LEFT ),   "Center left" )
694                .value( "PS_CENTER_RIGHT",  sint32( particle_origin::CENTER_RIGHT ),  "Center right" )
695                .value( "PS_BOTTOM_LEFT",   sint32( particle_origin::BOTTOM_LEFT ),   "Bottom left" )
696                .value( "PS_BOTTOM_CENTER", sint32( particle_origin::BOTTOM_CENTER ), "Bottom center" )
697                .value( "PS_BOTTOM_RIGHT",  sint32( particle_origin::BOTTOM_RIGHT ),  "Bottom right" )
698                ;
699
700        if ( debug_data )
701        {
702                if ( !m_debug_affector_types )
703                        m_debug_affector_types = new nv::hash_map< nv::particle_affector_func, nv::type_entry* >;
704
705                int you_could_get_rid_of_the_init_function_using_these; int error;
706                int universal_vm_based_affector;
707                // compile_affector( ... ) in lua, returns array of bytecode
708                // function is a normal bytecode runner!
709
710
711                (*m_debug_affector_types)[ nv_particle_affector_linear_force ]
712                        = db->create_type<nvpe_linear_force_data>( "linear force" )
713                        .field( "force_vector", &nvpe_linear_force_data::force_vector )
714                        .field( "average",      &nvpe_linear_force_data::average )
715                        .get();
716
717                ( *m_debug_affector_types )[nv_particle_affector_deflector_plane]
718                        = db->create_type<nvpe_deflector_plane_data>( "deflector plane" )
719                        .field( "plane_point",  &nvpe_deflector_plane_data::plane_point )
720                        .field( "plane_normal", &nvpe_deflector_plane_data::plane_normal )
721                        .field( "bounce",       &nvpe_deflector_plane_data::bounce )
722                        .field( "distance",     &nvpe_deflector_plane_data::distance )
723                        .get();
724
725                ( *m_debug_affector_types )[nv_particle_affector_color_fader]
726                        = db->create_type<nvpe_linear_force_data>( "color fader" )
727                        .field( "adjustment", & nvpe_color_fader_data::adjustment )
728                        .get();
729
730                ( *m_debug_affector_types )[nv_particle_affector_scaler]
731                        = db->create_type<nvpe_scaler_data>( "scaler" )
732                        .field( "adjustment", & nvpe_scaler_data::adjustment )
733                        .get();
734        }
735}
736
Note: See TracBrowser for help on using the repository browser.