]> git.mxchange.org Git - simgear.git/blobdiff - simgear/scene/material/matlib.cxx
Random buildings - initial commit.
[simgear.git] / simgear / scene / material / matlib.cxx
index de8e6241c7fd780da74c550fa740074037db7d13..822147250f6ec1cb97a3747c0eb265302d11f092 100644 (file)
 #  include <simgear_config.h>
 #endif
 
-#if defined ( __CYGWIN__ )
-#include <ieeefp.h>
-#endif
-
 #include <simgear/compiler.h>
 #include <simgear/constants.h>
 #include <simgear/structure/exception.hxx>
 #include <string.h>
 #include <string>
 
-#include <osg/AlphaFunc>
-#include <osg/BlendFunc>
-#include <osg/CullFace>
-#include <osg/Material>
-#include <osg/Point>
-#include <osg/PointSprite>
-#include <osg/PolygonMode>
-#include <osg/PolygonOffset>
-#include <osg/StateSet>
-#include <osg/TexEnv>
-#include <osg/TexGen>
-#include <osg/Texture2D>
+#include <osgDB/Registry>
 
 #include <simgear/debug/logstream.hxx>
 #include <simgear/misc/sg_path.hxx>
 
 #include "mat.hxx"
 
+#include "Effect.hxx"
+#include "Technique.hxx"
 #include "matlib.hxx"
 
-SG_USING_STD(string);
+using std::string;
 
 // Constructor
 SGMaterialLib::SGMaterialLib ( void ) {
 }
 
 // Load a library of material properties
-bool SGMaterialLib::load( const string &fg_root, const string& mpath, const char *season,
+bool SGMaterialLib::load( const string &fg_root, const string& mpath,
         SGPropertyNode *prop_root )
 {
     SGPropertyNode materials;
@@ -80,30 +67,23 @@ bool SGMaterialLib::load( const string &fg_root, const string& mpath, const char
                 << ex.getMessage() );
         throw;
     }
-
+    osg::ref_ptr<osgDB::Options> options
+        = new osgDB::Options;
+    options->setObjectCacheHint(osgDB::Options::CACHE_ALL);
+    options->setDatabasePath(fg_root);
     int nMaterials = materials.nChildren();
     for (int i = 0; i < nMaterials; i++) {
         const SGPropertyNode *node = materials.getChild(i);
         if (!strcmp(node->getName(), "material")) {
-            const SGPropertyNode *conditionNode = node->getChild("condition");
-            if (conditionNode) {
-                SGSharedPtr<const SGCondition> condition = sgReadCondition(prop_root, conditionNode);
-                if (!condition->test()) {
-                    SG_LOG(SG_INPUT, SG_DEBUG, "Skipping material entry #"
-                        << i << " (condition false)");
-                    continue;
-                }
-            }
-
-            SGSharedPtr<SGMaterial> m = new SGMaterial(fg_root, node, season);
+            SGSharedPtr<SGMaterial> m = new SGMaterial(options.get(), node, prop_root);
 
             vector<SGPropertyNode_ptr>names = node->getChildren("name");
             for ( unsigned int j = 0; j < names.size(); j++ ) {
                 string name = names[j]->getStringValue();
                 // cerr << "Material " << name << endl;
-                matlib[name] = m;
+                matlib[name].push_back(m);
                 m->add_name(name);
-                SG_LOG( SG_TERRAIN, SG_INFO, "  Loading material "
+                SG_LOG( SG_TERRAIN, SG_DEBUG, "  Loading material "
                         << names[j]->getStringValue() );
             }
         } else {
@@ -115,55 +95,23 @@ bool SGMaterialLib::load( const string &fg_root, const string& mpath, const char
     return true;
 }
 
-
-// Load a library of material properties
-bool SGMaterialLib::add_item ( const string &tex_path )
-{
-    string material_name = tex_path;
-    int pos = tex_path.rfind( "/" );
-    material_name = material_name.substr( pos + 1 );
-
-    return add_item( material_name, tex_path );
-}
-
-
-// Load a library of material properties
-bool SGMaterialLib::add_item ( const string &mat_name, const string &full_path )
-{
-    int pos = full_path.rfind( "/" );
-    string tex_name = full_path.substr( pos + 1 );
-    string tex_path = full_path.substr( 0, pos );
-
-    SG_LOG( SG_TERRAIN, SG_INFO, "  Loading material " 
-           << mat_name << " (" << full_path << ")");
-
-    matlib[mat_name] = new SGMaterial( full_path );
-    matlib[mat_name]->add_name(mat_name);
-
-    return true;
-}
-
-
-// Load a library of material properties
-bool SGMaterialLib::add_item ( const string &mat_name, osg::StateSet *state )
-{
-    matlib[mat_name] = new SGMaterial( state );
-    matlib[mat_name]->add_name(mat_name);
-
-    SG_LOG( SG_TERRAIN, SG_INFO, "  Loading material given a premade "
-           << "osg::StateSet = " << mat_name );
-
-    return true;
-}
-
-
 // find a material record by material name
 SGMaterial *SGMaterialLib::find( const string& material ) {
     SGMaterial *result = NULL;
     material_map_iterator it = matlib.find( material );
-    if ( it != end() ) {
-       result = it->second;
-       return result;
+    if ( it != end() ) {            
+        // We now have a list of materials that match this
+        // name. Find the first one that either doesn't have
+        // a condition, or has a condition that evaluates
+        // to true.
+        material_list_iterator iter = it->second.begin();        
+        while (iter != it->second.end()) {            
+            result = *iter;
+            if (result->valid()) {
+                return result;
+            }
+            iter++;
+        }
     }
 
     return NULL;
@@ -174,20 +122,20 @@ SGMaterialLib::~SGMaterialLib ( void ) {
     SG_LOG( SG_GENERAL, SG_INFO, "SGMaterialLib::~SGMaterialLib() size=" << matlib.size());
 }
 
-const SGMaterial*
-SGMaterialLib::findMaterial(const osg::StateSet* stateSet) const
+const SGMaterial *SGMaterialLib::findMaterial(const osg::Geode* geode)
 {
-  if (!stateSet)
-    return 0;
-  
-  const osg::Referenced* base = stateSet->getUserData();
-  if (!base)
-    return 0;
-
-  const SGMaterialUserData* matUserData
-    = dynamic_cast<const SGMaterialUserData*>(base);
-  if (!matUserData)
-    return 0;
-
-  return matUserData->getMaterial();
+    if (!geode)
+        return 0;
+    const simgear::EffectGeode* effectGeode;
+    effectGeode = dynamic_cast<const simgear::EffectGeode*>(geode);
+    if (!effectGeode)
+        return 0;
+    const simgear::Effect* effect = effectGeode->getEffect();
+    if (!effect)
+        return 0;
+    const SGMaterialUserData* userData;
+    userData = dynamic_cast<const SGMaterialUserData*>(effect->getUserData());
+    if (!userData)
+        return 0;
+    return userData->getMaterial();
 }