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

#include "model/engine.h"
#include "model/light.h"
#include "model/map.h"
#include "model/mapfile.h"
#include "model/vertexarray.h"

#include "sys/sys.h"

namespace model {

// function to test spawnflags
inline bool spawnflag_isset(unsigned int spawnflags, unsigned int flag) {
	return ((spawnflags & flag) == flag);
}

Model * Map::load(std::string const &name)
{
	// open the .map file
	MapFile mapfile;

	if (!mapfile.open(name)) {
		return 0;
	}

	Model *model = new Model(name);
	Light *light = 0;
	Flare *flare = 0;
	Engine *engine = 0;

	unsigned int u;

	while (mapfile.getline()) {

		if (mapfile.got_classname("worldspawn")) {

			// new wordspawn

		} else if (mapfile.classname().compare("worldspawn") == 0 ) {

			// worldspawn attributes
			if (mapfile.got_key_int("enginesound", u)) {
				model->model_enginesound = u;
				continue;
			}

		} else if (mapfile.got_classname("light")) {

			// new light
			light = new Light();
			model->add_light(light);

		} else if (mapfile.classname().compare("light") == 0 ) {

			// light attributes
			if (mapfile.got_key_vector3f("origin", light->light_location)) {
				light->light_location *= SCALE;
				continue;

			} else if (mapfile.got_key_color("_color", light->light_color)) {
				continue;

			} else if (mapfile.got_key_int("spawnflags", u)) {
				light->light_strobe = spawnflag_isset(u, 1);
				light->light_entity = spawnflag_isset(u, 2);

			} else if (mapfile.got_key_float("light", light->light_radius)) {
				light->light_radius /= 100.0f;

			} else if (mapfile.got_key_float("frequency", light->light_frequency)) {
				continue;

			} else if (mapfile.got_key_float("offset", light->light_offset)) {
				continue;
			
			} else if (mapfile.got_key_float("time", light->light_time)) {
				continue;

			}  else if (mapfile.got_key_int("flare", light->light_flare)) {
				continue;

			}
		
		} else if (mapfile.got_classname("target_flare")) {

			// new flare
			flare = new Flare();
			model->add_flare(flare);

		} else if (mapfile.classname().compare("target_flare") == 0 ) {
			
			// flare attributes
			if (mapfile.got_key_vector3f("origin", flare->light_location)) {
				flare->light_location *= SCALE;
				continue;

			} else if (mapfile.got_key_color("_color", flare->light_color)) {
				continue;

			} else if (mapfile.got_key_int("spawnflags", u)) {
				flare->light_strobe = spawnflag_isset(u, 1);
				flare->light_entity = spawnflag_isset(u, 2);

			} else if (mapfile.got_key_float("radius", flare->light_radius)) {
				flare->light_radius /= 100.0f;

			} else if (mapfile.got_key_float("frequency", flare->light_frequency)) {
				continue;

			} else if (mapfile.got_key_float("offset", flare->light_offset)) {
				continue;
			
			} else if (mapfile.got_key_float("time", flare->light_time)) {
				continue;

			}  else if (mapfile.got_key_int("flare", flare->light_flare)) {
				continue;

			}  else if (mapfile.got_key_float("angle", flare->flare_angle)) {
				flare->flare_angle = math::degrees360f(flare->flare_angle);
				continue;

			}
		
		} else if (mapfile.got_classname("target_engine")) {
			// new engine
			engine = new Engine();
			model->add_engine(engine);

		} else if (mapfile.classname().compare("target_engine") == 0 ) {
			// engine attributes

			if (mapfile.got_key_vector3f("origin", engine->engine_location)) {
				engine->engine_location *= SCALE;
				continue;

			} else if (mapfile.got_key_color("_color", engine->engine_color)) {
				continue;

			} else if (mapfile.got_key_float("radius", engine->engine_radius)) {
				engine->engine_radius /= 100.0f;
				continue;

			}  else if (mapfile.got_key_int("flare", engine->engine_flare)) {
				continue;

			} else if (mapfile.got_key_color("_color", engine->engine_color)) {
				continue;
			} 
		}
	}

	mapfile.close();

	// process the triangles calculated by the mapfile and add them to the vertex array
	if (VertexArray::instance() && !VertexArray::instance()->overflow() && ((mapfile.class_tris.size() + mapfile.class_etris.size() + mapfile.class_ltris.size()) > 0)) {

		math::Vector3f center = (mapfile.class_minbbox + mapfile.class_maxbbox) / 2;

		model->model_minbbox = mapfile.class_minbbox -  center;
		model->model_maxbbox = mapfile.class_maxbbox -  center;

		model->model_radius = model->model_maxbbox.length();

		// structural triangles
		model->model_first_vertex = VertexArray::instance()->index()/3;
		for (std::list<Triangle *>::iterator it = mapfile.class_tris.begin(); it != mapfile.class_tris.end() && !VertexArray::instance()->overflow(); it++) {
			Triangle *triangle = (*it);
			if (!triangle->detail()) {
				size_t count = 0;
				count += VertexArray::instance()->add_vertex(triangle->triangle_v0-center, triangle->normal(), triangle->color() );
				count += VertexArray::instance()->add_vertex(triangle->triangle_v1-center, triangle->normal(), triangle->color() );
				count += VertexArray::instance()->add_vertex(triangle->triangle_v2-center, triangle->normal(), triangle->color() );
				if (count == 3)
					model->model_vertex_count += count;
			}
		}

		// detail triangles
		for (std::list<Triangle *>::iterator it = mapfile.class_tris.begin(); it != mapfile.class_tris.end() && !VertexArray::instance()->overflow(); it++) {
			Triangle *triangle = (*it);
			if (triangle->detail()) {
				size_t count = 0;
				count += VertexArray::instance()->add_vertex(triangle->triangle_v0-center, triangle->normal(), triangle->color() );
				count += VertexArray::instance()->add_vertex(triangle->triangle_v1-center, triangle->normal(), triangle->color() );
				count += VertexArray::instance()->add_vertex(triangle->triangle_v2-center, triangle->normal(), triangle->color() );
				if (count == 3)
					model->model_vertex_countdetail += count;
			}
			delete triangle;
		}
		mapfile.class_tris.clear();
		
		// structural etriangles
		model->model_first_evertex = VertexArray::instance()->index()/3;
		for (std::list<Triangle *>::iterator it = mapfile.class_etris.begin(); it != mapfile.class_etris.end() && !VertexArray::instance()->overflow(); it++) {
			Triangle *triangle = (*it);
			if (!triangle->detail()) {
				size_t count = 0;
				count += VertexArray::instance()->add_vertex(triangle->triangle_v0-center, triangle->normal(), triangle->color() );
				count += VertexArray::instance()->add_vertex(triangle->triangle_v1-center, triangle->normal(), triangle->color() );
				count += VertexArray::instance()->add_vertex(triangle->triangle_v2-center, triangle->normal(), triangle->color() );
				if (count == 3)
					model->model_evertex_count += count;
			}
		}

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

		// structural ltriangles
		model->model_first_lvertex = VertexArray::instance()->index()/3;
		for (std::list<Triangle *>::iterator it = mapfile.class_ltris.begin(); it != mapfile.class_ltris.end() && !VertexArray::instance()->overflow(); it++) {
			Triangle *triangle = (*it);
			if (!triangle->detail()) {
				size_t count = 0;
				count += VertexArray::instance()->add_vertex(triangle->triangle_v0-center, triangle->normal(), triangle->color() );
				count += VertexArray::instance()->add_vertex(triangle->triangle_v1-center, triangle->normal(), triangle->color() );
				count += VertexArray::instance()->add_vertex(triangle->triangle_v2-center, triangle->normal(), triangle->color() );
				if (count == 3)
					model->model_lvertex_count += count;
			}
		}

		// detail ltriangles
		for (std::list<Triangle *>::iterator it = mapfile.class_ltris.begin(); it != mapfile.class_ltris.end() && !VertexArray::instance()->overflow(); it++) {
			Triangle *triangle = (*it);
			if (triangle->detail()) {
				size_t count = 0;
				count += VertexArray::instance()->add_vertex(triangle->triangle_v0-center, triangle->normal(), triangle->color() );
				count += VertexArray::instance()->add_vertex(triangle->triangle_v1-center, triangle->normal(), triangle->color() );
				count += VertexArray::instance()->add_vertex(triangle->triangle_v2-center, triangle->normal(), triangle->color() );
				if (count == 3)
					model->model_lvertex_countdetail += count;
			}
			delete triangle;
		}
		mapfile.class_ltris.clear();

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

		for (std::list<Flare *>::iterator flit = model->model_flare.begin(); flit != model->model_flare.end(); flit++) {
			(*flit)->light_location -= center;
		}

	}

	con_print  << "  maps/" << model->name() << ".map " << mapfile.brushes << " brushes " << model->tris() 
			<< " tris (" << model->details() << " detail)" << std::endl;

	return model;
}

}