]> git.mxchange.org Git - simgear.git/blobdiff - simgear/scene/tgdb/apt_signs.cxx
Merge branch 'timoore/aptsign' into next
[simgear.git] / simgear / scene / tgdb / apt_signs.cxx
index 97fef252208a1628745f34c5ef15cdc18d250553..d76622a6c397f9fec1752e9d666fca12a325da99 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 SIGN "OBJECT_SIGN: "
 #define RWY "OBJECT_RUNWAY_SIGN: "
 
-SG_USING_STD(vector);
+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;
@@ -68,18 +71,18 @@ struct pair {
     const char *keyword;
     const char *glyph_name;
 } cmds[] = {
-    {"@u",       "up"},
-    {"@d",       "down"},
-    {"@l",       "left"},
-    {"@lu",      "left-up"},
-    {"@ul",      "left-up"},
-    {"@ld",      "left-down"},
-    {"@dl",      "left-down"},
-    {"@r",       "right"},
-    {"@ru",      "right-up"},
-    {"@ur",      "right-up"},
-    {"@rd",      "right-down"},
-    {"@dr",      "right-down"},
+    {"@u",       "^u"},
+    {"@d",       "^d"},
+    {"@l",       "^l"},
+    {"@lu",      "^lu"},
+    {"@ul",      "^lu"},
+    {"@ld",      "^ld"},
+    {"@dl",      "^ld"},
+    {"@r",       "^r"},
+    {"@ru",      "^ru"},
+    {"@ur",      "^ru"},
+    {"@rd",      "^rd"},
+    {"@dr",      "^rd"},
     {0, 0},
 };
 
@@ -103,9 +106,9 @@ SGMakeSign(SGMaterialLib *matlib, const string& path, const string& content)
     osg::Group* object = new osg::Group;
     object->setName(content);
 
-    SGMaterial *material;
-    osg::StateSet *lighted_state;
-    osg::StateSet *unlighted_state;
+    SGMaterial *material = 0;
+    Effect *lighted_state = 0;
+    Effect *unlighted_state = 0;
 
     // Part I: parse & measure
     for (const char *s = content.data(); *s; s++) {
@@ -217,26 +220,31 @@ SGMakeSign(SGMaterialLib *matlib, const string& path, const string& content)
         }
 
         if (newmat.size()) {
-            material = matlib->find(newmat);
-            if (!material) {
+            SGMaterial *m = matlib->find(newmat);
+            if (!m) {
+                // log error, but keep using previous material to at least show something
                 SG_LOG(SG_TERRAIN, SG_ALERT, SIGN "ignoring unknown material `" << newmat << '\'');
-                continue;
-            }
-
-            // set material states (lighted & unlighted)
-            lighted_state = material->get_state();
-            string u = newmat + ".unlighted";
-
-            SGMaterial *m = matlib->find(u);
-            if (m) {
-                unlighted_state = m->get_state();
             } else {
-                SG_LOG(SG_TERRAIN, SG_ALERT, SIGN "ignoring unknown material `" << u << '\'');
-                unlighted_state = lighted_state;
+                material = m;
+                // set material states (lighted & unlighted)
+                lighted_state = material->get_effect();
+                newmat.append(".unlighted");
+
+                m = matlib->find(newmat);
+                if (m) {
+                    unlighted_state = m->get_effect();
+                } else {
+                    SG_LOG(SG_TERRAIN, SG_ALERT, SIGN "ignoring unknown material `" << newmat << '\'');
+                    unlighted_state = lighted_state;
+                }
             }
-            newmat = "";
+            newmat.clear();
         }
 
+        // This can only happen if the default material is missing.
+        // Error has been already logged in the block above.
+        if (!material) continue;
         SGMaterialGlyph *glyph = material->get_glyph(name);
         if (!glyph) {
             SG_LOG( SG_TERRAIN, SG_ALERT, SIGN "unsupported glyph `" << *s << '\'');
@@ -244,7 +252,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 +327,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;
@@ -336,19 +344,26 @@ SGMakeSign(SGMaterialLib *matlib, const string& path, const string& content)
     vl->push_back(osg::Vec3(0, hpos,               dist + sign_height));
     vl->push_back(osg::Vec3(0, hpos - total_width, dist + sign_height));
 
+    osg::Vec2Array* tl = new osg::Vec2Array;
+    for (int i = 0; i < 4; ++i)
+        tl->push_back(osg::Vec2(0.0, 0.0));
     osg::Vec3Array* nl = new osg::Vec3Array;
     nl->push_back(osg::Vec3(0, 1, 0));
-
+    osg::Vec4Array* cl = new osg::Vec4Array;
+    cl->push_back(osg::Vec4(0.0, 0.0, 0.0, 1.0));
     osg::Geometry* geometry = new osg::Geometry;
     geometry->setVertexArray(vl);
     geometry->setNormalArray(nl);
     geometry->setNormalBinding(osg::Geometry::BIND_OVERALL);
+    geometry->setTexCoordArray(0, tl);
+    geometry->setColorArray(cl);
+    geometry->setColorBinding(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 +382,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;
 }