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