]> git.mxchange.org Git - simgear.git/blobdiff - simgear/scene/material/mat.cxx
Use bool instead of int to represent boolean values
[simgear.git] / simgear / scene / material / mat.cxx
index b9d4fa6db6731f3e782452aa000523baf956f3f6..2d488f345eb7db8da34c8bbebbba5b3a9fcdd932 100644 (file)
@@ -49,9 +49,8 @@ SG_USING_STD(map);
 #include <simgear/misc/sgstream.hxx>
 
 #include <simgear/scene/model/model.hxx>
-#include "mat.hxx"
 
-static map<string, osg::ref_ptr<osg::Texture2D> > _tex_cache;
+#include "mat.hxx"
 
 \f
 ////////////////////////////////////////////////////////////////////////
@@ -101,11 +100,6 @@ SGMaterial::read_properties( const string &fg_root, const SGPropertyNode * props
   {
     string tname = textures[i]->getStringValue();
     string otname = tname;
-    if (season && strncmp(season, "summer", 6))
-    {
-        if (tname.substr(0,7) == "Terrain")
-            tname.insert(7,"."+string(season));
-    }
 
     if (tname == "") {
         tname = "unknown.rgb";
@@ -142,13 +136,22 @@ SGMaterial::read_properties( const string &fg_root, const SGPropertyNode * props
   wrapv = props->getBoolValue("wrapv", true);
   mipmap = props->getBoolValue("mipmap", true);
   light_coverage = props->getDoubleValue("light-coverage", 0.0);
+  tree_coverage = props->getDoubleValue("tree-coverage", 0.0);
+  tree_height = props->getDoubleValue("tree-height-m", 0.0);
+  tree_width = props->getDoubleValue("tree-width-m", 0.0);
+  tree_range = props->getDoubleValue("tree-range-m", 0.0);
+  tree_varieties = props->getIntValue("tree-varieties", 1);
+
+  SGPath tpath( fg_root );
+  tpath.append(props->getStringValue("tree-texture"));
+  tree_texture = tpath.str();
 
   // surface values for use with ground reactions
   solid = props->getBoolValue("solid", true);
   friction_factor = props->getDoubleValue("friction-factor", 1.0);
   rolling_friction = props->getDoubleValue("rolling-friction", 0.02);
   bumpiness = props->getDoubleValue("bumpiness", 0.0);
-  load_resistence = props->getDoubleValue("load-resistence", 1e30);
+  load_resistance = props->getDoubleValue("load-resistance", 1e30);
 
   // Taken from default values as used in ac3d
   ambient[0] = props->getDoubleValue("ambient/r", 0.2);
@@ -210,7 +213,7 @@ SGMaterial::init ()
     friction_factor = 1;
     rolling_friction = 0.02;
     bumpiness = 0;
-    load_resistence = 1e30;
+    load_resistance = 1e30;
 
     shininess = 1.0;
     for (int i = 0; i < 4; i++) {
@@ -221,35 +224,24 @@ SGMaterial::init ()
     }
 }
 
-bool
-SGMaterial::load_texture ( int n )
-{
-    int i   = (n >= 0) ? n   : 0 ;
-    int end = (n >= 0) ? n+1 : _status.size();
-
-    for (; i < end; i++)
-    {
-        if ( !_status[i].texture_loaded ) {
-            SG_LOG( SG_GENERAL, SG_INFO, "Loading deferred texture "
-                                          << _status[i].texture_path );
-            assignTexture(_status[i].state.get(), _status[i].texture_path,
-                                         wrapu, wrapv, mipmap );
-            _status[i].texture_loaded = true;
-       }
-    }
-    return true;
-}
-
 osg::StateSet *
-SGMaterial::get_state (int n) const
+SGMaterial::get_state (int n)
 {
     if (_status.size() == 0) {
         SG_LOG( SG_GENERAL, SG_WARN, "No state available.");
         return NULL;
     }
+    
+    int i = n >= 0 ? n : _current_ptr;
+
+    if(!_status[i].texture_loaded)
+    {
+        assignTexture(_status[i].state.get(), _status[i].texture_path,
+                      wrapu, wrapv, mipmap);
+        _status[i].texture_loaded = true;
+    }
+    osg::StateSet *st = _status[i].state.get();
 
-    osg::StateSet *st = (n >= 0) ? _status[n].state.get()
-                             : _status[_current_ptr].state.get();
     _current_ptr += 1;
     if (_current_ptr >= _status.size())
         _current_ptr = 0;
@@ -264,37 +256,41 @@ SGMaterial::build_state( bool defer_tex_load )
     for (unsigned int i = 0; i < _status.size(); i++)
     {
         osg::StateSet *stateSet = new osg::StateSet;
+        stateSet->setUserData(new SGMaterialUserData(this));
 
         // Set up the textured state
         osg::ShadeModel* shadeModel = new osg::ShadeModel;
         shadeModel->setMode(osg::ShadeModel::SMOOTH);
-        stateSet->setAttributeAndModes(shadeModel, osg::StateAttribute::ON);
+        stateSet->setAttribute(shadeModel);
 
         osg::CullFace* cullFace = new osg::CullFace;
         cullFace->setMode(osg::CullFace::BACK);
-        stateSet->setAttributeAndModes(cullFace, osg::StateAttribute::ON);
+        stateSet->setMode(GL_CULL_FACE, osg::StateAttribute::ON);
+        stateSet->setAttribute(cullFace);
 
         stateSet->setMode(GL_LIGHTING, osg::StateAttribute::ON);
-        stateSet->setMode(GL_BLEND, osg::StateAttribute::OFF);
-        stateSet->setMode(GL_ALPHA_TEST, osg::StateAttribute::OFF);
 
-        if ( !defer_tex_load ) {
-            SG_LOG(SG_INPUT, SG_INFO, "    " << _status[i].texture_path );
-           assignTexture( stateSet, _status[i].texture_path, wrapu, wrapv );
-            _status[i].texture_loaded = true;
-        } else {
-            _status[i].texture_loaded = false;
-        }
+        _status[i].texture_loaded = false;
 
         osg::Material* material = new osg::Material;
-        material->setColorMode(osg::Material::DIFFUSE);
+        material->setColorMode(osg::Material::AMBIENT_AND_DIFFUSE);
         material->setAmbient(osg::Material::FRONT_AND_BACK, ambient.osg());
         material->setDiffuse(osg::Material::FRONT_AND_BACK, diffuse.osg());
         material->setSpecular(osg::Material::FRONT_AND_BACK, specular.osg());
         material->setEmission(osg::Material::FRONT_AND_BACK, emission.osg());
         material->setShininess(osg::Material::FRONT_AND_BACK, shininess );
         stateSet->setAttribute(material);
-//         stateSet->setMode(GL_COLOR_MATERIAL, osg::StateAttribute::ON);
+
+        if (ambient[3] < 1 || diffuse[3] < 1 ||
+            specular[3] < 1 || emission[3] < 1) {
+          stateSet->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
+          stateSet->setMode(GL_BLEND, osg::StateAttribute::ON);
+          stateSet->setMode(GL_ALPHA_TEST, osg::StateAttribute::ON);
+        } else {
+          stateSet->setRenderingHint(osg::StateSet::OPAQUE_BIN);
+          stateSet->setMode(GL_BLEND, osg::StateAttribute::OFF);
+          stateSet->setMode(GL_ALPHA_TEST, osg::StateAttribute::OFF);
+        }
 
         _status[i].state = stateSet;
     }
@@ -307,22 +303,13 @@ void SGMaterial::set_state( osg::StateSet *s )
 }
 
 void SGMaterial::assignTexture( osg::StateSet *state, const std::string &fname,
-                 int _wrapu, int _wrapv, int _mipmap )
+                 bool _wrapu, bool _wrapv, bool _mipmap )
 {
-   map<string, osg::ref_ptr<osg::Texture2D> >::iterator _tex_cache_iter;
-   _tex_cache_iter = _tex_cache.find(fname);
-   if (_tex_cache_iter == _tex_cache.end())
-   {
-      osg::Texture2D* texture = SGLoadTexture2D(fname, _wrapu, _wrapv,
-                                                mipmap ? -1 : 0);
-      state->setTextureAttributeAndModes(0, texture);
-      _tex_cache[fname] = texture;
-   }
-   else
-   {
-      state->setTextureAttributeAndModes(0, _tex_cache_iter->second.get());
-      // cout << "Cache hit: " << fname << endl;
-   }
+   osg::Texture2D* texture = SGLoadTexture2D(fname, 0, _wrapu, _wrapv,
+                                             mipmap ? -1 : 0);
+   texture->setMaxAnisotropy( SGGetTextureFilter());
+   state->setTextureAttributeAndModes(0, texture);
+
    osg::TexEnv* texEnv = new osg::TexEnv;
    texEnv->setMode(osg::TexEnv::MODULATE);
    state->setTextureAttributeAndModes(0, texEnv);
@@ -349,5 +336,12 @@ SGMaterialGlyph::SGMaterialGlyph(SGPropertyNode *p) :
 {
 }
 
+void
+SGSetTextureFilter( int max) {
+       SGSceneFeatures::instance()->setTextureFilter( max);
+}
 
-// end of mat.cxx
+int
+SGGetTextureFilter() {
+       return SGSceneFeatures::instance()->getTextureFilter();
+}