Ignore:
Timestamp:
05/17/14 02:35:19 (11 years ago)
Author:
epyon
Message:
  • massive update of mesh handling
  • universal mesh handling routines
  • removed a lot of legacy code
  • significantly streamlined MD5 loading
  • all tests updated to new features
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/md5_test/md5_test.cc

    r231 r239  
    1818#include <nv/time.hh>
    1919#include <nv/string.hh>
    20 #include <nv/interface/mesh.hh>
    2120#include <nv/gfx/skeletal_mesh.hh>
    2221#include <glm/gtx/rotate_vector.hpp>
    2322#include <glm/gtc/matrix_access.hpp>
    2423#include <glm/gtx/matrix_interpolation.hpp>
    25 
    26 class mesh_part
    27 {
    28 public:
    29         mesh_part( const std::string& path, nv::program* program, nv::window* window )
    30                 : m_mesh( nullptr ), m_program( program ), m_loader( nullptr ), m_animation( nullptr ), m_window( window )
    31         {
    32                
    33                 NV_PROFILE("mesh_construct");
    34                 {
    35                         NV_PROFILE("loader->load");
    36                         nv::c_file_system fs;
    37                         nv::stream* mesh_file = fs.open( path.c_str() );
    38                         m_loader = new nv::md5_loader();
    39                         m_loader->load( *mesh_file );
    40                         delete mesh_file;
    41                 }
    42 
    43                 {
    44                         NV_PROFILE("create_mesh");
    45                         m_mesh = new nv::skeletal_mesh( window->get_context(), m_loader );
    46                 }
    47 
    48         }
    49 
    50         void load_animation( const std::string& path )
    51         {
    52                 delete m_animation;
    53                 m_animation = nullptr;
    54                 NV_PROFILE("load_animation");
    55                 nv::c_file_system fs;
    56                 nv::stream* anim_file = fs.open( path.c_str() );
    57 
    58                 if ( anim_file != nullptr )
    59                 {
    60                         m_animation = new nv::md5_animation();
    61                         if ( !m_animation->load_animation(*anim_file) )
    62                         {
    63                                 delete m_animation;
    64                                 m_animation = nullptr;
    65                         }
    66                         m_mesh->setup_animation( m_animation );
    67                         delete anim_file;
    68                 }
    69         }
    70 
    71         void update( nv::uint32 ms )
    72         {
    73                 m_mesh->update( ms );
    74         }
    75 
    76         void draw( nv::context* context, nv::render_state& rstate, const glm::mat4& m, const glm::mat4& v, const glm::mat4& p )
    77         {
    78                 NV_PROFILE( "mesh-draw" );
    79                 glm::mat4 mv = v * m;
    80                 m_program->set_opt_uniform( "nv_m_model", m );
    81                 m_program->set_opt_uniform( "nv_m_modelview", mv );
    82                 m_program->set_opt_uniform( "nv_m_view_inv", glm::inverse( v ) );
    83                 m_program->set_opt_uniform( "nv_m_normal", glm::transpose(glm::inverse(glm::mat3(mv))) );
    84                 m_program->set_uniform( "matrix_mvp", p * mv );
    85                 context->draw( rstate, m_program, m_mesh );
    86         }
    87 
    88         ~mesh_part()
    89         {
    90                 delete m_mesh;
    91         }
    92 
    93 private:
    94         nv::skeletal_mesh* m_mesh;
    95         nv::program*       m_program;
    96         nv::md5_loader*    m_loader;
    97         nv::md5_animation* m_animation;
    98         nv::window*        m_window;
    99 };
    10024
    10125class application
     
    10731        ~application();
    10832protected:
     33        void load_animation( const std::string& path );
     34protected:
     35
     36
    10937        nv::device*       m_device;
    11038        nv::window*       m_window;
     
    11442        nv::clear_state   m_clear_state;
    11543        nv::render_state  m_render_state;
    116 
    117         mesh_part*   m_mesh;
    118         nv::program* m_program;
     44        nv::scene_state   m_scene_state;
     45
     46        nv::skeletal_mesh* m_mesh;
     47        nv::program*       m_program;
     48        nv::md5_mesh_data* m_mesh_data;
     49        nv::md5_animation* m_animation;
     50
    11951};
    12052
     
    12456        m_device = new nv::gl_device();
    12557        m_window = m_device->create_window( 800, 600, false );
     58        m_animation = nullptr;
    12659
    12760        nv::sampler sampler( nv::sampler::LINEAR, nv::sampler::REPEAT );
     
    15285        NV_PROFILE( "app_initialize" );
    15386        m_program = m_device->create_program( nv::slurp( "md5.vert" ), nv::slurp( "md5.frag" ) );
    154         m_mesh    = new mesh_part( "data/qshambler.md5mesh", m_program, m_window );
    155         m_mesh->load_animation( "data/idle02.md5anim" );
     87
     88        nv::md5_loader* loader = nullptr;
     89        {
     90                NV_PROFILE("loader->load");
     91                nv::c_file_system fs;
     92                nv::stream* mesh_file = fs.open( "data/qshambler.md5mesh" );
     93                loader = new nv::md5_loader();
     94                loader->load( *mesh_file );
     95                delete mesh_file;
     96        }
     97
     98        {
     99                NV_PROFILE("create_mesh");
     100                m_mesh_data = (nv::md5_mesh_data*)loader->release_mesh_data();
     101                m_mesh = new nv::skeletal_mesh( m_window->get_context(), m_mesh_data );
     102                delete loader;
     103        }
     104
     105        load_animation( "data/idle02.md5anim" );
    156106        return true;
    157107}
     
    182132                }
    183133
    184                 glm::mat4 view;
    185                 glm::mat4 projection;
    186134                {
    187135                        NV_PROFILE( "update_sh" );
     
    190138                        glm::vec3 eye = glm::rotate( source, (ticks / 20.f), glm::vec3( 0.0,1.0,0.0 ) );
    191139
    192                         view       = glm::lookAt(eye + move, glm::vec3(0.0f, 0.0f, 0.0f) + move, glm::vec3(0.0, 1.0, 0.0));
    193                         projection = glm::perspective(60.0f, 1.0f*800.0f/600.0f, 0.1f, 1000.0f);
     140                        m_scene_state.get_camera().set_lookat(eye + move, glm::vec3(0.0f, 0.0f, 0.0f) + move, glm::vec3(0.0, 1.0, 0.0));
     141                        m_scene_state.get_camera().set_perspective(60.0f, 1.0f*800.0f/600.0f, 0.1f, 1000.0f);
    194142
    195143                        m_diffuse ->bind( 0 );
    196144                        m_specular->bind( 1 );
    197145                        m_normal  ->bind( 2 );
    198                         m_program->set_opt_uniform( "nv_m_projection", projection );
    199                         m_program->set_uniform( "light_position", glm::vec3(180.0, 180.0, 0) );
    200                         m_program->set_uniform( "light_diffuse",  glm::vec4(0.7,0.7,0.7,1.0) );
    201                         m_program->set_uniform( "light_specular", glm::vec4(1.0,1.0,1.0,1.0) );
    202                         m_program->set_uniform( "diffuse", 0 );
    203                         m_program->set_uniform( "specular", 1 );
    204                         m_program->set_uniform( "normalmap", 2 );
     146                        m_program->set_opt_uniform( "light_position", glm::vec3(180.0, 180.0, 0) );
     147                        m_program->set_opt_uniform( "light_diffuse",  glm::vec4(0.7,0.7,0.7,1.0) );
     148                        m_program->set_opt_uniform( "light_specular", glm::vec4(1.0,1.0,1.0,1.0) );
    205149                }
    206150
    207151                {
    208152                        NV_PROFILE( "draw" );
    209                         glm::mat4 model      =
    210                                 glm::mat4(1.f,0.f,0.f,0.f,
    211                                           0.f,0.f,1.f,0.f,
    212                                                   0.f,1.f,0.f,0.f,
    213                                                   0.f,0.f,0.f,1.f);
    214 
    215                         m_mesh->draw( m_window->get_context(), m_render_state, model, view, projection );
     153                        m_scene_state.set_model(nv::mat4(
     154                                1.f,0.f,0.f,0.f,
     155                                0.f,0.f,1.f,0.f,
     156                                0.f,1.f,0.f,0.f,
     157                                0.f,0.f,0.f,1.f
     158                        ) );
     159
     160                        m_window->get_context()->draw( m_render_state, m_scene_state, m_program, m_mesh );
    216161                }
    217162
     
    251196}
    252197
     198void application::load_animation( const std::string& path )
     199{
     200        delete m_animation;
     201        m_animation = nullptr;
     202        NV_PROFILE("load_animation");
     203        nv::c_file_system fs;
     204        nv::stream* anim_file = fs.open( path.c_str() );
     205
     206        if ( anim_file != nullptr )
     207        {
     208                m_animation = new nv::md5_animation();
     209                if ( !m_animation->load_animation(*anim_file) )
     210                {
     211                        delete m_animation;
     212                        m_animation = nullptr;
     213                }
     214                m_mesh->setup_animation( m_animation );
     215                delete anim_file;
     216        }
     217}
     218
     219
    253220application::~application()
    254221{
Note: See TracChangeset for help on using the changeset viewer.