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>
37 #include <osg/CullFace>
38 #include <osg/Material>
39 #include <osg/ShadeModel>
40 #include <osg/StateSet>
42 #include <osg/Texture2D>
43 #include <osgDB/ReadFile>
44 #include <osgDB/FileUtils>
46 #include <simgear/debug/logstream.hxx>
47 #include <simgear/misc/sg_path.hxx>
48 #include <simgear/misc/sgstream.hxx>
50 #include <simgear/scene/model/model.hxx>
51 #include <simgear/scene/util/StateAttributeFactory.hxx>
54 #include "Technique.hxx"
58 using namespace simgear;
61 ////////////////////////////////////////////////////////////////////////
62 // Constructors and destructor.
63 ////////////////////////////////////////////////////////////////////////
65 SGMaterial::_internal_state::_internal_state(osg::StateSet *s,
66 const std::string &t, bool l ) :
67 state(s), texture_path(t), texture_loaded(l)
71 SGMaterial::SGMaterial( const string &fg_root, const SGPropertyNode *props )
74 read_properties( fg_root, props );
78 SGMaterial::SGMaterial( const string &texpath )
82 _internal_state st( NULL, texpath, false );
83 _status.push_back( st );
88 SGMaterial::SGMaterial( osg::StateSet *s )
94 SGMaterial::~SGMaterial (void)
99 ////////////////////////////////////////////////////////////////////////
101 ////////////////////////////////////////////////////////////////////////
104 SGMaterial::read_properties( const string &fg_root, const SGPropertyNode *props)
106 // Gather the path(s) to the texture(s)
107 vector<SGPropertyNode_ptr> textures = props->getChildren("texture");
108 for (unsigned int i = 0; i < textures.size(); i++)
110 string tname = textures[i]->getStringValue();
112 tname = "unknown.rgb";
115 SGPath tpath( fg_root );
116 tpath.append("Textures.high");
118 if ( !osgDB::fileExists(tpath.str()) ) {
119 tpath = SGPath( fg_root );
120 tpath.append("Textures");
124 if ( osgDB::fileExists(tpath.str()) ) {
125 _internal_state st( NULL, tpath.str(), false );
126 _status.push_back( st );
130 if (textures.size() == 0) {
131 string tname = "unknown.rgb";
132 SGPath tpath( fg_root );
133 tpath.append("Textures");
134 tpath.append("Terrain");
136 _internal_state st( NULL, tpath.str(), true );
137 _status.push_back( st );
140 xsize = props->getDoubleValue("xsize", 0.0);
141 ysize = props->getDoubleValue("ysize", 0.0);
142 wrapu = props->getBoolValue("wrapu", true);
143 wrapv = props->getBoolValue("wrapv", true);
144 mipmap = props->getBoolValue("mipmap", true);
145 light_coverage = props->getDoubleValue("light-coverage", 0.0);
146 tree_coverage = props->getDoubleValue("tree-coverage", 0.0);
147 tree_height = props->getDoubleValue("tree-height-m", 0.0);
148 tree_width = props->getDoubleValue("tree-width-m", 0.0);
149 tree_range = props->getDoubleValue("tree-range-m", 0.0);
150 tree_varieties = props->getIntValue("tree-varieties", 1);
152 SGPath tpath( fg_root );
153 tpath.append(props->getStringValue("tree-texture"));
154 tree_texture = tpath.str();
156 // surface values for use with ground reactions
157 solid = props->getBoolValue("solid", true);
158 friction_factor = props->getDoubleValue("friction-factor", 1.0);
159 rolling_friction = props->getDoubleValue("rolling-friction", 0.02);
160 bumpiness = props->getDoubleValue("bumpiness", 0.0);
161 load_resistance = props->getDoubleValue("load-resistance", 1e30);
163 // Taken from default values as used in ac3d
164 ambient[0] = props->getDoubleValue("ambient/r", 0.2);
165 ambient[1] = props->getDoubleValue("ambient/g", 0.2);
166 ambient[2] = props->getDoubleValue("ambient/b", 0.2);
167 ambient[3] = props->getDoubleValue("ambient/a", 1.0);
169 diffuse[0] = props->getDoubleValue("diffuse/r", 0.8);
170 diffuse[1] = props->getDoubleValue("diffuse/g", 0.8);
171 diffuse[2] = props->getDoubleValue("diffuse/b", 0.8);
172 diffuse[3] = props->getDoubleValue("diffuse/a", 1.0);
174 specular[0] = props->getDoubleValue("specular/r", 0.0);
175 specular[1] = props->getDoubleValue("specular/g", 0.0);
176 specular[2] = props->getDoubleValue("specular/b", 0.0);
177 specular[3] = props->getDoubleValue("specular/a", 1.0);
179 emission[0] = props->getDoubleValue("emissive/r", 0.0);
180 emission[1] = props->getDoubleValue("emissive/g", 0.0);
181 emission[2] = props->getDoubleValue("emissive/b", 0.0);
182 emission[3] = props->getDoubleValue("emissive/a", 1.0);
184 shininess = props->getDoubleValue("shininess", 1.0);
186 vector<SGPropertyNode_ptr> object_group_nodes =
187 ((SGPropertyNode *)props)->getChildren("object-group");
188 for (unsigned int i = 0; i < object_group_nodes.size(); i++)
189 object_groups.push_back(new SGMatModelGroup(object_group_nodes[i]));
191 // read glyph table for taxi-/runway-signs
192 vector<SGPropertyNode_ptr> glyph_nodes = props->getChildren("glyph");
193 for (unsigned int i = 0; i < glyph_nodes.size(); i++) {
194 const char *name = glyph_nodes[i]->getStringValue("name");
196 glyphs[name] = new SGMaterialGlyph(glyph_nodes[i]);
202 ////////////////////////////////////////////////////////////////////////
204 ////////////////////////////////////////////////////////////////////////
217 light_coverage = 0.0;
221 rolling_friction = 0.02;
223 load_resistance = 1e30;
226 for (int i = 0; i < 4; i++) {
227 ambient[i] = (i < 3) ? 0.2 : 1.0;
228 specular[i] = (i < 3) ? 0.0 : 1.0;
229 diffuse[i] = (i < 3) ? 0.8 : 1.0;
230 emission[i] = (i < 3) ? 0.0 : 1.0;
235 SGMaterial::get_state (int n)
237 if (_status.size() == 0) {
238 SG_LOG( SG_GENERAL, SG_WARN, "No state available.");
242 int i = n >= 0 ? n : _current_ptr;
244 if(!_status[i].texture_loaded)
246 assignTexture(_status[i].state.get(), _status[i].texture_path,
247 wrapu, wrapv, mipmap);
248 _status[i].texture_loaded = true;
250 osg::StateSet *st = _status[i].state.get();
253 if (_current_ptr >= _status.size())
259 Effect* SGMaterial::get_effect(int n)
261 if (_status.size() == 0) {
262 SG_LOG( SG_GENERAL, SG_WARN, "No effect available.");
265 int i = n >= 0 ? n : _current_ptr;
266 if(!_status[i].texture_loaded) {
267 assignTexture(_status[i].state.get(), _status[i].texture_path,
268 wrapu, wrapv, mipmap);
269 _status[i].texture_loaded = true;
271 // XXX This business of returning a "random" alternate texture is
272 // really bogus. It means that the appearance of the terrain
273 // depends on the order in which it is paged in!
274 _current_ptr = (_current_ptr + 1) % _status.size();
275 return _status[i].effect.get();
279 SGMaterial::build_state( bool defer_tex_load )
281 StateAttributeFactory *attrFact = StateAttributeFactory::instance();
282 SGMaterialUserData* user = new SGMaterialUserData(this);
283 for (unsigned int i = 0; i < _status.size(); i++)
285 Pass *pass = new Pass;
286 pass->setUserData(user);
288 // Set up the textured state
289 pass->setAttribute(attrFact->getSmoothShadeModel());
290 pass->setAttributeAndModes(attrFact->getCullFaceBack());
292 pass->setMode(GL_LIGHTING, osg::StateAttribute::ON);
294 _status[i].texture_loaded = false;
296 osg::Material* material = new osg::Material;
297 material->setColorMode(osg::Material::AMBIENT_AND_DIFFUSE);
298 material->setAmbient(osg::Material::FRONT_AND_BACK, ambient.osg());
299 material->setDiffuse(osg::Material::FRONT_AND_BACK, diffuse.osg());
300 material->setSpecular(osg::Material::FRONT_AND_BACK, specular.osg());
301 material->setEmission(osg::Material::FRONT_AND_BACK, emission.osg());
302 material->setShininess(osg::Material::FRONT_AND_BACK, shininess );
303 pass->setAttribute(material);
305 if (ambient[3] < 1 || diffuse[3] < 1 ||
306 specular[3] < 1 || emission[3] < 1) {
307 pass->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
308 pass->setMode(GL_BLEND, osg::StateAttribute::ON);
309 pass->setMode(GL_ALPHA_TEST, osg::StateAttribute::ON);
311 pass->setRenderingHint(osg::StateSet::OPAQUE_BIN);
312 pass->setMode(GL_BLEND, osg::StateAttribute::OFF);
313 pass->setMode(GL_ALPHA_TEST, osg::StateAttribute::OFF);
316 _status[i].state = pass;
317 Technique* tniq = new Technique();
318 tniq->setGLExtensionsPred(1.1, std::vector<std::string>());
319 tniq->passes.push_back(pass);
320 Effect* effect = new Effect;
321 effect->techniques.push_back(tniq);
322 effect->setUserData(user);
323 _status[i].effect = effect;
328 void SGMaterial::set_state( osg::StateSet *s )
330 _status.push_back( _internal_state( s, "", true ) );
333 void SGMaterial::assignTexture( osg::StateSet *state, const std::string &fname,
334 bool _wrapu, bool _wrapv, bool _mipmap )
336 osg::Texture2D* texture = SGLoadTexture2D(fname, 0, _wrapu, _wrapv,
338 texture->setMaxAnisotropy( SGGetTextureFilter());
339 state->setTextureAttributeAndModes(0, texture);
341 StateAttributeFactory *attrFact = StateAttributeFactory::instance();
342 state->setTextureAttributeAndModes(0, attrFact->getStandardTexEnv());
345 SGMaterialGlyph* SGMaterial::get_glyph (const string& name) const
347 map<string, SGSharedPtr<SGMaterialGlyph> >::const_iterator it;
348 it = glyphs.find(name);
349 if (it == glyphs.end())
356 ////////////////////////////////////////////////////////////////////////
358 ////////////////////////////////////////////////////////////////////////
360 SGMaterialGlyph::SGMaterialGlyph(SGPropertyNode *p) :
361 _left(p->getDoubleValue("left", 0.0)),
362 _right(p->getDoubleValue("right", 1.0))
367 SGSetTextureFilter( int max) {
368 SGSceneFeatures::instance()->setTextureFilter( max);
372 SGGetTextureFilter() {
373 return SGSceneFeatures::instance()->getTextureFilter();