]> git.mxchange.org Git - simgear.git/blob - simgear/scene/tgdb/apt_signs.cxx
Construct effects from property lists
[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     SGMaterial *material;
110     Effect *lighted_state;
111     Effect *unlighted_state;
112
113     // Part I: parse & measure
114     for (const char *s = content.data(); *s; s++) {
115         string name;
116         string value;
117
118         if (*s == '{') {
119             if (cmd)
120                 SG_LOG(SG_TERRAIN, SG_ALERT, SIGN "unexpected { in sign contents");
121             cmd = true;
122             continue;
123
124         } else if (*s == '}') {
125             if (!cmd)
126                 SG_LOG(SG_TERRAIN, SG_ALERT, SIGN "unexpected } in sign contents");
127             cmd = false;
128             continue;
129
130         } else if (!cmd) {
131             name = *s;
132
133         } else {
134             if (*s == ',')
135                 continue;
136
137             for (; *s; s++) {
138                 name += *s;
139                 if (s[1] == ',' || s[1] == '}' || s[1] == '=')
140                     break;
141             }
142             if (!*s) {
143                 SG_LOG(SG_TERRAIN, SG_ALERT, SIGN "unclosed { in sign contents");
144             } else if (s[1] == '=') {
145                 for (s += 2; *s; s++) {
146                     value += *s;
147                     if (s[1] == ',' || s[1] == '}')
148                         break;
149                 }
150                 if (!*s)
151                     SG_LOG(SG_TERRAIN, SG_ALERT, SIGN "unclosed { in sign contents");
152             }
153
154             if (name == "@size") {
155                 sign_height = strtod(value.data(), 0);
156                 continue;
157             }
158
159             if (name == "@light") {
160                 lighted = (value != "0" && value != "no" && value != "off" && value != "false");
161                 continue;
162             }
163
164             if (name == "@material") {
165                 newmat = value.data();
166                 continue;
167
168             } else if (name.size() == 2 || name.size() == 3) {
169                 string n = name;
170                 if (n.size() == 3 && n[2] >= '1' && n[2] <= '5') {
171                     size = n[2] - '1';
172                     n = n.substr(0, 2);
173                 }
174
175                 if (n == "@Y") {
176                     if (size < 3) {
177                         sign_height = HT[size < 0 ? 2 : size];
178                         newmat = "YellowSign";
179                         newtype = 'Y';
180                         continue;
181                     }
182
183                 } else if (n == "@R") {
184                     if (size < 3) {
185                         sign_height = HT[size < 0 ? 2 : size];
186                         newmat = "RedSign";
187                         newtype = 'R';
188                         continue;
189                     }
190
191                 } else if (n == "@L") {
192                     if (size < 3) {
193                         sign_height = HT[size < 0 ? 2 : size];
194                         newmat = "FramedSign";
195                         newtype = 'L';
196                         continue;
197                     }
198
199                 } else if (n == "@B") {
200                     if (size < 0 || size == 3 || size == 4) {
201                         sign_height = HT[size < 0 ? 3 : size];
202                         newmat = "BlackSign";
203                         newtype = 'B';
204                         continue;
205                     }
206                 }
207             }
208
209             for (int i = 0; cmds[i].keyword; i++) {
210                 if (name == cmds[i].keyword) {
211                     name = cmds[i].glyph_name;
212                     break;
213                 }
214             }
215
216             if (name[0] == '@') {
217                 SG_LOG(SG_TERRAIN, SG_ALERT, SIGN "ignoring unknown command `" << name << '\'');
218                 continue;
219             }
220         }
221
222         if (newmat.size()) {
223             material = matlib->find(newmat);
224             if (!material) {
225                 SG_LOG(SG_TERRAIN, SG_ALERT, SIGN "ignoring unknown material `" << newmat << '\'');
226                 continue;
227             }
228
229             // set material states (lighted & unlighted)
230             lighted_state = material->get_effect();
231             string u = newmat + ".unlighted";
232
233             SGMaterial *m = matlib->find(u);
234             if (m) {
235                 unlighted_state = m->get_effect();
236             } else {
237                 SG_LOG(SG_TERRAIN, SG_ALERT, SIGN "ignoring unknown material `" << u << '\'');
238                 unlighted_state = lighted_state;
239             }
240             newmat = "";
241         }
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 }