Changeset 287
- Timestamp:
- 07/23/14 15:24:03 (11 years ago)
- Location:
- trunk
- Files:
-
- 23 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/nv/formats/assimp_loader.hh
r285 r287 56 56 struct assimp_animation 57 57 { 58 float fps; 59 float duration; 60 bool pretransformed; 58 uint16 frame_rate; 59 float duration; 60 bool flat; 61 uint32 max_frames; 61 62 dynamic_array< assimp_animated_node_data > nodes; 62 63 }; … … 71 72 virtual size_t get_mesh_count() const { return m_mesh_count; } 72 73 virtual ~assimp_loader(); 74 // TODO: remove 73 75 assimp_model* release_merged_model(); 76 // TODO: implement release_mesh_nodes_data 77 virtual mesh_data_pack* release_mesh_data_pack(); 74 78 assimp_animation* release_animation( size_t index, bool pre_transform ); 79 // TODO: implement 80 virtual mesh_nodes_data* release_mesh_nodes_data(); 75 81 bool load_bones( size_t index, std::vector< assimp_bone >& bones ); 76 82 void scene_report() const; 77 83 private: 84 void load_mesh_data( mesh_data* data, size_t index ); 78 85 void initialize( const string& a_ext, const mat3& a_rotate_transform, float a_scale, uint32 a_assimp_flags ); 79 86 uint32 load_node( assimp_animation* data, const void* vnode, sint32 this_id, sint32 parent_id ); -
trunk/nv/formats/md2_loader.hh
r285 r287 29 29 virtual bool load( stream& source ); 30 30 virtual mesh_data* release_mesh_data( size_t index = 0 ); 31 virtual mesh_data_pack* release_mesh_data_pack(); 31 32 virtual size_t get_mesh_count() const { return 1; } 32 33 private: 33 v irtual mesh_data* release_mesh_frame(sint32 frame );34 void release_mesh_frame( mesh_data* data, sint32 frame ); 34 35 size_t get_max_frames() const; 35 36 void reindex(); -
trunk/nv/formats/md3_loader.hh
r285 r287 36 36 virtual bool load( stream& source ); 37 37 virtual mesh_data* release_mesh_data( size_t index = 0 ); 38 virtual mesh_data * release_mesh_frame( sint32 frame);38 virtual mesh_data_pack* release_mesh_data_pack(); 39 39 virtual size_t get_mesh_count() const { return 1; } 40 40 size_t get_max_frames() const; 41 virtual tag_map* create_tag_map(); 42 virtual mesh_node_data* release_mesh_node_data( size_t index ); 43 virtual size_t get_node_count() const; 41 virtual mesh_nodes_data* release_mesh_nodes_data(); 44 42 private: 43 void release_mesh_frame( mesh_data* data, sint32 frame ); 45 44 key_raw_channel* load_tags( const std::string& tag ); 46 45 void* m_md3; -
trunk/nv/formats/md5_loader.hh
r282 r287 33 33 }; 34 34 35 struct md5_vtx_ data35 struct md5_vtx_pntiw 36 36 { 37 37 vec3 position; … … 88 88 }; 89 89 90 class md5_mesh_instance91 {92 friend class md5_mesh_data;93 public:94 const void* data() const { return m_pntdata.data(); }95 uint32 get_index_count() const { return m_indices; }96 size_t size() const { return m_pntdata.raw_size(); }97 void apply( const transform* skeleton );98 const md5_mesh_data* get_mesh_data() const { return m_data; }99 private:100 md5_mesh_instance( const md5_mesh_data* a_data );101 102 uint32 m_indices;103 dynamic_array< md5_vtx_pnt > m_pntdata;104 dynamic_array< transform > m_pos_offset;105 const md5_mesh_data* m_data;106 };107 108 class md5_mesh_data : public mesh_data109 {110 public:111 friend class md5_loader;112 friend class md5_mesh_instance;113 md5_mesh_instance* spawn() const;114 private:115 uint32* m_idata;116 md5_vtx_t* m_tdata;117 md5_vtx_pnt* m_pntdata;118 std::string m_shader;119 dynamic_array< transform > m_bone_offset;120 dynamic_array< md5_vtx_data > m_vtx_data;121 };122 123 124 90 class md5_loader : public mesh_loader 125 91 { … … 129 95 virtual bool load( stream& source ); 130 96 virtual mesh_data* release_mesh_data( size_t index = 0 ); 97 virtual mesh_nodes_data* release_mesh_nodes_data(); 98 // TODO: broken due to larger mesh_data structure! 99 virtual mesh_data_pack* release_mesh_data_pack(); 131 100 virtual size_t get_mesh_count() const { return m_meshes.size(); } 132 101 protected: … … 151 120 }; 152 121 protected: 153 bool prepare_mesh( md5_mesh_data* mdata, md5_weight* weights, md5_weight_info* weight_info );122 bool prepare_mesh( uint32 vtx_count, mesh_data* mdata, md5_weight* weights, md5_weight_info* weight_info ); 154 123 protected: 155 124 uint32 m_md5_version; … … 157 126 uint32 m_num_meshes; 158 127 159 dynamic_array<md5_joint> 160 dynamic_array<m d5_mesh_data*> m_meshes;128 dynamic_array<md5_joint> m_joints; 129 dynamic_array<mesh_data*> m_meshes; 161 130 }; 162 131 -
trunk/nv/formats/nmd_loader.hh
r285 r287 20 20 MESH, 21 21 STREAM, 22 BONE_ARRAY,22 NODE, 23 23 STRING_TABLE, 24 24 ANIMATION, 25 ANIMATION_NODE, 26 ANIMATION_CHANNEL, 25 KEY_CHANNEL, 27 26 }; 28 27 … … 44 43 struct nmd_animation_header 45 44 { 46 float fps;47 float duration;48 bool flat;45 uint16 frame_rate; 46 float duration; 47 bool flat; 49 48 }; 50 49 51 struct nmd_ animation_node_header50 struct nmd_node_header 52 51 { 53 52 sint16 parent_id; … … 55 54 }; 56 55 57 struct nmd_ animation_channel_header56 struct nmd_key_channel_header 58 57 { 59 58 key_descriptor format; … … 67 66 }; 68 67 69 struct nmd_bone70 {71 uint16 name;72 mat4 offset;73 };74 75 struct nmd_node76 {77 uint16 name;78 sint16 parent_id;79 mat4 transform;80 key_data* data;81 };82 83 struct nmd_animation84 {85 float fps;86 float duration;87 bool flat;88 uint16 node_count;89 nmd_node* nodes;90 91 nmd_animation() : node_count(0), nodes( nullptr ) {}92 ~nmd_animation()93 {94 for ( uint16 n = 0; n < node_count; ++n )95 {96 if ( nodes[n].data ) delete nodes[n].data;97 }98 delete[] nodes;99 }100 };101 102 struct nmd_bone_data103 {104 nmd_bone* bones;105 uint16 count;106 107 nmd_bone_data() : count(0), bones( nullptr ) {}108 ~nmd_bone_data()109 {110 if ( bones ) delete[] bones;111 }112 };113 114 115 116 68 class nmd_loader : public mesh_loader 117 69 { 118 70 public: 119 nmd_loader() : m_ animation( nullptr ), m_bone_data( nullptr ), m_strings( nullptr ) {}71 nmd_loader() : m_node_data( nullptr ), m_node_array( nullptr ), m_strings( nullptr ) {} 120 72 virtual bool load( stream& source ); 121 73 virtual mesh_data* release_mesh_data( size_t index = 0 ); 122 virtual nmd_animation* release_animation(); 123 virtual nmd_bone_data* release_bone_data(); 124 virtual string_table* release_string_table(); 74 virtual mesh_nodes_data* release_mesh_nodes_data(); 75 virtual mesh_data_pack* release_mesh_data_pack(); 125 76 virtual size_t get_mesh_count() const { return m_meshes.size(); } 126 77 virtual ~nmd_loader(); … … 128 79 void reset(); 129 80 bool load_mesh( stream& source, const nmd_element_header& e ); 130 bool load_bones( stream& source, const nmd_element_header& e );131 81 bool load_strings( stream& source ); 132 82 bool load_animation( stream& source, const nmd_element_header& e ); 133 83 134 nmd_animation* m_animation;135 nmd_bone_data* m_bone_data;84 mesh_nodes_data* m_node_data; 85 mesh_node_data* m_node_array; 136 86 string_table* m_strings; 87 std::vector< uint16 > m_mesh_names; 88 std::vector< uint16 > m_node_names; 137 89 std::vector< mesh_data* > m_meshes; 138 90 }; 139 91 140 92 // TODO: Temporary, find a better way and remove! 141 142 class nmd_temp_model; 143 144 class nmd_temp_animation 93 class nmd_temp_model_data 145 94 { 95 friend class nmd_temp_animation_data; 146 96 public: 147 nmd_temp_animation( nmd_loader* loader ); 148 void prepare( const nmd_temp_model* model ); 149 void animate( mat4* data, uint32 time ); 150 virtual uint32 get_frame_rate() const { return uint32(m_animation->fps); } 151 virtual uint32 get_frame_count() const { return uint32(m_animation->duration) / uint32(m_animation->fps); } 152 ~nmd_temp_animation(); 97 nmd_temp_model_data( nmd_loader* loader ); 98 const mesh_data* get_data( uint32 index ) const { return m_mesh_data[index]; } 99 uint32 get_count() const { return m_mesh_data.size(); } 100 const mesh_nodes_data* get_node_data() const { return m_node_data; } 101 ~nmd_temp_model_data(); 153 102 private: 154 void animate_rec( mat4* data, float time, uint32 node_id, const mat4& parent_mat );155 156 std::vector< key_data* > m_data;157 std::vector< sint16 > m_bone_ids;158 std::vector< std::vector< uint32 > > m_children;159 const mat4* m_offsets;160 string_table* m_strings;161 nmd_animation* m_animation;162 };163 164 // TODO: Temporary, find a better way and remove!165 class nmd_temp_model166 {167 friend class nmd_temp_animation;168 public:169 nmd_temp_model( nmd_loader* loader );170 const mesh_data* get_data( uint32 index ) const { return m_mesh_data[index]; }171 uint32 get_bone_count() const { return m_bone_offsets.size(); }172 uint32 get_count() const { return m_mesh_data.size(); }173 ~nmd_temp_model();174 private:175 std::unordered_map< std::string, nv::uint16 > m_bone_names;176 std::vector< mat4 > m_bone_offsets;177 103 std::vector< mesh_data*> m_mesh_data; 104 mesh_nodes_data* m_node_data; 178 105 }; 179 106 -
trunk/nv/formats/obj_loader.hh
r240 r287 26 26 obj_loader( bool normals = true, bool tangents = false ); 27 27 virtual bool load( stream& source ); 28 mesh_data* release_mesh_data( size_t index = 0 ); 28 virtual mesh_data* release_mesh_data( size_t index = 0 ); 29 virtual mesh_data_pack* release_mesh_data_pack(); 29 30 virtual size_t get_mesh_count() const { return m_meshes.size(); } 30 31 ~obj_loader(); -
trunk/nv/gfx/animation.hh
r286 r287 73 73 74 74 uint32 slot = 0; 75 int index = -1; 75 int index0 = -1; 76 int index1 = -1; 76 77 float factor = 1.0f; 77 78 if ( desc.slots[0].vslot == animation_slot::TIME ) 78 79 { 80 NV_ASSERT( desc.slots[0].offset == 0, "time offset not zero!" ); 79 81 slot++; 80 82 keyfresult--; … … 84 86 return keyfresult; 85 87 } 86 uint32 toffset = desc.slots[0].offset / 4; 87 for ( int i = 0 ; i < (int)count - 1 ; i++ ) 88 for ( int i = 1 ; i < (int)count ; i++ ) 88 89 { 89 if ( time < fdata[ i * keyfsize + keyfsize + toffset ] ) { index = i; break; }90 if ( time < fdata[ i * keyfsize ] ) { index0 = i - 1; break; } 90 91 } 91 NV_ASSERT( index >= 0, "animation time fail!"); 92 float time0 = fdata[ index * keyfsize + toffset ]; 93 float time1 = fdata[ index * keyfsize + keyfsize + toffset ]; 92 NV_ASSERT( index0 >= 0, "animation time fail!"); 93 index1 = index0 + 1; 94 float time0 = fdata[ index0 * keyfsize ]; 95 float time1 = fdata[ index1 * keyfsize ]; 94 96 float delta = time1 - time0; 95 97 factor = glm::clamp( (time - time0) / delta, 0.0f, 1.0f ); … … 102 104 return keyfresult; 103 105 } 104 index = glm::clamp<int>( int( time ), 0, count - 2 ); 105 factor = glm::clamp<float> ( time - index, 0.0f, 1.0f ); 106 index0 = glm::clamp<int>( int( time ), 0, count - 2 ); 107 index1 = index0 + 1; 108 factor = glm::clamp<float> ( time - index0, 0.0f, 1.0f ); 106 109 } 107 110 uint32 ret = 0; … … 110 113 ret += nv::interpolate_raw( 111 114 desc.slots[slot], factor, 112 fdata + index * keyfsize + desc.slots[slot].offset / 4,113 fdata + index * keyfsize +keyfsize + desc.slots[slot].offset / 4,115 fdata + index0 * keyfsize + desc.slots[slot].offset / 4, 116 fdata + index1 * keyfsize + desc.slots[slot].offset / 4, 114 117 result + ret ); 115 118 } -
trunk/nv/gfx/keyframed_mesh.hh
r285 r287 24 24 : animation_entry( name ), m_start( a_start ), m_frames( a_frames ), m_fps( a_fps ), m_looping( a_loop ) {} 25 25 virtual uint32 get_frame_rate() const { return m_fps; } 26 virtual uint32 get_frame_count() const { returnm_frames; }26 virtual float get_duration() const { return (float)m_frames; } 27 27 virtual bool is_looping() const { return m_looping; } 28 28 protected: … … 37 37 { 38 38 public: 39 keyframed_mesh( device* a_device, mesh_data* a_data, tag_map* a_tag_map );39 keyframed_mesh( device* a_device, const mesh_data* a_data, const mesh_nodes_data* a_tag_map ); 40 40 virtual size_t get_index_count() const { return m_index_count; } 41 41 virtual void run_animation( animation_entry* a_anim ); 42 42 size_t get_max_frames() const; 43 transform get_tag( uint32 tag_id ) const; 43 virtual transform get_node_transform( uint32 node_id ) const; 44 virtual mat4 get_node_matrix( uint32 node_id ) const; 44 45 virtual void setup_animation( uint32 start, uint32 count, uint32 fps, bool loop ); 45 46 virtual void set_frame( uint32 frame ); … … 58 59 }; 59 60 60 mesh_data*m_mesh_data;61 tag_map*m_tag_map;61 const mesh_data* m_mesh_data; 62 const mesh_nodes_data* m_tag_map; 62 63 63 64 uint32 m_start_frame; … … 79 80 { 80 81 public: 81 keyframed_mesh_gpu( device* a_device, mesh_data* a_data, tag_map* a_tag_map, program* a_program );82 keyframed_mesh_gpu( device* a_device, const mesh_data* a_data, const mesh_nodes_data* a_tag_map, program* a_program ); 82 83 void update( uint32 ms ); 83 84 private: … … 92 93 { 93 94 public: 94 keyframed_mesh_cpu( device* a_device, mesh_data* a_data, tag_map* a_tag_map );95 keyframed_mesh_cpu( device* a_device, const mesh_data* a_data, const mesh_nodes_data* a_tag_map ); 95 96 void update( uint32 ms ); 96 97 private: -
trunk/nv/gfx/skeletal_mesh.hh
r283 r287 37 37 { 38 38 public: 39 skeletal_mesh( device* a_device, md5_mesh_data* a_mesh_data);40 virtual size_t get_index_count() const { return m_ mesh_data->get_index_count(); }39 skeletal_mesh( device* a_device, const mesh_data* a_mesh_data, const mesh_nodes_data* bones ); 40 virtual size_t get_index_count() const { return m_indices; } 41 41 virtual void run_animation( animation_entry* a_anim ); 42 42 virtual void update_animation( animation_entry* a_anim, uint32 a_anim_time ); 43 43 virtual ~skeletal_mesh(); 44 44 protected: 45 md5_mesh_instance* m_mesh_data; 46 std::vector< transform > m_transform; 45 uint32 m_indices; 46 dynamic_array< md5_vtx_pnt > m_pntdata; 47 dynamic_array< transform > m_pos_offset; 48 dynamic_array< transform > m_bone_offset; 49 ; 50 const mesh_data* m_data; 51 const md5_vtx_pntiw* m_vtx_data; 52 dynamic_array< transform > m_transform; 47 53 }; 48 54 … … 50 56 { 51 57 public: 52 skeletal_animation_entry_gpu( const std::string& name, nmd_temp_animation* anim, bool a_looping ) 53 : animation_entry( name ), m_animation( anim ), m_looping( a_looping ), m_prepared( false ) {} 54 virtual uint32 get_frame_rate() const { return 0; } 55 virtual uint32 get_frame_count() const { return 0; } 58 skeletal_animation_entry_gpu( const std::string& name, const mesh_nodes_data* anim, bool a_looping ); 59 virtual uint32 get_frame_rate() const { return m_node_data->get_frame_rate(); } 60 virtual float get_duration() const { return m_node_data->get_duration(); } 56 61 virtual bool is_looping() const { return m_looping; } 57 void prepare( const nmd_temp_model* m_model ); 58 void update_skeleton( mat4* tr, uint32 time ); 62 void prepare( const mesh_nodes_data* bones ); 63 void update_skeleton( mat4* tr, uint32 time ) const; 64 ~skeletal_animation_entry_gpu(); 59 65 protected: 60 nmd_temp_animation* m_animation; 61 bool m_prepared; 62 bool m_looping; 66 void animate_rec( mat4* data, float time, uint32 node_id, const mat4& parent_mat ) const; 67 const mesh_nodes_data* m_node_data; 68 std::vector< uint32 >* m_children; 69 sint16* m_bone_ids; 70 mat4* m_offsets; 71 bool m_prepared; 72 bool m_looping; 63 73 }; 64 74 … … 66 76 { 67 77 public: 68 skeletal_mesh_gpu( device* a_device, const nmd_temp_model* a_model, uint32 index, bool primary);78 skeletal_mesh_gpu( device* a_device, const mesh_data* a_mesh, const mesh_nodes_data* a_bone_data ); 69 79 virtual size_t get_index_count() const { return m_index_count; } 70 80 virtual void run_animation( animation_entry* a_anim ); … … 72 82 virtual void update_animation( animation_entry* a_anim, uint32 73 83 a_anim_time ); 84 virtual transform get_node_transform( uint32 node_id ) const; 85 virtual mat4 get_node_matrix( uint32 node_id ) const; 86 ~skeletal_mesh_gpu() { delete m_transform; } 74 87 protected: 75 const nmd_temp_model* m_model; 76 bool m_primary; 88 const mesh_nodes_data* m_bone_data; 77 89 uint32 m_index_count; 78 std::vector< mat4 >m_transform;90 mat4* m_transform; 79 91 }; 80 92 -
trunk/nv/handle.hh
r274 r287 244 244 } 245 245 246 const value_type* get( handle h ) const 247 { 248 if ( h.is_nil() ) return nullptr; 249 sint32 eindex = m_indexes.get_index( h ); 250 return eindex >= 0 ? &(m_data[ eindex ]) : nullptr; 251 } 252 246 253 value_type* get( handle h ) 247 254 { -
trunk/nv/interface/animated_mesh.hh
r285 r287 28 28 virtual const std::string& get_name() const { return m_name; } 29 29 virtual uint32 get_frame_rate() const = 0; 30 virtual uint32 get_frame_count() const = 0;30 virtual float get_duration() const = 0; 31 31 virtual bool is_looping() const = 0; 32 32 virtual ~animation_entry() {} … … 41 41 virtual void update_animation( animation_entry*, uint32 ) {} 42 42 virtual void run_animation( animation_entry* ) {} 43 virtual transform get_ tag( uint32 ) const { return transform(); }44 43 virtual transform get_node_transform( uint32 ) const { return transform(); } 44 virtual mat4 get_node_matrix( uint32 ) const { return mat4(); } 45 45 }; 46 46 -
trunk/nv/interface/device.hh
r280 r287 104 104 { 105 105 const mesh_raw_channel* channel = channels[ch]; 106 if ( channel-> is_index())106 if ( channel->count > 0 ) 107 107 { 108 index_buffer* ib = create_index_buffer( hint, channel->size(), channel->data ); 109 va->set_index_buffer( ib, channel->desc.slots[0].etype, true ); 110 } 111 else 112 { 113 vertex_buffer* vb = create_vertex_buffer( hint, channel->size(), channel->data ); 114 va->add_vertex_buffers( vb, channel ); 108 if ( channel->is_index() ) 109 { 110 index_buffer* ib = create_index_buffer( hint, channel->size(), channel->data ); 111 va->set_index_buffer( ib, channel->desc.slots[0].etype, true ); 112 } 113 else 114 { 115 vertex_buffer* vb = create_vertex_buffer( hint, channel->size(), channel->data ); 116 va->add_vertex_buffers( vb, channel ); 117 } 115 118 } 116 119 } -
trunk/nv/interface/mesh_data.hh
r281 r287 79 79 { 80 80 public: 81 mesh_data() : m_index_channel( nullptr ) {} 81 explicit mesh_data() : m_index_channel( nullptr ) {} 82 explicit mesh_data( const std::string& name ) : m_name(name), m_index_channel( nullptr ) {} 83 82 84 void add_channel( mesh_raw_channel* channel ) 83 85 { … … 137 139 } 138 140 141 void set_name( const std::string& name ) { m_name = name; } 142 const std::string& get_name() const { return m_name; } 143 144 void move_to( mesh_data& other ) 145 { 146 other.m_name = m_name; 147 other.m_index_channel = m_index_channel; 148 for ( auto c : m_channels ) 149 { 150 other.m_channels.push_back( c ); 151 } 152 m_channels.clear(); 153 m_index_channel = nullptr; 154 } 155 139 156 virtual ~mesh_data() 140 157 { … … 142 159 } 143 160 private: 161 std::string m_name; 144 162 std::vector< mesh_raw_channel* > m_channels; 145 163 mesh_raw_channel* m_index_channel; 146 164 }; 147 165 148 149 166 } 150 167 -
trunk/nv/interface/mesh_loader.hh
r285 r287 26 26 { 27 27 28 // TODO: change to generic nodes!29 class tag_map30 {31 public:32 typedef std::unordered_map< std::string, uint32 > map;33 34 tag_map() {}35 36 const key_data* get_tag( uint32 id ) const37 {38 return m_data[id];39 }40 uint32 get_tag_id( const std::string& key ) const41 {42 return m_map.at( key );43 }44 void insert( const std::string& key, key_data* chan )45 {46 m_map[ key ] = m_data.size();47 m_data.push_back( chan );48 }49 ~tag_map()50 {51 for ( auto t : m_data )52 delete t;53 }54 private:55 std::vector< key_data* > m_data;56 map m_map;57 };58 59 28 struct mesh_node_data 60 29 { … … 66 35 }; 67 36 37 class mesh_nodes_data 38 { 39 public: 40 explicit mesh_nodes_data( const string& name, uint32 count, mesh_node_data* nodes ) 41 : m_name( name ), m_count( count ), m_nodes( nodes ), m_frame_rate(0), m_duration(0.0f), m_flat( false ) 42 { 43 } 44 45 explicit mesh_nodes_data( const string& name, uint32 count, mesh_node_data* nodes, 46 uint16 a_fps, float a_frames, bool a_flat ) 47 : m_name( name ), m_count( count ), m_nodes( nodes ), m_frame_rate(a_fps), m_duration(a_frames), m_flat( a_flat ) 48 { 49 } 50 51 size_t get_count() const { return m_count; } 52 53 const mesh_node_data* get_node( size_t i ) const 54 { 55 if ( i >= m_count ) return nullptr; 56 return &m_nodes[i]; 57 } 58 59 key_data* release_node_data( size_t i ) 60 { 61 key_data* result = m_nodes[i].data; 62 m_nodes[i].data = nullptr; 63 return result; 64 } 65 66 bool is_flat() const { return m_flat; } 67 uint16 get_frame_rate() const { return m_frame_rate; } 68 float get_duration() const { return m_duration; } 69 void set_name( const std::string& name ) { m_name = name; } 70 const string& get_name() const { return m_name; } 71 72 ~mesh_nodes_data() 73 { 74 if ( m_count > 0 ) 75 for ( uint32 i = 0; i < m_count; ++i ) delete m_nodes[i].data; 76 delete[] m_nodes; 77 } 78 79 private: 80 string m_name; 81 uint32 m_count; 82 mesh_node_data* m_nodes; 83 uint16 m_frame_rate; 84 float m_duration; 85 bool m_flat; 86 }; 87 88 class mesh_data_pack 89 { 90 public: 91 explicit mesh_data_pack( uint32 a_count, mesh_data* a_meshes, mesh_nodes_data* a_nodes = nullptr ) 92 { 93 m_count = a_count; 94 m_meshes = a_meshes; 95 m_nodes = a_nodes; 96 } 97 const mesh_data* get_mesh( uint32 index ) const 98 { 99 if ( index >= m_count ) return nullptr; 100 return &m_meshes[ index ]; 101 } 102 uint32 get_count() const { return m_count; } 103 const mesh_nodes_data* get_nodes() const { return m_nodes; } 104 ~mesh_data_pack() 105 { 106 delete[] m_meshes; 107 delete m_nodes; 108 } 109 private: 110 uint32 m_count; 111 mesh_data* m_meshes; 112 mesh_nodes_data* m_nodes; 113 }; 114 115 68 116 class mesh_loader 69 117 { … … 71 119 mesh_loader() {} 72 120 virtual ~mesh_loader() {} 73 virtual tag_map* create_tag_map() { return nullptr; }74 121 virtual bool load( stream& source ) = 0; 75 122 virtual mesh_data* release_mesh_data( size_t index = 0 ) = 0; 123 virtual mesh_data_pack* release_mesh_data_pack() = 0; 76 124 virtual size_t get_mesh_count() const = 0; 77 virtual mesh_node_data* release_mesh_node_data( size_t ) { return nullptr; } 78 virtual size_t get_node_count() const { return 0; } 125 virtual mesh_nodes_data* release_mesh_nodes_data() { return nullptr; } 79 126 }; 80 127 -
trunk/nv/interface/program.hh
r277 r287 140 140 // } 141 141 // TODO: nicer check 142 NV_ASSERT( count <= base->get_length(), "LENGTH CHECK FAIL" );142 NV_ASSERT( (int)count <= base->get_length(), "LENGTH CHECK FAIL" ); 143 143 ((uniform<T>*)( base ))->set_value( value, count ); 144 144 } -
trunk/src/formats/assimp_loader.cc
r285 r287 79 79 { 80 80 if ( index >= m_mesh_count ) return nullptr; 81 mesh_data* result = new mesh_data(); 81 mesh_data* result = new mesh_data; 82 load_mesh_data( result, index ); 83 return result; 84 } 85 void nv::assimp_loader::load_mesh_data( mesh_data* data, size_t index ) 86 { 82 87 const aiScene* scene = (const aiScene*)m_scene; 83 88 const aiMesh* mesh = scene->mMeshes[ index ]; 89 data->set_name( mesh->mName.data ); 84 90 85 91 vec3 vertex_offset = glm::vec3(); … … 94 100 channel = mesh_raw_channel::create< assimp_plain_vtx >( mesh->mNumVertices ); 95 101 96 result->add_channel( channel );102 data->add_channel( channel ); 97 103 for (unsigned int i=0; i<mesh->mNumVertices; i++) 98 104 { … … 139 145 140 146 mesh_raw_channel* ichannel = mesh_raw_channel::create_index( USHORT, mesh->mNumFaces * 3 ); 141 result->add_channel( ichannel );147 data->add_channel( ichannel ); 142 148 uint16* indices = (uint16*)ichannel->data; 143 149 for (unsigned int i=0; i<mesh->mNumFaces; i++) … … 149 155 } 150 156 } 151 152 return result;153 157 } 154 158 … … 322 326 const aiAnimation* anim = scene->mAnimations[0]; // if implemented, change in load_node also 323 327 324 result->f ps = (float)anim->mTicksPerSecond;328 result->frame_rate = (uint16)anim->mTicksPerSecond; 325 329 result->duration = (float)anim->mDuration; 326 result->pretransformed = pre_transform; 330 result->flat = pre_transform; 331 result->max_frames = 0; 327 332 328 333 load_node( result, root, 0, -1 ); 334 335 // DAE pre_transform hack 336 if ( result->flat && result->frame_rate == 1 ) 337 { 338 result->frame_rate = 32; 339 result->duration = (float)result->max_frames; 340 } 341 329 342 return result; 330 343 } … … 371 384 if (anode) 372 385 { 373 if ( data-> pretransformed)386 if ( data->flat ) 374 387 { 375 388 create_transformed_keys( &a_data, anode, parent_id >= 0 ? &(data->nodes[ parent_id ]) : nullptr ); … … 379 392 create_direct_keys( &a_data, anode ); 380 393 } 394 data->max_frames = glm::max<uint32>( a_data.data->get_channel(0)->count, data->max_frames ); 381 395 } 382 396 … … 386 400 next = load_node( data, node->mChildren[i], next, this_id ); 387 401 } 402 388 403 return next; 389 404 } … … 432 447 key_raw_channel* raw_pchannel = key_raw_channel::create<assimp_key_p>( node->mNumPositionKeys ); 433 448 key_raw_channel* raw_rchannel = key_raw_channel::create<assimp_key_r>( node->mNumRotationKeys ); 434 key_raw_channel* raw_schannel = key_raw_channel::create<assimp_key_s>( node->mNumScalingKeys );449 //key_raw_channel* raw_schannel = key_raw_channel::create<assimp_key_s>( node->mNumScalingKeys ); 435 450 data->data->add_channel( raw_pchannel ); 436 451 data->data->add_channel( raw_rchannel ); 437 data->data->add_channel( raw_schannel );452 //data->data->add_channel( raw_schannel ); 438 453 assimp_key_p* pchannel = ((assimp_key_p*)(raw_pchannel->data)); 439 454 assimp_key_r* rchannel = ((assimp_key_r*)(raw_rchannel->data)); 440 assimp_key_s* schannel = ((assimp_key_s*)(raw_schannel->data));455 //assimp_key_s* schannel = ((assimp_key_s*)(raw_schannel->data)); 441 456 442 457 for ( unsigned np = 0; np < node->mNumPositionKeys; ++np ) … … 450 465 rchannel[np].rotation = glm::quat_cast( m_r33 * glm::mat3_cast( assimp_quat_cast(node->mRotationKeys[np].mValue ) ) * m_ri33 ); 451 466 } 452 if ( node->mNumScalingKeys > 0 ) 453 { 454 nv::vec3 scale_vec0 = assimp_vec3_cast( node->mScalingKeys[0].mValue ); 455 float scale_value = glm::length( glm::abs( scale_vec0 - nv::vec3(1,1,1) ) ); 456 if ( node->mNumScalingKeys > 1 || scale_value > 0.001 ) 457 { 458 NV_LOG( nv::LOG_WARNING, "scale key significant!" ); 459 for ( unsigned np = 0; np < node->mNumRotationKeys; ++np ) 460 { 461 schannel[np].time = (float)node->mScalingKeys[np].mTime; 462 schannel[np].scale = assimp_vec3_cast(node->mScalingKeys[np].mValue); 463 } 464 } 465 else 466 { 467 schannel[0].time = (float)node->mScalingKeys[0].mTime; 468 schannel[0].scale = assimp_vec3_cast(node->mScalingKeys[0].mValue); 469 } 470 } 471 472 } 473 467 // if ( node->mNumScalingKeys > 0 ) 468 // { 469 // nv::vec3 scale_vec0 = assimp_vec3_cast( node->mScalingKeys[0].mValue ); 470 // float scale_value = glm::length( glm::abs( scale_vec0 - nv::vec3(1,1,1) ) ); 471 // if ( node->mNumScalingKeys > 1 || scale_value > 0.001 ) 472 // { 473 // NV_LOG( nv::LOG_WARNING, "scale key significant!" ); 474 // for ( unsigned np = 0; np < node->mNumRotationKeys; ++np ) 475 // { 476 // schannel[np].time = (float)node->mScalingKeys[np].mTime; 477 // schannel[np].scale = assimp_vec3_cast(node->mScalingKeys[np].mValue); 478 // } 479 // } 480 // else 481 // { 482 // schannel[0].time = (float)node->mScalingKeys[0].mTime; 483 // schannel[0].scale = assimp_vec3_cast(node->mScalingKeys[0].mValue); 484 // } 485 // } 486 487 } 488 489 mesh_data_pack* nv::assimp_loader::release_mesh_data_pack() 490 { 491 mesh_data* meshes = new mesh_data[ m_mesh_count ]; 492 for ( uint32 i = 0; i < m_mesh_count; ++i ) 493 { 494 load_mesh_data(&meshes[i],i); 495 } 496 return new mesh_data_pack( m_mesh_count, meshes, release_mesh_nodes_data() ); 497 } 498 499 mesh_nodes_data* nv::assimp_loader::release_mesh_nodes_data() 500 { 501 // TODO: implement 502 return nullptr; 503 } 504 -
trunk/src/formats/md2_loader.cc
r280 r287 315 315 mesh_data* nv::md2_loader::release_mesh_data( size_t ) 316 316 { 317 return release_mesh_frame( -1 ); 318 } 319 320 mesh_data* nv::md2_loader::release_mesh_frame( sint32 frame ) 317 mesh_data* data = new mesh_data( "md2_mesh" ); 318 release_mesh_frame( data, -1 ); 319 return data; 320 } 321 322 void nv::md2_loader::release_mesh_frame( mesh_data* data, sint32 frame ) 321 323 { 322 324 md2_t* md2 = (md2_t*)m_md2; … … 366 368 } 367 369 368 mesh_data* result = new mesh_data(); 369 result->add_channel( mc_pn ); 370 result->add_channel( mc_t ); 371 result->add_channel( ic ); 372 return result; 373 } 370 data->add_channel( mc_pn ); 371 data->add_channel( mc_t ); 372 data->add_channel( ic ); 373 } 374 375 mesh_data_pack* nv::md2_loader::release_mesh_data_pack() 376 { 377 mesh_data* data = new mesh_data[1]; 378 release_mesh_frame( &data[0], -1 ); 379 return new mesh_data_pack( 1, data ); 380 } -
trunk/src/formats/md3_loader.cc
r285 r287 321 321 mesh_data* nv::md3_loader::release_mesh_data( size_t ) 322 322 { 323 return release_mesh_frame( -1 ); 324 } 325 326 mesh_data* nv::md3_loader::release_mesh_frame( sint32 frame ) 323 mesh_data* data = new mesh_data; 324 release_mesh_frame( data, -1 ); 325 return data; 326 } 327 328 void nv::md3_loader::release_mesh_frame( mesh_data* data, sint32 frame ) 327 329 { 328 330 md3_t* md3 = (md3_t*)m_md3; … … 394 396 } 395 397 396 mesh_data* result = new mesh_data(); 397 result->add_channel( mc_pn ); 398 result->add_channel( mc_t ); 399 result->add_channel( ic ); 400 return result; 401 } 402 403 tag_map* nv::md3_loader::create_tag_map() 398 data->set_name( (char*)md3->header.name ); 399 data->add_channel( mc_pn ); 400 data->add_channel( mc_t ); 401 data->add_channel( ic ); 402 } 403 404 mesh_nodes_data* nv::md3_loader::release_mesh_nodes_data() 404 405 { 405 406 md3_t* md3 = (md3_t*)m_md3; 406 tag_map* result = new tag_map(); 407 for ( sint32 i = 0; i < md3->header.num_tags; ++i ) 407 uint32 node_count = md3->header.num_tags; 408 if ( node_count == 0 ) return nullptr;; 409 mesh_node_data* nodes = new mesh_node_data[ node_count ]; 410 for ( uint32 i = 0; i < node_count; ++i ) 408 411 { 409 412 const md3_tag_t& rtag = md3->tags[i]; 410 413 std::string name( (char*)(rtag.name) ); 411 414 412 key_data* data = new key_data; 413 data->add_channel( load_tags( name ) ); 414 result->insert( name, data ); 415 } 416 return result; 417 } 418 419 mesh_node_data* nv::md3_loader::release_mesh_node_data( size_t index ) 420 { 421 md3_t* md3 = (md3_t*)m_md3; 422 const md3_tag_t& rtag = md3->tags[index]; 423 std::string name( (char*)(rtag.name) ); 424 425 mesh_node_data* result = new mesh_node_data; 426 result->transform = mat4(); 427 result->name = name; 428 result->parent_id = -1; 429 result->target_id = -1; 430 result->data = new key_data; 415 nodes[i].transform = mat4(); 416 nodes[i].name = name; 417 nodes[i].parent_id = -1; 418 nodes[i].target_id = -1; 419 nodes[i].data = new key_data; 431 420 432 key_raw_channel* keys = load_tags( name ); 433 result->data->add_channel( keys ); 434 return result; 435 } 436 437 size_t nv::md3_loader::get_node_count() const 438 { 439 return ((md3_t*)m_md3)->header.num_tags; 421 key_raw_channel* keys = load_tags( name ); 422 nodes[i].data->add_channel( keys ); 423 } 424 return new mesh_nodes_data( "tags", node_count, nodes ); 425 } 426 427 mesh_data_pack* nv::md3_loader::release_mesh_data_pack() 428 { 429 mesh_data* data = new mesh_data[1]; 430 release_mesh_frame( &data[0], -1 ); 431 return new mesh_data_pack( 1, data, release_mesh_nodes_data() ); 440 432 } 441 433 -
trunk/src/formats/md5_loader.cc
r282 r287 10 10 #include "nv/logging.hh" 11 11 #include "nv/io/std_stream.hh" 12 #include "nv/profiler.hh"13 12 #include <cstring> 14 13 … … 45 44 bool md5_loader::load( stream& source ) 46 45 { 47 NV_PROFILE( "Load MD5" ); // 16XXms original48 46 std_stream sstream( &source ); 49 47 std::string command; … … 96 94 else if ( command == "mesh" ) 97 95 { 98 md5_mesh_data* mesh = new md5_mesh_data(); 99 100 int num_verts, num_tris, num_weights; 96 mesh_data* mesh = new mesh_data("md5_mesh"); 97 98 int num_verts = 0; 99 int num_tris = 0; 100 int num_weights = 0; 101 101 102 102 discard( sstream, "{" ); … … 106 106 if ( command == "shader" ) 107 107 { 108 s stream >> mesh->m_shader;109 remove_quotes( mesh->m_shader );110 // texturePath.replace_extension( ".tga");108 std::string shader; 109 sstream >> shader; 110 remove_quotes( shader ); 111 111 next_line( sstream ); 112 112 } … … 115 115 sstream >> num_verts; 116 116 117 md5_vtx_t* tdata = nullptr; 117 118 { 118 119 mesh_raw_channel* ch_pnt = mesh_raw_channel::create<md5_vtx_pnt>( num_verts ); 119 120 mesh_raw_channel* ch_t = mesh_raw_channel::create<md5_vtx_t>( num_verts ); 120 mesh ->m_pntdata = (md5_vtx_pnt*)ch_pnt->data;121 mesh->m_tdata= (md5_vtx_t*)ch_t->data;121 mesh_raw_channel* ch_pntiw = mesh_raw_channel::create<md5_vtx_pntiw>( num_verts ); 122 tdata = (md5_vtx_t*)ch_t->data; 122 123 mesh->add_channel( ch_pnt ); 123 124 mesh->add_channel( ch_t ); 125 // TODO: hack to prevent rendering 126 ch_pntiw->count = 0; 127 mesh->add_channel( ch_pntiw ); 124 128 } 125 mesh->m_vtx_data.resize( num_verts );126 129 weight_info.resize( num_verts ); 127 130 … … 138 141 weight_info[i].start_weight = start_weight; 139 142 weight_info[i].weight_count = weight_count; 140 mesh->m_tdata[i].texcoord = texcoord;143 tdata[i].texcoord = texcoord; 141 144 } 142 145 } … … 147 150 mesh_raw_channel* ch_i = mesh_raw_channel::create_index<uint32>( num_tris * 3 ); 148 151 uint32* vtx_i = (uint32*)ch_i->data; 149 mesh->m_idata = vtx_i;150 152 uint32 idx = 0; 151 153 mesh->add_channel( ch_i ); … … 190 192 } 191 193 192 prepare_mesh( mesh, weights.data(), weight_info.data() );194 prepare_mesh( weight_info.size(), mesh, weights.data(), weight_info.data() ); 193 195 194 196 m_meshes[ m_num_meshes ] = mesh; … … 201 203 } 202 204 203 bool md5_loader::prepare_mesh( md5_mesh_data* mdata, md5_weight* weights, md5_weight_info* weight_info ) 204 { 205 uint32 vtx_count = mdata->m_vtx_data.size(); 206 md5_vtx_pnt* vtcs = mdata->m_pntdata; 207 208 mdata->m_bone_offset.resize( m_joints.size() ); 209 for ( uint32 i = 0; i < m_joints.size(); ++i ) 210 { 211 transform j( m_joints[i].pos, m_joints[i].orient ); 212 mdata->m_bone_offset[i] = j.inverse(); 213 } 205 bool md5_loader::prepare_mesh( uint32 vtx_count, mesh_data* mdata, md5_weight* weights, md5_weight_info* weight_info ) 206 { 207 md5_vtx_pnt* vtcs = (md5_vtx_pnt*)mdata->get_channel< md5_vtx_pnt >()->data; 208 md5_vtx_pntiw* vtx_data = (md5_vtx_pntiw*)mdata->get_channel< md5_vtx_pntiw >()->data; 214 209 215 210 for ( uint32 i = 0; i < vtx_count; ++i ) … … 217 212 size_t start_weight = weight_info[i].start_weight; 218 213 size_t weight_count = weight_info[i].weight_count; 219 md5_vtx_ data& vdata = mdata->m_vtx_data[i];214 md5_vtx_pntiw& vdata = vtx_data[i]; 220 215 md5_vtx_pnt& vtc = vtcs[i]; 221 216 … … 268 263 } 269 264 265 const uint32* idata = (uint32*)mdata->get_index_channel()->data; 266 const md5_vtx_t* tdata = mdata->get_channel_data<md5_vtx_t>(); 267 270 268 // Prepare normals 271 269 uint32 tri_count = mdata->get_count() / 3; 272 270 for ( unsigned int i = 0; i < tri_count; ++i ) 273 271 { 274 uint32 ti0 = mdata->m_idata[ i * 3 ];275 uint32 ti1 = mdata->m_idata[ i * 3 + 1 ];276 uint32 ti2 = mdata->m_idata[ i * 3 + 2 ];272 uint32 ti0 = idata[ i * 3 ]; 273 uint32 ti1 = idata[ i * 3 + 1 ]; 274 uint32 ti2 = idata[ i * 3 + 2 ]; 277 275 278 276 glm::vec3 v1 = vtcs[ ti0 ].position; … … 288 286 vtcs[ ti2 ].normal += normal; 289 287 290 const vec2& w1 = mdata->m_tdata[ ti0 ].texcoord;291 const vec2& w2 = mdata->m_tdata[ ti1 ].texcoord;292 const vec2& w3 = mdata->m_tdata[ ti2 ].texcoord;288 const vec2& w1 = tdata[ ti0 ].texcoord; 289 const vec2& w2 = tdata[ ti1 ].texcoord; 290 const vec2& w3 = tdata[ ti2 ].texcoord; 293 291 294 292 vec2 st1 = w3 - w1; … … 306 304 for ( size_t i = 0; i < vtx_count; ++i ) 307 305 { 308 md5_vtx_ data& vdata = mdata->m_vtx_data[i];306 md5_vtx_pntiw& vdata = vtx_data[i]; 309 307 310 308 glm::vec3 normal = glm::normalize( vtcs[i].normal ); … … 541 539 } 542 540 543 md5_mesh_instance* nv::md5_mesh_data::spawn() const 544 { 545 return new md5_mesh_instance( this ); 546 } 541 mesh_nodes_data* nv::md5_loader::release_mesh_nodes_data() 542 { 543 mesh_node_data* nodes = new mesh_node_data[ m_num_joints ]; 544 for ( uint32 i = 0; i < m_num_joints; ++i ) 545 { 546 mesh_node_data& node = nodes[i]; 547 node.name = m_joints[i].name; 548 node.target_id = -1; 549 node.parent_id = -1; 550 node.transform = transform( m_joints[i].pos, m_joints[i].orient ).inverse().extract(); 551 node.data = nullptr; 552 } 553 return new mesh_nodes_data( "nodes", m_num_joints, nodes ); 554 } 555 556 mesh_data_pack* nv::md5_loader::release_mesh_data_pack() 557 { 558 uint32 size = m_meshes.size(); 559 mesh_data* meshes = new mesh_data[ size ]; 560 for ( uint32 i = 0; i < size; ++i ) 561 { 562 m_meshes[i]->move_to( meshes[i] ); 563 delete m_meshes[i]; 564 m_meshes[i] = nullptr; 565 } 566 return new mesh_data_pack( size, meshes, release_mesh_nodes_data() ); 567 } 568 547 569 548 570 nv::md5_loader::~md5_loader() … … 551 573 } 552 574 553 nv::md5_mesh_instance::md5_mesh_instance( const md5_mesh_data* a_data )554 : m_data( a_data ), m_indices( 0 )555 {556 m_indices = m_data->get_count();557 m_pntdata.assign( m_data->m_pntdata, m_data->m_vtx_data.size() );558 }559 560 void nv::md5_mesh_instance::apply( const transform* skeleton )561 {562 NV_PROFILE("md5::apply");563 size_t skeleton_size = m_data->m_bone_offset.size();564 size_t vertex_count = m_pntdata.size();565 m_pos_offset.resize( skeleton_size );566 for ( unsigned int i = 0; i < skeleton_size; ++i )567 {568 m_pos_offset[i] = skeleton[i] * m_data->m_bone_offset[i];569 }570 571 std::fill( m_pntdata.raw_data(), m_pntdata.raw_data() + m_pntdata.raw_size(), 0 );572 for ( unsigned int i = 0; i < vertex_count; ++i )573 {574 const md5_vtx_data& vert = m_data->m_vtx_data[i];575 md5_vtx_pnt& result = m_pntdata[i];576 577 for ( size_t j = 0; j < 4; ++j )578 {579 int index = vert.boneindex[j];580 float weight = vert.boneweight[j];581 const quat& orient = skeleton[index].get_orientation();582 const transform& offset = m_pos_offset[index];583 result.position += offset.transformed( vert.position ) * weight;584 result.normal += ( orient * vert.normal ) * weight;585 result.tangent += ( orient * vert.tangent ) * weight;586 }587 }588 } -
trunk/src/formats/nmd_loader.cc
r285 r287 7 7 #include "nv/formats/nmd_loader.hh" 8 8 #include "nv/io/std_stream.hh" 9 #include "nv/string.hh" 9 10 10 11 using namespace nv; … … 24 25 case nmd_type::MESH : load_mesh( source, element_header ); break; 25 26 case nmd_type::ANIMATION : load_animation( source, element_header ); break; 26 case nmd_type::BONE_ARRAY : load_bones( source, element_header ); break;27 27 case nmd_type::STRING_TABLE : load_strings( source ); break; 28 28 default: NV_ASSERT( false, "UNKNOWN NMD ELEMENT!" ); break; … … 47 47 mesh->add_channel( channel ); 48 48 } 49 m_mesh_names.push_back( e.name ); 49 50 m_meshes.push_back( mesh ); 50 51 return true; … … 54 55 { 55 56 mesh_data* result = m_meshes[ index ]; 57 if ( m_strings ) result->set_name( m_strings->get( m_mesh_names[ index ] ) ); 56 58 m_meshes[ index ] = nullptr; 57 59 return result; 60 } 61 62 mesh_data_pack* nv::nmd_loader::release_mesh_data_pack() 63 { 64 uint32 size = m_meshes.size(); 65 mesh_data* meshes = new mesh_data[ size ]; 66 for ( uint32 i = 0; i < size; ++i ) 67 { 68 m_meshes[i]->move_to( meshes[i] ); 69 delete m_meshes[i]; 70 } 71 m_meshes.clear(); 72 return new mesh_data_pack( size, meshes, release_mesh_nodes_data() ); 58 73 } 59 74 … … 61 76 { 62 77 for ( auto mesh : m_meshes ) if ( mesh ) delete mesh; 63 if ( m_animation ) delete m_animation;64 78 if ( m_strings ) delete m_strings; 65 if ( m_ bone_data ) delete m_bone_data;79 if ( m_node_data ) delete m_node_data; 66 80 m_meshes.clear(); 67 m_animation = nullptr; 68 m_bone_data = nullptr; 69 m_strings = nullptr; 81 m_mesh_names.clear(); 82 m_node_names.clear(); 83 84 m_node_data = nullptr; 85 m_node_array = nullptr; 86 m_strings = nullptr; 70 87 } 71 88 … … 82 99 } 83 100 84 bool nv::nmd_loader::load_bones( stream& source, const nmd_element_header& e )85 {86 NV_ASSERT( m_bone_data == nullptr, "MULTIPLE BONE ENTRIES!" );87 m_bone_data = new nmd_bone_data;88 m_bone_data->bones = new nmd_bone[ e.children ];89 m_bone_data->count = (uint16)e.children;90 source.read( m_bone_data->bones, sizeof( nmd_bone ), e.children );91 return true;92 }93 94 nmd_animation* nv::nmd_loader::release_animation()95 {96 nmd_animation* result = m_animation;97 m_animation = nullptr;98 return result;99 }100 101 nmd_bone_data* nv::nmd_loader::release_bone_data()102 {103 nmd_bone_data* result = m_bone_data;104 m_bone_data = nullptr;105 return result;106 }107 108 string_table* nv::nmd_loader::release_string_table()109 {110 string_table* result = m_strings;111 m_strings = nullptr;112 return result;113 }114 115 116 101 bool nv::nmd_loader::load_animation( stream& source, const nmd_element_header& e ) 117 102 { 118 NV_ASSERT( m_animation == nullptr, "MULTIPLE ANIMATION ENTRIES!" ); 119 nmd_animation_header header; 120 source.read( &header, sizeof( header ), 1 ); 121 m_animation = new nmd_animation; 122 m_animation->fps = header.fps; 123 m_animation->duration = header.duration; 124 m_animation->flat = header.flat; 125 m_animation->node_count = (uint16)e.children; 126 m_animation->nodes = new nmd_node[ e.children ]; 103 NV_ASSERT( m_node_data == nullptr, "MULTIPLE NODE ENTRIES!" ); 104 nmd_animation_header animation_header; 105 source.read( &animation_header, sizeof( animation_header ), 1 ); 106 m_node_array = new mesh_node_data[ e.children ]; 127 107 for ( uint32 i = 0; i < e.children; ++i ) 128 108 { 129 109 nmd_element_header element_header; 130 110 source.read( &element_header, sizeof( element_header ), 1 ); 131 NV_ASSERT( element_header.type == nmd_type::ANIMATION_NODE, "ANIMATION_NODE expected!" ); 132 m_animation->nodes[i].name = element_header.name; 133 111 NV_ASSERT( element_header.type == nmd_type::NODE, "NODE expected!" ); 112 m_node_names.push_back( element_header.name ); 134 113 uint16 ch_count = element_header.children; 135 114 136 nmd_ animation_node_header node_header;115 nmd_node_header node_header; 137 116 source.read( &node_header, sizeof( node_header ), 1 ); 138 m_ animation->nodes[i].parent_id = node_header.parent_id;139 m_ animation->nodes[i].transform = node_header.transform;140 m_ animation->nodes[i].data = nullptr;117 m_node_array[i].parent_id = node_header.parent_id; 118 m_node_array[i].transform = node_header.transform; 119 m_node_array[i].data = nullptr; 141 120 if ( ch_count > 0 ) 142 121 { 143 122 key_data* kdata = new key_data; 144 m_ animation->nodes[i].data = kdata;123 m_node_array[i].data = kdata; 145 124 for ( uint32 c = 0; c < ch_count; ++c ) 146 125 { 147 126 source.read( &element_header, sizeof( element_header ), 1 ); 148 NV_ASSERT( element_header.type == nmd_type:: ANIMATION_CHANNEL, "ANIMATION_CHANNEL expected!" );149 nv::nmd_ animation_channel_header cheader;127 NV_ASSERT( element_header.type == nmd_type::KEY_CHANNEL, "CHANNEL expected!" ); 128 nv::nmd_key_channel_header cheader; 150 129 source.read( &cheader, sizeof( cheader ), 1 ); 151 130 key_raw_channel* channel = key_raw_channel::create( cheader.format, cheader.count ); … … 155 134 } 156 135 } 136 m_node_data = new mesh_nodes_data( "animation", e.children, m_node_array, animation_header.frame_rate, animation_header.duration, animation_header.flat ); 157 137 return true; 158 138 } 159 139 140 mesh_nodes_data* nv::nmd_loader::release_mesh_nodes_data() 141 { 142 if ( m_node_data ) 143 { 144 if ( m_strings ) 145 { 146 for ( uint32 i = 0; i < m_node_data->get_count(); ++i ) 147 { 148 m_node_array[i].name = m_strings->get( m_node_names[i] ); 149 } 150 } 151 mesh_nodes_data* result = m_node_data; 152 m_node_data = nullptr; 153 m_node_array = nullptr; 154 return result; 155 } 156 return nullptr; 157 } 160 158 161 159 // TEMPORARY 162 nv::nmd_temp_animation::nmd_temp_animation( nmd_loader* loader )163 {164 m_animation = loader->release_animation();165 m_strings = loader->release_string_table();166 160 167 for ( uint32 n = 0; n < m_animation->node_count; ++n ) 168 { 169 nmd_node& node = m_animation->nodes[n]; 170 m_data.push_back( node.data ); 171 node.data = nullptr; 172 } 173 174 if ( !m_animation->flat ) 175 { 176 m_children.resize( m_animation->node_count ); 177 for ( nv::uint32 n = 0; n < m_animation->node_count; ++n ) 178 { 179 const nmd_node& node = m_animation->nodes[n]; 180 if ( node.parent_id != -1 ) 181 { 182 m_children[ node.parent_id ].push_back( n ); 183 } 184 } 185 } 186 187 m_bone_ids.resize( m_animation->node_count ); 188 } 189 190 nv::nmd_temp_animation::~nmd_temp_animation() 191 { 192 for ( auto node : m_data ) delete node; 193 delete m_animation; 194 delete m_strings; 195 } 196 197 void nv::nmd_temp_animation::prepare( const nmd_temp_model* model ) 198 { 199 m_offsets = model->m_bone_offsets.data(); 200 for ( uint32 n = 0; n < m_animation->node_count; ++n ) 201 { 202 const nmd_node& node = m_animation->nodes[n]; 203 sint16 bone_id = -1; 204 205 auto bi = model->m_bone_names.find( m_strings->get( node.name ) ); 206 if ( bi != model->m_bone_names.end() ) 207 { 208 bone_id = bi->second; 209 } 210 m_bone_ids[n] = bone_id; 211 } 212 } 213 214 void nv::nmd_temp_animation::animate( mat4* data, uint32 time ) 215 { 216 float tick_time = ( time / 1000.0f ) * m_animation->fps; 217 float anim_time = fmodf( tick_time, m_animation->duration ); 218 219 if ( !m_animation->flat ) 220 { 221 animate_rec( data, anim_time, 0, mat4() ); 222 return; 223 } 224 225 for ( uint32 n = 0; n < m_animation->node_count; ++n ) 226 if ( m_bone_ids[n] >= 0 ) 227 { 228 const nmd_node* node = &m_animation->nodes[ n ]; 229 nv::mat4 node_mat( node->transform ); 230 231 if ( m_data[n] ) 232 { 233 node_mat = m_data[n]->get_matrix( anim_time ); 234 } 235 236 sint16 bone_id = m_bone_ids[n]; 237 data[ bone_id ] = node_mat * m_offsets[ bone_id ]; 238 } 239 240 } 241 242 void nv::nmd_temp_animation::animate_rec( mat4* data, float time, uint32 node_id, const mat4& parent_mat ) 243 { 244 // TODO: fix transforms, which are now embedded, 245 // see note in assimp_loader.cc:load_node 246 const nmd_node* node = &m_animation->nodes[ node_id ]; 247 mat4 node_mat( node->transform ); 248 249 if ( m_data[ node_id ] ) 250 { 251 node_mat = m_data[ node_id ]->get_matrix( time ); 252 } 253 254 mat4 global_mat = parent_mat * node_mat; 255 256 sint16 bone_id = m_bone_ids[ node_id ]; 257 if ( bone_id >= 0 ) 258 { 259 data[ bone_id ] = global_mat * m_offsets[ bone_id ]; 260 } 261 262 for ( auto child : m_children[ node_id ] ) 263 { 264 animate_rec( data, time, child, global_mat ); 265 } 266 } 267 268 nv::nmd_temp_model::nmd_temp_model( nmd_loader* loader ) 161 nv::nmd_temp_model_data::nmd_temp_model_data( nmd_loader* loader ) 269 162 { 270 163 for ( unsigned m = 0; m < loader->get_mesh_count(); ++m ) … … 272 165 m_mesh_data.push_back(loader->release_mesh_data(m)); 273 166 } 274 nmd_bone_data* bone_data = loader->release_bone_data(); 275 string_table* strings = loader->release_string_table(); 276 277 for ( nv::uint16 bi = 0; bi < bone_data->count; ++bi ) 278 { 279 m_bone_names[ strings->get( bone_data->bones[bi].name ) ] = bi; 280 m_bone_offsets.push_back( bone_data->bones[bi].offset ); 281 } 282 283 delete bone_data; 284 delete strings; 167 m_node_data = loader->release_mesh_nodes_data(); 285 168 } 286 169 287 nv::nmd_temp_model ::~nmd_temp_model()170 nv::nmd_temp_model_data::~nmd_temp_model_data() 288 171 { 289 172 for ( unsigned m = 0; m < m_mesh_data.size(); ++m ) … … 291 174 delete m_mesh_data[m]; 292 175 } 176 delete m_node_data; 293 177 } -
trunk/src/formats/obj_loader.cc
r280 r287 50 50 std::string line; 51 51 std::string cmd; 52 std::string name; 53 std::string next_name; 52 54 53 55 std::size_t size; … … 77 79 bool obj_reader::read_stream( std::istream& stream ) 78 80 { 81 name = next_name; 79 82 bool added_faces = false; 80 83 f32 x, y, z; … … 146 149 if ( cmd == "g" ) 147 150 { 148 if (added_faces) return true; 151 ss >> next_name; 152 if (added_faces) 153 return true; 154 name = next_name; 149 155 continue; 150 156 } … … 335 341 channel->count = reader->size * 3; 336 342 337 mesh_data* mesh = new mesh_data( );343 mesh_data* mesh = new mesh_data(reader->name); 338 344 mesh->add_channel( channel ); 339 345 m_meshes.push_back( mesh ); … … 357 363 for ( auto mesh : m_meshes ) if ( mesh ) delete mesh; 358 364 } 365 366 mesh_data_pack* nv::obj_loader::release_mesh_data_pack() 367 { 368 uint32 size = m_meshes.size(); 369 mesh_data* meshes = new mesh_data[ size ]; 370 for ( uint32 i = 0; i < size; ++i ) 371 { 372 m_meshes[i]->move_to( meshes[i] ); 373 delete m_meshes[i]; 374 } 375 m_meshes.clear(); 376 return new mesh_data_pack( size, meshes ); 377 } -
trunk/src/gfx/keyframed_mesh.cc
r285 r287 15 15 using namespace nv; 16 16 17 nv::keyframed_mesh::keyframed_mesh( device* a_device, mesh_data* a_data, tag_map* a_tag_map )17 nv::keyframed_mesh::keyframed_mesh( device* a_device, const mesh_data* a_data, const mesh_nodes_data* a_tag_map ) 18 18 : animated_mesh() 19 19 , m_mesh_data( a_data ) … … 40 40 } 41 41 42 transform keyframed_mesh::get_ tag( uint32 tag) const42 transform keyframed_mesh::get_node_transform( uint32 node_id ) const 43 43 { 44 44 NV_ASSERT( m_tag_map, "TAGMAP FAIL" ); 45 const key_data* data = m_tag_map->get_tag( tag ); 45 NV_ASSERT( node_id < m_tag_map->get_count(), "TAGMAP FAIL" ); 46 const key_data* data = m_tag_map->get_node( node_id )->data; 46 47 NV_ASSERT( data, "TAG FAIL" ); 47 48 transform last = data->get_raw_transform( m_last_frame ); 48 49 transform next = data->get_raw_transform( m_next_frame ); 49 50 return interpolate( last, next, m_interpolation ); 51 } 52 53 mat4 keyframed_mesh::get_node_matrix( uint32 node_id ) const 54 { 55 return get_node_transform( node_id ).extract(); 50 56 } 51 57 … … 125 131 } 126 132 127 nv::keyframed_mesh_gpu::keyframed_mesh_gpu( device* a_device, mesh_data* a_data, tag_map* a_tag_map, program* a_program )133 nv::keyframed_mesh_gpu::keyframed_mesh_gpu( device* a_device, const mesh_data* a_data, const mesh_nodes_data* a_tag_map, program* a_program ) 128 134 : keyframed_mesh( a_device, a_data, a_tag_map ) 129 135 , m_loc_next_position( 0 ) … … 159 165 } 160 166 161 nv::keyframed_mesh_cpu::keyframed_mesh_cpu( device* a_device, mesh_data* a_data, tag_map* a_tag_map )167 nv::keyframed_mesh_cpu::keyframed_mesh_cpu( device* a_device, const mesh_data* a_data, const mesh_nodes_data* a_tag_map ) 162 168 : keyframed_mesh( a_device, a_data, a_tag_map ) 163 169 { -
trunk/src/gfx/skeletal_mesh.cc
r283 r287 11 11 12 12 13 nv::skeletal_mesh::skeletal_mesh( device* a_device, md5_mesh_data* a_mesh_data)13 nv::skeletal_mesh::skeletal_mesh( device* a_device, const mesh_data* a_mesh_data, const mesh_nodes_data* bones ) 14 14 : animated_mesh() 15 , m_mesh_data( nullptr ) 16 { 17 m_mesh_data = a_mesh_data->spawn(); 15 , m_data( a_mesh_data ) 16 { 17 const mesh_raw_channel* pnt_chan = a_mesh_data->get_channel<md5_vtx_pnt>(); 18 m_pntdata.assign( (const md5_vtx_pnt*)pnt_chan->data, pnt_chan->count ); 19 m_bone_offset.resize( bones->get_count() ); 20 for ( uint32 i = 0; i < bones->get_count(); ++i ) 21 { 22 m_bone_offset[i] = transform( bones->get_node(i)->transform ); 23 } 24 m_vtx_data = a_mesh_data->get_channel_data<md5_vtx_pntiw>(); 25 m_indices = a_mesh_data->get_count(); 18 26 m_va = a_device->create_vertex_array( a_mesh_data, nv::STREAM_DRAW ); 19 27 } … … 28 36 uint32 new_time = a_anim_time % anim_duration; 29 37 anim->update_skeleton( m_transform.data(), (float)new_time * 0.001f ); 30 m_mesh_data->apply( m_transform.data() ); 38 39 //m_mesh_data->apply( m_transform.data() ); 40 { 41 size_t skeleton_size = m_bone_offset.size(); 42 size_t vertex_count = m_pntdata.size(); 43 m_pos_offset.resize( skeleton_size ); 44 for ( unsigned int i = 0; i < skeleton_size; ++i ) 45 { 46 m_pos_offset[i] = m_transform[i] * m_bone_offset[i]; 47 } 48 49 std::fill( m_pntdata.raw_data(), m_pntdata.raw_data() + m_pntdata.raw_size(), 0 ); 50 for ( unsigned int i = 0; i < vertex_count; ++i ) 51 { 52 const md5_vtx_pntiw& vert = m_vtx_data[i]; 53 54 for ( size_t j = 0; j < 4; ++j ) 55 { 56 int index = vert.boneindex[j]; 57 float weight = vert.boneweight[j]; 58 const quat& orient = m_transform[index].get_orientation(); 59 const transform& offset = m_pos_offset[index]; 60 m_pntdata[i].position += offset.transformed( vert.position ) * weight; 61 m_pntdata[i].normal += ( orient * vert.normal ) * weight; 62 m_pntdata[i].tangent += ( orient * vert.tangent ) * weight; 63 } 64 } 65 } 66 31 67 vertex_buffer* vb = m_va->find_buffer( nv::slot::POSITION ); 32 68 vb->bind(); 33 vb->update( m_ mesh_data->data(), 0, m_mesh_data->size() );69 vb->update( m_pntdata.data(), 0, m_pntdata.raw_size() ); 34 70 vb->unbind(); 35 71 } … … 39 75 { 40 76 delete m_va; 41 delete m_mesh_data;42 77 } 43 78 … … 52 87 } 53 88 54 void nv::skeletal_animation_entry_gpu::update_skeleton( mat4* data, uint32 time ) 55 { 56 m_animation->animate( data, time ); 57 } 58 59 void nv::skeletal_animation_entry_gpu::prepare( const nmd_temp_model* m_model ) 60 { 61 m_animation->prepare( m_model ); 62 } 63 64 nv::skeletal_mesh_gpu::skeletal_mesh_gpu( device* a_device, const nmd_temp_model* a_model, uint32 index, bool primary ) 65 : animated_mesh(), m_primary( primary ), m_model( a_model ) 66 { 67 const mesh_data* data = a_model->get_data( index ); 68 m_va = a_device->create_vertex_array( data, nv::STATIC_DRAW ); 69 m_index_count = data->get_count(); 89 90 nv::skeletal_animation_entry_gpu::skeletal_animation_entry_gpu( const std::string& name, const mesh_nodes_data* anim, bool a_looping ) 91 : animation_entry( name ) 92 , m_node_data( anim ) 93 { 94 uint32 node_count = m_node_data->get_count(); 95 96 m_prepared = false; 97 m_looping = a_looping; 98 m_children = nullptr; 99 m_offsets = nullptr; 100 m_bone_ids = new sint16[ node_count ]; 101 102 if ( !m_node_data->is_flat() ) 103 { 104 m_children = new std::vector< uint32 >[ node_count ]; 105 for ( uint32 n = 0; n < node_count; ++n ) 106 { 107 const mesh_node_data* node = m_node_data->get_node(n); 108 if ( node->parent_id != -1 ) 109 { 110 m_children[ node->parent_id ].push_back( n ); 111 } 112 } 113 } 114 } 115 116 void nv::skeletal_animation_entry_gpu::update_skeleton( mat4* data, uint32 time ) const 117 { 118 float tick_time = ( time * 0.001f ) * m_node_data->get_frame_rate(); 119 float anim_time = fmodf( tick_time, m_node_data->get_duration() ); 120 121 if ( !m_node_data->is_flat() ) 122 { 123 animate_rec( data, anim_time, 0, mat4() ); 124 return; 125 } 126 127 for ( uint32 n = 0; n < m_node_data->get_count(); ++n ) 128 if ( m_bone_ids[n] >= 0 ) 129 { 130 const mesh_node_data* node = m_node_data->get_node(n); 131 nv::mat4 node_mat( node->transform ); 132 133 if ( node->data ) 134 { 135 node_mat = node->data->get_matrix( anim_time ); 136 } 137 138 sint16 bone_id = m_bone_ids[n]; 139 data[ bone_id ] = node_mat * m_offsets[ bone_id ]; 140 } 141 } 142 143 void nv::skeletal_animation_entry_gpu::prepare( const mesh_nodes_data* bones ) 144 { 145 if ( m_prepared ) return; 146 std::unordered_map< std::string, nv::uint16 > bone_names; 147 m_offsets = new mat4[ bones->get_count() ]; 148 for ( nv::uint16 bi = 0; bi < bones->get_count(); ++bi ) 149 { 150 const mesh_node_data* bone = bones->get_node(bi); 151 bone_names[ bone->name ] = bi; 152 m_offsets[bi] = bone->transform; 153 } 154 155 for ( uint32 n = 0; n < m_node_data->get_count(); ++n ) 156 { 157 const mesh_node_data* node = m_node_data->get_node(n); 158 sint16 bone_id = -1; 159 160 auto bi = bone_names.find( node->name ); 161 if ( bi != bone_names.end() ) 162 { 163 bone_id = bi->second; 164 } 165 m_bone_ids[n] = bone_id; 166 } 167 m_prepared = true; 168 } 169 170 void nv::skeletal_animation_entry_gpu::animate_rec( mat4* data, float time, uint32 node_id, const mat4& parent_mat ) const 171 { 172 // TODO: fix transforms, which are now embedded, 173 // see note in assimp_loader.cc:load_node 174 const mesh_node_data* node = m_node_data->get_node( node_id ); 175 mat4 node_mat( node->transform ); 176 177 if ( node->data ) 178 { 179 node_mat = node->data->get_matrix( time ); 180 } 181 182 mat4 global_mat = parent_mat * node_mat; 183 184 sint16 bone_id = m_bone_ids[ node_id ]; 185 if ( bone_id >= 0 ) 186 { 187 data[ bone_id ] = global_mat * m_offsets[ bone_id ]; 188 } 189 190 for ( auto child : m_children[ node_id ] ) 191 { 192 animate_rec( data, time, child, global_mat ); 193 } 194 } 195 196 nv::skeletal_animation_entry_gpu::~skeletal_animation_entry_gpu() 197 { 198 delete[] m_offsets; 199 delete[] m_children; 200 delete[] m_bone_ids; 201 } 202 203 nv::skeletal_mesh_gpu::skeletal_mesh_gpu( device* a_device, const mesh_data* a_mesh, const mesh_nodes_data* a_bone_data ) 204 : animated_mesh(), m_bone_data( a_bone_data ), m_transform( nullptr ) 205 { 206 m_va = a_device->create_vertex_array( a_mesh, nv::STATIC_DRAW ); 207 m_index_count = a_mesh->get_count(); 208 if ( m_bone_data ) 209 { 210 m_transform = new mat4[ m_bone_data->get_count() ]; 211 } 70 212 } 71 213 72 214 void nv::skeletal_mesh_gpu::run_animation( animation_entry* a_anim ) 73 215 { 74 if ( m_ primary&& a_anim != nullptr )216 if ( m_bone_data && a_anim != nullptr ) 75 217 { 76 218 skeletal_animation_entry_gpu * anim = (skeletal_animation_entry_gpu*)(a_anim); 77 m_transform.resize( m_model->get_bone_count() ); 78 anim->prepare( m_model ); 219 anim->prepare( m_bone_data ); 79 220 update_animation( a_anim, 0 ); 80 221 } … … 83 224 void nv::skeletal_mesh_gpu::update_animation( animation_entry* a_anim, uint32 a_anim_time ) 84 225 { 85 if ( m_ primary&& a_anim )226 if ( m_bone_data && a_anim ) 86 227 { 87 228 skeletal_animation_entry_gpu * anim = (skeletal_animation_entry_gpu*)a_anim; 88 anim->update_skeleton( m_transform .data(), a_anim_time );229 anim->update_skeleton( m_transform, a_anim_time ); 89 230 } 90 231 } … … 92 233 void nv::skeletal_mesh_gpu::update( program* a_program ) const 93 234 { 94 if (m_primary) 95 a_program->set_uniform_array( "nv_m_bones", m_transform ); 96 } 235 if ( m_bone_data ) 236 a_program->set_uniform_array( "nv_m_bones", m_transform, m_bone_data->get_count() ); 237 } 238 239 nv::transform nv::skeletal_mesh_gpu::get_node_transform( uint32 node_id ) const 240 { 241 return transform( m_transform[ node_id ] ); 242 } 243 244 nv::mat4 nv::skeletal_mesh_gpu::get_node_matrix( uint32 node_id ) const 245 { 246 return m_transform[ node_id ]; 247 }
Note: See TracChangeset
for help on using the changeset viewer.