]> git.mxchange.org Git - simgear.git/commitdiff
materials use only simgear::Effect
authortimoore <timoore>
Wed, 15 Jul 2009 23:10:32 +0000 (23:10 +0000)
committerTim Moore <timoore@redhat.com>
Thu, 16 Jul 2009 10:09:44 +0000 (12:09 +0200)
Eliminate SGMaterial::get_state function.

Use Effect in BVH visitor, ocean tile generation, and airport signs.

simgear/scene/material/mat.cxx
simgear/scene/material/mat.hxx
simgear/scene/material/matlib.cxx
simgear/scene/material/matlib.hxx
simgear/scene/model/BoundingVolumeBuildVisitor.hxx
simgear/scene/tgdb/SGOceanTile.cxx
simgear/scene/tgdb/apt_signs.cxx

index dddec02dea7c86fc67382cc75949082699fd6b7c..7a0628f0dec1463e0fe1d7caf4428f5d9aa2b18b 100644 (file)
@@ -231,31 +231,6 @@ SGMaterial::init ()
     }
 }
 
-osg::StateSet *
-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();
-
-    _current_ptr += 1;
-    if (_current_ptr >= _status.size())
-        _current_ptr = 0;
-
-    return st;
-}
-
 Effect* SGMaterial::get_effect(int n)
 {
     if (_status.size() == 0) {
index faccaf6bfa3a22d0bb8af5c2cd4b6f76dc3ee85f..e041b6987a97dd8972c2f27eec44d30065a61265 100644 (file)
@@ -120,7 +120,6 @@ public:
   /**
    * Get the textured state.
    */
-  osg::StateSet *get_state (int n = -1);
   simgear::Effect *get_effect(int n = -1);
 
   /**
index 6f5d10bf40662332787fada77f71bacb07e158b2..4f1357707479c24d555c6437ca5485329402194f 100644 (file)
@@ -135,19 +135,15 @@ SGMaterialLib::~SGMaterialLib ( void ) {
 }
 
 const SGMaterial*
-SGMaterialLib::findMaterial(const osg::StateSet* stateSet)
+SGMaterialLib::findMaterial(const simgear::Effect* effect)
 {
-  if (!stateSet)
-    return 0;
-  
-  const osg::Referenced* base = stateSet->getUserData();
-  if (!base)
+  if (!effect)
     return 0;
 
   const SGMaterialUserData* matUserData
-    = dynamic_cast<const SGMaterialUserData*>(base);
+    = dynamic_cast<const SGMaterialUserData*>(effect->getUserData());
   if (!matUserData)
     return 0;
-
-  return matUserData->getMaterial();
+  else
+    return matUserData->getMaterial();
 }
index 145225b9132bfa2ba38478bfa08873ea81c66b24..373ae83d81bc2c0ec93fae467e8a795e59f4f2c9 100644 (file)
@@ -48,6 +48,11 @@ using std::map;
 using std::vector;
 using std::less;
 
+namespace simgear
+{
+class Effect;
+}
+
 // Material management class
 class SGMaterialLib {
 
@@ -77,7 +82,7 @@ public:
     material_map_iterator end() { return matlib.end(); }
     const_material_map_iterator end() const { return matlib.end(); }
 
-    static const SGMaterial* findMaterial(const osg::StateSet* stateSet);
+    static const SGMaterial* findMaterial(const simgear::Effect* effect);
 
     // Destructor
     ~SGMaterialLib ( void );
index 80587ff02d105fe32fafe138c56928e547d7c5a9..0489f82793f6810d3f5de1903d644fd220b79338 100644 (file)
@@ -27,6 +27,8 @@
 #include <osg/Transform>
 #include <osg/TriangleFunctor>
 
+#include <simgear/scene/material/Effect.hxx>
+#include <simgear/scene/material/EffectGeode.hxx>
 #include <simgear/scene/material/mat.hxx>
 #include <simgear/scene/material/matlib.hxx>
 #include <simgear/scene/util/SGNodeMasks.hxx>
@@ -384,10 +386,10 @@ public:
     {
     }
 
-    const SGMaterial* pushMaterial(osg::StateSet* stateSet)
+    const SGMaterial* pushMaterial(Effect* effect)
     {
         const SGMaterial* oldMaterial = _primitiveFunctor.getCurrentMaterial();
-        const SGMaterial* material = SGMaterialLib::findMaterial(stateSet);
+        const SGMaterial* material = SGMaterialLib::findMaterial(effect);
         if (material)
             _primitiveFunctor.setCurrentMaterial(material);
         return oldMaterial;
@@ -395,9 +397,7 @@ public:
 
     void fillWith(osg::Drawable* drawable)
     {
-        const SGMaterial* oldMaterial = pushMaterial(drawable->getStateSet());
         drawable->accept(_primitiveFunctor);
-        _primitiveFunctor.setCurrentMaterial(oldMaterial);
     }
 
     virtual void apply(osg::Geode& geode)
@@ -405,7 +405,11 @@ public:
         if (hasBoundingVolumeTree(geode))
             return;
 
-        const SGMaterial* oldMaterial = pushMaterial(geode.getStateSet());
+        const SGMaterial* oldMaterial = 0;
+
+        EffectGeode *eg = dynamic_cast<EffectGeode*>(&geode);
+        if (eg)
+            oldMaterial = pushMaterial(eg->getEffect());
 
         bool flushHere = getNodePath().size() <= 1 || _dumpIntoLeafs;
         if (flushHere) {
@@ -426,8 +430,8 @@ public:
             for(unsigned i = 0; i < geode.getNumDrawables(); ++i)
                 fillWith(geode.getDrawable(i));
         }
-
-        _primitiveFunctor.setCurrentMaterial(oldMaterial);
+        if (eg)
+            _primitiveFunctor.setCurrentMaterial(oldMaterial);
     }
 
     virtual void apply(osg::Group& group)
@@ -453,8 +457,6 @@ public:
         if (hasBoundingVolumeTree(node))
             return;
 
-        const SGMaterial* oldMaterial = pushMaterial(node.getStateSet());
-
         // push the current active primitive list
         PFunctor previousPrimitives;
         _primitiveFunctor.swap(previousPrimitives);
@@ -469,8 +471,6 @@ public:
 
         // pop the current active primitive list
         _primitiveFunctor.swap(previousPrimitives);
-
-        _primitiveFunctor.setCurrentMaterial(oldMaterial);
     }
 
     void traverseAndCollect(osg::Node& node)
@@ -488,12 +488,8 @@ public:
         // Note that we do not need to push the already collected list of
         // primitives, since we are now in the topmost node ...
 
-        const SGMaterial* oldMaterial = pushMaterial(node.getStateSet());
-
         // walk the children
         traverse(node);
-
-        _primitiveFunctor.setCurrentMaterial(oldMaterial);
     }
 
     void addBoundingVolumeTreeToNode(osg::Node& node)
index 0fcb16f8084037519c71b4ddde810680c1dc4300..9ff60f3e0c8729b84371968b5bef7ef0af3655fe 100644 (file)
 #include <simgear/math/sg_geodesy.hxx>
 #include <simgear/math/sg_types.hxx>
 #include <simgear/misc/texcoord.hxx>
+#include <simgear/scene/material/Effect.hxx>
+#include <simgear/scene/material/EffectGeode.hxx>
 #include <simgear/scene/material/mat.hxx>
 #include <simgear/scene/material/matlib.hxx>
+
 #include <simgear/scene/util/VectorArrayAdapter.hxx>
 
 using namespace simgear;
@@ -254,7 +257,7 @@ void fillDrawElementsWithApron(short height, short width,
 
 osg::Node* SGOceanTile(const SGBucket& b, SGMaterialLib *matlib)
 {
-    osg::StateSet *stateSet = 0;
+    Effect *effect = 0;
 
     double tex_width = 1000.0;
   
@@ -266,7 +269,7 @@ osg::Node* SGOceanTile(const SGBucket& b, SGMaterialLib *matlib)
         tex_width = mat->get_xsize();
     
         // set OSG State
-        stateSet = mat->get_state();
+        effect = mat->get_effect();
     } else {
         SG_LOG( SG_TERRAIN, SG_ALERT, "Ack! unknown use material name = Ocean");
     }
@@ -302,10 +305,10 @@ osg::Node* SGOceanTile(const SGBucket& b, SGMaterialLib *matlib)
                                            + 2 * (latPoints - 1)));
     fillDrawElementsWithApron(latPoints, lonPoints, drawElements->begin());
     geometry->addPrimitiveSet(drawElements);
-    geometry->setStateSet(stateSet);
 
-    osg::Geode* geode = new osg::Geode;
+    EffectGeode* geode = new EffectGeode;
     geode->setName("Ocean tile");
+    geode->setEffect(effect);
     geode->addDrawable(geometry);
 
     osg::MatrixTransform* transform = new osg::MatrixTransform;
index 4053dda16ff204b0a513d4af480ba9d48a1bd3dc..34d61c1213522547d28de0a8917fa6828b5892a1 100644 (file)
@@ -33,6 +33,8 @@
 
 #include <simgear/debug/logstream.hxx>
 #include <simgear/math/sg_types.hxx>
+#include <simgear/scene/material/Effect.hxx>
+#include <simgear/scene/material/EffectGeode.hxx>
 #include <simgear/scene/material/mat.hxx>
 #include <simgear/scene/material/matlib.hxx>
 
 #define RWY "OBJECT_RUNWAY_SIGN: "
 
 using std::vector;
+using namespace simgear;
 
 // for temporary storage of sign elements
 struct element_info {
-    element_info(SGMaterial *m, osg::StateSet *s, SGMaterialGlyph *g, double h)
+    element_info(SGMaterial *m, Effect *s, SGMaterialGlyph *g, double h)
         : material(m), state(s), glyph(g), height(h)
     {
         scale = h * m->get_xsize()
                 / (m->get_ysize() < 0.001 ? 1.0 : m->get_ysize());
     }
     SGMaterial *material;
-    osg::StateSet *state;
+    Effect *state;
     SGMaterialGlyph *glyph;
     double height;
     double scale;
@@ -104,8 +107,8 @@ SGMakeSign(SGMaterialLib *matlib, const string& path, const string& content)
     object->setName(content);
 
     SGMaterial *material;
-    osg::StateSet *lighted_state;
-    osg::StateSet *unlighted_state;
+    Effect *lighted_state;
+    Effect *unlighted_state;
 
     // Part I: parse & measure
     for (const char *s = content.data(); *s; s++) {
@@ -224,12 +227,12 @@ SGMakeSign(SGMaterialLib *matlib, const string& path, const string& content)
             }
 
             // set material states (lighted & unlighted)
-            lighted_state = material->get_state();
+            lighted_state = material->get_effect();
             string u = newmat + ".unlighted";
 
             SGMaterial *m = matlib->find(u);
             if (m) {
-                unlighted_state = m->get_state();
+                unlighted_state = m->get_effect();
             } else {
                 SG_LOG(SG_TERRAIN, SG_ALERT, SIGN "ignoring unknown material `" << u << '\'');
                 unlighted_state = lighted_state;
@@ -244,7 +247,7 @@ SGMakeSign(SGMaterialLib *matlib, const string& path, const string& content)
         }
 
         // in managed mode push frame stop and frame start first
-        osg::StateSet *state = lighted ? lighted_state : unlighted_state;
+        Effect *state = lighted ? lighted_state : unlighted_state;
         element_info *e;
         if (newtype && newtype != oldtype) {
             if (close) {
@@ -319,9 +322,9 @@ SGMakeSign(SGMaterialLib *matlib, const string& path, const string& content)
         geometry->setColorBinding(osg::Geometry::BIND_OVERALL);
         geometry->setTexCoordArray(0, tl);
         geometry->addPrimitiveSet(new osg::DrawArrays(GL_TRIANGLE_STRIP, 0, vl->size()));
-        osg::Geode* geode = new osg::Geode;
+        EffectGeode* geode = new EffectGeode;
         geode->addDrawable(geometry);
-        geode->setStateSet(element->state);
+        geode->setEffect(element->state);
 
         object->addChild(geode);
         hpos += abswidth;
@@ -344,11 +347,11 @@ SGMakeSign(SGMaterialLib *matlib, const string& path, const string& content)
     geometry->setNormalArray(nl);
     geometry->setNormalBinding(osg::Geometry::BIND_OVERALL);
     geometry->addPrimitiveSet(new osg::DrawArrays(GL_TRIANGLE_STRIP, 0, vl->size()));
-    osg::Geode* geode = new osg::Geode;
+    EffectGeode* geode = new EffectGeode;
     geode->addDrawable(geometry);
     SGMaterial *mat = matlib->find("BlackSign");
     if (mat)
-      geode->setStateSet(mat->get_state());
+      geode->setEffect(mat->get_effect());
     object->addChild(geode);
 
     return object;
@@ -367,14 +370,12 @@ SGMakeRunwaySign(SGMaterialLib *matlib, const string& path, const string& name)
     osg::Vec3 heightVec(0, 0, 1);
     osg::Geometry* geometry;
     geometry = osg::createTexturedQuadGeometry(corner, widthVec, heightVec);
-
-    SGMaterial *mat = matlib->find(name);
-    if (mat)
-      geometry->setStateSet(mat->get_state());
-
-    osg::Geode* geode = new osg::Geode;
+    EffectGeode* geode = new EffectGeode;
     geode->setName(name);
     geode->addDrawable(geometry);
+    SGMaterial *mat = matlib->find(name);
+    if (mat)
+      geode->setEffect(mat->get_effect());
 
     return geode;
 }