Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
blob: 8c853f8224b44fa05e906d6010be547ff026802b (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
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
/*
   render/model.h
   This file is part of the Osirion project and is distributed under
   the terms of the GNU General Public License version 2
*/

#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
#include <sstream>
#include <vector>
#include <list>

#include "model/model.h"
#include "filesystem/filesystem.h"

namespace model
{

const float MAX_BOUNDS = 16384;
const float delta = 10e-10;

/* ---------- core::Triangle ---------------------------------------  */
Triangle::Triangle(math::Vector3f const &v0, math::Vector3f const &v1, math::Vector3f const &v2, math::Vector3f const &n, math::Color *color, bool detail) :
	triangle_v0(v0),
	triangle_v1(v1),
	triangle_v2(v2),
	triangle_normal(n)
{

	if (color)
		triangle_color = *color;
	else
		math::Color(1.0f, 1.0f, 1.0f);	

	triangle_detail = detail;
}

Triangle::~Triangle()
{
}

/* ---------- core::Model ------------------------------------------  */

std::map<std::string, Model*> Model::registry;

Model::Model(std::string const & name) :
		model_name(name)
{
	model_valid = false;
	model_scale = 1.0f / 1024.0f;

	model_first_vertex = 0;
	model_first_evertex = 0;

	model_vertex_count = 0;
	model_vertex_countdetail = 0;
	model_evertex_count = 0;
	model_evertex_countdetail = 0;
	
	std::string fn("maps/");
	fn.append(name);
	fn.append(".map");
	filesystem::File *f = filesystem::open(fn.c_str());
	
	if (!f) {
		return;
	}
	
	fn = f->path();
	fn.append(f->name());
	filesystem::close(f);
	
	std::ifstream ifs(fn.c_str());
	if (!ifs.is_open()) {
		con_warn << "Could not stream " << fn << "!\n";
		return;
	}
	
	// --------- the actual reading
	using math::Vector3f;
	using math::Plane3f;
	
	std::vector<Plane3f *> 	planes;
	unsigned int 		level = 0;
	char 			data[1024];
	
	std::string 		class_name;
	math::Vector3f 		class_origin;
	float 			class_angle = 0;
	math::Color		class_color;
	unsigned int		class_spawnflags = 0;
	float			class_light = 0.0f;
	float			class_radius = 0.0f;
	float			class_frequency = 1.0f;
	float			class_offset = 0;
	float			class_time = 0.0f;
	unsigned int		class_flare = 0;
	bool			brush_detail = false;

	while (ifs) {
		ifs.getline(data, 1023);
		std::istringstream linestream(data);
		std::string firstword;
		
		if (linestream >> firstword) {
			if (firstword == "//") {
				//cout << "   COMMENT!" << std::endl;
				continue;
			} else if (firstword == "{") {
				if (!level) {
					class_angle = 0;
					class_name.clear();
					class_origin = math::Vector3f(0,0,0);
					class_color = math::Color(1, 1, 1);
					class_spawnflags = 0;
					class_light = 0.0f;
					class_radius = 0.0f;
					class_offset = 0;
					class_frequency = 1.0f;
					class_time = 0.0f;
					class_flare = 0;
					brush_detail = false;
				}
				level ++;
				//cout << "   LEVEL +" << level << std::endl;
			} else if (firstword == "}") {
				//cout << "   LEVEL -" << level << std::endl;
				if ((level == 2) && (class_name == "worldspawn")) {

					if (VertexArray::instance()) {
						// for every face
						std::vector<Vector3f *>points;
						for (std::vector<Plane3f *>::iterator face = planes.begin(); face != planes.end(); face++) {
							make_face((*face), planes, brush_detail);
						}
					}

					// clean planes
					for (std::vector<Plane3f *>::iterator it = planes.begin(); it != planes.end(); it++) {
						delete(*it);
					}
					planes.clear();
					brush_detail = false;
					
				} else if ((level == 1) && (class_name == "target_engine")) {
					model::Engine *engine = new Engine(class_origin * model_scale);
					if (class_radius)
						engine->engine_radius = class_radius / 100.0f;
					add_engine(engine);

				} else if ((level == 1) && (class_name == "light")) {
					Light *light = new Light(class_origin * model_scale, class_color, (class_spawnflags & 1) == 1);
					if (class_light)
						light->light_radius = class_light / 100.0f;
					else if (class_radius)
						light->light_radius = class_radius / 100.0f;
					if (class_offset > 0)
						light->light_offset = class_offset;
					if (class_frequency > 0 )
						light->light_frequency = class_frequency;
					if (class_time > 0 )
						light->light_time = class_time;
					if (class_flare > 0)
						light->light_flare = class_flare;
					add_light(light);
				}
				
				if (level == 1) {
					class_angle = 0;
					class_name.clear();
					class_origin = Vector3f(0,0,0);
					class_color = math::Color(1, 1, 1);
					class_spawnflags = 0;
				}
				
				level--;
				
			} else if (firstword == "\"classname\"") {
				class_name.clear();
				if (linestream >> class_name) {
					if (class_name.size() > 2) {
						class_name.erase(0,1);
						class_name.erase(class_name.size()-1, 1);
						//linestream >> class_name;
						//con_debug << "   classname '" << class_name << "'" << std::endl;
					} else {
						class_name.clear();
					}
				} else {
					//cout << "   EMPTY CLASS" << std::endl;
				}
			} else if (firstword == "\"origin\"") {
				std::string tmp;
				char c;
				while ((linestream.get(c)) && (c != '"'));
				while ((linestream.get(c)) && (c != '"'))
					tmp += c;
				std::istringstream is(tmp);
				is >> class_origin.x;
				is >> class_origin.y;
				is >> class_origin.z;
				//con_debug << "   origin '" << class_origin << "'" << std::endl;

			} else if (firstword == "\"_color\"") {
				std::string tmp;
				char c;
				while ((linestream.get(c)) && (c != '"'));
				while ((linestream.get(c)) && (c != '"'))
					tmp += c;
				std::istringstream is(tmp);
				is >> class_color.r;
				is >> class_color.g;
				is >> class_color.b;

			} else if (firstword == "\"angle\"") {
				std::string tmp;
				char c;
				while ((linestream.get(c)) && (c != '"'));
				while ((linestream.get(c)) && (c != '"'))
					tmp += c;
				std::istringstream is(tmp);
				is >> class_angle;
				//con_debug << "   angle '" << class_angle << "'" << std::endl;

			} else if (firstword == "\"spawnflags\"") {
				std::string tmp;
				char c;
				while ((linestream.get(c)) && (c != '"'));
				while ((linestream.get(c)) && (c != '"'))
					tmp += c;
				std::istringstream is(tmp);
				is >> class_spawnflags;
				//con_debug << "   spawnflags '" << class_spawnflags << "'" << std::endl;

			} else if (firstword == "\"light\"") {
				std::string tmp;
				char c;
				while ((linestream.get(c)) && (c != '"'));
				while ((linestream.get(c)) && (c != '"'))
					tmp += c;
				std::istringstream is(tmp);
				is >> class_light;

			} else if (firstword == "\"radius\"") {
				std::string tmp;
				char c;
				while ((linestream.get(c)) && (c != '"'));
				while ((linestream.get(c)) && (c != '"'))
					tmp += c;
				std::istringstream is(tmp);
				is >> class_radius;

			} else if (firstword == "\"frequency\"") {
				std::string tmp;
				char c;
				while ((linestream.get(c)) && (c != '"'));
				while ((linestream.get(c)) && (c != '"'))
					tmp += c;
				std::istringstream is(tmp);
				is >> class_frequency;

			} else if (firstword == "\"offset\"") {
				std::string tmp;
				char c;
				while ((linestream.get(c)) && (c != '"'));
				while ((linestream.get(c)) && (c != '"'))
					tmp += c;
				std::istringstream is(tmp);
				is >> class_offset;
			
			} else if (firstword == "\"time\"") {
				std::string tmp;
				char c;
				while ((linestream.get(c)) && (c != '"'));
				while ((linestream.get(c)) && (c != '"'))
					tmp += c;
				std::istringstream is(tmp);
				is >> class_time;

			} else if (firstword == "\"flare\"") {
				std::string tmp;
				char c;
				while ((linestream.get(c)) && (c != '"'));
				while ((linestream.get(c)) && (c != '"'))
					tmp += c;
				std::istringstream is(tmp);
				is >> class_flare;

			} else if (firstword == "(") {
				if ((level == 2) && (class_name == "worldspawn")) {
					//cout << "   BRUSH PLANE" << std::endl;
					Vector3f p1;
					Vector3f p2;
					Vector3f p3;
					std::string tmp;
					std::string texture;
					int n;
					
					linestream >> p1;
					linestream >> tmp; // )
					linestream >> tmp; // (
					linestream >> p2;
					linestream >> tmp; // )
					linestream >> tmp; // (
					linestream >> p3;
					linestream >> tmp; // )
					linestream >> texture;
					
					// 5 numbers (texture alignment?)
					for (int i=0; i < 5; i++)
						linestream >> tmp;

					if (linestream >> n) {
						if (n > 0)
							brush_detail =  true;
					}
					//cout << data << std::endl;
					//cout << "(" << p1 << ") (" <<  p2 << ") (" << p3 << ") " << texture << std::endl;
					
					Plane3f *plane = new Plane3f(p1, p2, p3);
					plane->texture() = texture;
					planes.push_back(plane);
					//cout << "normal " << plane->normal() << std::endl;
				} else {
					//cout << "   UNKNOWN line for '" << classname << "' level " << level << std::endl;
				}
			}
		}
	}
	
	ifs.close();

	if ((model_tris.size() + model_etris.size()) > 0) {

		math::Vector3f center = (model_minbbox + model_maxbbox) / 2;

		model_minbbox -= center;
		model_maxbbox -= center;
		model_radius = model_maxbbox.length();

		// structural triangles
		model_first_vertex = VertexArray::instance()->index()/3;
		for (std::list<Triangle *>::iterator it = model_tris.begin(); it != model_tris.end(); it++) {
			Triangle *triangle = (*it);
			if (!triangle->detail()) {
				VertexArray::instance()->add_vertex(triangle->triangle_v0-center, triangle->normal(), triangle->color() );
				VertexArray::instance()->add_vertex(triangle->triangle_v1-center, triangle->normal(), triangle->color() );
				VertexArray::instance()->add_vertex(triangle->triangle_v2-center, triangle->normal(), triangle->color() );
				model_vertex_count += 3;
			}
		}
		// detail triangles
		for (std::list<Triangle *>::iterator it = model_tris.begin(); it != model_tris.end(); it++) {
			Triangle *triangle = (*it);
			if (triangle->detail()) {
				VertexArray::instance()->add_vertex(triangle->triangle_v0-center, triangle->normal(), triangle->color() );
				VertexArray::instance()->add_vertex(triangle->triangle_v1-center, triangle->normal(), triangle->color() );
				VertexArray::instance()->add_vertex(triangle->triangle_v2-center, triangle->normal(), triangle->color() );
				model_vertex_countdetail += 3;
			}
			delete triangle;
		}
		model_tris.clear();
		
		// structural etriangles
		model_first_evertex = VertexArray::instance()->index()/3;
		for (std::list<Triangle *>::iterator it = model_etris.begin(); it != model_etris.end(); it++) {
			Triangle *triangle = (*it);
			if (!triangle->detail()) {
				VertexArray::instance()->add_vertex(triangle->triangle_v0-center, triangle->normal(), triangle->color() );
				VertexArray::instance()->add_vertex(triangle->triangle_v1-center, triangle->normal(), triangle->color() );
				VertexArray::instance()->add_vertex(triangle->triangle_v2-center, triangle->normal(), triangle->color() );
				model_evertex_count += 3;
			}
		}

		// detail etriangles
		for (std::list<Triangle *>::iterator it = model_etris.begin(); it != model_etris.end(); it++) {
			Triangle *triangle = (*it);
			if (triangle->detail()) {
				VertexArray::instance()->add_vertex(triangle->triangle_v0-center, triangle->normal(), triangle->color() );
				VertexArray::instance()->add_vertex(triangle->triangle_v1-center, triangle->normal(), triangle->color() );
				VertexArray::instance()->add_vertex(triangle->triangle_v2-center, triangle->normal(), triangle->color() );
				model_evertex_countdetail += 3;
			}
			delete triangle;
		}
		model_etris.clear();

		// reposition light and engines
		for (std::list<Engine *>::iterator eit = model_engine.begin(); eit != model_engine.end(); eit++) {
			(*eit)->engine_location -= center;
		}
	
		for (std::list<Light *>::iterator lit = model_light.begin(); lit != model_light.end(); lit++) {
			(*lit)->light_location -= center;
		}

		model_valid = true;	
	}

	con_debug  << "  maps/" << name << ".map " << tris() << " triangles (" << details() << " detail)" << std::endl;
}

Model::~Model()
{
	// delete all engines
	for (std::list<Engine *>::iterator eit = model_engine.begin(); eit != model_engine.end(); eit++) {
		delete(*eit);
	}
	model_engine.clear();
	
	// delete all lights
	for (std::list<Light *>::iterator lit = model_light.begin(); lit != model_light.end(); lit++) {
		delete (*lit);
	}
	model_light.clear();
}

size_t Model::tris() const
{ 
		return ((model_vertex_count + model_vertex_countdetail + 
		model_evertex_count + model_evertex_countdetail)/3); 
}

size_t Model::details() const
{ 
		return ((model_vertex_countdetail + model_evertex_countdetail)/3); 
}

void Model::make_face(math::Plane3f *face, std::vector<math::Plane3f *> & planes, bool detail)
{
	using math::Vector3f;
	using math::Plane3f;
	
	// ignore caulk
	if (face->texture() == "common/caulk") {
		return;
	}
	
	// FIXME clip should be parsed as collision blocks
	if (face->texture() == "common/clip") {
		return;
	}

	// using suggestions from
	// http://www.flipcode.com/archives/Level_Editing.shtml
	
	std::vector<math::Vector3f *> vl;
	
	// inital vertices
	
	// check if the face is x-axis oriented
	if ((fabsf(face->normal().x) >= fabsf(face->normal().y)) && (fabsf(face->normal().x) >=  fabsf(face->normal().z))) {
		//cout << "  x oriented" << std::endl;
		
		if (face->normal().x >= 0) {
			vl.push_back(new math::Vector3f(0, -MAX_BOUNDS, -MAX_BOUNDS));
			vl.push_back(new math::Vector3f(0, -MAX_BOUNDS, MAX_BOUNDS));
			vl.push_back(new math::Vector3f(0, MAX_BOUNDS, MAX_BOUNDS));
			vl.push_back(new math::Vector3f(0, MAX_BOUNDS, -MAX_BOUNDS));
		} else {
			vl.push_back(new math::Vector3f(0, MAX_BOUNDS, -MAX_BOUNDS));
			vl.push_back(new math::Vector3f(0, MAX_BOUNDS, MAX_BOUNDS));
			vl.push_back(new math::Vector3f(0, -MAX_BOUNDS, MAX_BOUNDS));
			vl.push_back(new math::Vector3f(0, -MAX_BOUNDS, -MAX_BOUNDS));
		}
		// calculate the x coordinate of each face vertex
		for (std::vector<Vector3f *>::iterator it = vl.begin(); it != vl.end(); it++) {
			(*it)->x = (-face->d() -
				    face->normal().z * (*it)->z -
				    face->normal().y * (*it)->y) /
				   face->normal().x;
		}
	}
	
	// check if the face is y-axis oriented
	else if ((fabsf(face->normal().y) >= fabsf(face->normal().x)) && (fabsf(face->normal().y) >=  fabsf(face->normal().z))) {
		//cout << "  y oriented" << std::endl;
		
		if (face->normal().y >= 0) {
			vl.push_back(new Vector3f(MAX_BOUNDS, 0, -MAX_BOUNDS));
			vl.push_back(new Vector3f(MAX_BOUNDS, 0, MAX_BOUNDS));
			vl.push_back(new Vector3f(-MAX_BOUNDS, 0, MAX_BOUNDS));
			vl.push_back(new Vector3f(-MAX_BOUNDS, 0, -MAX_BOUNDS));
		} else {
			vl.push_back(new Vector3f(-MAX_BOUNDS, 0, -MAX_BOUNDS));
			vl.push_back(new Vector3f(-MAX_BOUNDS, 0, MAX_BOUNDS));
			vl.push_back(new Vector3f(MAX_BOUNDS, 0, MAX_BOUNDS));
			vl.push_back(new Vector3f(MAX_BOUNDS, 0, -MAX_BOUNDS));
		}
		
		// calculate the x coordinate of each face vertex
		for (std::vector<Vector3f *>::iterator it = vl.begin(); it != vl.end(); it++) {
			(*it)->y = (-face->d() -
				    face->normal().z * (*it)->z -
				    face->normal().x * (*it)->x) /
				   face->normal().y;
		}
	}
	
	// face must be z-axis oriented
	else {
		//cout << "  z oriented" << std::endl;
		if (face->normal().z >= 0) {
			vl.push_back(new Vector3f(-MAX_BOUNDS, -MAX_BOUNDS, 0));
			vl.push_back(new Vector3f(-MAX_BOUNDS, MAX_BOUNDS, 0));
			vl.push_back(new Vector3f(MAX_BOUNDS, MAX_BOUNDS, 0));
			vl.push_back(new Vector3f(MAX_BOUNDS, -MAX_BOUNDS, 0));
		} else {
			vl.push_back(new Vector3f(MAX_BOUNDS, -MAX_BOUNDS, 0));
			vl.push_back(new Vector3f(MAX_BOUNDS, MAX_BOUNDS, 0));
			vl.push_back(new Vector3f(-MAX_BOUNDS, MAX_BOUNDS, 0));
			vl.push_back(new Vector3f(-MAX_BOUNDS, -MAX_BOUNDS, 0));
		}
		
		// calculate the x coordinate of each face vertex
		for (std::vector<Vector3f *>::iterator it = vl.begin(); it != vl.end(); it++) {
			(*it)->z = (-face->d() -
				    face->normal().x * (*it)->x -
				    face->normal().y * (*it)->y) /
				   face->normal().z;
		}
	}
	
	
	// intersect the face with every plane
	for (std::vector<Plane3f *>::iterator pit = planes.begin(); pit != planes.end(); pit++) {
		Plane3f *plane = (*pit);
		if (plane == face) {
			continue;
		}
		
		Vector3f fn = crossproduct(face->point(1)-face->point(0), face->point(2)-face->point(0));
		Vector3f pn = crossproduct(plane->point(1)-plane->point(0), plane->point(2)-plane->point(0));
		
		Vector3f t = crossproduct(fn, pn);
		if ((t.x == 0)  && (t.y == 0) && (t.z == 0)) {
			continue;
		}
		
		//cout << "  intersecting with plane with normal " << plane->normal() << std::endl;
	
		// intersect face with plane
		for (int i=0; vl.size() - i > 0; i++) {
		
			Vector3f v(*vl.at(i));
			
			
			Vector3f next;
			if (vl.size() - i > 1) {
				//cout << " -- at " << i+1 << std::endl;
				next = *vl.at(i+1);
			} else {
				next = *vl.front();
			}
			
			Vector3f prev;
			if (i > 0) {
				//cout << " -- at " << i-1 << std::endl;
				prev = *vl.at(i-1);
			} else {
				prev = *vl.back();
			}
			
			//cout << " vertex " << i << " prev " << prev << " v " << v << " next " << next << std::endl;
			if ((v.x*plane->normal().x + v.y*plane->normal().y + v.z*plane->normal().z +plane->d()) < delta) {
			
				// find current
				std::vector<Vector3f *>::iterator vit = vl.begin();
				while ((*vit) != vl.at(i)) {
					vit++;
				}
				
				// check if prev - v intersects with plane
				if ((prev.x*plane->normal().x + prev.y*plane->normal().y + prev.z*plane->normal().z + plane->d()) > -delta) {
				
					// calculate intersection
					float t1 = -plane->normal().x * prev.x - plane->normal().y * prev.y - plane->normal().z * prev.z -plane->d();
					float t2 = (plane->normal().x * v.x - plane->normal().x * prev.x +
						    plane->normal().y * v.y - plane->normal().y * prev.y +
						    plane->normal().z * v.z - plane->normal().z * prev.z);
					//cout << "prev t2 " << t2 << std::endl;
					Vector3f *s = new Vector3f;
					
					if (t2 == 0) {
						*s = v;
					} else {
						for (int j = 0; j < 3; j++)
							(*s)[j] = prev [j] + t1 * (v[j] - prev[j]) / t2;
					}
					
					//cout << "  added " << *s << std::endl;
					vit = vl.insert(vit,s);
					vit++;
					i++;
				}
				
				// check if next - v intersects with plane
				if ((next.x*plane->normal().x +  next.y*plane->normal().y + next.z*plane->normal().z + plane->d()) > -delta) {
					// calculate intersection
					
					// calculate intersection
					float t1 = -plane->normal().x * v.x - plane->normal().y * v.y - plane->normal().z * v.z -plane->d();
					float t2 = (plane->normal().x * next.x - plane->normal().x * v.x +
						    plane->normal().y * next.y - plane->normal().y * v.y +
						    plane->normal().z * next.z - plane->normal().z * v.z);
					//cout << "next t2 " << t2 << std::endl;
					Vector3f *s = new Vector3f;
					
					if (t2 == 0) {
						*s = v;
					} else {
						for (int j = 0; j < 3; j++)
							(*s)[j] = v [j] + t1 * (next[j] - v[j]) / t2;
					}
					
					//cout << "  added " << *s << std::endl;
					vit = vl.insert(vit,s);
					vit++;
					i++;
				}
				
				// erase
				delete *vit;
				vl.erase(vit);
				i--;
			}
			
		}
	}
	
	if (vl.size() > 2) {
	
		math::Color *color = 0;
		if (face->texture() == "colors/white") {
			color = new math::Color(1, 1, 1);
		} else if (face->texture() == "colors/grey90") {
			color = new math::Color(0.9, 0.9, 0.9);
		} else if (face->texture() == "colors/grey75") {
			color = new math::Color(0.75, 0.75, 0.75);
		} else if (face->texture() == "colors/grey50") {
			color = new math::Color(0.5, 0.5, 0.5);
		} else if (face->texture() == "colors/grey25") {
			color = new math::Color(0.25, 0.25, 0.25);
		} else if (face->texture() == "colors/black") {
			color = new math::Color(0, 0, 0);
		} else if (face->texture() == "common/entity") {
			color = 0;
		} else
			// unknown textures get hot pink
			color = new math::Color(1.0f, 0.0, 1.0f);
	
		// calculate bounding box
		for (std::vector<Vector3f *>::iterator it = vl.begin(); it != vl.end(); it++) {	

			*(*it) *= model_scale;

			for (int i=0; i < 3; i++) {
				if (model_maxbbox[i] < (*(*it))[i])
					model_maxbbox[i] = (*(*it))[i];

				if (model_minbbox[i] > (*(*it))[i])
					model_minbbox[i] = (*(*it))[i];
			}
		}

		// split face into triangles
		while (vl.size() >2 ) {
			std::vector<Vector3f *>::iterator v0 = vl.begin();
			std::vector<Vector3f *>::reverse_iterator vn = vl.rbegin();
			std::vector<Vector3f *>::reverse_iterator vn1 = vl.rbegin();
			++vn1;
			
			Vector3f n(face->normal()*-1);
			n.normalize();

			if (!color) {
				// evertices will be added to the VertexArray after normal vertices
				Triangle *triangle = new Triangle(*(*vn1), *(*vn), *(*v0), n, 0, detail);
				model_etris.push_back(triangle);
				//VertexArray::add_evertex(*(*vn1), n);
				//VertexArray::add_evertex(*(*vn), n);
				//VertexArray::add_evertex(*(*v0), n);
				//model_evertex_count += 3;
			} else {
				Triangle *triangle = new Triangle(*(*vn1), *(*vn), *(*v0), n, color, detail);
				model_tris.push_back(triangle);
				//VertexArray::add_vertex(*(*vn1), n, *color);
				//VertexArray::add_vertex(*(*vn), n, *color);
				//VertexArray::add_vertex(*(*v0), n, *color);
				//model_vertex_count += 3;
			}
		
			delete (*vn);
			vl.pop_back();
		}
		//add_face(mf);
		if (color) delete color;
	} else {
		con_debug << "Unresolved face!\n";
	}
	
	for (std::vector<Vector3f *>::iterator it = vl.begin(); it != vl.end(); it++) {
		delete(*it);
	}
	
	vl.clear();
	
}

void Model::add_engine(Engine *engine)
{
	model_engine.push_back(engine);
}

void Model::add_light(Light *light)
{
	model_light.push_back(light);
}

Model *Model::find(std::string const & name)
{
	std::map<std::string, Model*>::iterator it = registry.find(name);
	if (it == registry.end())
		return 0;
	else
		return (*it).second;
}

Model *Model::get(std::string const & name)
{
	Model *model = find(name);
	if (!model) {
		model = new Model(name);
		registry[model->name()] = model;
	}
	return model;
}

void Model::clear()
{
	// delete all models in the registry
	for (std::map<std::string, Model*>::iterator mit = registry.begin(); mit != registry.end(); mit++) {
		delete(*mit).second;
	}
	registry.clear();

	// clear the vertex array
	if (VertexArray::instance())
		VertexArray::instance()->clear();
}

void Model::list()
{
	for (std::map<std::string, Model*>::iterator mit = registry.begin(); mit != registry.end(); mit++) {
		con_print << "  " << (*mit).second->name() << " "
			<< (*mit).second->tris() << " triangles ("
			<< (*mit).second->details() << " detail) "
			<< (*mit).second->model_engine.size() << " engines "
			<< (*mit).second->model_light.size() << " lights\n";
	}
	con_print << registry.size() << " registered models" << std::endl;
	if (VertexArray::instance())
		con_print << "vertex array " << (VertexArray::instance()->index() * 100 / VertexArray::instance()->size()) 
				<< "% used" << std::endl;
}

}