1 // apt_signs.cxx -- build airport signs on the fly
3 // Written by Curtis Olson, started July 2001.
5 // Copyright (C) 2001 Curtis L. Olson - http://www.flightgear.org/~curt
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24 # include <simgear_config.h>
28 #include <boost/foreach.hpp>
31 #include <osg/Geometry>
33 #include <osg/MatrixTransform>
34 #include <osg/StateSet>
36 #include <simgear/debug/logstream.hxx>
37 #include <simgear/math/sg_types.hxx>
38 #include <simgear/scene/material/Effect.hxx>
39 #include <simgear/scene/material/EffectGeode.hxx>
40 #include <simgear/scene/material/mat.hxx>
41 #include <simgear/scene/material/matlib.hxx>
43 #include "apt_signs.hxx"
45 #define SIGN "OBJECT_SIGN: "
48 using namespace simgear;
50 // for temporary storage of sign elements
52 element_info(SGMaterial *m, SGMaterialGlyph *g, double h, double c)
53 : material(m), glyph(g), height(h), coverwidth(c)
55 scale = h * m->get_xsize()
56 / (m->get_ysize() < 0.001 ? 1.0 : m->get_ysize());
57 abswidth = c == 0 ? g->get_width() * scale : c;
60 SGMaterialGlyph *glyph;
67 typedef std::vector<element_info*> ElementVec;
69 // Standard panel height sizes. The first value is unused to
70 // make sure the size value equals 1:1 the value from the apt.dat file:
71 const double HT[6] = {0.1, 0.460, 0.610, 0.760, 1.220, 0.760};
73 const double grounddist = 0.2; // hard-code sign distance from surface for now
74 const double thick = 0.1; // half the thickness of the 3D sign
77 // translation table for "command" to "glyph name"
80 const char *glyph_name;
98 osg::DrawArrays* quads;
100 osg::Vec3Array* vertices;
101 osg::Vec3Array* normals;
103 void addGlyph(SGMaterialGlyph* glyph, double x, double y, double width, double height, const osg::Matrix& xform)
106 vertices->push_back(xform.preMult(osg::Vec3(thick, x, y)));
107 vertices->push_back(xform.preMult(osg::Vec3(thick, x + width, y)));
108 vertices->push_back(xform.preMult(osg::Vec3(thick, x + width, y + height)));
109 vertices->push_back(xform.preMult(osg::Vec3(thick, x, y + height)));
111 // texture coordinates
112 double xoffset = glyph->get_left();
113 double texWidth = glyph->get_width();
115 uvs->push_back(osg::Vec2(xoffset, 0));
116 uvs->push_back(osg::Vec2(xoffset + texWidth, 0));
117 uvs->push_back(osg::Vec2(xoffset + texWidth, 1));
118 uvs->push_back(osg::Vec2(xoffset, 1));
121 for (int i=0; i<4; ++i)
122 normals->push_back(xform.preMult(osg::Vec3(0, -1, 0)));
124 quads->setCount(vertices->size());
127 void addSignCase(double caseWidth, double caseHeight, const osg::Matrix& xform)
129 int last = vertices->size();
130 double texsize = caseWidth / caseHeight;
133 vertices->push_back(osg::Vec3(-thick, -caseWidth, grounddist));
134 vertices->push_back(osg::Vec3(thick, -caseWidth, grounddist));
135 vertices->push_back(osg::Vec3(thick, -caseWidth, grounddist + caseHeight));
136 vertices->push_back(osg::Vec3(-thick, -caseWidth, grounddist + caseHeight));
138 uvs->push_back(osg::Vec2(1, 1));
139 uvs->push_back(osg::Vec2(0.75, 1));
140 uvs->push_back(osg::Vec2(0.75, 0));
141 uvs->push_back(osg::Vec2(1, 0));
143 for (int i=0; i<4; ++i)
144 normals->push_back(osg::Vec3(-1, 0.0, 0));
147 vertices->push_back(osg::Vec3(-thick, -caseWidth, grounddist + caseHeight));
148 vertices->push_back(osg::Vec3(thick, -caseWidth, grounddist + caseHeight));
149 vertices->push_back(osg::Vec3(thick, caseWidth, grounddist + caseHeight));
150 vertices->push_back(osg::Vec3(-thick, caseWidth, grounddist + caseHeight));
152 uvs->push_back(osg::Vec2(1, texsize));
153 uvs->push_back(osg::Vec2(0.75, texsize));
154 uvs->push_back(osg::Vec2(0.75, 0));
155 uvs->push_back(osg::Vec2(1, 0));
157 for (int i=0; i<4; ++i)
158 normals->push_back(osg::Vec3(0, 0, 1));
161 vertices->push_back(osg::Vec3(-thick, caseWidth, grounddist + caseHeight));
162 vertices->push_back(osg::Vec3(thick, caseWidth, grounddist + caseHeight));
163 vertices->push_back(osg::Vec3(thick, caseWidth, grounddist));
164 vertices->push_back(osg::Vec3(-thick, caseWidth, grounddist));
166 uvs->push_back(osg::Vec2(1, 1));
167 uvs->push_back(osg::Vec2(0.75, 1));
168 uvs->push_back(osg::Vec2(0.75, 0));
169 uvs->push_back(osg::Vec2(1, 0));
171 for (int i=0; i<4; ++i)
172 normals->push_back(osg::Vec3(1, 0.0, 0));
175 // transform all the newly added vertices and normals by the matrix
176 for (unsigned int i=last; i<vertices->size(); ++i) {
177 (*vertices)[i]= xform.preMult((*vertices)[i]);
178 (*normals)[i] = xform.preMult((*normals)[i]);
181 quads->setCount(vertices->size());
185 typedef std::map<Effect*, GlyphGeometry*> EffectGeometryMap;
187 GlyphGeometry* makeGeometry(Effect* eff, osg::Group* group)
189 GlyphGeometry* gg = new GlyphGeometry;
191 EffectGeode* geode = new EffectGeode;
192 geode->setEffect(eff);
194 gg->vertices = new osg::Vec3Array;
195 gg->normals = new osg::Vec3Array;
196 gg->uvs = new osg::Vec2Array;
198 osg::Vec4Array* cl = new osg::Vec4Array;
199 cl->push_back(osg::Vec4(1, 1, 1, 1));
201 osg::Geometry* geometry = new osg::Geometry;
202 geometry->setVertexArray(gg->vertices);
203 geometry->setNormalArray(gg->normals);
204 geometry->setNormalBinding(osg::Geometry::BIND_PER_VERTEX);
205 geometry->setColorArray(cl);
206 geometry->setColorBinding(osg::Geometry::BIND_OVERALL);
207 geometry->setTexCoordArray(0, gg->uvs);
209 gg->quads = new osg::DrawArrays(GL_QUADS, 0, gg->vertices->size());
210 geometry->addPrimitiveSet(gg->quads);
211 geode->addDrawable(geometry);
212 group->addChild(geode);
216 // see $FG_ROOT/Docs/README.scenery
221 class AirportSignBuilder::AirportSignBuilderPrivate
224 SGMaterialLib* materials;
225 EffectGeometryMap geometries;
226 osg::MatrixTransform* signsGroup;
227 GlyphGeometry* signCaseGeometry;
229 GlyphGeometry* getGeometry(Effect* eff)
231 EffectGeometryMap::iterator it = geometries.find(eff);
232 if (it != geometries.end()) {
236 GlyphGeometry* gg = makeGeometry(eff, signsGroup);
237 geometries[eff] = gg;
241 void makeFace(const ElementVec& elements, double hpos, const osg::Matrix& xform)
243 BOOST_FOREACH(element_info* element, elements) {
244 GlyphGeometry* gg = getGeometry(element->material->get_effect());
245 gg->addGlyph(element->glyph, hpos, grounddist, element->abswidth, element->height, xform);
246 hpos += element->abswidth;
253 AirportSignBuilder::AirportSignBuilder(SGMaterialLib* mats, const SGGeod& center) :
254 d(new AirportSignBuilderPrivate)
256 d->signsGroup = new osg::MatrixTransform;
257 d->signsGroup->setMatrix(makeZUpFrame(center));
261 d->signCaseGeometry = d->getGeometry(d->materials->find("signcase")->get_effect());
264 osg::Node* AirportSignBuilder::getSignsGroup()
266 if (0 == d->signsGroup->getNumChildren())
268 return d->signsGroup;
271 AirportSignBuilder::~AirportSignBuilder()
273 EffectGeometryMap::iterator it;
274 for (it = d->geometries.begin(); it != d->geometries.end(); ++it) {
279 void AirportSignBuilder::addSign(const SGGeod& pos, double heading, const std::string& content, int size)
281 double sign_height = 1.0; // meter
282 string newmat = "BlackSign";
283 ElementVec elements1, elements2;
284 element_info *close1 = 0;
285 element_info *close2 = 0;
286 double total_width1 = 0.0;
287 double total_width2 = 0.0;
289 bool isBackside = false;
290 char oldtype = 0, newtype = 0;
291 SGMaterial *material = 0;
293 if (size < -1 || size > 5){
294 SG_LOG(SG_TERRAIN, SG_ALERT, SIGN "Found illegal sign size value of '" << size << "' for " << content << ".");
298 // Part I: parse & measure
299 for (const char *s = content.data(); *s; s++) {
305 SG_LOG(SG_TERRAIN, SG_ALERT, SIGN "Illegal taxiway sign syntax. Unexpected '{' in '" << content << "'.");
309 } else if (*s == '}') {
311 SG_LOG(SG_TERRAIN, SG_ALERT, SIGN "Illegal taxiway sign syntax. Unexpected '}' in '" << content << "'.");
324 if (s[1] == ',' || s[1] == '}' )
329 SG_LOG(SG_TERRAIN, SG_ALERT, SIGN "unclosed { in sign contents");
330 } else if (s[1] == '=') {
331 for (s += 2; *s; s++) {
333 if (s[1] == ',' || s[1] == '}')
337 SG_LOG(SG_TERRAIN, SG_ALERT, SIGN "unclosed { in sign contents");
340 if (name == "no-entry") {
341 sign_height = HT[size < 0 ? 3 : size];
346 else if (name == "critical") {
347 sign_height = HT[size < 0 ? 3 : size];
348 newmat = "SpecialSign";
352 else if (name == "safety") {
353 sign_height = HT[size < 0 ? 3 : size];
354 newmat = "SpecialSign";
358 else if (name == "hazard") {
359 sign_height = HT[size < 0 ? 3 : size];
360 newmat = "SpecialSign";
369 if (name.size() == 2 || name.size() == 3) {
371 if (n.size() == 3 && n[2] >= '1' && n[2] <= '5') {
378 SG_LOG(SG_TERRAIN, SG_ALERT, SIGN "Wrong size for " << name << ". Allowed values are 1 to 3");
380 sign_height = HT[size < 0 ? 3 : size];
381 newmat = "YellowSign";
385 } else if (n == "@R") {
388 SG_LOG(SG_TERRAIN, SG_ALERT, SIGN "Wrong size for " << name << ". Allowed values are 1 to 3");
390 sign_height = HT[size < 0 ? 3 : size];
395 } else if (n == "@L") {
398 SG_LOG(SG_TERRAIN, SG_ALERT, SIGN "Wrong size for " << name << ". Allowed values are 1 to 3");
400 sign_height = HT[size < 0 ? 3 : size];
401 newmat = "FramedSign";
405 } else if (n == "@B") {
406 if ( (size != -1) && (size != 4) && (size != 5) ) {
408 SG_LOG(SG_TERRAIN, SG_ALERT, SIGN "Wrong size for " << name << ". Allowed values are 4 or 5");
410 sign_height = HT[size < 0 ? 4 : size];
411 newmat = "BlackSign";
417 for (int i = 0; cmds[i].keyword; i++) {
418 if (name == cmds[i].keyword) {
419 name = cmds[i].glyph_name;
424 if (name[0] == '@') {
425 SG_LOG(SG_TERRAIN, SG_ALERT, SIGN "ignoring unknown command `" << name << '\'');
431 material = d->materials->find(newmat);
435 SGMaterialGlyph *glyph = material->get_glyph(name);
437 SG_LOG( SG_TERRAIN, SG_ALERT, SIGN "unsupported glyph '" << *s << '\'');
441 // in managed mode push frame stop and frame start first
443 if (newtype && newtype != oldtype) {
445 elements1.push_back(close1);
446 total_width1 += close1->glyph->get_width() * close1->scale;
450 SGMaterialGlyph *g = material->get_glyph("stop-frame");
452 close1 = new element_info(material, g, sign_height, 0);
453 g = material->get_glyph("start-frame");
455 element_info* e1 = new element_info(material, g, sign_height, 0);
456 elements1.push_back(e1);
457 total_width1 += e1->glyph->get_width() * e1->scale;
460 // now the actually requested glyph (front)
461 element_info* e1 = new element_info(material, glyph, sign_height, 0);
462 elements1.push_back(e1);
463 total_width1 += e1->glyph->get_width() * e1->scale;
465 if (newtype && newtype != oldtype) {
467 elements2.push_back(close2);
468 total_width2 += close2->glyph->get_width() * close2->scale;
472 SGMaterialGlyph *g = material->get_glyph("stop-frame");
474 close2 = new element_info(material, g, sign_height, 0);
475 g = material->get_glyph("start-frame");
477 element_info* e2 = new element_info(material, g, sign_height, 0);
478 elements2.push_back(e2);
479 total_width2 += e2->glyph->get_width() * e2->scale;
482 // now the actually requested glyph (back)
483 element_info* e2 = new element_info(material, glyph, sign_height, 0);
484 elements2.push_back(e2);
485 total_width2 += e2->glyph->get_width() * e2->scale;
489 // in managed mode close frame
491 elements1.push_back(close1);
492 total_width1 += close1->glyph->get_width() * close1->scale;
497 elements2.push_back(close2);
498 total_width2 += close2->glyph->get_width() * close2->scale;
503 double boxwidth = std::max(total_width1, total_width2) * 0.5;
504 double hpos = -boxwidth;
505 SGMaterial *mat = d->materials->find("signcase");
507 double coverSize = fabs(total_width1 - total_width2) * 0.5;
508 element_info* s1 = new element_info(mat, mat->get_glyph("cover1"), sign_height, coverSize);
509 element_info* s2 = new element_info(mat, mat->get_glyph("cover2"), sign_height, coverSize);
511 if (total_width1 < total_width2) {
512 elements1.insert(elements1.begin(), s1);
513 elements1.push_back(s2);
514 } else if (total_width2 < total_width1) {
515 elements2.insert(elements2.begin(), s1);
516 elements2.push_back(s2);
523 const osg::Vec3 Z_AXIS(0, 0, 1);
524 osg::Matrix m(makeZUpFrame(pos));
525 m.preMultRotate(osg::Quat(SGMiscd::deg2rad(heading), Z_AXIS));
527 // apply the inverse of the group transform, so sign vertices
528 // are relative to the tile center, and hence have a magnitude which
529 // fits in a float with sufficent precision.
530 m.postMult(d->signsGroup->getInverseMatrix());
532 d->makeFace(elements1, hpos, m);
535 back.preMultRotate(osg::Quat(M_PI, Z_AXIS));
536 d->makeFace(elements2, hpos, back);
538 d->signCaseGeometry->addSignCase(boxwidth, sign_height, m);
541 } // of namespace simgear