source: trunk/src/engine/animation.cc @ 508

Last change on this file since 508 was 508, checked in by epyon, 9 years ago
  • nv::engine upgrades
  • default_resource_manager implementation
File size: 6.6 KB
Line 
1// Copyright (C) 2016-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/animation.hh"
8
9#include "nv/stl/range.hh"
10#include "nv/lua/lua_nova.hh"
11#include "nv/io/c_file_system.hh"
12#include "nv/formats/nmd_loader.hh"
13
14using namespace nv;
15
16nv::resource< animator_data > nv::animator_manager::load_animator( const nv::string_view& id, nv::pose_data_set* poses )
17{
18        lua::table_guard table( m_lua, lua::path( "animators", id ) );
19        return load_animator( table, id, poses );
20}
21
22bool nv::animator_manager::load_resource( nv::lua::table_guard& table, nv::shash64 id )
23{
24        return load_animator( table, id, nullptr ).is_valid();
25}
26
27nv::resource< animator_data > nv::animator_manager::load_animator( nv::lua::table_guard& table, nv::shash64 id, nv::pose_data_set* poses /*= nullptr */ )
28{
29        uint32 count = table.get_size();
30        if ( count == 0 )
31        {
32                return nv::resource< animator_data >();
33        }
34
35        nv::animator_data* animator = new nv::animator_data;
36        animator->poses = poses;
37        if ( poses == nullptr )
38        {
39                string128 poses_path = table.get_string128( "poses" );
40                if ( !poses_path.empty() )
41                {
42                        nv::c_file_system fs;
43                        nv::stream* poses_file = open_stream( fs, poses_path );
44                        NV_ASSERT_ALWAYS( poses_file, "CANT FIND POSES FILE!" );
45                        nv::nmd_loader* ploader = new nv::nmd_loader( nullptr );
46                        ploader->load( *poses_file );
47                        delete poses_file;
48
49                        animator->poses = ploader->release_pose_data_set();
50                        NV_ASSERT( animator->poses, "NO POSES LOADED?" );
51                }
52        }
53
54        animator->layers.resize( count );
55        for ( auto layer_idx : nv::range( 1u, count ) )
56        {
57                nv::hash_store< shash64, uint32 > state_names;
58                nv::lua::table_guard layer_table( table, layer_idx );
59                nv::animator_layer_data& layer = animator->layers[layer_idx - 1];
60                layer.name = layer_table.get_string( "name", m_strings, 0 );
61                layer.mask = layer_table.get_integer( "mask", -1 );
62                layer.def_state = -1;
63
64                if ( layer.mask >= 0 )
65                {
66                        const auto& tree = animator->poses->get_tree();
67                        layer.mask_vector.resize( tree.size(), false );
68                        fill_mask_vector_rec( layer.mask_vector, tree, uint32( layer.mask ) );
69                }
70
71                if ( layer_table.is_table( "states" ) )
72                {
73                        nv::lua::table_guard states_table( layer_table, "states" );
74                        uint32 state_count = states_table.get_size();
75                        if ( state_count > 0 )
76                        {
77                                layer.states.resize( state_count );
78
79                                // Two passes for transition name resolution
80                                for ( auto state_idx : nv::range( 1u, state_count ) )
81                                {
82                                        nv::lua::table_guard state_table( states_table, state_idx );
83                                        nv::animator_state_data& state = layer.states[state_idx - 1];
84
85                                        state.name = state_table.get_string( "name", m_strings, 0 );
86                                        NV_ASSERT( state.name, "Animation state without name is invalid!" );
87                                        state_names[state.name] = state_idx - 1;
88                                        state.loop = state_table.get_boolean( "loop", true );
89                                        state.duration = state_table.get_float( "duration", 0.0f );
90                                        state.interp = nv::interpolation( state_table.get_unsigned( "interpolation", uint32( nv::interpolation::SPHERICAL ) ) );
91
92                                        shash64 pose_set = state_table.get_string_hash_64( "pose_set" );
93                                        if ( pose_set )
94                                        {
95                                                auto it = animator->poses->m_sets.find( pose_set );
96                                                NV_ASSERT( it->second.name, "POSE NOT FOUND" );
97                                                uint32 pid = it->second.start;
98                                                uint32 pose_count = it->second.count;
99                                                uint32 start = state_table.get_unsigned( "start", 0 );
100                                                uint32 stop = state_table.get_unsigned( "stop", pose_count - 1 );
101                                                for ( nv::uint32 i = pid + start; i < pid + stop + 1; ++i )
102                                                {
103                                                        state.poses.push_back( nv::animator_pose_data{ i/*, 0.0f*/ } );
104                                                }
105
106                                                NV_ASSERT( state.poses.size() > 0, "POSES EMPTY!" );
107                                                state.base_pose = state.poses[0].pose;
108                                        }
109                                }
110
111                                // Second pass for transitions only
112                                for ( auto state_idx : nv::range( 1u, state_count ) )
113                                {
114                                        nv::animator_state_data& state = layer.states[state_idx - 1];
115                                        nv::lua::table_guard state_table( states_table, state_idx );
116
117                                        if ( state_table.is_table( "transitions" ) )
118                                        {
119                                                nv::lua::table_guard transitions_table( state_table, "transitions" );
120                                                uint32 transition_count = transitions_table.get_size();
121                                                if ( transition_count > 0 )
122                                                {
123                                                        for ( auto transition_idx : nv::range( 1u, transition_count ) )
124                                                        {
125                                                                nv::lua::table_guard transition_table( transitions_table, transition_idx );
126                                                                shash64 name = transition_table.get_string( "name", m_strings, 0 );
127                                                                shash64 target_name = transition_table.get_string_hash_64( "target", 0 );
128                                                                NV_ASSERT( name, "Transition state without name is invalid!" );
129                                                                NV_ASSERT( target_name, "Transition state without name is invalid!" );
130                                                                auto it = state_names.find( target_name );
131                                                                NV_ASSERT( it != state_names.end(), "Transition target NOT FOUND!" );
132
133                                                                nv::animator_transition_data tr_data;
134                                                                tr_data.target = it->second;
135                                                                tr_data.duration = transition_table.get_float( "duration", 0.0f );
136                                                                tr_data.interp = nv::interpolation( transition_table.get_unsigned( "interpolation", uint32( nv::interpolation::SPHERICAL ) ) );
137                                                                tr_data.easing = read_easing( transition_table );
138
139                                                                state.transitions[name] = tr_data;
140                                                        }
141                                                }
142                                        }
143                                }
144                        }
145                }
146
147
148
149
150                shash64 def_state_name = layer_table.get_string_hash_64( "default", 0 );
151                if ( def_state_name )
152                {
153                        auto dsi = state_names.find( def_state_name );
154                        NV_ASSERT( dsi != state_names.end(), "Unknown default!" );
155                        layer.def_state = sint32( dsi->second );
156                }
157        }
158
159        return add( id, animator );
160}
161
162nv::easing animator_manager::read_easing( nv::lua::table_guard& table )
163{
164        nv::easing result;
165        result.in = nv::easing_type( table.get_integer( "easing", int( nv::easing_type::NONE ) ) );
166        result.in = nv::easing_type( table.get_integer( "ease_in", int( result.in ) ) );
167        result.out = nv::easing_type( table.get_integer( "ease_out", int( nv::easing_type::NONE ) ) );
168        if ( table.has_field( "ease_in_out" ) )
169        {
170                result.in = result.out = nv::easing_type( table.get_integer( "ease_in_out", int( nv::easing_type::NONE ) ) );
171        }
172        if ( result.in == nv::easing_type::NONE && result.out == nv::easing_type::NONE )
173        {
174                result.in = nv::easing_type::LINEAR;
175                result.out = nv::easing_type::NONE;
176        }
177        else
178        {
179                return result;
180        }
181        return result;
182}
183
184nv::resource< nv::animator_data > animator_manager::create_animator( const nv::string_view& id, nv::pose_data_set* poses )
185{
186        animator_data* data = new animator_data;
187        data->poses = poses;
188        return add( id, data );
189}
Note: See TracBrowser for help on using the repository browser.