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

#include "auxiliary/functions.h"
#include "core/application.h"
#include "ui/modelview.h"
#include "ui/paint.h"
#include "ui/ui.h"
#include "sys/sys.h"
#include "render/camera.h"
#include "render/draw.h"
#include "render/render.h"
#include <sstream>
#include <iomanip>

namespace ui
{

float	modelview_light[] = { 1, 0, 0, 1 };

ModelView::ModelView(Widget *parent, const char *modelname) : Widget(parent)
{
	set_border(false);
	set_background(false);
	set_label("modelview");

	set_modelname(modelname);

	modelview_radius = 0.75f;
	modelview_zoom = 1.0f;
	modelview_dragging = false;
	
	modelview_axis.clear();
	modelview_axis.change_direction(180);
	modelview_axis.change_pitch(-15);
}

ModelView::~ModelView()
{}

void ModelView::print(const size_t indent) const
{
	std::string marker("");
	con_print << aux::pad_left(marker, indent*2) << label() << " \"" << modelname() << "\"" << std::endl;
}

void ModelView::set_modelname(const char *modelname)
{
	if (modelname)
		modelview_modelname.assign(modelname);
	else
		modelview_modelname.clear();
	
	set_background(false);
	
	modelview_axis.clear();
	modelview_axis.change_direction(180);
	modelview_axis.change_pitch(-15);
	modelview_zoom = 1.0f;
}

void ModelView::set_colors(const math::Color & color_primary, const math::Color & color_secondary)
{
	modelview_color_primary.assign(color_primary);
	modelview_color_secondary.assign(color_secondary);
}

void ModelView::set_zoom(const float zoom)
{
	modelview_zoom = zoom;
	math::clamp(modelview_zoom, 1.0f, 5.0f);
}

bool ModelView::on_keypress(const int key, const unsigned int modifier)
{
	if (key == 512 + SDL_BUTTON_WHEELUP) {
		modelview_zoom -= 0.25f;
		if (modelview_zoom < 1.0f)
			modelview_zoom = 1.0f;
		return true;
	} else 	if (key == 512 + SDL_BUTTON_WHEELDOWN) {
		modelview_zoom += 0.25f;
		if (modelview_zoom > 5.0f)
			modelview_zoom = 5.0f;
		return true;
	} else if (key == 512 + SDL_BUTTON_LEFT) {
		modelview_dragging = true;
		return true;
	}

	return false;
}

bool ModelView::on_keyrelease(const int key, const unsigned int modifier)
{
	if (key == 512 + SDL_BUTTON_LEFT) {
		modelview_dragging = false;
		return true;
	}

	return false;
}

void ModelView::on_mousemove(const math::Vector2f &cursor) 
{
	if ((width() <= 0) || (height() <= 0)) {
		return;
	}
	
	if (modelview_dragging) {	
		const math::Vector2f pos(cursor - modelview_cursor);
		
		const math::Vector3f up(0.0f, 0.0f, 1.0f);
		const float zrot =  2.0f * M_PI * pos.x() / width();
		modelview_axis.rotate(up, -zrot);

		const math::Vector3f left(0.0f, 1.0f, 0.0f);
		const float yrot =  2.0f * M_PI * pos.y() / height();
		modelview_axis.rotate(left, yrot);
	
		modelview_cursor.assign(cursor);

	}
	modelview_cursor.assign(cursor);
	
	//FIXME this is perhaps not the best way to do this
	for(size_t i = 0; i<3; i++) {
		modelview_light[i] = (-modelview_axis[i][0] / modelscale * 2);
	}
}

void ModelView::on_mouseover(const math::Vector2f &cursor)
{
	modelview_cursor.assign(cursor);
	modelview_dragging = false;
}

void ModelView::draw_background()
{
	Paint::set_color(1.0f, 1.0f, 1.0f);
	Paint::draw_bitmap(global_location(), size(), "textures/common/notex");	
}

void ModelView::draw()
{
	if (!modelview_modelname.size()) {
		return;
	}

	if (!core::application()->connected() || !core::game()->time()) {
		return;
	}
	
	Paint::set_color(1.0f, 1.0f, 1.0f);
	model::Model *model = model::Model::load(modelview_modelname);
	if (!model) {
		modelview_modelname.clear();
		set_background(true);
		return;
	}

	math ::Vector2f center(global_location() + size() * 0.5f);

	const float minscreen(math::min(root()->width(),root()->height()));
	const float minwidget(math::min(width(), height()));
	const float reference_radius = radius() * minwidget / minscreen;
	modelscale = reference_radius / model->radius();

	// gl 3d mode
	render::Camera::frustum_default(modelview_zoom, center.x(), center.y());

	// reset lighting
	render::pass_reset_lights();

	// enable lighting
	gl::enable(GL_LIGHTING);
	
	gl::disable(GL_BLEND);
	gl::depthmask(GL_TRUE);		// enable writing to the depth buffer
	gl::enable(GL_DEPTH_TEST);	// enable depth test

	gl::enable(GL_CULL_FACE);	// enable culling
	gl::enable(GL_COLOR_MATERIAL);	// enable color tracking
	
	// clear depth buffer
	gl::clear(GL_DEPTH_BUFFER_BIT);
	
	// enable vertex arrays
	glEnableClientState(GL_VERTEX_ARRAY);
	glEnableClientState(GL_TEXTURE_COORD_ARRAY);
	glEnableClientState(GL_NORMAL_ARRAY);
	
	// apply manipulation
	gl::push();
	
	
	
	gl::multmatrix(modelview_axis);
	gl::scale(modelscale, modelscale, modelscale);
	
	render::State::set_normalize(true);
	
	// FIXME the inventory window needs to be able to use actual thrust values
	float thrust = ::sinf(core::application()->time() * 0.5f);
	if (thrust < 0.0f ) {
		thrust = 1.0f + thrust;
	} else {
		thrust = 1.0f;
	}

	// let there be ModelView:: light!
	GLfloat ambient_light[] = { 0.0f, 0.0f, 0.0f, 1.0f };
	GLfloat diffuse_light[4];
	GLfloat specular_light[4];
	for (size_t i = 0; i < 3; i++) {
		diffuse_light[i] = render::r_diffuse->value() * 0.6f;
		specular_light[i] = render::r_specular->value() * 0.2f;
	}
	diffuse_light[3] = 1.0f;
	specular_light[3] = 1.0f;
	
	glLightfv(GL_LIGHT1, GL_POSITION, modelview_light);
	glLightfv(GL_LIGHT1, GL_AMBIENT, ambient_light);
	glLightfv(GL_LIGHT1, GL_DIFFUSE, diffuse_light);
	glLightfv(GL_LIGHT1, GL_SPECULAR, specular_light);

	gl::enable(GL_LIGHT1);

	render::draw_model_fragments(model, modelview_color_primary, modelview_color_secondary, core::application()->time(), true, true, thrust);
	
	render::State::set_normalize(false);
	
	gl::pop();

	gl::disable(GL_LIGHT1);

	glDisableClientState(GL_VERTEX_ARRAY);
	glDisableClientState(GL_NORMAL_ARRAY);
	glDisableClientState(GL_TEXTURE_COORD_ARRAY);

	gl::disable(GL_LIGHTING);
	gl::enable(GL_TEXTURE_2D);
	gl::enable(GL_BLEND);
	gl::depthmask(GL_FALSE);		// disable depth buffer writing

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

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

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

		// load flare texture
		std::stringstream texturename;
		texturename << "textures/fx/flare" << std::setfill('0') << std::setw(2) << flare->flare();
		flare->set_texture(render::Textures::load(texturename.str()));
	}
	
	render::draw_model_lights(model, modelscale, math::Vector3f(), modelview_axis, core::localplayer()->color(), thrust, 0.0f);
	
	gl::disable(GL_TEXTURE_2D);
	gl::disable(GL_COLOR_MATERIAL);	// disable color tracking
	gl::disable(GL_CULL_FACE);	// disable culling
	gl::depthmask(GL_FALSE);	// enable depth buffer writing
	gl::disable(GL_DEPTH_TEST);	// disable depth buffer testing

	// gl 2d mode
	render::Camera::ortho();
}

void ModelView::draw_border()
{
	Paint::set_color(palette()->foreground());
	Paint::draw_border(global_location(), size());
}

}