00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #ifdef HAVE_CONFIG_H
00025 # include <simgear_config.h>
00026 #endif
00027
00028 #include <simgear/compiler.h>
00029
00030 #include <string.h>
00031 #include <map>
00032 #include <vector>
00033 #include<string>
00034
00035 #include <boost/foreach.hpp>
00036 #include "mat.hxx"
00037
00038 #include <osg/CullFace>
00039 #include <osg/Material>
00040 #include <osg/ShadeModel>
00041 #include <osg/StateSet>
00042 #include <osg/TexEnv>
00043 #include <osg/Texture2D>
00044 #include <osgDB/ReaderWriter>
00045 #include <osgDB/ReadFile>
00046 #include <osgDB/Registry>
00047 #include <osgDB/FileUtils>
00048
00049 #include <simgear/debug/logstream.hxx>
00050 #include <simgear/misc/sg_path.hxx>
00051 #include <simgear/misc/sgstream.hxx>
00052 #include <simgear/props/props_io.hxx>
00053 #include <simgear/scene/model/model.hxx>
00054 #include <simgear/scene/util/RenderConstants.hxx>
00055 #include <simgear/scene/util/StateAttributeFactory.hxx>
00056
00057 #include "Effect.hxx"
00058 #include "Technique.hxx"
00059 #include "Pass.hxx"
00060
00061 using std::map;
00062 using std::string;
00063 using namespace simgear;
00064
00065
00067
00069
00070 SGMaterial::_internal_state::_internal_state(Effect *e, const string &t, bool l,
00071 const osgDB::ReaderWriter::Options* o ) :
00072 effect(e), texture_path(t), effect_realized(l), options(o)
00073 {
00074 }
00075
00076 SGMaterial::SGMaterial( const osgDB::ReaderWriter::Options* options,
00077 const SGPropertyNode *props )
00078 {
00079 init();
00080 read_properties( options, props );
00081 buildEffectProperties(options);
00082 }
00083
00084 SGMaterial::~SGMaterial (void)
00085 {
00086 }
00087
00088
00090
00092
00093 void
00094 SGMaterial::read_properties(const osgDB::ReaderWriter::Options* options,
00095 const SGPropertyNode *props)
00096 {
00097
00098 vector<SGPropertyNode_ptr> textures = props->getChildren("texture");
00099 for (unsigned int i = 0; i < textures.size(); i++)
00100 {
00101 string tname = textures[i]->getStringValue();
00102 if (tname.empty()) {
00103 tname = "unknown.rgb";
00104 }
00105 SGPath tpath("Textures.high");
00106 tpath.append(tname);
00107 string fullTexPath = osgDB::findDataFile(tpath.str(), options);
00108 if (fullTexPath.empty()) {
00109 tpath = SGPath("Textures");
00110 tpath.append(tname);
00111 fullTexPath = osgDB::findDataFile(tpath.str(), options);
00112 }
00113
00114 if (!fullTexPath.empty() ) {
00115 _internal_state st( NULL, fullTexPath, false, options );
00116 _status.push_back( st );
00117 }
00118 }
00119
00120 if (textures.size() == 0) {
00121 SGPath tpath("Textures");
00122 tpath.append("Terrain");
00123 tpath.append("unknown.rgb");
00124 _internal_state st( NULL, tpath.str(), true, options );
00125 _status.push_back( st );
00126 }
00127
00128 xsize = props->getDoubleValue("xsize", 0.0);
00129 ysize = props->getDoubleValue("ysize", 0.0);
00130 wrapu = props->getBoolValue("wrapu", true);
00131 wrapv = props->getBoolValue("wrapv", true);
00132 mipmap = props->getBoolValue("mipmap", true);
00133 light_coverage = props->getDoubleValue("light-coverage", 0.0);
00134 wood_coverage = props->getDoubleValue("wood-coverage", 0.0);
00135 wood_size = props->getDoubleValue("wood-size", 0.0);
00136 tree_density = props->getDoubleValue("tree-density", 1.0);
00137 tree_height = props->getDoubleValue("tree-height-m", 0.0);
00138 tree_width = props->getDoubleValue("tree-width-m", 0.0);
00139 tree_range = props->getDoubleValue("tree-range-m", 0.0);
00140 tree_varieties = props->getIntValue("tree-varieties", 1);
00141
00142 const SGPropertyNode* treeTexNode = props->getChild("tree-texture");
00143 if (treeTexNode) {
00144 string treeTexPath = props->getStringValue("tree-texture");
00145 tree_texture = osgDB::findDataFile(treeTexPath, options);
00146 }
00147
00148
00149 solid = props->getBoolValue("solid", true);
00150 friction_factor = props->getDoubleValue("friction-factor", 1.0);
00151 rolling_friction = props->getDoubleValue("rolling-friction", 0.02);
00152 bumpiness = props->getDoubleValue("bumpiness", 0.0);
00153 load_resistance = props->getDoubleValue("load-resistance", 1e30);
00154
00155
00156 ambient[0] = props->getDoubleValue("ambient/r", 0.2);
00157 ambient[1] = props->getDoubleValue("ambient/g", 0.2);
00158 ambient[2] = props->getDoubleValue("ambient/b", 0.2);
00159 ambient[3] = props->getDoubleValue("ambient/a", 1.0);
00160
00161 diffuse[0] = props->getDoubleValue("diffuse/r", 0.8);
00162 diffuse[1] = props->getDoubleValue("diffuse/g", 0.8);
00163 diffuse[2] = props->getDoubleValue("diffuse/b", 0.8);
00164 diffuse[3] = props->getDoubleValue("diffuse/a", 1.0);
00165
00166 specular[0] = props->getDoubleValue("specular/r", 0.0);
00167 specular[1] = props->getDoubleValue("specular/g", 0.0);
00168 specular[2] = props->getDoubleValue("specular/b", 0.0);
00169 specular[3] = props->getDoubleValue("specular/a", 1.0);
00170
00171 emission[0] = props->getDoubleValue("emissive/r", 0.0);
00172 emission[1] = props->getDoubleValue("emissive/g", 0.0);
00173 emission[2] = props->getDoubleValue("emissive/b", 0.0);
00174 emission[3] = props->getDoubleValue("emissive/a", 1.0);
00175
00176 shininess = props->getDoubleValue("shininess", 1.0);
00177
00178 if (props->hasChild("effect"))
00179 effect = props->getStringValue("effect");
00180
00181 vector<SGPropertyNode_ptr> object_group_nodes =
00182 ((SGPropertyNode *)props)->getChildren("object-group");
00183 for (unsigned int i = 0; i < object_group_nodes.size(); i++)
00184 object_groups.push_back(new SGMatModelGroup(object_group_nodes[i]));
00185
00186
00187 vector<SGPropertyNode_ptr> glyph_nodes = props->getChildren("glyph");
00188 for (unsigned int i = 0; i < glyph_nodes.size(); i++) {
00189 const char *name = glyph_nodes[i]->getStringValue("name");
00190 if (name)
00191 glyphs[name] = new SGMaterialGlyph(glyph_nodes[i]);
00192 }
00193 }
00194
00195
00196
00198
00200
00201 void
00202 SGMaterial::init ()
00203 {
00204 _status.clear();
00205 _current_ptr = 0;
00206 xsize = 0;
00207 ysize = 0;
00208 wrapu = true;
00209 wrapv = true;
00210
00211 mipmap = true;
00212 light_coverage = 0.0;
00213
00214 solid = true;
00215 friction_factor = 1;
00216 rolling_friction = 0.02;
00217 bumpiness = 0;
00218 load_resistance = 1e30;
00219
00220 shininess = 1.0;
00221 for (int i = 0; i < 4; i++) {
00222 ambient[i] = (i < 3) ? 0.2 : 1.0;
00223 specular[i] = (i < 3) ? 0.0 : 1.0;
00224 diffuse[i] = (i < 3) ? 0.8 : 1.0;
00225 emission[i] = (i < 3) ? 0.0 : 1.0;
00226 }
00227 effect = "Effects/terrain-default";
00228 }
00229
00230 Effect* SGMaterial::get_effect(int n)
00231 {
00232 if (_status.size() == 0) {
00233 SG_LOG( SG_GENERAL, SG_WARN, "No effect available.");
00234 return 0;
00235 }
00236 int i = n >= 0 ? n : _current_ptr;
00237 if(!_status[i].effect_realized) {
00238 _status[i].effect->realizeTechniques(_status[i].options.get());
00239 _status[i].effect_realized = true;
00240 }
00241
00242
00243
00244 _current_ptr = (_current_ptr + 1) % _status.size();
00245 return _status[i].effect.get();
00246 }
00247
00248 void SGMaterial::buildEffectProperties(const osgDB::ReaderWriter::Options*
00249 options)
00250 {
00251 using namespace osg;
00252 ref_ptr<SGMaterialUserData> user = new SGMaterialUserData(this);
00253 SGPropertyNode_ptr propRoot = new SGPropertyNode();
00254 makeChild(propRoot, "inherits-from")->setStringValue(effect);
00255 SGPropertyNode* paramProp = makeChild(propRoot, "parameters");
00256 SGPropertyNode* materialProp = makeChild(paramProp, "material");
00257 makeChild(materialProp, "ambient")->setValue(SGVec4d(ambient));
00258 makeChild(materialProp, "diffuse")->setValue(SGVec4d(diffuse));
00259 makeChild(materialProp, "specular")->setValue(SGVec4d(specular));
00260 makeChild(materialProp, "emissive")->setValue(SGVec4d(emission));
00261 makeChild(materialProp, "shininess")->setFloatValue(shininess);
00262 if (ambient[3] < 1 || diffuse[3] < 1 ||
00263 specular[3] < 1 || emission[3] < 1) {
00264 makeChild(paramProp, "transparent")->setBoolValue(true);
00265 SGPropertyNode* binProp = makeChild(paramProp, "render-bin");
00266 makeChild(binProp, "bin-number")->setIntValue(TRANSPARENT_BIN);
00267 makeChild(binProp, "bin-name")->setStringValue("DepthSortedBin");
00268 }
00269 BOOST_FOREACH(_internal_state& matState, _status)
00270 {
00271 SGPropertyNode_ptr effectProp = new SGPropertyNode();
00272 copyProperties(propRoot, effectProp);
00273 SGPropertyNode* effectParamProp = effectProp->getChild("parameters", 0);
00274 SGPropertyNode* texProp = makeChild(effectParamProp, "texture");
00275 makeChild(texProp, "image")->setStringValue(matState.texture_path);
00276 makeChild(texProp, "filter")
00277 ->setStringValue(mipmap ? "linear-mipmap-linear" : "nearest");
00278 makeChild(texProp, "wrap-s")
00279 ->setStringValue(wrapu ? "repeat" : "clamp");
00280 makeChild(texProp, "wrap-t")
00281 ->setStringValue(wrapv ? "repeat" : "clamp");
00282 matState.effect = makeEffect(effectProp, false, options);
00283 matState.effect->setUserData(user.get());
00284 }
00285 }
00286
00287 SGMaterialGlyph* SGMaterial::get_glyph (const string& name) const
00288 {
00289 map<string, SGSharedPtr<SGMaterialGlyph> >::const_iterator it;
00290 it = glyphs.find(name);
00291 if (it == glyphs.end())
00292 return 0;
00293
00294 return it->second;
00295 }
00296
00297
00299
00301
00302 SGMaterialGlyph::SGMaterialGlyph(SGPropertyNode *p) :
00303 _left(p->getDoubleValue("left", 0.0)),
00304 _right(p->getDoubleValue("right", 1.0))
00305 {
00306 }
00307
00308 void
00309 SGSetTextureFilter( int max) {
00310 SGSceneFeatures::instance()->setTextureFilter( max);
00311 }
00312
00313 int
00314 SGGetTextureFilter() {
00315 return SGSceneFeatures::instance()->getTextureFilter();
00316 }