Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
blob: 60726b090399a47ad69cbedfc245425134d26e9f (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
/*
   client/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 "render/render.h"
#include "render/sphere.h"
#include "render/box.h"
#include "client/client.h"
#include "client/camera.h"
#include "client/draw.h"

namespace client 
{

render::Sphere sphere(math::Vector3f(0,0,0),1);
render::Box cube(math::Vector3f(0.5f, 0.5f, 0.5f), math::Vector3f(-0.5f, -0.5f, -0.5f));

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

void draw_entity_cube(core::Entity *entity)
{
	cube.topcolor = entity->color();
	cube.bottomcolor = entity->color();
	cube.radius = entity->radius();
	cube.draw();
}


void draw_entity_diamond(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,0.0f,r/2);
	gl::vertex(0.0f,0.0f,-r/2);

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

// draw an entity of entity_type core::Entity::Default
void draw_entity_default(core::Entity *entity)
{
	render::gl::push();
	render::gl::translate(entity->location());
	
	switch(entity->shape()) {		
		case core::Entity::Sphere:
			draw_entity_sphere(entity);
			break;

		case core::Entity::Diamond:
			draw_entity_diamond(entity);
			break;

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


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

math::Vector3f v4(-1.0f, -1.0f, -1.0f);
math::Vector3f v5(-1.0f,  1.0f, -1.0f);
math::Vector3f v6(-1.0f,  1.0f,  1.0f);
math::Vector3f v7(-1.0f, -1.0f,  1.0f);
float angle = 0;

void draw_ship(core::EntityControlable *entity)
{
	using math::Vector3f;
	using math::Color;
	using namespace render;

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

	Vector3f tl(0.25, 0.125, 0.125);
	Vector3f br(-0.25, -0.125, -0.125);
	
	Box box(tl, br);
	box.draw();

	tl = Vector3f(0, 0.07, 0.25);
	br = Vector3f(-0.5, -0.07, 0.125);
	Box engine1(tl, br);
	engine1.topcolor = Color(0.7, 0.7, 0.7);
	engine1.bottomcolor = engine1.topcolor * 0.5;
	engine1.draw();

	tl = Vector3f(0, 0.07, -0.125);
	br = Vector3f(-0.5, -0.07, -0.25);
	Box engine2(tl, br);
	engine2.topcolor = engine1.topcolor;
	engine2.bottomcolor = engine1.bottomcolor;
	engine2.draw();

	tl = Vector3f(0.4, 0.07, 0.07);
	br = Vector3f(0.25, -0.07, -0.07);
	Box cockpit(tl, br);
	cockpit.topcolor = engine1.topcolor;
	cockpit.bottomcolor = engine1.bottomcolor;
	cockpit.draw();

	if(entity->thrust() > 0 ) {
		gl::color(1.0f,0 ,0 );
		gl::begin(gl::Lines);
		gl::vertex(-0.5f, 0, 0.185);
		gl::vertex(-0.5f-0.25f*entity->thrust(), 0, 0.185);
	
		gl::vertex(-0.5f, 0, -0.185f);
		gl::vertex(-0.5f-0.25f*entity->thrust(), 0, -0.185f);
		gl::end();
	}

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

	// draw the shield
	gl::color(Color(0.0f, 1.0f ,0.0f , 0.5f));
	
	gl::begin(gl::LineStrip);
	gl::vertex(v0);
	gl::vertex(v1);
	gl::vertex(v2);
	gl::vertex(v3);
	gl::vertex(v0);
	gl::end();
	
	gl::begin(gl::LineStrip);
	gl::vertex(v4);
    	gl::vertex(v5);
        gl::vertex(v6);
        gl::vertex(v7);
	gl::vertex(v4);
	gl::end();

	gl::pop();
}

void draw_spacegrid()
{
	using namespace render::gl;

	translate(camera::target);

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

	float dx =  camera::target.x - floorf(camera::target.x);
	float dz =  camera::target.z - floorf(camera::target.z);

	color(0,0, 1.0f);
	begin(Lines);
	for (int i=-gridsize; i <= gridsize; i++) {
		color(0,0, 0, 0);
		vertex(i-dx, y, -gridsize-dz);
		color(0,0, (gridsize-abs(i))*s, (gridsize-abs(i))*s);
		vertex(i-dx, y, -dz);
		vertex(i-dx, y, -dz);
		color(0,0, 0, 0);
		vertex(i-dx, y, gridsize-dz);

		vertex(-gridsize-dx, y, i-dz);
		color(0,0, (gridsize-abs(i))*s, (gridsize-abs(i))*s);
		vertex(-dx, y, i-dz);
		vertex(-dx, y, i-dz);
		color(0,0, 0, 0);
		vertex(gridsize-dx, y, i-dz);
	}
	end();
}

void draw_world(float seconds)
{
	// used for animations
    	angle += 180.0f * seconds;
    	if( angle > 360.0f ) {
            angle -= 360.0f;
    	}

	// draw entities
	using namespace render;
	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_ship(static_cast<core::EntityControlable *> ((*it).second));
				break;
			default:
				break;
		}
	}

	// draw the background grid
	draw_spacegrid();
}

}