Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
blob: 618db79705bb8ed69ce8f285d78b73e331aafaa7 (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
/*
   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;
	Engine *engine = 0;

	unsigned int u;

	while (mapfile.getline()) {

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

		} 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_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;

			}
		}
	}

	mapfile.close();

	if (VertexArray::instance() && ((mapfile.class_tris.size() + mapfile.class_etris.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(); 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->model_vertex_count += 3;
			}
		}

		// detail triangles
		for (std::list<Triangle *>::iterator it = mapfile.class_tris.begin(); it != mapfile.class_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->model_vertex_countdetail += 3;
			}
			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(); 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->model_evertex_count += 3;
			}
		}

		// detail etriangles
		for (std::list<Triangle *>::iterator it = mapfile.class_etris.begin(); it != mapfile.class_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->model_evertex_countdetail += 3;
			}
			delete triangle;
		}
		mapfile.class_etris.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;
		}

	}

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

	return model;
}

}