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/Texture2D>
44 #include <osgDB/Options>
45 #include <osgDB/ReadFile>
46 #include <osgDB/Registry>
47 #include <osgDB/FileUtils>
49 #include <simgear/debug/logstream.hxx>
50 #include <simgear/misc/sg_path.hxx>
51 #include <simgear/misc/sgstream.hxx>
52 #include <simgear/props/props_io.hxx>
53 #include <simgear/scene/model/model.hxx>
54 #include <simgear/scene/util/RenderConstants.hxx>
55 #include <simgear/scene/util/StateAttributeFactory.hxx>
58 #include "Technique.hxx"
63 using namespace simgear;
66 ////////////////////////////////////////////////////////////////////////
67 // Constructors and destructor.
68 ////////////////////////////////////////////////////////////////////////
70 SGMaterial::_internal_state::_internal_state(Effect *e, const string &t, bool l,
71 const osgDB::Options* o ) :
72 effect(e), texture_path(t), effect_realized(l), options(o)
76 SGMaterial::SGMaterial( const osgDB::Options* options,
77 const SGPropertyNode *props )
80 read_properties( options, props );
81 buildEffectProperties(options);
84 SGMaterial::~SGMaterial (void)
89 ////////////////////////////////////////////////////////////////////////
91 ////////////////////////////////////////////////////////////////////////
94 SGMaterial::read_properties(const osgDB::Options* options,
95 const SGPropertyNode *props)
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();
103 tname = "unknown.rgb";
105 SGPath tpath("Textures.high");
107 osgDB::Registry* reg = osgDB::Registry::instance();
108 string fullTexPath = reg->findDataFile(tpath.str(), options,
109 osgDB::CASE_SENSITIVE);
110 if (fullTexPath.empty()) {
111 tpath = SGPath("Textures");
113 fullTexPath = reg->findDataFile(tpath.str(), options,
114 osgDB::CASE_SENSITIVE);
117 if (!fullTexPath.empty() ) {
118 _internal_state st( NULL, fullTexPath, false, options );
119 _status.push_back( st );
123 if (textures.size() == 0) {
124 SGPath tpath("Textures");
125 tpath.append("Terrain");
126 tpath.append("unknown.rgb");
127 _internal_state st( NULL, tpath.str(), true, options );
128 _status.push_back( st );
131 xsize = props->getDoubleValue("xsize", 0.0);
132 ysize = props->getDoubleValue("ysize", 0.0);
133 wrapu = props->getBoolValue("wrapu", true);
134 wrapv = props->getBoolValue("wrapv", true);
135 mipmap = props->getBoolValue("mipmap", true);
136 light_coverage = props->getDoubleValue("light-coverage", 0.0);
137 tree_coverage = props->getDoubleValue("tree-coverage", 0.0);
138 tree_height = props->getDoubleValue("tree-height-m", 0.0);
139 tree_width = props->getDoubleValue("tree-width-m", 0.0);
140 tree_range = props->getDoubleValue("tree-range-m", 0.0);
141 tree_varieties = props->getIntValue("tree-varieties", 1);
142 const SGPropertyNode* treeTexNode = props->getChild("tree-texture");
144 string treeTexPath = props->getStringValue("tree-texture");
145 tree_texture = osgDB::Registry::instance()
146 ->findDataFile(treeTexPath, options, osgDB::CASE_SENSITIVE);
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 if (props->hasChild("effect"))
180 effect = props->getStringValue("effect");
182 vector<SGPropertyNode_ptr> object_group_nodes =
183 ((SGPropertyNode *)props)->getChildren("object-group");
184 for (unsigned int i = 0; i < object_group_nodes.size(); i++)
185 object_groups.push_back(new SGMatModelGroup(object_group_nodes[i]));
187 // read glyph table for taxi-/runway-signs
188 vector<SGPropertyNode_ptr> glyph_nodes = props->getChildren("glyph");
189 for (unsigned int i = 0; i < glyph_nodes.size(); i++) {
190 const char *name = glyph_nodes[i]->getStringValue("name");
192 glyphs[name] = new SGMaterialGlyph(glyph_nodes[i]);
198 ////////////////////////////////////////////////////////////////////////
200 ////////////////////////////////////////////////////////////////////////
213 light_coverage = 0.0;
217 rolling_friction = 0.02;
219 load_resistance = 1e30;
222 for (int i = 0; i < 4; i++) {
223 ambient[i] = (i < 3) ? 0.2 : 1.0;
224 specular[i] = (i < 3) ? 0.0 : 1.0;
225 diffuse[i] = (i < 3) ? 0.8 : 1.0;
226 emission[i] = (i < 3) ? 0.0 : 1.0;
228 effect = "Effects/terrain-default";
231 Effect* SGMaterial::get_effect(int n)
233 if (_status.size() == 0) {
234 SG_LOG( SG_GENERAL, SG_WARN, "No effect available.");
237 int i = n >= 0 ? n : _current_ptr;
238 if(!_status[i].effect_realized) {
239 _status[i].effect->realizeTechniques(_status[i].options.get());
240 _status[i].effect_realized = true;
242 // XXX This business of returning a "random" alternate texture is
243 // really bogus. It means that the appearance of the terrain
244 // depends on the order in which it is paged in!
245 _current_ptr = (_current_ptr + 1) % _status.size();
246 return _status[i].effect.get();
249 void SGMaterial::buildEffectProperties(const osgDB::Options* options)
252 SGPropertyNode_ptr propRoot = new SGPropertyNode();
253 makeChild(propRoot, "inherits-from")->setStringValue(effect);
254 SGPropertyNode* paramProp = makeChild(propRoot, "parameters");
255 SGPropertyNode* materialProp = makeChild(paramProp, "material");
256 makeChild(materialProp, "ambient")->setValue(SGVec4d(ambient));
257 makeChild(materialProp, "diffuse")->setValue(SGVec4d(diffuse));
258 makeChild(materialProp, "specular")->setValue(SGVec4d(specular));
259 makeChild(materialProp, "emissive")->setValue(SGVec4d(emission));
260 makeChild(materialProp, "shininess")->setFloatValue(shininess);
261 if (ambient[3] < 1 || diffuse[3] < 1 ||
262 specular[3] < 1 || emission[3] < 1) {
263 makeChild(paramProp, "transparent")->setBoolValue(true);
264 SGPropertyNode* binProp = makeChild(paramProp, "render-bin");
265 makeChild(binProp, "bin-number")->setIntValue(TRANSPARENT_BIN);
266 makeChild(binProp, "bin-name")->setStringValue("DepthSortedBin");
268 BOOST_FOREACH(_internal_state& matState, _status)
270 SGPropertyNode_ptr effectProp = new SGPropertyNode();
271 copyProperties(propRoot, effectProp);
272 SGPropertyNode* effectParamProp = effectProp->getChild("parameters", 0);
273 SGPropertyNode* texProp = makeChild(effectParamProp, "texture");
274 SGPropertyNode* tex2dProp = makeChild(texProp, "texture2d");
275 makeChild(tex2dProp, "image")->setStringValue(matState.texture_path);
276 makeChild(tex2dProp, "filter")
277 ->setStringValue(mipmap ? "linear-mipmap-linear" : "nearest");
278 makeChild(tex2dProp, "wrap-s")
279 ->setStringValue(wrapu ? "repeat" : "clamp");
280 makeChild(tex2dProp, "wrap-t")
281 ->setStringValue(wrapv ? "repeat" : "clamp");
282 matState.effect = makeEffect(effectProp, false, options);
286 SGMaterialGlyph* SGMaterial::get_glyph (const string& name) const
288 map<string, SGSharedPtr<SGMaterialGlyph> >::const_iterator it;
289 it = glyphs.find(name);
290 if (it == glyphs.end())
297 ////////////////////////////////////////////////////////////////////////
299 ////////////////////////////////////////////////////////////////////////
301 SGMaterialGlyph::SGMaterialGlyph(SGPropertyNode *p) :
302 _left(p->getDoubleValue("left", 0.0)),
303 _right(p->getDoubleValue("right", 1.0))
308 SGSetTextureFilter( int max) {
309 SGSceneFeatures::instance()->setTextureFilter( max);
313 SGGetTextureFilter() {
314 return SGSceneFeatures::instance()->getTextureFilter();