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

#include <string>
#include <sstream>

#include "SDL/SDL.h"

#include "render/state.h"
#include "render/gl.h"
#include "render/render.h"

namespace render
{

int	State::state_width = 0;
int 	State::state_height = 0;
float	State::state_aspect = 0;
bool	State::state_has_generate_mipmaps = false;
bool	State::state_has_vbo = false;
GLuint	State::state_vbo = 0;
int	State::state_maxlights;
int	State::state_maxtextureunits;
size_t	State::state_logo_texture_id = 0;

	
math::Color	State::state_color_primary;
math::Color	State::state_color_secondary;
math::Color	State::state_color_engine;

bool		State::state_power = true;

void State::init(int width, int height)
{
	resize(width, height);

	state_has_generate_mipmaps = false;

	std::string version(gl::version());
	for (size_t i = 0; i < version.size(); i++) {
		if (version[i] == '.')
			version[i] = ' ';
	}

	std::stringstream versionstream(version);
	int major, minor;
	if (versionstream >> major >> minor) {

		if (major > 1) {
			state_has_generate_mipmaps = true;
		} else if (major == 1) {
			if (minor > 3)
				state_has_generate_mipmaps = true;
		}

	} else {
		con_warn << "Could not determine OpenGL version!" << std::endl;
	}
	
	con_print << "  hardware generated mipmaps ";
	if (state_has_generate_mipmaps)
		con_print << "available" << std::endl;
	else
		con_print << "not available" << std::endl;
	
	// initialize gl functions
	state_has_vbo = true;
	gl::genbuffers = (gl::genbuffers_func) SDL_GL_GetProcAddress("glGenBuffers");
	if (!gl::genbuffers) {
		con_debug << "  glGenBuffers not available" << std::endl;
		state_has_vbo = false;
	}
	
	gl::deletebuffers = (gl::deletebuffers_func) SDL_GL_GetProcAddress("glDeleteBuffers");
	if (!gl::deletebuffers) {
		con_debug << "  glDeleteBuffers not available" << std::endl;
		state_has_vbo = false;
	}

	gl::bindbuffer = (gl::bindbuffer_func) SDL_GL_GetProcAddress("glBindBuffer");
	if (!gl::bindbuffer) {
		con_debug << "  glBindBuffer not available" << std::endl;
		state_has_vbo = false;
	}
	
	gl::bufferdata = (gl::bufferdata_func) SDL_GL_GetProcAddress("glBufferData");
	if (!gl::bufferdata) {
		con_debug << "  glBufferData not available" << std::endl;
		state_has_vbo = false;
	}

	con_print << "  vertex buffer objects ";
	if (state_has_vbo)
		con_print << "enabled" << std::endl;
	else
		con_print << "disabled" << std::endl;

	// probe maximal number of lights
	gl::getinteger(GL_MAX_LIGHTS, &state_maxlights);
	con_debug << "  maximum number of OpenGL lights is " << state_maxlights << std::endl;

	// probe maximal number of texture units
	gl::getinteger(GL_MAX_TEXTURE_UNITS, &state_maxtextureunits);
	con_debug << "  maximum number of OpenGL texture units is " << state_maxtextureunits << std::endl;
	
	// bind multitexture functions
	gl::activetexture = (gl::activetexture_func) SDL_GL_GetProcAddress("glActiveTexture");
	gl::clientactivetexture = (gl::activetexture_func) SDL_GL_GetProcAddress("glClientActiveTexture");
	
	// Generate VBO
	if (state_has_vbo)
		gl::genbuffers(1, &state_vbo);
}

void State::shutdown()
{
	// Delete VBO
	if (state_has_vbo)
		gl::deletebuffers(1, &state_vbo);
}

void State::resize(int width, int height)
{
	state_width = width;
	state_height = height;

	state_aspect = (float) width / (float) height;

	clear();
}

void State::clear()
{
	// set viewport
	gl::viewport(0, 0, state_width, state_height);

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

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

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

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

	// lighting model
	gl::lightmodel(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);
	gl::lightmodel(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);

	// color tracking
	gl::colormaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);

	// 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 State::set_normalize(const bool enable)
{
	if (r_normalize && r_normalize->value()) {
		// enable full normalization
		if(enable) {
			gl::enable(GL_NORMALIZE);
		} else {
			gl::disable(GL_NORMALIZE);
		}
	} else {
		// enable rescaling of normals
		if(enable) {
			gl::enable(GL_RESCALE_NORMAL);
		} else {
			gl::disable(GL_RESCALE_NORMAL);
		}
	}
}

void State::set_color(const math::Color & color)
{
	state_color_primary.assign(color);
}

void State::set_color_second(const math::Color & color)
{
	state_color_secondary.assign(color);
}
	
void State::set_color_engine(const math::Color & color) {
	state_color_engine.assign(color);
}

void State::set_color(const core::Entity *entity)
{
	state_color_primary.assign(entity->color());
	state_color_secondary.assign(entity->color_second());
}

void State::set_power(const bool power)
{
	state_power = power;
}

void State::use_material_layer(const model::Material * material, const model::Layer *layer)
{
	if (!material || !layer) {
		gl::color(math::Color(1.0f, 0.0f, 1.0f));
		gl::specular(math::Color(1.0f, 0.0f, 1.0f));
		return;
	}
		
	// material has the decal flag set
	if (material->has_flag(model::Material::FlagDecal)) {
		gl::enable(GL_POLYGON_OFFSET_FILL);
		gl::enable(GL_ALPHA_TEST);
		gl::polygonoffset(-1,-1);
		gl::alphafunc(GL_GEQUAL, 0.5f);
	}
	
	math::Color color(layer->color());
	math::Color specular(layer->color());
	
	// apply color generation rules
	switch (layer->rgbgen()) {
		case model::Layer::RGBGenIdentity:
			break;
			
		case model::Layer::RGBGenColor:
			break;
			
		case model::Layer::RGBGenEngine:
			// assign current engine color
			color.assign(state_color_engine);
			specular.assign(0.0f);
			break;
			
		case model::Layer::RGBGenPrimary:
			// assign current primary entity color
			color.assign(state_color_primary);
			specular.assign(state_color_primary);
			break;
			
		case model::Layer::RGBGenSecondary:
			// assign current secondry entity color
			color.assign(state_color_secondary);
			specular.assign(state_color_secondary);
			break;
			
		case model::Layer::RGBGenTertiary:
			// assign current tertiary entity color
			for (size_t i = 0; i < 3; i++) {
				color[i] = (state_color_primary[i] + state_color_secondary[i]) / 2;
				specular[i] = (state_color_primary[i] + state_color_secondary[i]) / 2;
			}
			break;
	}
	
	// blend current color with layer color
	if ((layer->rgbgen() != model::Layer::RGBGenIdentity) && (layer->rgbgen() != model::Layer::RGBGenColor)) {
		color.r *= layer->color().r;
		color.g *= layer->color().g;
		color.b *= layer->color().b;
		specular.r *= layer->specular().r;
		specular.g *= layer->specular().g;
		specular.b *= layer->specular().b;
	}
	
	gl::color(color);
	gl::specular(specular);
	
	// lighted or fullbright
	if (state_power && layer->fullbright()) {
		gl::disable(GL_LIGHTING);
		
	} else if (state_power && (layer->rgbgen() == model::Layer::RGBGenEngine)) {
		gl::disable(GL_LIGHTING);
		
	} else {
		gl::enable(GL_LIGHTING);
	}
	
	// texture map
	if (layer->texmap() == model::Layer::TexMapImage)  {
		
		// map a texture image		
		Textures::bind(layer->texture_id());
		gl::enable(GL_TEXTURE_2D);
		
		// texture coordinate generation
		if (layer->tcgen() == model::Layer::TCGenEnvironment) {
			gl::texgen(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
			gl::texgen(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
				
			gl::enable(GL_TEXTURE_GEN_S);
			gl::enable(GL_TEXTURE_GEN_T);	
		}
		
	} else if (layer->texmap() == model::Layer::TexMapEnvironment) {
		
		// map the current skybox
		
		if (core::localplayer()->zone()->sky().size()) {
			gl::enable(GL_TEXTURE_CUBE_MAP);
				
			gl::texgen(GL_S, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP);
			gl::texgen(GL_T, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP);
			gl::texgen(GL_R, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP);
				
			gl::enable(GL_TEXTURE_GEN_S);
			gl::enable(GL_TEXTURE_GEN_T);
			gl::enable(GL_TEXTURE_GEN_R);
		} else {
			color.assign(0.0f, 0.0f, 0.0f);
		}
		gl::material(GL_FRONT, GL_SHININESS, 4);
		
	}
	
	// alpha blending
	switch(layer->blendfunc()) {
		case model::Layer::BlendFuncNone:
			gl::disable(GL_BLEND);
			break;
		case model::Layer::BlendFuncAdd:
			gl::blendfunc(GL_ONE, GL_ONE);
			gl::enable(GL_BLEND);
			break;
		case model::Layer::BlendFuncBlend:
			gl::enable(GL_BLEND);
			break;
	}	
}

void State::reset()
{
	gl::blendfunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
	gl::enable(GL_BLEND);
	
	gl::disable(GL_POLYGON_OFFSET_FILL);
	gl::disable(GL_ALPHA_TEST);
	gl::disable(GL_TEXTURE_GEN_S);
	gl::disable(GL_TEXTURE_GEN_T);
	gl::disable(GL_TEXTURE_GEN_R);
	gl::disable(GL_LIGHTING);
	gl::disable(GL_TEXTURE_CUBE_MAP);
	gl::disable(GL_TEXTURE_2D);
	
	// default specular shininess setting
	gl::material(GL_FRONT, GL_SHININESS, 8);
	
	gl::color(math::Color(1.0f));
	gl::specular(math::Color(0.0f));
}
	
} // namespace render