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;
234 Effect* SGMaterial::get_effect(int n)
236 if (_status.size() == 0) {
237 SG_LOG( SG_GENERAL, SG_WARN, "No effect available.");
240 int i = n >= 0 ? n : _current_ptr;
241 if(!_status[i].texture_loaded) {
242 assignTexture(_status[i].state.get(), _status[i].texture_path,
243 wrapu, wrapv, mipmap);
244 _status[i].texture_loaded = true;
246 // XXX This business of returning a "random" alternate texture is
247 // really bogus. It means that the appearance of the terrain
248 // depends on the order in which it is paged in!
249 _current_ptr = (_current_ptr + 1) % _status.size();
250 return _status[i].effect.get();
254 SGMaterial::build_state( bool defer_tex_load )
256 StateAttributeFactory *attrFact = StateAttributeFactory::instance();
257 SGMaterialUserData* user = new SGMaterialUserData(this);
258 for (unsigned int i = 0; i < _status.size(); i++)
260 Pass *pass = new Pass;
261 pass->setUserData(user);
263 // Set up the textured state
264 pass->setAttribute(attrFact->getSmoothShadeModel());
265 pass->setAttributeAndModes(attrFact->getCullFaceBack());
267 pass->setMode(GL_LIGHTING, osg::StateAttribute::ON);
269 _status[i].texture_loaded = false;
271 osg::Material* material = new osg::Material;
272 material->setColorMode(osg::Material::AMBIENT_AND_DIFFUSE);
273 material->setAmbient(osg::Material::FRONT_AND_BACK, ambient.osg());
274 material->setDiffuse(osg::Material::FRONT_AND_BACK, diffuse.osg());
275 material->setSpecular(osg::Material::FRONT_AND_BACK, specular.osg());
276 material->setEmission(osg::Material::FRONT_AND_BACK, emission.osg());
277 material->setShininess(osg::Material::FRONT_AND_BACK, shininess );
278 pass->setAttribute(material);
280 if (ambient[3] < 1 || diffuse[3] < 1 ||
281 specular[3] < 1 || emission[3] < 1) {
282 pass->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
283 pass->setMode(GL_BLEND, osg::StateAttribute::ON);
284 pass->setMode(GL_ALPHA_TEST, osg::StateAttribute::ON);
286 pass->setRenderingHint(osg::StateSet::OPAQUE_BIN);
287 pass->setMode(GL_BLEND, osg::StateAttribute::OFF);
288 pass->setMode(GL_ALPHA_TEST, osg::StateAttribute::OFF);
291 _status[i].state = pass;
292 Technique* tniq = new Technique(true);
293 tniq->passes.push_back(pass);
294 Effect* effect = new Effect;
295 effect->techniques.push_back(tniq);
296 effect->setUserData(user);
297 _status[i].effect = effect;
302 void SGMaterial::set_state( osg::StateSet *s )
304 _status.push_back( _internal_state( s, "", true ) );
307 void SGMaterial::assignTexture( osg::StateSet *state, const std::string &fname,
308 bool _wrapu, bool _wrapv, bool _mipmap )
310 osg::Texture2D* texture = SGLoadTexture2D(fname, 0, _wrapu, _wrapv,
312 texture->setMaxAnisotropy( SGGetTextureFilter());
313 state->setTextureAttributeAndModes(0, texture);
315 StateAttributeFactory *attrFact = StateAttributeFactory::instance();
316 state->setTextureAttributeAndModes(0, attrFact->getStandardTexEnv());
319 SGMaterialGlyph* SGMaterial::get_glyph (const string& name) const
321 map<string, SGSharedPtr<SGMaterialGlyph> >::const_iterator it;
322 it = glyphs.find(name);
323 if (it == glyphs.end())
330 ////////////////////////////////////////////////////////////////////////
332 ////////////////////////////////////////////////////////////////////////
334 SGMaterialGlyph::SGMaterialGlyph(SGPropertyNode *p) :
335 _left(p->getDoubleValue("left", 0.0)),
336 _right(p->getDoubleValue("right", 1.0))
341 SGSetTextureFilter( int max) {
342 SGSceneFeatures::instance()->setTextureFilter( max);
346 SGGetTextureFilter() {
347 return SGSceneFeatures::instance()->getTextureFilter();