Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
blob: 9f47f1eeaffe86011307d9f9f777dde184b2baf2 (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
/*
   model/material.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 "model/material.h"

#include "auxiliary/functions.h"
#include "filesystem/filestream.h"
#include "math/functions.h"
#include "sys/sys.h"

namespace model
{

Material::ImageLoaderFuncPtr 	Material::material_imageloaderfunc = 0;
Material::Registry 		Material::material_registry;

Material::Material(const std::string &name) :
		material_name(name),
		material_flags(0),
		material_size(64.0f, 64.0f)
{
	aux::to_lowercase(material_name);
}

Material::~Material()
{
	for (Layers::iterator it = material_layers.begin(); it != material_layers.end(); ++it) {
		delete (*it);
		(*it) = 0;
	}
}

void Material::set_size(const math::Vector2f & size)
{
	material_size.assign(size);
}

Layer * Material::add_layer()
{	
	Layer * layer = new Layer();
	material_layers.push_back(layer);
	return (layer);
}

void Material::print()
{
	con_print << name() << std::endl;
	con_print << "  flags:          " << flags() << " ";
	if (flags() == FlagNone) {
		con_print << "none";
	} else {
		if (flags() & FlagIgnore) {
			con_print << "ignore ";
		}
		if (flags() & FlagClip) {
			con_print << "clip ";
		}
		if (flags() & FlagOrigin) {
			con_print << "origin ";
		}
		if (flags() & FlagDecal) {
			con_print << "decal ";
		}
		if (flags() & FlagBounds) {
			con_print << "bounds ";
		}
	}

	int count = 0;
	
	for (Layers::const_iterator lit = material_layers.begin(); lit != material_layers.end(); ++lit) {
		const Layer *layer = (*lit);
		
		count++;
		con_print << "  layer " << count << std::endl;
		
		con_print << "    texture: ";
		if (layer->texmap() == Layer::TexMapNone) {
			con_print << "none";
		} else if (layer->texmap() == Layer::TexMapImage) {
			con_print << layer->texture();
		}
		con_print << std::endl;
		
		con_print << "    color:       " << layer->color() << std::endl;
		
		con_print << "    rgbgen:     ";		
		switch (layer->rgbgen()) {
			case Layer::RGBGenIdentity:
				con_print << "identity";
				break;
			case Layer::RGBGenColor:
				con_print << "color";
				break;
			case Layer::RGBGenEngine:
				con_print << "engine";
				break;
			case Layer::RGBGenPrimary:
				con_print << "entity primary";
				break;
			case Layer::RGBGenSecondary:
				con_print << "entity secondary";
				break;
			case Layer::RGBGenTertiary:
				con_print << "entity tertiary";
				break;
		}
		con_print << std::endl;
		
		if (layer->fullbright()) {
			con_print << "    fullbright" << std::endl;
		}
		
		switch(layer->blendfunc()) {
			case Layer::BlendFuncNone:
				break;
			case Layer::BlendFuncAdd:
				con_print << "    blendfunc:  add" << std::endl;
				break;
			case Layer::BlendFuncBlend:
				con_print << "    blendfunc:  blend" << std::endl;
				break;
		}
	}
}

/* ---- static ----------------------------------------------------- */

void Material::init()
{
	con_print << "^BInitializing materials..." << std::endl;

	filesystem::IFileStream shaderlistfile("materials/shaderlist.txt");
	if (!shaderlistfile.is_open()) {
		con_warn << "Could not open " << shaderlistfile.name() << std::endl;
		return;
	}

	con_debug << "  " << shaderlistfile.name() << std::endl;

	char line[1024];
	while (shaderlistfile.getline(line, 1023)) {
		if ((line[0] == 0) || (line[0] == '#') || (line[0] == ';')) {
			continue;
			if ((line[0] == '/') && (line[1] == '/'))
				continue;
		} else {
			std::string s(line);
			aux::trim(s);
			load_shaderfile(s);
		}
	}

	shaderlistfile.close();
}

void Material::clear()
{
	con_print << "^BClearing materials..." << std::endl;

	for (Registry::iterator i = material_registry.begin(); i != material_registry.end(); ++i) {
		delete(*i).second;
	}

	material_registry.clear();
}

void Material::shutdown()
{
	clear();
}

void Material::load_shaderfile(const std::string &shadername)
{
	std::string shaderfilename("materials/");
	shaderfilename.append(shadername);
	shaderfilename.append(".shader");

	filesystem::IFileStream shaderfile(shaderfilename);
	if (!shaderfile.is_open()) {
		con_warn << "Could not open " << shaderfile.name() << std::endl;
		return;
	}

	int parselevel = 0;
	unsigned int linenumber = 0;
	char line[1024];
	unsigned int count = 0;
	float r, g, b, a, shine;
	
	Material *material = 0;
	Layer *layer = 0;

	while (shaderfile.getline(line, 1023)) {
		linenumber++;

		// read materials
		std::string s(line);
		aux::trim(s);

		// skip empty lines
		if (!s.size())
			continue;

		// skip comments
		if ((s[0] == '#') || (s[0] == ';') || ((s[0] == '/') && (s[1] == '/')))
			continue;

		std::istringstream linestream(s);
		std::string firstword;

		if (linestream >> firstword) {
			if (firstword.compare("//") == 0) {
				continue;

			} else if (firstword.compare("}") == 0){
				if (parselevel == 2) {
					// leaving layer definition
					layer = 0;
				} else if (parselevel == 1) {
					// leaving material definition
					material = 0;
				}
				parselevel--;

			} else if (firstword.compare("{") == 0) {
				parselevel++;

			} else if (parselevel == 0) {
				
				// add new material
				material = find(firstword);
				if (material) {
					con_warn << "Duplicate material '" << firstword << "'" << std::endl;
					material = 0;
					layer = 0;
				} else {
					material = new Material(firstword);
					material_registry[material->name()] = material;
					count++;
					
					layer = 0;
				}

			} else if ((parselevel == 1) && (material)) {
				
				// inside material definition
				
				aux::to_lowercase(firstword);
	
				if (firstword.compare("qer_editorimage") == 0) {
					// keyword qer_editorimage is ignored
					continue;
				} else if (firstword.compare("qer_trans") == 0) {
					// keyword qer_trans is ignored
					continue;
				} else if (firstword.compare("surfaceparm") == 0) {
					// keyword surfaceparm is ignored
					continue;
					
				} else if (firstword.compare("ignore") == 0) {
					material->set_flags(FlagIgnore);
				} else if (firstword.compare("clip") == 0) {
					material->set_flags(FlagClip);
				} else if (firstword.compare("origin") == 0) {
					material->set_flags(FlagOrigin);
				} else if (firstword.compare("decal") == 0) {
					material->set_flags(FlagDecal);
				} else if (firstword.compare("bounds") == 0) {
					material->set_flags(FlagBounds);
				} else {
					con_warn << shaderfile.name() << " unknown material key '" << firstword << "' at line " << linenumber << std::endl;
				}
				
			} else if ((parselevel == 2) && (material)) {
				
				// inside layer definition
				
				// create new layer if required
				if (!layer) {
					layer = new Layer();
					material->material_layers.push_back(layer);
				}
				
				aux::to_lowercase(firstword);
				
				if (firstword.compare("color") == 0) {
					if (linestream >> r >> g >> b) {
						if (math::max(r, math::max(g, b)) > 1.0f) {
							r /= 255.0f;
							g /= 255.0f;
							b /= 255.0f;
						}
						if (!(linestream >> a)) {
							a = 1.0f;
						}
						layer->set_color(math::Color(r, g, b, a));
						layer->set_rgbgen(Layer::RGBGenColor);
						// set specular too, preserving the old behavior
						layer->set_specular(math::Color(r, g, b, a));
					}
				} else if (firstword.compare("specular") == 0) {
					if (linestream >> r >> g >> b) {
						if (math::max(r, math::max(g, b)) > 1.0f) {
							r /= 255.0f;
							g /= 255.0f;
							b /= 255.0f;
						}
						if (!(linestream >> a)) {
							a = 1.0f;
						}
						layer->set_specular(math::Color(r, g, b, a));
					}
				} else if (firstword.compare("shininess") == 0) {
					if (linestream >> shine) {
						layer->set_shininess(shine);
					}
				} else if (firstword.compare("engine") == 0) {
					layer->set_rgbgen(Layer::RGBGenEngine);
				} else if (firstword.compare("entity") == 0) {
					layer->set_rgbgen(Layer::RGBGenPrimary);
				} else if (firstword.compare("entitysecond") == 0) {
					layer->set_rgbgen(Layer::RGBGenSecondary);
				} else if (firstword.compare("entitythird") == 0) {
					layer->set_rgbgen(Layer::RGBGenTertiary);
				} else if ((firstword.compare("bright") == 0) || (firstword.compare("fullbright") == 0)) {
					layer->set_fullbright(true);
				} else if (firstword.compare("environment") == 0) {
					layer->set_tcgen(Layer::TCGenEnvironment);
					layer->set_texmap(Layer::TexMapEnvironment);
				} else if (firstword.compare("blendfunc") == 0) {
					if (linestream >> firstword) {
						aux::to_lowercase(firstword);
						if (firstword.compare("none") == 0) {
							layer->set_blendfunc(Layer::BlendFuncNone);
						} else if (firstword.compare("add") == 0) {
							layer->set_blendfunc(Layer::BlendFuncAdd);
						} else if (firstword.compare("blend") == 0) {
							layer->set_blendfunc(Layer::BlendFuncBlend);
						} else {
							con_warn << shaderfile.name() << " unknown blend function '" << firstword << "' at line " << linenumber << std::endl;
						}
					} else {
						con_warn << shaderfile.name() << " missing blend function at line " << linenumber << std::endl;
					}
				} else if (firstword.compare("map") == 0) {
					
					if (linestream >> firstword) {
						aux::to_lowercase(firstword);
						if (firstword.compare("$environment") == 0) {
							layer->set_texmap(Layer::TexMapEnvironment);
						} else if (firstword.compare("$logo") == 0) {
							layer->set_texmap(Layer::TexMapLogo);
						} else {
							// regular texture from image file
							// FIXME strip extension from filename
							layer->set_texture(firstword);
							
							if (material_imageloaderfunc) {
								material_imageloaderfunc(layer);
								if ((layer->size().width() > 0) && (layer->size().height() > 0)) {
									material->material_size.assign(layer->size());
								}
							}
						}
					} else {
						con_warn << shaderfile.name() << " missing texture map at line " << linenumber << std::endl;
					}
				} else {
					con_warn << shaderfile.name() << " unknown layer key '" << firstword << "' at line " << linenumber << std::endl;
				}
					
			}
		}
	}

	con_debug << "  " << shaderfile.name() << " " << count << " materials " << std::endl;

	shaderfile.close();
}

void Material::list()
{
	for (Registry::iterator i = material_registry.begin(); i != material_registry.end(); ++i) {
		con_print << "  " << (*i).second->name() << std::endl;
	}
	con_print << material_registry.size() << " registered materials" << std::endl;
}

Material *Material::find(const std::string &name)
{
	std::string searchstr(name);
	aux::to_lowercase(searchstr);
	
	for (Registry::iterator i = material_registry.begin(); i != material_registry.end(); ++i) {
		if ((*i).first.compare(searchstr) == 0)
			return (*i).second;
	}
	return 0;
}

Material *Material::load(const std::string &name, const bool ui_texture)
{
	// check if the requested material already exist
	Material *material = find(name);
	if (material)
	{
		return material;
	}
	
	// create a new material
	material = new Material(name);
	
	// create a single layer
	Layer *layer = new Layer();
	
	// add a texture
	layer->set_texture(name);
	if (ui_texture) {
		layer->set_fullbright(true);
		layer->set_blendfunc(Layer::BlendFuncBlend);
	}	
	if (material_imageloaderfunc) {
		material_imageloaderfunc(layer);
		if ((layer->size().width() > 0) && (layer->size().height() > 0)) {
			material->material_size.assign(layer->size());
		}
	}
	
	// add the new layer to the material
	material->material_layers.push_back(layer);
	
	// add the new material to the registry
	material_registry[material->name()] = material;
	
	return material;
}

void Material::set_imageloader_func(ImageLoaderFuncPtr func)
{
	material_imageloaderfunc = func;
}

}