]> git.mxchange.org Git - simgear.git/blob - simgear/scene/tgdb/apt_signs.cxx
Clement de l'Hamaide: Add support for pitch and roll for OBJECT_SHARED and OBJECT_STA...
[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     double texsize = caseWidth / caseHeight;
129
130     //left
131     vertices->push_back(osg::Vec3(-thick, -caseWidth,  grounddist));
132     vertices->push_back(osg::Vec3(thick, -caseWidth,  grounddist));
133     vertices->push_back(osg::Vec3(thick, -caseWidth,  grounddist + caseHeight));
134     vertices->push_back(osg::Vec3(-thick, -caseWidth,  grounddist + caseHeight));
135
136     uvs->push_back(osg::Vec2(1,    1));
137     uvs->push_back(osg::Vec2(0.75, 1));
138     uvs->push_back(osg::Vec2(0.75, 0));
139     uvs->push_back(osg::Vec2(1,    0));
140     
141     for (int i=0; i<4; ++i)
142       normals->push_back(osg::Vec3(-1, 0.0, 0));
143
144     //top
145     vertices->push_back(osg::Vec3(-thick, -caseWidth,  grounddist + caseHeight));
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 + caseHeight));
149     
150     uvs->push_back(osg::Vec2(1,    texsize));
151     uvs->push_back(osg::Vec2(0.75, texsize));
152     uvs->push_back(osg::Vec2(0.75, 0));
153     uvs->push_back(osg::Vec2(1,    0));
154
155     for (int i=0; i<4; ++i)
156       normals->push_back(osg::Vec3(0, 0, 1));
157
158     //right
159     vertices->push_back(osg::Vec3(-thick, caseWidth, grounddist + caseHeight));
160     vertices->push_back(osg::Vec3(thick,  caseWidth, grounddist + caseHeight));
161     vertices->push_back(osg::Vec3(thick,  caseWidth, grounddist));
162     vertices->push_back(osg::Vec3(-thick, caseWidth, grounddist));
163
164     uvs->push_back(osg::Vec2(1,    1));
165     uvs->push_back(osg::Vec2(0.75, 1));
166     uvs->push_back(osg::Vec2(0.75, 0));
167     uvs->push_back(osg::Vec2(1,    0));
168
169     for (int i=0; i<4; ++i)
170       normals->push_back(osg::Vec3(1, 0.0, 0));
171
172
173   // transform all the newly added vertices and normals by the matrix
174     for (unsigned int i=last; i<vertices->size(); ++i) {
175       (*vertices)[i]= xform.preMult((*vertices)[i]);
176       (*normals)[i] = xform.preMult((*normals)[i]);
177     }
178     
179     quads->setCount(vertices->size());
180   }
181 };
182
183 typedef std::map<Effect*, GlyphGeometry*> EffectGeometryMap;
184
185 GlyphGeometry* makeGeometry(Effect* eff, osg::Group* group)
186 {
187   GlyphGeometry* gg = new GlyphGeometry;
188   
189   EffectGeode* geode = new EffectGeode;
190   geode->setEffect(eff);
191  
192   gg->vertices = new osg::Vec3Array;
193   gg->normals = new osg::Vec3Array;
194   gg->uvs = new osg::Vec2Array;
195   
196   osg::Vec4Array* cl = new osg::Vec4Array;
197   cl->push_back(osg::Vec4(1, 1, 1, 1));
198   
199   osg::Geometry* geometry = new osg::Geometry;
200   geometry->setVertexArray(gg->vertices);
201   geometry->setNormalArray(gg->normals);
202   geometry->setNormalBinding(osg::Geometry::BIND_PER_VERTEX);
203   geometry->setColorArray(cl);
204   geometry->setColorBinding(osg::Geometry::BIND_OVERALL);
205   geometry->setTexCoordArray(0, gg->uvs);
206   
207   gg->quads = new osg::DrawArrays(GL_QUADS, 0, gg->vertices->size());
208   geometry->addPrimitiveSet(gg->quads);
209   geode->addDrawable(geometry);
210   group->addChild(geode);
211   return gg;
212 }
213
214 // see $FG_ROOT/Docs/README.scenery
215
216 namespace simgear
217 {
218
219 class AirportSignBuilder::AirportSignBuilderPrivate
220 {
221 public:
222     SGMaterialLib* materials;
223     EffectGeometryMap geometries;
224     osg::MatrixTransform* signsGroup;
225     GlyphGeometry* signCaseGeometry;
226     
227     GlyphGeometry* getGeometry(Effect* eff)
228     {
229         EffectGeometryMap::iterator it = geometries.find(eff);
230         if (it != geometries.end()) {
231             return it->second;
232         }
233         
234         GlyphGeometry* gg = makeGeometry(eff, signsGroup);
235         geometries[eff] = gg;
236         return gg;
237     }
238     
239     void makeFace(const ElementVec& elements, double hpos, const osg::Matrix& xform)
240     {
241         BOOST_FOREACH(element_info* element, elements) {
242             GlyphGeometry* gg = getGeometry(element->material->get_effect());
243             gg->addGlyph(element->glyph, hpos, grounddist, element->abswidth, element->height, xform);
244             hpos += element->abswidth;
245             delete element;
246         }
247     }
248     
249 };
250
251 AirportSignBuilder::AirportSignBuilder(SGMaterialLib* mats, const SGGeod& center) :
252     d(new AirportSignBuilderPrivate)
253 {
254     d->signsGroup = new osg::MatrixTransform;
255     d->signsGroup->setMatrix(makeZUpFrame(center));
256     
257     assert(mats);
258     d->materials = mats;
259     d->signCaseGeometry = d->getGeometry(d->materials->find("signcase")->get_effect());
260 }
261
262 osg::Node* AirportSignBuilder::getSignsGroup()
263 {
264     return d->signsGroup;
265 }
266
267 AirportSignBuilder::~AirportSignBuilder()
268 {
269     EffectGeometryMap::iterator it;
270     for (it = d->geometries.begin(); it != d->geometries.end(); ++it) {
271         delete it->second;
272     }
273 }
274
275 void AirportSignBuilder::addSign(const SGGeod& pos, double heading, const std::string& content)
276 {
277     double sign_height = 1.0;  // meter
278     string newmat = "BlackSign";
279     ElementVec elements1, elements2;
280     element_info *close1 = 0;
281     element_info *close2 = 0;
282     double total_width1 = 0.0;
283     double total_width2 = 0.0;
284     bool cmd = false;
285     bool isBackside = false;
286     int size = -1;
287     char oldtype = 0, newtype = 0;
288     SGMaterial *material = 0;
289
290     // Part I: parse & measure
291     for (const char *s = content.data(); *s; s++) {
292         string name;
293         string value;
294
295         if (*s == '{') {
296             if (cmd)
297                 SG_LOG(SG_TERRAIN, SG_ALERT, SIGN "Illegal taxiway sign syntax. Unexpected '{' in '" << content << "'.");
298             cmd = true;
299             continue;
300
301         } else if (*s == '}') {
302             if (!cmd)
303                 SG_LOG(SG_TERRAIN, SG_ALERT, SIGN "Illegal taxiway sign syntax. Unexpected '}' in '" << content << "'.");
304             cmd = false;
305             continue;
306
307         } else if (!cmd) {
308             name = *s;
309
310         } else {
311             if (*s == ',')
312                 continue;
313
314             for (; *s; s++) {
315                 name += *s;
316                 if (s[1] == ',' || s[1] == '}' || s[1] == '=')
317                     break;
318             }
319             if (!*s) {
320                 SG_LOG(SG_TERRAIN, SG_ALERT, SIGN "unclosed { in sign contents");
321             } else if (s[1] == '=') {
322                 for (s += 2; *s; s++) {
323                     value += *s;
324                     if (s[1] == ',' || s[1] == '}')
325                         break;
326                 }
327                 if (!*s)
328                     SG_LOG(SG_TERRAIN, SG_ALERT, SIGN "unclosed { in sign contents");
329             }
330
331             if (name == "@@") {
332                 isBackside = true;
333                 continue;
334             }
335
336             if (name == "@size") {
337                 sign_height = strtod(value.data(), 0);
338                 continue;
339             }
340
341             if (name.size() == 2 || name.size() == 3) {
342                 string n = name;
343                 if (n.size() == 3 && n[2] >= '1' && n[2] <= '5') {
344                     size = n[2] - '1';
345                     n = n.substr(0, 2);
346                 }
347                 if (n == "@Y") {
348                     if (size < 3) {
349                         sign_height = HT[size < 0 ? 2 : size];
350                         newmat = "YellowSign";
351                         newtype = 'Y';
352                         continue;
353                     }
354
355                 } else if (n == "@R") {
356                     if (size < 3) {
357                         sign_height = HT[size < 0 ? 2 : size];
358                         newmat = "RedSign";
359                         newtype = 'R';
360                         continue;
361                     }
362
363                 } else if (n == "@L") {
364                     if (size < 3) {
365                         sign_height = HT[size < 0 ? 2 : size];
366                         newmat = "FramedSign";
367                         newtype = 'L';
368                         continue;
369                     }
370
371                 } else if (n == "@B") {
372                     if (size < 0 || size == 3 || size == 4) {
373                         sign_height = HT[size < 0 ? 3 : size];
374                         newmat = "BlackSign";
375                         newtype = 'B';
376                         continue;
377                     }
378                 }
379             }
380
381             for (int i = 0; cmds[i].keyword; i++) {
382                 if (name == cmds[i].keyword) {
383                     name = cmds[i].glyph_name;
384                     break;
385                 }
386             }
387
388             if (name[0] == '@') {
389                 SG_LOG(SG_TERRAIN, SG_ALERT, SIGN "ignoring unknown command `" << name << '\'');
390                 continue;
391             }
392         }
393
394         if (newmat.size()) {
395             material = d->materials->find(newmat);
396             newmat.clear();
397         }
398  
399         SGMaterialGlyph *glyph = material->get_glyph(name);
400         if (!glyph) {
401             SG_LOG( SG_TERRAIN, SG_ALERT, SIGN "unsupported glyph '" << *s << '\'');
402             continue;
403         }
404
405     // in managed mode push frame stop and frame start first
406         if (!isBackside) {
407             if (newtype && newtype != oldtype) {
408                 if (close1) {
409                     elements1.push_back(close1);
410                     total_width1 += close1->glyph->get_width() * close1->scale;
411                     close1 = 0;
412                 }
413                 oldtype = newtype;
414                 SGMaterialGlyph *g = material->get_glyph("stop-frame");
415                 if (g)
416                     close1 = new element_info(material, g, sign_height, 0);
417                 g = material->get_glyph("start-frame");
418                 if (g) {
419                     element_info* e1 = new element_info(material, g, sign_height, 0);
420                     elements1.push_back(e1);
421                     total_width1 += e1->glyph->get_width() * e1->scale;
422                 }
423             }
424             // now the actually requested glyph (front)
425             element_info* e1 = new element_info(material, glyph, sign_height, 0);
426             elements1.push_back(e1);
427             total_width1 += e1->glyph->get_width() * e1->scale;
428         } else {
429             if (newtype && newtype != oldtype) {
430                 if (close2) {
431                     elements2.push_back(close2);
432                     total_width2 += close2->glyph->get_width() * close2->scale;
433                     close2 = 0;
434                 }
435                 oldtype = newtype;
436                 SGMaterialGlyph *g = material->get_glyph("stop-frame");
437                 if (g)
438                     close2 = new element_info(material, g, sign_height, 0);
439                 g = material->get_glyph("start-frame");
440                 if (g) {
441                     element_info* e2 = new element_info(material, g, sign_height, 0);
442                     elements2.push_back(e2);
443                     total_width2 += e2->glyph->get_width() * e2->scale;
444                 }
445             }
446             // now the actually requested glyph (back)
447             element_info* e2 = new element_info(material, glyph, sign_height, 0);
448             elements2.push_back(e2);
449             total_width2 += e2->glyph->get_width() * e2->scale;
450         }
451     }
452
453     // in managed mode close frame
454     if (close1) {
455         elements1.push_back(close1);
456         total_width1 += close1->glyph->get_width() * close1->scale;
457         close1 = 0;
458     }
459
460     if (close2) {
461         elements2.push_back(close2);
462         total_width2 += close2->glyph->get_width() * close2->scale;
463         close2 = 0;
464     }
465
466   // Part II: typeset
467     double boxwidth = std::max(total_width1, total_width2) * 0.5;
468     double hpos = -boxwidth;
469     SGMaterial *mat = d->materials->find("signcase");
470   
471     double coverSize = fabs(total_width1 - total_width2) * 0.5;
472     element_info* s1 = new element_info(mat, mat->get_glyph("cover1"), sign_height, coverSize);
473     element_info* s2 = new element_info(mat, mat->get_glyph("cover2"), sign_height, coverSize);
474   
475     if (total_width1 < total_width2) {            
476         elements1.insert(elements1.begin(), s1);
477         elements1.push_back(s2);
478     } else if (total_width2 < total_width1) {
479         elements2.insert(elements2.begin(), s1);
480         elements2.push_back(s2);
481     } else {
482         delete s1;
483         delete s2;
484     }
485     
486 // position the sign
487     const osg::Vec3 Z_AXIS(0, 0, 1);
488     osg::Matrix m(makeZUpFrame(pos));
489     m.preMultRotate(osg::Quat(SGMiscd::deg2rad(heading), Z_AXIS));
490     
491     // apply the inverse of the group transform, so sign vertices
492     // are relative to the tile center, and hence have a magnitude which
493     // fits in a float with sufficent precision.
494     m.postMult(d->signsGroup->getInverseMatrix());
495     
496     d->makeFace(elements1, hpos, m);
497 // Create back side
498     osg::Matrix back(m);
499     back.preMultRotate(osg::Quat(M_PI, Z_AXIS));
500     d->makeFace(elements2, hpos, back);
501     
502     d->signCaseGeometry->addSignCase(boxwidth, sign_height, m);
503 }
504
505 } // of namespace simgear
506