Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStijn Buys <ingar@osirion.org>2009-02-08 13:13:11 +0000
committerStijn Buys <ingar@osirion.org>2009-02-08 13:13:11 +0000
commitc8a6ff1c0693ca1bd9025dcbdd7c22e3d193f8dd (patch)
tree67b9568002fa77ea34aa993362fd53636f70fca8 /src/model/material.cc
parent43f7733dfdd8700430a238d230ed573c12e72c87 (diff)
materials registry, read shaderlist.txt
Diffstat (limited to 'src/model/material.cc')
-rw-r--r--src/model/material.cc153
1 files changed, 153 insertions, 0 deletions
diff --git a/src/model/material.cc b/src/model/material.cc
new file mode 100644
index 0000000..0167f7b
--- /dev/null
+++ b/src/model/material.cc
@@ -0,0 +1,153 @@
+/*
+ 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 "model/material.h"
+#include "sys/sys.h"
+
+namespace model
+{
+
+Material::Material(const std::string &name) : material_name(name)
+{
+}
+
+Material::~Material()
+{
+}
+
+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;
+ char line[1024];
+ unsigned int count = 0;
+
+ while (shaderfile.getline(line, 1023)) {
+ // 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 *material = new Material(firstword);
+ add(material);
+ count++;
+ }
+
+ } else if (parselevel == 1) {
+
+ }
+ }
+ }
+
+ 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;
+}
+
+}