Index: trunk/src/formats/md3_loader.cc
===================================================================
--- trunk/src/formats/md3_loader.cc	(revision 236)
+++ trunk/src/formats/md3_loader.cc	(revision 239)
@@ -234,5 +234,5 @@
 
 md3_loader::md3_loader()
-	: m_md3( nullptr ), m_size( 0 )
+	: m_md3( nullptr )
 {
 	if ( !s_normal_ready )
@@ -276,5 +276,4 @@
 {
 	m_tags.clear();
-	m_size = 0;
 
 	m_md3 = (void*)(new md3_t);
@@ -285,46 +284,4 @@
 	return true;
 }
-
-/*
-mesh* nv::md3_loader::release_mesh()
-{
-	return get_frame( 0 );
-}
-
-mesh* nv::md3_loader::get_frame( sint32 frame )
-{
-	mesh* m = new mesh();
-	md3_t* md3 = (md3_t*)m_md3;
-
-	NV_LOG( LOG_INFO, "Tags:" );
-	for ( sint32 i = 0; i < md3->header.num_tags; ++i )
-	{
-		const md3_tag_t& rtag = md3->tags[i + md3->header.num_tags * frame];
-
-		md3_tag& tag   = m_tags[ (char*)(rtag.name) ];
-		tag.name       = (char*)(rtag.name);
-		vec4 axisx     = vec4( md3_vec3( rtag.axis[0] ), 0.0 );
-		vec4 axisz     = vec4( md3_vec3( rtag.axis[1] ), 0.0 );
-		vec4 axisy     = vec4( md3_vec3( rtag.axis[2] ), 0.0 );
-		vec4 origin    = vec4( md3_vec3( rtag.origin ),  1.0 );
-		tag.transform  = glm::mat4( axisx, axisy, axisz, origin );
-		NV_LOG( LOG_INFO, "Tag " << tag.name << " found" );
-	}
-
-	vertex_attribute< vec3 >* position = m->add_attribute<vec3>("nv_position");
-	vertex_attribute< vec3 >* normal   = m->add_attribute<vec3>("nv_normal");
-	vertex_attribute< vec2 >* texcoord = m->add_attribute<vec2>("nv_texcoord");
-	vertex_attribute< uint32 >* indices  = m->add_indices<uint32>();
-
-	load_positions( position->get(), frame );
-	load_normals( normal->get(), frame );
-
-	load_texcoords( texcoord->get() );
-	load_indicies( indices->get() );
-
-	m_size = indices->get().size();
-	return m;
-}
-*/
 
 void nv::md3_loader::load_tags( std::vector<transform>& t, const std::string& tag )
@@ -351,95 +308,32 @@
 }
 
-mesh_data_old* nv::md3_loader::release_mesh_data()
-{
-	mesh_data_creator m;
-
-	load_positions( m.get_positions() );
-	load_normals( m.get_normals() );
-	load_texcoords( m.get_texcoords() );
-	load_indicies( m.get_indices() );
-	
-	std::vector< std::string > names;
-	load_tag_names( names );
-
-	for ( auto& name : names )
-	{
-		load_tags( m.get_tag_map()[ name ], name );
-	}
-
-	return m.release();
-}
-
-mesh_data_old* nv::md3_loader::get_frame( sint32 frame )
-{
-	mesh_data_creator m;
-
-	load_positions( m.get_positions(), frame );
-	load_normals( m.get_normals(), frame );
-	load_texcoords( m.get_texcoords() );
-	load_indicies( m.get_indices() );
-
-	std::vector< std::string > names;
-	load_tag_names( names );
-
-	for ( auto& name : names )
-	{
-		load_tags( m.get_tag_map()[ name ], name );
-	}
-	return m.release();
-}
-
-size_t md3_loader::get_max_frames() const
-{
-	return static_cast< size_t >( ((md3_t*)m_md3)->header.num_frames );
-}
-
-void md3_loader::load_tag_names( std::vector< std::string >& tags )
-{
-	tags.clear();
-	md3_t* md3 = (md3_t*)m_md3;
-	for ( sint32 i = 0; i < md3->header.num_tags; ++i )
-	{
-		const md3_tag_t& rtag = md3->tags[i + md3->header.num_tags];
-		tags.push_back( (char*)(rtag.name) );
-	}
-}
-
-transform md3_loader::get_tag( sint32 frame, const std::string& name ) const
-{
-	md3_t* md3 = (md3_t*)m_md3;
-	for ( sint32 i = 0; i < md3->header.num_tags; ++i )
-	{
-		const md3_tag_t& rtag = md3->tags[i + md3->header.num_tags * frame];
-		std::string rname((char*)(rtag.name));
-		if (rname == name)
-		{
-			vec3 axisx ( md3_vec3( rtag.axis[0] ) );
-			vec3 axisz ( md3_vec3( rtag.axis[1] ) );
-			vec3 axisy ( md3_vec3( rtag.axis[2] ) );
-			vec3 origin( md3_vec3( rtag.origin ) );
-			return transform( origin, quat( mat3( axisx, axisy, axisz ) ) );
-		}
-	}
-	return transform();
-}
-
-const md3_tag* md3_loader::get_tag( const std::string& name ) const
-{
-	auto it = m_tags.find( name );
-	if ( it == m_tags.end() ) return nullptr;
-	return &(it->second);
-}
-
-void md3_loader::load_positions( std::vector<vec3>& p, sint32 frame /*=-1*/ )
+struct vtx_md3_pn
+{
+	nv::vec3 position;
+	nv::vec3 normal;
+};
+
+struct vtx_md3_t
+{
+	nv::vec2 texcoord;
+};
+
+mesh_data* nv::md3_loader::release_mesh_data()
+{
+	return release_mesh_data( -1 );
+}
+
+mesh_data* nv::md3_loader::release_mesh_data( sint32 frame )
 {
 	md3_t* md3 = (md3_t*)m_md3;
 	sint32 num_surfaces = md3->header.num_surfaces;
-	p.clear();
+	sint32 num_verts    = md3->vertices_per_frame;
 	sint32 current_frame = ( frame == -1 ? 0 : frame );
 	sint32 frame_count   = ( frame == -1 ? md3->header.num_frames : 1 );
 
-	p.reserve( static_cast< size_t >( md3->vertices_per_frame * frame_count ) );
-
+	mesh_raw_channel* mc_pn = mesh_raw_channel::create< vtx_md3_pn >( num_verts * frame_count );
+	vtx_md3_pn* vtx_pn = (vtx_md3_pn*)mc_pn->data;
+
+	uint32 index = 0;
 	while ( frame_count > 0 )
 	{
@@ -453,80 +347,73 @@
 			{
 				md3_vertex_t& v = surface.vertices[j];
-				p.push_back( vec3( v.x * MD3_XYZ_SCALE, v.z * MD3_XYZ_SCALE, v.y * MD3_XYZ_SCALE ) );
+				vtx_pn[index].position = vec3( v.x * MD3_XYZ_SCALE, v.z * MD3_XYZ_SCALE, v.y * MD3_XYZ_SCALE );
+				vtx_pn[index].normal   = s_normal_cache[ v.normal ];
+				index++;
 			}
+
 		}
 		++current_frame;
 		--frame_count;
 	}
-}
-
-void md3_loader::load_normals( std::vector<vec3>& n, sint32 frame /*=-1*/ )
-{
-	md3_t* md3 = (md3_t*)m_md3;
-	sint32 num_surfaces = md3->header.num_surfaces;
-	n.clear();
-	sint32 current_frame = ( frame == -1 ? 0 : frame );
-	sint32 frame_count   = ( frame == -1 ? md3->header.num_frames : 1 );
-
-	n.reserve( static_cast< size_t >( md3->vertices_per_frame * frame_count ) );
-
-	while ( frame_count > 0 )
-	{
-		for ( sint32 i = 0; i < num_surfaces; ++i )
-		{
-			md3_surface_t& surface = md3->surfaces[i];
-			sint32         vcount  = surface.header.num_verts;
-			sint32         offset  = vcount * current_frame;
-			sint32         limit   = vcount + offset;
-			for (sint32 j = offset; j < limit; ++j )
-			{
-				n.push_back( s_normal_cache[ surface.vertices[j].normal ] );
-			}
-		}
-		++current_frame;
-		--frame_count;
-	}
-}
-
-void md3_loader::load_texcoords( std::vector<vec2>& t )
-{
-	md3_t* md3 = (md3_t*)m_md3;
-	sint32 num_surfaces = md3->header.num_surfaces;
-	t.clear();
+
+	index = 0;
+	mesh_raw_channel* mc_t = mesh_raw_channel::create< vtx_md3_t >( num_verts );
+	vtx_md3_t* vtx_t = (vtx_md3_t*)mc_t->data;
 	for ( sint32 i = 0; i < num_surfaces; ++i )
 	{
 		const md3_surface_t& surface = md3->surfaces[i];
 		const uint32         vcount  = static_cast< uint32 >( surface.header.num_verts );
-		t.reserve( t.size() + vcount );
 		for (uint32 j = 0; j < vcount; ++j )
 		{
-			t.push_back( md3_texcoord( surface.st[j] ) );
+			vtx_t[index++].texcoord = md3_texcoord( surface.st[j] );
 		}
 	}
-}
-
-void md3_loader::load_indicies( std::vector<uint32>& idx )
-{
-	md3_t* md3 = (md3_t*)m_md3;
-	sint32 num_surfaces = md3->header.num_surfaces;
-	idx.clear();
+
+	sint32 index_count = 0;
+	for ( sint32 i = 0; i < num_surfaces; ++i )
+	{
+		index_count += md3->surfaces[i].header.num_triangles * 3;
+	}
+
+	index = 0;
 	sint32 index_base = 0;
+	mesh_raw_index_channel* ic = mesh_raw_index_channel::create< uint16 >( index_count );
+	uint16* icp = (uint16*)ic->data;
 	for ( sint32 i = 0; i < num_surfaces; ++i )
 	{
 		const md3_surface_t& surface = md3->surfaces[i];
 		const size_t         tcount  = static_cast< size_t >( surface.header.num_triangles );
-
-		idx.reserve( idx.size() + tcount * 3 );
 		for (size_t j = 0; j < tcount; ++j )
 		{
 			const md3_triangle_t& t = surface.triangles[j];
-			idx.push_back( static_cast< uint32 >( index_base + t.indexes[0] ) );
-			idx.push_back( static_cast< uint32 >( index_base + t.indexes[1] ) );
-			idx.push_back( static_cast< uint32 >( index_base + t.indexes[2] ) );
+			icp[index++] = static_cast< uint16 >( index_base + t.indexes[0] );
+			icp[index++] = static_cast< uint16 >( index_base + t.indexes[1] );
+			icp[index++] = static_cast< uint16 >( index_base + t.indexes[2] );
 		}
-
 		index_base += surface.header.num_verts;
 	}
 
-}
-
+	mesh_data* result = new mesh_data();
+	result->add_channel( mc_pn );
+	result->add_channel( mc_t );
+	result->set_index_channel( ic );
+	return result;
+}
+
+tag_map* nv::md3_loader::create_tag_map()
+{
+	md3_t* md3 = (md3_t*)m_md3;
+	tag_map* result = new tag_map();
+	for ( sint32 i = 0; i < md3->header.num_tags; ++i )
+	{
+		const md3_tag_t& rtag = md3->tags[i + md3->header.num_tags];
+		std::string name( (char*)(rtag.name) );
+		load_tags( result->get_map()[ name ], name );
+	}
+	return result;
+}
+
+size_t md3_loader::get_max_frames() const
+{
+	return static_cast< size_t >( ((md3_t*)m_md3)->header.num_frames );
+}
