1 // materialmgr.cxx -- class to handle material properties
3 // Written by Curtis Olson, started May 1998.
5 // Copyright (C) 1998 Curtis L. Olson - curt@me.umn.edu
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 #ifdef SG_MATH_EXCEPTION_CLASH
39 #include <simgear/compiler.h>
40 #include <simgear/misc/exception.hxx>
45 #include <simgear/debug/logstream.hxx>
46 #include <simgear/misc/sg_path.hxx>
47 #include <simgear/misc/sgstream.hxx>
49 #include <Include/general.hxx>
50 #include <Main/globals.hxx>
51 #include <Main/fg_props.hxx>
52 #include <Scenery/tileentry.hxx>
59 // global material management class
60 FGMaterialLib material_lib;
64 FGMaterialLib::FGMaterialLib ( void ) {
69 // Load a library of material properties
70 bool FGMaterialLib::load( const string& mpath ) {
72 SGPropertyNode materials;
74 SG_LOG(SG_INPUT, SG_INFO, "Reading materials from " << mpath);
76 readProperties(mpath, &materials);
77 } catch (const sg_exception &ex) {
78 SG_LOG(SG_INPUT, SG_ALERT, "Error reading materials: " << ex.getMessage());
82 int nMaterials = materials.nChildren();
83 for (int i = 0; i < nMaterials; i++) {
84 const SGPropertyNode * node = materials.getChild(i);
85 if (string(node->getName()) == "material") {
86 FGNewMat * m = new FGNewMat(node);
88 vector<const SGPropertyNode *>names = node->getChildren("name");
89 for (unsigned int j = 0; j < names.size(); j++) {
90 string name = names[j]->getStringValue();
92 std::cerr << "Material " << name << endl;
94 SG_LOG( SG_TERRAIN, SG_INFO, " Loading material "
95 << names[j]->getStringValue());
98 SG_LOG(SG_INPUT, SG_ALERT,
99 "Skipping bad material entry " << node->getName());
103 // hard coded light state
104 ssgSimpleState *lights = new ssgSimpleState;
106 lights->disable( GL_TEXTURE_2D );
107 lights->enable( GL_CULL_FACE );
108 lights->enable( GL_COLOR_MATERIAL );
109 lights->setColourMaterial( GL_AMBIENT_AND_DIFFUSE );
110 lights->setMaterial( GL_EMISSION, 0, 0, 0, 1 );
111 lights->setMaterial( GL_SPECULAR, 0, 0, 0, 1 );
112 lights->enable( GL_BLEND );
113 lights->disable( GL_ALPHA_TEST );
114 lights->disable( GL_LIGHTING );
116 matlib["LIGHTS"] = new FGNewMat(lights);
122 // Load a library of material properties
123 bool FGMaterialLib::add_item ( const string &tex_path )
125 string material_name = tex_path;
126 int pos = tex_path.rfind( "/" );
127 material_name = material_name.substr( pos + 1 );
129 return add_item( material_name, tex_path );
133 // Load a library of material properties
134 bool FGMaterialLib::add_item ( const string &mat_name, const string &full_path )
136 int pos = full_path.rfind( "/" );
137 string tex_name = full_path.substr( pos + 1 );
138 string tex_path = full_path.substr( 0, pos );
140 SG_LOG( SG_TERRAIN, SG_INFO, " Loading material "
141 << mat_name << " (" << full_path << ")");
143 material_lib.matlib[mat_name] = new FGNewMat(full_path);
149 // Load a library of material properties
150 bool FGMaterialLib::add_item ( const string &mat_name, ssgSimpleState *state )
152 FGNewMat *m = new FGNewMat(state);
154 SG_LOG( SG_TERRAIN, SG_INFO, " Loading material given a premade "
155 << "ssgSimpleState = " << mat_name );
157 material_lib.matlib[mat_name] = m;
163 // find a material record by material name
164 FGNewMat *FGMaterialLib::find( const string& material ) {
165 FGNewMat *result = NULL;
166 material_map_iterator it = matlib.find( material );
177 FGMaterialLib::~FGMaterialLib ( void ) {
178 // Free up all the material entries first
179 for ( material_map_iterator it = begin(); it != end(); it++ ) {
180 FGNewMat *slot = it->second;
182 if ( slot->getRef() <= 0 ) {
189 // Set the step for all of the state selectors in the material slots
190 void FGMaterialLib::set_step ( int step )
192 // container::iterator it = begin();
193 for ( material_map_iterator it = begin(); it != end(); it++ ) {
194 const string &key = it->first;
195 SG_LOG( SG_GENERAL, SG_INFO,
196 "Updating material " << key << " to step " << step );
197 FGNewMat *slot = it->second;
198 slot->get_state()->selectStep(step);
203 // Get the step for the state selectors
204 int FGMaterialLib::get_step ()
206 material_map_iterator it = begin();
207 return it->second->get_state()->getSelectStep();
211 // Load one pending "deferred" texture. Return true if a texture
212 // loaded successfully, false if no pending, or error.
213 void FGMaterialLib::load_next_deferred() {
214 // container::iterator it = begin();
215 for ( material_map_iterator it = begin(); it != end(); it++ ) {
216 /* we don't need the key, but here's how we'd get it if we wanted it. */
217 // const string &key = it->first;
218 FGNewMat *slot = it->second;
219 if (slot->load_texture())