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>
36 #ifdef SG_MATH_EXCEPTION_CLASH
40 #include <osg/CullFace>
41 #include <osg/Material>
42 #include <osg/ShadeModel>
44 #include <osg/Texture2D>
45 #include <osgDB/ReadFile>
47 #include <simgear/debug/logstream.hxx>
48 #include <simgear/misc/sg_path.hxx>
49 #include <simgear/misc/sgstream.hxx>
51 #include <simgear/scene/model/model.hxx>
56 ////////////////////////////////////////////////////////////////////////
57 // Constructors and destructor.
58 ////////////////////////////////////////////////////////////////////////
61 SGMaterial::SGMaterial( const string &fg_root, const SGPropertyNode *props, const char *season )
64 read_properties( fg_root, props, season );
68 SGMaterial::SGMaterial( const string &texpath )
72 _internal_state st( NULL, texpath, false );
73 _status.push_back( st );
78 SGMaterial::SGMaterial( osg::StateSet *s )
84 SGMaterial::~SGMaterial (void)
90 ////////////////////////////////////////////////////////////////////////
92 ////////////////////////////////////////////////////////////////////////
95 SGMaterial::read_properties( const string &fg_root, const SGPropertyNode * props, const char *season )
97 // Gather the path(s) to the texture(s)
98 vector<SGPropertyNode_ptr> textures = props->getChildren("texture");
99 for (unsigned int i = 0; i < textures.size(); i++)
101 string tname = textures[i]->getStringValue();
102 string otname = tname;
103 if (season && strncmp(season, "summer", 6))
105 if (tname.substr(0,7) == "Terrain")
106 tname.insert(7,"."+string(season));
110 tname = "unknown.rgb";
113 SGPath tpath( fg_root );
114 tpath.append("Textures.high");
116 if ( !ulFileExists(tpath.c_str()) ) {
117 tpath = SGPath( fg_root );
118 tpath.append("Textures");
122 if ( ulFileExists(tpath.c_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);
149 // surface values for use with ground reactions
150 solid = props->getBoolValue("solid", true);
151 friction_factor = props->getDoubleValue("friction-factor", 1.0);
152 rolling_friction = props->getDoubleValue("rolling-friction", 0.02);
153 bumpiness = props->getDoubleValue("bumpiness", 0.0);
154 load_resistance = props->getDoubleValue("load-resistance", 1e30);
156 // Taken from default values as used in ac3d
157 ambient[0] = props->getDoubleValue("ambient/r", 0.2);
158 ambient[1] = props->getDoubleValue("ambient/g", 0.2);
159 ambient[2] = props->getDoubleValue("ambient/b", 0.2);
160 ambient[3] = props->getDoubleValue("ambient/a", 1.0);
162 diffuse[0] = props->getDoubleValue("diffuse/r", 0.8);
163 diffuse[1] = props->getDoubleValue("diffuse/g", 0.8);
164 diffuse[2] = props->getDoubleValue("diffuse/b", 0.8);
165 diffuse[3] = props->getDoubleValue("diffuse/a", 1.0);
167 specular[0] = props->getDoubleValue("specular/r", 0.0);
168 specular[1] = props->getDoubleValue("specular/g", 0.0);
169 specular[2] = props->getDoubleValue("specular/b", 0.0);
170 specular[3] = props->getDoubleValue("specular/a", 1.0);
172 emission[0] = props->getDoubleValue("emissive/r", 0.0);
173 emission[1] = props->getDoubleValue("emissive/g", 0.0);
174 emission[2] = props->getDoubleValue("emissive/b", 0.0);
175 emission[3] = props->getDoubleValue("emissive/a", 1.0);
177 shininess = props->getDoubleValue("shininess", 1.0);
179 vector<SGPropertyNode_ptr> object_group_nodes =
180 ((SGPropertyNode *)props)->getChildren("object-group");
181 for (unsigned int i = 0; i < object_group_nodes.size(); i++)
182 object_groups.push_back(new SGMatModelGroup(object_group_nodes[i]));
184 vector<SGPropertyNode_ptr> tree_texture_nodes =
185 ((SGPropertyNode *)props)->getChildren("tree-texture");
186 for (unsigned int i = 0; i < tree_texture_nodes.size(); i++) {
187 SGPath tpath( fg_root );
188 tpath.append(tree_texture_nodes[i]->getStringValue());
189 tree_textures.push_back(tpath.str());
192 // read glyph table for taxi-/runway-signs
193 vector<SGPropertyNode_ptr> glyph_nodes = props->getChildren("glyph");
194 for (unsigned int i = 0; i < glyph_nodes.size(); i++) {
195 const char *name = glyph_nodes[i]->getStringValue("name");
197 glyphs[name] = new SGMaterialGlyph(glyph_nodes[i]);
203 ////////////////////////////////////////////////////////////////////////
205 ////////////////////////////////////////////////////////////////////////
218 light_coverage = 0.0;
222 rolling_friction = 0.02;
224 load_resistance = 1e30;
227 for (int i = 0; i < 4; i++) {
228 ambient[i] = (i < 3) ? 0.2 : 1.0;
229 specular[i] = (i < 3) ? 0.0 : 1.0;
230 diffuse[i] = (i < 3) ? 0.8 : 1.0;
231 emission[i] = (i < 3) ? 0.0 : 1.0;
236 SGMaterial::get_state (int n)
238 if (_status.size() == 0) {
239 SG_LOG( SG_GENERAL, SG_WARN, "No state available.");
243 int i = n >= 0 ? n : _current_ptr;
245 if(!_status[i].texture_loaded)
247 assignTexture(_status[i].state.get(), _status[i].texture_path,
248 wrapu, wrapv, mipmap);
249 _status[i].texture_loaded = true;
251 osg::StateSet *st = _status[i].state.get();
254 if (_current_ptr >= _status.size())
262 SGMaterial::build_state( bool defer_tex_load )
264 for (unsigned int i = 0; i < _status.size(); i++)
266 osg::StateSet *stateSet = new osg::StateSet;
267 stateSet->setUserData(new SGMaterialUserData(this));
269 // Set up the textured state
270 osg::ShadeModel* shadeModel = new osg::ShadeModel;
271 shadeModel->setMode(osg::ShadeModel::SMOOTH);
272 stateSet->setAttribute(shadeModel);
274 osg::CullFace* cullFace = new osg::CullFace;
275 cullFace->setMode(osg::CullFace::BACK);
276 stateSet->setMode(GL_CULL_FACE, osg::StateAttribute::ON);
277 stateSet->setAttribute(cullFace);
279 stateSet->setMode(GL_LIGHTING, osg::StateAttribute::ON);
281 _status[i].texture_loaded = false;
283 osg::Material* material = new osg::Material;
284 material->setColorMode(osg::Material::AMBIENT_AND_DIFFUSE);
285 material->setAmbient(osg::Material::FRONT_AND_BACK, ambient.osg());
286 material->setDiffuse(osg::Material::FRONT_AND_BACK, diffuse.osg());
287 material->setSpecular(osg::Material::FRONT_AND_BACK, specular.osg());
288 material->setEmission(osg::Material::FRONT_AND_BACK, emission.osg());
289 material->setShininess(osg::Material::FRONT_AND_BACK, shininess );
290 stateSet->setAttribute(material);
292 if (ambient[3] < 1 || diffuse[3] < 1 ||
293 specular[3] < 1 || emission[3] < 1) {
294 stateSet->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
295 stateSet->setMode(GL_BLEND, osg::StateAttribute::ON);
296 stateSet->setMode(GL_ALPHA_TEST, osg::StateAttribute::ON);
298 stateSet->setRenderingHint(osg::StateSet::OPAQUE_BIN);
299 stateSet->setMode(GL_BLEND, osg::StateAttribute::OFF);
300 stateSet->setMode(GL_ALPHA_TEST, osg::StateAttribute::OFF);
303 _status[i].state = stateSet;
308 void SGMaterial::set_state( osg::StateSet *s )
310 _status.push_back( _internal_state( s, "", true ) );
313 void SGMaterial::assignTexture( osg::StateSet *state, const std::string &fname,
314 int _wrapu, int _wrapv, int _mipmap )
316 osg::Texture2D* texture = SGLoadTexture2D(fname, 0, _wrapu, _wrapv,
318 texture->setMaxAnisotropy( SGGetTextureFilter());
319 state->setTextureAttributeAndModes(0, texture);
321 osg::TexEnv* texEnv = new osg::TexEnv;
322 texEnv->setMode(osg::TexEnv::MODULATE);
323 state->setTextureAttributeAndModes(0, texEnv);
326 SGMaterialGlyph* SGMaterial::get_glyph (const string& name) const
328 map<string, SGSharedPtr<SGMaterialGlyph> >::const_iterator it;
329 it = glyphs.find(name);
330 if (it == glyphs.end())
337 ////////////////////////////////////////////////////////////////////////
339 ////////////////////////////////////////////////////////////////////////
341 SGMaterialGlyph::SGMaterialGlyph(SGPropertyNode *p) :
342 _left(p->getDoubleValue("left", 0.0)),
343 _right(p->getDoubleValue("right", 1.0))
348 SGSetTextureFilter( int max) {
349 SGSceneFeatures::instance()->setTextureFilter( max);
353 SGGetTextureFilter() {
354 return SGSceneFeatures::instance()->getTextureFilter();