From c8a6ff1c0693ca1bd9025dcbdd7c22e3d193f8dd Mon Sep 17 00:00:00 2001 From: Stijn Buys Date: Sun, 8 Feb 2009 13:13:11 +0000 Subject: materials registry, read shaderlist.txt --- src/model/material.cc | 153 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 153 insertions(+) create mode 100644 src/model/material.cc (limited to 'src/model/material.cc') 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 + +#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; +} + +} -- cgit v1.2.3