Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
blob: 608dac359170d03ec71fc3770782fb9db5475ff8 (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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
/*
   model/asefile.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/asefile.h"

#include <sstream>
#include <cstring>

namespace model
{

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

ASEFile::ASEFile(std::string const &name)
{
	asefile_name.append(name);
	asefile_name.append(".ase");
	asefile_ifs.open(asefile_name);

	ase_box.assign(MAX_BOUNDS, - MAX_BOUNDS);

	// a single fragmentgroup wil contain all the model triangles
	ase_fragmentgroup = new FragmentGroup();
	ase_fragmentgroup->set_type(FragmentGroup::None);
	
	// reset counters
	ase_vertexcount = 0;
	ase_facecount = 0 ;
}

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

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

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

	for (MaterialList::iterator it = ase_materials.begin(); it != ase_materials.end(); it++) {
		SubMaterialList *submaterials = (*it).second;
		
		for (SubMaterialList::iterator smit = submaterials->begin(); smit != submaterials->end(); smit++) {
			(*smit).second = 0;
		}
		submaterials->clear();
		delete submaterials;
		(*it).second = 0;		
	}
	ase_materials.clear();

	if (asefile_ifs.is_open())
		asefile_ifs.close();
}

bool ASEFile::read_header(std::istream &is)
{
	if (!is.good()) {
		return false;
	}

	char data[1024];
	memset(data, 0, sizeof(data));

	if (!is.getline(data, sizeof(data) - 1)) {
		return false;
	}

	std::istringstream line(data);
	std::string word;
	line >> word;

	if (!word.compare("*3DSMAX_ASCIIEXPORT") == 0) {
		return false;
	}

	return true;
}

Material *ASEFile::read_submaterial(std::istream &is)
{
	Material *material = 0;
	char data[1024];
	memset(data, 0, sizeof(data));
	int level = 1;

	while (is.getline(data, sizeof(data) - 1)) {

		std::istringstream line(data);
		std::string word;
		line >> word;

		if ((level == 1) && (word.compare("*MATERIAL_NAME") == 0)) {
			// read material name
			std::string n;
			char c;
			while ((line.get(c)) && (c != '"'));
			while ((line.get(c)) && (c != '"'))
				n += c;
			
			// find material
			material = Material::find(n);
			if (!material) {
				material = new Material(n);
				Material::add(material);
				material->set_texture(material->name());
			}
				
		} else {
			do {
				if (word.compare("{") == 0) {
					level++;
				} else if (word.compare("}") == 0) {
					level--;
				}
			} while (line >> word);
		}

		if (level == 0) {
			return material;
		}
	}

	return material;
}

ASEFile::SubMaterialList *ASEFile::read_material(std::istream &is)
{
	SubMaterialList *submaterials = new SubMaterialList;
	std::string materialname;
	
	char data[1024];
	memset(data, 0, sizeof(data));
	int level = 1;
	size_t index = 0;

	while (is.getline(data, sizeof(data) - 1)) {

		std::istringstream line(data);
		std::string word;
		line >> word;

		if ((level == 1) && (word.compare("*MATERIAL_NAME") == 0)) {
			// read material name
			std::string n;
			char c;
			while ((line.get(c)) && (c != '"'));
			while ((line.get(c)) && (c != '"'))
				n += c;

			materialname.assign(n);
			
		} else if ((level == 1) && (word.compare("*SUBMATERIAL") == 0)) {
				
			if ((line >> index) && (line >> word) && (word.compare("{") == 0)) {
				// this material defines submaterials, we ignore the parent definition
				materialname.clear();

				// add material to the submaterial list
				(*submaterials)[index] = read_submaterial(is);
			}
			
		} else {
			do {
				if (word.compare("{") == 0) {
					level++;
				} else if (word.compare("}") == 0) {
					level--;
				}
			} while (line >> word);
		}

		if (level == 0) {
			
			if (materialname.size()) {
				// no submaterials, add a single material to the submaterial list
				Material *material = Material::find(materialname);
				if (!material) {
					material = new Material(materialname);
					Material::add(material);
					material->set_texture(materialname);
				}
				(*submaterials)[0] = material;
			}
			
			return submaterials;
		}
	}

	return submaterials;
}

bool ASEFile::read_material_list(std::istream &is)
{
	char data[1024];
	memset(data, 0, sizeof(data));
	int level = 1;
	
	while (is.getline(data, sizeof(data) - 1)) {

		std::istringstream line(data);
		std::string word;
		size_t index;
		line >> word;

		if ((level == 1) && (word.compare("*MATERIAL") == 0)) {
			if ((line >> index) && (line >> word) && (word.compare("{") == 0)) {
				// add material to the ase material list
				ase_materials[index] = read_material(is);
				//con_debug << "  " << name() << " " << "*MATERIAL " << index << " " << ase_materials[index]->name() << std::endl;
			}

		} else {
			do {
				if (word.compare("{") == 0) {
					level++;
				} else if (word.compare("}") == 0) {
					level--;
				}
			} while (line >> word);
		}

		if (level == 0)
			return true;
	}

	return false;
}

bool ASEFile::read_mesh_vertex_list(std::istream &is)
{
	char data[1024];
	memset(data, 0, sizeof(data));

	while (is.getline(data, sizeof(data) - 1)) {
		std::istringstream line(data);

		std::string firstword;
		line >> firstword;

		if (firstword.compare("}") == 0) {
			return true;

		} else if (firstword.compare("*MESH_VERTEX") == 0) {
			size_t index;
			float x, y, z;
			if (line >> index >> x >> y >> z) {
				math::Vector3f *v = new math::Vector3f(x, y, z);
				ase_vertexlist[index] = v;
				ase_box.expand(*v * SCALE);
				ase_vertexcount++;
			}
		}
	}

	return false;
}

bool ASEFile::read_mesh_face_list(std::istream &is)
{
	char data[1024];
	memset(data, 0, sizeof(data));

	while (is.getline(data, sizeof(data) - 1)) {
		std::istringstream line(data);

		std::string word;
		line >> word;

		if (word.compare("}") == 0) {
			return true;

		} else if (word.compare("*MESH_FACE") == 0) {
			std::string facestr;
			size_t a, b, c;
			if ((line >> facestr) &&
					(line >> word) && (line >> a) &&
					(line >> word) && (line >> b) &&
					(line >> word) && (line >> c)) {
				
				size_t index;
			
				if (facestr.size() && facestr[facestr.size()-1] == ':') {
					facestr.erase(facestr.size() - 1);
				}
				
				std::istringstream faceindexstr(facestr);
				if (faceindexstr >> index) {
					Triangle *triangle = new Triangle(*ase_vertexlist[a], *ase_vertexlist[b], *ase_vertexlist[c]);
					ase_facelist[index] = triangle;

					while (line >> word) {
						if (word.compare("*MESH_MTLID") == 0) {
							size_t submaterial_index = 0;
							if (line >> submaterial_index) {
								triangle->set_material_index(submaterial_index);
							}
						}
					}
				}
				
			}
			ase_facecount++;
		}
	}

	return false;
}

bool ASEFile::read_mesh_normals(std::istream &is)
{
	char data[1024];
	memset(data, 0, sizeof(data));

	size_t count = 0;

	size_t index = 0;
	size_t vertindex = 0;
	float x, y, z;
	FaceList::iterator it;

	while (is.getline(data, sizeof(data) - 1)) {
		std::istringstream line(data);

		std::string firstword;
		line >> firstword;

		if (firstword.compare("}") == 0) {
			//con_debug << "    " << count << " face normals" << std::endl;
			return true;

		} else if (firstword.compare("*MESH_FACENORMAL") == 0) {
			if (line >> index >> x >> y >> z) {
				it = ase_facelist.find(index);
				if (it != ase_facelist.end()) {
					(*it).second->normal().assign(x, y, z);
					count++;
				} else {
					con_warn << "  could not find face " << index << std::endl;
				}
				vertindex = 0;
			} else {
				it = ase_facelist.end();
			}
		} else if (firstword.compare("*MESH_VERTEXNORMAL") == 0) {

			if ((it != ase_facelist.end()) && (line >> index >> x >> y >> z)) {

				if (vertindex == 0) {
					(*it).second->n0().assign(x, y, z);
				} else if (vertindex == 1) {
					(*it).second->n1().assign(x, y, z);
				} else if (vertindex == 2) {
					(*it).second->n2().assign(x, y, z);
				}

				vertindex++;
			}
		}
	}

	return false;
}
bool ASEFile::read_mesh_tvertex_list(std::istream &is)
{
	size_t count = 0;

	char data[1024];
	memset(data, 0, sizeof(data));

	while (is.getline(data, sizeof(data) - 1)) {
		std::istringstream line(data);

		std::string firstword;
		line >> firstword;

		if (firstword.compare("}") == 0) {
			//con_debug << "    " << count << " texture vertices" << std::endl;
			return true;

		} else if (firstword.compare("*MESH_TVERT") == 0) {
			size_t index;
			float x, y, z;
			if (line >> index >> x >> y >> z) {
				math::Vector3f *v = new math::Vector3f(x, y, z);
				ase_tvertexlist[index] = v;
				count++;
			}
		}
	}

	return false;
}

bool ASEFile::read_mesh_tface_list(std::istream &is)
{
	size_t count = 0;

	char data[1024];
	memset(data, 0, sizeof(data));

	while (is.getline(data, sizeof(data) - 1)) {
		std::istringstream line(data);

		std::string firstword;
		line >> firstword;

		if (firstword.compare("}") == 0) {
			//con_debug << "    " << count << " face texture coordinates" << std::endl;
			return true;

		} else if (firstword.compare("*MESH_TFACE") == 0) {
			// face texture coordinates
			size_t index, a, b, c;
			if (line >> index >> a >> b >> c) {
				Triangle *triangle = ase_facelist[index];
				math::Vector3f *t0 = ase_tvertexlist[a];
				math::Vector3f *t1 = ase_tvertexlist[b];
				math::Vector3f *t2 = ase_tvertexlist[c];
				if (triangle) {
					if (t0)
						triangle->t0().assign(t0->x(), 1.0f - t0->y());
					if (t1)
						triangle->t1().assign(t1->x(), 1.0f - t1->y());
					if (t2)
						triangle->t2().assign(t2->x(), 1.0f - t2->y());
				}
				count++;
			}
		}
	}

	return false;
}

bool ASEFile::read_mesh(std::istream &is)
{
	char data[1024];
	memset(data, 0, sizeof(data));
	int level = 1;

	ase_vertexlist.clear();

	while (is.getline(data, sizeof(data) - 1)) {
		std::istringstream line(data);

		std::string word;
		line >> word;

		if ((level == 1) && (word.compare("*MESH_VERTEX_LIST") == 0)) {
			if ((line >> word) && (word.compare("{") == 0)) {
				//con_debug << "  " << name() << " *MESH_VERTEX_LIST" << std::endl;
				read_mesh_vertex_list(is);
			}

		} else if ((level == 1) && (word.compare("*MESH_FACE_LIST") == 0)) {
			if ((line >> word) && (word.compare("{") == 0)) {
				//con_debug << "  " << name() << " *MESH_FACE_LIST" << std::endl;
				read_mesh_face_list(is);
			}

		} else if ((level == 1) && (word.compare("*MESH_NORMALS") == 0)) {
			if ((line >> word) && (word.compare("{") == 0)) {
				//con_debug << "  " << name() << " *MESH_NORMALS" << std::endl;
				read_mesh_normals(is);
			}

		} else if ((level == 1) && (word.compare("*MESH_TVERTLIST") == 0)) {
			if ((line >> word) && (word.compare("{") == 0)) {
				//con_debug << "  " << name() << " *MESH_TVERTLIST" << std::endl;
				read_mesh_tvertex_list(is);
			}

		} else if ((level == 1) && (word.compare("*MESH_TFACELIST") == 0)) {
			if ((line >> word) && (word.compare("{") == 0)) {
				//con_debug << "  " << name() << " *MESH_TFACELIST" << std::endl;
				read_mesh_tface_list(is);
			}

		} else {

			do {
				if (word.compare("{") == 0) {
					level++;
				} else if (word.compare("}") == 0) {
					level--;
				}
			} while (line >> word);
		}

		if (level == 0)
			return true;
	}

	return false;
}

bool ASEFile::clear_geom()
{
	// delete texture vertices
	for (VertexList::iterator it = ase_tvertexlist.begin(); it != ase_tvertexlist.end(); it++) {
		delete(*it).second;
	}
	ase_tvertexlist.clear();

	// delete geometry vertices
	for (VertexList::iterator it = ase_vertexlist.begin(); it != ase_vertexlist.end(); it++) {
		delete(*it).second;
	}
	ase_vertexlist.clear();

	// delete faces
	for (FaceList::iterator it = ase_facelist.begin(); it != ase_facelist.end(); it++) {
		delete(*it).second;
	}
	ase_facelist.clear();

	return true;
}

bool ASEFile::read_geom(std::istream &is)
{
	char data[1024];
	memset(data, 0, sizeof(data));
	int level = 1;
	SubMaterialList *submateriallist = 0;

	while (is.getline(data, sizeof(data) - 1)) {

		std::istringstream line(data);
		std::string word;
		size_t index;
		line >> word;

		if ((level == 1) && (word.compare("*MESH") == 0)) {
			if ((line >> word) && (word.compare("{") == 0)) {
				//con_debug << "  " << name() << " " << "*MESH" << std::endl;
				read_mesh(is);
			}
		} else if ((level == 1) && (word.compare("*MATERIAL_REF") == 0)) {
			// the material to use for this GEOMOBJECT
			if (line >> index) {
				//con_debug << "  " << name() << " " << "*MATERIAL_REF " << index << std::endl;
				
				// find the Material for index
				MaterialList::iterator it = ase_materials.find(index);
				if (it != ase_materials.end()) {
					submateriallist = (*it).second;
				} else {
					submateriallist = 0;
				}
			}
		} else {
			do {
				if (word.compare("{") == 0) {
					level++;
				} else if (word.compare("}") == 0) {
					level--;
				}
			} while (line >> word);
		}

		if (level == 0) {
			
			// import only if the material is defined and there actually are triangles to import
			if (submateriallist && ase_facelist.size()) {
				
				// iterate submaterial list, create a fragment per material
				for (SubMaterialList::iterator smit = submateriallist->begin(); smit != submateriallist->end(); smit++) {
					size_t submaterial_index = (*smit).first;
					Material *material = (*smit).second;

					if (!material->ignore_is_set()) {
						
						// load GEOMOBJECT triangles with matching material into the fragment
						Fragment *fragment = new Fragment(Fragment::Triangles, material);
						for (FaceList::iterator it = ase_facelist.begin(); it != ase_facelist.end(); it++) {
							
							Triangle *triangle = (*it).second;
							if (triangle->material_index() == submaterial_index) {
								fragment->add_vertex((triangle->v0() * SCALE) , triangle->n0(), triangle->t0(), false);
								fragment->add_vertex((triangle->v1() * SCALE) , triangle->n1(), triangle->t1(), false);
								fragment->add_vertex((triangle->v2() * SCALE) , triangle->n2(), triangle->t2(), false);
							}
						}
						
						if (fragment->structural_size() + fragment->detail_size() > 0) {
							ase_fragmentgroup->add_fragment(fragment);
						} else {
							delete fragment;
						}
					}
				}
			}

			// clear geometry data
			clear_geom();
			return true;
		}
	}

	clear_geom();
	return false;
}

bool ASEFile::read()
{
	if (!read_header(asefile_ifs)) {
		con_warn << name() << ": not a valid ASE file!\n";
		return 0;
	}

	char data[1024];
	memset(data, 0, sizeof(data));

	while (asefile_ifs.getline(data, sizeof(data) - 1)) {
		std::istringstream line(data);

		std::string word;
		line >> word;

		if (word.compare("*GEOMOBJECT") == 0) {

			if ((line >> word) && (word.compare("{") == 0)) {
				//con_debug << "  " << name() << " " << "*GEOMOBJECT" << std::endl;
				read_geom(asefile_ifs);
			}
		} else if (word.compare("*MATERIAL_LIST") == 0) {

			if ((line >> word) && (word.compare("{") == 0)) {
				//con_debug << "  " << name() << " " << "*MATERIAL_LIST" << std::endl;
				read_material_list(asefile_ifs);
			}
		}
	}

	return true;
}

Model *ASEFile::load(const std::string &name)
{
	ASEFile asefile(name);

	if (!asefile.is_open() || !asefile.read() || !asefile.fragmentgroup()->size()) {
		// the fragmentgroup is not deleted by the destructor
		delete asefile.fragmentgroup();
		return 0;
	}
	
	// create a new model
	Model *model = new Model(name);

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

	asefile.fragmentgroup()->set_location(ase_center * -1.0f);
	
	for (FragmentGroup::Fragments::const_iterator fit = asefile.fragmentgroup()->fragments().begin(); fit != asefile.fragmentgroup()->fragments().end(); fit++) {		
		const Fragment *fragment = (*fit);
		model->model_tris_count += (fragment->structural_size() + fragment->detail_size()) / 3;
	}

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

	con_debug << "  " << asefile.name() << " " << asefile.vertexcount() << " vertices " << asefile.facecount() << " faces " << std::endl;
	
	return model;
}

} // namespace model