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 <boost/foreach.hpp>
38 #include <osg/CullFace>
39 #include <osg/Material>
40 #include <osg/ShadeModel>
41 #include <osg/StateSet>
43 #include <osg/Texture>
44 #include <osg/Texture2D>
45 #include <osgDB/ReaderWriter>
46 #include <osgDB/ReadFile>
47 #include <osgDB/Registry>
48 #include <osgDB/FileUtils>
50 #include <simgear/debug/logstream.hxx>
51 #include <simgear/misc/sg_path.hxx>
52 #include <simgear/misc/sgstream.hxx>
53 #include <simgear/scene/util/SGReaderWriterOptions.hxx>
54 #include <simgear/props/props_io.hxx>
55 #include <simgear/scene/model/model.hxx>
56 #include <simgear/scene/util/RenderConstants.hxx>
57 #include <simgear/scene/util/StateAttributeFactory.hxx>
60 #include "Technique.hxx"
65 using namespace simgear;
68 ////////////////////////////////////////////////////////////////////////
69 // Constructors and destructor.
70 ////////////////////////////////////////////////////////////////////////
72 SGMaterial::_internal_state::_internal_state(Effect *e, bool l,
73 const SGReaderWriterOptions* o)
74 : effect(e), effect_realized(l), options(o)
78 SGMaterial::_internal_state::_internal_state(Effect *e, const string &t, bool l,
79 const SGReaderWriterOptions* o)
80 : effect(e), effect_realized(l), options(o)
82 texture_paths.push_back(std::make_pair(t,0));
85 void SGMaterial::_internal_state::add_texture(const std::string &t, int i)
87 texture_paths.push_back(std::make_pair(t,i));
90 SGMaterial::SGMaterial( const SGReaderWriterOptions* options,
91 const SGPropertyNode *props,
92 SGPropertyNode *prop_root )
95 read_properties( options, props, prop_root );
96 buildEffectProperties(options);
99 SGMaterial::SGMaterial( const osgDB::Options* options,
100 const SGPropertyNode *props,
101 SGPropertyNode *prop_root)
103 osg::ref_ptr<SGReaderWriterOptions> opt;
104 opt = SGReaderWriterOptions::copyOrCreate(options);
106 read_properties(opt.get(), props, prop_root);
107 buildEffectProperties(opt.get());
110 SGMaterial::~SGMaterial (void)
115 ////////////////////////////////////////////////////////////////////////
117 ////////////////////////////////////////////////////////////////////////
120 SGMaterial::read_properties(const SGReaderWriterOptions* options,
121 const SGPropertyNode *props,
122 SGPropertyNode *prop_root)
124 std::vector<bool> dds;
125 std::vector<SGPropertyNode_ptr> textures = props->getChildren("texture");
126 for (unsigned int i = 0; i < textures.size(); i++)
128 string tname = textures[i]->getStringValue();
131 tname = "unknown.rgb";
134 SGPath tpath("Textures.high");
136 string fullTexPath = SGModelLib::findDataFile(tpath.str(), options);
137 if (fullTexPath.empty()) {
138 tpath = SGPath("Textures");
140 fullTexPath = SGModelLib::findDataFile(tpath.str(), options);
141 if (fullTexPath.empty()) {
142 SG_LOG(SG_GENERAL, SG_ALERT, "Cannot find texture \""
143 << tname << "\" in Textures or Textures.high folders.");
147 if (tpath.lower_extension() == "dds") {
150 dds.push_back(false);
153 if (!fullTexPath.empty() ) {
154 _internal_state st( NULL, fullTexPath, false, options );
155 _status.push_back( st );
159 std::vector<SGPropertyNode_ptr> texturesets = props->getChildren("texture-set");
160 for (unsigned int i = 0; i < texturesets.size(); i++)
162 _internal_state st( NULL, false, options );
163 std::vector<SGPropertyNode_ptr> textures = texturesets[i]->getChildren("texture");
164 for (unsigned int j = 0; j < textures.size(); j++)
166 string tname = textures[j]->getStringValue();
168 tname = "unknown.rgb";
171 SGPath tpath("Textures.high");
173 string fullTexPath = SGModelLib::findDataFile(tpath.str(), options);
174 if (fullTexPath.empty()) {
175 tpath = SGPath("Textures");
177 fullTexPath = SGModelLib::findDataFile(tpath.str(), options);
178 if (fullTexPath.empty() ) {
179 SG_LOG(SG_GENERAL, SG_ALERT, "Cannot find texture \""
180 << tname << "\" in Textures or Textures.high folders.");
185 if (tpath.lower_extension() == "dds") {
188 dds.push_back(false);
192 st.add_texture(fullTexPath, textures[j]->getIndex());
195 if (!st.texture_paths.empty() ) {
196 _status.push_back( st );
200 if (textures.size() == 0 && texturesets.size() == 0) {
201 SGPath tpath("Textures");
202 tpath.append("Terrain");
203 tpath.append("unknown.rgb");
204 _internal_state st( NULL, tpath.str(), true, options );
205 _status.push_back( st );
208 std::vector<SGPropertyNode_ptr> masks = props->getChildren("object-mask");
209 for (unsigned int i = 0; i < masks.size(); i++)
211 string omname = masks[i]->getStringValue();
213 if (! omname.empty()) {
214 SGPath ompath("Textures.high");
215 ompath.append(omname);
216 string fullMaskPath = SGModelLib::findDataFile(ompath.str(), options);
218 if (fullMaskPath.empty()) {
219 ompath = SGPath("Textures");
220 ompath.append(omname);
221 fullMaskPath = SGModelLib::findDataFile(ompath.str(), options);
224 if (fullMaskPath.empty()) {
225 SG_LOG(SG_GENERAL, SG_ALERT, "Cannot find texture \""
226 << omname << "\" in Textures or Textures.high folders.");
230 osg::Image* image = osgDB::readImageFile(fullMaskPath, options);
231 if (image && image->valid())
233 osg::Texture2D* object_mask = new osg::Texture2D;
235 bool dds_mask = (ompath.lower_extension() == "dds");
237 if (dds[i] != dds_mask) {
238 // Texture format does not match mask format. This is relevant for
239 // the object mask, as DDS textures have an origin at the bottom
240 // left rather than top left, therefore we flip the object mask
242 image->flipVertical();
245 object_mask->setImage(image);
247 // We force the filtering to be nearest, as the red channel (rotation)
248 // in particular, doesn't make sense to be interpolated between pixels.
249 object_mask->setFilter(osg::Texture::MIN_FILTER, osg::Texture::NEAREST);
250 object_mask->setFilter(osg::Texture::MAG_FILTER, osg::Texture::NEAREST);
252 object_mask->setDataVariance(osg::Object::STATIC);
253 object_mask->setWrap(osg::Texture::WRAP_S, osg::Texture::REPEAT);
254 object_mask->setWrap(osg::Texture::WRAP_T, osg::Texture::REPEAT);
255 _masks.push_back(object_mask);
261 xsize = props->getDoubleValue("xsize", 0.0);
262 ysize = props->getDoubleValue("ysize", 0.0);
263 wrapu = props->getBoolValue("wrapu", true);
264 wrapv = props->getBoolValue("wrapv", true);
265 mipmap = props->getBoolValue("mipmap", true);
266 light_coverage = props->getDoubleValue("light-coverage", 0.0);
268 // Building properties
269 building_coverage = props->getDoubleValue("building-coverage", 0.0);
270 building_spacing = props->getDoubleValue("building-spacing-m", 5.0);
272 string bt = props->getStringValue("building-texture", "Textures/buildings.png");
273 building_texture = SGModelLib::findDataFile(bt, options);
275 if (building_texture.empty()) {
276 SG_LOG(SG_GENERAL, SG_ALERT, "Cannot find texture \"" << bt);
279 building_small_ratio = props->getDoubleValue("building-small-ratio", 0.8);
280 building_medium_ratio = props->getDoubleValue("building-medium-ratio", 0.15);
281 building_large_ratio = props->getDoubleValue("building-large-ratio", 0.05);
283 building_small_pitch = props->getDoubleValue("building-small-pitch", 0.8);
284 building_medium_pitch = props->getDoubleValue("building-medium-pitch", 0.2);
285 building_large_pitch = props->getDoubleValue("building-large-pitch", 0.1);
287 building_small_min_floors = props->getIntValue("building-small-min-floors", 1);
288 building_small_max_floors = props->getIntValue("building-small-max-floors", 3);
289 building_medium_min_floors = props->getIntValue("building-medium-min-floors", 3);
290 building_medium_max_floors = props->getIntValue("building-medium-max-floors", 8);
291 building_large_min_floors = props->getIntValue("building-large-min-floors", 5);
292 building_large_max_floors = props->getIntValue("building-large-max-floors", 20);
294 building_small_min_width = props->getFloatValue("building-small-min-width-m", 15.0);
295 building_small_max_width = props->getFloatValue("building-small-max-width-m", 60.0);
296 building_small_min_depth = props->getFloatValue("building-small-min-depth-m", 10.0);
297 building_small_max_depth = props->getFloatValue("building-small-max-depth-m", 20.0);
299 building_medium_min_width = props->getFloatValue("building-medium-min-width-m", 25.0);
300 building_medium_max_width = props->getFloatValue("building-medium-max-width-m", 50.0);
301 building_medium_min_depth = props->getFloatValue("building-medium-min-depth-m", 20.0);
302 building_medium_max_depth = props->getFloatValue("building-medium-max-depth-m", 50.0);
304 building_large_min_width = props->getFloatValue("building-large-min-width-m", 50.0);
305 building_large_max_width = props->getFloatValue("building-large-max-width-m", 75.0);
306 building_large_min_depth = props->getFloatValue("building-large-min-depth-m", 50.0);
307 building_large_max_depth = props->getFloatValue("building-large-max-depth-m", 75.0);
309 // Random vegetation properties
310 wood_coverage = props->getDoubleValue("wood-coverage", 0.0);
311 tree_height = props->getDoubleValue("tree-height-m", 0.0);
312 tree_width = props->getDoubleValue("tree-width-m", 0.0);
313 tree_range = props->getDoubleValue("tree-range-m", 0.0);
314 tree_varieties = props->getIntValue("tree-varieties", 1);
316 const SGPropertyNode* treeTexNode = props->getChild("tree-texture");
319 string treeTexPath = props->getStringValue("tree-texture");
321 if (! treeTexPath.empty()) {
322 SGPath treePath("Textures.high");
323 treePath.append(treeTexPath);
324 tree_texture = SGModelLib::findDataFile(treePath.str(), options);
326 if (tree_texture.empty()) {
327 treePath = SGPath("Textures");
328 treePath.append(treeTexPath);
329 tree_texture = SGModelLib::findDataFile(treePath.str(), options);
334 // surface values for use with ground reactions
335 solid = props->getBoolValue("solid", true);
336 friction_factor = props->getDoubleValue("friction-factor", 1.0);
337 rolling_friction = props->getDoubleValue("rolling-friction", 0.02);
338 bumpiness = props->getDoubleValue("bumpiness", 0.0);
339 load_resistance = props->getDoubleValue("load-resistance", 1e30);
341 // Taken from default values as used in ac3d
342 ambient[0] = props->getDoubleValue("ambient/r", 0.2);
343 ambient[1] = props->getDoubleValue("ambient/g", 0.2);
344 ambient[2] = props->getDoubleValue("ambient/b", 0.2);
345 ambient[3] = props->getDoubleValue("ambient/a", 1.0);
347 diffuse[0] = props->getDoubleValue("diffuse/r", 0.8);
348 diffuse[1] = props->getDoubleValue("diffuse/g", 0.8);
349 diffuse[2] = props->getDoubleValue("diffuse/b", 0.8);
350 diffuse[3] = props->getDoubleValue("diffuse/a", 1.0);
352 specular[0] = props->getDoubleValue("specular/r", 0.0);
353 specular[1] = props->getDoubleValue("specular/g", 0.0);
354 specular[2] = props->getDoubleValue("specular/b", 0.0);
355 specular[3] = props->getDoubleValue("specular/a", 1.0);
357 emission[0] = props->getDoubleValue("emissive/r", 0.0);
358 emission[1] = props->getDoubleValue("emissive/g", 0.0);
359 emission[2] = props->getDoubleValue("emissive/b", 0.0);
360 emission[3] = props->getDoubleValue("emissive/a", 1.0);
362 shininess = props->getDoubleValue("shininess", 1.0);
364 if (props->hasChild("effect"))
365 effect = props->getStringValue("effect");
367 std::vector<SGPropertyNode_ptr> object_group_nodes =
368 ((SGPropertyNode *)props)->getChildren("object-group");
369 for (unsigned int i = 0; i < object_group_nodes.size(); i++)
370 object_groups.push_back(new SGMatModelGroup(object_group_nodes[i]));
372 // read glyph table for taxi-/runway-signs
373 std::vector<SGPropertyNode_ptr> glyph_nodes = props->getChildren("glyph");
374 for (unsigned int i = 0; i < glyph_nodes.size(); i++) {
375 const char *name = glyph_nodes[i]->getStringValue("name");
377 glyphs[name] = new SGMaterialGlyph(glyph_nodes[i]);
380 // Read conditions node
381 const SGPropertyNode *conditionNode = props->getChild("condition");
383 condition = sgReadCondition(prop_root, conditionNode);
389 ////////////////////////////////////////////////////////////////////////
391 ////////////////////////////////////////////////////////////////////////
403 light_coverage = 0.0;
404 building_coverage = 0.0;
408 rolling_friction = 0.02;
410 load_resistance = 1e30;
413 for (int i = 0; i < 4; i++) {
414 ambient[i] = (i < 3) ? 0.2 : 1.0;
415 specular[i] = (i < 3) ? 0.0 : 1.0;
416 diffuse[i] = (i < 3) ? 0.8 : 1.0;
417 emission[i] = (i < 3) ? 0.0 : 1.0;
419 effect = "Effects/terrain-default";
422 Effect* SGMaterial::get_effect(int i)
424 if(!_status[i].effect_realized) {
425 if (!_status[i].effect.valid())
427 _status[i].effect->realizeTechniques(_status[i].options.get());
428 _status[i].effect_realized = true;
430 return _status[i].effect.get();
433 Effect* SGMaterial::get_effect(SGTexturedTriangleBin triangleBin)
435 if (_status.size() == 0) {
436 SG_LOG( SG_GENERAL, SG_WARN, "No effect available.");
440 int i = triangleBin.getTextureIndex() % _status.size();
441 return get_effect(i);
444 Effect* SGMaterial::get_effect()
446 return get_effect(0);
450 osg::Texture2D* SGMaterial::get_object_mask(SGTexturedTriangleBin triangleBin)
452 if (_status.size() == 0) {
453 SG_LOG( SG_GENERAL, SG_WARN, "No mask available.");
457 // Note that the object mask is closely linked to the texture/effect
458 // so we index based on the texture index,
459 unsigned int i = triangleBin.getTextureIndex() % _status.size();
460 if (i < _masks.size()) {
467 void SGMaterial::buildEffectProperties(const SGReaderWriterOptions* options)
470 ref_ptr<SGMaterialUserData> user = new SGMaterialUserData(this);
471 SGPropertyNode_ptr propRoot = new SGPropertyNode();
472 makeChild(propRoot, "inherits-from")->setStringValue(effect);
473 SGPropertyNode* paramProp = makeChild(propRoot, "parameters");
474 SGPropertyNode* materialProp = makeChild(paramProp, "material");
475 makeChild(materialProp, "ambient")->setValue(SGVec4d(ambient));
476 makeChild(materialProp, "diffuse")->setValue(SGVec4d(diffuse));
477 makeChild(materialProp, "specular")->setValue(SGVec4d(specular));
478 makeChild(materialProp, "emissive")->setValue(SGVec4d(emission));
479 makeChild(materialProp, "shininess")->setFloatValue(shininess);
480 if (ambient[3] < 1 || diffuse[3] < 1 ||
481 specular[3] < 1 || emission[3] < 1) {
482 makeChild(paramProp, "transparent")->setBoolValue(true);
483 SGPropertyNode* binProp = makeChild(paramProp, "render-bin");
484 makeChild(binProp, "bin-number")->setIntValue(TRANSPARENT_BIN);
485 makeChild(binProp, "bin-name")->setStringValue("DepthSortedBin");
487 BOOST_FOREACH(_internal_state& matState, _status)
489 SGPropertyNode_ptr effectProp = new SGPropertyNode();
490 copyProperties(propRoot, effectProp);
491 SGPropertyNode* effectParamProp = effectProp->getChild("parameters", 0);
492 for (unsigned int i = 0; i < matState.texture_paths.size(); i++) {
493 SGPropertyNode* texProp = makeChild(effectParamProp, "texture", matState.texture_paths[i].second);
494 makeChild(texProp, "image")->setStringValue(matState.texture_paths[i].first);
495 makeChild(texProp, "filter")
496 ->setStringValue(mipmap ? "linear-mipmap-linear" : "nearest");
497 makeChild(texProp, "wrap-s")
498 ->setStringValue(wrapu ? "repeat" : "clamp");
499 makeChild(texProp, "wrap-t")
500 ->setStringValue(wrapv ? "repeat" : "clamp");
502 makeChild(effectParamProp, "xsize")->setDoubleValue(xsize);
503 makeChild(effectParamProp, "ysize")->setDoubleValue(ysize);
504 makeChild(effectParamProp, "scale")->setValue(SGVec3d(xsize,ysize,0.0));
505 makeChild(effectParamProp, "light-coverage")->setDoubleValue(light_coverage);
507 matState.effect = makeEffect(effectProp, false, options);
508 if (matState.effect.valid())
509 matState.effect->setUserData(user.get());
513 SGMaterialGlyph* SGMaterial::get_glyph (const string& name) const
515 map<string, SGSharedPtr<SGMaterialGlyph> >::const_iterator it;
516 it = glyphs.find(name);
517 if (it == glyphs.end())
524 ////////////////////////////////////////////////////////////////////////
526 ////////////////////////////////////////////////////////////////////////
528 SGMaterialGlyph::SGMaterialGlyph(SGPropertyNode *p) :
529 _left(p->getDoubleValue("left", 0.0)),
530 _right(p->getDoubleValue("right", 1.0))
535 SGSetTextureFilter( int max) {
536 SGSceneFeatures::instance()->setTextureFilter( max);
540 SGGetTextureFilter() {
541 return SGSceneFeatures::instance()->getTextureFilter();