Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
blob: 585f13b3d6fa633579ac366b6fabe8ce2a212c60 (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
/*
   filesystem/mapfile.cc
   This file is part of the Osirion project and is distributed under
   the terms of the GNU General Public License version 2
*/

#include "filesystem/filesystem.h"
#include "math/mathlib.h"
#include "model/mapfile.h"
#include "model/vertexarray.h"
#include "sys/sys.h"

#include <sstream>
#include <string>

namespace model {

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

MapFile::MapFile() {}

MapFile::~MapFile() {}

bool MapFile::open(std::string const & name) {

	last_read_was_classname = false;
	last_read_was_key = false;
	key_current = "";
	value_current = "";
	classname_current = "";
	line_number = 0;
	parse_level = 0;
	brushes = 0;

	mapfile_name.assign("maps/");
	mapfile_name.append(name);
	mapfile_name.append(".map");
	
	filesystem::File *f = filesystem::open(mapfile_name.c_str());	
	if (!f) {
		con_warn << "Could not open " << mapfile_name << std::endl;
		return false;
	}
	
	std::string fn = f->path();
	fn.append(f->name());
	filesystem::close(f);
	
	mapfile_ifs.open(fn.c_str());
	if (!mapfile_ifs.is_open()) {
		con_warn << "Could not stream " << fn << "!\n";
		return false;
	}

	return true;
}


bool MapFile::got_classname() const {
	return last_read_was_classname;
}

bool MapFile::got_classname(const char * classnamelabel) const {
	return (last_read_was_classname && (classname_current.compare(classnamelabel) == 0));
}

bool MapFile::getline() {
	using math::Vector3f;

	char data[1024];

	last_read_was_classname = false;
	last_read_was_key = false;

	key_current = "";
	value_current = "";

	if (!mapfile_ifs.is_open())
		return false;

	if (mapfile_ifs.getline(data, 1023)) {
		line_number++;
		std::istringstream linestream(data);
		std::string firstword;
		
		if (linestream >> firstword) {
			if (!firstword.size()) {
				return true;

			} else if (firstword == "//") {
				return true;

			} else if (firstword == "{") {
				parse_level++;
			
			} else if (firstword == "}") {
				if ((parse_level == 2) && (classname_current == "worldspawn")) {
					// brush
					if (VertexArray::instance()) {
						// for every face
						for (std::vector<Plane *>::iterator face = planes.begin(); face != planes.end(); face++) {
							make_brushface((*face));
						}
					
						// clean planes
						for (std::vector<Plane *>::iterator it = planes.begin(); it != planes.end(); it++) {
							delete(*it);
						}
						planes.clear();
			
						brushes++;				
					}
					value_current.clear();
				}
				parse_level--;

			} else 	if (parse_level == 1) {

				if (firstword == "\"classname\"") {
					classname_current.clear();
	
					if (linestream >> classname_current) {
						if (classname_current.size() > 2) {
							classname_current.erase(0,1);
							classname_current.erase(classname_current.size()-1, 1);
							last_read_was_classname = true;
						} else {
							classname_current.clear();
						}
					}

				} else if ((firstword.size() > 2) && (firstword[0] == '\"') && (firstword[firstword.size()-1] == '\"')) {

					key_current.assign(firstword);
					key_current.erase(0,1);
					key_current.erase(key_current.size()-1, 1);
	
					value_current.clear();
					char c;
					while ((linestream.get(c)) && (c != '"'));
					while ((linestream.get(c)) && (c != '"'))
						value_current += c;
			
					last_read_was_key = true;
				}
				
			} else if (parse_level == 2) {

				if ((firstword == "(") && (classname_current == "worldspawn")) {
					// brush plane
					if (VertexArray::instance()) {
						Vector3f p1, p2, p3;
						std::string tmp;
						std::string texture;
						int n = 0;
						
						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;
	
						// content flags ?
						if (!(linestream >> n))
							n = 0;
						
						Plane *plane = new Plane(p1, p2, p3);
						plane->texture() = texture;
						if (n > 0)
							plane->detail() = true;

						// surface flags
						if (!(linestream >> n))
							n = 0;
						plane->surface_flags() = n;

						planes.push_back(plane);
					}
					value_current.clear();
				}
			}
		}
	} else {

		return false;
	}

	return true;
}

void MapFile::make_brushface(Plane *face)
{
	using math::Vector3f;
	
	// 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
	
	// vertex list
	std::vector<math::Vector3f *> vl;
	
	// calculate initial vertices on the bounding box
	
	// 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))) {

		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))) {
		
		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 {

		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<Plane *>::iterator pit = planes.begin(); pit != planes.end(); pit++) {
		Plane *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;
		}
		
		// 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) {
				next = *vl.at(i+1);
			} else {
				next = *vl.front();
			}
			
			Vector3f prev;
			if (i > 0) {
				prev = *vl.at(i-1);
			} else {
				prev = *vl.back();
			}
			
			if ((v.x*plane->normal().x + v.y*plane->normal().y + v.z*plane->normal().z +plane->d()) < MIN_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()) > -MIN_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);

					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;
					}
					
					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()) > -MIN_DELTA) {
					
					// 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;
					}
					
					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() == "colors/red") {
			color = new math::Color(1, 0, 0);
		} else if (face->texture() == "colors/green") {
			color = new math::Color(0, 1, 0);
		} else if (face->texture() == "colors/blue") {
			color = new math::Color(0, 0, 1);
		} 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) *= SCALE;

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

				if (class_minbbox[i] > (*(*it))[i])
					class_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, face->detail());
				class_etris.push_back(triangle);
			} else if ((face->surface_flags() & 1) == 1 ) {
				// lvertices
				Triangle *triangle = new Triangle(*(*vn1), *(*vn), *(*v0), n, color, face->detail());
				class_ltris.push_back(triangle);
			} else {
				Triangle *triangle = new Triangle(*(*vn1), *(*vn), *(*v0), n, color, face->detail());
				class_tris.push_back(triangle);
			}
		
			delete (*vn);
			vl.pop_back();
		}
		if (color) delete color;
	} else {
		con_debug << "Unresolved face!\n";
	}
	
	// clean up the vertex list
	for (std::vector<Vector3f *>::iterator it = vl.begin(); it != vl.end(); it++) {
		delete(*it);
	}
	
	vl.clear();
}

bool MapFile::got_key_string(const char * keylabel, std::string & valuestring) {
	if (last_read_was_key && (key_current.compare(keylabel) == 0 )) {
		valuestring.assign(value_current);
		return true;
	} else {
		return false;
	}
}

bool MapFile::got_key_vector3f(const char * keylabel, math::Vector3f & v) {
	if (last_read_was_key && (key_current.compare(keylabel) == 0 )) {
		std::istringstream is(value_current);
		float x, y, z;
		if ((is >> x) && (is >> y) && (is >> z)) {
			v = math::Vector3f(x,y,z);
		} else {
			 v= math::Vector3f();
		}
		return true;
	} else {
		return false;
	}
}

bool MapFile::got_key_float(const char * keylabel, float & f) {
	if (last_read_was_key && (key_current.compare(keylabel) == 0 )) {
		std::istringstream is(value_current);
		if (!(is >> f)) {
			f = 0;
		}
		return true;
	} else {
		return false;
	}
}

bool MapFile::got_key_int(const char * keylabel, unsigned int & u)
{
	if (last_read_was_key && (key_current.compare(keylabel) == 0 )) {
		std::istringstream is(value_current);
		if (!(is >> u)) {
			u = 0;
		}
		return true;
	} else {
		return false;
	}
}

bool MapFile::got_key(const char * keylabel) {
	return (last_read_was_key && (key_current.compare(keylabel) == 0 ));
}

bool MapFile::got_key_angle(const char * keylabel, float & f) {
	if (last_read_was_key && (key_current.compare(keylabel) == 0 )) {
		std::istringstream is(value_current);
		if ((is >> f)) {
			f = math::degrees360f(f);
		} else {
			f = 0;
		}
		return true;
	} else {
		return false;
	}
}

bool MapFile::got_key_color(const char * keylabel, math::Color & color) {
	if (last_read_was_key && (key_current.compare(keylabel) == 0 )) {
		std::istringstream is(value_current);
		float r, g, b;
		if ((is >> r) && (is >> g) && (is >> b)) {
			if ((r > 1) || (g > 1) || (b > 1)) {
				r /= 255; g /= 255; b /= 255;
			}
			color = math::Color(r, g, b);
		} else {
			color = math::Color();
		}
		return true;
	} else {
		return false;
	}
}

void MapFile::close()
{
	mapfile_ifs.close();
}

} // namespace filesystem