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 |
|
---|
11 | using namespace nv;
|
---|
12 |
|
---|
13 | struct 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 |
|
---|
31 | obj_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 |
|
---|
40 | bool 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 |
|
---|
119 | struct mesh_obj_reader : public obj_reader
|
---|
120 | {
|
---|
121 | mesh_obj_reader( mesh* m ) : m_mesh( m ), m_position( nullptr ), m_normal( nullptr ), m_tex_coord( nullptr ), m_tangent( nullptr ) {}
|
---|
122 | virtual std::size_t add_face( uint32* v, uint32* t, uint32* n, size_t count );
|
---|
123 | virtual void calculate_tangents();
|
---|
124 |
|
---|
125 | vertex_attribute< vec3 >* m_position;
|
---|
126 | vertex_attribute< vec3 >* m_normal;
|
---|
127 | vertex_attribute< vec2 >* m_tex_coord;
|
---|
128 | vertex_attribute< vec4 >* m_tangent;
|
---|
129 | mesh* m_mesh;
|
---|
130 | };
|
---|
131 |
|
---|
132 | std::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 |
|
---|
173 | // based on http://www.terathon.com/code/tangent.html
|
---|
174 | void mesh_obj_reader::calculate_tangents()
|
---|
175 | {
|
---|
176 | m_tangent = m_mesh->add_attribute< vec4 >( "tangent" );
|
---|
177 |
|
---|
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 | // TODO: simplify
|
---|
197 |
|
---|
198 | const vec3& v1 = vp[i1];
|
---|
199 | const vec3& v2 = vp[i2];
|
---|
200 | const vec3& v3 = vp[i3];
|
---|
201 |
|
---|
202 | const vec2& w1 = vt[i1];
|
---|
203 | const vec2& w2 = vt[i2];
|
---|
204 | const vec2& w3 = vt[i3];
|
---|
205 |
|
---|
206 | vec3 xyz1 = v2 - v1;
|
---|
207 | vec3 xyz2 = v3 - v1;
|
---|
208 | vec2 st1 = w2 - w1;
|
---|
209 | vec2 st2 = w3 - w1;
|
---|
210 |
|
---|
211 | float s1 = w2.x - w1.x;
|
---|
212 | float t1 = w2.y - w1.y;
|
---|
213 | float s2 = w3.x - w1.x;
|
---|
214 | float t2 = w3.y - w1.y;
|
---|
215 |
|
---|
216 | float r = 1.0f / (s1 * t2 - s2 * t1);
|
---|
217 |
|
---|
218 | vec3 sdir = ( t2 * xyz1 - t1 * xyz2 ) * r;
|
---|
219 | vec3 tdir = ( s1 * xyz2 - s2 * xyz1 ) * r;
|
---|
220 |
|
---|
221 | // the += below obviously doesn't make sense in this case, but I'll
|
---|
222 | // leave it here for when I move to indices
|
---|
223 | tan1[i1] += sdir;
|
---|
224 | tan1[i2] += sdir;
|
---|
225 | tan1[i3] += sdir;
|
---|
226 |
|
---|
227 | tan2[i1] += tdir;
|
---|
228 | tan2[i2] += tdir;
|
---|
229 | tan2[i3] += tdir;
|
---|
230 | }
|
---|
231 |
|
---|
232 | for (std::size_t a = 0; a < count; ++a )
|
---|
233 | {
|
---|
234 | const vec3& n = vn[a];
|
---|
235 | const vec3& t = tan1[a];
|
---|
236 |
|
---|
237 | tg[a] = vec4( glm::normalize(t - n * glm::dot( n, t )),
|
---|
238 | (glm::dot(glm::cross(n, t), tan2[a]) < 0.0f) ? -1.0f : 1.0f );
|
---|
239 | }
|
---|
240 |
|
---|
241 | }
|
---|
242 |
|
---|
243 | nv::obj_loader::obj_loader( bool tangents )
|
---|
244 | : m_mesh( nullptr ), m_tangents( tangents )
|
---|
245 | {
|
---|
246 |
|
---|
247 | }
|
---|
248 |
|
---|
249 | nv::obj_loader::~obj_loader()
|
---|
250 | {
|
---|
251 | delete m_mesh;
|
---|
252 | }
|
---|
253 |
|
---|
254 | bool nv::obj_loader::load( stream& source )
|
---|
255 | {
|
---|
256 | if ( m_mesh != nullptr )
|
---|
257 | {
|
---|
258 | delete m_mesh;
|
---|
259 | }
|
---|
260 | m_mesh = new mesh();
|
---|
261 | mesh_obj_reader reader( m_mesh );
|
---|
262 | std_stream sstream( &source );
|
---|
263 | reader.read_stream( sstream );
|
---|
264 | m_size = reader.size;
|
---|
265 | if ( m_tangents )
|
---|
266 | {
|
---|
267 | reader.calculate_tangents();
|
---|
268 | }
|
---|
269 | return true;
|
---|
270 | }
|
---|