source: trunk/src/formats/obj_loader.cc @ 406

Last change on this file since 406 was 406, checked in by epyon, 10 years ago
  • code compiles cleanly on maximum warning level
File size: 8.3 KB
RevLine 
[395]1// Copyright (C) 2012-2015 ChaosForge Ltd
[136]2// http://chaosforge.org/
3//
[395]4// This file is part of Nova libraries.
5// For conditions of distribution and use, see copying.txt file in root folder.
[136]6
7#include "nv/formats/obj_loader.hh"
[138]8#include "nv/io/std_stream.hh"
[136]9#include <sstream>
10
11using namespace nv;
12
[238]13struct obj_vertex_vt
14{
15        vec3 position;
16        vec2 texcoord;
17
[240]18        obj_vertex_vt( vec3 a_position, vec2 a_texcoord, vec3 )
[238]19                : position( a_position ), texcoord( a_texcoord ) {}
20};
21
22struct obj_vertex_vtn
23{
24        vec3 position;
25        vec2 texcoord;
26        vec3 normal;
27
28        obj_vertex_vtn( vec3 a_position, vec2 a_texcoord, vec3 a_normal )
29                : position( a_position ), texcoord( a_texcoord ), normal( a_normal ) {}
30};
31
32
33struct obj_vertex_vtnt
34{
35        vec3 position;
36        vec2 texcoord;
37        vec3 normal;
38        vec4 tangent;
39
40        obj_vertex_vtnt( vec3 a_position, vec2 a_texcoord, vec3 a_normal )
41                : position( a_position ), texcoord( a_texcoord ), normal( a_normal ) {}
42};
43
[136]44struct obj_reader
45{
[383]46        vector< vec3 > v;
47        vector< vec3 > n;
48        vector< vec2 > t;
[136]49
50        std::string line;
51        std::string cmd;
[287]52        std::string name;
53        std::string next_name;
[136]54
[376]55        nv::size_t size;
[374]56        bool   eof;
[136]57
58        obj_reader();
59        bool read_stream( std::istream& stream );
[376]60        virtual nv::size_t add_face( uint32* vi, uint32* ti, uint32* ni, nv::size_t count ) = 0;
61        virtual nv::size_t raw_size() const = 0;
[240]62        virtual void reset() = 0;
[239]63        virtual const uint8* raw_pointer() const = 0;
[238]64        virtual void calculate_tangents() {}
[136]65
66        virtual ~obj_reader(){}
67};
68
69obj_reader::obj_reader()
70{
71        // push in dummy 0-index objects for faster indexing
72        v.push_back( vec3() );
73        n.push_back( vec3() );
74        t.push_back( vec2() );
75        size = 0;
[240]76        eof = false;
[136]77}
78
79bool obj_reader::read_stream( std::istream& stream )
80{
[287]81        name = next_name;
[240]82        bool added_faces = false;
[136]83        f32 x, y, z;
[240]84        if ( eof ) return false;
[136]85
86        while ( std::getline( stream, line ) )
87        {
88                if ( line.length() < 3 || line[0] == '#' )
89                {
90                        continue;
91                }
92
93                std::istringstream ss(line);
94                ss >> cmd;
95
96                if ( cmd == "v" )
97                {
98                        ss >> x >> y >> z;
99                        v.push_back( vec3( x, y, z ) );
100                        continue;
101                }
102
103                if ( cmd == "vn" )
104                {
105                        ss >> x >> y >> z;
106                        n.push_back( vec3( x, y, z ) );
107                        continue;
108                }
109
110                if ( cmd == "vt" )
111                {
112                        ss >> x >> y;
113                        t.push_back( vec2( x, 1.0f - y ) );
114                        continue;
115                }
116
117                if ( cmd == "f" )
118                {
[240]119                        added_faces = true;
[136]120                        ss >> cmd;
121
122                        uint32 vis[8];
123                        uint32 tis[8];
124                        uint32 nis[8];
125                        bool   normals = false;
126                        uint32 count = 0;
127
128                        while ( !ss.fail() )
129                        {
130                                char ch;
131
132                                std::istringstream ss2( cmd );
133                                ss2 >> vis[count] >> ch;
134                                ss2 >> tis[count] >> ch;
135                                if ( ch == '/')
136                                {
137                                        normals = true;
138                                        ss2 >> nis[count];
139                                }
140
141                                ss >> cmd;
142                                count++;
143                        }
144
145                        size += add_face( vis, tis, normals ? nis : nullptr, count );
146                        continue;
147                }
148
[240]149                if ( cmd == "g" )
[136]150                {
[287]151                        ss >> next_name;
152                        if (added_faces)
153                                return true;
154                        name = next_name;
[136]155                        continue;
156                }
157
[240]158                if ( cmd == "s" )
159                {
160                        continue;
161                }
162
[136]163                // unknown command
164        }
165
[240]166        eof = true;
[136]167        return true;
168}
169
[240]170template < typename VTX >
171struct mesh_data_reader : public obj_reader
[238]172{
[240]173        mesh_data_reader( bool normals ) : m_normals( normals ) {}
[376]174        virtual nv::size_t add_face( uint32* vi, uint32* ti, uint32* ni, nv::size_t count )
[238]175        {
176                if ( count < 3 ) return 0; // TODO : report error?
[240]177
[238]178                // TODO : support if normals not present;
[240]179                vec3 nullvec;
[376]180                nv::size_t result = 0;
[238]181                // Simple triangulation - obj's shouldn't have more than quads anyway
[240]182
183                if ( m_normals )
[238]184                {
[376]185                        for ( nv::size_t i = 2; i < count; ++i )
[240]186                        {
187                                result++;
188                                m_data.emplace_back( v[ vi[ 0 ]   ], t[ ti[ 0   ] ], n[ ni[ 0   ] ] );
189                                m_data.emplace_back( v[ vi[ i-1 ] ], t[ ti[ i-1 ] ], n[ ni[ i-1 ] ] );
190                                m_data.emplace_back( v[ vi[ i ]   ], t[ ti[ i   ] ], n[ ni[ i   ] ] );
191                        }
[238]192                }
[240]193                else
194                {
[376]195                        for ( nv::size_t i = 2; i < count; ++i )
[240]196                        {
197                                result++;
198                                m_data.emplace_back( v[ vi[ 0 ]   ], t[ ti[ 0   ] ], nullvec );
199                                m_data.emplace_back( v[ vi[ i-1 ] ], t[ ti[ i-1 ] ], nullvec );
200                                m_data.emplace_back( v[ vi[ i ]   ], t[ ti[ i   ] ], nullvec );
201                        }
202                }
[238]203                return result;
204        }
[240]205        bool m_normals;
[383]206        vector< VTX > m_data;
[240]207        virtual void reset() { m_data.clear(); }
[376]208        virtual nv::size_t raw_size() const { return m_data.size() * sizeof( VTX ); }
[406]209        virtual const uint8* raw_pointer() const { return reinterpret_cast< const uint8* >( m_data.data() ); }
[238]210};
211
[240]212
213struct mesh_data_reader_vt : public mesh_data_reader< obj_vertex_vt >
[238]214{
[240]215        mesh_data_reader_vt() : mesh_data_reader( false ) {}
[238]216};
217
[240]218struct mesh_data_reader_vtn : public mesh_data_reader< obj_vertex_vtn >
[238]219{
[240]220        mesh_data_reader_vtn() : mesh_data_reader( true ) {}
221};
[238]222
[240]223struct mesh_data_reader_vtnt : public mesh_data_reader< obj_vertex_vtnt >
224{
225        mesh_data_reader_vtnt() : mesh_data_reader( true ) {}
226
[238]227        // based on http://www.terathon.com/code/tangent.html
228        void calculate_tangents()
229        {
[376]230                nv::size_t count = m_data.size();
231                nv::size_t tcount = count / 3;
[238]232
[383]233                vector< vec3 > tan1( count );
234                vector< vec3 > tan2( count );
[238]235
[376]236                for ( nv::size_t a = 0; a < tcount; ++a )
[238]237                {
[376]238                        nv::size_t i1 = a * 3;
239                        nv::size_t i2 = a * 3 + 1;
240                        nv::size_t i3 = a * 3 + 2;
[238]241                        obj_vertex_vtnt& vtx1 = m_data[ i1 ];
242                        obj_vertex_vtnt& vtx2 = m_data[ i2 ];
243                        obj_vertex_vtnt& vtx3 = m_data[ i3 ];
244
245                        // TODO: simplify
246                        vec3 xyz1 = vtx2.position - vtx1.position;
247                        vec3 xyz2 = vtx3.position - vtx1.position;
248                        //vec2 st1  = w2 - w1;
249                        //vec2 st2  = w3 - w1;
250
251                        float s1 = vtx2.texcoord.x - vtx1.texcoord.x;
252                        float t1 = vtx2.texcoord.y - vtx1.texcoord.y;
253                        float s2 = vtx3.texcoord.x - vtx1.texcoord.x;
254                        float t2 = vtx3.texcoord.y - vtx1.texcoord.y;
255
256                        float stst = s1 * t2 - s2 * t1;
257                        float r = 0.0f;
258                        if (stst > 0.0f || stst < 0.0f) r = 1.0f / stst;
259
260                        vec3 sdir = ( t2 * xyz1 - t1 * xyz2 ) * r;
261                        vec3 tdir = ( s1 * xyz2 - s2 * xyz1 ) * r;
262
263                        // the += below obviously doesn't make sense in this case, but I'll
264                        // leave it here for when I move to indices
265                        tan1[i1] += sdir;
266                        tan1[i2] += sdir;
267                        tan1[i3] += sdir;
268
269                        // tan2 not needed anymore??
270                        tan2[i1] += tdir;
271                        tan2[i2] += tdir;
272                        tan2[i3] += tdir;
273                }
274
[376]275                for ( nv::size_t a = 0; a < count; ++a )
[238]276                {
[382]277                        const vec3& nv = m_data[a].normal;
278                        const vec3& tv = tan1[a];
279                        if ( ! (tv.x == 0.0f && tv.y == 0.0f && tv.z == 0.0f) )
[238]280                        {
[382]281                                m_data[a].tangent    = vec4( glm::normalize(tv - nv * glm::dot( nv, tv )), 0.0f );
282                                m_data[a].tangent[3] = (glm::dot(glm::cross(nv, tv), tan2[a]) < 0.0f) ? -1.0f : 1.0f;
[238]283                        }
284                }
285
286        }
287
288
289};
290
[239]291nv::obj_loader::obj_loader( bool normals /*= true*/, bool tangents /*= false */ )
[240]292        : m_normals( normals ), m_tangents( tangents )
[238]293{
294        if ( normals )
295        {
296                if ( tangents )
297                        m_descriptor.initialize<obj_vertex_vtnt>();
298                else
299                        m_descriptor.initialize<obj_vertex_vtn>();
300        }
301        else
302                m_descriptor.initialize<obj_vertex_vt>();
303}
304
[239]305bool nv::obj_loader::load( stream& source )
[238]306{
307        obj_reader* reader = nullptr;
308        if ( m_normals )
309        {
310                if ( m_tangents )
311                        reader = new mesh_data_reader_vtnt();
312                else
313                        reader = new mesh_data_reader_vtn();
314        }
315        else
316                reader = new mesh_data_reader_vt();
317        std_stream sstream( &source );
318
[240]319        while ( reader->read_stream( sstream ) )
[238]320        {
[240]321                if ( m_tangents )
322                {
323                        reader->calculate_tangents();
324                }
[238]325       
[240]326                mesh_raw_channel* channel = new mesh_raw_channel();
327                nv::uint8* data = nullptr;
[238]328
[240]329                if ( reader->raw_size() > 0 )
330                {
331                        data = new uint8[ reader->raw_size() ];
[383]332                        raw_copy_n( reader->raw_pointer(), reader->raw_size(), data );
[240]333                }
334                channel->data  = data;
335                channel->desc  = m_descriptor;
336                channel->count = reader->size * 3;
[239]337
[287]338                mesh_data* mesh = new mesh_data(reader->name);
[240]339                mesh->add_channel( channel );
340                m_meshes.push_back( mesh );
341
342                reader->reset();
[238]343        }
344        delete reader;
345        return true;
346
347}
348
[240]349mesh_data* nv::obj_loader::release_mesh_data( size_t index )
[238]350{
[240]351        mesh_data* result = m_meshes[ index ];
352        m_meshes[ index ] = nullptr;
[238]353        return result;
354}
355
[239]356nv::obj_loader::~obj_loader()
[238]357{
[240]358        for ( auto mesh : m_meshes ) if ( mesh ) delete mesh;
[238]359}
[287]360
361mesh_data_pack* nv::obj_loader::release_mesh_data_pack()
362{
363        uint32 size = m_meshes.size();
364        mesh_data* meshes = new mesh_data[ size ];
365        for ( uint32 i = 0; i < size; ++i )
366        {
367                m_meshes[i]->move_to( meshes[i] );
368                delete m_meshes[i];
369        }
370        m_meshes.clear();
371        return new mesh_data_pack( size, meshes );
372}
Note: See TracBrowser for help on using the repository browser.