]> git.mxchange.org Git - simgear.git/blob - simgear/scene/tgdb/apt_signs.cxx
build signs for an STG into single geometry.
[simgear.git] / simgear / scene / tgdb / apt_signs.cxx
1 // apt_signs.cxx -- build airport signs on the fly
2 //
3 // Written by Curtis Olson, started July 2001.
4 //
5 // Copyright (C) 2001  Curtis L. Olson  - http://www.flightgear.org/~curt
6 //
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.
11 //
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.
16 //
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.
20 //
21 // $Id$
22
23 #ifdef HAVE_CONFIG_H
24 #  include <simgear_config.h>
25 #endif
26
27 #include <vector>
28 #include <boost/foreach.hpp>
29
30 #include <osg/Geode>
31 #include <osg/Geometry>
32 #include <osg/Group>
33 #include <osg/MatrixTransform>
34 #include <osg/StateSet>
35
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
43 #include "apt_signs.hxx"
44
45 #define SIGN "OBJECT_SIGN: "
46
47 using std::vector;
48 using namespace simgear;
49
50 // for temporary storage of sign elements
51 struct element_info {
52     element_info(SGMaterial *m, SGMaterialGlyph *g, double h, double c)
53         : material(m), glyph(g), height(h), coverwidth(c)
54     {
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;
58     }
59     SGMaterial *material;
60     SGMaterialGlyph *glyph;
61     double height;
62     double scale;
63     double abswidth;
64     double coverwidth;
65 };
66
67 typedef std::vector<element_info*> ElementVec;
68
69 const double HT[5] = {0.460, 0.610, 0.760, 1.220, 0.760}; // standard panel height sizes
70 const double grounddist = 0.2;     // hard-code sign distance from surface for now
71 const double thick = 0.1;    // half the thickness of the 3D sign
72
73
74 // translation table for "command" to "glyph name"
75 struct pair {
76     const char *keyword;
77     const char *glyph_name;
78 } cmds[] = {
79     {"@u",       "^u"},
80     {"@d",       "^d"},
81     {"@l",       "^l"},
82     {"@lu",      "^lu"},
83     {"@ul",      "^lu"},
84     {"@ld",      "^ld"},
85     {"@dl",      "^ld"},
86     {"@r",       "^r"},
87     {"@ru",      "^ru"},
88     {"@ur",      "^ru"},
89     {"@rd",      "^rd"},
90     {"@dr",      "^rd"},
91     {0, 0},
92 };
93
94 struct GlyphGeometry
95 {
96   osg::DrawArrays* quads;
97   osg::Vec2Array* uvs;
98   osg::Vec3Array* vertices;
99   osg::Vec3Array* normals;
100   
101   void addGlyph(SGMaterialGlyph* glyph, double x, double y, double width, double height, const osg::Matrix& xform)
102   {
103     
104     vertices->push_back(xform.preMult(osg::Vec3(thick, x, y)));
105     vertices->push_back(xform.preMult(osg::Vec3(thick, x + width, y)));
106     vertices->push_back(xform.preMult(osg::Vec3(thick, x + width, y + height)));
107     vertices->push_back(xform.preMult(osg::Vec3(thick, x, y + height)));
108     
109     // texture coordinates
110     double xoffset = glyph->get_left();
111     double texWidth = glyph->get_width();
112     
113     uvs->push_back(osg::Vec2(xoffset,         0));
114     uvs->push_back(osg::Vec2(xoffset + texWidth, 0));
115     uvs->push_back(osg::Vec2(xoffset + texWidth, 1));
116     uvs->push_back(osg::Vec2(xoffset,         1));
117
118     // normals
119     for (int i=0; i<4; ++i)
120       normals->push_back(xform.preMult(osg::Vec3(0, -1, 0)));
121     
122     quads->setCount(vertices->size());
123   }
124   
125   void addSignCase(double caseWidth, double caseHeight, const osg::Matrix& xform)
126   {
127     int last = vertices->size();
128     
129     vertices->push_back(osg::Vec3(-thick, -caseWidth,  grounddist));
130     vertices->push_back(osg::Vec3(thick, -caseWidth,  grounddist));
131     vertices->push_back(osg::Vec3(thick, -caseWidth,  grounddist + caseHeight));
132     vertices->push_back(osg::Vec3(-thick, -caseWidth,  grounddist + caseHeight));
133     
134     
135     for (int i=0; i<4; ++i)
136       normals->push_back(osg::Vec3(-1, 0.0, 0));
137     
138     vertices->push_back(osg::Vec3(-thick, -caseWidth,  grounddist + caseHeight));
139     vertices->push_back(osg::Vec3(thick,  -caseWidth,  grounddist + caseHeight));
140     vertices->push_back(osg::Vec3(thick,  caseWidth, grounddist + caseHeight));
141     vertices->push_back(osg::Vec3(-thick, caseWidth, grounddist + caseHeight));
142     
143     for (int i=0; i<4; ++i)
144       normals->push_back(osg::Vec3(0, 0, 1));
145     
146     vertices->push_back(osg::Vec3(-thick, caseWidth, grounddist + caseHeight));
147     vertices->push_back(osg::Vec3(thick,  caseWidth, grounddist + caseHeight));
148     vertices->push_back(osg::Vec3(thick,  caseWidth, grounddist));
149     vertices->push_back(osg::Vec3(-thick, caseWidth, grounddist));
150     
151     for (int i=0; i<4; ++i)
152       normals->push_back(osg::Vec3(1, 0.0, 0));
153     
154     for (int i = 0; i < 3; ++i) {
155       uvs->push_back(osg::Vec2(1,    1));
156       uvs->push_back(osg::Vec2(0.75, 1));
157       uvs->push_back(osg::Vec2(0.75, 0));
158       uvs->push_back(osg::Vec2(1,    0));
159     }
160     
161   // transform all the newly added vertices and normals by the matrix
162     for (unsigned int i=last; i<vertices->size(); ++i) {
163       (*vertices)[i]= xform.preMult((*vertices)[i]);
164       (*normals)[i] = xform.preMult((*normals)[i]);
165     }
166     
167     quads->setCount(vertices->size());
168   }
169 };
170
171 typedef std::map<Effect*, GlyphGeometry*> EffectGeometryMap;
172
173 GlyphGeometry* makeGeometry(Effect* eff, osg::Group* group)
174 {
175   GlyphGeometry* gg = new GlyphGeometry;
176   
177   EffectGeode* geode = new EffectGeode;
178   geode->setEffect(eff);
179  
180   gg->vertices = new osg::Vec3Array;
181   gg->normals = new osg::Vec3Array;
182   gg->uvs = new osg::Vec2Array;
183   
184   osg::Vec4Array* cl = new osg::Vec4Array;
185   cl->push_back(osg::Vec4(1, 1, 1, 1));
186   
187   osg::Geometry* geometry = new osg::Geometry;
188   geometry->setVertexArray(gg->vertices);
189   geometry->setNormalArray(gg->normals);
190   geometry->setNormalBinding(osg::Geometry::BIND_PER_VERTEX);
191   geometry->setColorArray(cl);
192   geometry->setColorBinding(osg::Geometry::BIND_OVERALL);
193   geometry->setTexCoordArray(0, gg->uvs);
194   
195   gg->quads = new osg::DrawArrays(GL_QUADS, 0, gg->vertices->size());
196   geometry->addPrimitiveSet(gg->quads);
197   geode->addDrawable(geometry);
198   group->addChild(geode);
199   return gg;
200 }
201
202 // see $FG_ROOT/Docs/README.scenery
203
204 namespace simgear
205 {
206
207 class AirportSignBuilder::AirportSignBuilderPrivate
208 {
209 public:
210     SGMaterialLib* materials;
211     EffectGeometryMap geometries;
212     osg::MatrixTransform* signsGroup;
213     GlyphGeometry* signCaseGeometry;
214     
215     GlyphGeometry* getGeometry(Effect* eff)
216     {
217         EffectGeometryMap::iterator it = geometries.find(eff);
218         if (it != geometries.end()) {
219             return it->second;
220         }
221         
222         GlyphGeometry* gg = makeGeometry(eff, signsGroup);
223         geometries[eff] = gg;
224         return gg;
225     }
226     
227     void makeFace(const ElementVec& elements, double hpos, const osg::Matrix& xform)
228     {
229         BOOST_FOREACH(element_info* element, elements) {
230             GlyphGeometry* gg = getGeometry(element->material->get_effect());
231             gg->addGlyph(element->glyph, hpos, grounddist, element->abswidth, element->height, xform);
232             hpos += element->abswidth;
233             delete element;
234         }
235     }
236     
237 };
238
239 AirportSignBuilder::AirportSignBuilder(SGMaterialLib* mats, const SGGeod& center) :
240     d(new AirportSignBuilderPrivate)
241 {
242     d->signsGroup = new osg::MatrixTransform;
243     d->signsGroup->setMatrix(makeZUpFrame(center));
244     
245     assert(mats);
246     d->materials = mats;
247     d->signCaseGeometry = d->getGeometry(d->materials->find("signcase")->get_effect());
248 }
249
250 osg::Node* AirportSignBuilder::getSignsGroup()
251 {
252     return d->signsGroup;
253 }
254
255 AirportSignBuilder::~AirportSignBuilder()
256 {
257     EffectGeometryMap::iterator it;
258     for (it = d->geometries.begin(); it != d->geometries.end(); ++it) {
259         delete it->second;
260     }
261 }
262
263 void AirportSignBuilder::addSign(const SGGeod& pos, double heading, const std::string& content)
264 {
265     double sign_height = 1.0;  // meter
266     string newmat = "BlackSign";
267     ElementVec elements1, elements2;
268     element_info *close1 = 0;
269     element_info *close2 = 0;
270     double total_width1 = 0.0;
271     double total_width2 = 0.0;
272     bool cmd = false;
273     bool isBackside = false;
274     int size = -1;
275     char oldtype = 0, newtype = 0;
276     SGMaterial *material = 0;
277
278     // Part I: parse & measure
279     for (const char *s = content.data(); *s; s++) {
280         string name;
281         string value;
282
283         if (*s == '{') {
284             if (cmd)
285                 SG_LOG(SG_TERRAIN, SG_ALERT, SIGN "Illegal taxiway sign syntax. Unexpected '{' in '" << content << "'.");
286             cmd = true;
287             continue;
288
289         } else if (*s == '}') {
290             if (!cmd)
291                 SG_LOG(SG_TERRAIN, SG_ALERT, SIGN "Illegal taxiway sign syntax. Unexpected '}' in '" << content << "'.");
292             cmd = false;
293             continue;
294
295         } else if (!cmd) {
296             name = *s;
297
298         } else {
299             if (*s == ',')
300                 continue;
301
302             for (; *s; s++) {
303                 name += *s;
304                 if (s[1] == ',' || s[1] == '}' || s[1] == '=')
305                     break;
306             }
307             if (!*s) {
308                 SG_LOG(SG_TERRAIN, SG_ALERT, SIGN "unclosed { in sign contents");
309             } else if (s[1] == '=') {
310                 for (s += 2; *s; s++) {
311                     value += *s;
312                     if (s[1] == ',' || s[1] == '}')
313                         break;
314                 }
315                 if (!*s)
316                     SG_LOG(SG_TERRAIN, SG_ALERT, SIGN "unclosed { in sign contents");
317             }
318
319             if (name == "@@") {
320                 isBackside = true;
321                 continue;
322             }
323
324             if (name == "@size") {
325                 sign_height = strtod(value.data(), 0);
326                 continue;
327             }
328
329             if (name.size() == 2 || name.size() == 3) {
330                 string n = name;
331                 if (n.size() == 3 && n[2] >= '1' && n[2] <= '5') {
332                     size = n[2] - '1';
333                     n = n.substr(0, 2);
334                 }
335                 if (n == "@Y") {
336                     if (size < 3) {
337                         sign_height = HT[size < 0 ? 2 : size];
338                         newmat = "YellowSign";
339                         newtype = 'Y';
340                         continue;
341                     }
342
343                 } else if (n == "@R") {
344                     if (size < 3) {
345                         sign_height = HT[size < 0 ? 2 : size];
346                         newmat = "RedSign";
347                         newtype = 'R';
348                         continue;
349                     }
350
351                 } else if (n == "@L") {
352                     if (size < 3) {
353                         sign_height = HT[size < 0 ? 2 : size];
354                         newmat = "FramedSign";
355                         newtype = 'L';
356                         continue;
357                     }
358
359                 } else if (n == "@B") {
360                     if (size < 0 || size == 3 || size == 4) {
361                         sign_height = HT[size < 0 ? 3 : size];
362                         newmat = "BlackSign";
363                         newtype = 'B';
364                         continue;
365                     }
366                 }
367             }
368
369             for (int i = 0; cmds[i].keyword; i++) {
370                 if (name == cmds[i].keyword) {
371                     name = cmds[i].glyph_name;
372                     break;
373                 }
374             }
375
376             if (name[0] == '@') {
377                 SG_LOG(SG_TERRAIN, SG_ALERT, SIGN "ignoring unknown command `" << name << '\'');
378                 continue;
379             }
380         }
381
382         if (newmat.size()) {
383             material = d->materials->find(newmat);
384             newmat.clear();
385         }
386  
387         SGMaterialGlyph *glyph = material->get_glyph(name);
388         if (!glyph) {
389             SG_LOG( SG_TERRAIN, SG_ALERT, SIGN "unsupported glyph '" << *s << '\'');
390             continue;
391         }
392
393     // in managed mode push frame stop and frame start first
394         if (!isBackside) {
395             if (newtype && newtype != oldtype) {
396                 if (close1) {
397                     elements1.push_back(close1);
398                     total_width1 += close1->glyph->get_width() * close1->scale;
399                     close1 = 0;
400                 }
401                 oldtype = newtype;
402                 SGMaterialGlyph *g = material->get_glyph("stop-frame");
403                 if (g)
404                     close1 = new element_info(material, g, sign_height, 0);
405                 g = material->get_glyph("start-frame");
406                 if (g) {
407                     element_info* e1 = new element_info(material, g, sign_height, 0);
408                     elements1.push_back(e1);
409                     total_width1 += e1->glyph->get_width() * e1->scale;
410                 }
411             }
412             // now the actually requested glyph (front)
413             element_info* e1 = new element_info(material, glyph, sign_height, 0);
414             elements1.push_back(e1);
415             total_width1 += e1->glyph->get_width() * e1->scale;
416         } else {
417             if (newtype && newtype != oldtype) {
418                 if (close2) {
419                     elements2.push_back(close2);
420                     total_width2 += close2->glyph->get_width() * close2->scale;
421                     close2 = 0;
422                 }
423                 oldtype = newtype;
424                 SGMaterialGlyph *g = material->get_glyph("stop-frame");
425                 if (g)
426                     close2 = new element_info(material, g, sign_height, 0);
427                 g = material->get_glyph("start-frame");
428                 if (g) {
429                     element_info* e2 = new element_info(material, g, sign_height, 0);
430                     elements2.push_back(e2);
431                     total_width2 += e2->glyph->get_width() * e2->scale;
432                 }
433             }
434             // now the actually requested glyph (back)
435             element_info* e2 = new element_info(material, glyph, sign_height, 0);
436             elements2.push_back(e2);
437             total_width2 += e2->glyph->get_width() * e2->scale;
438         }
439     }
440
441     // in managed mode close frame
442     if (close1) {
443         elements1.push_back(close1);
444         total_width1 += close1->glyph->get_width() * close1->scale;
445         close1 = 0;
446     }
447
448     if (close2) {
449         elements2.push_back(close2);
450         total_width2 += close2->glyph->get_width() * close2->scale;
451         close2 = 0;
452     }
453
454   // Part II: typeset
455     double boxwidth = std::max(total_width1, total_width2) * 0.5;
456     double hpos = -boxwidth;
457     SGMaterial *mat = d->materials->find("signcase");
458   
459     double coverSize = fabs(total_width1 - total_width2) * 0.5;
460     element_info* s1 = new element_info(mat, mat->get_glyph("cover"), sign_height, coverSize);
461     element_info* s2 = new element_info(mat, mat->get_glyph("cover"), sign_height, coverSize);
462   
463     if (total_width1 < total_width2) {            
464         elements1.insert(elements1.begin(), s1);
465         elements1.push_back(s2);
466     } else if (total_width2 < total_width1) {
467         elements2.insert(elements2.begin(), s1);
468         elements2.push_back(s2);
469     } else {
470         delete s1;
471         delete s2;
472     }
473     
474 // position the sign
475     const osg::Vec3 Z_AXIS(0, 0, 1);
476     osg::Matrix m(makeZUpFrame(pos));
477     m.preMultRotate(osg::Quat(SGMiscd::deg2rad(heading), Z_AXIS));
478     
479     // apply the inverse of the group transform, so sign vertices
480     // are relative to the tile center, and hence have a magnitude which
481     // fits in a float with sufficent precision.
482     m.postMult(d->signsGroup->getInverseMatrix());
483     
484     d->makeFace(elements1, hpos, m);
485 // Create back side
486     osg::Matrix back(m);
487     back.preMultRotate(osg::Quat(M_PI, Z_AXIS));
488     d->makeFace(elements2, hpos, back);
489     
490     d->signCaseGeometry->addSignCase(boxwidth, sign_height, m);
491 }
492
493 } // of namespace simgear
494