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

Last change on this file since 411 was 411, checked in by epyon, 10 years ago
  • mesh_raw_channel and key_raw_channel merged into raw_data_channel
File size: 8.3 KB
Line 
1// Copyright (C) 2012-2015 ChaosForge Ltd
2// http://chaosforge.org/
3//
4// This file is part of Nova libraries.
5// For conditions of distribution and use, see copying.txt file in root folder.
6
7#include "nv/formats/obj_loader.hh"
8#include "nv/io/std_stream.hh"
9#include <sstream>
10
11using namespace nv;
12
13struct obj_vertex_vt
14{
15        vec3 position;
16        vec2 texcoord;
17
18        obj_vertex_vt( vec3 a_position, vec2 a_texcoord, vec3 )
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
44struct obj_reader
45{
46        vector< vec3 > v;
47        vector< vec3 > n;
48        vector< vec2 > t;
49
50        std::string line;
51        std::string cmd;
52        std::string name;
53        std::string next_name;
54
55        nv::size_t size;
56        bool   eof;
57
58        obj_reader();
59        bool read_stream( std::istream& stream );
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;
62        virtual void reset() = 0;
63        virtual const uint8* raw_pointer() const = 0;
64        virtual void calculate_tangents() {}
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;
76        eof = false;
77}
78
79bool obj_reader::read_stream( std::istream& stream )
80{
81        name = next_name;
82        bool added_faces = false;
83        f32 x, y, z;
84        if ( eof ) return false;
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                {
119                        added_faces = true;
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
149                if ( cmd == "g" )
150                {
151                        ss >> next_name;
152                        if (added_faces)
153                                return true;
154                        name = next_name;
155                        continue;
156                }
157
158                if ( cmd == "s" )
159                {
160                        continue;
161                }
162
163                // unknown command
164        }
165
166        eof = true;
167        return true;
168}
169
170template < typename VTX >
171struct mesh_data_reader : public obj_reader
172{
173        mesh_data_reader( bool normals ) : m_normals( normals ) {}
174        virtual nv::size_t add_face( uint32* vi, uint32* ti, uint32* ni, nv::size_t count )
175        {
176                if ( count < 3 ) return 0; // TODO : report error?
177
178                // TODO : support if normals not present;
179                vec3 nullvec;
180                nv::size_t result = 0;
181                // Simple triangulation - obj's shouldn't have more than quads anyway
182
183                if ( m_normals )
184                {
185                        for ( nv::size_t i = 2; i < count; ++i )
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                        }
192                }
193                else
194                {
195                        for ( nv::size_t i = 2; i < count; ++i )
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                }
203                return result;
204        }
205        bool m_normals;
206        vector< VTX > m_data;
207        virtual void reset() { m_data.clear(); }
208        virtual nv::size_t raw_size() const { return m_data.size() * sizeof( VTX ); }
209        virtual const uint8* raw_pointer() const { return reinterpret_cast< const uint8* >( m_data.data() ); }
210};
211
212
213struct mesh_data_reader_vt : public mesh_data_reader< obj_vertex_vt >
214{
215        mesh_data_reader_vt() : mesh_data_reader( false ) {}
216};
217
218struct mesh_data_reader_vtn : public mesh_data_reader< obj_vertex_vtn >
219{
220        mesh_data_reader_vtn() : mesh_data_reader( true ) {}
221};
222
223struct mesh_data_reader_vtnt : public mesh_data_reader< obj_vertex_vtnt >
224{
225        mesh_data_reader_vtnt() : mesh_data_reader( true ) {}
226
227        // based on http://www.terathon.com/code/tangent.html
228        void calculate_tangents()
229        {
230                nv::size_t count = m_data.size();
231                nv::size_t tcount = count / 3;
232
233                vector< vec3 > tan1( count );
234                vector< vec3 > tan2( count );
235
236                for ( nv::size_t a = 0; a < tcount; ++a )
237                {
238                        nv::size_t i1 = a * 3;
239                        nv::size_t i2 = a * 3 + 1;
240                        nv::size_t i3 = a * 3 + 2;
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
275                for ( nv::size_t a = 0; a < count; ++a )
276                {
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) )
280                        {
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;
283                        }
284                }
285
286        }
287
288
289};
290
291nv::obj_loader::obj_loader( bool normals /*= true*/, bool tangents /*= false */ )
292        : m_normals( normals ), m_tangents( tangents )
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
305bool nv::obj_loader::load( stream& source )
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
319        while ( reader->read_stream( sstream ) )
320        {
321                if ( m_tangents )
322                {
323                        reader->calculate_tangents();
324                }
325       
326                raw_data_channel* channel = new raw_data_channel();
327                nv::uint8* data = nullptr;
328
329                if ( reader->raw_size() > 0 )
330                {
331                        data = new uint8[ reader->raw_size() ];
332                        raw_copy_n( reader->raw_pointer(), reader->raw_size(), data );
333                }
334                channel->data  = data;
335                channel->desc  = m_descriptor;
336                channel->count = reader->size * 3;
337
338                mesh_data* mesh = new mesh_data(reader->name);
339                mesh->add_channel( channel );
340                m_meshes.push_back( mesh );
341
342                reader->reset();
343        }
344        delete reader;
345        return true;
346
347}
348
349mesh_data* nv::obj_loader::release_mesh_data( size_t index )
350{
351        mesh_data* result = m_meshes[ index ];
352        m_meshes[ index ] = nullptr;
353        return result;
354}
355
356nv::obj_loader::~obj_loader()
357{
358        for ( auto mesh : m_meshes ) if ( mesh ) delete mesh;
359}
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.