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

#include <sstream>
#include <iomanip>

#include "render/particlesystemscript.h"
#include "math/functions.h"
#include "core/application.h"
#include "core/entity.h"
#include "core/entityglobe.h"
#include "core/range.h"
#include "model/model.h"
#include "render/camera.h"
#include "render/renderext.h"
#include "render/render.h"
#include "render/textures.h"
#include "sys/sys.h"

namespace render
{

RenderExt::RenderExt(core::Entity *entity) : core::Extension(core::Extension::Render, entity)
{
	state_visible = false;
	state_detailvisible = false;
	state_fuzz = math::randomf();
	state_enginetime = state_fuzz * core::application()->time();
	state_explosion = 0;

	if (!entity->model() && entity->modelname().size()) {
		entity->set_modelname(entity->modelname());
		if (!entity->model())
			entity->set_modelname("");
	}

	if (entity->model()) {
		model::Model *model = entity->model();

		for (model::Model::Lights::iterator lit = model->lights().begin(); lit != model->lights().end(); ++lit) {
			model::Light *light = (*lit);

			// load light texture
			std::stringstream flarename;
			flarename << "textures/fx/flare" << std::setfill('0') << std::setw(2) << light->flare();
			light->set_texture(Textures::load(flarename.str()));
		}

		for (model::Model::Flares::iterator flit = model->flares().begin(); flit != model->flares().end(); ++flit) {
			model::Flare *flare = (*flit);

			// load flare texture
			std::stringstream flarename;
			flarename << "textures/fx/flare" << std::setfill('0') << std::setw(2) << flare->flare();
			flare->set_texture(Textures::load(flarename.str()));
		}

		for (model::Model::ParticleSystems::const_iterator pit = model->particles().begin(); pit != model->particles().end(); ++pit) {
			// load particle systems
			const ParticleSystemScript *script = ParticleSystemScript::load((*pit)->script());
			if (script) {
				ParticleSystem *ps = new ParticleSystem(script, entity, (*pit));
				state_particles.push_back(ps);
			}
		}

	} else if (entity->type() == core::Entity::Globe) {
		core::EntityGlobe *globe = static_cast<core::EntityGlobe *>(entity);

		// load globe textures
		if (!globe->texture_id() && globe->texturename().size()) {
			globe->set_texture_id(Textures::load("textures/" + globe->texturename()));
			if (!globe->texture_id())
				globe->set_texturename("");
		}
		
		if (!globe->corona_id() && globe->coronaname().size()) {
			globe->set_corona_id(Textures::load("textures/" + globe->coronaname()));
			if (!globe->corona_id()) {
				globe->set_coronaname("");
			}
		}
		
		if (!globe->rings_id() && globe->ringsname().size()) {
			globe->set_rings_id(Textures::load("textures/" + globe->ringsname()));
			if (!globe->rings_id()) {
				globe->set_ringsname("");
			}
		}
	}
}

RenderExt::~RenderExt()
{
	for (ParticleSystems::iterator it = state_particles.begin(); it != state_particles.end(); it++) {
		delete(*it);
	}
	state_particles.clear();
}

void RenderExt::frame(float elapsed)
{
	state_distance = math::distance(Camera::eye(), entity()->location());

	state_visible = entity()->visible();
	state_detailvisible = false;
	state_behind = false;
	state_power = true;
	state_thrust = 0.0f;

	if (!state_visible) {
		return;
	}

	int state = core::Entity::Normal;
	
	if ((entity()->type() == core::Entity::Dynamic) || (entity()->type() == core::Entity::Projectile)) {

		state = static_cast<core::EntityDynamic *>(entity())->state();
		
		if ((state == core::Entity::NoPower) || (state == core::Entity::Destroyed)) {
			state_power = false;
		}

		state_enginetime += elapsed;
		
	} else if ((entity()->type() == core::Entity::Controlable)) {
		
		core::EntityControlable *controlable = static_cast<core::EntityControlable *>(entity());
		
		state = controlable->state();

		if (controlable->state() == core::Entity::Docked) {
			state_visible = false;
			state_power = false;
			return;
		}
		
		if ((controlable == core::localcontrol()) && (Camera::mode() == Camera::Cockpit)) {
			state_visible = false;
			return;
		}

		if ((controlable->state() == core::Entity::NoPower) || (controlable->state() == core::Entity::Destroyed)) {
			state_power = false;
		}
		
		if ((controlable->state() == core::Entity::Impulse) || (controlable->state() == core::Entity::ImpulseInitiate)) {
			state_thrust = 1.0f;
		} else {
			state_thrust = controlable->thrust();
		}
			
		state_enginetime += elapsed * state_thrust;
		
	} else {
		
		state_enginetime += elapsed;
	}

	if (state == core::Entity::Destroyed) {
		
		if (!state_explosion) {
			// add explosion
			const ParticleSystemScript *script = ParticleSystemScript::load("explosion");
			if (script) {
				state_explosion = new ParticleSystem(script, entity(), 0);
	
				// forece all ejectors te explosion state
				for (ParticleSystem::Ejectors::iterator it = state_explosion->ejectors().begin(); it != state_explosion->ejectors().end(); ++it) {		
					ParticleEjector *ejector = (*it);
					ejector->set_explosion(true);
				}

				state_particles.push_back(state_explosion);
			}
		}
		
	} else {

		if (state_explosion) {
			// remove explosion
			for (ParticleSystems::iterator it = particles().begin(); state_explosion && (it != particles().end());) {
				if ((*it) == state_explosion) {
					particles().erase(it++);
					state_explosion = 0;
				} else {
					++it;
				}
			}
		}
	}

	if (entity()->model()) { 
		
		if (distance() < core::range::fxdistance) {
			// entity within detail range
			state_visible = true;
			state_detailvisible = true;
							
			if ((entity()->type() == core::Entity::Controlable) || (entity()->type() == core::Entity::Dynamic)) {
				
				if (static_cast<core::EntityDynamic *>(entity())->state() == core::Entity::Destroyed) {
					state_visible = false;
				}
			}

		} else if (distance() < core::range::maxdistance) {

			// funky radius factor
			float r = entity()->model()->radius();
			math::clamp(r, entity()->model()->radius(), core::range::maxdistance / core::range::fxdistance);

			if (distance() < core::range::fxdistance * r) {
				// entity within drawing distance, outside detail range
				state_visible = true;
				state_detailvisible = false;

			} else {
				// entity within drawing distance, outside detail range
				state_visible = true;
				state_detailvisible = false;
			}

		} else {
			// entity out of range
			state_visible = false;
			state_detailvisible = false;
			return;
		}
	}
	
	if (math::dotproduct(Camera::axis().forward(), entity()->location() + Camera::axis().forward() * entity()->radius() - Camera::eye()) < 0.0f) {
		state_behind = true;
	}
}

} // namespace render