~tomprogs/gewoopi/trunk

« back to all changes in this revision

Viewing changes to src/scene/mesh_loader/loader_obj.cpp

  • Committer: Thomas Geymayer
  • Date: 2010-03-26 11:56:05 UTC
  • Revision ID: tomgey@gmail.com-20100326115605-iir4j91614620ij3
Fixed .obj loading, enable grabbing and general cleanup.

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
#include <boost/bind.hpp>
24
24
#include <boost/function.hpp>
25
25
#include <boost/spirit/include/classic_core.hpp>
 
26
#include <boost/spirit/include/classic_error_handling.hpp>
 
27
 
26
28
 
27
29
namespace gewoopi
28
30
{
46
48
  {
47
49
    LOG_STR_EVENT_3("Loading model: " + file.string() );
48
50
 
49
 
    iterator_t file_it( file.string() );
50
 
 
51
 
    if( !file_it )
52
 
      throw std::runtime_error("Failed to open " + file.string() );
53
 
 
54
51
    // reset parse variables
55
52
    _index_position = 0;
56
53
    _index_normal   = 0;
61
58
    _normals.clear();
62
59
    _vertices.clear();
63
60
 
 
61
    //--------------------------------------------------------------------------
 
62
 
64
63
    using namespace boost::spirit::classic;
65
64
    using boost::bind;
66
65
 
92
91
             // texture coord and normal are optional
93
92
             >> !(  '/'
94
93
                 >> !int_p // ignore texture coord for the moment
95
 
                 >> '/'
96
 
                 >> !int_p[ bind(&LoaderOBJ::addNormalIndex, this, _1) ]
 
94
                 >> !( '/'
 
95
                       >> !int_p[ bind(&LoaderOBJ::addNormalIndex, this, _1) ]
 
96
                     )
97
97
                 )
98
98
             )
99
99
      ;
110
110
      =  ch_p('#') >> *(anychar_p - eol_p)
111
111
      ;
112
112
    static rule_t const unknow
113
 
      =  +(anychar_p - eol_p)
 
113
      =  /*( str_p("vt") | str_p("usemtl") | ch_p('s') | ch_p('o') )
 
114
         >>*/ +(anychar_p - eol_p)
114
115
      ;
115
116
    static rule_t const line
116
117
      =  (  vertex_position[ bind(&LoaderOBJ::addPosition, this, _1, _2) ]
118
119
         |  face           [ bind(&LoaderOBJ::addFace,     this, _1, _2) ]
119
120
         |  mtllib
120
121
         |  comment
121
 
         |  unknow
 
122
         |  unknow         [ bind(&LoaderOBJ::printUnknown,this, _1, _2) ]
122
123
         )
123
124
         >> spaces >> *eol_p
124
125
         ;
125
126
 
126
 
    parse_info<iterator_t> info
127
 
      = parse( file_it, file_it.make_end(), *line );
128
 
 
129
 
    if( !info.full )
130
 
      throw std::runtime_error(
131
 
        "Failed to load modell (" + file.string() + ")" );
 
127
    //--------------------------------------------------------------------------
 
128
 
 
129
    core::vfs::ifstream file_stream(file);
 
130
 
 
131
    if( !file_stream )
 
132
    {
 
133
      LOG_FMT_WARNING("Failed to open %1%", file.string() );
 
134
      return MeshPtr();
 
135
    }
 
136
 
 
137
    core::string line_str;
 
138
 
 
139
    while( std::getline(file_stream, line_str).good() )
 
140
    {
 
141
      parse_info<iterator_t> info
 
142
        = parse( line_str.begin(), line_str.end(), line );
 
143
 
 
144
      if( !(info.hit && info.full) )
 
145
        LOG_FMT_WARNING_1("Failed to parse line (%1%)", line_str);
 
146
    }
132
147
 
133
148
    // copy vertices to an VertexArrayPtr for the Mesh class
134
149
    VertexArray vert_array( new Vertex[_vertices.size()] );
218
233
  void LoaderOBJ::addFace( iterator_t, iterator_t const& )
219
234
  {
220
235
    if( _index_position_index < 3 )
221
 
      throw std::runtime_error(
222
 
        "Invalid Face (num_positions="
223
 
        + utilities::toStr(_index_position_index) + ")" );
 
236
    {
 
237
      LOG_FMT_WARNING_3("Invalid face (%1% vertices)", _index_position_index);
 
238
      _index_position_index = 0;
 
239
      _index_normal_index   = 0;
 
240
      return;
 
241
    }
224
242
 
225
243
    if(    _index_normal_index
226
244
        && (_index_normal_index != _index_position_index) )
227
245
      throw std::runtime_error(
228
246
        "Invalid Face (num_normals="
229
 
        + utilities::toStr(_index_normal_index) + ")" );
 
247
        + utilities::toStr(_index_normal_index) + ")" ); // TODO clean up
230
248
 
231
249
    math::Vector3f normal;
232
250
    if( !_index_normal_index )
302
320
    LOG_STR_INFO_2( "Found material lib: " + name );
303
321
  }
304
322
 
 
323
  //----------------------------------------------------------------------------
 
324
  void LoaderOBJ::printUnknown( iterator_t first, iterator_t const& last )
 
325
  {
 
326
    std::string name( first, last );
 
327
    //LOG_STR_INFO_2( "unknown: " + name );
 
328
  }
 
329
 
305
330
} // namespace scene
306
331
} // namespace gewoopi