1 // mat.cxx -- class to handle material properties
3 // Written by Curtis Olson, started May 1998.
5 // Copyright (C) 1998 - 2000 Curtis L. Olson - http://www.flightgear.org/~curt
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25 # include <simgear_config.h>
28 #include <simgear/compiler.h>
35 #include <osg/CullFace>
36 #include <osg/Material>
37 #include <osg/ShadeModel>
38 #include <osg/StateSet>
40 #include <osg/Texture2D>
41 #include <osgDB/ReadFile>
42 #include <osgDB/FileUtils>
44 #include <simgear/debug/logstream.hxx>
45 #include <simgear/misc/sg_path.hxx>
46 #include <simgear/misc/sgstream.hxx>
48 #include <simgear/scene/model/model.hxx>
49 #include <simgear/scene/util/StateAttributeFactory.hxx>
52 #include "Technique.hxx"
56 using namespace simgear;
59 ////////////////////////////////////////////////////////////////////////
60 // Constructors and destructor.
61 ////////////////////////////////////////////////////////////////////////
63 SGMaterial::_internal_state::_internal_state(osg::StateSet *s,
64 const std::string &t, bool l ) :
65 state(s), texture_path(t), texture_loaded(l)
69 SGMaterial::SGMaterial( const string &fg_root, const SGPropertyNode *props )
72 read_properties( fg_root, props );
76 SGMaterial::SGMaterial( const string &texpath )
80 _internal_state st( NULL, texpath, false );
81 _status.push_back( st );
86 SGMaterial::SGMaterial( osg::StateSet *s )
92 SGMaterial::~SGMaterial (void)
97 ////////////////////////////////////////////////////////////////////////
99 ////////////////////////////////////////////////////////////////////////
102 SGMaterial::read_properties( const string &fg_root, const SGPropertyNode *props)
104 // Gather the path(s) to the texture(s)
105 vector<SGPropertyNode_ptr> textures = props->getChildren("texture");
106 for (unsigned int i = 0; i < textures.size(); i++)
108 string tname = textures[i]->getStringValue();
110 tname = "unknown.rgb";
113 SGPath tpath( fg_root );
114 tpath.append("Textures.high");
116 if ( !osgDB::fileExists(tpath.str()) ) {
117 tpath = SGPath( fg_root );
118 tpath.append("Textures");
122 if ( osgDB::fileExists(tpath.str()) ) {
123 _internal_state st( NULL, tpath.str(), false );
124 _status.push_back( st );
128 if (textures.size() == 0) {
129 string tname = "unknown.rgb";
130 SGPath tpath( fg_root );
131 tpath.append("Textures");
132 tpath.append("Terrain");
134 _internal_state st( NULL, tpath.str(), true );
135 _status.push_back( st );
138 xsize = props->getDoubleValue("xsize", 0.0);
139 ysize = props->getDoubleValue("ysize", 0.0);
140 wrapu = props->getBoolValue("wrapu", true);
141 wrapv = props->getBoolValue("wrapv", true);
142 mipmap = props->getBoolValue("mipmap", true);
143 light_coverage = props->getDoubleValue("light-coverage", 0.0);
144 tree_coverage = props->getDoubleValue("tree-coverage", 0.0);
145 tree_height = props->getDoubleValue("tree-height-m", 0.0);
146 tree_width = props->getDoubleValue("tree-width-m", 0.0);
147 tree_range = props->getDoubleValue("tree-range-m", 0.0);
148 tree_varieties = props->getIntValue("tree-varieties", 1);
150 SGPath tpath( fg_root );
151 tpath.append(props->getStringValue("tree-texture"));
152 tree_texture = tpath.str();
154 // surface values for use with ground reactions
155 solid = props->getBoolValue("solid", true);
156 friction_factor = props->getDoubleValue("friction-factor", 1.0);
157 rolling_friction = props->getDoubleValue("rolling-friction", 0.02);
158 bumpiness = props->getDoubleValue("bumpiness", 0.0);
159 load_resistance = props->getDoubleValue("load-resistance", 1e30);
161 // Taken from default values as used in ac3d
162 ambient[0] = props->getDoubleValue("ambient/r", 0.2);
163 ambient[1] = props->getDoubleValue("ambient/g", 0.2);
164 ambient[2] = props->getDoubleValue("ambient/b", 0.2);
165 ambient[3] = props->getDoubleValue("ambient/a", 1.0);
167 diffuse[0] = props->getDoubleValue("diffuse/r", 0.8);
168 diffuse[1] = props->getDoubleValue("diffuse/g", 0.8);
169 diffuse[2] = props->getDoubleValue("diffuse/b", 0.8);
170 diffuse[3] = props->getDoubleValue("diffuse/a", 1.0);
172 specular[0] = props->getDoubleValue("specular/r", 0.0);
173 specular[1] = props->getDoubleValue("specular/g", 0.0);
174 specular[2] = props->getDoubleValue("specular/b", 0.0);
175 specular[3] = props->getDoubleValue("specular/a", 1.0);
177 emission[0] = props->getDoubleValue("emissive/r", 0.0);
178 emission[1] = props->getDoubleValue("emissive/g", 0.0);
179 emission[2] = props->getDoubleValue("emissive/b", 0.0);
180 emission[3] = props->getDoubleValue("emissive/a", 1.0);
182 shininess = props->getDoubleValue("shininess", 1.0);
184 vector<SGPropertyNode_ptr> object_group_nodes =
185 ((SGPropertyNode *)props)->getChildren("object-group");
186 for (unsigned int i = 0; i < object_group_nodes.size(); i++)
187 object_groups.push_back(new SGMatModelGroup(object_group_nodes[i]));
189 // read glyph table for taxi-/runway-signs
190 vector<SGPropertyNode_ptr> glyph_nodes = props->getChildren("glyph");
191 for (unsigned int i = 0; i < glyph_nodes.size(); i++) {
192 const char *name = glyph_nodes[i]->getStringValue("name");
194 glyphs[name] = new SGMaterialGlyph(glyph_nodes[i]);
200 ////////////////////////////////////////////////////////////////////////
202 ////////////////////////////////////////////////////////////////////////
215 light_coverage = 0.0;
219 rolling_friction = 0.02;
221 load_resistance = 1e30;
224 for (int i = 0; i < 4; i++) {
225 ambient[i] = (i < 3) ? 0.2 : 1.0;
226 specular[i] = (i < 3) ? 0.0 : 1.0;
227 diffuse[i] = (i < 3) ? 0.8 : 1.0;
228 emission[i] = (i < 3) ? 0.0 : 1.0;
233 SGMaterial::get_state (int n)
235 if (_status.size() == 0) {
236 SG_LOG( SG_GENERAL, SG_WARN, "No state available.");
240 int i = n >= 0 ? n : _current_ptr;
242 if(!_status[i].texture_loaded)
244 assignTexture(_status[i].state.get(), _status[i].texture_path,
245 wrapu, wrapv, mipmap);
246 _status[i].texture_loaded = true;
248 osg::StateSet *st = _status[i].state.get();
251 if (_current_ptr >= _status.size())
257 Effect* SGMaterial::get_effect(int n)
259 if (_status.size() == 0) {
260 SG_LOG( SG_GENERAL, SG_WARN, "No effect available.");
263 int i = n >= 0 ? n : _current_ptr;
264 if(!_status[i].texture_loaded) {
265 assignTexture(_status[i].state.get(), _status[i].texture_path,
266 wrapu, wrapv, mipmap);
267 _status[i].texture_loaded = true;
269 // XXX This business of returning a "random" alternate texture is
270 // really bogus. It means that the appearance of the terrain
271 // depends on the order in which it is paged in!
272 _current_ptr = (_current_ptr + 1) % _status.size();
273 return _status[i].effect.get();
277 SGMaterial::build_state( bool defer_tex_load )
279 StateAttributeFactory *attrFact = StateAttributeFactory::instance();
280 SGMaterialUserData* user = new SGMaterialUserData(this);
281 for (unsigned int i = 0; i < _status.size(); i++)
283 osg::StateSet *stateSet = new osg::StateSet;
284 stateSet->setUserData(user);
286 // Set up the textured state
287 stateSet->setAttribute(attrFact->getSmoothShadeModel());
288 stateSet->setAttributeAndModes(attrFact->getCullFaceBack());
290 stateSet->setMode(GL_LIGHTING, osg::StateAttribute::ON);
292 _status[i].texture_loaded = false;
294 osg::Material* material = new osg::Material;
295 material->setColorMode(osg::Material::AMBIENT_AND_DIFFUSE);
296 material->setAmbient(osg::Material::FRONT_AND_BACK, ambient.osg());
297 material->setDiffuse(osg::Material::FRONT_AND_BACK, diffuse.osg());
298 material->setSpecular(osg::Material::FRONT_AND_BACK, specular.osg());
299 material->setEmission(osg::Material::FRONT_AND_BACK, emission.osg());
300 material->setShininess(osg::Material::FRONT_AND_BACK, shininess );
301 stateSet->setAttribute(material);
303 if (ambient[3] < 1 || diffuse[3] < 1 ||
304 specular[3] < 1 || emission[3] < 1) {
305 stateSet->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
306 stateSet->setMode(GL_BLEND, osg::StateAttribute::ON);
307 stateSet->setMode(GL_ALPHA_TEST, osg::StateAttribute::ON);
309 stateSet->setRenderingHint(osg::StateSet::OPAQUE_BIN);
310 stateSet->setMode(GL_BLEND, osg::StateAttribute::OFF);
311 stateSet->setMode(GL_ALPHA_TEST, osg::StateAttribute::OFF);
314 _status[i].state = stateSet;
315 Pass* pass = new Pass;
316 pass->setStateSet(_status[i].state.get());
317 Technique* tniq = new Technique(true);
318 tniq->passes.push_back(pass);
319 Effect* effect = new Effect;
320 effect->techniques.push_back(tniq);
321 effect->setUserData(user);
322 _status[i].effect = effect;
327 void SGMaterial::set_state( osg::StateSet *s )
329 _status.push_back( _internal_state( s, "", true ) );
332 void SGMaterial::assignTexture( osg::StateSet *state, const std::string &fname,
333 bool _wrapu, bool _wrapv, bool _mipmap )
335 osg::Texture2D* texture = SGLoadTexture2D(fname, 0, _wrapu, _wrapv,
337 texture->setMaxAnisotropy( SGGetTextureFilter());
338 state->setTextureAttributeAndModes(0, texture);
340 StateAttributeFactory *attrFact = StateAttributeFactory::instance();
341 state->setTextureAttributeAndModes(0, attrFact->getStandardTexEnv());
344 SGMaterialGlyph* SGMaterial::get_glyph (const string& name) const
346 map<string, SGSharedPtr<SGMaterialGlyph> >::const_iterator it;
347 it = glyphs.find(name);
348 if (it == glyphs.end())
355 ////////////////////////////////////////////////////////////////////////
357 ////////////////////////////////////////////////////////////////////////
359 SGMaterialGlyph::SGMaterialGlyph(SGPropertyNode *p) :
360 _left(p->getDoubleValue("left", 0.0)),
361 _right(p->getDoubleValue("right", 1.0))
366 SGSetTextureFilter( int max) {
367 SGSceneFeatures::instance()->setTextureFilter( max);
371 SGGetTextureFilter() {
372 return SGSceneFeatures::instance()->getTextureFilter();