Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
blob: 323986bf5d4f51e9bc2c828b6e6f34c640ce8881 (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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
/*
   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;
	
	obj_collisionmodel = nullptr;
	obj_load_clip = false;
}

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();
}

math::Vector3f * OBJFile::find_vertex(size_t index)
{
	VertexList::iterator it = obj_vertexlist.find(index);
	if (it == obj_vertexlist.end())
	{
		return 0;
	}
	else
	{
		return (*it).second;
	}
}

math::Vector3f *OBJFile::find_normal(size_t index)
{
	NormalList::iterator it = obj_normallist.find(index);
	if (it == obj_normallist.end())
	{
		return 0;
	}
	else
	{
		return (*it).second;
	}
}
	
math::Vector2f *OBJFile::find_uv(size_t index)
{
	UVList::iterator it = obj_uvlist.find(index);
	if (it == obj_uvlist.end())
	{
		return 0;
	}
	else
	{
		return (*it).second;
	}
}
	
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().compare(materialname) == 0) {
						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 vertex */
			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) { /* vertex 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) { /* vertex normal */
			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;
			
			for (size_t j = 0; j < 4; j++)
			{
				v[j] = 0;
				uv[j] = 0;
				vn[j] = 0;
				have_vn[j] = false;
				have_uv[j] = false;
			}

			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;
			}
			
			if(points.size() == 3) {
				math::Vector3f *v0 = find_vertex(v[0]-1);
				math::Vector3f *v1 = find_vertex(v[1]-1);
				math::Vector3f *v2 = find_vertex(v[2]-1);
				
				
				math::Vector3f *n0 = find_normal(vn[0]-1);
				math::Vector3f *n1 = find_normal(vn[1]-1);
				math::Vector3f *n2 = find_normal(vn[2]-1);
				
				math::Vector2f *t0 = find_uv(uv[0]-1);
				math::Vector2f *t1 = find_uv(uv[1]-1);
				math::Vector2f *t2 = find_uv(uv[2]-1);
				
				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()));
					obj_normallist[vn[0]-1] = n0;
				}
				if(!n1)
				{	
					n1 = new math::Vector3f(normal(triangle->v1(), triangle->v2(), triangle->v0()));
					obj_normallist[vn[1]-1] = n1;
				}
				if(!n2)
				{	
					n2 = new math::Vector3f(normal(triangle->v2(), triangle->v0(), triangle->v1()));
					obj_normallist[vn[2]-1] = n2;
				}

				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) {
				math::Vector3f *v0 = find_vertex(v[0]-1);
				math::Vector3f *v1 = find_vertex(v[1]-1);
				math::Vector3f *v2 = find_vertex(v[2]-1);
				math::Vector3f *v3 = find_vertex(v[3]-1);
				
				math::Vector3f *n0 = find_normal(vn[0]-1);
				math::Vector3f *n1 = find_normal(vn[1]-1);
				math::Vector3f *n2 = find_normal(vn[2]-1);
				math::Vector3f *n3 = find_normal(vn[3]-1);
				
				math::Vector2f *t0 = find_uv(uv[0]-1);
				math::Vector2f *t1 = find_uv(uv[1]-1);
				math::Vector2f *t2 = find_uv(uv[2]-1);
				math::Vector2f *t3 = find_uv(uv[3]-1);
								
				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()));
					obj_normallist[vn[0]-1] = n0;
				}
				if(!n1)
				{	
					n1 = new math::Vector3f(normal(quad->v1(), quad->v2(), quad->v0()));
					obj_normallist[vn[1]-1] = n1;
				}
				if(!n2)
				{	
					n2 = new math::Vector3f(normal(quad->v2(), quad->v3(), quad->v1()));
					obj_normallist[vn[2]-1] = n2;
				}
				if(!n3)
				{	
					n2 = new math::Vector3f(normal(quad->v3(), quad->v0(), quad->v2()));
					obj_normallist[vn[3]-1] = n3;
				}

				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];
		
		if (material->has_flag_clip())		// add mesh to the collision model if the material has the Clip flag set
		{
			CollisionMesh *collisionmesh = new CollisionMesh();
			
			// add triangles to the collision mesh
			for(TriList::iterator it = obj_trilist.begin(); it != obj_trilist.end(); ++it)
			{
				if((*it).first == i)
				{
					collisionmesh->add_triangle((*it).second->v0() * SCALE, (*it).second->v1() * SCALE, (*it).second->v2() * SCALE);
				}
			}
			
			for(QuadList::iterator it = obj_quadlist.begin(); it != obj_quadlist.end(); ++it)
			{
				if((*it).first == i)
				{
					collisionmesh->add_triangle((*it).second->v0() * SCALE, (*it).second->v1() * SCALE, (*it).second->v2() * SCALE);
					collisionmesh->add_triangle((*it).second->v2() * SCALE, (*it).second->v3() * SCALE, (*it).second->v0() * SCALE);
				}
			}
			
			obj_collisionmodel->add_mesh(collisionmesh);
		}		
		else if (!material->has_flag_ignore())	// do not load meshes if the material has the Ignore flag
		{
			
			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 nullptr;
	}

	// create a new model
	Model *model = new Model(name);

	// load clip into collision meshes
	// if the model has been loaded before (e.g. on r_restart), the collision model won't be reloaded 
	objfile.obj_collisionmodel = CollisionModel::find(name);
	if (!objfile.obj_collisionmodel)
	{
		objfile.obj_load_clip = true;
		objfile.obj_collisionmodel = new CollisionModel(name);
		CollisionModel::add(objfile.obj_collisionmodel);
	} else {
		objfile.obj_load_clip = false;
	}
	model->set_collisionmodel(objfile.obj_collisionmodel);
	
	if (!objfile.read() || !objfile.fragmentgroup()->size())
	{
		con_warn << "  " << objfile.name() << " empty model" << std::endl;
		return model;
	}
	
	// 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