Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
blob: 9e1e5ea645b695278720074009f723e89a7207bb (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
/*
   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 "core/core.h"
#include "core/model.h"
#include "render/render.h"
#include "render/draw.h"
#include "render/sphere.h"

namespace render
{

Sphere sphere(1);

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;
math::Vector3f camera_target;
float angle = 0;

void draw_model(core::Model *model, core::Entity *entity)
{
	// draw all faces
	for (std::list<core::Face *>::iterator fit = model->model_face.begin(); fit != model->model_face.end(); fit++) {	
		if ((*fit)->color()) {
			render::gl::color(*(*fit)->color());
		} else {
			render::gl::color(entity->color());
		}

		// draw all vertexes
		gl::begin(gl::Polygon);
		//gl::begin(gl::LineLoop);
		gl::normal((*fit)->normal());
		for (std::vector<math::Vector3f *>::iterator vit = (*fit)->face_vertex.begin(); vit != (*fit)->face_vertex.end(); vit++) {
			gl::vertex(*(*vit));
		}
		gl::end();
	}

	if (r_drawradius && r_drawradius->value()) {
		sphere.sphere_color = entity->color();
		sphere.sphere_color.a = 0.25f;
		sphere.radius = model->radius();
		gl::enable(GL_BLEND);		// enable alpha blending again
		sphere.draw();
		gl::disable(GL_BLEND);
	}
}

void draw_model_engines(core::Model *model, core::EntityControlable *entity)
{
	if (model->model_engine.size() && entity->thrust()) {
		gl::color(1.0f,0 ,0);
		gl::begin(gl::Lines);
		
		for (std::list<core::Engine *>::iterator eit = model->model_engine.begin(); eit != model->model_engine.end(); eit++) {
			math::Vector3f const & v = (*eit)->location();
			gl::vertex(v);
			gl::vertex(v.x - 0.0625f*entity->thrust(), v.y, v.z);
		}
		gl::end();
	}

}

void draw_model_lights(core::Model *model, core::Entity *entity)
{
	if (model->model_light.size()) {
		gl::disable(GL_LIGHTING);
		glPointSize(10);
		gl::begin(gl::Points);

		for (std::list<core::Light *>::iterator lit = model->model_light.begin(); lit != model->model_light.end(); lit++) {
			math::Vector3f location = (*lit)->location();
			gl::color((*lit)->color());
			gl::vertex(location);
		}
		
		gl::end();
		glPointSize(1);
		gl::enable(GL_LIGHTING);
	}
}

void draw_entity_sphere(core::Entity *entity)
{
	sphere.sphere_color = entity->color();
	sphere.radius = entity->radius();
	sphere.draw();
}

void draw_entity_cube(core::Entity *entity)
{
	float radius = entity->radius()/2;
	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_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();
}


// draw an entity of entity_type core::Entity::Default
void draw_entity_default(core::Entity *entity)
{
	using namespace render;

	core::Model *model = 0;
	if (entity->modelname().size()) {
		model = core::Model::get(entity->modelname());
	}

	if (model && math::distancesquared(camera_target, entity->location()) > drawdistance*drawdistance*model->radius())
		return;


	gl::push();
	gl::translate(entity->location());
	gl::rotate(entity->direction(), 0.0f, 0.0f, 1.0f );

	if (model) {
		draw_model(model, entity);
		//draw_model_lights(model, entity);
	} else {
		if ((entity->flags() & core::Entity::Bright) == core::Entity::Bright)
			gl::disable(GL_LIGHTING);

		switch(entity->shape()) {		
			case core::Entity::Sphere:
				draw_entity_sphere(entity);
				break;
			
			case core::Entity::Diamond:

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

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

		if ((entity->flags() & core::Entity::Bright) == core::Entity::Bright)
			gl::enable(GL_LIGHTING);
	}

	gl::pop();
}

// draw an entity of entity_type core::Entity::Controlable
void draw_entity_controlable(core::EntityControlable *entity)
{
	core::Model *model = 0;
	if (entity->modelname().size())
		model = core::Model::get(entity->modelname());

	gl::push();
	gl::translate(entity->location());
	gl::rotate(entity->direction(), 0.0f, 0.0f, 1.0f );

	if (model ) {
		draw_model(model, entity);
		draw_model_engines(model, entity);
		//draw_model_lights(model, entity);
	}

	// shield rotation
    	gl::rotate(angle, 0.0f, 0.0f, 1.0f );
	gl::scale(0.2f,  0.2f,  0.2f);

	// draw the shield
	gl::color(math::Color(0.0f, 1.0f ,0.0f , 0.5f));

	gl::begin(gl::LineLoop);
	gl::normal(0, 0.5, 0.5);
	gl::vertex(v1);
	gl::normal(0, -0.5, 0.5);
	gl::vertex(v0);
	gl::normal(0, -0.5, -0.5);
	gl::vertex(v4);
	gl::normal(0, 0.5, -0.5);
	gl::vertex(v5);
	gl::end();
	
	gl::begin(gl::LineLoop);
	gl::normal(0, -0.5, 0.5);
	gl::vertex(v3);
	gl::normal(0, 0.5, 0.5);
    	gl::vertex(v2);
	gl::normal(0, 0.5, -0.5);
        gl::vertex(v6);
	gl::normal(0, -0.5, -0.5);
        gl::vertex(v7);
	gl::end();

	gl::pop();
}


void draw_spacegrid(math::Vector3f const &target)
{
	int gridsize = 32;
	float s = 1.0f / gridsize;
	float z = -4.0f;

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

	gl::translate(target);
	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();
}

void draw(math::Vector3f const &eye, math::Vector3f const &target, float seconds)
{
	// used for animations
    	angle += 180.0f * seconds;
    	if( angle > 360.0f ) {
            angle -= 360.0f;
    	}

	camera_target = target;

	// draw entities
	using namespace render;
	
	gl::enable(GL_DEPTH_TEST);	// enable depth buffer writing
	gl::enable(GL_CULL_FACE);	// enable culling
	gl::enable(GL_COLOR_MATERIAL);	// enable color tracking
	gl::enable(GL_LIGHTING);

	gl::disable(GL_BLEND);		// disbable alpha blending for world polys

	std::map<unsigned int, core::Entity *>::iterator it;
	for (it=core::Entity::registry.begin(); it != core::Entity::registry.end(); it++) {
		switch ((*it).second->type()) {
			case core::Entity::Default:
				draw_entity_default((*it).second);
				break;
			case core::Entity::Controlable:
				draw_entity_controlable(static_cast<core::EntityControlable *> ((*it).second));
				break;
			default:
				break;
		}
	}

	gl::disable(GL_LIGHTING);
	gl::disable(GL_COLOR_MATERIAL);	// disable color tracking
	gl::disable(GL_CULL_FACE);	// disable culling
	gl::enable(GL_BLEND);		// enable alpha blending again

	draw_spacegrid(target);		// draw the blue spacegrid

	gl::disable(GL_DEPTH_TEST);	// disable depth buffer writing
}

}