- // 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 + ".rgb";
-
- point_list nodes; nodes.clear();
- point_list normals; normals.clear();
- point_list texcoords; texcoords.clear();
- int_list vertex_index; vertex_index.clear();
- int_list normal_index; normal_index.clear();
- int_list tex_index; tex_index.clear();
-
- 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 ) );
+ 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;
+ bool cmd = false;
+ int size = -1;
+ char oldtype = 0, newtype = 0;
+
+ 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;
+ string value;
+
+ if (*s == '{') {
+ if (cmd)
+ 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, SIGN "unexpected } in sign contents");
+ cmd = false;
+ continue;
+
+ } else if (!cmd) {
+ name = *s;
+
+ } else {
+ if (*s == ',')
+ continue;
+
+ for (; *s; s++) {
+ name += *s;
+ if (s[1] == ',' || s[1] == '}' || s[1] == '=')
+ break;
+ }
+ if (!*s) {
+ SG_LOG(SG_TERRAIN, SG_ALERT, SIGN "unclosed { in sign contents");
+ } else if (s[1] == '=') {
+ for (s += 2; *s; s++) {
+ value += *s;
+ if (s[1] == ',' || s[1] == '}')
+ break;
+ }
+ if (!*s)
+ SG_LOG(SG_TERRAIN, SG_ALERT, SIGN "unclosed { in sign contents");
+ }
+
+ if (name == "@size") {
+ sign_height = strtod(value.data(), 0);
+ continue;
+ }
+
+ if (name == "@light") {
+ lighted = (value != "0" && value != "no" && value != "off" && value != "false");
+ continue;
+ }
+
+ if (name == "@material") {
+ newmat = value.data();
+ 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 (n == "@Y") {
+ if (size < 3) {
+ sign_height = HT[size < 0 ? 2 : size];
+ newmat = "YellowSign";
+ newtype = 'Y';
+ continue;
+ }
+
+ } else if (n == "@R") {
+ if (size < 3) {
+ sign_height = HT[size < 0 ? 2 : size];
+ newmat = "RedSign";
+ newtype = 'R';
+ continue;
+ }
+
+ } else if (n == "@L") {
+ if (size < 3) {
+ sign_height = HT[size < 0 ? 2 : size];
+ newmat = "FramedSign";
+ newtype = 'L';
+ continue;
+ }
+
+ } else if (n == "@B") {
+ if (size < 0 || size == 3 || size == 4) {
+ sign_height = HT[size < 0 ? 3 : size];
+ newmat = "BlackSign";
+ newtype = 'B';
+ continue;
+ }
+ }
+ }
+
+ for (int i = 0; cmds[i].keyword; i++) {
+ if (name == cmds[i].keyword) {
+ name = cmds[i].glyph_name;
+ break;
+ }
+ }
+
+ if (name[0] == '@') {
+ SG_LOG(SG_TERRAIN, SG_ALERT, SIGN "ignoring unknown command `" << name << '\'');
+ 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, 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;
+ }
+ 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
+ e = new element_info(material, state, glyph, sign_height);
+ elements.push_back(e);
+ total_width += e->glyph->get_width() * e->scale;
+ }