]> git.mxchange.org Git - simgear.git/blob - simgear/scene/tgdb/apt_signs.cxx
Merge branch 'frohlich/weak' into next
[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
29 #include <osg/StateSet>
30 #include <osg/Geode>
31 #include <osg/Geometry>
32 #include <osg/Group>
33
34 #include <simgear/debug/logstream.hxx>
35 #include <simgear/math/sg_types.hxx>
36 #include <simgear/scene/material/Effect.hxx>
37 #include <simgear/scene/material/EffectGeode.hxx>
38 #include <simgear/scene/material/mat.hxx>
39 #include <simgear/scene/material/matlib.hxx>
40
41 #include "apt_signs.hxx"
42
43 #define SIGN "OBJECT_SIGN: "
44 #define RWY "OBJECT_RUNWAY_SIGN: "
45
46 using std::vector;
47 using namespace simgear;
48
49 // for temporary storage of sign elements
50 struct element_info {
51     element_info(SGMaterial *m, Effect *s, SGMaterialGlyph *g, double h)
52         : material(m), state(s), glyph(g), height(h)
53     {
54         scale = h * m->get_xsize()
55                 / (m->get_ysize() < 0.001 ? 1.0 : m->get_ysize());
56     }
57     SGMaterial *material;
58     Effect *state;
59     SGMaterialGlyph *glyph;
60     double height;
61     double scale;
62 };
63
64
65 // standard panel height sizes
66 const double HT[5] = {0.460, 0.610, 0.760, 1.220, 0.760};
67
68
69 // translation table for "command" to "glyph name"
70 struct pair {
71     const char *keyword;
72     const char *glyph_name;
73 } cmds[] = {
74     {"@u",       "^u"},
75     {"@d",       "^d"},
76     {"@l",       "^l"},
77     {"@lu",      "^lu"},
78     {"@ul",      "^lu"},
79     {"@ld",      "^ld"},
80     {"@dl",      "^ld"},
81     {"@r",       "^r"},
82     {"@ru",      "^ru"},
83     {"@ur",      "^ru"},
84     {"@rd",      "^rd"},
85     {"@dr",      "^rd"},
86     {0, 0},
87 };
88
89
90 // see $FG_ROOT/Docs/README.scenery
91 //
92 osg::Node*
93 SGMakeSign(SGMaterialLib *matlib, const string& path, const string& content)
94 {
95     double sign_height = 1.0;  // meter
96     bool lighted = true;
97     string newmat = "BlackSign";
98
99     vector<element_info *> elements;
100     element_info *close = 0;
101     double total_width = 0.0;
102     bool cmd = false;
103     int size = -1;
104     char oldtype = 0, newtype = 0;
105
106     osg::Group* object = new osg::Group;
107     object->setName(content);
108
109     // Part I: parse & measure
110     for (const char *s = content.data(); *s; s++) {
111         string name;
112         string value;
113
114         if (*s == '{') {
115             if (cmd)
116                 SG_LOG(SG_TERRAIN, SG_ALERT, SIGN "unexpected { in sign contents");
117             cmd = true;
118             continue;
119
120         } else if (*s == '}') {
121             if (!cmd)
122                 SG_LOG(SG_TERRAIN, SG_ALERT, SIGN "unexpected } in sign contents");
123             cmd = false;
124             continue;
125
126         } else if (!cmd) {
127             name = *s;
128
129         } else {
130             if (*s == ',')
131                 continue;
132
133             for (; *s; s++) {
134                 name += *s;
135                 if (s[1] == ',' || s[1] == '}' || s[1] == '=')
136                     break;
137             }
138             if (!*s) {
139                 SG_LOG(SG_TERRAIN, SG_ALERT, SIGN "unclosed { in sign contents");
140             } else if (s[1] == '=') {
141                 for (s += 2; *s; s++) {
142                     value += *s;
143                     if (s[1] == ',' || s[1] == '}')
144                         break;
145                 }
146                 if (!*s)
147                     SG_LOG(SG_TERRAIN, SG_ALERT, SIGN "unclosed { in sign contents");
148             }
149
150             if (name == "@size") {
151                 sign_height = strtod(value.data(), 0);
152                 continue;
153             }
154
155             if (name == "@light") {
156                 lighted = (value != "0" && value != "no" && value != "off" && value != "false");
157                 continue;
158             }
159
160             if (name == "@material") {
161                 newmat = value.data();
162                 continue;
163
164             } else if (name.size() == 2 || name.size() == 3) {
165                 string n = name;
166                 if (n.size() == 3 && n[2] >= '1' && n[2] <= '5') {
167                     size = n[2] - '1';
168                     n = n.substr(0, 2);
169                 }
170
171                 if (n == "@Y") {
172                     if (size < 3) {
173                         sign_height = HT[size < 0 ? 2 : size];
174                         newmat = "YellowSign";
175                         newtype = 'Y';
176                         continue;
177                     }
178
179                 } else if (n == "@R") {
180                     if (size < 3) {
181                         sign_height = HT[size < 0 ? 2 : size];
182                         newmat = "RedSign";
183                         newtype = 'R';
184                         continue;
185                     }
186
187                 } else if (n == "@L") {
188                     if (size < 3) {
189                         sign_height = HT[size < 0 ? 2 : size];
190                         newmat = "FramedSign";
191                         newtype = 'L';
192                         continue;
193                     }
194
195                 } else if (n == "@B") {
196                     if (size < 0 || size == 3 || size == 4) {
197                         sign_height = HT[size < 0 ? 3 : size];
198                         newmat = "BlackSign";
199                         newtype = 'B';
200                         continue;
201                     }
202                 }
203             }
204
205             for (int i = 0; cmds[i].keyword; i++) {
206                 if (name == cmds[i].keyword) {
207                     name = cmds[i].glyph_name;
208                     break;
209                 }
210             }
211
212             if (name[0] == '@') {
213                 SG_LOG(SG_TERRAIN, SG_ALERT, SIGN "ignoring unknown command `" << name << '\'');
214                 continue;
215             }
216         }
217
218
219         if (newmat.size() == 0 )
220           continue;
221
222         SGMaterial *material = matlib->find(newmat);
223         if (!material) {
224             SG_LOG(SG_TERRAIN, SG_ALERT, SIGN "ignoring unknown material `" << newmat << '\'');
225             continue;
226         }
227
228
229         // set material states (lighted & unlighted)
230         Effect *lighted_state = material->get_effect();
231         Effect *unlighted_state;
232         string u = newmat + ".unlighted";
233
234         SGMaterial *m = matlib->find(u);
235         if (m) {
236             unlighted_state = m->get_effect();
237         } else {
238             SG_LOG(SG_TERRAIN, SG_ALERT, SIGN "ignoring unknown material `" << u << '\'');
239             unlighted_state = lighted_state;
240         }
241         newmat = "";
242
243         SGMaterialGlyph *glyph = material->get_glyph(name);
244         if (!glyph) {
245             SG_LOG( SG_TERRAIN, SG_ALERT, SIGN "unsupported glyph `" << *s << '\'');
246             continue;
247         }
248
249         // in managed mode push frame stop and frame start first
250         Effect *state = lighted ? lighted_state : unlighted_state;
251         element_info *e;
252         if (newtype && newtype != oldtype) {
253             if (close) {
254                 elements.push_back(close);
255                 total_width += close->glyph->get_width() * close->scale;
256                 close = 0;
257             }
258             oldtype = newtype;
259             SGMaterialGlyph *g = material->get_glyph("stop-frame");
260             if (g)
261                 close = new element_info(material, state, g, sign_height);
262             g = material->get_glyph("start-frame");
263             if (g) {
264                 e = new element_info(material, state, g, sign_height);
265                 elements.push_back(e);
266                 total_width += e->glyph->get_width() * e->scale;
267             }
268         }
269         // now the actually requested glyph
270         e = new element_info(material, state, glyph, sign_height);
271         elements.push_back(e);
272         total_width += e->glyph->get_width() * e->scale;
273     }
274
275     // in managed mode close frame
276     if (close) {
277         elements.push_back(close);
278         total_width += close->glyph->get_width() * close->scale;
279         close = 0;
280     }
281
282
283     // Part II: typeset
284     double hpos = -total_width / 2;
285     const double dist = 0.25;        // hard-code distance from surface for now
286
287     for (unsigned int i = 0; i < elements.size(); i++) {
288         element_info *element = elements[i];
289
290         double xoffset = element->glyph->get_left();
291         double height = element->height;
292         double width = element->glyph->get_width();
293         double abswidth = width * element->scale;
294
295         // vertices
296         osg::Vec3Array* vl = new osg::Vec3Array;
297         vl->push_back(osg::Vec3(0, hpos,            dist));
298         vl->push_back(osg::Vec3(0, hpos + abswidth, dist));
299         vl->push_back(osg::Vec3(0, hpos,            dist + height));
300         vl->push_back(osg::Vec3(0, hpos + abswidth, dist + height));
301
302         // texture coordinates
303         osg::Vec2Array* tl = new osg::Vec2Array;
304         tl->push_back(osg::Vec2(xoffset,         0));
305         tl->push_back(osg::Vec2(xoffset + width, 0));
306         tl->push_back(osg::Vec2(xoffset,         1));
307         tl->push_back(osg::Vec2(xoffset + width, 1));
308
309         // normals
310         osg::Vec3Array* nl = new osg::Vec3Array;
311         nl->push_back(osg::Vec3(0, -1, 0));
312
313         // colors
314         osg::Vec4Array* cl = new osg::Vec4Array;
315         cl->push_back(osg::Vec4(1, 1, 1, 1));
316
317         osg::Geometry* geometry = new osg::Geometry;
318         geometry->setVertexArray(vl);
319         geometry->setNormalArray(nl);
320         geometry->setNormalBinding(osg::Geometry::BIND_OVERALL);
321         geometry->setColorArray(cl);
322         geometry->setColorBinding(osg::Geometry::BIND_OVERALL);
323         geometry->setTexCoordArray(0, tl);
324         geometry->addPrimitiveSet(new osg::DrawArrays(GL_TRIANGLE_STRIP, 0, vl->size()));
325         EffectGeode* geode = new EffectGeode;
326         geode->addDrawable(geometry);
327         geode->setEffect(element->state);
328
329         object->addChild(geode);
330         hpos += abswidth;
331         delete element;
332     }
333
334
335     // minimalistic backside
336     osg::Vec3Array* vl = new osg::Vec3Array;
337     vl->push_back(osg::Vec3(0, hpos,               dist));
338     vl->push_back(osg::Vec3(0, hpos - total_width, dist));
339     vl->push_back(osg::Vec3(0, hpos,               dist + sign_height));
340     vl->push_back(osg::Vec3(0, hpos - total_width, dist + sign_height));
341
342     osg::Vec3Array* nl = new osg::Vec3Array;
343     nl->push_back(osg::Vec3(0, 1, 0));
344
345     osg::Geometry* geometry = new osg::Geometry;
346     geometry->setVertexArray(vl);
347     geometry->setNormalArray(nl);
348     geometry->setNormalBinding(osg::Geometry::BIND_OVERALL);
349     geometry->addPrimitiveSet(new osg::DrawArrays(GL_TRIANGLE_STRIP, 0, vl->size()));
350     EffectGeode* geode = new EffectGeode;
351     geode->addDrawable(geometry);
352     SGMaterial *mat = matlib->find("BlackSign");
353     if (mat)
354       geode->setEffect(mat->get_effect());
355     object->addChild(geode);
356
357     return object;
358 }
359
360 osg::Node*
361 SGMakeRunwaySign(SGMaterialLib *matlib, const string& path, const string& name)
362 {
363     // for demo purposes we assume each element (letter) is 1x1 meter.
364     // Sign is placed 0.25 meters above the ground
365
366     float width = name.length() / 3.0;
367
368     osg::Vec3 corner(-width, 0, 0.25f);
369     osg::Vec3 widthVec(2*width + 1, 0, 0);
370     osg::Vec3 heightVec(0, 0, 1);
371     osg::Geometry* geometry;
372     geometry = osg::createTexturedQuadGeometry(corner, widthVec, heightVec);
373     EffectGeode* geode = new EffectGeode;
374     geode->setName(name);
375     geode->addDrawable(geometry);
376     SGMaterial *mat = matlib->find(name);
377     if (mat)
378       geode->setEffect(mat->get_effect());
379
380     return geode;
381 }