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

Last change on this file since 147 was 147, checked in by epyon, 12 years ago
  • obj_loader - support for tangent space calculation
File size: 5.8 KB
RevLine 
[136]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"
[138]8#include "nv/io/std_stream.hh"
[136]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{
[147]121        mesh_obj_reader( mesh* m ) : m_mesh( m ), m_position( nullptr ), m_normal( nullptr ), m_tex_coord( nullptr ), m_tangent( nullptr ) {}
[136]122        virtual std::size_t add_face( uint32* v, uint32* t, uint32* n, size_t count );
[147]123        virtual void calculate_tangents();
[136]124
125        vertex_attribute< vec3 >* m_position;
126        vertex_attribute< vec3 >* m_normal;
127        vertex_attribute< vec2 >* m_tex_coord;
[147]128        vertex_attribute< vec4 >* m_tangent;
[136]129        mesh* m_mesh;
130};
131
132std::size_t mesh_obj_reader::add_face( uint32* vi, uint32* ti, uint32* ni, size_t count )
133{
134        if ( count < 3 )
135        {
136                // TODO : report error?
137                return 0;
138        }
139
140        if ( m_position == nullptr )
141        {
142                m_position  = m_mesh->add_attribute< vec3 >( "position" );
143        }
144        if ( m_tex_coord == nullptr )
145        {
146                m_tex_coord = m_mesh->add_attribute< vec2 >( "texcoord" );
147        }
148        if ( m_normal == nullptr && ni != nullptr )
149        {
150                m_normal = m_mesh->add_attribute< vec3 >( "normal" );
151        }
152
153        // TODO : support if normals not present;
154
155        std::vector< vec3 >& vp = m_position->get();
156        std::vector< vec2 >& vt = m_tex_coord->get();
157        std::vector< vec3 >& vn = m_normal->get();
158
159        std::size_t result = 0;
160
161        // Simple triangulation - obj's shouldn't have more than quads anyway
162        for ( size_t i = 2; i < count; ++i )
163        {
164                result++;
165                vp.push_back( v[ vi[ 0 ] ] );   vt.push_back( t[ ti[ 0 ] ] );   vn.push_back( n[ ni[ 0 ] ] );
166                vp.push_back( v[ vi[ i-1 ] ] ); vt.push_back( t[ ti[ i-1 ] ] ); vn.push_back( n[ ni[ i-1 ] ] );
167                vp.push_back( v[ vi[ i ] ] );   vt.push_back( t[ ti[ i ] ] );   vn.push_back( n[ ni[ i ] ] );
168        }
169
170        return result;
171}
172
[147]173// based on http://www.terathon.com/code/tangent.html
174void mesh_obj_reader::calculate_tangents()
[136]175{
[147]176        m_tangent = m_mesh->add_attribute< vec4 >( "tangent" );
[136]177
[147]178        std::vector< vec3 >& vp = m_position->get();
179        std::vector< vec2 >& vt = m_tex_coord->get();
180        std::vector< vec3 >& vn = m_normal->get();
181        std::vector< vec4 >& tg = m_tangent->get();
182
183        std::size_t count  = vp.size();
184        std::size_t tcount = count / 3;
185
186        std::vector< vec3 > tan1( count );
187        std::vector< vec3 > tan2( count );
188        tg.resize( count );
189
190        for (std::size_t a = 0; a < tcount; ++a )
191        {
192                uint32 i1 = a * 3;
193                uint32 i2 = a * 3 + 1;
194                uint32 i3 = a * 3 + 2;
195
196                const vec3& v1 = vp[i1];
197                const vec3& v2 = vp[i2];
198                const vec3& v3 = vp[i3];
199
200                const vec2& w1 = vt[i1];
201                const vec2& w2 = vt[i2];
202                const vec2& w3 = vt[i3];
203
204                vec3 xyz1 = v2 - v1;
205                vec3 xyz2 = v3 - v1;
206                vec2 st1  = w2 - w1;
207                vec2 st2  = w3 - w1;
208
209                float x1 = v2.x - v1.x;
210                float y1 = v2.y - v1.y;
211                float z1 = v2.z - v1.z;
212
213                float x2 = v3.x - v1.x;
214                float y2 = v3.y - v1.y;
215                float z2 = v3.z - v1.z;
216
217                float s1 = w2.x - w1.x;
218                float t1 = w2.y - w1.y;
219                float s2 = w3.x - w1.x;
220                float t2 = w3.y - w1.y;
221
222                float r = 1.0f / (s1 * t2 - s2 * t1);
223
224                vec3 sdir = ( t2 * xyz1 - t1 * xyz2 ) * r;
225                vec3 tdir = ( s1 * xyz2 - s2 * xyz1 ) * r;
226
227                // the += below obviously doesn't make sense in this case, but I'll
228                // leave it here for when I move to indices
229                tan1[i1] += sdir;
230                tan1[i2] += sdir;
231                tan1[i3] += sdir;
232
233                tan2[i1] += tdir;
234                tan2[i2] += tdir;
235                tan2[i3] += tdir;
236        }
237
238        for (std::size_t a = 0; a < count; ++a )
239        {
240                const vec3& n = vn[a];
241                const vec3& t = tan1[a];
242
243                tg[a] = vec4( glm::normalize(t - n * glm::dot( n, t )),
244                    (glm::dot(glm::cross(n, t), tan2[a]) < 0.0f) ? -1.0f : 1.0f );
245        }
246
[136]247}
248
[147]249nv::obj_loader::obj_loader( bool tangents )
250        : m_mesh( nullptr ), m_tangents( tangents )
251{
252
253}
254
[136]255nv::obj_loader::~obj_loader()
256{
257        delete m_mesh;
258}
259
260bool nv::obj_loader::load( stream& source )
261{
262        if ( m_mesh != nullptr )
263        {
264                delete m_mesh;
265        }
266        m_mesh = new mesh();
267        mesh_obj_reader reader( m_mesh );
[138]268        std_stream sstream( &source );
[136]269        reader.read_stream( sstream );
270        m_size = reader.size;
[147]271        if ( m_tangents )
272        {
273                reader.calculate_tangents();
274        }
[136]275        return true;
276}
Note: See TracBrowser for help on using the repository browser.