source: trunk/src/engine/particle_manager.cc @ 519

Last change on this file since 519 was 518, checked in by epyon, 9 years ago
  • refactoring of the particle_engine
  • particle_manager added
  • cleanups
File size: 8.0 KB
Line 
1#include "nv/engine/particle_manager.hh"
2
3#include "nv/lua/lua_raw.hh"
4#include "nv/lua/lua_math.hh"
5
6static int nv_psystem_emitter_closure( lua_State * L )
7{
8        luaL_checktype( L, 1, LUA_TTABLE );
9        lua_settop( L, 1 );
10        lua_pushvalue( L, lua_upvalueindex( 1 ) ); // type
11        lua_setfield( L, 1, "sub_type" );
12        lua_pushliteral( L, "emitter" );
13        lua_setfield( L, 1, "type" );
14        return 1;
15}
16
17static int nv_psystem_affector_closure( lua_State * L )
18{
19        luaL_checktype( L, 1, LUA_TTABLE );
20        lua_settop( L, 1 );
21        lua_pushvalue( L, lua_upvalueindex( 1 ) ); // type
22        lua_setfield( L, 1, "sub_type" );
23        lua_pushliteral( L, "affector" );
24        lua_setfield( L, 1, "type" );
25        return 1;
26}
27
28static int nv_psystem_emitter( lua_State * L )
29{
30        luaL_checkstring( L, 1 );
31        lua_settop( L, 1 );
32        lua_pushcclosure( L, nv_psystem_emitter_closure, 1 );
33        return 1;
34}
35
36static int nv_psystem_affector( lua_State * L )
37{
38        luaL_checkstring( L, 1 );
39        lua_settop( L, 1 );
40        lua_pushcclosure( L, nv_psystem_affector_closure, 1 );
41        return 1;
42}
43
44
45void nv::particle_manager::initialize( lua::state* state )
46{
47        lua_resource_manager_base::initialize( state );
48        lua_State* L = state->get_raw();
49        lua_pushcfunction( L, nv_psystem_emitter );
50        lua_setglobal( L, "emitter" );
51        lua_pushcfunction( L, nv_psystem_affector );
52        lua_setglobal( L, "affector" );
53}
54
55bool nv::particle_manager::load_resource( lua::table_guard& table, shash64 id )
56{
57        particle_system_data* data = new particle_system_data;
58        data->quota   = table.get<uint32>("quota", 1024 );
59//      data->local   = table.get<bool>("local_space", false );
60        data->accurate_facing = table.get<bool>("accurate_facing", false );
61        data->emitter_count   = 0;
62        data->affector_count  = 0;
63
64        const_string orientation = table.get_string( "orientation", "point" );
65        if ( orientation == "point" )                     { data->orientation = particle_orientation::POINT; }
66        else if ( orientation == "oriented" )             { data->orientation = particle_orientation::ORIENTED; }
67        else if ( orientation == "oriented_common" )      { data->orientation = particle_orientation::ORIENTED_COMMON; }
68        else if ( orientation == "perpendicular" )        { data->orientation = particle_orientation::PERPENDICULAR; }
69        else if ( orientation == "perpendicular_common" ) { data->orientation = particle_orientation::PERPENDICULAR_COMMON; }
70        else
71        {
72                NV_LOG_ERROR( "Unknown orientation type! (", orientation, ")!" );
73                data->orientation = particle_orientation::POINT;
74        }
75
76        const_string origin = table.get_string( "origin", "center" );
77        if      ( origin == "center" )        { data->origin = particle_origin::CENTER; }
78        else if ( origin == "top_left" )      { data->origin = particle_origin::TOP_LEFT; }
79        else if ( origin == "top_center" )    { data->origin = particle_origin::TOP_CENTER; }
80        else if ( origin == "top_right" )     { data->origin = particle_origin::TOP_RIGHT; }
81        else if ( origin == "center_left" )   { data->origin = particle_origin::CENTER_LEFT; }
82        else if ( origin == "center_right" )  { data->origin = particle_origin::CENTER_RIGHT; }
83        else if ( origin == "bottom_left" )   { data->origin = particle_origin::BOTTOM_LEFT; }
84        else if ( origin == "bottom_center" ) { data->origin = particle_origin::BOTTOM_CENTER; }
85        else if ( origin == "bottom_right" )  { data->origin = particle_origin::BOTTOM_RIGHT; }
86        else
87        {
88                NV_LOG_ERROR( "Unknown particle origin! (", origin, ")!" );
89                data->origin = particle_origin::CENTER;
90        }
91
92        data->common_up  = math::normalize( table.get<vec3>("common_up",  vec3(1,0,0) ) );
93        data->common_dir = math::normalize( table.get<vec3>("common_dir", vec3(0,1,0) ) );
94
95        vec2 def_size        = table.get<vec2>("size", vec2(0.1,0.1) );
96        uint32 elements = table.get_size();
97        for ( uint32 i = 0; i < elements; ++i )
98        {
99                lua::table_guard element( table, i+1 );
100                const_string type     = element.get_string( "type" );
101                const_string sub_type = element.get_string( "sub_type" );
102                if ( type == "emitter" )
103                {
104                        if ( data->emitter_count < MAX_PARTICLE_EMITTERS )
105                        {
106                                particle_emitter_data& edata = data->emitters[ data->emitter_count ];
107                                edata.emitter_func = particle_engine::get_emitter( sub_type );
108                                if ( edata.emitter_func == nullptr )
109                                {
110                                        NV_LOG_WARNING( "Unknown emitter type in particle system! (", sub_type, ")" );
111                                        break;
112                                }
113
114                                edata.position     = element.get<vec3>("position", vec3() );
115                                edata.extents      = element.get<vec3>("extents", vec3(1,1,1) );
116                                edata.extents[0]   = element.get<float>("width",  edata.extents[0] );
117                                edata.extents[1]   = element.get<float>("depth",  edata.extents[1] );
118                                edata.extents[2]   = element.get<float>("height", edata.extents[2] );
119                                edata.extents[0]   = element.get<float>("radius",  edata.extents[0] );
120                                edata.iextents     = element.get<vec3>("inner_extents", vec3() );
121                                edata.iextents[0]  = element.get<float>("inner_width",  edata.iextents[0] );
122                                edata.iextents[1]  = element.get<float>("inner_depth",  edata.iextents[1] );
123                                edata.iextents[2]  = element.get<float>("inner_height", edata.iextents[2] );
124                                edata.iextents[0]  = element.get<float>("inner_radius",  edata.iextents[0] );
125                                edata.hextents     = 0.5f * edata.extents;
126                                edata.ihextents    = 0.5f * edata.iextents;
127                                edata.precise      = element.get<bool>("precise", false );
128                                edata.square       = element.get<bool>("square", true );
129                                vec4 color         = element.get<vec4>("color", vec4(1,1,1,1) );
130                                edata.color_min    = element.get<vec4>("color_min", color );
131                                edata.color_max    = element.get<vec4>("color_max", color );
132                                vec2 size          = element.get<vec2>("size", def_size );
133                                edata.size_min     = element.get<vec2>("size_min", size );
134                                edata.size_max     = element.get<vec2>("size_max", size );
135                                edata.angle        = element.get<float>("angle", 0.0f );
136                                float velocity     = element.get<float>("velocity", 0.0f );
137                                edata.velocity_min = element.get<float>("velocity_min", velocity );
138                                edata.velocity_max = element.get<float>("velocity_max", velocity );
139                                float lifetime     = element.get<float>("lifetime", 1.0f );
140                                edata.lifetime_min = element.get<float>("lifetime_min", lifetime );
141                                edata.lifetime_max = element.get<float>("lifetime_max", lifetime );
142                                float duration     = element.get<float>("duration", 0.0f );
143                                edata.duration_min = element.get<float>("duration_min", duration );
144                                edata.duration_max = element.get<float>("duration_max", duration );
145                                float repeat       = element.get<float>("repeat_delay", 0.0f );
146                                edata.repeat_min   = element.get<float>("repeat_delay_min", repeat );
147                                edata.repeat_max   = element.get<float>("repeat_delay_max", repeat );
148
149                                edata.rate         = element.get<float>("rate", 1.0f );
150                                edata.dir          = math::normalize( element.get<vec3>("direction", vec3(0,1,0) ) );
151                               
152                                edata.odir = vec3( 0, 0, 1 );
153                                if ( edata.dir != vec3( 0, 1, 0 ) && edata.dir != vec3( 0, -1, 0 ) )
154                                        edata.odir = math::normalize( math::cross( edata.dir, vec3( 0, 1, 0 ) ) );
155                                edata.cdir = math::cross( edata.dir, edata.odir );
156
157                                data->emitter_count++;
158                        }
159                        else
160                        {
161                                NV_LOG_ERROR( "Too many emitters (", MAX_PARTICLE_EMITTERS, " is MAX)!" );
162                        }
163                }
164                else if ( type == "affector" )
165                {
166                        if ( data->affector_count < MAX_PARTICLE_AFFECTORS )
167                        {
168                                particle_affector_data& adata = data->affectors[ data->affector_count ];
169                                auto afuncs = particle_engine::get_affector( sub_type );
170                                if ( !afuncs.init || !afuncs.process )
171                                {
172                                        NV_LOG_WARNING( "Unknown affector type in particle system! (", sub_type, ")" );
173                                        continue;
174                                }
175                                adata.process = afuncs.process;
176                                if ( !afuncs.init( &element, &adata ) )
177                                {
178                                        NV_LOG_WARNING( "Bad data passed to ", sub_type, " affector in particle system!" );
179                                }
180                                data->affector_count++;
181                        }
182                        else
183                        {
184                                NV_LOG_ERROR( "Too many affectors (", MAX_PARTICLE_AFFECTORS, " is MAX)!" );
185                        }
186                }
187                else
188                {
189                        NV_LOG_WARNING( "Unknown element in particle system! (", type, ")" );
190                }
191        }
192        add( id, data );
193        return true;
194}
195
Note: See TracBrowser for help on using the repository browser.