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

Last change on this file since 224 was 224, checked in by epyon, 11 years ago
  • universal mesh format
  • removed keyframed_mesh_data
File size: 5.2 KB
Line 
1// Copyright (C) 2012-2013 ChaosForge / Kornel Kisielewicz
2// http://chaosforge.org/
3//
4// This file is part of NV Libraries.
5// For conditions of distribution and use, see copyright notice in nv.hh
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_reader
14{
15        std::vector< vec3 > v;
16        std::vector< vec3 > n;
17        std::vector< vec2 > t;
18
19        std::string line;
20        std::string cmd;
21
22        std::size_t size;
23
24        obj_reader();
25        bool read_stream( std::istream& stream );
26
27        virtual std::size_t add_face( uint32* vi, uint32* ti, uint32* ni, size_t count ) = 0;
28        virtual ~obj_reader(){}
29};
30
31obj_reader::obj_reader()
32{
33        // push in dummy 0-index objects for faster indexing
34        v.push_back( vec3() );
35        n.push_back( vec3() );
36        t.push_back( vec2() );
37        size = 0;
38}
39
40bool obj_reader::read_stream( std::istream& stream )
41{
42        f32 x, y, z;
43
44        while ( std::getline( stream, line ) )
45        {
46                if ( line.length() < 3 || line[0] == '#' )
47                {
48                        continue;
49                }
50
51                std::istringstream ss(line);
52                ss >> cmd;
53
54                if ( cmd == "v" )
55                {
56                        ss >> x >> y >> z;
57                        v.push_back( vec3( x, y, z ) );
58                        continue;
59                }
60
61                if ( cmd == "vn" )
62                {
63                        ss >> x >> y >> z;
64                        n.push_back( vec3( x, y, z ) );
65                        continue;
66                }
67
68                if ( cmd == "vt" )
69                {
70                        ss >> x >> y;
71                        t.push_back( vec2( x, 1.0f - y ) );
72                        continue;
73                }
74
75                if ( cmd == "f" )
76                {
77                        ss >> cmd;
78
79                        uint32 vis[8];
80                        uint32 tis[8];
81                        uint32 nis[8];
82                        bool   normals = false;
83                        uint32 count = 0;
84
85                        while ( !ss.fail() )
86                        {
87                                char ch;
88
89                                std::istringstream ss2( cmd );
90                                ss2 >> vis[count] >> ch;
91                                ss2 >> tis[count] >> ch;
92                                if ( ch == '/')
93                                {
94                                        normals = true;
95                                        ss2 >> nis[count];
96                                }
97
98                                ss >> cmd;
99                                count++;
100                        }
101
102                        size += add_face( vis, tis, normals ? nis : nullptr, count );
103                        continue;
104                }
105
106                if ( cmd == "g" || cmd == "s" )
107                {
108                        // ignored
109                        continue;
110                }
111
112                // unknown command
113        }
114
115        return true;
116}
117
118
119struct mesh_obj_reader : public obj_reader
120{
121        mesh_obj_reader( mesh_data_creator* m ) : m_mesh( m ) {}
122        virtual std::size_t add_face( uint32* v, uint32* t, uint32* n, size_t count );
123        virtual void calculate_tangents();
124
125        mesh_data_creator* m_mesh;
126};
127
128size_t mesh_obj_reader::add_face( uint32* vi, uint32* ti, uint32* ni, size_t count )
129{
130        if ( count < 3 )
131        {
132                // TODO : report error?
133                return 0;
134        }
135
136        // TODO : support if normals not present;
137
138        std::vector< vec3 >& vp = m_mesh->get_positions();
139        std::vector< vec3 >& vn = m_mesh->get_normals();
140        std::vector< vec2 >& vt = m_mesh->get_texcoords();
141
142        std::size_t result = 0;
143
144        // Simple triangulation - obj's shouldn't have more than quads anyway
145        for ( size_t i = 2; i < count; ++i )
146        {
147                result++;
148                vp.push_back( v[ vi[ 0 ] ] );   vt.push_back( t[ ti[ 0 ] ] );   vn.push_back( n[ ni[ 0 ] ] );
149                vp.push_back( v[ vi[ i-1 ] ] ); vt.push_back( t[ ti[ i-1 ] ] ); vn.push_back( n[ ni[ i-1 ] ] );
150                vp.push_back( v[ vi[ i ] ] );   vt.push_back( t[ ti[ i ] ] );   vn.push_back( n[ ni[ i ] ] );
151        }
152
153        return result;
154}
155
156// based on http://www.terathon.com/code/tangent.html
157void mesh_obj_reader::calculate_tangents()
158{
159        const std::vector< vec3 >& vp = m_mesh->get_positions();
160        const std::vector< vec2 >& vt = m_mesh->get_texcoords();
161        const std::vector< vec3 >& vn = m_mesh->get_normals();
162        std::vector< vec3 >& tg = m_mesh->get_tangents();
163
164        size_t count  = vp.size();
165        size_t tcount = count / 3;
166
167        std::vector< vec3 > tan1( count );
168        std::vector< vec3 > tan2( count );
169        tg.resize( count );
170
171        for (size_t a = 0; a < tcount; ++a )
172        {
173                size_t i1 = a * 3;
174                size_t i2 = a * 3 + 1;
175                size_t i3 = a * 3 + 2;
176
177                // TODO: simplify
178
179                const vec3& v1 = vp[i1];
180                const vec3& v2 = vp[i2];
181                const vec3& v3 = vp[i3];
182
183                const vec2& w1 = vt[i1];
184                const vec2& w2 = vt[i2];
185                const vec2& w3 = vt[i3];
186
187                vec3 xyz1 = v2 - v1;
188                vec3 xyz2 = v3 - v1;
189                //vec2 st1  = w2 - w1;
190                //vec2 st2  = w3 - w1;
191
192                float s1 = w2.x - w1.x;
193                float t1 = w2.y - w1.y;
194                float s2 = w3.x - w1.x;
195                float t2 = w3.y - w1.y;
196
197                float r = 1.0f / (s1 * t2 - s2 * t1);
198
199                vec3 sdir = ( t2 * xyz1 - t1 * xyz2 ) * r;
200                vec3 tdir = ( s1 * xyz2 - s2 * xyz1 ) * r;
201
202                // the += below obviously doesn't make sense in this case, but I'll
203                // leave it here for when I move to indices
204                tan1[i1] += sdir;
205                tan1[i2] += sdir;
206                tan1[i3] += sdir;
207
208                // tan2 not needed anymore??
209                tan2[i1] += tdir;
210                tan2[i2] += tdir;
211                tan2[i3] += tdir;
212        }
213
214        for (std::size_t a = 0; a < count; ++a )
215        {
216                const vec3& n = vn[a];
217                const vec3& t = tan1[a];
218
219                tg[a] = vec3( glm::normalize(t - n * glm::dot( n, t )) );
220                //tg[a][3] =    (glm::dot(glm::cross(n, t), tan2[a]) < 0.0f) ? -1.0f : 1.0f;
221        }
222
223}
224
225nv::obj_loader::obj_loader( bool tangents )
226        : m_mesh( nullptr ), m_tangents( tangents )
227{
228
229}
230
231nv::obj_loader::~obj_loader()
232{
233        delete m_mesh;
234}
235
236bool nv::obj_loader::load( stream& source )
237{
238        if ( m_mesh != nullptr )
239        {
240                delete m_mesh;
241        }
242        mesh_data_creator creator;
243        mesh_obj_reader reader( &creator );
244        std_stream sstream( &source );
245        reader.read_stream( sstream );
246        m_size = reader.size;
247        if ( m_tangents )
248        {
249                reader.calculate_tangents();
250        }
251        m_mesh = creator.release();
252        return true;
253}
Note: See TracBrowser for help on using the repository browser.