Changeset 239


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
Location:
trunk
Files:
2 deleted
25 edited

Legend:

Unmodified
Added
Removed
  • trunk/nv/formats/md2_loader.hh

    r236 r239  
    2828                virtual ~md2_loader();
    2929                virtual bool load( stream& source );
    30                 virtual size_t get_size() { return m_size; }
    31                 virtual mesh_data_old* release_mesh_data();
    32                 virtual mesh_data_old* get_frame( sint32 frame );
    33 
    34 //              virtual mesh* release_mesh();
    35 //              virtual mesh* get_frame( sint32 frame );
    36 //              virtual const md3_tag* get_tag( const std::string& name ) const;
    37 //              virtual mat4 get_tag( sint32 frame, const std::string& name ) const;
     30                virtual mesh_data* release_mesh_data();
     31                virtual mesh_data* release_mesh_data( sint32 frame );
    3832                size_t get_max_frames() const;
    39 //              void load_tag_names( std::vector< std::string >& tags );
    40 //              void load_tags( std::vector<mat4>& t, const std::string& tag );
    41                 void load_positions( std::vector<vec3>& p, sint32 frame =-1 );
    42                 void load_normals( std::vector<vec3>& n, sint32 frame =-1 );
    43                 void load_texcoords( std::vector<vec2>& t );
    44                 void load_indicies( std::vector<uint32>& idx );
    4533        private:
    4634                void reindex();
    4735        private:
    4836                void* m_md2;
    49 //              std::unordered_map< std::string, md3_tag > m_tags;
    50                 std::size_t m_size;
    5137                std::vector< uint16 > m_new_indexes;
    5238                std::vector< uint16 > m_new_vindexes;
  • trunk/nv/formats/md3_loader.hh

    r236 r239  
    1818#include <vector>
    1919#include <nv/transform.hh>
    20 #include <nv/gfx/mesh_data.hh>
     20#include <nv/interface/mesh_data.hh>
    2121#include <nv/interface/mesh_loader.hh>
    2222
    2323namespace nv
    2424{
    25 
    26         struct md3_tag
    27         {
    28                 std::string name;
    29                 transform   trans;
    30         };
    3125
    3226        class md3_loader : public mesh_loader
     
    3630                virtual ~md3_loader();
    3731                virtual bool load( stream& source );
    38                 virtual size_t get_size() { return m_size; }
    39                 virtual mesh_data_old* release_mesh_data();
    40                 virtual mesh_data_old* get_frame( sint32 frame );
     32                virtual mesh_data* release_mesh_data();
     33                virtual mesh_data* release_mesh_data( sint32 frame );
     34                size_t get_max_frames() const;
     35                virtual tag_map* create_tag_map();
     36        private:
     37                void load_tags( std::vector<transform>& t, const std::string& tag );
     38                void* m_md3;
    4139
    42                 virtual const md3_tag* get_tag( const std::string& name ) const;
    43                 virtual transform get_tag( sint32 frame, const std::string& name ) const;
    44                 size_t get_max_frames() const;
    45                 void load_tag_names( std::vector< std::string >& tags );
    46                 void load_tags( std::vector<transform>& t, const std::string& tag );
    47                 void load_positions( std::vector<vec3>& p, sint32 frame =-1 );
    48                 void load_normals( std::vector<vec3>& n, sint32 frame =-1 );
    49                 void load_texcoords( std::vector<vec2>& t );
    50                 void load_indicies( std::vector<uint32>& idx );
    51         private:
    52                 void* m_md3;
     40                struct md3_tag
     41                {
     42                        std::string name;
     43                        transform   trans;
     44                };
     45
    5346                std::unordered_map< std::string, md3_tag > m_tags;
    54                 std::size_t m_size;
    5547        };
    5648
  • trunk/nv/formats/md5_loader.hh

    r236 r239  
    2222{
    2323
     24        struct md5_vtx_pnt
     25        {
     26                vec3 position;
     27                vec3 normal;
     28                vec3 tangent;
     29        };
     30
     31        struct md5_vtx_t
     32        {
     33                vec2 texcoord;
     34        };
     35
     36        struct md5_vtx_data
     37        {
     38                glm::vec3 normal;
     39                glm::vec3 tangent;
     40                size_t    start_weight;
     41                size_t    weight_count;
     42        };
     43
     44        struct md5_weight
     45        {
     46                size_t    joint_id;
     47                float     bias;
     48                glm::vec3 pos;
     49        };
     50
    2451        class md5_animation
    2552        {
    2653        protected:
    2754                friend class md5_loader;
     55                friend class md5_mesh_data;
    2856                struct md5_joint_info
    2957                {
     
    137165        };
    138166
     167        class md5_mesh_data : public mesh_data
     168        {
     169        public:
     170                friend class md5_loader;
     171
     172                void apply( const md5_animation& animation );
     173        private:
     174                uint32*                     m_idata;
     175                md5_vtx_t*                  m_tdata;
     176                md5_vtx_pnt*                m_pntdata;
     177                std::string                 m_shader;
     178                std::vector< md5_vtx_data > m_vtx_data;
     179                std::vector< md5_weight   > m_weights;
     180        };
     181
    139182
    140183        class md5_loader : public mesh_loader
     
    142185        public:
    143186                md5_loader() {}
    144                 virtual ~md5_loader() {}
     187                virtual ~md5_loader();
    145188                virtual bool load( stream& source );
    146                 virtual size_t get_size();
    147                 virtual mesh_data_old* release_mesh_data() { return release_submesh_data(0); }
    148                 virtual mesh_data_old* release_submesh_data( uint32 mesh_id );
    149 //              virtual mesh* get_frame( sint32 /*frame*/ ) { return nullptr; }
     189                virtual mesh_data* release_mesh_data() { return release_mesh_data( 0 ); }
     190                virtual mesh_data* release_mesh_data( uint32 mesh );
    150191                bool check_animation( const md5_animation& animation ) const;
    151                 void apply( const md5_animation& animation );
    152 //              virtual const md3_tag* get_tag( const std::string& name ) const;
    153 //              virtual mat4 get_tag( sint32 frame, const std::string& name ) const;
    154 //              size_t get_max_frames() const;
    155 //              void load_tag_names( std::vector< std::string >& tags );
    156 //              void load_tags( std::vector<mat4>& t, const std::string& tag );
    157 //              void load_positions( std::vector<vec3>& p, sint32 frame =-1 );
    158 //              void load_normals( std::vector<vec3>& n, sint32 frame =-1 );
    159 //              void load_texcoords( std::vector<vec2>& t );
    160 //              void load_indicies( std::vector<uint16>& idx );
    161         protected:
    162                 typedef std::vector<glm::vec3> md5_vec_buffer;
    163                 typedef std::vector<glm::vec2> md5_tc_buffer;
    164                 typedef std::vector<uint32> md5_index_buffer;
    165 
    166                 struct md5_vertex
    167                 {
    168                         glm::vec3 position;
    169                         glm::vec3 normal;
    170                         glm::vec3 tangent;
    171                         glm::vec2 texcoord;
    172                         size_t    start_weight;
    173                         size_t    weight_count;
    174                 };
    175 
    176                 typedef std::vector<md5_vertex> md5_vertex_list;
    177 
    178                 struct md5_triangle
    179                 {
    180                         size_t indices[3];
    181                 };
    182                 typedef std::vector<md5_triangle> md5_triangle_list;
    183 
    184                 struct md5_weight
    185                 {
    186                         size_t    joint_id;
    187                         float     bias;
    188                         glm::vec3 pos;
    189                 };
    190                 typedef std::vector<md5_weight> md5_weight_list;
     192        protected:
    191193
    192194                struct md5_joint
     
    197199                        quat  orient;
    198200                };
    199                 typedef std::vector<md5_joint> md5_joint_list;
    200 
    201                 struct md5_mesh
    202                 {
    203                         std::string       shader;
    204                         md5_vertex_list   verts;
    205                         md5_triangle_list tris;
    206                         md5_weight_list   weights;
    207 
    208                         md5_vec_buffer    position_buffer;
    209                         md5_vec_buffer    normal_buffer;
    210                         md5_vec_buffer    tangent_buffer;
    211                         md5_tc_buffer     texcoord_buffer;
    212                         md5_index_buffer  index_buffer;
    213                 };
    214                 typedef std::vector<md5_mesh*> md5_mesh_list;
    215         public:
    216                 const md5_vec_buffer&   get_positions( uint32 mesh_id ) const { return m_meshes[mesh_id]->position_buffer; }
    217                 const md5_vec_buffer&   get_normals  ( uint32 mesh_id ) const { return m_meshes[mesh_id]->normal_buffer; }
    218                 const md5_vec_buffer&   get_tangents ( uint32 mesh_id ) const { return m_meshes[mesh_id]->tangent_buffer; }
    219                 const md5_tc_buffer&    get_texcoords( uint32 mesh_id ) const { return m_meshes[mesh_id]->texcoord_buffer; }
    220                 const md5_index_buffer& get_indices  ( uint32 mesh_id ) const { return m_meshes[mesh_id]->index_buffer; }
    221                 size_t get_vertex_count( uint32 mesh_id ) const { return m_meshes[mesh_id]->position_buffer.size(); }
    222                 size_t get_index_count( uint32 mesh_id )  const { return m_meshes[mesh_id]->index_buffer.size(); }
    223         protected:
    224                 bool prepare_mesh( md5_mesh* mesh );
    225                 bool prepare_normals( md5_mesh* mesh );
    226                 bool prepare_animated_mesh( md5_mesh* mesh, const md5_animation::md5_frame_skeleton& skel );
     201        protected:
     202                bool prepare_mesh( md5_mesh_data* mdata );
    227203        protected:
    228204                uint32 m_md5_version;
     
    230206                uint32 m_num_meshes;
    231207
    232                 md5_joint_list m_joints;
    233                 md5_mesh_list  m_meshes;
    234 
    235                 //bool           m_animation_present;
    236                 //md5_animation m_animation;
    237 
    238                 size_t m_size;
    239                 glm::mat4x4     m_model_matrix;
     208                std::vector<md5_joint>      m_joints;
     209                std::vector<md5_mesh_data*> m_meshes;
    240210        };
    241211
  • trunk/nv/formats/obj_loader.hh

    r238 r239  
    2424        {
    2525        public:
    26                 obj_loader( bool tangents = false );
    27                 virtual ~obj_loader();
    28                 virtual bool load( stream& source );
    29                 virtual size_t get_size() { return m_size; }
    30                 virtual mesh_data_old* release_mesh_data() { mesh_data_old* m = m_mesh; m_mesh = nullptr; return m; }
    31         private:
    32                 mesh_data_old*  m_mesh;
    33                 bool            m_tangents;
    34                 std::size_t     m_size;
    35         };
    36 
    37         class wavefront_loader
    38         {
    39         public:
    40                 wavefront_loader( bool normals = true, bool tangents = false );
     26                obj_loader( bool normals = true, bool tangents = false );
    4127                virtual bool load( stream& source );
    4228                mesh_data* release_mesh_data();
    43                 ~wavefront_loader();
     29                ~obj_loader();
    4430        private:
    4531                vertex_descriptor m_descriptor;
     
    4935        };
    5036
    51 
    5237}
    5338
  • trunk/nv/gfx/keyframed_mesh.hh

    r236 r239  
    3737        {
    3838        public:
    39                 keyframed_mesh( context* a_context, mesh_data_old* a_data );
    40                 virtual size_t get_index_count() const { return m_data->get_index_count(); }
     39                keyframed_mesh( context* a_context, mesh_data* a_data, tag_map* a_tag_map );
     40                virtual size_t get_index_count() const { return m_index_count; }
    4141                virtual void run_animation( animation_entry* a_anim );
    4242                size_t get_max_frames() const;
     
    4848                virtual ~keyframed_mesh();
    4949        protected:
    50                 mesh_data_old* m_data;
     50                struct vertex_pn
     51                {
     52                        vec3 position;
     53                        vec3 normal;
     54                };
     55
     56                mesh_data* m_mesh_data;
     57                tag_map*   m_tag_map;
    5158
    5259                uint32 m_start_frame;
     
    5966                bool   m_looping;
    6067                bool   m_active;
     68
     69                uint32 m_index_count;
     70                uint32 m_frame_count;
     71                uint32 m_vertex_count;
    6172        };
    6273
     
    6576        {
    6677        public:
    67                 keyframed_mesh_gpu( context* a_context, mesh_data_old* a_data, program* a_program );
     78                keyframed_mesh_gpu( context* a_context, mesh_data* a_data, tag_map* a_tag_map, program* a_program );
    6879                void update( uint32 ms );
    6980        private:
     
    7889        {
    7990        public:
    80                 keyframed_mesh_cpu( context* a_context, mesh_data_old* a_data );
     91                keyframed_mesh_cpu( context* a_context, mesh_data* a_data, tag_map* a_tag_map );
    8192                void update( uint32 ms );
    8293        private:
    83                 std::vector<vec3> m_position;
    84                 std::vector<vec3> m_normal;
    85                 vertex_buffer* m_vb_position;
    86                 vertex_buffer* m_vb_normal;
     94                std::vector<vertex_pn> m_vertex;
     95                vertex_buffer*         m_vb;
    8796        };
    8897
  • trunk/nv/gfx/skeletal_mesh.hh

    r230 r239  
    3131        };
    3232
     33        // TODO: INSTANCING! currently md5_mesh data is deleted!
    3334        class skeletal_mesh : public animated_mesh
    3435        {
    3536        public:
    36                 skeletal_mesh( context* a_context, md5_loader* a_loader );
    37                 virtual size_t get_index_count() const { return m_data->get_index_count(0); }
     37                skeletal_mesh( context* a_context, md5_mesh_data* a_mesh_data );
     38                virtual size_t get_index_count() const { return m_mesh_data->get_count(); }
    3839                virtual void run_animation( animation_entry* a_anim );
    3940                virtual void setup_animation( md5_animation* a_anim );
     
    4142                virtual ~skeletal_mesh();
    4243        protected:
    43                 vertex_buffer* m_vb_position;
    44                 vertex_buffer* m_vb_normal;
    45                 vertex_buffer* m_vb_tangent;
    46 
    47                 md5_loader*    m_data;
     44                md5_mesh_data* m_mesh_data;
    4845                md5_animation* m_animation;
    4946        };
  • trunk/nv/interface/device.hh

    r238 r239  
    1515#include <nv/common.hh>
    1616#include <nv/string.hh>
    17 #include <nv/interface/mesh.hh>
    1817#include <nv/interface/mesh_data.hh>
    1918#include <nv/interface/vertex_buffer.hh>
     
    3837                virtual uint32 get_ticks() = 0;
    3938                virtual void delay( uint32 ms ) = 0;
    40 
    41                 virtual vertex_array* create_vertex_array( const mesh_pack* m, const attribute_map* am, buffer_hint hint )
    42                 {
    43                         vertex_array* result = create_vertex_array();
    44                         for ( auto& attr : m->get_attributes() )
    45                         {
    46                                 // TODO : error checking
    47                                 vertex_buffer* vb = create_vertex_buffer( hint, attr.second->get_size(), attr.second->get_data() );
    48                                 result->add_vertex_buffer( am->at( attr.first )->get_location(), vb, attr.second->get_base_type(), attr.second->get_components() );
    49                         }
    50                         if ( m->has_indices() )
    51                         {
    52                                 const vertex_attribute_base* i = m->get_indices();
    53                                 index_buffer* vb = create_index_buffer( hint, i->get_size(), i->get_data() );
    54                                 result->set_index_buffer( vb, i->get_base_type(), true );
    55                         }
    56                         return result;
    57                 }
    5839
    5940                template < typename VTX, slot SLOT >
     
    121102                                const mesh_raw_channel* channel = data->get_channel_data()[ch];
    122103                                vertex_buffer* vb = create_vertex_buffer( hint, channel->size, channel->data );
    123                                 for ( uint32 s = 0; s < channel->desc.count; ++s )
    124                                 {
    125                                         const vertex_descriptor_slot& slot = channel->desc.slots[s];
    126                                         const datatype_info&          info = get_datatype_info(slot.etype);
    127                                         va->add_vertex_buffer( slot.vslot, vb, info.base , info.elements, slot.offset, channel->desc.size, false );
    128                                 }
     104                                va->add_vertex_buffers( vb, channel );
    129105                        }
    130106                        if ( data->get_index_channel() != nullptr )
  • trunk/nv/interface/mesh_data.hh

    r238 r239  
    99
    1010#include <nv/common.hh>
     11#include <vector>
    1112#include <nv/math.hh>
    12 #include <nv/interface/context.hh>
    1313#include <nv/interface/vertex.hh>
    1414
    1515namespace nv
    1616{
     17
     18        // TODO: friend mesh_data_creator class?
     19        // TODO: private const etc
     20
    1721
    1822        struct mesh_raw_channel
     
    2731                {
    2832                        if ( data != nullptr ) delete[] data;
     33                }
     34
     35                template < typename VTX >
     36                static mesh_raw_channel* create( uint32 count = 0 )
     37                {
     38                        mesh_raw_channel* result = new mesh_raw_channel();
     39                        result->desc.initialize<VTX>();
     40                        result->count = count;
     41                        result->size  = count * sizeof( VTX );
     42                        result->data  = (count > 0 ? ( new uint8[ result->size ] ) : nullptr );
     43                        return result;
    2944                }
    3045        };
     
    4257                        if ( data != nullptr ) delete[] data;
    4358                }
    44         };
    4559
    46         struct mesh_raw_data
    47         {
     60                template < typename ITYPE >
     61                static mesh_raw_index_channel* create( uint32 count = 0 )
     62                {
     63                        mesh_raw_index_channel* result = new mesh_raw_index_channel();
     64                        result->etype = type_to_enum< ITYPE >::type;
     65                        result->count = count;
     66                        result->size  = count * sizeof( ITYPE );
     67                        result->data  = (count > 0 ? ( new uint8[ result->size ] ) : nullptr );
     68                        return result;
     69                }
    4870        };
    4971
     
    7597                }
    7698
    77                 ~mesh_data()
     99                virtual ~mesh_data()
    78100                {
    79101                        for ( auto channel : m_channels ) delete channel;
  • trunk/nv/interface/mesh_loader.hh

    r236 r239  
    1515
    1616#include <nv/common.hh>
     17#include <vector>
     18#include <unordered_map>
     19#include <nv/transform.hh>
    1720#include <nv/string.hh>
     21#include <nv/interface/mesh_data.hh>
    1822#include <nv/interface/stream.hh>
    19 #include <nv/gfx/mesh_data.hh>
    2023
    2124namespace nv
    2225{
     26
     27        class tag_map
     28        {
     29        public:
     30                typedef std::vector< transform > transforms;
     31                typedef std::unordered_map< std::string, transforms > map;
     32
     33                tag_map () {}
     34                map& get_map()             { return m_map; }
     35                const map& get_map() const { return m_map; }
     36                const transforms* get_tag( const std::string& key ) const
     37                {
     38                        auto it = m_map.find( key );
     39                        return ( it != m_map.end() ? &(it->second) : nullptr );
     40                }
     41        private:
     42                map m_map;
     43        };
     44
    2345
    2446        class mesh_loader
     
    2749                mesh_loader() {}
    2850                virtual ~mesh_loader() {}
     51                virtual tag_map* create_tag_map() { return nullptr; }
    2952                virtual bool load( stream& source ) = 0;
    30                 virtual size_t get_size() = 0;
    31                 virtual mesh_data_old* release_mesh_data() = 0;
     53                virtual mesh_data* release_mesh_data() = 0;
    3254        };
    3355
  • trunk/nv/interface/vertex_buffer.hh

    r214 r239  
    1515#include <nv/common.hh>
    1616#include <nv/math.hh>
     17#include <nv/interface/vertex.hh>
     18#include <nv/interface/mesh_data.hh>
    1719#include <unordered_map>
    1820#include <string>
     
    99101                        m_map[ location ] = p;
    100102                }
     103
     104                void add_vertex_buffers( vertex_buffer* buffer, const mesh_raw_channel* channel )
     105                {
     106                        for ( uint32 s = 0; s < channel->desc.count; ++s )
     107                        {
     108                                add_vertex_buffer( buffer, channel->desc.slots[s], channel->desc.size );
     109                        }
     110                }
     111
     112                void add_vertex_buffer( vertex_buffer* buffer, const vertex_descriptor_slot& slot, size_t vtx_size )
     113                {
     114                        const datatype_info& info = get_datatype_info(slot.etype);
     115                        add_vertex_buffer( slot.vslot, buffer, info.base , info.elements, slot.offset, vtx_size, false );
     116                }
     117
    101118                void update_vertex_buffer( int location, vertex_buffer* b, bool owner )
    102119                {
     
    127144                        m_index       = buffer;
    128145                }
     146                vertex_buffer* find_buffer( int location )
     147                {
     148                        auto i = m_map.find( location );
     149                        if ( i != m_map.end() )
     150                        {
     151                                return i->second->get_buffer();
     152                        }
     153                        return nullptr;
     154                }
    129155                bool has_index_buffer() const { return m_index != nullptr; }
    130156                datatype get_index_buffer_type() const { return m_index_type; }
  • trunk/src/formats/md2_loader.cc

    r236 r239  
    203203static bool s_md2_normal_ready = false;
    204204
    205 md2_loader::md2_loader() : m_md2( nullptr ), m_size( 0 )
     205md2_loader::md2_loader() : m_md2( nullptr )
    206206{
    207207        if ( !s_md2_normal_ready )
     
    230230bool md2_loader::load( stream& source )
    231231{
    232         m_size = 0;
    233232        m_md2 = (void*)(new md2_t);
    234233        if ( !read_md2( (md2_t*)m_md2, source ) )
     
    237236        }
    238237        reindex();
    239         m_size = m_new_indexes.size();
    240238        return true;
    241239}
    242240
    243 mesh_data_old* nv::md2_loader::release_mesh_data()
    244 {
    245         mesh_data_creator m;
    246 
    247         load_positions( m.get_positions() );
    248         load_normals( m.get_normals() );
    249         load_texcoords( m.get_texcoords() );
    250         load_indicies( m.get_indices() );
    251 
    252         m_size = m.get_indices().size();
    253         return m.release();
    254 }
    255 
    256 mesh_data_old* nv::md2_loader::get_frame( sint32 frame )
    257 {
    258         mesh_data_creator m;
    259 
    260         load_positions( m.get_positions(), frame );
    261         load_normals( m.get_normals(), frame );
    262         load_texcoords( m.get_texcoords() );
    263         load_indicies( m.get_indices() );
    264 
    265         m_size = m.get_indices().size();
    266         return m.release();
    267 }
    268 
    269 
    270 /*
    271 mesh* nv::md2_loader::release_mesh()
    272 {
    273         return get_frame( 0 );
    274 }
    275 
    276 mesh* nv::md2_loader::get_frame( sint32 frame )
    277 {
    278         md2_t* md2 = (md2_t*)m_md2;
    279         if ( md2 == nullptr || frame >= md2->header.num_frames ) return nullptr;
    280         mesh* m = new mesh();
    281 
    282         vertex_attribute< vec3 >*   position = m->add_attribute<vec3>("nv_position");
    283         vertex_attribute< vec3 >*   normal   = m->add_attribute<vec3>("nv_normal");
    284         vertex_attribute< vec2 >*   texcoord = m->add_attribute<vec2>("nv_texcoord");
    285         vertex_attribute< uint32 >* indices  = m->add_indices<uint32>();
    286 
    287         load_positions( position->get(), frame );
    288         load_normals( normal->get(), frame );
    289 
    290         load_texcoords( texcoord->get() );
    291         load_indicies( indices->get() );
    292 
    293         m_size = indices->get().size();
    294         return m;
    295 }
    296 */
    297 
    298241size_t md2_loader::get_max_frames() const
    299242{
    300243        return static_cast< size_t >( ((md2_t*)m_md2)->header.num_frames );
    301 }
    302 
    303 void md2_loader::load_positions( std::vector<vec3>& p, sint32 frame /*=-1*/ )
    304 {
    305         md2_t* md2 = (md2_t*)m_md2;
    306         size_t num_frames = static_cast< size_t >( md2->header.num_frames );
    307         size_t num_verts  =     m_new_vindexes.size();
    308         p.clear();
    309         size_t current_frame = ( frame == -1 ? 0 : static_cast< size_t >( frame ) );
    310         size_t frame_count   = ( frame == -1 ? num_frames : 1 );
    311 
    312         p.reserve( num_verts * frame_count );
    313 
    314         while ( frame_count > 0 )
    315         {
    316                 const md2_frame_t& cframe = md2->frames[current_frame];
    317                 NV_LOG( LOG_INFO, "FrameID = " << cframe.name );
    318 
    319                 vec3 scale     = md2_vec3( cframe.scale );
    320                 vec3 translate = md2_vec3( cframe.translate );
    321 
    322                 for (size_t i = 0; i < num_verts; ++i )
    323                 {
    324                         const md2_vertex_t& v = cframe.vertices[ m_new_vindexes[ i ] ];
    325                         p.push_back( vec3( v.v[0], v.v[2], v.v[1] ) * scale + translate );
    326                 }
    327                 ++current_frame;
    328                 --frame_count;
    329         }
    330 }
    331 
    332 void md2_loader::load_normals( std::vector<vec3>& n, sint32 frame /*=-1*/ )
    333 {
    334         md2_t* md2 = (md2_t*)m_md2;
    335         size_t num_frames = static_cast< size_t >( md2->header.num_frames );
    336         size_t num_verts  =     m_new_vindexes.size();
    337         n.clear();
    338         size_t current_frame = ( frame == -1 ? 0 : static_cast< size_t>( frame ) );
    339         size_t frame_count   = ( frame == -1 ? num_frames : 1 );
    340 
    341         n.reserve( num_verts * frame_count );
    342 
    343         while ( frame_count > 0 )
    344         {
    345                 const md2_frame_t& cframe = md2->frames[current_frame];
    346 
    347                 for (size_t i = 0; i < num_verts; ++i )
    348                 {
    349                         const md2_vertex_t& v = cframe.vertices[ m_new_vindexes[ i ] ];
    350                         n.push_back( s_md2_normal_cache[ v.n ] );
    351                 }
    352                 ++current_frame;
    353                 --frame_count;
    354         }
    355 }
    356 
    357 void md2_loader::load_texcoords( std::vector<vec2>& t )
    358 {
    359         md2_t* md2 = (md2_t*)m_md2;
    360         size_t num_verts  = m_new_vindexes.size();
    361 
    362         t.clear();
    363         t.reserve( num_verts );
    364 
    365         vec2 scale( 1.0f / (float) md2->header.skinwidth, 1.0f / (float) md2->header.skinheight );
    366 
    367         for (size_t i = 0; i < num_verts; ++i )
    368         {
    369                 const md2_texcoord_t& st = md2->texcoords[ m_new_tindexes[ i ] ];
    370                 t.push_back( scale * vec2( st.s, st.t ) );
    371         }
    372 
    373 }
    374 
    375 void md2_loader::load_indicies( std::vector<uint32>& idx )
    376 {
    377         idx.assign( m_new_indexes.begin(), m_new_indexes.end() );
    378244}
    379245
     
    433299        NV_LOG( LOG_INFO, "Reuse count      = " << stats_reuse );
    434300}
     301
     302
     303struct vtx_md2_pn
     304{
     305        nv::vec3 position;
     306        nv::vec3 normal;
     307};
     308
     309struct vtx_md2_t
     310{
     311        nv::vec2 texcoord;
     312};
     313
     314
     315mesh_data* nv::md2_loader::release_mesh_data()
     316{
     317        return release_mesh_data( -1 );
     318}
     319
     320mesh_data* nv::md2_loader::release_mesh_data( sint32 frame )
     321{
     322        md2_t* md2 = (md2_t*)m_md2;
     323        size_t num_frames = static_cast< size_t >( md2->header.num_frames );
     324        size_t num_verts  =     m_new_vindexes.size();
     325        size_t current_frame = ( frame == -1 ? 0 : static_cast< size_t >( frame ) );
     326        size_t frame_count   = ( frame == -1 ? num_frames : 1 );
     327
     328        mesh_raw_channel* mc_pn = mesh_raw_channel::create< vtx_md2_pn >( num_verts * frame_count );
     329        vtx_md2_pn* vtx_pn = (vtx_md2_pn*)mc_pn->data;
     330
     331        uint32 index = 0;
     332        while ( frame_count > 0 )
     333        {
     334                const md2_frame_t& cframe = md2->frames[current_frame];
     335                NV_LOG( LOG_INFO, "FrameID = " << cframe.name );
     336
     337                vec3 scale     = md2_vec3( cframe.scale );
     338                vec3 translate = md2_vec3( cframe.translate );
     339
     340                for (size_t i = 0; i < num_verts; ++i )
     341                {
     342                        const md2_vertex_t& v = cframe.vertices[ m_new_vindexes[ i ] ];
     343                        vtx_pn[index].position = vec3( v.v[0], v.v[2], v.v[1] ) * scale + translate;
     344                        vtx_pn[index].normal   = s_md2_normal_cache[ v.n ];
     345                        index++;
     346                }
     347                ++current_frame;
     348                --frame_count;
     349        }
     350
     351        mesh_raw_channel* mc_t = mesh_raw_channel::create< vtx_md2_t >( num_verts );
     352        vtx_md2_t* vtx_t = (vtx_md2_t*)mc_t->data;
     353
     354        vec2 scale( 1.0f / (float) md2->header.skinwidth, 1.0f / (float) md2->header.skinheight );
     355        for (size_t i = 0; i < num_verts; ++i )
     356        {
     357                const md2_texcoord_t& st = md2->texcoords[ m_new_tindexes[ i ] ];
     358                vtx_t[i].texcoord = scale * vec2( st.s, st.t );
     359        }
     360
     361        mesh_raw_index_channel* ic = mesh_raw_index_channel::create< uint16 >( m_new_indexes.size() );
     362        if ( m_new_indexes.size() > 0 )
     363        {
     364                uint16* icp = (uint16*)ic->data;
     365                std::copy_n( m_new_indexes.data(), m_new_indexes.size(), icp );
     366        }
     367
     368        mesh_data* result = new mesh_data();
     369        result->add_channel( mc_pn );
     370        result->add_channel( mc_t );
     371        result->set_index_channel( ic );
     372        return result;
     373}
  • trunk/src/formats/md3_loader.cc

    r236 r239  
    234234
    235235md3_loader::md3_loader()
    236         : m_md3( nullptr ), m_size( 0 )
     236        : m_md3( nullptr )
    237237{
    238238        if ( !s_normal_ready )
     
    276276{
    277277        m_tags.clear();
    278         m_size = 0;
    279278
    280279        m_md3 = (void*)(new md3_t);
     
    285284        return true;
    286285}
    287 
    288 /*
    289 mesh* nv::md3_loader::release_mesh()
    290 {
    291         return get_frame( 0 );
    292 }
    293 
    294 mesh* nv::md3_loader::get_frame( sint32 frame )
    295 {
    296         mesh* m = new mesh();
    297         md3_t* md3 = (md3_t*)m_md3;
    298 
    299         NV_LOG( LOG_INFO, "Tags:" );
    300         for ( sint32 i = 0; i < md3->header.num_tags; ++i )
    301         {
    302                 const md3_tag_t& rtag = md3->tags[i + md3->header.num_tags * frame];
    303 
    304                 md3_tag& tag   = m_tags[ (char*)(rtag.name) ];
    305                 tag.name       = (char*)(rtag.name);
    306                 vec4 axisx     = vec4( md3_vec3( rtag.axis[0] ), 0.0 );
    307                 vec4 axisz     = vec4( md3_vec3( rtag.axis[1] ), 0.0 );
    308                 vec4 axisy     = vec4( md3_vec3( rtag.axis[2] ), 0.0 );
    309                 vec4 origin    = vec4( md3_vec3( rtag.origin ),  1.0 );
    310                 tag.transform  = glm::mat4( axisx, axisy, axisz, origin );
    311                 NV_LOG( LOG_INFO, "Tag " << tag.name << " found" );
    312         }
    313 
    314         vertex_attribute< vec3 >* position = m->add_attribute<vec3>("nv_position");
    315         vertex_attribute< vec3 >* normal   = m->add_attribute<vec3>("nv_normal");
    316         vertex_attribute< vec2 >* texcoord = m->add_attribute<vec2>("nv_texcoord");
    317         vertex_attribute< uint32 >* indices  = m->add_indices<uint32>();
    318 
    319         load_positions( position->get(), frame );
    320         load_normals( normal->get(), frame );
    321 
    322         load_texcoords( texcoord->get() );
    323         load_indicies( indices->get() );
    324 
    325         m_size = indices->get().size();
    326         return m;
    327 }
    328 */
    329286
    330287void nv::md3_loader::load_tags( std::vector<transform>& t, const std::string& tag )
     
    351308}
    352309
    353 mesh_data_old* nv::md3_loader::release_mesh_data()
    354 {
    355         mesh_data_creator m;
    356 
    357         load_positions( m.get_positions() );
    358         load_normals( m.get_normals() );
    359         load_texcoords( m.get_texcoords() );
    360         load_indicies( m.get_indices() );
    361        
    362         std::vector< std::string > names;
    363         load_tag_names( names );
    364 
    365         for ( auto& name : names )
    366         {
    367                 load_tags( m.get_tag_map()[ name ], name );
    368         }
    369 
    370         return m.release();
    371 }
    372 
    373 mesh_data_old* nv::md3_loader::get_frame( sint32 frame )
    374 {
    375         mesh_data_creator m;
    376 
    377         load_positions( m.get_positions(), frame );
    378         load_normals( m.get_normals(), frame );
    379         load_texcoords( m.get_texcoords() );
    380         load_indicies( m.get_indices() );
    381 
    382         std::vector< std::string > names;
    383         load_tag_names( names );
    384 
    385         for ( auto& name : names )
    386         {
    387                 load_tags( m.get_tag_map()[ name ], name );
    388         }
    389         return m.release();
    390 }
    391 
    392 size_t md3_loader::get_max_frames() const
    393 {
    394         return static_cast< size_t >( ((md3_t*)m_md3)->header.num_frames );
    395 }
    396 
    397 void md3_loader::load_tag_names( std::vector< std::string >& tags )
    398 {
    399         tags.clear();
    400         md3_t* md3 = (md3_t*)m_md3;
    401         for ( sint32 i = 0; i < md3->header.num_tags; ++i )
    402         {
    403                 const md3_tag_t& rtag = md3->tags[i + md3->header.num_tags];
    404                 tags.push_back( (char*)(rtag.name) );
    405         }
    406 }
    407 
    408 transform md3_loader::get_tag( sint32 frame, const std::string& name ) const
    409 {
    410         md3_t* md3 = (md3_t*)m_md3;
    411         for ( sint32 i = 0; i < md3->header.num_tags; ++i )
    412         {
    413                 const md3_tag_t& rtag = md3->tags[i + md3->header.num_tags * frame];
    414                 std::string rname((char*)(rtag.name));
    415                 if (rname == name)
    416                 {
    417                         vec3 axisx ( md3_vec3( rtag.axis[0] ) );
    418                         vec3 axisz ( md3_vec3( rtag.axis[1] ) );
    419                         vec3 axisy ( md3_vec3( rtag.axis[2] ) );
    420                         vec3 origin( md3_vec3( rtag.origin ) );
    421                         return transform( origin, quat( mat3( axisx, axisy, axisz ) ) );
    422                 }
    423         }
    424         return transform();
    425 }
    426 
    427 const md3_tag* md3_loader::get_tag( const std::string& name ) const
    428 {
    429         auto it = m_tags.find( name );
    430         if ( it == m_tags.end() ) return nullptr;
    431         return &(it->second);
    432 }
    433 
    434 void md3_loader::load_positions( std::vector<vec3>& p, sint32 frame /*=-1*/ )
     310struct vtx_md3_pn
     311{
     312        nv::vec3 position;
     313        nv::vec3 normal;
     314};
     315
     316struct vtx_md3_t
     317{
     318        nv::vec2 texcoord;
     319};
     320
     321mesh_data* nv::md3_loader::release_mesh_data()
     322{
     323        return release_mesh_data( -1 );
     324}
     325
     326mesh_data* nv::md3_loader::release_mesh_data( sint32 frame )
    435327{
    436328        md3_t* md3 = (md3_t*)m_md3;
    437329        sint32 num_surfaces = md3->header.num_surfaces;
    438         p.clear();
     330        sint32 num_verts    = md3->vertices_per_frame;
    439331        sint32 current_frame = ( frame == -1 ? 0 : frame );
    440332        sint32 frame_count   = ( frame == -1 ? md3->header.num_frames : 1 );
    441333
    442         p.reserve( static_cast< size_t >( md3->vertices_per_frame * frame_count ) );
    443 
     334        mesh_raw_channel* mc_pn = mesh_raw_channel::create< vtx_md3_pn >( num_verts * frame_count );
     335        vtx_md3_pn* vtx_pn = (vtx_md3_pn*)mc_pn->data;
     336
     337        uint32 index = 0;
    444338        while ( frame_count > 0 )
    445339        {
     
    453347                        {
    454348                                md3_vertex_t& v = surface.vertices[j];
    455                                 p.push_back( vec3( v.x * MD3_XYZ_SCALE, v.z * MD3_XYZ_SCALE, v.y * MD3_XYZ_SCALE ) );
     349                                vtx_pn[index].position = vec3( v.x * MD3_XYZ_SCALE, v.z * MD3_XYZ_SCALE, v.y * MD3_XYZ_SCALE );
     350                                vtx_pn[index].normal   = s_normal_cache[ v.normal ];
     351                                index++;
    456352                        }
     353
    457354                }
    458355                ++current_frame;
    459356                --frame_count;
    460357        }
    461 }
    462 
    463 void md3_loader::load_normals( std::vector<vec3>& n, sint32 frame /*=-1*/ )
    464 {
    465         md3_t* md3 = (md3_t*)m_md3;
    466         sint32 num_surfaces = md3->header.num_surfaces;
    467         n.clear();
    468         sint32 current_frame = ( frame == -1 ? 0 : frame );
    469         sint32 frame_count   = ( frame == -1 ? md3->header.num_frames : 1 );
    470 
    471         n.reserve( static_cast< size_t >( md3->vertices_per_frame * frame_count ) );
    472 
    473         while ( frame_count > 0 )
    474         {
    475                 for ( sint32 i = 0; i < num_surfaces; ++i )
    476                 {
    477                         md3_surface_t& surface = md3->surfaces[i];
    478                         sint32         vcount  = surface.header.num_verts;
    479                         sint32         offset  = vcount * current_frame;
    480                         sint32         limit   = vcount + offset;
    481                         for (sint32 j = offset; j < limit; ++j )
    482                         {
    483                                 n.push_back( s_normal_cache[ surface.vertices[j].normal ] );
    484                         }
    485                 }
    486                 ++current_frame;
    487                 --frame_count;
    488         }
    489 }
    490 
    491 void md3_loader::load_texcoords( std::vector<vec2>& t )
    492 {
    493         md3_t* md3 = (md3_t*)m_md3;
    494         sint32 num_surfaces = md3->header.num_surfaces;
    495         t.clear();
     358
     359        index = 0;
     360        mesh_raw_channel* mc_t = mesh_raw_channel::create< vtx_md3_t >( num_verts );
     361        vtx_md3_t* vtx_t = (vtx_md3_t*)mc_t->data;
    496362        for ( sint32 i = 0; i < num_surfaces; ++i )
    497363        {
    498364                const md3_surface_t& surface = md3->surfaces[i];
    499365                const uint32         vcount  = static_cast< uint32 >( surface.header.num_verts );
    500                 t.reserve( t.size() + vcount );
    501366                for (uint32 j = 0; j < vcount; ++j )
    502367                {
    503                         t.push_back( md3_texcoord( surface.st[j] ) );
     368                        vtx_t[index++].texcoord = md3_texcoord( surface.st[j] );
    504369                }
    505370        }
    506 }
    507 
    508 void md3_loader::load_indicies( std::vector<uint32>& idx )
    509 {
    510         md3_t* md3 = (md3_t*)m_md3;
    511         sint32 num_surfaces = md3->header.num_surfaces;
    512         idx.clear();
     371
     372        sint32 index_count = 0;
     373        for ( sint32 i = 0; i < num_surfaces; ++i )
     374        {
     375                index_count += md3->surfaces[i].header.num_triangles * 3;
     376        }
     377
     378        index = 0;
    513379        sint32 index_base = 0;
     380        mesh_raw_index_channel* ic = mesh_raw_index_channel::create< uint16 >( index_count );
     381        uint16* icp = (uint16*)ic->data;
    514382        for ( sint32 i = 0; i < num_surfaces; ++i )
    515383        {
    516384                const md3_surface_t& surface = md3->surfaces[i];
    517385                const size_t         tcount  = static_cast< size_t >( surface.header.num_triangles );
    518 
    519                 idx.reserve( idx.size() + tcount * 3 );
    520386                for (size_t j = 0; j < tcount; ++j )
    521387                {
    522388                        const md3_triangle_t& t = surface.triangles[j];
    523                         idx.push_back( static_cast< uint32 >( index_base + t.indexes[0] ) );
    524                         idx.push_back( static_cast< uint32 >( index_base + t.indexes[1] ) );
    525                         idx.push_back( static_cast< uint32 >( index_base + t.indexes[2] ) );
     389                        icp[index++] = static_cast< uint16 >( index_base + t.indexes[0] );
     390                        icp[index++] = static_cast< uint16 >( index_base + t.indexes[1] );
     391                        icp[index++] = static_cast< uint16 >( index_base + t.indexes[2] );
    526392                }
    527 
    528393                index_base += surface.header.num_verts;
    529394        }
    530395
    531 }
    532 
     396        mesh_data* result = new mesh_data();
     397        result->add_channel( mc_pn );
     398        result->add_channel( mc_t );
     399        result->set_index_channel( ic );
     400        return result;
     401}
     402
     403tag_map* nv::md3_loader::create_tag_map()
     404{
     405        md3_t* md3 = (md3_t*)m_md3;
     406        tag_map* result = new tag_map();
     407        for ( sint32 i = 0; i < md3->header.num_tags; ++i )
     408        {
     409                const md3_tag_t& rtag = md3->tags[i + md3->header.num_tags];
     410                std::string name( (char*)(rtag.name) );
     411                load_tags( result->get_map()[ name ], name );
     412        }
     413        return result;
     414}
     415
     416size_t md3_loader::get_max_frames() const
     417{
     418        return static_cast< size_t >( ((md3_t*)m_md3)->header.num_frames );
     419}
  • trunk/src/formats/md5_loader.cc

    r236 r239  
    9090                else if ( command == "mesh" )
    9191                {
    92                         md5_mesh* mesh = new md5_mesh;
     92                        md5_mesh_data* mesh = new md5_mesh_data();
     93
    9394                        int num_verts, num_tris, num_weights;
    9495
     
    99100                                if ( command == "shader" )
    100101                                {
    101                                         sstream >> mesh->shader;
    102                                         remove_quotes( mesh->shader );
     102                                        sstream >> mesh->m_shader;
     103                                        remove_quotes( mesh->m_shader );
    103104                                        // texturePath.replace_extension( ".tga" );
    104105                                        next_line( sstream );
     
    107108                                {
    108109                                        sstream >> num_verts;
     110
     111                                        {
     112                                                mesh_raw_channel* ch_pnt = mesh_raw_channel::create<md5_vtx_pnt>( num_verts );
     113                                                mesh_raw_channel* ch_t   = mesh_raw_channel::create<md5_vtx_t>( num_verts );
     114                                                mesh->m_pntdata          = (md5_vtx_pnt*)ch_pnt->data;
     115                                                mesh->m_tdata            = (md5_vtx_t*)ch_t->data;
     116                                                mesh->add_channel( ch_pnt );
     117                                                mesh->add_channel( ch_t );
     118                                        }
     119                                        mesh->m_vtx_data.resize( num_verts );
     120
    109121                                        next_line( sstream );
    110122                                        std::string line;
    111123                                        for ( int i = 0; i < num_verts; ++i )
    112124                                        {
    113                                                 md5_vertex vert;
     125                                                md5_vtx_data& vdata = mesh->m_vtx_data[i];
     126                                                size_t weight_count;
     127                                                size_t start_weight;
     128                                                vec2 texcoord;
    114129
    115130                                                std::getline( sstream, line );
    116                                                 sscanf( line.c_str(), "%*s %*u ( %f %f ) %u %u", &(vert.texcoord.x), &(vert.texcoord.y), &(vert.start_weight), &(vert.weight_count) );
    117 
    118 //                                              std::string ignore;
    119 //                                              discard( sstream, "vert" );
    120 //                                              sstream >> ignore;
    121 //                                              discard( sstream, "(" );
    122 //                                              sstream >> vert.texcoord.x >> vert.texcoord.y;
    123 //                                              discard( sstream, ")" );
    124 //                                              sstream >> vert.start_weight >> vert.weight_count;
    125 //                                              next_line( sstream );
    126 
    127                                                 mesh->verts.push_back(vert);
    128                                                 mesh->texcoord_buffer.push_back( vert.texcoord );
     131                                                sscanf( line.c_str(), "%*s %*u ( %f %f ) %u %u", &(texcoord.x), &(texcoord.y), &(start_weight), &(weight_count) );
     132                                                vdata.start_weight = start_weight;
     133                                                vdata.weight_count = weight_count;
     134                                                mesh->m_tdata[i].texcoord = texcoord;
    129135                                        } 
    130136                                }
     
    132138                                {
    133139                                        sstream >> num_tris;
     140
     141                                        mesh_raw_index_channel* ch_i = mesh_raw_index_channel::create<uint32>( num_tris * 3 );
     142                                        uint32* vtx_i                = (uint32*)ch_i->data;
     143                                        mesh->m_idata                = vtx_i;
     144                                        uint32 idx = 0;
     145                                        mesh->set_index_channel( ch_i );
     146
    134147                                        next_line( sstream );
    135148                                        std::string line;
    136149                                        for ( int i = 0; i < num_tris; ++i )
    137150                                        {
    138                                                 md5_triangle tri;
     151                                                size_t ti0;
     152                                                size_t ti1;
     153                                                size_t ti2;
    139154
    140155                                                std::getline( sstream, line );
    141                                                 sscanf( line.c_str(), "%*s %*u %u %u %u )", &(tri.indices[0]), &(tri.indices[1]), &(tri.indices[2]));
    142 
    143 //                                              std::string ignore;
    144 //                                              discard( sstream, "tri" );
    145 //                                              sstream >> ignore >> tri.indices[0] >> tri.indices[1] >> tri.indices[2];
    146 //                                              next_line( sstream );
    147 
    148                                                 mesh->tris.push_back( tri );
    149                                                 mesh->index_buffer.push_back( (uint32)tri.indices[0] );
    150                                                 mesh->index_buffer.push_back( (uint32)tri.indices[1] );
    151                                                 mesh->index_buffer.push_back( (uint32)tri.indices[2] );
     156                                                sscanf( line.c_str(), "%*s %*u %u %u %u )", &(ti0), &(ti1), &(ti2));
     157
     158                                                vtx_i[idx++] = (uint32)ti0;
     159                                                vtx_i[idx++] = (uint32)ti1;
     160                                                vtx_i[idx++] = (uint32)ti2;
    152161                                        }             
    153162                                }
     
    155164                                {
    156165                                        sstream >> num_weights;
    157                                         mesh->weights.reserve( num_weights );
     166                                        mesh->m_weights.reserve( num_weights );
    158167                                        next_line( sstream );
    159168                                        std::string line;
     
    164173                                                std::getline( sstream, line );
    165174                                                sscanf( line.c_str(), "%*s %*u %u %f ( %f %f %f )", &(weight.joint_id), &(weight.bias), &(weight.pos.x), &(weight.pos.y), &(weight.pos.z));
    166 
    167 //                                              std::string ignore;
    168 //                                              discard( sstream, "weight" );
    169 //                                              sstream >> ignore >> weight.joint_id >> weight.bias;
    170 //                                              discard( sstream, "(" );
    171 //                                              sstream >> weight.pos.x >> weight.pos.y >> weight.pos.z;
    172 //                                              discard( sstream, ")" );
    173 //                                              next_line( sstream );
    174                                                 mesh->weights.push_back(weight);
     175                                                mesh->m_weights.push_back(weight);
    175176                                        }
    176177                                }
     
    184185
    185186                        prepare_mesh( mesh );
    186                         prepare_normals( mesh );
    187187
    188188                        m_meshes.push_back(mesh);
     
    196196}
    197197
    198 bool md5_loader::prepare_mesh( md5_mesh* mesh )
    199 {
    200         mesh->position_buffer.clear();
    201         mesh->texcoord_buffer.clear();
    202 
    203         for ( uint32 i = 0; i < mesh->verts.size(); ++i )
    204         {
    205                 md5_vertex& vert = mesh->verts[i];
    206 
    207                 vert.position = glm::vec3(0);
    208                 vert.normal   = glm::vec3(0);
    209                 vert.tangent  = glm::vec3(0);
    210 
    211                 for ( size_t j = 0; j < vert.weight_count; ++j )
    212                 {
    213                         md5_weight& weight = mesh->weights[vert.start_weight + j];
     198bool md5_loader::prepare_mesh( md5_mesh_data* mdata )
     199{
     200        uint32 vtx_count = mdata->m_vtx_data.size();
     201        md5_vtx_pnt* vtcs = mdata->m_pntdata;
     202
     203        for ( uint32 i = 0; i < vtx_count; ++i )
     204        {
     205                md5_vtx_data& vdata = mdata->m_vtx_data[i];
     206                md5_vtx_pnt& vtc = vtcs[i];
     207
     208                vtc.position = glm::vec3(0);
     209                vtc.normal   = glm::vec3(0);
     210                vtc.tangent  = glm::vec3(0);
     211
     212                for ( size_t j = 0; j < vdata.weight_count; ++j )
     213                {
     214                        md5_weight& weight = mdata->m_weights[vdata.start_weight + j];
    214215                        md5_joint&  joint  = m_joints[weight.joint_id];
    215216
    216217                        glm::vec3 rot_pos = joint.orient * weight.pos;
    217218
    218                         vert.position += ( joint.pos + rot_pos ) * weight.bias;
    219                 }
    220 
    221                 mesh->position_buffer.push_back(vert.position);
    222                 mesh->texcoord_buffer.push_back(vert.texcoord);
    223         }
    224 
    225         return true;
    226 }
    227 
    228 bool md5_loader::prepare_normals( md5_mesh* mesh )
    229 {
    230         mesh->normal_buffer.clear();
    231 
    232         for ( unsigned int i = 0; i < mesh->tris.size(); ++i )
    233         {
    234                 const md5_triangle& tri = mesh->tris[i];
    235                 glm::vec3 v1 = mesh->verts[ tri.indices[0] ].position;
    236                 glm::vec3 v2 = mesh->verts[ tri.indices[1] ].position;
    237                 glm::vec3 v3 = mesh->verts[ tri.indices[2] ].position;
     219                        vtc.position += ( joint.pos + rot_pos ) * weight.bias;
     220                }
     221        }
     222
     223        // Prepare normals
     224        uint32 tri_count = mdata->get_count() / 3;
     225        for ( unsigned int i = 0; i < tri_count; ++i )
     226        {
     227                uint32 ti0 = mdata->m_idata[ i * 3 ];
     228                uint32 ti1 = mdata->m_idata[ i * 3 + 1 ];
     229                uint32 ti2 = mdata->m_idata[ i * 3 + 2 ];
     230 
     231                glm::vec3 v1 = vtcs[ ti0 ].position;
     232                glm::vec3 v2 = vtcs[ ti1 ].position;
     233                glm::vec3 v3 = vtcs[ ti2 ].position;
    238234                glm::vec3 xyz1 = v3 - v1;
    239235                glm::vec3 xyz2 = v2 - v1;
     
    241237                glm::vec3 normal = glm::cross( xyz1, xyz2 );
    242238
    243                 mesh->verts[ tri.indices[0] ].normal += normal;
    244                 mesh->verts[ tri.indices[1] ].normal += normal;
    245                 mesh->verts[ tri.indices[2] ].normal += normal;
    246 
    247                 const vec2& w1 = mesh->verts[ tri.indices[0] ].texcoord;
    248                 const vec2& w2 = mesh->verts[ tri.indices[1] ].texcoord;
    249                 const vec2& w3 = mesh->verts[ tri.indices[2] ].texcoord;
     239                vtcs[ ti0 ].normal += normal;
     240                vtcs[ ti1 ].normal += normal;
     241                vtcs[ ti2 ].normal += normal;
     242
     243                const vec2& w1 = mdata->m_tdata[ ti0 ].texcoord;
     244                const vec2& w2 = mdata->m_tdata[ ti1 ].texcoord;
     245                const vec2& w3 = mdata->m_tdata[ ti2 ].texcoord;
    250246
    251247                vec2 st1 = w3 - w1;
     
    256252                vec3 tangent = (( xyz1 * st2.y ) - ( xyz2 * st1.y )) * coef;
    257253
    258                 mesh->verts[ tri.indices[0] ].tangent += tangent;
    259                 mesh->verts[ tri.indices[1] ].tangent += tangent;
    260                 mesh->verts[ tri.indices[2] ].tangent += tangent;
    261         }
    262 
    263         for ( size_t i = 0; i < mesh->verts.size(); ++i )
    264         {
    265                 md5_vertex& vert = mesh->verts[i];
    266 
    267                 glm::vec3 normal  = glm::normalize( vert.normal );
    268                 glm::vec3 tangent = glm::normalize( vert.tangent );
    269                 mesh->normal_buffer.push_back( normal );
    270                 mesh->tangent_buffer.push_back( tangent );
    271 
    272                 vert.normal  = glm::vec3(0);
    273                 vert.tangent = glm::vec3(0);
    274 
    275                 for ( size_t j = 0; j < vert.weight_count; ++j )
    276                 {
    277                         const md5_weight& weight = mesh->weights[vert.start_weight + j];
    278                         const md5_joint&  joint  = m_joints[weight.joint_id];
    279                         vert.normal  += ( normal  * joint.orient ) * weight.bias;
    280                         vert.tangent += ( tangent * joint.orient ) * weight.bias;
    281                 }
     254                vtcs[ ti0 ].tangent += tangent;
     255                vtcs[ ti1 ].tangent += tangent;
     256                vtcs[ ti2 ].tangent += tangent;
     257        }
     258
     259        for ( size_t i = 0; i < vtx_count; ++i )
     260        {
     261                md5_vtx_data& vdata = mdata->m_vtx_data[i];
     262
     263                glm::vec3 normal  = glm::normalize( vtcs[i].normal );
     264                glm::vec3 tangent = glm::normalize( vtcs[i].tangent );
     265                vtcs[i].normal   = normal;
     266                vtcs[i].tangent  = tangent;
     267
     268                vdata.normal  = glm::vec3(0);
     269                vdata.tangent = glm::vec3(0);
     270 
     271                for ( size_t j = 0; j < vdata.weight_count; ++j )
     272                {
     273                        const md5_weight& weight = mdata->m_weights[vdata.start_weight + j];
     274                        const md5_joint&  joint  = m_joints[weight.joint_id];
     275                        vdata.normal  += ( normal  * joint.orient ) * weight.bias;
     276                        vdata.tangent += ( tangent * joint.orient ) * weight.bias;
     277                }
    282278        }
    283279
     
    285281}
    286282
    287 mesh_data_old* nv::md5_loader::release_submesh_data( uint32 mesh_id )
    288 {
    289         mesh_data_creator m;
    290         m.get_positions().assign( m_meshes[mesh_id]->position_buffer.begin(), m_meshes[mesh_id]->position_buffer.begin() );
    291         m.get_normals()  .assign( m_meshes[mesh_id]->normal_buffer.begin(),   m_meshes[mesh_id]->normal_buffer.begin() );
    292         m.get_tangents() .assign( m_meshes[mesh_id]->tangent_buffer.begin(),  m_meshes[mesh_id]->tangent_buffer.begin() );
    293         m.get_texcoords().assign( m_meshes[mesh_id]->texcoord_buffer.begin(), m_meshes[mesh_id]->texcoord_buffer.begin() );
    294         m.get_indices()  .assign( m_meshes[mesh_id]->index_buffer.begin(),    m_meshes[mesh_id]->index_buffer.begin() );
    295 
    296         return m.release();
    297 }
    298 
    299 /*
    300 mesh* md5_loader::release_mesh()
    301 {
    302         mesh* m = new mesh();
    303         auto position = m->add_attribute< vec3 >( "nv_position" );
    304         auto normal   = m->add_attribute< vec3 >( "nv_normal" );
    305         auto texcoord = m->add_attribute< vec2 >( "nv_texcoord" );
    306         auto tangent  = m->add_attribute< vec3 >( "nv_tangent" );
    307         auto indices  = m->add_indices< uint32 >();
    308 
    309         position->get().assign( m_meshes[0].position_buffer.begin(), m_meshes[0].position_buffer.end() );
    310         normal  ->get().assign( m_meshes[0].normal_buffer.begin(),   m_meshes[0].normal_buffer.end() );
    311         texcoord->get().assign( m_meshes[0].texcoord_buffer.begin(), m_meshes[0].texcoord_buffer.end() );
    312         tangent ->get().assign( m_meshes[0].tangent_buffer.begin(),  m_meshes[0].tangent_buffer.end() );
    313         indices ->get().assign( m_meshes[0].index_buffer.begin(),    m_meshes[0].index_buffer.end() );
    314 
    315         m_size = m_meshes[0].index_buffer.size();
    316         return m;
    317 }
    318 */
    319283
    320284md5_animation::md5_animation()
     
    570534}
    571535
    572 bool md5_loader::prepare_animated_mesh( md5_mesh* mesh, const md5_animation::md5_frame_skeleton& skel )
    573 {
    574         for ( unsigned int i = 0; i < mesh->verts.size(); ++i )
    575         {
    576                 const md5_vertex& vert = mesh->verts[i];
    577                 glm::vec3& pos     = mesh->position_buffer[i];
    578                 glm::vec3& normal  = mesh->normal_buffer[i];
    579                 glm::vec3& tangent = mesh->tangent_buffer[i];
    580 
    581                 pos     = glm::vec3(0);
    582                 normal  = glm::vec3(0);
    583                 tangent = glm::vec3(0);
     536mesh_data* nv::md5_loader::release_mesh_data( uint32 mesh )
     537{
     538        mesh_data* result = m_meshes[ mesh ];
     539        m_meshes[ mesh ] = nullptr;
     540        return result;
     541}
     542
     543void nv::md5_mesh_data::apply( const md5_animation& animation )
     544{
     545        const md5_animation::md5_frame_skeleton& skeleton = animation.get_skeleton();
     546
     547        for ( unsigned int i = 0; i < m_vtx_data.size(); ++i )
     548        {
     549                const md5_vtx_data& vert = m_vtx_data[i];
     550                md5_vtx_pnt& result = m_pntdata[i];
     551
     552                result.position = glm::vec3(0);
     553                result.normal   = glm::vec3(0);
     554                result.tangent  = glm::vec3(0);
    584555
    585556                for ( size_t j = 0; j < vert.weight_count; ++j )
    586557                {
    587                         const md5_weight& weight = mesh->weights[vert.start_weight + j];
    588                         const md5_animation::md5_skeleton_joint& joint = skel.joints[weight.joint_id];
     558                        const md5_weight& weight = m_weights[vert.start_weight + j];
     559                        const md5_animation::md5_skeleton_joint& joint = skeleton.joints[weight.joint_id];
    589560
    590561                        glm::vec3 rot_pos = joint.orient * weight.pos;
    591                         pos += ( joint.pos + rot_pos ) * weight.bias;
    592 
    593                         normal  += ( joint.orient * vert.normal  ) * weight.bias;
    594                         tangent += ( joint.orient * vert.tangent ) * weight.bias;
    595                 }
    596         }
    597         return true;
    598 }
    599 
    600 void md5_loader::apply( const md5_animation& animation )
    601 {
    602         const md5_animation::md5_frame_skeleton& skeleton = animation.get_skeleton();
    603 
    604         for ( unsigned int i = 0; i < m_meshes.size(); ++i )
    605         {
    606                 prepare_animated_mesh( m_meshes[i], skeleton );
    607         }
    608 }
    609 
    610 size_t nv::md5_loader::get_size()
    611 {
    612         return m_size;
    613 }
     562                        result.position += ( joint.pos + rot_pos ) * weight.bias;
     563
     564                        result.normal  += ( joint.orient * vert.normal  ) * weight.bias;
     565                        result.tangent += ( joint.orient * vert.tangent ) * weight.bias;
     566                }
     567        }
     568}
     569
     570nv::md5_loader::~md5_loader()
     571{
     572        for ( auto m : m_meshes ) { if (m) delete m; }
     573}
  • trunk/src/formats/obj_loader.cc

    r238 r239  
    5656        bool read_stream( std::istream& stream );
    5757        virtual size_t add_face( uint32* vi, uint32* ti, uint32* ni, size_t count ) = 0;
    58         virtual size_t raw_size() { return 0; }
    59         virtual const uint8* raw_pointer() { return nullptr; }
     58        virtual size_t raw_size() const = 0;
     59        virtual const uint8* raw_pointer() const = 0;
    6060        virtual void calculate_tangents() {}
    6161
     
    151151
    152152
    153 struct mesh_obj_reader : public obj_reader
    154 {
    155         mesh_obj_reader( mesh_data_creator* m ) : m_mesh( m ) {}
    156         virtual std::size_t add_face( uint32* vi, uint32* ti, uint32* ni, size_t count );
    157         virtual void calculate_tangents();
    158 
    159         mesh_data_creator* m_mesh;
    160 };
    161 
    162 size_t mesh_obj_reader::add_face( uint32* vi, uint32* ti, uint32* ni, size_t count )
    163 {
    164         if ( count < 3 )
    165         {
    166                 // TODO : report error?
    167                 return 0;
    168         }
    169 
    170         // TODO : support if normals not present;
    171 
    172         std::vector< vec3 >& vp = m_mesh->get_positions();
    173         std::vector< vec3 >& vn = m_mesh->get_normals();
    174         std::vector< vec2 >& vt = m_mesh->get_texcoords();
    175 
    176         std::size_t result = 0;
    177 
    178         // Simple triangulation - obj's shouldn't have more than quads anyway
    179         for ( size_t i = 2; i < count; ++i )
    180         {
    181                 result++;
    182                 vp.push_back( v[ vi[ 0 ] ] );   vt.push_back( t[ ti[ 0 ] ] );   vn.push_back( n[ ni[ 0 ] ] );
    183                 vp.push_back( v[ vi[ i-1 ] ] ); vt.push_back( t[ ti[ i-1 ] ] ); vn.push_back( n[ ni[ i-1 ] ] );
    184                 vp.push_back( v[ vi[ i ] ] );   vt.push_back( t[ ti[ i ] ] );   vn.push_back( n[ ni[ i ] ] );
    185         }
    186 
    187         return result;
    188 }
    189 
    190 // based on http://www.terathon.com/code/tangent.html
    191 void mesh_obj_reader::calculate_tangents()
    192 {
    193         const std::vector< vec3 >& vp = m_mesh->get_positions();
    194         const std::vector< vec2 >& vt = m_mesh->get_texcoords();
    195         const std::vector< vec3 >& vn = m_mesh->get_normals();
    196         std::vector< vec3 >& tg = m_mesh->get_tangents();
    197 
    198         size_t count  = vp.size();
    199         size_t tcount = count / 3;
    200 
    201         std::vector< vec3 > tan1( count );
    202         std::vector< vec3 > tan2( count );
    203         tg.resize( count );
    204 
    205         for (size_t a = 0; a < tcount; ++a )
    206         {
    207                 size_t i1 = a * 3;
    208                 size_t i2 = a * 3 + 1;
    209                 size_t i3 = a * 3 + 2;
    210 
    211                 // TODO: simplify
    212 
    213                 const vec3& v1 = vp[i1];
    214                 const vec3& v2 = vp[i2];
    215                 const vec3& v3 = vp[i3];
    216 
    217                 const vec2& w1 = vt[i1];
    218                 const vec2& w2 = vt[i2];
    219                 const vec2& w3 = vt[i3];
    220 
    221                 vec3 xyz1 = v2 - v1;
    222                 vec3 xyz2 = v3 - v1;
    223                 //vec2 st1  = w2 - w1;
    224                 //vec2 st2  = w3 - w1;
    225 
    226                 float s1 = w2.x - w1.x;
    227                 float t1 = w2.y - w1.y;
    228                 float s2 = w3.x - w1.x;
    229                 float t2 = w3.y - w1.y;
    230 
    231                 float stst = s1 * t2 - s2 * t1;
    232                 float r = 0.0f;
    233                 if (stst > 0.0f || stst < 0.0f) r = 1.0f / stst;
    234 
    235                 vec3 sdir = ( t2 * xyz1 - t1 * xyz2 ) * r;
    236                 vec3 tdir = ( s1 * xyz2 - s2 * xyz1 ) * r;
    237 
    238                 // the += below obviously doesn't make sense in this case, but I'll
    239                 // leave it here for when I move to indices
    240                 tan1[i1] += sdir;
    241                 tan1[i2] += sdir;
    242                 tan1[i3] += sdir;
    243 
    244                 // tan2 not needed anymore??
    245                 tan2[i1] += tdir;
    246                 tan2[i2] += tdir;
    247                 tan2[i3] += tdir;
    248         }
    249 
    250         for (std::size_t a = 0; a < count; ++a )
    251         {
    252                 const vec3& n = vn[a];
    253                 const vec3& t = tan1[a];
    254                 if ( ! (t.x == 0.0f && t.y == 0.0f && t.z == 0.0f) )
    255                         tg[a] = vec3( glm::normalize(t - n * glm::dot( n, t )) );
    256                         //tg[a][3] =    (glm::dot(glm::cross(n, t), tan2[a]) < 0.0f) ? -1.0f : 1.0f;
    257         }
    258 
    259 }
    260 
    261 nv::obj_loader::obj_loader( bool tangents )
    262         : m_mesh( nullptr ), m_tangents( tangents )
    263 {
    264 
    265 }
    266 
    267 nv::obj_loader::~obj_loader()
    268 {
    269         delete m_mesh;
    270 }
    271 
    272 bool nv::obj_loader::load( stream& source )
    273 {
    274         if ( m_mesh != nullptr ) delete m_mesh;
    275         mesh_data_creator creator;
    276         mesh_obj_reader reader( &creator );
    277         std_stream sstream( &source );
    278         reader.read_stream( sstream );
    279         m_size = reader.size;
    280         if ( m_tangents )
    281         {
    282                 reader.calculate_tangents();
    283         }
    284         m_mesh = creator.release();
    285         return true;
    286 }
    287153
    288154struct mesh_data_reader_vt : public obj_reader
     
    305171        }
    306172        std::vector< obj_vertex_vt > m_data;
    307         virtual size_t raw_size() { return m_data.size() * sizeof( obj_vertex_vt ); }
    308         virtual const uint8* raw_pointer() { return (const uint8*)m_data.data(); }
     173        virtual size_t raw_size() const { return m_data.size() * sizeof( obj_vertex_vt ); }
     174        virtual const uint8* raw_pointer() const { return (const uint8*)m_data.data(); }
    309175};
    310176
     
    328194        }
    329195        std::vector< obj_vertex_vtn > m_data;
    330         virtual size_t raw_size() { return m_data.size() * sizeof( obj_vertex_vtn ); }
    331         virtual const uint8* raw_pointer() { return (const uint8*)m_data.data(); }
     196        virtual size_t raw_size() const { return m_data.size() * sizeof( obj_vertex_vtn ); }
     197        virtual const uint8* raw_pointer() const { return (const uint8*)m_data.data(); }
    332198};
    333199
     
    351217        }
    352218        std::vector< obj_vertex_vtnt > m_data;
    353         virtual size_t raw_size() { return m_data.size() * sizeof( obj_vertex_vtnt ); }
    354         virtual const uint8* raw_pointer() { return (const uint8*)m_data.data(); }
     219        virtual size_t raw_size() const { return m_data.size() * sizeof( obj_vertex_vtnt ); }
     220        virtual const uint8* raw_pointer() const { return (const uint8*)m_data.data(); }
    355221
    356222        // based on http://www.terathon.com/code/tangent.html
     
    423289};
    424290
    425 nv::wavefront_loader::wavefront_loader( bool normals /*= true*/, bool tangents /*= false */ )
     291nv::obj_loader::obj_loader( bool normals /*= true*/, bool tangents /*= false */ )
    426292        : m_normals( normals ), m_tangents( tangents ), m_mesh( nullptr )
    427293{
     
    437303}
    438304
    439 bool nv::wavefront_loader::load( stream& source )
     305bool nv::obj_loader::load( stream& source )
    440306{
    441307        if ( m_mesh ) delete m_mesh;
     
    462328        mesh_raw_channel* channel = new mesh_raw_channel();
    463329        nv::uint8* data = nullptr;
     330
    464331        if ( reader->raw_size() > 0 )
    465332        {
     
    479346}
    480347
    481 mesh_data* nv::wavefront_loader::release_mesh_data()
     348mesh_data* nv::obj_loader::release_mesh_data()
    482349{
    483350        mesh_data* result = m_mesh;
     
    486353}
    487354
    488 nv::wavefront_loader::~wavefront_loader()
     355nv::obj_loader::~obj_loader()
    489356{
    490357        if ( m_mesh ) delete m_mesh;
  • trunk/src/gfx/keyframed_mesh.cc

    r236 r239  
    1515using namespace nv;
    1616
    17 keyframed_mesh::keyframed_mesh( context* a_context, mesh_data_old* a_data )
     17nv::keyframed_mesh::keyframed_mesh( context* a_context, mesh_data* a_data, tag_map* a_tag_map )
    1818        : animated_mesh()
    19         , m_data( a_data )
     19        , m_mesh_data( a_data )
     20        , m_tag_map( a_tag_map )
    2021        , m_start_frame( false )
    2122        , m_stop_frame( false )
     
    2930{
    3031        m_va = a_context->get_device()->create_vertex_array();
     32
     33        m_index_count  = m_mesh_data->get_index_channel()->count;
     34        m_vertex_count = m_mesh_data->get_channel_data()[1]->count;
     35        m_frame_count  = m_mesh_data->get_channel_data()[0]->count / m_vertex_count;
    3136}
    3237
    3338size_t keyframed_mesh::get_max_frames() const
    3439{
    35         return m_data->get_frame_count();
     40        return m_frame_count;
    3641}
    3742
    3843transform keyframed_mesh::get_tag( const std::string& tag ) const
    3944{
    40         const std::vector< transform >& transforms = m_data->get_tag_map().at( tag );
    41         return interpolate( transforms[ m_last_frame ], transforms[ m_next_frame ], m_interpolation );
     45        NV_ASSERT( m_tag_map, "TAGMAP FAIL" );
     46        const std::vector< transform >* transforms = m_tag_map->get_tag( tag );
     47        NV_ASSERT( transforms, "TAG FAIL" );
     48        return interpolate( (*transforms)[ m_last_frame ], (*transforms)[ m_next_frame ], m_interpolation );
    4249}
    4350
     
    115122}
    116123
    117 keyframed_mesh_gpu::keyframed_mesh_gpu( context* a_context, mesh_data_old* a_data, program* a_program )
    118         : keyframed_mesh( a_context, a_data )
     124nv::keyframed_mesh_gpu::keyframed_mesh_gpu( context* a_context, mesh_data* a_data, tag_map* a_tag_map, program* a_program )
     125        : keyframed_mesh( a_context, a_data, a_tag_map )
    119126        , m_loc_next_position( 0 )
    120127        , m_loc_next_normal( 0 )
     
    122129        , m_gpu_next_frame( 0xFFFFFFFF )
    123130{
    124         nv::vertex_buffer* vb;
    125131        m_loc_next_position = a_program->get_attribute( "nv_next_position" )->get_location();
    126132        m_loc_next_normal   = a_program->get_attribute( "nv_next_normal" )->get_location();
     133        m_va = a_context->get_device()->create_vertex_array( a_data, nv::STATIC_DRAW );
     134        vertex_buffer* vb = m_va->find_buffer( nv::POSITION );
     135        m_va->add_vertex_buffer( m_loc_next_position, vb, nv::FLOAT, 3, 0,              sizeof( vertex_pn ), false );
     136        m_va->add_vertex_buffer( m_loc_next_normal,   vb, nv::FLOAT, 3, sizeof( vec3 ), sizeof( vertex_pn ), false );
     137}
    127138
    128         vb = a_context->get_device()->create_vertex_buffer( nv::STATIC_DRAW, m_data->get_vertex_count() * sizeof( nv::vec3 ) * m_data->get_frame_count(), (void*)m_data->get_positions().data() );
    129         m_va->add_vertex_buffer( m_loc_next_position, vb, nv::FLOAT, 3, 0, 0, false );
    130         m_va->add_vertex_buffer( nv::POSITION, vb, nv::FLOAT, 3 );
    131 
    132         vb = a_context->get_device()->create_vertex_buffer( nv::STATIC_DRAW, m_data->get_vertex_count() * sizeof( nv::vec3 ) * m_data->get_frame_count(), (void*)m_data->get_normals().data() );
    133         m_va->add_vertex_buffer( m_loc_next_normal, vb, nv::FLOAT, 3, 0, 0, false );
    134         m_va->add_vertex_buffer( nv::NORMAL, vb, nv::FLOAT, 3 );
    135 
    136         vb = a_context->get_device()->create_vertex_buffer( nv::STATIC_DRAW, m_data->get_vertex_count() * sizeof( nv::vec2 ), (void*)m_data->get_texcoords().data() );
    137         m_va->add_vertex_buffer( nv::slot::TEXCOORD, vb, nv::FLOAT, 2 );
    138         nv::index_buffer* ib = a_context->get_device()->create_index_buffer( nv::STATIC_DRAW, m_data->get_index_count() * sizeof( nv::uint32 ), (void*)m_data->get_indices().data() );
    139         m_va->set_index_buffer( ib, nv::UINT, true );
    140 }
    141139
    142140void nv::keyframed_mesh_gpu::update( uint32 ms )
     
    144142        keyframed_mesh::update( ms );
    145143
    146         size_t vtx_count = m_data->get_vertex_count();
    147144        if ( m_gpu_last_frame != m_last_frame )
    148145        {
    149                 m_va->update_vertex_buffer( slot::POSITION, m_last_frame * vtx_count * sizeof( nv::vec3 ) );
    150                 m_va->update_vertex_buffer( slot::NORMAL,   m_last_frame * vtx_count * sizeof( nv::vec3 ) );
     146                m_va->update_vertex_buffer( slot::POSITION, m_last_frame * m_vertex_count * sizeof( vertex_pn ) );
     147                m_va->update_vertex_buffer( slot::NORMAL,   m_last_frame * m_vertex_count * sizeof( vertex_pn ) + sizeof( vec3 ) );
    151148                m_gpu_last_frame = m_last_frame;
    152149        }
    153150        if ( m_gpu_next_frame != m_next_frame )
    154151        {
    155                 m_va->update_vertex_buffer( m_loc_next_position, m_next_frame * vtx_count * sizeof( nv::vec3 ) );
    156                 m_va->update_vertex_buffer( m_loc_next_normal,   m_next_frame * vtx_count * sizeof( nv::vec3 ) );
     152                m_va->update_vertex_buffer( m_loc_next_position, m_next_frame * m_vertex_count * sizeof( vertex_pn ) );
     153                m_va->update_vertex_buffer( m_loc_next_normal,   m_next_frame * m_vertex_count * sizeof( vertex_pn ) + sizeof( vec3 ) );
    157154                m_gpu_next_frame = m_next_frame;
    158155        }
    159156}
    160157
     158nv::keyframed_mesh_cpu::keyframed_mesh_cpu( context* a_context, mesh_data* a_data, tag_map* a_tag_map )
     159        : keyframed_mesh( a_context, a_data, a_tag_map )
     160{
     161        m_vb = a_context->get_device()->create_vertex_buffer( nv::STATIC_DRAW, m_vertex_count * sizeof( vertex_pn ), (void*)m_mesh_data->get_channel_data()[0]->data );
     162        m_va->add_vertex_buffers( m_vb, m_mesh_data->get_channel_data()[0] );
    161163
    162 nv::keyframed_mesh_cpu::keyframed_mesh_cpu( context* a_context, mesh_data_old* a_data )
    163         : keyframed_mesh( a_context, a_data )
    164 {
    165         m_vb_position = a_context->get_device()->create_vertex_buffer( nv::STATIC_DRAW, m_data->get_vertex_count() * sizeof( nv::vec3 ), (void*)m_data->get_position_frame(0) );
    166         m_va->add_vertex_buffer( nv::slot::POSITION, m_vb_position, nv::FLOAT, 3 );
     164        nv::vertex_buffer* vb = a_context->get_device()->create_vertex_buffer( nv::STATIC_DRAW, m_vertex_count * sizeof( nv::vec2 ), (void*)m_mesh_data->get_channel_data()[1]->data );
     165        m_va->add_vertex_buffers( vb, m_mesh_data->get_channel_data()[1] );
    167166
    168         m_vb_normal   = a_context->get_device()->create_vertex_buffer( nv::STATIC_DRAW, m_data->get_vertex_count() * sizeof( nv::vec3 ), (void*)m_data->get_normal_frame(0) );
    169         m_va->add_vertex_buffer( nv::slot::NORMAL, m_vb_normal, nv::FLOAT, 3 );
     167        nv::index_buffer* ib = a_context->get_device()->create_index_buffer( nv::STATIC_DRAW, m_mesh_data->get_index_channel()->size, (void*)m_mesh_data->get_index_channel()->data );
     168        m_va->set_index_buffer( ib, m_mesh_data->get_index_channel()->etype, true );
    170169
    171         nv::vertex_buffer* vb;
    172         vb = a_context->get_device()->create_vertex_buffer( nv::STATIC_DRAW, m_data->get_vertex_count() * sizeof( nv::vec2 ), (void*)m_data->get_texcoords().data() );
    173         m_va->add_vertex_buffer( nv::slot::TEXCOORD, vb, nv::FLOAT, 2 );
    174 
    175         nv::index_buffer* ib = a_context->get_device()->create_index_buffer( nv::STATIC_DRAW, m_data->get_index_count() * sizeof( nv::uint32 ), (void*)m_data->get_indices().data() );
    176         m_va->set_index_buffer( ib, nv::UINT, true );
    177 
    178         m_position.resize( m_data->get_vertex_count() );
    179         m_normal.resize( m_data->get_vertex_count() );
     170        m_vertex.resize( m_vertex_count );
    180171}
    181172
     
    184175        keyframed_mesh::update( ms );
    185176
    186         size_t vtx_count = m_data->get_vertex_count();
    187         const vec3* prev_position = m_data->get_position_frame( m_last_frame );
    188         const vec3* next_position = m_data->get_position_frame( m_next_frame );
    189         const vec3* prev_normal   = m_data->get_normal_frame( m_last_frame );
    190         const vec3* next_normal   = m_data->get_normal_frame( m_next_frame );
     177        const vertex_pn* data = (const vertex_pn*)(m_mesh_data->get_channel_data()[0]->data);
     178        const vertex_pn* prev = data + m_vertex_count * m_last_frame;
     179        const vertex_pn* next = data + m_vertex_count * m_next_frame;
    191180
    192         for ( size_t i = 0; i < vtx_count; ++i )
     181        for ( size_t i = 0; i < m_vertex_count; ++i )
    193182        {
    194                 m_position[i] = glm::mix( prev_position[i], next_position[i], m_interpolation );
    195                 m_normal[i]   = glm::mix( prev_normal[i],   next_normal[i],   m_interpolation );
     183                m_vertex[i].position = glm::mix( prev[i].position, next[i].position, m_interpolation );
     184                m_vertex[i].normal   = glm::mix( prev[i].normal,   next[i].normal,   m_interpolation );
    196185        }
    197186
    198         m_vb_position->bind();
    199         m_vb_position->update( m_position.data(), 0, vtx_count * sizeof( nv::vec3 ) );
    200         m_vb_position->unbind();
    201 
    202         m_vb_normal->bind();
    203         m_vb_normal->update( m_normal.data(), 0, vtx_count * sizeof( nv::vec3 ) );
    204         m_vb_normal->unbind();
     187        m_vb->bind();
     188        m_vb->update( m_vertex.data(), 0, m_vertex_count * sizeof( vertex_pn ) );
     189        m_vb->unbind();
    205190}
  • trunk/src/gfx/skeletal_mesh.cc

    r237 r239  
    1111
    1212
    13 nv::skeletal_mesh::skeletal_mesh( context* a_context, md5_loader* a_loader )
     13nv::skeletal_mesh::skeletal_mesh( context* a_context, md5_mesh_data* a_mesh_data )
    1414        : animated_mesh()
    15         , m_data( a_loader )
     15        , m_mesh_data( a_mesh_data )
    1616        , m_animation( nullptr )
    1717{
    18         nv::uint32 vcount = a_loader->get_vertex_count(0);
    19         m_va = a_context->get_device()->create_vertex_array();
     18        m_va = a_context->get_device()->create_vertex_array( a_mesh_data, nv::STREAM_DRAW );
     19}
    2020
    21         m_vb_position = a_context->get_device()->create_vertex_buffer( nv::STREAM_DRAW, vcount * sizeof( nv::vec3 ), (const void*)a_loader->get_positions(0).data() );
    22         m_va->add_vertex_buffer( nv::slot::POSITION, m_vb_position, nv::FLOAT, 3, 0, 0, false );
    23         m_vb_normal = a_context->get_device()->create_vertex_buffer( nv::STREAM_DRAW, vcount * sizeof( nv::vec3 ), (const void*)a_loader->get_normals(0).data()  );
    24         m_va->add_vertex_buffer( nv::slot::NORMAL,   m_vb_normal, nv::FLOAT, 3, 0, 0, false );
    25         m_vb_tangent = a_context->get_device()->create_vertex_buffer( nv::STREAM_DRAW, vcount * sizeof( nv::vec3 ), (const void*)a_loader->get_tangents(0).data() );
    26         m_va->add_vertex_buffer( nv::slot::TANGENT,  m_vb_tangent, nv::FLOAT, 3, 0, 0, false );
    27 
    28         nv::vertex_buffer* vb = a_context->get_device()->create_vertex_buffer( nv::STATIC_DRAW, vcount * sizeof( nv::vec2 ), (const void*)a_loader->get_texcoords(0).data() );
    29         m_va->add_vertex_buffer( nv::slot::TEXCOORD, vb, nv::FLOAT, 2 );
    30         nv::index_buffer* ib = a_context->get_device()->create_index_buffer( nv::STATIC_DRAW, a_loader->get_index_count(0) * sizeof( nv::uint32 ), (const void*)a_loader->get_indices(0).data() );
    31         m_va->set_index_buffer( ib, nv::UINT, true );
    32 }
    3321
    3422void nv::skeletal_mesh::setup_animation( md5_animation* a_anim )
     
    4533        {
    4634                m_animation->update( ms * 0.001f );
    47                 m_data->apply( *m_animation );
     35                m_mesh_data->apply( *m_animation );
     36                vertex_buffer* vb = m_va->find_buffer( nv::POSITION );
     37                const mesh_raw_channel* pch = m_mesh_data->get_channel_data()[0];
     38                vb->bind();
     39                vb->update( (const void*)pch->data, 0, pch->size );
     40                vb->unbind();
    4841        }
    49 
    50         nv::uint32 usize = m_data->get_vertex_count(0) * sizeof( nv::vec3 );
    51         m_vb_position->bind();
    52         m_vb_position->update( (const void*)m_data->get_positions(0).data(), 0, usize );
    53         m_vb_normal  ->bind();
    54         m_vb_normal  ->update( (const void*)m_data->get_normals(0).data(),   0, usize );
    55         m_vb_tangent ->bind();
    56         m_vb_tangent ->update( (const void*)m_data->get_tangents(0).data(),  0, usize );
    57 
    58         // Technically this is not needed, because the va is just a fake class,
    59         // but if it's real it will be needed?
    60 //      m_va->update_vertex_buffer( nv::slot::POSITION, m_vb_position, false );
    61 //      m_va->update_vertex_buffer( nv::slot::NORMAL,   m_vb_normal,   false );
    62 //      m_va->update_vertex_buffer( nv::slot::TANGENT,  m_vb_tangent,  false );
    63         // TODO: answer is - probably not
    6442}
    6543
     
    6745{
    6846        delete m_va;
     47        delete m_mesh_data;
    6948}
    7049
  • trunk/src/gui/gui_environment.cc

    r234 r239  
    2222        */
    2323
    24 #include "nv/interface/mesh.hh"
    2524#include "nv/gfx/sliced_buffer.hh"
    2625#include "nv/gfx/texture_atlas.hh"
  • trunk/tests/gui_test/nv_gui_test.cc

    r229 r239  
    11#include <nv/gl/gl_device.hh>
    22#include <nv/gui/gui_environment.hh>
     3#include <nv/interface/context.hh>
    34#include <nv/logging.hh>
    45#include <nv/logger.hh>
  • trunk/tests/md2_test/md2_test.cc

    r231 r239  
    11#include <nv/common.hh>
    2 #include <iomanip>
    32#include <nv/gfx/keyframed_mesh.hh>
    43#include <nv/interface/vertex_buffer.hh>
     
    1817#include <nv/time.hh>
    1918#include <nv/string.hh>
    20 #include <nv/interface/mesh.hh>
    2119#include <glm/gtx/rotate_vector.hpp>
    22 #include <glm/gtc/matrix_access.hpp>
    23 #include <glm/gtx/matrix_interpolation.hpp>
    24 
    25 class mesh_part
    26 {
    27 public:
    28         mesh_part( const std::string& path, nv::program* program, nv::window* window )
    29         {
    30                 NV_PROFILE("mesh_construct");
    31                 nv::md2_loader* loader;
    32                 {
    33                         NV_PROFILE("loader->load");
    34                         nv::c_file_system fs;
    35                         nv::stream* mesh_file = fs.open( path.c_str() );
    36                         loader = new nv::md2_loader();
    37                         loader->load( *mesh_file );
    38                         m_mesh_data = loader->release_mesh_data();
    39                         delete mesh_file;
    40                         delete loader;
    41                 }
    42 
    43                 {
    44                         NV_PROFILE("create_mesh");
    45                         m_mesh      = new nv::keyframed_mesh_gpu( window->get_context(), m_mesh_data, program );
    46                 }
    47 
    48         }
    49 
    50         void setup_animation( nv::uint32 start, nv::uint32 stop, nv::uint32 fps, bool loop )
    51         {
    52                 m_mesh->setup_animation( start, stop, fps, loop );
    53         }
    54 
    55         void update( nv::uint32 ms )
    56         {
    57                 m_mesh->update( ms );
    58         }
    59 
    60         nv::mesh_interface* get_mesh() { return m_mesh; }
    61 
    62         size_t get_max_frames()
    63         {
    64                 return m_mesh->get_max_frames();
    65         }
    66 
    67         ~mesh_part()
    68         {
    69                 delete m_mesh_data;
    70                 delete m_mesh;
    71         }
    72 
    73 private:
    74         nv::mesh_data*       m_mesh_data;
    75         nv::keyframed_mesh*  m_mesh;
    76 };
    7720
    7821class application
     
    8730        nv::window*       m_window;
    8831        nv::texture2d*    m_diffuse;
     32        nv::program*      m_program;
     33
    8934        nv::clear_state   m_clear_state;
    9035        nv::render_state  m_render_state;
     36        nv::scene_state   m_scene_state;
    9137
    92         mesh_part*   m_mesh;
    93         nv::program* m_program;
     38        nv::mesh_data*      m_mesh_data;
     39        nv::keyframed_mesh* m_mesh;
    9440};
    9541
     
    11965        NV_PROFILE( "app_initialize" );
    12066        m_program = m_device->create_program( nv::slurp( "obj.vert" ), nv::slurp( "obj.frag" ) );
    121         m_mesh    = new mesh_part( "data/manc.md2", m_program, m_window );
     67
     68        nv::md2_loader* loader;
     69        {
     70                NV_PROFILE("loader->load");
     71                nv::c_file_system fs;
     72                nv::stream* mesh_file = fs.open( "data/manc.md2" );
     73                loader = new nv::md2_loader();
     74                loader->load( *mesh_file );
     75                m_mesh_data = loader->release_mesh_data();
     76                delete mesh_file;
     77                delete loader;
     78        }
     79
     80        {
     81                NV_PROFILE("create_mesh");
     82                m_mesh      = new nv::keyframed_mesh_gpu( m_window->get_context(), m_mesh_data, nullptr, m_program );
     83        }
     84
    12285        return true;
    12386}
     
    12891        NV_PROFILE( "app_run" );
    12992        int keypress = 0;
    130 
    131         glm::vec3 move( 0, 25.f, 0 );
    13293
    13394        nv::uint32 ticks   = m_device->get_ticks();
     
    142103                ticks      = m_device->get_ticks();
    143104                nv::uint32 elapsed = ticks - last_ticks;
     105                m_window->get_context()->clear( m_clear_state );
    144106                m_mesh->update( elapsed );
    145                 {
    146                         NV_PROFILE( "clear" );
    147                         m_window->get_context()->clear( m_clear_state );
    148                 }
     107                glm::vec3 eye = glm::rotate( glm::vec3( 100.0f, 25.0f, 0.0f ), (ticks / 20.f), glm::vec3( 0.0,1.0,0.0 ) );
    149108
    150                 glm::mat4 view;
    151                 glm::mat4 projection;
    152                 {
    153                         NV_PROFILE( "update_sh" );
     109                m_scene_state.set_model( nv::mat4(1.0f) );
     110                m_scene_state.get_camera().set_lookat(eye, glm::vec3(0.0f, 25.0f, 0.0f), glm::vec3(0.0, 1.0, 0.0));
     111                m_scene_state.get_camera().set_perspective(60.0f, 1.0f*800.0f/600.0f, 0.1f, 1000.0f);
    154112
    155                         glm::vec3 source( 100.0f, 0.0f, 0.0f );
    156                         glm::vec3 eye = glm::rotate( source, (ticks / 20.f), glm::vec3( 0.0,1.0,0.0 ) );
     113                m_diffuse->bind( 0 );
     114                m_program->set_opt_uniform( "light_position", glm::vec3(120.0, 120.0, 0) );
     115                m_program->set_opt_uniform( "light_diffuse",  glm::vec4(1.0,1.0,1.0,1.0) );
     116                m_program->set_opt_uniform( "light_specular", glm::vec4(1.0,1.0,1.0,1.0) );
     117                m_mesh->update( m_program );
     118                m_window->get_context()->draw( m_render_state, m_scene_state, m_program, m_mesh );
     119                m_window->swap_buffers();
    157120
    158                         view       = glm::lookAt(eye + move, glm::vec3(0.0f, 0.0f, 0.0f) + move, glm::vec3(0.0, 1.0, 0.0));
    159                         projection = glm::perspective(60.0f, 1.0f*800.0f/600.0f, 0.1f, 1000.0f);
    160 
    161                         m_diffuse->bind( 0 );
    162                         m_program->set_opt_uniform( "nv_m_projection", projection );
    163                         m_program->set_uniform( "light_position", glm::vec3(120.0, 120.0, 0) );
    164                         m_program->set_uniform( "light_diffuse",  glm::vec4(1.0,1.0,1.0,1.0) );
    165                         m_program->set_uniform( "light_specular", glm::vec4(1.0,1.0,1.0,1.0) );
    166                         m_program->set_uniform( "diffuse", 0 );
    167                 }
    168 
    169                 {
    170                         NV_PROFILE( "draw" );
    171                         glm::mat4 model      = glm::mat4(1.0f);
    172                         glm::mat4 mv = view * model;
    173                         m_program->set_opt_uniform( "nv_m_modelview", mv );
    174                         m_program->set_opt_uniform( "nv_m_normal", glm::transpose(glm::inverse(glm::mat3(mv))) );
    175                         m_program->set_uniform( "matrix_mvp", projection * mv );
    176                         m_mesh->get_mesh()->update( m_program );
    177                         m_window->get_context()->draw( m_render_state, m_program, m_mesh->get_mesh() );
    178                 }
    179 
    180                 {
    181                         NV_PROFILE( "swap" );
    182                         m_window->swap_buffers();
    183                 }
    184 
    185                 {
    186                         NV_PROFILE( "pollevent" );
    187                         nv::io_event event;
    188                         while(m_window->poll_event(event))
    189                         {     
    190                                 switch (event.type)
     121                nv::io_event event;
     122                while(m_window->poll_event(event))
     123                {     
     124                        switch (event.type)
     125                        {
     126                        case nv::EV_QUIT:
     127                                keypress = 1;
     128                                break;
     129                        case nv::EV_KEY:
     130                                if (event.key.pressed)
    191131                                {
    192                                 case nv::EV_QUIT:
    193                                         keypress = 1;
    194                                         break;
    195                                 case nv::EV_KEY:
    196                                         if (event.key.pressed)
     132                                        switch (event.key.code)
    197133                                        {
    198                                                 switch (event.key.code)
    199                                                 {
    200                                                 case nv::KEY_ESCAPE : keypress = 1; break;
    201                                                 case nv::KEY_F1 : nv::profiler::pointer()->log_report(); break;
    202                                                 default: break;
    203                                                 }
     134                                        case nv::KEY_ESCAPE : keypress = 1; break;
     135                                        case nv::KEY_F1 : nv::profiler::pointer()->log_report(); break;
     136                                        default: break;
    204137                                        }
    205                                         break;
    206                                 default: break;
    207138                                }
     139                                break;
     140                        default: break;
    208141                        }
    209142                }
     
    216149{
    217150        delete m_program;
     151        delete m_mesh_data;
    218152        delete m_mesh;
    219153        delete m_diffuse;
  • trunk/tests/md2_test/obj.frag

    r189 r239  
    11#version 120
    22
    3 uniform sampler2D diffuse;
     3uniform sampler2D nv_t_diffuse;
    44uniform vec4 light_diffuse;
    55uniform vec4 light_specular;
     
    99varying vec3 v_light_vector;
    1010varying vec3 v_view_vector;
    11  
     11
    1212void main(void) {
     13        vec3 diff_texel      = vec3( texture2D( nv_t_diffuse, v_texcoord ) );   
     14
    1315        vec3 nnormal         = normalize( v_normal );
    1416        vec3 nlight_vector   = normalize( v_light_vector );
    15         vec3 nview_vector    = normalize( v_view_vector );
    1617        vec3 nreflect_vector = reflect( -nlight_vector, nnormal );
    1718
    18         float specular_value = clamp( dot( nreflect_vector, nview_vector ), 0.0, 1.0 );
    19         specular_value       = pow( specular_value, 6.0 );
    20         float diffuse_value  = max( dot( nlight_vector, nnormal ), 0.0 );
     19        float specular_amount = 0.2;
     20        float diffuse_amount  = 1.0 - specular_amount;
    2121
    22         vec3 diff_texel      = vec3( texture2D( diffuse, v_texcoord ) );       
    23        
    24         float final_specular = specular_value * 0.2;
    25         float final_diffuse  = diffuse_value * 0.5;
    26         float final_ambient  = 0.3;
     22        float dot_prod_specular = dot( nreflect_vector, normalize( v_view_vector ) );
     23        float dot_prod_diffuse  = dot( nlight_vector, nnormal );
    2724
    28         vec3 ambient_color   = final_ambient * diff_texel;
    29         vec3 diffuse_color   = light_diffuse.xyz * final_diffuse * diff_texel;
     25        float diffuse_factor  = max( dot_prod_diffuse, 0.0 );
     26        float specular_factor = pow( max( dot_prod_specular, 0.0 ), 16.0 ); // 100.0
     27
     28        float final_diffuse  = diffuse_amount * diffuse_factor * 0.5;
     29        float final_specular = specular_amount * specular_factor;
     30
     31        vec3 diffuse_color   = light_diffuse.xyz  * final_diffuse * diff_texel;
    3032        vec3 specular_color  = light_specular.xyz * final_specular;
     33        vec3 self_ilum_color = vec3 (0.0, 0.0, 0.0);
     34        vec3 ambient_color   = vec3 (0.0, 0.0, 0.0);
    3135
    32         gl_FragColor = vec4( ambient_color + diffuse_color + specular_color, 1.0 );
    33 //      gl_FragColor = vec4( diff_texel, 1.0 );
     36        gl_FragColor = vec4( max( self_ilum_color, diffuse_color + specular_color + ambient_color ), 1.0 );
    3437}
  • trunk/tests/md2_test/obj.vert

    r189 r239  
    1212varying vec2 v_texcoord;
    1313
    14 uniform mat4 matrix_mvp;
    15 uniform mat4 nv_m_modelview;
    16 uniform mat4 nv_m_projection;
    17 uniform mat3 nv_m_normal;
     14uniform mat4 nv_m_mvp;
     15uniform mat4 nv_m_model;
     16uniform mat4 nv_m_model_inv;
     17uniform vec3 nv_v_camera_position;
     18uniform vec3 light_position;
    1819uniform float nv_interpolate;
    19 uniform vec3 light_position;
    2020
    21 void main(void) {
    22         vec4 vertex     = vec4( mix( nv_position, nv_next_position, nv_interpolate ), 1.0 );
    23         vec3 eye_pos    = vec3( nv_m_modelview * vertex );
    24         v_normal        = normalize( nv_m_normal * mix( nv_normal, nv_next_normal, nv_interpolate ) );
    25         v_light_vector  = vec3( normalize( light_position - eye_pos ) );
    26         v_view_vector   = vec3( normalize( -eye_pos ) );
     21void main(void)
     22{
     23        vec4 position   = vec4( mix( nv_position, nv_next_position, nv_interpolate ), 1.0 );
     24        v_normal        = normalize( mix( nv_normal, nv_next_normal, nv_interpolate ) );
     25        v_texcoord      = nv_texcoord;
     26        gl_Position     = nv_m_mvp * position;
    2727
    28         v_texcoord      = nv_texcoord;
    29         gl_Position     = matrix_mvp * vertex;
     28        vec3 camera_loc = vec3(nv_m_model_inv * vec4 (nv_v_camera_position, 1.0) );
     29        vec3 light_loc  = vec3(nv_m_model_inv * vec4 (light_position, 1.0) );
     30
     31        v_view_vector  = normalize( nv_position - camera_loc  );
     32        v_light_vector = normalize( nv_position - light_loc );
    3033}
  • trunk/tests/md3_test/md3_test.cc

    r231 r239  
    1818#include <nv/time.hh>
    1919#include <nv/string.hh>
    20 #include <nv/interface/mesh.hh>
    2120#include <glm/gtx/rotate_vector.hpp>
    2221#include <glm/gtc/matrix_access.hpp>
     
    4342                {
    4443                        NV_PROFILE("create_mesh_data");
    45                         m_data      = loader->release_mesh_data();
     44                        m_mesh_data = loader->release_mesh_data();
     45                        m_tag_map   = loader->create_tag_map();
    4646                }
    4747                delete loader;
     
    5050                        NV_PROFILE("create_mesh");
    5151                        if ( GPU_ANIMATION )
    52                                 m_mesh      = new nv::keyframed_mesh_gpu( window->get_context(), m_data, program );
     52                                m_mesh      = new nv::keyframed_mesh_gpu( window->get_context(), m_mesh_data, m_tag_map, program );
    5353                        else
    54                                 m_mesh      = new nv::keyframed_mesh_cpu( window->get_context(), m_data );
     54                                m_mesh      = new nv::keyframed_mesh_cpu( window->get_context(), m_mesh_data, m_tag_map );
    5555                }
    5656
     
    6767        }
    6868
    69         void update( nv::uint32 ms )
     69        void update( nv::uint32 ms, nv::program* program )
    7070        {
    7171                m_mesh->update( ms );
     72                m_mesh->update( program );
    7273        }
    7374
     
    7980        }
    8081
    81         void draw( nv::context* c, nv::program* program, nv::render_state& rstate, const glm::mat4& m, const glm::mat4& v, const glm::mat4& p )
    82         {
    83                 NV_PROFILE( "mesh-draw" );
    84                 glm::mat4 mv = v * m;
    85                 program->set_opt_uniform( "nv_m_modelview", mv );
    86                 program->set_opt_uniform( "nv_m_normal", glm::transpose(glm::inverse(glm::mat3(mv))) );
    87                 program->set_uniform( "matrix_mvp", p * mv );
    88                
    89                 c->draw( rstate, program, m_mesh );
    90         }
    91 
    9282        ~mesh_part()
    9383        {
    94                 delete m_data;
     84                delete m_tag_map;
     85                delete m_mesh_data;
    9586                delete m_mesh;
    9687        }
    9788
    9889private:
    99         nv::mesh_data*           m_data;
    100         nv::keyframed_mesh*      m_mesh;
     90        nv::tag_map*        m_tag_map;
     91        nv::mesh_data*      m_mesh_data;
     92        nv::keyframed_mesh* m_mesh;
    10193};
    10294
     
    113105        nv::texture2d*    m_diffuse;
    114106        nv::texture2d*    m_diffuse_weapon;
     107
    115108        nv::clear_state   m_clear_state;
    116109        nv::render_state  m_render_state;
     110        nv::scene_state   m_scene_state;
    117111
    118112        mesh_part* m_torso;
     
    120114        mesh_part* m_head;
    121115        mesh_part* m_weapon;
    122         nv::program*      m_program;
     116        nv::program* m_program;
    123117};
    124118
     
    182176                ticks      = m_device->get_ticks();
    183177                nv::uint32 elapsed = ticks - last_ticks;
    184                 m_torso->update( elapsed );
    185                 m_legs->update( elapsed );
     178                m_torso->update( elapsed, m_program );
     179                m_legs->update( elapsed, m_program );
    186180                {
    187181                        NV_PROFILE( "clear" );
     
    197191                        glm::vec3 eye = glm::rotate( source, (ticks / 20.f), glm::vec3( 0.0,1.0,0.0 ) );
    198192
    199                         view       = glm::lookAt(eye + move, glm::vec3(0.0f, 0.0f, 0.0f) + move, glm::vec3(0.0, 1.0, 0.0));
    200                         projection = glm::perspective(60.0f, 1.0f*800.0f/600.0f, 0.1f, 1000.0f);
     193                        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));
     194                        m_scene_state.get_camera().set_perspective(60.0f, 1.0f*800.0f/600.0f, 0.1f, 1000.0f);
    201195
    202196                        m_diffuse->bind( 0 );
    203                         m_program->set_opt_uniform( "nv_m_projection", projection );
    204197                        m_program->set_uniform( "light_position", glm::vec3(120.0, 120.0, 0) );
    205198                        m_program->set_uniform( "light_diffuse",  glm::vec4(1.0,1.0,1.0,1.0) );
    206199                        m_program->set_uniform( "light_specular", glm::vec4(1.0,1.0,1.0,1.0) );
    207                         m_program->set_uniform( "diffuse", 0 );
    208200                }
    209201
     
    211203                        NV_PROFILE( "draw" );
    212204                        glm::mat4 model      = glm::mat4(1.0f);
    213                         m_legs->draw( m_window->get_context(), m_program, m_render_state, model, view, projection );
     205
     206                        m_scene_state.set_model( model );
     207                        m_window->get_context()->draw( m_render_state, m_scene_state, m_program, m_legs->get_mesh() );
    214208
    215209                        //model = m_legs->get_transform( "tag_torso", last_legs_frame, legs_frame, legs_interpolate );
    216210                        model = m_legs->get_transform( "tag_torso" );
    217                         m_torso->draw( m_window->get_context(), m_program, m_render_state, model, view, projection );
     211                        m_scene_state.set_model( model );
     212                        m_window->get_context()->draw( m_render_state, m_scene_state, m_program, m_torso->get_mesh() );
    218213
    219214                        glm::mat4 head = model * m_torso->get_transform( "tag_head" ); //, last_torso_frame, torso_frame, torso_interpolate );
    220                         m_head->draw( m_window->get_context(), m_program, m_render_state, head, view, projection );
     215                        m_scene_state.set_model( head );
     216                        m_window->get_context()->draw( m_render_state, m_scene_state, m_program, m_head->get_mesh() );
    221217
    222218                        glm::mat4 weapon = model * m_torso->get_transform( "tag_weapon" ); //, last_torso_frame, torso_frame, torso_interpolate );
     219                        m_scene_state.set_model( weapon );
    223220                        m_diffuse_weapon->bind( 0 );
    224                         m_weapon->draw( m_window->get_context(), m_program, m_render_state, weapon, view, projection );
     221                        m_window->get_context()->draw( m_render_state, m_scene_state, m_program, m_weapon->get_mesh() );
    225222
    226223                }
  • 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{
  • trunk/tests/objload_test/objload_test.cc

    r238 r239  
    1313#include <nv/math.hh>
    1414#include <nv/string.hh>
    15 #include <nv/interface/mesh.hh>
    1615
    1716class application
     
    7170//              delete loader;
    7271
    73         nv::wavefront_loader* loader = new nv::wavefront_loader();
     72        nv::obj_loader* loader = new nv::obj_loader();
    7473        loader->load( *mesh_file );
    7574        m_mesh = loader->release_mesh_data();
  • trunk/tests/render_test/rl.cc

    r238 r239  
    1010#include <nv/math.hh>
    1111#include <nv/string.hh>
    12 #include <nv/interface/mesh.hh>
    1312
    1413const nv::uint16 size_x  = 16;
Note: See TracChangeset for help on using the changeset viewer.