Index: trunk/src/formats/md5_loader.cc
===================================================================
--- trunk/src/formats/md5_loader.cc	(revision 190)
+++ trunk/src/formats/md5_loader.cc	(revision 191)
@@ -15,4 +15,6 @@
 using namespace nv;
 
+// based on http://tfc.duke.free.fr/coding/md5-specs-en.html
+
 static void next_line( std::istream& stream )
 {
@@ -71,5 +73,5 @@
 			discard( sstream, "{" );
 			md5_joint joint;
-			for ( int i = 0; i < m_num_joints; ++i )
+			for ( size_t i = 0; i < m_num_joints; ++i )
 			{
 				sstream >> joint.name >> joint.parent_id;
@@ -286,2 +288,296 @@
 	return m;
 }
+
+md5_animation::md5_animation()
+	: m_md5_version( 0 )
+	, m_num_frames( 0 )
+	, m_num_joints( 0 )
+	, m_frame_rate( 0 )
+	, m_num_animated_components( 0 )
+	, m_anim_duration( 0 )
+	, m_frame_duration( 0 )
+	, m_anim_time( 0 )
+{
+
+}
+
+md5_animation::~md5_animation()
+{
+
+}
+
+bool md5_animation::load_animation( stream& source )
+{
+	m_joint_infos.clear();
+	m_bounds.clear();
+	m_base_frames.clear();
+	m_frames.clear();
+	m_animated_skeleton.joints.clear();
+	m_num_frames = 0;
+
+	std_stream sstream( &source );
+	std::string command;
+
+	sstream >> command;
+	while ( !sstream.eof() )
+	{
+		if ( command == "MD5Version" )
+		{
+			sstream >> m_md5_version;
+			assert( m_md5_version == 10 );
+		}
+		else if ( command == "commandline" )
+		{
+			next_line( sstream ); 
+		}
+		else if ( command == "numFrames" )
+		{
+			sstream >> m_num_frames;
+			next_line( sstream ); 
+		}
+		else if ( command == "numJoints" )
+		{
+			sstream >> m_num_joints;
+			next_line( sstream ); 
+		}
+		else if ( command == "frameRate" )
+		{
+			sstream >> m_frame_rate;
+			next_line( sstream ); 
+		}
+		else if ( command == "numAnimatedComponents" )
+		{
+			sstream >> m_num_animated_components;
+			next_line( sstream ); 
+		}
+		else if ( command == "hierarchy" )
+		{
+			discard( sstream, "{" );
+			for ( int i = 0; i < m_num_joints; ++i )
+			{
+				md5_joint_info joint;
+				sstream >> joint.name >> joint.parent_id >> joint.flags >> joint.start_index;
+				remove_quotes( joint.name );
+				m_joint_infos.push_back( joint );
+				next_line( sstream ); 
+			}
+			discard( sstream, "}" );
+		}
+		else if ( command == "bounds" )
+		{
+			discard( sstream, "{" );
+			next_line( sstream ); 
+			for ( int i = 0; i < m_num_frames; ++i ) 
+			{
+				md5_bound bound;
+				discard( sstream, "(" );
+				sstream >> bound.min.x >> bound.min.y >> bound.min.z;
+				discard( sstream, ")" );
+				discard( sstream, "(" );
+				sstream >> bound.max.x >> bound.max.y >> bound.max.z;
+
+				m_bounds.push_back( bound );
+
+				next_line( sstream ); 
+			}
+
+			discard( sstream, "}" );
+			next_line( sstream ); 
+		}
+		else if ( command == "baseframe" )
+		{
+			discard( sstream, "{" );
+			next_line( sstream ); 
+
+			for ( int i = 0; i < m_num_joints; ++i )
+			{
+				md5_base_frame base_frame;
+				discard( sstream, "(" );
+				sstream >> base_frame.pos.x >> base_frame.pos.y >> base_frame.pos.z;
+				discard( sstream, ")" );
+				discard( sstream, "(" );
+				sstream >> base_frame.orient.x >> base_frame.orient.y >> base_frame.orient.z;
+				next_line( sstream ); 
+
+				m_base_frames.push_back( base_frame );
+			}
+			discard( sstream, "}" );
+			next_line( sstream ); 
+		}
+		else if ( command == "frame" )
+		{
+			md5_frame_data frame;
+			sstream >> frame.frame_id;
+			discard( sstream, "{" );
+			next_line( sstream ); 
+
+			for ( int i = 0; i < m_num_animated_components; ++i )
+			{
+				float frameData;
+				sstream >> frameData;
+				frame.frame_data.push_back(frameData);
+			}
+
+			m_frames.push_back(frame);
+
+			build_frame_skeleton( m_skeletons, m_joint_infos, m_base_frames, frame );
+
+			discard( sstream, "}" );
+			next_line( sstream ); 
+		}
+
+		sstream >> command;
+	} 
+
+	m_animated_skeleton.joints.assign( m_num_joints, md5_skeleton_joint() );
+
+	m_frame_duration = 1.0f / (float)m_frame_rate;
+	m_anim_duration = ( m_frame_duration * (float)m_num_frames );
+	m_anim_time = 0.0f;
+
+	assert( m_joint_infos.size() == (size_t)m_num_joints );
+	assert( m_bounds.size()      == (size_t)m_num_frames );
+	assert( m_base_frames.size() == (size_t)m_num_joints );
+	assert( m_frames.size()      == (size_t)m_num_frames );
+	assert( m_skeletons.size()   == (size_t)m_num_frames );
+
+	return true;
+}
+
+void md5_animation::update( float delta_time )
+{
+	if ( m_num_frames < 1 ) return;
+
+	m_anim_time += delta_time;
+
+	while ( m_anim_time > m_anim_duration ) m_anim_time -= m_anim_duration;
+	while ( m_anim_time < 0.0f ) m_anim_time += m_anim_duration;
+
+	float frame_num = m_anim_time * (float)m_frame_rate;
+	int frame0 = (int)floorf( frame_num );
+	int frame1 = (int)ceilf( frame_num );
+	frame0 = frame0 % m_num_frames;
+	frame1 = frame1 % m_num_frames;
+
+	float interpolate = fmodf( m_anim_time, m_frame_duration ) / m_frame_duration;
+
+	interpolate_skeletons( m_animated_skeleton, m_skeletons[frame0], m_skeletons[frame1], interpolate );
+}
+
+void md5_animation::build_frame_skeleton( md5_frame_skeleton_list& skeletons, const md5_joint_info_list& joint_infos, const md5_base_frame_list& base_frames, const md5_frame_data& frame_data )
+{
+	md5_frame_skeleton skeleton;
+
+	for ( unsigned int i = 0; i < joint_infos.size(); ++i )
+	{
+		unsigned int j = 0;
+
+		const md5_joint_info& jinfo = joint_infos[i];
+		md5_skeleton_joint animated_joint = base_frames[i];
+
+		animated_joint.parent = jinfo.parent_id;
+
+		if ( jinfo.flags & 1 )  animated_joint.pos.x    = frame_data.frame_data[ jinfo.start_index + j++ ];
+		if ( jinfo.flags & 2 )  animated_joint.pos.y    = frame_data.frame_data[ jinfo.start_index + j++ ];
+		if ( jinfo.flags & 4 )  animated_joint.pos.z    = frame_data.frame_data[ jinfo.start_index + j++ ];
+		if ( jinfo.flags & 8 )  animated_joint.orient.x = frame_data.frame_data[ jinfo.start_index + j++ ];
+		if ( jinfo.flags & 16 ) animated_joint.orient.y = frame_data.frame_data[ jinfo.start_index + j++ ];
+		if ( jinfo.flags & 32 )	animated_joint.orient.z = frame_data.frame_data[ jinfo.start_index + j++ ];
+
+		unit_quat_w( animated_joint.orient );
+
+		if ( animated_joint.parent >= 0 ) // Has a parent joint
+		{
+			md5_skeleton_joint& pjoint = skeleton.joints[animated_joint.parent];
+			glm::vec3 rot_pos = pjoint.orient * animated_joint.pos;
+
+			animated_joint.pos    = pjoint.pos + rot_pos;
+			animated_joint.orient = pjoint.orient * animated_joint.orient;
+
+			animated_joint.orient = glm::normalize( animated_joint.orient );
+		}
+
+		skeleton.joints.push_back( animated_joint );
+	}
+
+	skeletons.push_back( skeleton );
+}
+
+void md5_animation::interpolate_skeletons( md5_frame_skeleton& final_skeleton, const md5_frame_skeleton& skeleton0, const md5_frame_skeleton& skeleton1, float interpolate )
+{
+	for ( int i = 0; i < m_num_joints; ++i )
+	{
+		md5_skeleton_joint& final_joint = final_skeleton.joints[i];
+		const md5_skeleton_joint& joint0 = skeleton0.joints[i]; 
+		const md5_skeleton_joint& joint1 = skeleton1.joints[i];
+
+		final_joint.parent = joint0.parent;
+
+		final_joint.pos    = glm::mix( joint0.pos, joint1.pos, interpolate );
+		final_joint.orient = glm::mix( joint0.orient, joint1.orient, interpolate );
+	}
+}
+
+bool md5_loader::check_animation( const md5_animation& animation ) const
+{
+	if ( m_num_joints != animation.get_num_joints() )
+	{
+		return false;
+	}
+
+	for ( uint32 i = 0; i < m_joints.size(); ++i )
+	{
+		const md5_joint& mjoint = m_joints[i];
+		const md5_animation::md5_joint_info& ajoint = animation.get_joint_info( i );
+
+		if ( mjoint.name != ajoint.name || mjoint.parent_id != ajoint.parent_id )
+		{
+			return false;
+		}
+	}
+
+	return true;
+}
+
+bool md5_loader::prepare_animated_mesh( md5_mesh& mesh, const md5_animation::md5_frame_skeleton& skel )
+{
+	for ( unsigned int i = 0; i < mesh.verts.size(); ++i )
+	{
+		const md5_vertex& vert = mesh.verts[i];
+		glm::vec3& pos     = mesh.position_buffer[i];
+		glm::vec3& normal  = mesh.normal_buffer[i];
+		glm::vec3& tangent = mesh.tangent_buffer[i];
+
+		pos     = glm::vec3(0);
+		normal  = glm::vec3(0);
+		tangent = glm::vec3(0);
+
+		for ( int j = 0; j < vert.weight_count; ++j )
+		{
+			const md5_weight& weight = mesh.weights[vert.start_weight + j];
+			const md5_animation::md5_skeleton_joint& joint = skel.joints[weight.joint_id];
+
+			glm::vec3 rot_pos = joint.orient * weight.pos;
+			pos += ( joint.pos + rot_pos ) * weight.bias;
+
+			normal  += ( joint.orient * vert.normal  ) * weight.bias;
+			tangent += ( joint.orient * vert.tangent ) * weight.bias;
+		}
+	}
+	return true;
+}
+
+void md5_loader::apply( const md5_animation& animation )
+{
+	const md5_animation::md5_frame_skeleton& skeleton = animation.get_skeleton();
+
+	for ( unsigned int i = 0; i < m_meshes.size(); ++i )
+	{
+		prepare_animated_mesh( m_meshes[i], skeleton );
+	}
+}
+
+size_t nv::md5_loader::get_size()
+{
+	return m_size;
+}
Index: trunk/src/gl/gl_vertex_buffer.cc
===================================================================
--- trunk/src/gl/gl_vertex_buffer.cc	(revision 190)
+++ trunk/src/gl/gl_vertex_buffer.cc	(revision 191)
@@ -20,4 +20,5 @@
 void gl_vertex_buffer::update( const void* data, size_t offset, size_t size )
 {
+	// IMPORTANT - THIS DOES NOT BIND, SHOULD IT?
 	glBufferSubData( GL_ARRAY_BUFFER, (GLintptr)offset, (GLsizeiptr)size, data );
 }
