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 ) {}
|
---|
122 | virtual std::size_t add_face( uint32* v, uint32* t, uint32* n, size_t count );
|
---|
123 |
|
---|
124 | vertex_attribute< vec3 >* m_position;
|
---|
125 | vertex_attribute< vec3 >* m_normal;
|
---|
126 | vertex_attribute< vec2 >* m_tex_coord;
|
---|
127 | mesh* m_mesh;
|
---|
128 | };
|
---|
129 |
|
---|
130 | std::size_t mesh_obj_reader::add_face( uint32* vi, uint32* ti, uint32* ni, size_t count )
|
---|
131 | {
|
---|
132 | if ( count < 3 )
|
---|
133 | {
|
---|
134 | // TODO : report error?
|
---|
135 | return 0;
|
---|
136 | }
|
---|
137 |
|
---|
138 | if ( m_position == nullptr )
|
---|
139 | {
|
---|
140 | m_position = m_mesh->add_attribute< vec3 >( "position" );
|
---|
141 | }
|
---|
142 | if ( m_tex_coord == nullptr )
|
---|
143 | {
|
---|
144 | m_tex_coord = m_mesh->add_attribute< vec2 >( "texcoord" );
|
---|
145 | }
|
---|
146 | if ( m_normal == nullptr && ni != nullptr )
|
---|
147 | {
|
---|
148 | m_normal = m_mesh->add_attribute< vec3 >( "normal" );
|
---|
149 | }
|
---|
150 |
|
---|
151 | // TODO : support if normals not present;
|
---|
152 |
|
---|
153 | std::vector< vec3 >& vp = m_position->get();
|
---|
154 | std::vector< vec2 >& vt = m_tex_coord->get();
|
---|
155 | std::vector< vec3 >& vn = m_normal->get();
|
---|
156 |
|
---|
157 | std::size_t result = 0;
|
---|
158 |
|
---|
159 | // Simple triangulation - obj's shouldn't have more than quads anyway
|
---|
160 | for ( size_t i = 2; i < count; ++i )
|
---|
161 | {
|
---|
162 | result++;
|
---|
163 | vp.push_back( v[ vi[ 0 ] ] ); vt.push_back( t[ ti[ 0 ] ] ); vn.push_back( n[ ni[ 0 ] ] );
|
---|
164 | vp.push_back( v[ vi[ i-1 ] ] ); vt.push_back( t[ ti[ i-1 ] ] ); vn.push_back( n[ ni[ i-1 ] ] );
|
---|
165 | vp.push_back( v[ vi[ i ] ] ); vt.push_back( t[ ti[ i ] ] ); vn.push_back( n[ ni[ i ] ] );
|
---|
166 | }
|
---|
167 |
|
---|
168 | return result;
|
---|
169 | }
|
---|
170 |
|
---|
171 | nv::obj_loader::obj_loader()
|
---|
172 | : m_mesh( nullptr )
|
---|
173 | {
|
---|
174 |
|
---|
175 | }
|
---|
176 |
|
---|
177 | nv::obj_loader::~obj_loader()
|
---|
178 | {
|
---|
179 | delete m_mesh;
|
---|
180 | }
|
---|
181 |
|
---|
182 | bool nv::obj_loader::load( stream& source )
|
---|
183 | {
|
---|
184 | if ( m_mesh != nullptr )
|
---|
185 | {
|
---|
186 | delete m_mesh;
|
---|
187 | }
|
---|
188 | m_mesh = new mesh();
|
---|
189 | mesh_obj_reader reader( m_mesh );
|
---|
190 | std_stream sstream( &source );
|
---|
191 | reader.read_stream( sstream );
|
---|
192 | m_size = reader.size;
|
---|
193 | return true;
|
---|
194 | }
|
---|