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>
42 #include <simgear/scene/util/OsgMath.hxx>
44 #include "apt_signs.hxx"
46 #define SIGN "OBJECT_SIGN: "
49 using namespace simgear;
51 // for temporary storage of sign elements
53 element_info(SGMaterial *m, SGMaterialGlyph *g, double h, double c)
54 : material(m), glyph(g), height(h), coverwidth(c)
56 scale = h * m->get_xsize()
57 / (m->get_ysize() < 0.001 ? 1.0 : m->get_ysize());
58 abswidth = c == 0 ? g->get_width() * scale : c;
61 SGMaterialGlyph *glyph;
68 typedef std::vector<element_info*> ElementVec;
70 // Standard panel height sizes. The first value is unused to
71 // make sure the size value equals 1:1 the value from the apt.dat file:
72 const double HT[6] = {0.1, 0.460, 0.610, 0.760, 1.220, 0.760};
74 const double grounddist = 0.2; // hard-code sign distance from surface for now
75 const double thick = 0.1; // half the thickness of the 3D sign
78 // translation table for "command" to "glyph name"
81 const char *glyph_name;
99 osg::DrawArrays* quads;
101 osg::Vec3Array* vertices;
102 osg::Vec3Array* normals;
104 void addGlyph(SGMaterialGlyph* glyph, double x, double y, double width, double height, const osg::Matrix& xform)
107 vertices->push_back(xform.preMult(osg::Vec3(thick, x, y)));
108 vertices->push_back(xform.preMult(osg::Vec3(thick, x + width, y)));
109 vertices->push_back(xform.preMult(osg::Vec3(thick, x + width, y + height)));
110 vertices->push_back(xform.preMult(osg::Vec3(thick, x, y + height)));
112 // texture coordinates
113 double xoffset = glyph->get_left();
114 double texWidth = glyph->get_width();
116 uvs->push_back(osg::Vec2(xoffset, 0));
117 uvs->push_back(osg::Vec2(xoffset + texWidth, 0));
118 uvs->push_back(osg::Vec2(xoffset + texWidth, 1));
119 uvs->push_back(osg::Vec2(xoffset, 1));
122 for (int i=0; i<4; ++i)
123 normals->push_back(xform.preMult(osg::Vec3(0, -1, 0)));
125 quads->setCount(vertices->size());
128 void addSignCase(double caseWidth, double caseHeight, const osg::Matrix& xform)
130 int last = vertices->size();
131 double texsize = caseWidth / caseHeight;
134 vertices->push_back(osg::Vec3(-thick, -caseWidth, grounddist));
135 vertices->push_back(osg::Vec3(thick, -caseWidth, grounddist));
136 vertices->push_back(osg::Vec3(thick, -caseWidth, grounddist + caseHeight));
137 vertices->push_back(osg::Vec3(-thick, -caseWidth, grounddist + caseHeight));
139 uvs->push_back(osg::Vec2(1, 1));
140 uvs->push_back(osg::Vec2(0.75, 1));
141 uvs->push_back(osg::Vec2(0.75, 0));
142 uvs->push_back(osg::Vec2(1, 0));
144 for (int i=0; i<4; ++i)
145 normals->push_back(osg::Vec3(-1, 0.0, 0));
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));
151 vertices->push_back(osg::Vec3(-thick, caseWidth, grounddist + caseHeight));
153 uvs->push_back(osg::Vec2(1, texsize));
154 uvs->push_back(osg::Vec2(0.75, texsize));
155 uvs->push_back(osg::Vec2(0.75, 0));
156 uvs->push_back(osg::Vec2(1, 0));
158 for (int i=0; i<4; ++i)
159 normals->push_back(osg::Vec3(0, 0, 1));
162 vertices->push_back(osg::Vec3(-thick, caseWidth, grounddist + caseHeight));
163 vertices->push_back(osg::Vec3(thick, caseWidth, grounddist + caseHeight));
164 vertices->push_back(osg::Vec3(thick, caseWidth, grounddist));
165 vertices->push_back(osg::Vec3(-thick, caseWidth, grounddist));
167 uvs->push_back(osg::Vec2(1, 1));
168 uvs->push_back(osg::Vec2(0.75, 1));
169 uvs->push_back(osg::Vec2(0.75, 0));
170 uvs->push_back(osg::Vec2(1, 0));
172 for (int i=0; i<4; ++i)
173 normals->push_back(osg::Vec3(1, 0.0, 0));
176 // transform all the newly added vertices and normals by the matrix
177 for (unsigned int i=last; i<vertices->size(); ++i) {
178 (*vertices)[i]= xform.preMult((*vertices)[i]);
179 (*normals)[i] = xform.preMult((*normals)[i]);
182 quads->setCount(vertices->size());
186 typedef std::map<Effect*, GlyphGeometry*> EffectGeometryMap;
188 GlyphGeometry* makeGeometry(Effect* eff, osg::Group* group)
190 GlyphGeometry* gg = new GlyphGeometry;
192 EffectGeode* geode = new EffectGeode;
193 geode->setEffect(eff);
195 gg->vertices = new osg::Vec3Array;
196 gg->normals = new osg::Vec3Array;
197 gg->uvs = new osg::Vec2Array;
199 osg::Vec4Array* cl = new osg::Vec4Array;
200 cl->push_back(osg::Vec4(1, 1, 1, 1));
202 osg::Geometry* geometry = new osg::Geometry;
203 geometry->setVertexArray(gg->vertices);
204 geometry->setNormalArray(gg->normals);
205 geometry->setNormalBinding(osg::Geometry::BIND_PER_VERTEX);
206 geometry->setColorArray(cl);
207 geometry->setColorBinding(osg::Geometry::BIND_OVERALL);
208 geometry->setTexCoordArray(0, gg->uvs);
210 gg->quads = new osg::DrawArrays(GL_QUADS, 0, gg->vertices->size());
211 geometry->addPrimitiveSet(gg->quads);
212 geode->addDrawable(geometry);
213 group->addChild(geode);
217 // see $FG_ROOT/Docs/README.scenery
222 class AirportSignBuilder::AirportSignBuilderPrivate
225 SGMaterialLib* materials;
226 EffectGeometryMap geometries;
227 osg::MatrixTransform* signsGroup;
228 GlyphGeometry* signCaseGeometry;
230 GlyphGeometry* getGeometry(Effect* eff)
232 EffectGeometryMap::iterator it = geometries.find(eff);
233 if (it != geometries.end()) {
237 GlyphGeometry* gg = makeGeometry(eff, signsGroup);
238 geometries[eff] = gg;
242 void makeFace(const ElementVec& elements, double hpos, const osg::Matrix& xform)
244 BOOST_FOREACH(element_info* element, elements) {
245 GlyphGeometry* gg = getGeometry(element->material->get_effect());
246 gg->addGlyph(element->glyph, hpos, grounddist, element->abswidth, element->height, xform);
247 hpos += element->abswidth;
254 AirportSignBuilder::AirportSignBuilder(SGMaterialLib* mats, const SGGeod& center) :
255 d(new AirportSignBuilderPrivate)
257 d->signsGroup = new osg::MatrixTransform;
258 d->signsGroup->setMatrix(makeZUpFrame(center));
262 d->signCaseGeometry = d->getGeometry(d->materials->find("signcase")->get_effect());
265 osg::Node* AirportSignBuilder::getSignsGroup()
267 if (0 == d->signsGroup->getNumChildren())
269 return d->signsGroup;
272 AirportSignBuilder::~AirportSignBuilder()
274 EffectGeometryMap::iterator it;
275 for (it = d->geometries.begin(); it != d->geometries.end(); ++it) {
280 void AirportSignBuilder::addSign(const SGGeod& pos, double heading, const std::string& content, int size)
282 double sign_height = 1.0; // meter
283 string newmat = "BlackSign";
284 ElementVec elements1, elements2;
285 element_info *close1 = 0;
286 element_info *close2 = 0;
287 double total_width1 = 0.0;
288 double total_width2 = 0.0;
290 bool isBackside = false;
291 char oldtype = 0, newtype = 0;
292 SGMaterial *material = 0;
294 if (size < -1 || size > 5){
295 SG_LOG(SG_TERRAIN, SG_ALERT, SIGN "Found illegal sign size value of '" << size << "' for " << content << ".");
299 // Part I: parse & measure
300 for (const char *s = content.data(); *s; s++) {
306 SG_LOG(SG_TERRAIN, SG_ALERT, SIGN "Illegal taxiway sign syntax. Unexpected '{' in '" << content << "'.");
310 } else if (*s == '}') {
312 SG_LOG(SG_TERRAIN, SG_ALERT, SIGN "Illegal taxiway sign syntax. Unexpected '}' in '" << content << "'.");
325 if (s[1] == ',' || s[1] == '}' )
330 SG_LOG(SG_TERRAIN, SG_ALERT, SIGN "unclosed { in sign contents");
331 } else if (s[1] == '=') {
332 for (s += 2; *s; s++) {
334 if (s[1] == ',' || s[1] == '}')
338 SG_LOG(SG_TERRAIN, SG_ALERT, SIGN "unclosed { in sign contents");
341 if (name == "no-entry") {
342 sign_height = HT[size < 0 ? 3 : size];
347 else if (name == "critical") {
348 sign_height = HT[size < 0 ? 3 : size];
349 newmat = "SpecialSign";
353 else if (name == "safety") {
354 sign_height = HT[size < 0 ? 3 : size];
355 newmat = "SpecialSign";
359 else if (name == "hazard") {
360 sign_height = HT[size < 0 ? 3 : size];
361 newmat = "SpecialSign";
370 if (name.size() == 2 || name.size() == 3) {
372 if (n.size() == 3 && n[2] >= '1' && n[2] <= '5') {
379 SG_LOG(SG_TERRAIN, SG_ALERT, SIGN << content << " has wrong size. Allowed values are 1 to 3");
381 sign_height = HT[size < 0 ? 3 : size];
382 newmat = "YellowSign";
386 } else if (n == "@R") {
389 SG_LOG(SG_TERRAIN, SG_ALERT, SIGN << content << " has wrong size. Allowed values are 1 to 3");
391 sign_height = HT[size < 0 ? 3 : size];
396 } else if (n == "@L") {
399 SG_LOG(SG_TERRAIN, SG_ALERT, SIGN << content << " has wrong size. Allowed values are 1 to 3");
401 sign_height = HT[size < 0 ? 3 : size];
402 newmat = "FramedSign";
406 } else if (n == "@B") {
407 if ( (size != -1) && (size != 4) && (size != 5) ) {
409 SG_LOG(SG_TERRAIN, SG_ALERT, SIGN << content << " has wrong size. Allowed values are 4 or 5");
411 sign_height = HT[size < 0 ? 4 : size];
412 newmat = "BlackSign";
418 for (int i = 0; cmds[i].keyword; i++) {
419 if (name == cmds[i].keyword) {
420 name = cmds[i].glyph_name;
425 if (name[0] == '@') {
426 SG_LOG(SG_TERRAIN, SG_ALERT, SIGN "ignoring unknown command `" << name << '\'');
432 material = d->materials->find(newmat);
436 SGMaterialGlyph *glyph = material->get_glyph(name);
438 SG_LOG( SG_TERRAIN, SG_ALERT, SIGN "unsupported glyph '" << *s << '\'');
442 // in managed mode push frame stop and frame start first
444 if (newtype && newtype != oldtype) {
446 elements1.push_back(close1);
447 total_width1 += close1->glyph->get_width() * close1->scale;
451 SGMaterialGlyph *g = material->get_glyph("stop-frame");
453 close1 = new element_info(material, g, sign_height, 0);
454 g = material->get_glyph("start-frame");
456 element_info* e1 = new element_info(material, g, sign_height, 0);
457 elements1.push_back(e1);
458 total_width1 += e1->glyph->get_width() * e1->scale;
461 // now the actually requested glyph (front)
462 element_info* e1 = new element_info(material, glyph, sign_height, 0);
463 elements1.push_back(e1);
464 total_width1 += e1->glyph->get_width() * e1->scale;
466 if (newtype && newtype != oldtype) {
468 elements2.push_back(close2);
469 total_width2 += close2->glyph->get_width() * close2->scale;
473 SGMaterialGlyph *g = material->get_glyph("stop-frame");
475 close2 = new element_info(material, g, sign_height, 0);
476 g = material->get_glyph("start-frame");
478 element_info* e2 = new element_info(material, g, sign_height, 0);
479 elements2.push_back(e2);
480 total_width2 += e2->glyph->get_width() * e2->scale;
483 // now the actually requested glyph (back)
484 element_info* e2 = new element_info(material, glyph, sign_height, 0);
485 elements2.push_back(e2);
486 total_width2 += e2->glyph->get_width() * e2->scale;
490 // in managed mode close frame
492 elements1.push_back(close1);
493 total_width1 += close1->glyph->get_width() * close1->scale;
498 elements2.push_back(close2);
499 total_width2 += close2->glyph->get_width() * close2->scale;
504 double boxwidth = std::max(total_width1, total_width2) * 0.5;
505 double hpos = -boxwidth;
506 SGMaterial *mat = d->materials->find("signcase");
508 double coverSize = fabs(total_width1 - total_width2) * 0.5;
509 element_info* s1 = new element_info(mat, mat->get_glyph("cover1"), sign_height, coverSize);
510 element_info* s2 = new element_info(mat, mat->get_glyph("cover2"), sign_height, coverSize);
512 if (total_width1 < total_width2) {
513 elements1.insert(elements1.begin(), s1);
514 elements1.push_back(s2);
515 } else if (total_width2 < total_width1) {
516 elements2.insert(elements2.begin(), s1);
517 elements2.push_back(s2);
524 const osg::Vec3 Z_AXIS(0, 0, 1);
525 osg::Matrix m(makeZUpFrame(pos));
526 m.preMultRotate(osg::Quat(SGMiscd::deg2rad(heading), Z_AXIS));
528 // apply the inverse of the group transform, so sign vertices
529 // are relative to the tile center, and hence have a magnitude which
530 // fits in a float with sufficent precision.
531 m.postMult(d->signsGroup->getInverseMatrix());
533 d->makeFace(elements1, hpos, m);
536 back.preMultRotate(osg::Quat(M_PI, Z_AXIS));
537 d->makeFace(elements2, hpos, back);
539 d->signCaseGeometry->addSignCase(boxwidth, sign_height, m);
542 } // of namespace simgear