Ignore:
Timestamp:
07/24/15 12:52:05 (10 years ago)
Author:
epyon
Message:
  • more std removal - only <random> left
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/formats/obj_loader.cc

    r433 r442  
    66
    77#include "nv/formats/obj_loader.hh"
    8 #include "nv/io/std_stream.hh"
    98#include "nv/interface/data_channel_access.hh"
    109
    11 #include <sstream>
     10#include <cstdio>
    1211
    1312using namespace nv;
     
    5049        vector< vec2 > t;
    5150
    52         std::string line;
    53         std::string cmd;
    54         std::string name;
    55         std::string next_name;
     51        string32 name;
     52        string32 next_name;
    5653
    5754        nv::size_t size;
     
    5956
    6057        obj_reader();
    61         bool read_stream( std::istream& stream );
     58        bool read_stream( stream& str );
    6259        virtual nv::size_t add_face( uint32* vi, uint32* ti, uint32* ni, nv::size_t count ) = 0;
    6360        virtual nv::size_t raw_size() const = 0;
     
    7976}
    8077
    81 bool obj_reader::read_stream( std::istream& stream )
    82 {
    83         name = next_name;
     78bool obj_reader::read_stream( stream& str )
     79{
     80        name.assign( next_name );
    8481        bool added_faces = false;
    8582        f32 x, y, z;
    8683        if ( eof ) return false;
    87 
    88         while ( std::getline( stream, line ) )
    89         {
    90                 if ( line.length() < 3 || line[0] == '#' )
    91                 {
    92                         continue;
    93                 }
    94 
    95                 std::istringstream ss(line);
    96                 ss >> cmd;
    97 
    98                 if ( cmd == "v" )
    99                 {
    100                         ss >> x >> y >> z;
     84        char buffer[256];
     85        while ( str.gets( buffer, 256 ) )
     86        {
     87                string_view cline( static_cast<const char*>( buffer ) );
     88                cline = nv::trimmed( cline );
     89
     90                if ( cline.length() < 3 || cline[0] == '#' )
     91                {
     92                        continue;
     93                }
     94
     95                if ( cline.starts_with( "vn " ) )
     96                {
     97                        sscanf( cline.data(), "vn %f %f %f", &x, &y, &z );
     98                        n.push_back( vec3( x, y, z ) );
     99                        continue;
     100                }
     101
     102                if ( cline.starts_with( "vt " ) )
     103                {
     104                        sscanf( cline.data(), "vt %f %f", &x, &y );
     105                        t.push_back( vec2( x, 1.0f - y ) );
     106                        continue;
     107                }
     108
     109                if ( cline.starts_with( "v " ) )
     110                {
     111                        sscanf( cline.data(), "v %f %f %f", &x, &y, &z );
    101112                        v.push_back( vec3( x, y, z ) );
    102113                        continue;
    103114                }
    104115
    105                 if ( cmd == "vn" )
    106                 {
    107                         ss >> x >> y >> z;
    108                         n.push_back( vec3( x, y, z ) );
    109                         continue;
    110                 }
    111 
    112                 if ( cmd == "vt" )
    113                 {
    114                         ss >> x >> y;
    115                         t.push_back( vec2( x, 1.0f - y ) );
    116                         continue;
    117                 }
    118 
    119                 if ( cmd == "f" )
     116                if ( cline.starts_with( "f " ) )
    120117                {
    121118                        added_faces = true;
    122                         ss >> cmd;
    123119
    124120                        uint32 vis[8];
     
    128124                        uint32 count = 0;
    129125
    130                         while ( !ss.fail() )
     126                        string_view scan( cline );
     127                        scan.remove_prefix( 2 );
     128                        size_t pos = 0;
     129                        while ( pos != string_view::npos )
    131130                        {
    132                                 char ch;
    133 
    134                                 std::istringstream ss2( cmd );
    135                                 ss2 >> vis[count] >> ch;
    136                                 ss2 >> tis[count] >> ch;
    137                                 if ( ch == '/')
     131                                scan.remove_prefix( pos );
     132                                scan = nv::trimmed( scan );
     133                                if ( sscanf( scan.data(), "%u/%u/%u", &vis[count], &tis[count], &nis[count] ) == 3 )
    138134                                {
    139135                                        normals = true;
    140                                         ss2 >> nis[count];
    141136                                }
    142 
    143                                 ss >> cmd;
     137                                else if ( !normals && sscanf( scan.data(), "%u/%u", &vis[count], &tis[count] ) == 2 )
     138                                {
     139
     140                                }
     141                                else
     142                                {
     143                                        break;
     144                                }
    144145                                count++;
     146                                pos = scan.find_first_of( "\t " );
    145147                        }
    146148
     
    149151                }
    150152
    151                 if ( cmd == "g" )
    152                 {
    153                         ss >> next_name;
     153                if ( cline.starts_with( "g " ) )
     154                {
     155                        next_name.assign( nv::trimmed( cline.without_prefix( 2 ) ) );
    154156                        if (added_faces)
    155157                                return true;
    156                         name = next_name;
    157                         continue;
    158                 }
    159 
    160                 if ( cmd == "s" )
     158                        name.assign( next_name );
     159                        continue;
     160                }
     161
     162                if ( cline.starts_with( "s " ) )
    161163                {
    162164                        continue;
     
    317319        else
    318320                reader = new mesh_data_reader_vt();
    319         std_stream sstream( &source );
    320 
    321         while ( reader->read_stream( sstream ) )
     321        while ( reader->read_stream( source ) )
    322322        {
    323323                if ( m_tangents )
     
    328328                data_channel_set* result = data_channel_set_creator::create_set( 1 );
    329329                data_channel_set_creator raccess( result );
    330                 raccess.set_name( make_name( reader->name.c_str() ) );
     330                raccess.set_name( make_name( reader->name ) );
    331331                uint8* rdata = raccess.add_channel( m_descriptor, reader->size * 3 ).raw_data();
    332332
Note: See TracChangeset for help on using the changeset viewer.