Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
blob: e4cec92bd7458cf1f7f0ae5b9aed322430b7575f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/*
   model/objfile.cc
   This file is part of the Osirion project and is distributed under
   the terms of the GNU General Public License version 2
*/

#include "sys/sys.h"
#include "model/objfile.h"

#include <sstream>
#include <cstring>

/* NOTE: Does NOT support reverse indexes. Cannot find an example
	of this in use, or a program that will export using them.
*/

namespace model
{

// max geometry bounds
const float MAX_BOUNDS = 16384.0f;

OBJFile::OBJFile(std::string const &name)
{
	objfile_name.append(name);
	objfile_name.append(".obj");
	objfile_ifs.open(objfile_name);

	obj_box.assign(MAX_BOUNDS, - MAX_BOUNDS);

	// a single fragmentgroup will contain all the model polygons
	obj_fragmentgroup = new FragmentGroup();
	obj_fragmentgroup->set_type(FragmentGroup::None);
	
	// reset counters
	obj_normalcount = 0;
}

OBJFile::~OBJFile()
{
	for (VertexList::iterator it = obj_vertexlist.begin(); it != obj_vertexlist.end(); it++) {
		delete(*it).second;
	}
	obj_vertexlist.clear();

	for (NormalList::iterator it = obj_normallist.begin(); it != obj_normallist.end(); it++) {
		delete(*it).second;
	}
	obj_normallist.clear();
	
	for (UVList::iterator it = obj_uvlist.begin(); it != obj_uvlist.end(); it++) {
		delete(*it).second;
	}
	obj_uvlist.clear();

	for (TriList::iterator it = obj_trilist.begin(); it != obj_trilist.end(); it++) {
		delete(*it).second;
	}
	obj_trilist.clear();

	for (QuadList::iterator it = obj_quadlist.begin(); it != obj_quadlist.end(); it++) {
		delete(*it).second;
	}
	obj_quadlist.clear();
	
	if (objfile_ifs.is_open())
		objfile_ifs.close();
}

bool OBJFile::read()
{
	char data[1024];
	memset(data, 0, sizeof(data));
	
	math::Vector3f zero;
	
	size_t current_m = 0;
	size_t index_line = 0;
	
	Fragment *fragment = 0;
	Material *material = 0;
	
	while (objfile_ifs.getline(data, sizeof(data) - 1)) {
		std::istringstream line(data);
		
		index_line++;
		
		std::string word;
		line >> word;
		
		if( word.compare("usemtl") == 0) { /* material definition */
			
			std::string materialname;
			material = 0;
			
			if (line >> materialname) {
				for(MaterialList::iterator it = obj_materiallist.begin(); it != obj_materiallist.end(); ++it) {
					if( (*it).second->name() == materialname) {
						material = (*it).second;
						current_m = (*it).first;
						break;
					}
				}
				
				if (!material) {
					material = Material::load(materialname);
					obj_materiallist[obj_materiallist.size()] = material;
					current_m = obj_materiallist.size()-1;
				}
			}
			else
				con_warn << objfile_name << " invalid material definition at line " << index_line << std::endl;
		
		
		} else if (word.compare("v") == 0) { /* new wertex */
			float x, y, z;
			line >> x >> y >> z;
			
			math::Vector3f *v = new math::Vector3f(x, y, z);
			obj_vertexlist[obj_vertexlist.size()] = v;
			obj_box.expand(*v * SCALE);
		
		} else if (word.compare("vt") == 0) { /* wertex texture coordinate */
			float u, v;
			line >> u >> v;
			
			math::Vector2f *uv = new math::Vector2f(u, v);
			obj_uvlist[obj_uvlist.size()] = uv;
		
		} else if (word.compare("vn") == 0) { /* wertex wormal */
			float x, y, z;
			line >> x >> y >> z;
			
			math::Vector3f *nm = new math::Vector3f(x, y, z);
			nm->normalize();
			obj_normallist[obj_normalcount] = nm;
			obj_normalcount++;
		
		} else if (word.compare("f") == 0) { /* face/polygon */
			size_t v[4];
			size_t uv[4];
			size_t vn[4];
			
			bool have_vn[4], have_uv[4];
			size_t nslash;

			std::string strbuf(line.str());
			strbuf.erase(0,2); //remove "f "

			std::stringstream tmp(strbuf);
			
			std::vector<std::string> points;
			int i=0;
			while(!tmp.eof()) {
				nslash = 0;
				
				have_vn[i] = 0;
				have_uv[i] = 0;
				
				std::string point;
				if (!(tmp >> point))  // bordercase for space at end of face definition
					continue;

				for(size_t c=0; c<point.length(); c++) {
					if(point[c] == '/') {
						nslash++;
						have_uv[i]=true;
						point[c] = ' ';
					}
					if(point[c] == ' ' && point[c+1] == '/') {
						have_uv[i]=false;
						have_vn[i]=true;
						nslash=2;
						point.erase(c+1,1);
					}
				}
				
				if(nslash==2 && have_vn[i] == false) {
					have_vn[i] = true;
					have_uv[i] = true;
				}
				points.push_back(point);
				
				std::stringstream tmp2(point);
				tmp2 >> v[i];
				if(have_uv[i]) tmp2 >> uv[i];
				if(have_vn[i]) tmp2 >> vn[i];
				i++;
			}

			if(points.size() != 4 && points.size() != 3) {
				con_warn << objfile_name << " face with " << points.size() << " vertices at line " << index_line << std::endl;
				points.clear();
				continue;
			}
			
			math::Vector3f *v0 = obj_vertexlist.find(v[0]-1)->second;
			math::Vector3f *v1 = obj_vertexlist.find(v[1]-1)->second;
			math::Vector3f *v2 = obj_vertexlist.find(v[2]-1)->second;
			math::Vector3f *v3 = obj_vertexlist.find(v[3]-1)->second;
			
			math::Vector3f *n0 = obj_normallist[vn[0]-1];
			math::Vector3f *n1 = obj_normallist[vn[1]-1];
			math::Vector3f *n2 = obj_normallist[vn[2]-1];
			math::Vector3f *n3 = obj_normallist[vn[3]-1];
			
			math::Vector2f *t0 = obj_uvlist.find(uv[0]-1)->second;
			math::Vector2f *t1 = obj_uvlist.find(uv[1]-1)->second;
			math::Vector2f *t2 = obj_uvlist.find(uv[2]-1)->second;
			math::Vector2f *t3 = obj_uvlist.find(uv[3]-1)->second;
			
			if(points.size() == 3) {
				if(!v0 || !v1 || !v2) {
					con_warn << objfile_name << " invalid vertex called from line " << index_line << std::endl;
					continue;
				}
	
				Triangle *triangle = new Triangle(*v0, *v1, *v2);

				if(!n0) n0 = new math::Vector3f(normal(triangle->v0(), triangle->v1(), triangle->v2()));
				if(!n1) n1 = new math::Vector3f(normal(triangle->v1(), triangle->v2(), triangle->v0()));
				if(!n2) n2 = new math::Vector3f(normal(triangle->v2(), triangle->v0(), triangle->v1()));

				triangle->n0().assign(n0->x(), n0->y(), n0->z());
				triangle->n1().assign(n1->x(), n1->y(), n1->z());
				triangle->n2().assign(n2->x(), n2->y(), n2->z());
				
				if(t0)	triangle->t0().assign(t0->x(), 1.0 - t0->y());
				if(t1)	triangle->t1().assign(t1->x(), 1.0 - t1->y());
				if(t2)	triangle->t2().assign(t2->x(), 1.0 - t2->y());
					
				std::pair<size_t, Triangle *> poly(current_m, triangle);
				obj_trilist.push_back(poly);
		
			} else if(points.size() == 4) {
				
				if(!v0 || !v1 || !v2 || !v3) {
					con_warn << objfile_name << " invalid vertex called from line " << index_line << std::endl;
					continue;
				}
				
				Quad *quad = new Quad(*v0, *v1, *v2, *v3, zero);
				
				if(!n0) n0 = new math::Vector3f(normal(quad->v0(), quad->v1(), quad->v3()));
				if(!n1) n1 = new math::Vector3f(normal(quad->v1(), quad->v2(), quad->v0()));
				if(!n2) n2 = new math::Vector3f(normal(quad->v2(), quad->v3(), quad->v1()));
				if(!n3) n3 = new math::Vector3f(normal(quad->v3(), quad->v0(), quad->v2()));

				quad->n0().assign(n0->x(), n0->y(), n0->z());
				quad->n1().assign(n1->x(), n1->y(), n1->z());
				quad->n2().assign(n2->x(), n2->y(), n2->z());
				quad->n3().assign(n3->x(), n3->y(), n3->z());
				
				if(t0)	quad->t0().assign(t0->x(), 1.0 - t0->y());
				if(t1)	quad->t1().assign(t1->x(), 1.0 - t1->y());
				if(t2)	quad->t2().assign(t2->x(), 1.0 - t2->y());
				if(t3)	quad->t3().assign(t3->x(), 1.0 - t3->y());
				
				std::pair<size_t, Quad *> poly(current_m, quad);
				obj_quadlist.push_back(poly);
			}
		}
	}
	
	//Go through tri and quad lists and create fragments for each material
	size_t total_m = obj_materiallist.size();
	for(size_t i = 0; i <= total_m; i++) {
		material = obj_materiallist[i];
		fragment = new Fragment(Fragment::Triangles, material);
		
		for(TriList::iterator it = obj_trilist.begin(); it != obj_trilist.end(); ++it) {
			if((*it).first == i) {
				fragment->add_vertex(((*it).second->v0() * SCALE) , (*it).second->n0(), (*it).second->t0(), false);
				fragment->add_vertex(((*it).second->v1() * SCALE) , (*it).second->n1(), (*it).second->t1(), false);
				fragment->add_vertex(((*it).second->v2() * SCALE) , (*it).second->n2(), (*it).second->t2(), false);
			}
		}
		
		if( fragment->structural_size() + fragment->detail_size() > 0 )
			obj_fragmentgroup->add_fragment(fragment);
			
		fragment = new Fragment(Fragment::Quads, material);
		
		for(QuadList::iterator it = obj_quadlist.begin(); it != obj_quadlist.end(); ++it) {
			if((*it).first == i) {
				fragment->add_vertex(((*it).second->v0() * SCALE) , (*it).second->n0(), (*it).second->t0(), false);
				fragment->add_vertex(((*it).second->v1() * SCALE) , (*it).second->n1(), (*it).second->t1(), false);
				fragment->add_vertex(((*it).second->v2() * SCALE) , (*it).second->n2(), (*it).second->t2(), false);
				fragment->add_vertex(((*it).second->v3() * SCALE) , (*it).second->n3(), (*it).second->t3(), false);
			}
		}
		
		if( fragment->structural_size() + fragment->detail_size() > 0 )
			obj_fragmentgroup->add_fragment(fragment);
	}
	
	return true;
}

Model *OBJFile::load(const std::string &name)
{
	OBJFile objfile(name);

	if (!objfile.is_open()) {
		return 0;
	}

	if (!objfile.read()) {
		return 0;
	}

	if (!objfile.fragmentgroup()->size())
		return 0;
	
	// create a new model
	Model *model = new Model(name);

	// center model around (0,0,0) and set the bounding box
	math::Vector3f obj_center((objfile.box().min() + objfile.box().max()) * 0.5f);	
	model->model_box.assign(
		objfile.box().min() - obj_center,
		objfile.box().max() - obj_center
	);	
	model->set_radius(model->box().max().length());
	model->set_origin(obj_center * -1.0f);

	objfile.fragmentgroup()->set_location(obj_center * -1.0f);
	
	for (FragmentGroup::Fragments::const_iterator fit = objfile.fragmentgroup()->fragments().begin(); fit != objfile.fragmentgroup()->fragments().end(); fit++) {		
		const Fragment *fragment = (*fit);
		
		if(fragment->type() == Fragment::Triangles)
			model->model_tris_count += (fragment->structural_size() + fragment->detail_size()) / 3;
		else
			model->model_quad_count += (fragment->structural_size() + fragment->detail_size()) / 4;
	}

	model->add_group(objfile.fragmentgroup());

	con_debug << "  " << objfile.name() << " " << objfile.vertexcount() << " vertices " << model->model_tris_count << " triangles " << model->model_quad_count << " quads" << std::endl;
	
	return model;
}

} // namespace model