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

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