]> git.mxchange.org Git - simgear.git/blobdiff - simgear/scene/tgdb/apt_signs.cxx
Phase 1 - single geometry per material
[simgear.git] / simgear / scene / tgdb / apt_signs.cxx
index d76622a6c397f9fec1752e9d666fca12a325da99..8341005189fb7f0940c829b99ae3f26748c2741d 100644 (file)
 #endif
 
 #include <vector>
+#include <boost/foreach.hpp>
 
-#include <osg/StateSet>
 #include <osg/Geode>
 #include <osg/Geometry>
 #include <osg/Group>
+#include <osg/MatrixTransform>
+#include <osg/StateSet>
 
 #include <simgear/debug/logstream.hxx>
 #include <simgear/math/sg_types.hxx>
 #include "apt_signs.hxx"
 
 #define SIGN "OBJECT_SIGN: "
-#define RWY "OBJECT_RUNWAY_SIGN: "
 
 using std::vector;
 using namespace simgear;
 
 // for temporary storage of sign elements
 struct element_info {
-    element_info(SGMaterial *m, Effect *s, SGMaterialGlyph *g, double h)
-        : material(m), state(s), glyph(g), height(h)
+    element_info(SGMaterial *m, Effect *s, SGMaterialGlyph *g, double h, double c)
+        : material(m), state(s), glyph(g), height(h), coverwidth(c)
     {
         scale = h * m->get_xsize()
                 / (m->get_ysize() < 0.001 ? 1.0 : m->get_ysize());
+        abswidth = c == 0 ? g->get_width() * scale : c;
     }
     SGMaterial *material;
     Effect *state;
     SGMaterialGlyph *glyph;
     double height;
     double scale;
+    double abswidth;
+    double coverwidth;
 };
 
+typedef std::vector<element_info*> ElementVec;
+
 
-// standard panel height sizes
-const double HT[5] = {0.460, 0.610, 0.760, 1.220, 0.760};
+
+
+const double HT[5] = {0.460, 0.610, 0.760, 1.220, 0.760}; // standard panel height sizes
+const double grounddist = 0.2;     // hard-code sign distance from surface for now
+const double thick = 0.1;    // half the thickness of the 3D sign
 
 
 // translation table for "command" to "glyph name"
@@ -86,29 +95,139 @@ struct pair {
     {0, 0},
 };
 
+struct GlyphGeometry
+{
+  osg::DrawArrays* quads;
+  osg::Vec2Array* uvs;
+  osg::Vec3Array* vertices;
+  osg::Vec3Array* normals;
+  
+  void addGlyph(SGMaterialGlyph* glyph, double x, double y, double width, double height, const osg::Matrix& xform)
+  {
+    
+    vertices->push_back(xform.preMult(osg::Vec3(thick, x, y)));
+    vertices->push_back(xform.preMult(osg::Vec3(thick, x + width, y)));
+    vertices->push_back(xform.preMult(osg::Vec3(thick, x + width, y + height)));
+    vertices->push_back(xform.preMult(osg::Vec3(thick, x, y + height)));
+    
+    // texture coordinates
+    double xoffset = glyph->get_left();
+    double texWidth = glyph->get_width();
+    
+    uvs->push_back(osg::Vec2(xoffset,         0));
+    uvs->push_back(osg::Vec2(xoffset + texWidth, 0));
+    uvs->push_back(osg::Vec2(xoffset + texWidth, 1));
+    uvs->push_back(osg::Vec2(xoffset,         1));
+
+    // normals
+    for (int i=0; i<4; ++i)
+      normals->push_back(xform.preMult(osg::Vec3(0, -1, 0)));
+    
+    quads->setCount(vertices->size());
+  }
+  
+  void addSignCase(double caseWidth, double caseHeight, const osg::Matrix& xform)
+  {
+    vertices->push_back(osg::Vec3(-thick, -caseWidth,  grounddist));
+    vertices->push_back(osg::Vec3(thick, -caseWidth,  grounddist));
+    vertices->push_back(osg::Vec3(thick, -caseWidth,  grounddist + caseHeight));
+    vertices->push_back(osg::Vec3(-thick, -caseWidth,  grounddist + caseHeight));
+    
+    
+    for (int i=0; i<4; ++i)
+      normals->push_back(xform.preMult(osg::Vec3(-1, 0.0, 0)));
+    
+    vertices->push_back(osg::Vec3(-thick, -caseWidth,  grounddist + caseHeight));
+    vertices->push_back(osg::Vec3(thick,  -caseWidth,  grounddist + caseHeight));
+    vertices->push_back(osg::Vec3(thick,  caseWidth, grounddist + caseHeight));
+    vertices->push_back(osg::Vec3(-thick, caseWidth, grounddist + caseHeight));
+    
+    for (int i=0; i<4; ++i)
+      normals->push_back(xform.preMult(osg::Vec3(0, 0, 1)));
+    
+    vertices->push_back(osg::Vec3(-thick, caseWidth, grounddist + caseHeight));
+    vertices->push_back(osg::Vec3(thick,  caseWidth, grounddist + caseHeight));
+    vertices->push_back(osg::Vec3(thick,  caseWidth, grounddist));
+    vertices->push_back(osg::Vec3(-thick, caseWidth, grounddist));
+    
+    for (int i=0; i<4; ++i)
+      normals->push_back(xform.preMult(osg::Vec3(1, 0.0, 0)));
+    
+    for (int i = 0; i < 3; ++i) {
+      uvs->push_back(osg::Vec2(1,    1));
+      uvs->push_back(osg::Vec2(0.75, 1));
+      uvs->push_back(osg::Vec2(0.75, 0));
+      uvs->push_back(osg::Vec2(1,    0));
+    }
+    
+    quads->setCount(vertices->size());
+  }
+};
+
+typedef std::map<Effect*, GlyphGeometry*> EffectGeometryMap;
+
+void SGMakeSignFace(EffectGeometryMap& glyphs, const ElementVec& elements, double hpos, const osg::Matrix& xform)
+{
+    BOOST_FOREACH(element_info* element, elements) {
+        GlyphGeometry* gg = glyphs[element->state];
+        gg->addGlyph(element->glyph, hpos, grounddist, element->abswidth, element->height, xform);
+        hpos += element->abswidth;
+        delete element;
+    }
+}
+
+GlyphGeometry* makeGeometry(Effect* eff, osg::Group* group)
+{
+  GlyphGeometry* gg = new GlyphGeometry;
+  
+  EffectGeode* geode = new EffectGeode;
+  geode->setEffect(eff);
+  gg->vertices = new osg::Vec3Array;
+  gg->normals = new osg::Vec3Array;
+  gg->uvs = new osg::Vec2Array;
+  
+  osg::Vec4Array* cl = new osg::Vec4Array;
+  cl->push_back(osg::Vec4(1, 1, 1, 1));
+  
+  osg::Geometry* geometry = new osg::Geometry;
+  geometry->setVertexArray(gg->vertices);
+  geometry->setNormalArray(gg->normals);
+  geometry->setNormalBinding(osg::Geometry::BIND_PER_VERTEX);
+  geometry->setColorArray(cl);
+  geometry->setColorBinding(osg::Geometry::BIND_OVERALL);
+  geometry->setTexCoordArray(0, gg->uvs);
+  
+  gg->quads = new osg::DrawArrays(GL_QUADS, 0, gg->vertices->size());
+  geometry->addPrimitiveSet(gg->quads);
+  geode->addDrawable(geometry);
+  group->addChild(geode);
+  return gg;
+}
 
 // see $FG_ROOT/Docs/README.scenery
 //
 osg::Node*
-SGMakeSign(SGMaterialLib *matlib, const string& path, const string& content)
+SGMakeSign(SGMaterialLib *matlib, const string& content)
 {
     double sign_height = 1.0;  // meter
-    bool lighted = true;
     string newmat = "BlackSign";
-
-    vector<element_info *> elements;
-    element_info *close = 0;
-    double total_width = 0.0;
+    ElementVec elements1, elements2;
+    element_info *close1 = 0;
+    element_info *close2 = 0;
+    double total_width1 = 0.0;
+    double total_width2 = 0.0;
     bool cmd = false;
+    bool isBackside = false;
     int size = -1;
     char oldtype = 0, newtype = 0;
 
+    EffectGeometryMap geoms;
+  
     osg::Group* object = new osg::Group;
     object->setName(content);
 
     SGMaterial *material = 0;
-    Effect *lighted_state = 0;
-    Effect *unlighted_state = 0;
 
     // Part I: parse & measure
     for (const char *s = content.data(); *s; s++) {
@@ -117,13 +236,13 @@ SGMakeSign(SGMaterialLib *matlib, const string& path, const string& content)
 
         if (*s == '{') {
             if (cmd)
-                SG_LOG(SG_TERRAIN, SG_ALERT, SIGN "unexpected { in sign contents");
+                SG_LOG(SG_TERRAIN, SG_ALERT, SIGN "Illegal taxiway sign syntax. Unexpected '{' in '" << content << "'.");
             cmd = true;
             continue;
 
         } else if (*s == '}') {
             if (!cmd)
-                SG_LOG(SG_TERRAIN, SG_ALERT, SIGN "unexpected } in sign contents");
+                SG_LOG(SG_TERRAIN, SG_ALERT, SIGN "Illegal taxiway sign syntax. Unexpected '}' in '" << content << "'.");
             cmd = false;
             continue;
 
@@ -151,27 +270,22 @@ SGMakeSign(SGMaterialLib *matlib, const string& path, const string& content)
                     SG_LOG(SG_TERRAIN, SG_ALERT, SIGN "unclosed { in sign contents");
             }
 
-            if (name == "@size") {
-                sign_height = strtod(value.data(), 0);
+            if (name == "@@") {
+                isBackside = true;
                 continue;
             }
 
-            if (name == "@light") {
-                lighted = (value != "0" && value != "no" && value != "off" && value != "false");
+            if (name == "@size") {
+                sign_height = strtod(value.data(), 0);
                 continue;
             }
 
-            if (name == "@material") {
-                newmat = value.data();
-                continue;
-
-            } else if (name.size() == 2 || name.size() == 3) {
+            if (name.size() == 2 || name.size() == 3) {
                 string n = name;
                 if (n.size() == 3 && n[2] >= '1' && n[2] <= '5') {
                     size = n[2] - '1';
                     n = n.substr(0, 2);
                 }
-
                 if (n == "@Y") {
                     if (size < 3) {
                         sign_height = HT[size < 0 ? 2 : size];
@@ -220,174 +334,117 @@ SGMakeSign(SGMaterialLib *matlib, const string& path, const string& content)
         }
 
         if (newmat.size()) {
-            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 << '\'');
-            } else {
-                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;
-                }
-            }
+            material = matlib->find(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 << '\'');
+            SG_LOG( SG_TERRAIN, SG_ALERT, SIGN "unsupported glyph '" << *s << '\'');
             continue;
         }
 
         // in managed mode push frame stop and frame start first
-        Effect *state = lighted ? lighted_state : unlighted_state;
-        element_info *e;
-        if (newtype && newtype != oldtype) {
-            if (close) {
-                elements.push_back(close);
-                total_width += close->glyph->get_width() * close->scale;
-                close = 0;
+        Effect *state = material->get_effect();
+        if (geoms[state] == NULL) {
+          geoms[state] = makeGeometry(state, object);
+        }
+      
+        element_info *e1;
+        element_info *e2;
+        if (!isBackside) {
+            if (newtype && newtype != oldtype) {
+                if (close1) {
+                    elements1.push_back(close1);
+                    total_width1 += close1->glyph->get_width() * close1->scale;
+                    close1 = 0;
+                }
+                oldtype = newtype;
+                SGMaterialGlyph *g = material->get_glyph("stop-frame");
+                if (g)
+                    close1 = new element_info(material, state, g, sign_height, 0);
+                g = material->get_glyph("start-frame");
+                if (g) {
+                    e1 = new element_info(material, state, g, sign_height, 0);
+                    elements1.push_back(e1);
+                    total_width1 += e1->glyph->get_width() * e1->scale;
+                }
             }
-            oldtype = newtype;
-            SGMaterialGlyph *g = material->get_glyph("stop-frame");
-            if (g)
-                close = new element_info(material, state, g, sign_height);
-            g = material->get_glyph("start-frame");
-            if (g) {
-                e = new element_info(material, state, g, sign_height);
-                elements.push_back(e);
-                total_width += e->glyph->get_width() * e->scale;
+            // now the actually requested glyph (front)
+            e1 = new element_info(material, state, glyph, sign_height, 0);
+            elements1.push_back(e1);
+            total_width1 += e1->glyph->get_width() * e1->scale;
+        } else {
+            if (newtype && newtype != oldtype) {
+                if (close2) {
+                    elements2.push_back(close2);
+                    total_width2 += close2->glyph->get_width() * close2->scale;
+                    close2 = 0;
+                }
+                oldtype = newtype;
+                SGMaterialGlyph *g = material->get_glyph("stop-frame");
+                if (g)
+                    close2 = new element_info(material, state, g, sign_height, 0);
+                g = material->get_glyph("start-frame");
+                if (g) {
+                    e2 = new element_info(material, state, g, sign_height, 0);
+                    elements2.push_back(e2);
+                    total_width2 += e2->glyph->get_width() * e2->scale;
+                }
             }
+            // now the actually requested glyph (back)
+            e2 = new element_info(material, state, glyph, sign_height, 0);
+            elements2.push_back(e2);
+            total_width2 += e2->glyph->get_width() * e2->scale;
         }
-        // now the actually requested glyph
-        e = new element_info(material, state, glyph, sign_height);
-        elements.push_back(e);
-        total_width += e->glyph->get_width() * e->scale;
     }
 
     // in managed mode close frame
-    if (close) {
-        elements.push_back(close);
-        total_width += close->glyph->get_width() * close->scale;
-        close = 0;
+    if (close1) {
+        elements1.push_back(close1);
+        total_width1 += close1->glyph->get_width() * close1->scale;
+        close1 = 0;
     }
 
-
-    // Part II: typeset
-    double hpos = -total_width / 2;
-    const double dist = 0.25;        // hard-code distance from surface for now
-
-    for (unsigned int i = 0; i < elements.size(); i++) {
-        element_info *element = elements[i];
-
-        double xoffset = element->glyph->get_left();
-        double height = element->height;
-        double width = element->glyph->get_width();
-        double abswidth = width * element->scale;
-
-        // vertices
-        osg::Vec3Array* vl = new osg::Vec3Array;
-        vl->push_back(osg::Vec3(0, hpos,            dist));
-        vl->push_back(osg::Vec3(0, hpos + abswidth, dist));
-        vl->push_back(osg::Vec3(0, hpos,            dist + height));
-        vl->push_back(osg::Vec3(0, hpos + abswidth, dist + height));
-
-        // texture coordinates
-        osg::Vec2Array* tl = new osg::Vec2Array;
-        tl->push_back(osg::Vec2(xoffset,         0));
-        tl->push_back(osg::Vec2(xoffset + width, 0));
-        tl->push_back(osg::Vec2(xoffset,         1));
-        tl->push_back(osg::Vec2(xoffset + width, 1));
-
-        // normals
-        osg::Vec3Array* nl = new osg::Vec3Array;
-        nl->push_back(osg::Vec3(0, -1, 0));
-
-        // colors
-        osg::Vec4Array* cl = new osg::Vec4Array;
-        cl->push_back(osg::Vec4(1, 1, 1, 1));
-
-        osg::Geometry* geometry = new osg::Geometry;
-        geometry->setVertexArray(vl);
-        geometry->setNormalArray(nl);
-        geometry->setNormalBinding(osg::Geometry::BIND_OVERALL);
-        geometry->setColorArray(cl);
-        geometry->setColorBinding(osg::Geometry::BIND_OVERALL);
-        geometry->setTexCoordArray(0, tl);
-        geometry->addPrimitiveSet(new osg::DrawArrays(GL_TRIANGLE_STRIP, 0, vl->size()));
-        EffectGeode* geode = new EffectGeode;
-        geode->addDrawable(geometry);
-        geode->setEffect(element->state);
-
-        object->addChild(geode);
-        hpos += abswidth;
-        delete element;
+    if (close2) {
+        elements2.push_back(close2);
+        total_width2 += close2->glyph->get_width() * close2->scale;
+        close2 = 0;
     }
 
 
-    // minimalistic backside
-    osg::Vec3Array* vl = new osg::Vec3Array;
-    vl->push_back(osg::Vec3(0, hpos,               dist));
-    vl->push_back(osg::Vec3(0, hpos - total_width, dist));
-    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()));
-    EffectGeode* geode = new EffectGeode;
-    geode->addDrawable(geometry);
-    SGMaterial *mat = matlib->find("BlackSign");
-    if (mat)
-      geode->setEffect(mat->get_effect());
-    object->addChild(geode);
+  // Part II: typeset
+    double boxwidth = std::max(total_width1, total_width2) * 0.5;
+    double hpos = -boxwidth;
+    SGMaterial *mat = matlib->find("signcase");
+    geoms[mat->get_effect()] = makeGeometry(mat->get_effect(), object);
+  
+    double coverSize = fabs(total_width1 - total_width2) * 0.5;
+  
+    if (total_width1 < total_width2) {
+        if (mat) {
+            element_info* s1 = new element_info(mat, mat->get_effect(), mat->get_glyph("cover"), sign_height, coverSize);
+            element_info* s2 = new element_info(mat, mat->get_effect(), mat->get_glyph("cover"), sign_height, coverSize);
+            elements1.insert(elements1.begin(), s1);
+            elements1.push_back(s2);
+        }
 
-    return object;
-}
+    } else if (total_width2 < total_width1) {
+        if (mat) {
+            element_info* s1 = new element_info(mat, mat->get_effect(), mat->get_glyph("cover"), sign_height, coverSize);
+            element_info* s2 = new element_info(mat, mat->get_effect(), mat->get_glyph("cover"), sign_height, coverSize);
+            elements2.insert(elements2.begin(), s1);
+            elements2.push_back(s2);
+        }
+    }
 
-osg::Node*
-SGMakeRunwaySign(SGMaterialLib *matlib, const string& path, const string& name)
-{
-    // for demo purposes we assume each element (letter) is 1x1 meter.
-    // Sign is placed 0.25 meters above the ground
-
-    float width = name.length() / 3.0;
-
-    osg::Vec3 corner(-width, 0, 0.25f);
-    osg::Vec3 widthVec(2*width + 1, 0, 0);
-    osg::Vec3 heightVec(0, 0, 1);
-    osg::Geometry* geometry;
-    geometry = osg::createTexturedQuadGeometry(corner, widthVec, heightVec);
-    EffectGeode* geode = new EffectGeode;
-    geode->setName(name);
-    geode->addDrawable(geometry);
-    SGMaterial *mat = matlib->find(name);
-    if (mat)
-      geode->setEffect(mat->get_effect());
-
-    return geode;
+    // Create front side
+    SGMakeSignFace(geoms, elements1, hpos, osg::Matrix::identity());
+
+    // Create back side
+    const osg::Vec3d axis(0, 0, 1);
+    SGMakeSignFace(geoms, elements2, hpos, osg::Matrix::rotate( M_PI, axis));
+    geoms[mat->get_effect()]->addSignCase(boxwidth, sign_height, osg::Matrix::identity());
+  
+    return object;
 }