Index: trunk/src/gfx/mesh_creator.cc
===================================================================
--- trunk/src/gfx/mesh_creator.cc	(revision 412)
+++ trunk/src/gfx/mesh_creator.cc	(revision 413)
@@ -24,6 +24,7 @@
 		if ( pkeys && pkeys->get_channel_count() > 0 && keys && keys->get_channel_count() > 0 )
 		{
-			nv_key_transform*  channel = reinterpret_cast<nv_key_transform*>(keys->get_channel(0)->data);
-			nv_key_transform* pchannel = reinterpret_cast<nv_key_transform*>(pkeys->get_channel(0)->data);
+			data_channel_creator< nv_key_transform > channel_creator( const_cast< raw_data_channel* >( keys->get_channel( 0 ) ) );
+			nv_key_transform* channel = channel_creator.data();
+			const nv_key_transform* pchannel = pkeys->get_channel(0)->data_cast< nv_key_transform >();
 			for ( unsigned n = 0; n < count; ++n )
 			{
@@ -66,8 +67,6 @@
 			}
 
-			raw_data_channel* raw_channel = raw_data_channel::create<nv_key_transform>( max_keys );
+			data_channel_creator< nv_key_transform > kt_channel( max_keys );
 			key_data* new_keys = new key_data;
-			new_keys->add_channel( raw_channel );
-			nv_key_transform* channel = reinterpret_cast<nv_key_transform*>(raw_channel->data);
 			data_descriptor final_key = old_keys->get_final_key();
 
@@ -82,8 +81,9 @@
 					pkey += old_keys->get_raw( old_keys->get_channel(c), idx, pkey );
 				}
-				channel[n].tform = extract_transform_raw( final_key, key );
+				kt_channel.data()[n].tform = extract_transform_raw( final_key, key );
 			}
 
 			delete old_keys;
+			new_keys->add_channel( kt_channel.release() );
 			m_data->m_nodes[i].data = new_keys;
 		}
@@ -106,9 +106,9 @@
 			for ( size_t c = 0; c < kdata->get_channel_count(); ++c )
 			{
-				const raw_data_channel* channel = kdata->get_channel(c);
-				size_t key_size = channel->element_size();
-				for ( size_t n = 0; n < channel->element_count(); ++n )
-				{
-					transform_key_raw( channel->descriptor(), channel->data + n * key_size, scale, r33, ri33 );
+				raw_data_channel_creator channel( const_cast< raw_data_channel* >( kdata->get_channel( c ) ) );
+				size_t key_size = channel.element_size();
+				for ( size_t n = 0; n < channel.size(); ++n )
+				{
+					transform_key_raw( kdata->get_channel( c )->descriptor(), channel.raw_data() + n * key_size, scale, r33, ri33 );
 				}
 			}
@@ -125,7 +125,7 @@
 	for ( uint32 c = 0; c < m_data->get_channel_count(); ++c )
 	{
-		const raw_data_channel* channel = m_data->get_channel(c);
-		const data_descriptor&  desc    = channel->descriptor();
-		uint8* raw_data = channel->data;
+		raw_data_channel_creator channel( m_data->m_channels[ c ] );
+		const data_descriptor&  desc    = channel.descriptor();
+		uint8* raw_data = channel.raw_data();
 		uint32 vtx_size = desc.element_size();
 		int p_offset = -1;
@@ -142,5 +142,5 @@
 
 		if ( p_offset != -1 )
-			for ( uint32 i = 0; i < channel->element_count(); i++)
+			for ( uint32 i = 0; i < channel.size(); i++)
 			{
 				vec3& p = *reinterpret_cast<vec3*>( raw_data + vtx_size*i + p_offset );
@@ -149,5 +149,5 @@
 
 		if ( n_offset != -1 )
-			for ( uint32 i = 0; i < channel->element_count(); i++)
+			for ( uint32 i = 0; i < channel.size(); i++)
 			{
 				vec3& n = *reinterpret_cast<vec3*>( raw_data + vtx_size*i + n_offset );
@@ -155,5 +155,5 @@
 			}
 		if ( t_offset != -1 )
-			for ( uint32 i = 0; i < channel->element_count(); i++)
+			for ( uint32 i = 0; i < channel.size(); i++)
 			{
 				vec4& t = *reinterpret_cast<vec4*>(raw_data + vtx_size*i + t_offset );
@@ -173,6 +173,6 @@
 	size_t n_offset = 0;
 	if ( ch_n == -1 ) return;
-	raw_data_channel* channel = m_data->m_channels[ unsigned( ch_n ) ];
-	for ( const auto& cslot : channel->descriptor() )
+	raw_data_channel_creator channel( m_data->m_channels[ unsigned( ch_n ) ] );
+	for ( const auto& cslot : channel.descriptor() )
 		if ( cslot.vslot == slot::NORMAL )
 		{
@@ -180,7 +180,7 @@
 		}
 
-	for ( uint32 i = 0; i < channel->element_count(); ++i )
-	{
-		vec3& normal = *reinterpret_cast<vec3*>( channel->data + channel->element_size() * i + n_offset );
+	for ( uint32 i = 0; i < channel.size(); ++i )
+	{
+		vec3& normal = *reinterpret_cast<vec3*>( channel.raw_data() + channel.element_size() * i + n_offset );
 		normal = -normal;
 	}
@@ -249,6 +249,6 @@
 	}
 
-	raw_data_channel* g_channel = raw_data_channel::create<vertex_g>( p_channel->element_count() );
-	vec4* tangents              = reinterpret_cast<vec4*>( g_channel->data );
+	data_channel_creator< vertex_g > g_channel( p_channel->element_count() );
+	vec4* tangents              = &( g_channel.data()[0].tangent );
 	vec3* tangents2             = new vec3[ p_channel->element_count() ];
 	uint32 tri_count = i_channel ? i_channel->element_count() / 3 : t_channel->element_count() / 3;
@@ -263,5 +263,5 @@
 		if ( i_type == UINT )
 		{
-			const uint32* idata = reinterpret_cast<const uint32*>( i_channel->data );
+			const uint32* idata = reinterpret_cast<const uint32*>( i_channel->raw_data() );
 			ti0 = idata[ i * 3 ];
 			ti1 = idata[ i * 3 + 1 ];
@@ -270,5 +270,5 @@
 		else if ( i_type == USHORT )
 		{
-			const uint16* idata = reinterpret_cast<const uint16*>( i_channel->data );
+			const uint16* idata = reinterpret_cast<const uint16*>( i_channel->raw_data() );
 			ti0 = idata[ i * 3 ];
 			ti1 = idata[ i * 3 + 1 ];
@@ -282,7 +282,7 @@
 		}
 
-		const vec2& w1 = *reinterpret_cast<vec2*>(t_channel->data + t_channel->element_size()*ti0 + t_offset );
-		const vec2& w2 = *reinterpret_cast<vec2*>(t_channel->data + t_channel->element_size()*ti1 + t_offset );
-		const vec2& w3 = *reinterpret_cast<vec2*>(t_channel->data + t_channel->element_size()*ti2 + t_offset );
+		const vec2& w1 = *reinterpret_cast<const vec2*>(t_channel->raw_data() + t_channel->element_size()*ti0 + t_offset );
+		const vec2& w2 = *reinterpret_cast<const vec2*>(t_channel->raw_data() + t_channel->element_size()*ti1 + t_offset );
+		const vec2& w3 = *reinterpret_cast<const vec2*>(t_channel->raw_data() + t_channel->element_size()*ti2 + t_offset );
 		vec2 st1 = w3 - w1;
 		vec2 st2 = w2 - w1;
@@ -295,7 +295,7 @@
 			uint32 nti1 = t_channel->element_count() * set + ti1;
 			uint32 nti2 = t_channel->element_count() * set + ti2;
-			vec3 v1 = *reinterpret_cast<vec3*>(p_channel->data + p_channel->element_size()*nti0 + p_offset );
-			vec3 v2 = *reinterpret_cast<vec3*>(p_channel->data + p_channel->element_size()*nti1 + p_offset );
-			vec3 v3 = *reinterpret_cast<vec3*>(p_channel->data + p_channel->element_size()*nti2 + p_offset );
+			const vec3& v1 = *reinterpret_cast<const vec3*>(p_channel->raw_data() + p_channel->element_size()*nti0 + p_offset );
+			const vec3& v2 = *reinterpret_cast<const vec3*>(p_channel->raw_data() + p_channel->element_size()*nti1 + p_offset );
+			const vec3& v3 = *reinterpret_cast<const vec3*>(p_channel->raw_data() + p_channel->element_size()*nti2 + p_offset );
 			vec3 xyz1 = v3 - v1;
 			vec3 xyz2 = v2 - v1;
@@ -321,5 +321,5 @@
 	for ( unsigned int i = 0; i < vtx_count; ++i )
 	{
-		const vec3 n = *reinterpret_cast<vec3*>( n_channel->data + n_channel->element_size()*i + n_offset );
+		const vec3 n = *reinterpret_cast<const vec3*>( n_channel->raw_data() + n_channel->element_size()*i + n_offset );
 		const vec3 t = vec3(tangents[i]);
 		if ( ! ( t.x == 0.0f && t.y == 0.0f && t.z == 0.0f ) )
@@ -331,7 +331,6 @@
 	delete tangents2;
 
-	m_data->m_channels[ n_channel_index ] = merge_channels( n_channel, g_channel );
+	m_data->m_channels[ n_channel_index ] = merge_channels( n_channel, g_channel.channel() );
 	delete n_channel;
-	delete g_channel;
 }
 
@@ -342,16 +341,12 @@
 	desc.append( b->descriptor() );
 
-	uint8* data = new uint8[a->element_count() * desc.element_size() ];
+	raw_data_channel_creator result( desc, a->element_count() );
 	for ( uint32 i = 0; i < a->element_count(); ++i )
 	{
-		raw_copy_n( a->data + i * a->element_size(), a->element_size(), data + i*desc.element_size() );
-		raw_copy_n( b->data + i * b->element_size(), b->element_size(), data + i*desc.element_size() + a->element_size() );
-	}
-
-	raw_data_channel* result = new raw_data_channel;
-	result->m_count = a->element_count();
-	result->m_desc  = desc;
-	result->data  = data;
-	return result;
+		raw_copy_n( a->raw_data() + i * a->element_size(), a->element_size(), result.raw_data() + i*desc.element_size() );
+		raw_copy_n( b->raw_data() + i * b->element_size(), b->element_size(), result.raw_data() + i*desc.element_size() + a->element_size() );
+	}
+
+	return result.release();
 }
 
@@ -363,12 +358,11 @@
 	size_t vtx_size = a->element_size();
 
-	uint8* data = new uint8[ ( a->element_count() + b->element_count() ) * vtx_size ];
-	
+	raw_data_channel_creator result( a->descriptor(), a->element_count() + b->element_count() );
 
 	if ( frame_count == 1 )
 	{
 		size_t a_size = vtx_size * a->element_count();
-		raw_copy_n( a->data, a_size, data );
-		raw_copy_n( b->data, vtx_size * b->element_count(), data + a_size );
+		raw_copy_n( a->raw_data(), a_size, result.raw_data() );
+		raw_copy_n( b->raw_data(), vtx_size * b->element_count(), result.raw_data() + a_size );
 	}
 	else
@@ -381,6 +375,6 @@
 		for ( size_t i = 0; i < frame_count; ++i )
 		{
-			raw_copy_n( a->data + pos_a, frame_size_a, data + pos );
-			raw_copy_n( b->data + pos_b, frame_size_b, data + pos + frame_size_a );				pos_a += frame_size_a;
+			raw_copy_n( a->raw_data() + pos_a, frame_size_a, result.raw_data() + pos );
+			raw_copy_n( b->raw_data() + pos_b, frame_size_b, result.raw_data() + pos + frame_size_a );				pos_a += frame_size_a;
 			pos_b += frame_size_b; 
 			pos   += frame_size_a + frame_size_b;
@@ -388,9 +382,5 @@
 	}
 
-	raw_data_channel* result = new raw_data_channel;
-	result->m_count = a->element_count() + b->element_count();
-	result->m_desc  = a->descriptor();
-	result->data  = data;
-	return result;
+	return result.release();
 }
 
@@ -437,6 +427,7 @@
 				{
 					NV_ASSERT( size + osize < uint16(-1), "Index out of range!" );
-					uint16* indexes = reinterpret_cast<uint16*>( m_data->m_channels[c]->data );
-					for ( uint16 i = uint16( old->element_count() ); i < m_data->m_channels[c]->element_count(); ++i )
+					raw_data_channel_creator ic( m_data->m_channels[c] );
+					uint16* indexes = reinterpret_cast<uint16*>( ic.raw_data() );
+					for ( uint16 i = uint16( old->element_count() ); i < ic.size(); ++i )
 						indexes[i] += uint16( size );
 
@@ -445,6 +436,7 @@
 			case UINT   : 
 				{
-					uint32* indexes = reinterpret_cast<uint32*>( m_data->m_channels[c]->data );
-					for ( uint32 i = old->element_count(); i < m_data->m_channels[c]->element_count(); ++i )
+					raw_data_channel_creator ic( m_data->m_channels[c] );
+					uint32* indexes = reinterpret_cast<uint32*>( ic.raw_data() );
+					for ( uint32 i = old->element_count(); i < ic.size(); ++i )
 						indexes[i] += size;
 				}
