Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
blob: 68e80a8d236548c6b0d33ef89c459bb47b4c8098 (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
/*
   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 "auxiliary/functions.h"
#include "filesystem/filestream.h"
#include "math/functions.h"
#include "model/material.h"
#include "sys/sys.h"

namespace model
{

Material::Material(const std::string &name) : material_name(name), material_color(1.0f)
{
	aux::to_lowercase(material_name);
	material_flags = 0;
}

Material::~Material()
{
}

void Material::set_color(const math::Color &color)
{
	material_color.assign(color);
	material_color.a = 1.0f;
}

Material::Registry Material::material_registry;

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

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

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

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

	shaderlist.close();
}

void Material::add(Material *material)
{
	if (!find(material->name())) {
		material_registry[material->name()] = material;
	}
}

void Material::load_shader(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;
	Material *material = 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) {
				parselevel--;

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

			} else if ((firstword.size()) && (parselevel == 0) ) {

				if (firstword.compare(0, 9, "textures/") == 0) {
					firstword.erase(0, 9);
					material = find(firstword);
					if (material) {
						con_warn << "Duplicate material '" << firstword << "'" << std::endl;
					} else {
						material = new Material(firstword);
						add(material);
						con_debug << "  " << firstword << std::endl;
					}
					count++;
				}

			} else if ((parselevel == 1) && (material)) {
				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;
						}						
						material->set_color(math::Color(r, g, b, 1.0f));
					}
				} else if (firstword.compare("engine") == 0) {
					material->set_flags(Engine);
				} else if (firstword.compare("bright") == 0) {
					material->set_flags(Bright);
				} else if (firstword.compare("environment") == 0) {
					material->set_flags(Environment);
				} else if (firstword.compare("entity") == 0) {
					material->set_flags(Primary);
				} else if (firstword.compare("entitysecond") == 0) {
					material->set_flags(Secondary);
				} else if (firstword.compare("entitythird") == 0) {
					material->set_flags(Tertiary);		
				} else if (firstword.compare("qer_editorimage") == 0) {
					continue;
				} else if (firstword.compare("qer_trans") == 0) {
					continue;
				} else {
					con_warn << shaderfile.name() << " unknown key '" << firstword 
						<< "' at line " << linenumber << std::endl;
				}
			}
		}
	}

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

	shaderfile.close();
}

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

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::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)
{
	for (Registry::iterator i = material_registry.begin(); i != material_registry.end(); ++i) {
		if ((*i).first.compare(name) == 0)
			return (*i).second;
	}
	return 0;
}

}