Index: trunk/tests/md2_test/md2_test.cc
===================================================================
--- trunk/tests/md2_test/md2_test.cc	(revision 231)
+++ trunk/tests/md2_test/md2_test.cc	(revision 239)
@@ -1,4 +1,3 @@
 #include <nv/common.hh>
-#include <iomanip>
 #include <nv/gfx/keyframed_mesh.hh>
 #include <nv/interface/vertex_buffer.hh>
@@ -18,61 +17,5 @@
 #include <nv/time.hh>
 #include <nv/string.hh>
-#include <nv/interface/mesh.hh>
 #include <glm/gtx/rotate_vector.hpp>
-#include <glm/gtc/matrix_access.hpp>
-#include <glm/gtx/matrix_interpolation.hpp>
-
-class mesh_part
-{
-public:
-	mesh_part( const std::string& path, nv::program* program, nv::window* window )
-	{
-		NV_PROFILE("mesh_construct");
-		nv::md2_loader* loader;
-		{
-			NV_PROFILE("loader->load");
-			nv::c_file_system fs;
-			nv::stream* mesh_file = fs.open( path.c_str() );
-			loader = new nv::md2_loader();
-			loader->load( *mesh_file );
-			m_mesh_data = loader->release_mesh_data();
-			delete mesh_file;
-			delete loader;
-		}
-
-		{
-			NV_PROFILE("create_mesh");
-			m_mesh      = new nv::keyframed_mesh_gpu( window->get_context(), m_mesh_data, program );
-		}
-
-	}
-
-	void setup_animation( nv::uint32 start, nv::uint32 stop, nv::uint32 fps, bool loop )
-	{
-		m_mesh->setup_animation( start, stop, fps, loop );
-	}
-
-	void update( nv::uint32 ms )
-	{
-		m_mesh->update( ms );
-	}
-
-	nv::mesh_interface* get_mesh() { return m_mesh; }
-
-	size_t get_max_frames()
-	{
-		return m_mesh->get_max_frames();
-	}
-
-	~mesh_part()
-	{
-		delete m_mesh_data;
-		delete m_mesh;
-	}
-
-private:
-	nv::mesh_data*       m_mesh_data;
-	nv::keyframed_mesh*  m_mesh;
-};
 
 class application
@@ -87,9 +30,12 @@
 	nv::window*       m_window;
 	nv::texture2d*    m_diffuse;
+	nv::program*      m_program;
+
 	nv::clear_state   m_clear_state;
 	nv::render_state  m_render_state;
+	nv::scene_state   m_scene_state;
 
-	mesh_part*   m_mesh;
-	nv::program* m_program;
+	nv::mesh_data*      m_mesh_data;
+	nv::keyframed_mesh* m_mesh;
 };
 
@@ -119,5 +65,22 @@
 	NV_PROFILE( "app_initialize" );
 	m_program = m_device->create_program( nv::slurp( "obj.vert" ), nv::slurp( "obj.frag" ) );
-	m_mesh    = new mesh_part( "data/manc.md2", m_program, m_window );
+
+	nv::md2_loader* loader;
+	{
+		NV_PROFILE("loader->load");
+		nv::c_file_system fs;
+		nv::stream* mesh_file = fs.open( "data/manc.md2" );
+		loader = new nv::md2_loader();
+		loader->load( *mesh_file );
+		m_mesh_data = loader->release_mesh_data();
+		delete mesh_file;
+		delete loader;
+	}
+
+	{
+		NV_PROFILE("create_mesh");
+		m_mesh      = new nv::keyframed_mesh_gpu( m_window->get_context(), m_mesh_data, nullptr, m_program );
+	}
+
 	return true;
 }
@@ -128,6 +91,4 @@
 	NV_PROFILE( "app_run" );
 	int keypress = 0;
-
-	glm::vec3 move( 0, 25.f, 0 );
 
 	nv::uint32 ticks   = m_device->get_ticks();
@@ -142,68 +103,40 @@
 		ticks      = m_device->get_ticks();
 		nv::uint32 elapsed = ticks - last_ticks;
+		m_window->get_context()->clear( m_clear_state );
 		m_mesh->update( elapsed );
-		{
-			NV_PROFILE( "clear" );
-			m_window->get_context()->clear( m_clear_state );
-		}
+		glm::vec3 eye = glm::rotate( glm::vec3( 100.0f, 25.0f, 0.0f ), (ticks / 20.f), glm::vec3( 0.0,1.0,0.0 ) );
 
-		glm::mat4 view;
-		glm::mat4 projection;
-		{
-			NV_PROFILE( "update_sh" );
+		m_scene_state.set_model( nv::mat4(1.0f) );
+		m_scene_state.get_camera().set_lookat(eye, glm::vec3(0.0f, 25.0f, 0.0f), glm::vec3(0.0, 1.0, 0.0));
+		m_scene_state.get_camera().set_perspective(60.0f, 1.0f*800.0f/600.0f, 0.1f, 1000.0f);
 
-			glm::vec3 source( 100.0f, 0.0f, 0.0f );
-			glm::vec3 eye = glm::rotate( source, (ticks / 20.f), glm::vec3( 0.0,1.0,0.0 ) );
+		m_diffuse->bind( 0 );
+		m_program->set_opt_uniform( "light_position", glm::vec3(120.0, 120.0, 0) );
+		m_program->set_opt_uniform( "light_diffuse",  glm::vec4(1.0,1.0,1.0,1.0) );
+		m_program->set_opt_uniform( "light_specular", glm::vec4(1.0,1.0,1.0,1.0) );
+		m_mesh->update( m_program );
+		m_window->get_context()->draw( m_render_state, m_scene_state, m_program, m_mesh );
+		m_window->swap_buffers();
 
-			view       = glm::lookAt(eye + move, glm::vec3(0.0f, 0.0f, 0.0f) + move, glm::vec3(0.0, 1.0, 0.0));
-			projection = glm::perspective(60.0f, 1.0f*800.0f/600.0f, 0.1f, 1000.0f);
-
-			m_diffuse->bind( 0 );
-			m_program->set_opt_uniform( "nv_m_projection", projection );
-			m_program->set_uniform( "light_position", glm::vec3(120.0, 120.0, 0) );
-			m_program->set_uniform( "light_diffuse",  glm::vec4(1.0,1.0,1.0,1.0) );
-			m_program->set_uniform( "light_specular", glm::vec4(1.0,1.0,1.0,1.0) );
-			m_program->set_uniform( "diffuse", 0 );
-		}
-
-		{
-			NV_PROFILE( "draw" );
-			glm::mat4 model      = glm::mat4(1.0f);
-			glm::mat4 mv = view * model;
-			m_program->set_opt_uniform( "nv_m_modelview", mv );
-			m_program->set_opt_uniform( "nv_m_normal", glm::transpose(glm::inverse(glm::mat3(mv))) );
-			m_program->set_uniform( "matrix_mvp", projection * mv );
-			m_mesh->get_mesh()->update( m_program );
-			m_window->get_context()->draw( m_render_state, m_program, m_mesh->get_mesh() );
-		}
-
-		{
-			NV_PROFILE( "swap" );
-			m_window->swap_buffers();
-		}
-
-		{
-			NV_PROFILE( "pollevent" );
-			nv::io_event event;
-			while(m_window->poll_event(event)) 
-			{      
-				switch (event.type) 
+		nv::io_event event;
+		while(m_window->poll_event(event)) 
+		{      
+			switch (event.type) 
+			{
+			case nv::EV_QUIT:
+				keypress = 1;
+				break;
+			case nv::EV_KEY:
+				if (event.key.pressed)
 				{
-				case nv::EV_QUIT:
-					keypress = 1;
-					break;
-				case nv::EV_KEY:
-					if (event.key.pressed)
+					switch (event.key.code) 
 					{
-						switch (event.key.code) 
-						{
-						case nv::KEY_ESCAPE : keypress = 1; break;
-						case nv::KEY_F1 : nv::profiler::pointer()->log_report(); break;
-						default: break;
-						}
+					case nv::KEY_ESCAPE : keypress = 1; break;
+					case nv::KEY_F1 : nv::profiler::pointer()->log_report(); break;
+					default: break;
 					}
-					break;
-				default: break;
 				}
+				break;
+			default: break;
 			}
 		}
@@ -216,4 +149,5 @@
 {
 	delete m_program;
+	delete m_mesh_data;
 	delete m_mesh;
 	delete m_diffuse;
