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

#include <GL/gl.h>

#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>

#include "client/client.h"
#include "client/chat.h"
#include "client/console.h"
#include "client/input.h"
#include "client/targets.h"
#include "client/video.h"
#include "render/draw.h"
#include "render/render.h"
#include "render/textures.h"
#include "render/camera.h"
#include "core/core.h"
#include "core/stats.h"
#include "math/mathlib.h"
#include "sys/sys.h"

namespace client
{

core::Cvar *draw_ui = 0;
core::Cvar *draw_stats = 0;
core::Cvar *draw_location = 0;

core::Cvar *ui_pointercolor = 0;
core::Cvar *ui_pointerhovercolor =0;
namespace view
{

const size_t 			fps_counter_size = 32; // fps is the average of 32 frames
float 				fps_counter_time[fps_counter_size];
size_t 				fps_counter_index = 0;

const size_t			net_counter_size = 128;
float				net_counter_time[net_counter_size];
size_t				net_counter_traffic[net_counter_size];
size_t				net_counter_index;

void init()
{
	draw_stats = core::Cvar::get("draw_stats", "0", core::Cvar::Archive);
	draw_stats->set_info("[bool] draw network and render statistics");

	draw_location = core::Cvar::get("draw_location", "0", core::Cvar::Archive);
	draw_location->set_info("[bool] draw world location");

	draw_ui = core::Cvar::get("draw_ui", "1", core::Cvar::Archive);
	draw_ui->set_info("[bool] draw the user interface");

	ui_pointercolor = core::Cvar::get("ui_pointercolor", "0 .5 0", core::Cvar::Archive);
	ui_pointercolor->set_info("[r g b] mouse pointer color");

	ui_pointerhovercolor = core::Cvar::get("ui_pointerhovercolor", "0 1 0", core::Cvar::Archive);
	ui_pointerhovercolor->set_info("[r g b] mouse pointer hover color");

	targets::init();

	for (size_t i =0; i < fps_counter_size; i++)
		fps_counter_time[i] = 0.0f;

	for (size_t i = 0; i < net_counter_size; i++)
		net_counter_traffic[i] = 0;
}

void shutdown()
{
	targets::shutdown();
}

void draw_loader()
{
	using namespace render;

	render::Textures::bind("bitmaps/loader");
	
	gl::begin(gl::Quads);

	glTexCoord2f(0.0f, 0.0f);
	gl::vertex(0,0, 0);

	glTexCoord2f(1.0f, 0.0f);
	gl::vertex(video::width,0,0);

	glTexCoord2f(1.0f, 1.0f);
	gl::vertex(video::width,video::height,0);

	glTexCoord2f(0.0f, 1.0f);
	gl::vertex(0,video::height,0);

	gl::end();
}

void draw_status()
{
	using namespace render;

	std::stringstream status;

	if (core::game()) {
		int minutes = (int) floorf(core::game()->clientframetime() / 60.0f);
		int seconds = (int) floorf( core::game()->clientframetime() - (float) minutes* 60.0f);
	
		status << "^Ntime  ^B" << std::setfill(' ') << std::setw(3) << minutes << ":" << std::setfill('0') << std::setw(2) << seconds;
		Text::draw(video::width-Text::fontwidth()*12-4, Text::fontheight()+ 4, status);
	}

	// print stats if desired
	if (draw_stats && draw_stats->value()) {
		// average fps
		fps_counter_time[fps_counter_index] = core::application()->time();
		fps_counter_index = (fps_counter_index + 1 ) % fps_counter_size;
		float min_time = core::application()->time();
		for (size_t i=0; i < fps_counter_size; i++)
			if (fps_counter_time[i] < min_time)
				min_time = fps_counter_time[i];
		float fps = 0.0f;
		float t = (core::application()->time() - min_time);
		if (t > 0) {
			fps = roundf(((float) fps_counter_size - 1.0f) / t);
		}

		std::stringstream stats;
		stats << "^Nfps   ^B" << std::setw(6) << fps << "\n";

		if (core::application()->connected()) {
			stats << "^Ntris   ^B" << std::setw(5) << render::Stats::tris << "\n";
			stats << "^Nquads  ^B" << std::setw(5) << render::Stats::quads << "\n";

			if (core::Stats::network_bytes_sent + core::Stats::network_bytes_received) {
				net_counter_traffic[net_counter_index] = core::Stats::network_bytes_sent + core::Stats::network_bytes_received;
				net_counter_time[net_counter_index] = core::application()->time();
				size_t index_max = net_counter_index;

				net_counter_index = (net_counter_index + 1) % net_counter_size;
				size_t index_min = net_counter_index;

				float d = net_counter_time[index_max] - net_counter_time[index_min];
				if (d > 0) {
					float traffic = net_counter_traffic[index_max] - net_counter_traffic[index_min];

					stats << "^Nnet   ^B" << std::setw(6) << roundf( (float) traffic / d ) << "\n";
				}
			}
		}

		Text::draw(video::width-Text::fontwidth()*12-4, 4 + Text::fontheight()*2, stats);
	}

	// draw a basic HUD
	if (core::localcontrol()) {
		status.str("");
		status <<  "^Nthrust ^B" << std::setfill(' ') << std::setw(5) << std::fixed 
			<< std::setprecision(2) <<  core::localcontrol()->thrust() << " ";

		status 	<< "^Nspeed ^B" <<  std::setfill(' ') << std::setw(5) << std::fixed 
			<< std::setprecision(2) <<  core::localcontrol()->speed();

		Text::draw(4, video::height - Text::fontheight() -4, status);

		if (draw_location->value()) {
			std::stringstream location;
			location << "^Nloc  ^B" << std::fixed << std::setprecision(2) << std::setfill(' ') << 
			std::setw(7) << core::localcontrol()->location().x << " " <<
			std::setw(7) << core::localcontrol()->location().y << " " <<
			std::setw(7) << core::localcontrol()->location().z;
			Text::draw(4, video::height - Text::fontheight()*2 -4, location);
		}

		if (core::localplayer()->zone()) {
			Text::draw(video::width - 4-Text::fontwidth()*24, video::height - Text::fontheight()*3 -4, core::localplayer()->zone()->name());
		}

		core::Entity *entity = targets::current();
		if (entity) {
			std::stringstream target;
			target << "^B" << entity->name() << "\n";			
			target << "^Ndist ^B";

			math::Vector3f v = entity->state()->location() - core::localcontrol()->state()->location();
			float d = v.length() - entity->radius() - core::localcontrol()->radius();
			if (d > 0 )
				target << std::fixed << std::setprecision(2) << std::setfill(' ') << std::setw(8) << d;
			else
				target << "      --";

			Text::draw(video::width - 4-Text::fontwidth()*24, video::height - Text::fontheight()*2 -4, target);
		}
	}

}

void draw_cursor()
{
	if (!core::localcontrol() || console()->visible()) 
		return;

	float angle = 0;
	float pointer_size = 48.0f;
	float x = (float) input::mouse_position_x() - (pointer_size / 2.0f);
	float y = (float) input::mouse_position_y() - (pointer_size / 2.0f);
	bool cursor_animated = false;
	math::Color color(1.0, 0.5);
	
	if (render::Camera::mode() == render::Camera::Overview) {
		render::Textures::bind("bitmaps/pointers/aim");
	} else {
		// draw center cursor in Cockpit and Track mode
		if (input::mouse_control && 
			(render::Camera::mode() == render::Camera::Cockpit || render::Camera::mode() == render::Camera::Track)) {
			
			if (ui_pointercolor) {
				std::stringstream colorstr(ui_pointercolor->str());
				colorstr >> color;
			}

			render::Textures::bind("bitmaps/pointers/center");
			float cx = (video::width - pointer_size) /2;
			float cy = (video::height - pointer_size) /2;

			render::gl::color(color);
			render::gl::begin(render::gl::Quads);

			glTexCoord2f(0,0 );
			render::gl::vertex(cx,cy,0.0f);
		
			glTexCoord2f(1, 0);
			render::gl::vertex(cx+pointer_size, cy, 0.0f);
		
			glTexCoord2f(1, 1);
			render::gl::vertex(cx+pointer_size, cy+pointer_size, 0.0f);
		
			glTexCoord2f(0, 1);
			render::gl::vertex(cx, cy+pointer_size, 0.0f);
		
			render::gl::end();
		}

		if (targets::hover()) {

			if (ui_pointerhovercolor) {
				std::stringstream colorstr(ui_pointerhovercolor->str());
				colorstr >> color;
			}
			render::Textures::bind("bitmaps/pointers/target");

			cursor_animated = true;

		} else if (input::mouse_control) {

			if (ui_pointercolor) {
				std::stringstream colorstr(ui_pointercolor->str());
				colorstr >> color;
			}

			render::Textures::bind("bitmaps/pointers/control");

			if (!input::mouse_deadzone) {
				x = input::mouse_position_x() - (pointer_size /2);
				y = input::mouse_position_y() - (pointer_size /2);

 			} else {
 				x = (video::width - pointer_size) /2;
 				y = (video::height - pointer_size) /2;
 			}

		} else {

			render::Textures::bind("bitmaps/pointers/aim");
		}

	}

	if (cursor_animated) {
		render::gl::push();
		render::gl::translate(x+pointer_size/2, y+pointer_size/2, 0.0f);

		angle = core::application()->time()* 0.75f - floorf(core::application()->time() * 0.75f);
		angle *= 360.0f;
		render::gl::rotate(angle, math::Vector3f(0, 0, 1.0f));
		render::gl::translate(-x-pointer_size/2, -y-pointer_size/2, 0.0f);
	}

	render::gl::color(color);
	render::gl::begin(render::gl::Quads);

	glTexCoord2f(0,0 );
	render::gl::vertex(x,y,0.0f);

	glTexCoord2f(1, 0);
	render::gl::vertex(x+pointer_size, y, 0.0f);

	glTexCoord2f(1, 1);
	render::gl::vertex(x+pointer_size, y+pointer_size, 0.0f);

	glTexCoord2f(0, 1);
	render::gl::vertex(x, y+pointer_size, 0.0f);

	render::gl::end();

	if (cursor_animated) {
		render::gl::pop();
	}
}

void reset()
{
	using namespace render;

	// set clear color
	gl::clearcolor(0.0f, 0.0f, 0.0f, 1.0f);

	// load identity matrices
	gl::matrixmode(GL_MODELVIEW);
	gl::loadidentity();

	gl::matrixmode(GL_MODELVIEW);
	gl::loadidentity();

	// shading model: Gouraud (smooth, the default)
	gl::shademodel(GL_SMOOTH);
	//gl::shademodel(GL_FLAT);

	// lighting settings for the default light GL_LIGHT0
 	GLfloat light_position[] = { 0.0, 0.0, 0.0, 1.0 };
	GLfloat ambient_light[] = { 0.01f, 0.01f, 0.01f, 1.0f };
	GLfloat diffuse_light[] = { 0.2f, 0.2f, 0.2f, 1.0f };
	GLfloat specular_light[] = { 0.2f, 0.2f, 0.2f, 1.0f };
	
	glLightfv(GL_LIGHT0, GL_POSITION, light_position);
	glLightfv(GL_LIGHT0, GL_AMBIENT, ambient_light);
	glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse_light);
	glLightfv(GL_LIGHT0, GL_SPECULAR, specular_light);

	// GL_LIGHT0 is always enabled
	gl::enable(GL_LIGHT0);

	// color tracking
	glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);	

	// material settings	
	GLfloat specular_reflectance[] = { 0.2f, 0.2f, 0.2f, 1.0f };
	glMaterialfv(GL_FRONT, GL_SPECULAR, specular_reflectance);
	glMateriali(GL_FRONT, GL_SHININESS, 128); // shininess 1-128

	// alpha blending function
	gl::blendfunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

	gl::disable(GL_LIGHTING);
	gl::disable(GL_COLOR_MATERIAL);

	gl::cullface(GL_BACK);
	gl::frontface(GL_CCW);
	gl::disable(GL_CULL_FACE);
	gl::disable(GL_DEPTH_TEST);	
	gl::disable(GL_BLEND);	

	gl::disable(GL_TEXTURE_2D);

}

void frame(float seconds)
{
	using namespace render;
	
	// flush console messages
	console()->flush();

	// Clear the color and depth buffers.
	gl::clear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	render::Stats::clear();

	if (core::application()->connected() && core::game()->serverframetime()) {
		render::draw(seconds);		// draw the world
		targets::frame();
		if (draw_ui->value()) {
			targets::draw_target();
		}
	}

	// switch to ortographic projection to draw the GUI
	gl::matrixmode(GL_PROJECTION);
	gl::loadidentity();
	glOrtho(0, video::width, video::height, 0, -1024.0f, 1024.0f);

	gl::matrixmode(GL_MODELVIEW);
	gl::loadidentity();

	gl::enable(GL_TEXTURE_2D);

	gl::color(1.0f, 1.0f, 1.0f, 1.0f);

	if (!core::application()->connected()) {
		// draw the loader bitmap
		draw_loader();

		// force console on if not connected
		if (!console()->visible())
			console()->toggle();
	}

	gl::enable(GL_BLEND);

	// draw text elements
	if (draw_ui->value()) {

		Text::setfont("bitmaps/fonts/gui", 16, 24);

		// draw the player status
		draw_status();		

		// draw the mouse cursor
		draw_cursor();
	}

	Text::setfont("bitmaps/fonts/console", 12, 18);
	chat::draw();
	console()->draw();

	gl::disable(GL_TEXTURE_2D);
	
	gl::disable(GL_BLEND);
}

} //namespace view

} // namespace client