Changeset 292 for trunk


Ignore:
Timestamp:
07/26/14 04:25:23 (11 years ago)
Author:
epyon
Message:
  • nmd_loader now (temporarily) holds nmd dump code
  • image_data now holds pixel format information
  • minor cleanups
Location:
trunk
Files:
12 edited

Legend:

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

    r291 r292  
    9191        };
    9292
     93        // HACK : TEMPORARY - will go to it's own file, probably nmd_io
     94        void nmd_dump( const nv::mesh_data_pack* model, stream& stream_out );
     95
    9396}
    9497
  • trunk/nv/gl/gl_device.hh

    r245 r292  
    2828                virtual vertex_array* create_vertex_array();
    2929                virtual image_data* create_image_data( const std::string& filename ); // temporary
    30                 virtual texture2d* create_texture2d( ivec2 size, image_format aformat, datatype adatatype, sampler asampler, void* data = nullptr );
     30                virtual texture2d* create_texture2d( ivec2 size, pixel_format aformat, datatype adatatype, sampler asampler, void* data = nullptr );
    3131                virtual uint32 get_ticks();
    3232                virtual void delay( uint32 ms );
  • trunk/nv/gl/gl_enum.hh

    r233 r292  
    3333        unsigned int stencil_operation_to_enum( stencil_test_face::operation type );
    3434        unsigned int buffer_hint_to_enum( buffer_hint hint );
    35         unsigned int image_format_to_enum( image_format format );
     35        unsigned int image_format_to_enum( pixel_format format );
    3636        unsigned int sampler_filter_to_enum( sampler::filter filter );
    3737        unsigned int sampler_wrap_to_enum( sampler::wrap wrap );
  • trunk/nv/gl/gl_texture2d.hh

    r121 r292  
    2222        {
    2323        public:
    24                 gl_texture2d( ivec2 size, image_format aformat, datatype adatatype, sampler asampler, void* data = nullptr );
     24                gl_texture2d( ivec2 size, pixel_format aformat, datatype adatatype, sampler asampler, void* data = nullptr );
    2525                virtual void assign( void* data );
    2626                virtual void bind( size_t slot = 0 );
  • trunk/nv/interface/device.hh

    r287 r292  
    3535                virtual vertex_array* create_vertex_array() = 0;
    3636                virtual image_data* create_image_data( const std::string& filename ) = 0; // temporary
    37                 virtual texture2d* create_texture2d( ivec2 size, image_format aformat, datatype adatatype, sampler asampler, void* data = nullptr ) = 0;
     37                virtual texture2d* create_texture2d( ivec2 size, pixel_format aformat, datatype adatatype, sampler asampler, void* data = nullptr ) = 0;
    3838                virtual uint32 get_ticks() = 0;
    3939                virtual void delay( uint32 ms ) = 0;
     40
     41                texture2d* create_texture2d( ivec2 size, image_format aformat, sampler asampler, void* data = nullptr )
     42                {
     43                        return create_texture2d( size, aformat.format, aformat.type, asampler, data );
     44                }
     45                virtual texture2d* create_texture2d( image_data* data, sampler asampler )
     46                {
     47                        return create_texture2d( data->get_size(), data->get_format(), asampler, (void*)data->get_data() );
     48                }
    4049
    4150                template < typename VTX, slot SLOT >
  • trunk/nv/interface/image_data.hh

    r121 r292  
    2020namespace nv
    2121{
     22        enum pixel_format
     23        {
     24                RGB,
     25                RGBA
     26        };
     27       
     28        struct image_format
     29        {
     30                pixel_format format;
     31                datatype     type;
     32
     33                image_format( pixel_format f = RGB, datatype d = UBYTE )
     34                        : format( f ), type( d )  {}
     35        };
     36
    2237        class image_data
    2338        {
    2439        public:
    25                 image_data( ivec2 size, size_t depth, const uint8 * data )
    26                         : m_size( size ), m_depth( depth ), m_data( nullptr )
    27                 {
    28                         std::size_t bsize = static_cast<std::size_t>(m_size.x * m_size.y) * m_depth;
     40                image_data( image_format format, ivec2 size, const uint8 * data )
     41                        : m_format( format ), m_size( size ), m_data( nullptr ) { initialize( data ); }
     42                uint8* release_data() { uint8* r = m_data; m_data = nullptr; return r; }
     43                const uint8 * get_data()    const { return m_data; }
     44                const ivec2 get_size() const { return m_size; }
     45                // TODO : better depth check (template?)
     46                size_t get_depth()    const { return m_format.format == RGB ? 3 : 4; }
     47                image_format get_format() const { return m_format; }
     48                ~image_data() { if (m_data) delete[] m_data; }
     49        private:
     50                void initialize( const uint8* data )
     51                {
     52                        std::size_t bsize = static_cast<std::size_t>(m_size.x * m_size.y) * get_depth();
    2953                        m_data = new uint8[ bsize ];
    3054                        std::copy( data, data + bsize, m_data );
    3155                }
    32                 uint8* release_data() { uint8* r = m_data; m_data = nullptr; return r; }
    33                 const uint8 * get_data()    const { return m_data; }
    34                 const ivec2 get_size() const { return m_size; }
    35                 size_t get_depth()    const { return m_depth; }
    36                 ~image_data() { if (m_data) delete[] m_data; }
    37         private:
     56
     57                image_format m_format;
    3858                ivec2  m_size;  //!< Defines the size of the image as a vector
    39                 size_t m_depth; //!< Defines the depth of the image
    4059                uint8* m_data;  //!< Holder for data
    4160        };
  • trunk/nv/interface/texture2d.hh

    r214 r292  
    1515#include <nv/common.hh>
    1616#include <nv/math.hh>
     17#include <nv/interface/image_data.hh>
    1718
    1819namespace nv
     
    5051        };
    5152
    52         enum image_format
    53         {
    54                 RGB,
    55                 RGBA
    56         };
    57 
    5853        class texture2d
    5954        {
    6055        public:
    61 
    62                 texture2d( ivec2 size, image_format aformat, datatype adatatype, sampler asampler ) :
    63                         m_size( size ), m_format( aformat ), m_datatype( adatatype ), m_sampler( asampler ) {}
     56                texture2d( ivec2 size, pixel_format aformat, datatype adatatype, sampler asampler ) :
     57                        m_size( size ), m_format( aformat, adatatype ), m_sampler( asampler ) {}
    6458                virtual void assign( void* data ) = 0;
    6559                virtual void bind( size_t slot = 0 ) = 0;
     
    7064                int get_height() const { return m_size.y; }
    7165                image_format get_format() const { return m_format; }
    72                 datatype get_datatype() const { return m_datatype; }
     66                image_format get_pixel_format() const { return m_format.format; }
     67                datatype get_type() const { return m_format.type; }
    7368                const sampler& get_sampler() const { return m_sampler; }
    7469                virtual ~texture2d() {}
     
    7671                ivec2        m_size;
    7772                image_format m_format;
    78                 datatype     m_datatype;
    7973                sampler      m_sampler;
    8074        };
  • trunk/src/formats/assimp_loader.cc

    r291 r292  
    6363bool nv::assimp_loader::load( stream& source )
    6464{
     65        load_assimp_library();
    6566        if ( m_scene != nullptr ) aiReleaseImport( (const aiScene*)m_scene );
    6667        m_scene = nullptr;
    6768        m_mesh_count = 0;
    68         load_assimp_library();
    6969        NV_LOG( nv::LOG_NOTICE, "AssImp loading file..." );
    7070        int size = (int)source.size();
  • trunk/src/formats/nmd_loader.cc

    r291 r292  
    157157}
    158158
     159// ----------------------------------------------------------------
     160// nmd format dump
     161// HACK : TEMPORARY - will go to it's own file, probably nmd_io
     162static void nmd_dump_mesh( const mesh_data* mesh, stream& stream_out )
     163{
     164        const std::vector< mesh_raw_channel* >& data  = mesh->get_raw_channels();
     165
     166        uint32 size = sizeof( nmd_element_header );
     167        for ( auto chan : data )
     168        {
     169                size += sizeof( nmd_element_header ) + sizeof( nmd_stream_header );
     170                size += chan->size();
     171        }
     172
     173        nmd_element_header eheader;
     174        eheader.type     = nmd_type::MESH;
     175        eheader.name     = 0;
     176        eheader.children = (uint16)data.size();
     177        eheader.size     = size;
     178        stream_out.write( &eheader, sizeof( eheader ), 1 );
     179
     180        for ( auto chan : data )
     181        {
     182                nmd_element_header cheader;
     183                eheader.name     = 0;
     184                cheader.type     = nmd_type::STREAM;
     185                cheader.children = 0;
     186                cheader.size     = chan->size() + sizeof( nmd_stream_header );
     187                stream_out.write( &cheader, sizeof( cheader ), 1 );
     188
     189                nmd_stream_header sheader;
     190                sheader.format = chan->desc;
     191                sheader.count  = chan->count;
     192                stream_out.write( &sheader, sizeof( sheader ), 1 );
     193                stream_out.write( chan->data, chan->desc.size, chan->count );
     194        }
     195}
     196
     197static void nmd_dump_nodes_data( const mesh_nodes_data* nodes, stream& stream_out, string_table_creator* strings )
     198{
     199        uint32 total = sizeof( nmd_animation_header );
     200        for ( uint32 i = 0; i < nodes->get_count(); ++i )
     201        {
     202                const mesh_node_data* node = nodes->get_node(i);
     203                total += sizeof( nmd_element_header ) + sizeof( nmd_node_header );
     204                if ( node->data )
     205                        for ( uint32 c = 0; c < node->data->get_channel_count(); ++c )
     206                        {
     207                                total += sizeof( nmd_element_header ) + sizeof( nmd_key_channel_header );
     208                                total += node->data->get_channel(c)->size();
     209                        }
     210        }
     211
     212        nmd_element_header header;
     213        header.name     = 0;
     214        header.type     = nmd_type::ANIMATION;
     215        header.children = (uint16)nodes->get_count();
     216        header.size     = total;
     217        stream_out.write( &header, sizeof( header ), 1 );
     218
     219        nmd_animation_header aheader;
     220        aheader.frame_rate  = nodes->get_frame_rate();
     221        aheader.duration    = nodes->get_duration();
     222        aheader.flat        = nodes->is_flat();
     223        stream_out.write( &aheader, sizeof( aheader ), 1 );
     224
     225        for ( uint32 i = 0; i < nodes->get_count(); ++i )
     226        {
     227                const mesh_node_data* node = nodes->get_node(i);
     228                uint32 chan_size  = 0;
     229                uint32 chan_count = ( node->data ? node->data->get_channel_count() : 0 );
     230                for ( uint32 c = 0; c < chan_count; ++c )
     231                {
     232                        chan_size += sizeof( nmd_element_header ) + sizeof( nv::nmd_key_channel_header );
     233                        chan_size += node->data->get_channel(c)->size();
     234                }
     235
     236                nmd_element_header eheader;
     237                eheader.type     = nmd_type::NODE;
     238                eheader.name     = strings->insert( node->name );
     239                eheader.children = (uint16)chan_count;
     240                eheader.size     = sizeof( nmd_node_header ) + chan_size;
     241                stream_out.write( &eheader, sizeof( eheader ), 1 );
     242
     243                nmd_node_header nheader;
     244                nheader.parent_id = (uint16)node->parent_id;
     245                nheader.transform = node->transform;
     246                stream_out.write( &nheader, sizeof( nheader ), 1 );
     247
     248                for ( uint32 c = 0; c < chan_count; ++c )
     249                {
     250                        const key_raw_channel* channel = node->data->get_channel(c);
     251
     252                        eheader.type     = nmd_type::KEY_CHANNEL;
     253                        eheader.children = 0;
     254                        eheader.size     = sizeof( nmd_key_channel_header ) + channel->size();
     255                        stream_out.write( &eheader, sizeof( eheader ), 1 );
     256
     257                        nmd_key_channel_header cheader;
     258                        cheader.format    = channel->desc;
     259                        cheader.count     = channel->count;
     260                        stream_out.write( &cheader, sizeof( cheader ), 1 );
     261                        stream_out.write( channel->data, channel->desc.size, channel->count );
     262                }
     263        }
     264}
     265
     266void nv::nmd_dump( const mesh_data_pack* model, stream& stream_out )
     267{
     268        string_table_creator strings;
     269        {
     270                nmd_header header;
     271                header.id       = four_cc<'n','m','f','1'>::value;
     272                header.elements = 1; // +1 string array
     273                header.elements += model->get_count();
     274                if ( model->get_nodes() && model->get_nodes()->get_count() > 0 )
     275                        header.elements += 1;//  +1 bone array
     276                header.version  = 1;
     277                stream_out.write( &header, sizeof( header ), 1 );
     278        }
     279
     280        for ( uint32 i = 0; i < model->get_count(); ++i )
     281        {
     282                const mesh_data* mesh = model->get_mesh(i);
     283                nmd_dump_mesh( mesh, stream_out );
     284        }
     285
     286        if ( model->get_nodes() && model->get_nodes()->get_count() > 0 )
     287        {
     288                nmd_dump_nodes_data( model->get_nodes(), stream_out, &strings );
     289        }
     290
     291        nmd_element_header sheader;
     292        sheader.type     = nv::nmd_type::STRING_TABLE;
     293        sheader.name     = 0;
     294        sheader.size     = strings.dump_size();
     295        sheader.children = 0;
     296        stream_out.write( &sheader, sizeof( sheader ), 1 );
     297        strings.dump( &stream_out );
     298}
  • trunk/src/gl/gl_device.cc

    r245 r292  
    8181                return nullptr;
    8282        }
    83         image_data* data = new image_data( glm::ivec2( image->w, image->h ), image->format->BytesPerPixel, (nv::uint8*)image->pixels );
     83        // TODO: BGR vs RGB, single channel
     84        assert( image->format->BytesPerPixel > 2 );
     85        image_format format(image->format->BytesPerPixel == 3 ? RGB : RGBA, UBYTE );
     86        image_data* data = new image_data( format, glm::ivec2( image->w, image->h ), (nv::uint8*)image->pixels );
    8487        return data;
    8588}
    8689
    87 texture2d* gl_device::create_texture2d( ivec2 size, image_format aformat, datatype adatatype, sampler asampler, void* data /*= nullptr */ )
     90texture2d* gl_device::create_texture2d( ivec2 size, pixel_format aformat, datatype adatatype, sampler asampler, void* data /*= nullptr */ )
    8891{
    8992        return new gl_texture2d( size, aformat, adatatype, asampler, data );
  • trunk/src/gl/gl_enum.cc

    r237 r292  
    147147}
    148148
    149 unsigned int nv::image_format_to_enum( image_format format )
     149unsigned int nv::image_format_to_enum( pixel_format format )
    150150{
    151151        switch( format )
  • trunk/src/gl/gl_texture2d.cc

    r204 r292  
    1010using namespace nv;
    1111
    12 nv::gl_texture2d::gl_texture2d( ivec2 size, image_format aformat, datatype adatatype, sampler asampler, void* data /*= nullptr */ )
     12nv::gl_texture2d::gl_texture2d( ivec2 size, pixel_format aformat, datatype adatatype, sampler asampler, void* data /*= nullptr */ )
    1313        : texture2d( size, aformat, adatatype, asampler ), m_name()
    1414{
     
    3838{
    3939        glBindTexture( GL_TEXTURE_2D, m_name.get_value() );
    40         glTexImage2D( GL_TEXTURE_2D, 0, (GLint)nv::image_format_to_enum(m_format), m_size.x, m_size.y, 0, nv::image_format_to_enum(m_format), nv::datatype_to_gl_enum(m_datatype), data );
     40        glTexImage2D( GL_TEXTURE_2D, 0, (GLint)nv::image_format_to_enum(m_format.format), m_size.x, m_size.y, 0, nv::image_format_to_enum(m_format.format), nv::datatype_to_gl_enum(m_format.type), data );
    4141        glBindTexture( GL_TEXTURE_2D, 0 );
    4242}
Note: See TracChangeset for help on using the changeset viewer.