X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=simgear%2Fscene%2Ftgdb%2Fapt_signs.cxx;h=d76622a6c397f9fec1752e9d666fca12a325da99;hb=284a9e061280535de01b0e3f36a9b85ef8178e61;hp=488e470b4fc913f06242fcc21dca1fc1d91d0478;hpb=4f0b1e847c0d0cc28d919a912ff8e3488e4ae81e;p=simgear.git diff --git a/simgear/scene/tgdb/apt_signs.cxx b/simgear/scene/tgdb/apt_signs.cxx index 488e470b..d76622a6 100644 --- a/simgear/scene/tgdb/apt_signs.cxx +++ b/simgear/scene/tgdb/apt_signs.cxx @@ -24,31 +24,41 @@ # include #endif +#include + +#include +#include +#include +#include + #include #include -#include +#include +#include #include #include #include "apt_signs.hxx" -#define TAXI "OBJECT_TAXI_SIGN: " +#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, SGMaterialGlyph *g, double h, bool l) - : material(m), glyph(g), height(h), lighted(l) + 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; + Effect *state; SGMaterialGlyph *glyph; double height; double scale; - bool lighted; }; @@ -61,64 +71,59 @@ 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}, }; -// Y ... black on yellow -> direction, destination, boundary -// R ... white on red -> mandatory instruction -// L ... yellow on black with frame -> runway/taxiway location -// B ... white on black -> runway distance remaining -// -// u/d/l/r ... up/down/left/right arrow or two-letter combinations -// thereof (ur, dl, ...) +// see $FG_ROOT/Docs/README.scenery // -// Example: {l}E|{L}[T]|{Y,ur}L|E{r} -// -ssgBranch *sgMakeTaxiSign( SGMaterialLib *matlib, - const string path, const string content ) +osg::Node* +SGMakeSign(SGMaterialLib *matlib, const string& path, const string& content) { double sign_height = 1.0; // meter bool lighted = true; + string newmat = "BlackSign"; vector elements; element_info *close = 0; double total_width = 0.0; bool cmd = false; - char *newmat = "BlackSign"; int size = -1; char oldtype = 0, newtype = 0; - ssgBranch *object = new ssgBranch(); - object->setName((char *)content.c_str()); - SGMaterial *material = matlib->find(newmat); + 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++) { string name; - const char *newmat = 0; + string value; if (*s == '{') { if (cmd) - SG_LOG(SG_TERRAIN, SG_ALERT, TAXI "unexpected { in sign contents"); + SG_LOG(SG_TERRAIN, SG_ALERT, SIGN "unexpected { in sign contents"); cmd = true; continue; } else if (*s == '}') { if (!cmd) - SG_LOG(SG_TERRAIN, SG_ALERT, TAXI "unexpected } in sign contents"); + SG_LOG(SG_TERRAIN, SG_ALERT, SIGN "unexpected } in sign contents"); cmd = false; continue; @@ -129,14 +134,13 @@ ssgBranch *sgMakeTaxiSign( SGMaterialLib *matlib, if (*s == ',') continue; - string value; for (; *s; s++) { name += *s; if (s[1] == ',' || s[1] == '}' || s[1] == '=') break; } if (!*s) { - SG_LOG(SG_TERRAIN, SG_ALERT, TAXI "unclosed { in sign contents"); + SG_LOG(SG_TERRAIN, SG_ALERT, SIGN "unclosed { in sign contents"); } else if (s[1] == '=') { for (s += 2; *s; s++) { value += *s; @@ -144,7 +148,7 @@ ssgBranch *sgMakeTaxiSign( SGMaterialLib *matlib, break; } if (!*s) - SG_LOG(SG_TERRAIN, SG_ALERT, TAXI "unclosed { in sign contents"); + SG_LOG(SG_TERRAIN, SG_ALERT, SIGN "unclosed { in sign contents"); } if (name == "@size") { @@ -157,46 +161,51 @@ ssgBranch *sgMakeTaxiSign( SGMaterialLib *matlib, continue; } - if (name == "@material") + if (name == "@material") { newmat = value.data(); - else if (name.size() == 2 || name.size() == 3) { - if (name.size() == 3 && name[2] >= '1' && name[2] <= '5') { - size = name[2] - '1'; - name = name.substr(0, 2); + continue; + + } else 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 (name == "@Y") { + if (n == "@Y") { if (size < 3) { sign_height = HT[size < 0 ? 2 : size]; newmat = "YellowSign"; newtype = 'Y'; + continue; } - } else if (name == "@R") { + + } else if (n == "@R") { if (size < 3) { sign_height = HT[size < 0 ? 2 : size]; newmat = "RedSign"; newtype = 'R'; + continue; } - } else if (name == "@L") { + + } else if (n == "@L") { if (size < 3) { sign_height = HT[size < 0 ? 2 : size]; newmat = "FramedSign"; newtype = 'L'; + continue; } - } else if (name == "@B") { + + } else if (n == "@B") { if (size < 0 || size == 3 || size == 4) { sign_height = HT[size < 0 ? 3 : size]; newmat = "BlackSign"; newtype = 'B'; + continue; } } } - if (newmat) { - material = matlib->find(newmat); - continue; - } - for (int i = 0; cmds[i].keyword; i++) { if (name == cmds[i].keyword) { name = cmds[i].glyph_name; @@ -205,23 +214,45 @@ ssgBranch *sgMakeTaxiSign( SGMaterialLib *matlib, } if (name[0] == '@') { - SG_LOG(SG_TERRAIN, SG_ALERT, TAXI "ignoring unknown command `" << name << '\''); + SG_LOG(SG_TERRAIN, SG_ALERT, SIGN "ignoring unknown command `" << name << '\''); continue; } } - if (!material) { - SG_LOG( SG_TERRAIN, SG_ALERT, TAXI "material doesn't exit"); - continue; + 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; + } + } + 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, TAXI "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) { @@ -232,16 +263,16 @@ ssgBranch *sgMakeTaxiSign( SGMaterialLib *matlib, oldtype = newtype; SGMaterialGlyph *g = material->get_glyph("stop-frame"); if (g) - close = new element_info(material, g, sign_height, lighted); + close = new element_info(material, state, g, sign_height); g = material->get_glyph("start-frame"); if (g) { - e = new element_info(material, g, sign_height, lighted); + 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 - e = new element_info(material, glyph, sign_height, lighted); + e = new element_info(material, state, glyph, sign_height); elements.push_back(e); total_width += e->glyph->get_width() * e->scale; } @@ -256,12 +287,7 @@ ssgBranch *sgMakeTaxiSign( SGMaterialLib *matlib, // Part II: typeset double hpos = -total_width / 2; - - sgVec3 normal; - sgSetVec3(normal, 0, -1, 0); - - sgVec4 color; - sgSetVec4(color, 1.0, 1.0, 1.0, 1.0); + 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]; @@ -272,101 +298,96 @@ ssgBranch *sgMakeTaxiSign( SGMaterialLib *matlib, double abswidth = width * element->scale; // vertices - ssgVertexArray *vl = new ssgVertexArray(4); - vl->add(hpos, 0, 0); - vl->add(hpos + abswidth, 0, 0); - vl->add(hpos, 0, height); - vl->add(hpos + abswidth, 0, height); + 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 - ssgTexCoordArray *tl = new ssgTexCoordArray(4); - tl->add(xoffset, 0); - tl->add(xoffset + width, 0); - tl->add(xoffset, 1); - tl->add(xoffset + width, 1); + 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 - ssgNormalArray *nl = new ssgNormalArray(1); - nl->add(normal); + osg::Vec3Array* nl = new osg::Vec3Array; + nl->push_back(osg::Vec3(0, -1, 0)); // colors - ssgColourArray *cl = new ssgColourArray(1); - cl->add(color); - - ssgLeaf *leaf = new ssgVtxTable(GL_TRIANGLE_STRIP, vl, nl, tl, cl); - ssgSimpleState *state = element->material->get_state(); - if (!element->lighted) { - // FIXME: clone state for unlighted elements only once per material - state = (ssgSimpleState *)state->clone(SSG_CLONE_STATE); - state->setMaterial(GL_EMISSION, 0.3, 0.3, 0.3, 1); - } - leaf->setState(state); - - object->addKid(leaf); + 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; } - return object; -} - - + // 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); + return object; +} -ssgBranch *sgMakeRunwaySign( SGMaterialLib *matlib, - const string path, const string name ) +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 - ssgBranch *object = new ssgBranch(); - object->setName( (char *)name.c_str() ); - - double width = name.length() / 3.0; - - string material = name; - - point_list nodes; - point_list normals; - point_list texcoords; - int_list vertex_index; - int_list normal_index; - int_list tex_index; - - nodes.push_back( Point3D( -width, 0, 0.25 ) ); - nodes.push_back( Point3D( width + 1, 0, 0.25 ) ); - nodes.push_back( Point3D( -width, 0, 1.25 ) ); - nodes.push_back( Point3D( width + 1, 0, 1.25 ) ); - - normals.push_back( Point3D( 0, -1, 0 ) ); - - texcoords.push_back( Point3D( 0, 0, 0 ) ); - texcoords.push_back( Point3D( 1, 0, 0 ) ); - texcoords.push_back( Point3D( 0, 1, 0 ) ); - texcoords.push_back( Point3D( 1, 1, 0 ) ); - - vertex_index.push_back( 0 ); - vertex_index.push_back( 1 ); - vertex_index.push_back( 2 ); - vertex_index.push_back( 3 ); - - normal_index.push_back( 0 ); - normal_index.push_back( 0 ); - normal_index.push_back( 0 ); - normal_index.push_back( 0 ); - - tex_index.push_back( 0 ); - tex_index.push_back( 1 ); - tex_index.push_back( 2 ); - tex_index.push_back( 3 ); - - ssgLeaf *leaf = sgMakeLeaf( path, GL_TRIANGLE_STRIP, matlib, material, - nodes, normals, texcoords, - vertex_index, normal_index, tex_index, - false, NULL ); - - object->addKid( leaf ); - - return object; + 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; }