Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
blob: 8d8ea1f158d6594cb2c81242735f82bb0739e84e (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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
/*
   render/draw.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 <sstream>
#include <iomanip>

#include "core/core.h"
#include "model/fragment.h"
#include "model/material.h"
#include "model/model.h"
#include "render/render.h"
#include "render/textures.h"
#include "render/draw.h"
#include "render/dust.h"
#include "render/gl.h"

namespace render
{

size_t Stats::tris = 0;
size_t Stats::quads = 0;

void Stats::clear()
{
	tris = 0;
	quads = 0;
}

math::Vector3f v0(1, -1, 1);
math::Vector3f v1(1,  1, 1);
math::Vector3f v2(-1,  1,  1);
math::Vector3f v3(-1, -1,  1);

math::Vector3f v4(1, -1, -1);
math::Vector3f v5(1,  1, -1);
math::Vector3f v6(-1,  1,  -1);
math::Vector3f v7(-1, -1,  -1);

const float drawdistance = 128.0f;
const float drawfxdistance = 64.0f;
core::Zone *zone = 0;

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

/* ----- Default Entity shapes ------------------------------------- */

void draw_sphere(math::Color const & color, float radius)
{	
	//gl::push();
	gl::scale(radius, radius, radius);
	gl::color(color);

	size_t index = 0;
	size_t count = (model::SPHERESEGMENTS)*2;

	// draw body
	for (int j=0; j < (model::SPHERESEGMENTS-1)/2; j++) {
		glDrawArrays(gl::QuadStrip, index, count);
		index += count;
		Stats::quads += count/2-1;
	}

	//gl::pop();
}

void draw_sphere_inside(math::Color const & color, float radius)
{	
	//gl::push();
	gl::scale(radius, radius, radius);
	gl::color(color);

	size_t index = (model::SPHERESEGMENTS) * (model::SPHERESEGMENTS-1);
	size_t count = (model::SPHERESEGMENTS)*2;

	// draw body
	for (int j=0; j < (model::SPHERESEGMENTS-1)/2; j++) {
		glDrawArrays(gl::QuadStrip, index, count);
		index += count;
		Stats::quads += count/2-1;
	}

	//gl::pop();
}

void draw_entity_sphere(core::Entity *entity)
{
	if ((entity->type() == core::Entity::Globe) && !flag_is_set(entity->flags(), core::Entity::Bright)) {
		gl::disable(GL_LIGHT0);	// disable camera light, level light only
		
		core::EntityGlobe *globe = (core::EntityGlobe *)entity;
		if (globe->render_texture) {
			Textures::bind(globe->render_texture);
			gl::enable(GL_TEXTURE_2D);
		}
	}

	draw_sphere(entity->color(), entity->radius());

	if ((entity->type() == core::Entity::Globe) && !flag_is_set(entity->flags(), core::Entity::Bright)) {
		core::EntityGlobe *globe = (core::EntityGlobe *)entity;
		if (globe->render_texture) {
			gl::disable(GL_TEXTURE_2D);
		}
		gl::enable(GL_LIGHT0);
	}
}

void draw_entity_cube(core::Entity *entity)
{
	float radius = entity->radius();
	gl::scale(radius, radius, radius);

	gl::color(entity->color());
	gl::begin(gl::Quads);
	
	// top
	gl::normal(0,0,1);
	gl::vertex(v0);
	gl::vertex(v1);
        gl::vertex(v2);
	gl::vertex(v3);

	// bottom
	gl::normal(0,0, -1);
	gl::vertex(v7);
	gl::vertex(v6);
        gl::vertex(v5);
	gl::vertex(v4);
	
	// sides
	gl::normal(1,0,0);
	gl::vertex(v1);
    	gl::vertex(v0);
        gl::vertex(v4);
        gl::vertex(v5);

	gl::normal(-1,0,0);
        gl::vertex(v3);
    	gl::vertex(v2);
        gl::vertex(v6);
        gl::vertex(v7);

	gl::normal(0,1,0);
	gl::vertex(v2);
    	gl::vertex(v1);
        gl::vertex(v5);
        gl::vertex(v6);

	gl::normal(0,-1,0);
	gl::vertex(v0);
    	gl::vertex(v3);
        gl::vertex(v7);
        gl::vertex(v4);

	gl::end();
}

void draw_entity_diamond(core::Entity *entity)
{
	float radius = entity->radius()/2;

	gl::color(entity->color());

	gl::begin(gl::TriangleFan );
	gl::normal(0, 0 , 1);
	gl::vertex(0, 0, radius);

	gl::normal(1, 0 ,0);
	gl::vertex(radius, 0.0f, 0.0f);
	gl::normal(0, 1, 0);
	gl::vertex(0.0f, radius, 0.0f);
	gl::normal(-1, 0 ,0);
	gl::vertex(-radius, 0.0f, 0.0f);
	gl::normal(0, -1, 0);
	gl::vertex(0.0f, -radius, 0.0f);
	gl::normal(1, 0 ,0);
	gl::vertex(radius, 0.0f, 0.0f);

	gl::end();

	gl::begin(gl::TriangleFan);
	gl::normal(0, 0 , -1);
	gl::vertex(0, 0, -radius);
	
	gl::normal(1, 0,  0);
	gl::vertex(radius, 0.0f, 0.0f);

	gl::normal(0, -1, 0);
	gl::vertex(0.0f, -radius, 0.0f);

	gl::normal(-1, 0 ,0);
	gl::vertex(-radius, 0.0f, 0.0f);

	gl::normal(0, 1, 0);
	gl::vertex(0.0f, radius, 0.0f);

	gl::normal(1, 0 ,0);
	gl::vertex(radius, 0.0f, 0.0f);

	gl::end();

	gl::begin(gl::Lines);

	gl::vertex(radius, 0, 0);
	gl::vertex(2* radius, 0, 0);

	gl::vertex(0, radius, 0);
	gl::vertex(0, 2* radius, 0);

	gl::vertex(0, 0, radius);
	gl::vertex(0, 0, 2 * radius);

	gl::vertex(-radius, 0, 0);
	gl::vertex(-2 * radius, 0, 0);
	
	gl::vertex(0, -radius, 0);
	gl::vertex(0, -2 * radius, 0);

	gl::vertex(0, 0, -radius);
	gl::vertex(0, 0, -2 * radius);

	gl::end();
}

void draw_entity_axis(core::Entity *entity)
{
	using namespace render;
	float r = entity->radius();
	gl::begin(gl::Lines);
	gl::color(1.0f, 0.0f, 0.0f);
	gl::vertex(r,0.0f,0.0f);
	gl::color(entity->color());
	gl::vertex(-r,0.0f,0.0f);

	gl::vertex(0.0f,r/2,0.0f);
	gl::vertex(0.0f,-r/2,0.0f);

	gl::vertex(0.0f,0.0f,r);
	gl::vertex(0.0f,0.0f,-r);
	gl::end();
}

/* ----- model fragments ------------------------------------------- */

void draw_fragment(model::Fragment *fragment, bool draw_details)
{
	size_t index = fragment->index();

	size_t vertex_count = fragment->structural_size();
	if (draw_details)
		vertex_count += fragment->detail_size();

	switch (fragment->type()) {
		case model::Fragment::Triangles:
			glDrawArrays(gl::Triangles, index, vertex_count);
			Stats::tris += vertex_count / 3;
			break;

		case model::Fragment::Quads:
			glDrawArrays(gl::Quads, index, vertex_count);
			Stats::quads += vertex_count / 4;
			break;
	}
}

void draw_model_fragments(core::Entity *entity)
{
	using namespace model;

	Model *model = entity->model();
	if (!model)
		return;

	// default material, lighted and geometry color
	unsigned int material = Material::None;

	bool use_color_array = true;	// glEnableClientState(GL_COLOR_ARRAY) is set
	bool use_light = true;		// gl::disable(GL_LIGHTING) is set

	float thrust = 0;
	if (entity->type() == core::Entity::Controlable) {
		thrust = static_cast<core::EntityControlable *>(entity)->thrust();
	}

	for (Model::Fragments::iterator fit = model->fragments().begin(); fit != model->fragments().end(); fit++) {

		Fragment *fragment = (*fit);

		if (fragment->material() != material) {
			material = fragment->material();

			if (material & Material::Engine) {

				if (use_color_array) {
					glDisableClientState(GL_COLOR_ARRAY);
					use_color_array = false;
				}

				gl::color(thrust, 0.0f, 0.0f);

			} else if (material & Material::Tertiary) {
				if (use_color_array) {
					use_color_array = false;
					glDisableClientState(GL_COLOR_ARRAY);
				}

				math::Color color;

				if ((material & Material::Tertiary) == Material::Tertiary) {
					for (size_t i = 0; i < 3; i++)
						color[i] = (entity->color()[i] + entity->color_second()[i]) / 2;

				} else if ((material & Material::Secondary) == Material::Secondary) {
					color.assign(entity->color_second());

				} if ((material & Material::Primary) == Material::Primary) {
					color.assign(entity->color());
				}

				if (material & Material::Dark)
					color *= 0.5f;

				gl::color(color);
		
			} else {
				if (!use_color_array) {
					glEnableClientState(GL_COLOR_ARRAY);
					use_color_array = true;
				}
			}

			if (material & Material::Light) {
				if (use_light) {
					gl::disable(GL_LIGHTING);
					use_light = false;
				}
			} else if (material & Material::Engine) {
				if (use_light) {
					gl::disable(GL_LIGHTING);
					use_light = false;
				}
			} else {
				if (!use_light) {
					gl::enable(GL_LIGHTING);
					use_light = true;
				}
			}
		}

		draw_fragment(fragment, entity->state()->detailvisible());
	}

	if (!use_light) {
		gl::enable(GL_LIGHTING);
	}

	if (!use_color_array) {
		glEnableClientState(GL_COLOR_ARRAY);
	}
}


/* ----- Render passes --------------------------------------------- */

/* calculate entity visibility */
void pass_prepare(float seconds)
{
	using namespace model;

	// reset light state
	gl::disable(GL_LIGHT1);

	// get the current OpenGL transformation matrices
	GLdouble gl_projection_matrix[16];
	GLdouble gl_model_matrix[16];
	GLint gl_viewport[4];

	glGetDoublev(GL_PROJECTION, gl_projection_matrix);
	glGetDoublev(GL_MODELVIEW, gl_model_matrix);
	glGetIntegerv(GL_VIEWPORT, gl_viewport);

	for (core::Zone::Content::iterator it = zone->content().begin(); it != zone->content().end(); it++) {
		core::Entity *entity = (*it);
		
		// load entity models and light flare textures
		if (!entity->model() && entity->modelname().size()) {
			entity->entity_model = Model::load(entity->modelname());
						
			if (!entity->model()) {
				entity->entity_modelname.clear();
				entity->entity_radius = 0.25;
			} else {
				// set entity radius to model radius
				entity->entity_radius = entity->entity_model->radius();

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

					// load light texture
					// FIXME optimize
					std::stringstream flarename;
					flarename << "bitmaps/fx/flare" << std::setfill('0') << std::setw(2) << light->flare();
					light->render_texture = Textures::load(flarename.str());
				}

				for(Model::Engines::iterator eit = entity->model()->engines().begin(); eit != entity->model()->engines().end(); eit++) {
					Engine *engine = (*eit);

					if (!engine->flare()) engine->engine_flare = 1;

					// load engine texture
					// FIXME optimize
					std::stringstream flarename;
					flarename << "bitmaps/fx/flare" << std::setfill('0') << std::setw(2) << engine->flare();
					engine->render_texture = Textures::load(flarename.str());
				}

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

					// load flare texture
					// FIXME optimize
					std::stringstream flarename;
					flarename << "bitmaps/fx/flare" << std::setfill('0') << std::setw(2) << flare->flare();
					flare->render_texture = Textures::load(flarename.str());
				}
			}
		}

		if (!entity->state()) {
			entity->entity_clientstate = new core::ClientState(entity);
		}

		entity->state()->state_visible = false;	
		entity->state()->state_detailvisible = false;
		entity->state()->state_targetable = false;

		// calculate visibility for entities with models
		if (entity->model()) {

			entity->state()->state_visible = false;
			float dq = math::distancesquared(Camera::eye(), entity->location());
	
			if (dq <= drawfxdistance*drawfxdistance*entity->model()->radius())  {
				// entites within drawing distance
				entity->state()->state_visible = true;
				entity->state()->state_detailvisible = true;
			} else if (dq <= drawdistance*drawdistance*entity->model()->radius())  {
				// entities within drawdistance
				entity->state()->state_visible = true;
				entity->state()->state_detailvisible = false;
			}

		} else 	{
			entity->state()->state_visible = true;	

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

				if (flag_is_set(globe->flags(), core::Entity::Bright)) {

					// bright globes set level light
					GLfloat light_position[4];
					GLfloat diffuse_light[4];
					GLfloat ambient_light[] = { 0.0f, 0.0f, 0.0f, 1.0f };
					GLfloat specular_light[] = { 0.2f, 0.2f, 0.2f, 1.0f };
	
					for (size_t i=0; i <3; i++) {
						light_position[i] = globe->location()[i] - render::Camera::eye()[i];
						diffuse_light[i] = globe->color()[i] * 0.4;
					}
					light_position[3] = 1.0f;
					diffuse_light[3] = 1.0f;
					
					glLightfv(GL_LIGHT1, GL_POSITION, light_position);
					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);
				} else {
		
					// load globe textures
					// FIXME optimize
					if (globe->texture().size()) {
						std::stringstream texname;
						texname << "textures/" << globe->texture();
						globe->render_texture = Textures::load(texname.str());
					}
				}
			}		
		}
	}
}

/* Draw entities without model */
void draw_pass_default()
{
	for (core::Zone::Content::iterator it = zone->content().begin(); it != zone->content().end(); it++) {
		core::Entity *entity = (*it);

		// draw entities without model
		if (!entity->model()) {
			gl::push();
			gl::translate(entity->state()->location() - Camera::eye());
			gl::multmatrix(entity->state()->axis());

			if (flag_is_set(entity->flags(), core::Entity::Bright)) {
				gl::disable(GL_LIGHTING);
			}
		
			switch(entity->shape()) {
	
				case core::Entity::Sphere:
					draw_entity_sphere(entity);
					break;
					
				case core::Entity::Diamond:
					draw_entity_diamond(entity);
					break;

				case core::Entity::Axis:
					draw_entity_axis(entity);
					break;

				case core::Entity::Cube:
				
			default:
				draw_entity_cube(entity);
				break;
			}

			if (flag_is_set(entity->flags(), core::Entity::Bright)) {
				gl::enable(GL_LIGHTING);	
			}
				
			gl::pop();

		} else if (r_bbox->value()) {

			// draw bounding box if requested
			model::Model *model = entity->model();
			gl::color(entity->color());

			gl::push();
			gl::translate(entity->state()->location() - Camera::eye());
			gl::multmatrix(entity->state()->axis());
			
			// top
			gl::begin(gl::LineLoop);
			gl::vertex(model->model_maxbbox.x, model->model_maxbbox.y, model->model_maxbbox.z);
			gl::vertex(model->model_minbbox.x, model->model_maxbbox.y, model->model_maxbbox.z);
			gl::vertex(model->model_minbbox.x, model->model_minbbox.y, model->model_maxbbox.z);
			gl::vertex(model->model_maxbbox.x, model->model_minbbox.y, model->model_maxbbox.z);
			gl::end();

			// bottom
			gl::begin(gl::LineLoop);
			gl::vertex(model->model_maxbbox.x, model->model_maxbbox.y, model->model_minbbox.z);
			gl::vertex(model->model_minbbox.x, model->model_maxbbox.y, model->model_minbbox.z);
			gl::vertex(model->model_minbbox.x, model->model_minbbox.y, model->model_minbbox.z);
			gl::vertex(model->model_maxbbox.x, model->model_minbbox.y, model->model_minbbox.z);
			gl::end();
		
			gl::begin(gl::Lines);
			gl::vertex(model->model_maxbbox.x, model->model_maxbbox.y, model->model_maxbbox.z);
			gl::vertex(model->model_maxbbox.x, model->model_maxbbox.y, model->model_minbbox.z);
			gl::vertex(model->model_minbbox.x, model->model_maxbbox.y, model->model_maxbbox.z);
			gl::vertex(model->model_minbbox.x, model->model_maxbbox.y, model->model_minbbox.z);
			gl::vertex(model->model_minbbox.x, model->model_minbbox.y, model->model_maxbbox.z);
			gl::vertex(model->model_minbbox.x, model->model_minbbox.y, model->model_minbbox.z);
			gl::vertex(model->model_maxbbox.x, model->model_minbbox.y, model->model_maxbbox.z);
			gl::vertex(model->model_maxbbox.x, model->model_minbbox.y, model->model_minbbox.z);
			gl::end();

			gl::pop();
			
		}
	}
}

/* draw model geometry fragments */
void draw_pass_model_fragments()
{
	for (core::Zone::Content::iterator it = zone->content().begin(); it != zone->content().end(); it++) {
		core::Entity *entity = (*it);

		if (entity->model() && entity->state()->visible()) {
			gl::push();
			gl::translate(entity->state()->location()  - Camera::eye());
			gl::multmatrix(entity->state()->axis());

			draw_model_fragments(entity);

			gl::pop();
		}
	}
}

/* draw model lights and engines */
void draw_pass_model_fx(float elapsed)
{
	const size_t count = 8; // number of engine trail particles

	float t = 0.0f;
	float u = 0.0f;
	float a = 0.0f;
	float light_size = 0.0f;
	float engine_size = 0.0f;
	float trail_size = 0.0f;
	
	math::Vector3f quad[4];
	math::Vector3f location;
	math::Vector3f offset;
	math::Color color;

	model::Light *light;
	model::Flare *flare;
	model::Engine *engine;

	math::Axis flare_axis;

	size_t circle_texture = Textures::load("bitmaps/fx/circle00");
	size_t current_texture = Textures::bind("bitmaps/fx/flare00");

	gl::enable(GL_TEXTURE_2D);
	gl::begin(gl::Quads);

	for (core::Zone::Content::iterator it = zone->content().begin(); it != zone->content().end(); it++) {
		core::Entity *entity = (*it);

		if (entity->model() &&  entity->state() && entity->state()->detailvisible()) {

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

				// strobe frequency
				t = 1.0f;
				if (light->strobe())
					t = (core::application()->time() - light->offset() - entity->state()->fuzz()) * light->frequency();
				if ((!light->strobe()) || (( t - floorf(t)) <= light->time())) {
					location.assign(entity->state()->location()  - Camera::eye() + (entity->state()->axis() * light->location()));
					light_size = 0.0625 * light->radius();
	
					if (current_texture != light->texture()) {
						gl::end();
						current_texture = Textures::bind(light->texture());
						gl::begin(gl::Quads);
					}

					if (light->entity()) {
						color.assign(entity->color());
					} else {
						color.assign(light->color());
					}
					color.a = 0.8;
					gl::color(color);
					
					glTexCoord2f(0,1);
					gl::vertex(location + (Camera::axis().up() - Camera::axis().left()) * light_size);
					glTexCoord2f(0,0);
					gl::vertex(location + (Camera::axis().up() + Camera::axis().left()) * light_size);
					glTexCoord2f(1,0);
					gl::vertex(location + (Camera::axis().up() * -1 + Camera::axis().left()) * light_size);
					glTexCoord2f(1,1);
					gl::vertex(location + (Camera::axis().up() * -1 - Camera::axis().left()) * light_size);
					Stats::quads++;
				}
			}
			
			// draw flares
			for (model::Model::Flares::iterator flit = entity->model()->flares().begin(); flit != entity->model()->flares().end(); flit++) {				
				flare = (*flit);
				
				// strobe frequency
				t = 1.0f;
				if (flare->strobe())
					t = (core::application()->time() + entity->state()->fuzz() - flare->offset()) * flare->frequency();

				if ((!flare->strobe()) || (( t - floorf(t)) <= flare->time())) {
					flare_axis.assign(entity->state()->axis());
					if (flare->angle())
						flare_axis.change_direction(flare->angle());

					location.assign(entity->state()->location()  - Camera::eye() + (entity->state()->axis() * flare->location()));
					light_size = 0.0625 * flare->radius();
	
					if (current_texture != flare->texture()) {
						gl::end();
						current_texture = Textures::bind(flare->texture());
						gl::begin(gl::Quads);
					}

					if (flare->entity()) {
						color.assign(entity->color());
					} else {
						color.assign(flare->color());
					}

					// FIXME Camera::axis().forward() should be the vector flare_location - camera_eye
					a = dotproduct(flare_axis.forward(), Camera::axis().forward());
					if (a < -0.1f) {
						color.a = -a - 0.1f;
						gl::color(color);
						
						glTexCoord2f(0,1);
						gl::vertex(location + (flare_axis.up() + flare_axis.left()) * light_size);
						glTexCoord2f(0,0);
						gl::vertex(location + (flare_axis.up() - flare_axis.left()) * light_size);
						glTexCoord2f(1,0);
						gl::vertex(location + (flare_axis.up() * -1 - flare_axis.left()) * light_size);
						glTexCoord2f(1,1);
						gl::vertex(location + (flare_axis.up() * -1 + flare_axis.left()) * light_size);
	
						Stats::quads++;
					}
					
				}
			}

			// draw model engines for Controlable entities
			if ((entity->type() == core::Entity::Controlable) && entity->model()->engines().size()) {

				u = static_cast<core::EntityControlable *>(entity)->thrust();

				if (u > 0) {
					t = entity->state()->state_engine_trail_offset;
					t += elapsed * 4.0f * u;
				
					if (t > 1.0f)
						t -= 1.0f;
					math::clamp(t, 0.0f, 1.0f);
					entity->state()->state_engine_trail_offset = t;

					for(model::Model::Engines::iterator eit = entity->model()->engines().begin(); eit != entity->model()->engines().end(); eit++) {
	
						engine = (*eit);
	
						location.assign(entity->state()->location()  - Camera::eye() + (entity->state()->axis() * engine->location()));
						engine_size = 0.0625 * engine->radius();
						color.assign(engine->color());
						color.a = 0.9f * u;
	
						if (current_texture != engine->texture() ) {
							gl::end();
							current_texture = Textures::bind(engine->texture());
							gl::begin(gl::Quads);
						}
	
						quad[0].assign(entity->state()->axis().up() - entity->state()->axis().left());
						quad[1].assign(entity->state()->axis().up() + entity->state()->axis().left());
						quad[2].assign(entity->state()->axis().up() * -1 + entity->state()->axis().left());
						quad[3].assign(entity->state()->axis().up() * -1 - entity->state()->axis().left());
	
						gl::color(color);					
						glTexCoord2f(0,1);
						gl::vertex(location + quad[0] * engine_size);
						glTexCoord2f(0,0);
						gl::vertex(location + quad[1] * engine_size);
						glTexCoord2f(1,0);
						gl::vertex(location + quad[2] * engine_size);
						glTexCoord2f(1,1);
						gl::vertex(location + quad[3] * engine_size);
						Stats::quads++;
	
						// draw the engine trail
						if (current_texture != circle_texture) {
							gl::end();
							current_texture = Textures::bind(circle_texture);
							gl::begin(gl::Quads);
						}
						color.assign(1.0f, 1.0f);
						offset.assign(entity->state()->axis().forward() * engine_size);

						if (t > 0)
							location -= offset * t;

						for (size_t i = count; i > 0; i--) {
							trail_size = engine_size * 0.8f * (0.4f + ((-t + (float) i) / (float) count) * 0.6f);
							color.a = (0.1f + ((-t + (float) i) / (float) count) * 0.7f) * u;
							gl::color(color);
							glTexCoord2f(0,1);
							gl::vertex(location + quad[0] * trail_size);
							glTexCoord2f(0,0);
							gl::vertex(location + quad[1] * trail_size);
							glTexCoord2f(1,0);
							gl::vertex(location + quad[2] * trail_size);
							glTexCoord2f(1,1);
							gl::vertex(location + quad[3] * trail_size);
							Stats::quads++;
	
	
							gl::color(color);
							glTexCoord2f(1,1);
							gl::vertex(location + quad[3] * trail_size);
							glTexCoord2f(1,0);
							gl::vertex(location + quad[2] * trail_size);
							glTexCoord2f(0,0);
							gl::vertex(location + quad[1] * trail_size);
							glTexCoord2f(0,1);
							gl::vertex(location + quad[0] * trail_size);
							Stats::quads++;

							location -= offset;
						}
					}
				}
			}
		}	
	}

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

void draw_pass_model_corona()
{
	if (!(r_radius && r_radius->value()))
		return;

	for (core::Zone::Content::iterator it = zone->content().begin(); it != zone->content().end(); it++) {
		core::Entity *entity = (*it);

		if (entity->model() && entity->state()->visible()) {
			gl::push();
			gl::translate(entity->state()->location()  - Camera::eye());
			math::Color color = entity->color();
			color.a = 0.25f;
			draw_sphere(color, entity->model()->radius());
			gl::pop();
		}
	}

}


/* Draw skysphere */
void draw_pass_sky()
{
	if (!(r_sky && r_sky->value()))
		return;

	if (!core::localplayer()->zone())
		return;

	if (!core::localplayer()->zone()->sky_texture() && core::localplayer()->zone()->sky().size()) {

		std::string texture_name("textures/env/");
		texture_name.append(core::localplayer()->zone()->sky());
		core::localplayer()->zone()->set_sky_texture(Textures::load(texture_name));

		if (!core::localplayer()->zone()->sky_texture())
			core::localplayer()->zone()->set_sky("");
	}

	Textures::bind(core::localplayer()->zone()->sky_texture());
	gl::enable(GL_TEXTURE_2D);
	gl::push();

	//gl::translate(Camera::eye());	
	draw_sphere_inside(math::Color(), 128);

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

void draw_pass_spacegrid()
{
	if (!(r_grid && r_grid->value()))
		return;

	int gridsize = 32;
	float s = 1.0f / gridsize;
	float z = -4.0f;

	float dx =  Camera::target().x - floorf(Camera::target().x);
	float dy =  Camera::target().y - floorf(Camera::target().y);

	gl::push();
	gl::translate(Camera::target() - Camera::eye());
	gl::color(0,0, 1.0f);
	gl::normal(0, 0, 1.0f);

	gl::begin(gl::Lines);

	for (int i=-gridsize; i <= gridsize; i++) {
		gl::color(0,0, 0, 0);
		gl::vertex(i-dx, -gridsize-dy, z);
		gl::color(0,0, (gridsize-abs(i))*s, (gridsize-abs(i))*s);
		gl::vertex(i-dx, -dy, z );
		gl::vertex(i-dx, -dy ,z );
		gl::color(0,0, 0, 0);
		gl::vertex(i-dx, gridsize-dy, z);

		gl::vertex(-gridsize-dx, i-dy, z );
		gl::color(0,0, (gridsize-abs(i))*s, (gridsize-abs(i))*s);
		gl::vertex(-dx, i-dy, z);
		gl::vertex(-dx, i-dy, z);
		gl::color(0,0, 0, 0);
		gl::vertex(gridsize-dx, i-dy, z);
	}
	gl::end();

	gl::pop();
}

/* ----- Main draw routine ----------------------------------------- */

void draw(float seconds)
{
	zone = core::localplayer()->zone();
	if (!zone)
		return;
	
	Camera::frame(seconds);		
	Camera::draw(); // draw the current camera transformation

	// calculate client state
	pass_prepare(seconds);

	// enable wireframe mode if requested
	if (r_wireframe && r_wireframe->value()) {
		glPolygonMode(GL_FRONT, GL_LINE);
	} else {
		glPolygonMode(GL_FRONT, GL_FILL);
	}

	// set vertex array pointers	
	glVertexPointer(3, GL_FLOAT, 0, vertexarray->vertex());
	glNormalPointer(GL_FLOAT, 0, vertexarray->normal());
	glColorPointer(3, GL_FLOAT, 0, vertexarray->color());
	glTexCoordPointer(3, GL_FLOAT, 0, vertexarray->texture());

	// enable vertex arrays
	glEnableClientState(GL_VERTEX_ARRAY);
	glEnableClientState(GL_COLOR_ARRAY);
	glEnableClientState(GL_TEXTURE_COORD_ARRAY);
	glEnableClientState(GL_NORMAL_ARRAY);
	glDisableClientState(GL_COLOR_ARRAY);

	gl::enable(GL_DEPTH_TEST);	
	gl::depthmask(GL_FALSE);	// disable depth buffer writing

	draw_pass_sky();		// draw the skysphere

	
	gl::depthmask(GL_TRUE);		// enable writing to the depth buffer

	gl::enable(GL_CULL_FACE);	// enable culling
	gl::enable(GL_COLOR_MATERIAL);	// enable color tracking
	gl::enable(GL_LIGHTING);	
	//gl::enable(GL_RESCALE_NORMAL);	// rescale normals by the transformation matrix scale factor
	gl::enable(GL_NORMALIZE);
	
	draw_pass_default();		// draw entities without model
	
	gl::disable(GL_NORMALIZE);
	//gl::disable(GL_RESCALE_NORMAL);

	glEnableClientState(GL_COLOR_ARRAY);

	draw_pass_model_fragments();

	glDisableClientState(GL_COLOR_ARRAY);
	gl::disable(GL_LIGHTING);
	
	gl::enable(GL_BLEND);
	gl::depthmask(GL_FALSE);	// disable depth buffer writing
	
	draw_pass_spacegrid();		// draw the blue spacegrid
	Dust::draw();			// draw spacedust

	
	draw_pass_model_fx(seconds);		// draw entity lights and engines

	gl::enable(GL_LIGHTING);
	gl::enable(GL_RESCALE_NORMAL);

	draw_pass_model_corona();	// draw entity radius
	
	glDisableClientState(GL_VERTEX_ARRAY);
	glDisableClientState(GL_NORMAL_ARRAY);
	glDisableClientState(GL_TEXTURE_COORD_ARRAY);

	gl::disable(GL_RESCALE_NORMAL);
	gl::disable(GL_LIGHTING);
	gl::disable(GL_COLOR_MATERIAL);	// disable color tracking
	gl::disable(GL_CULL_FACE);	// disable culling
	
	gl::depthmask(GL_TRUE);	// enable depth buffer writing
	gl::disable(GL_DEPTH_TEST);	// disable depth buffer testing
	// GL_BLEND must be enabled for the GUI
}

// reset drawing parameters for a new localcontrol
void reset()
{
	Dust::reset();
}

}