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>
29 #include <osg/StateSet>
31 #include <osg/Geometry>
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>
41 #include "apt_signs.hxx"
43 #define SIGN "OBJECT_SIGN: "
44 #define RWY "OBJECT_RUNWAY_SIGN: "
47 using namespace simgear;
49 // for temporary storage of sign elements
51 element_info(SGMaterial *m, Effect *s, SGMaterialGlyph *g, double h)
52 : material(m), state(s), glyph(g), height(h)
54 scale = h * m->get_xsize()
55 / (m->get_ysize() < 0.001 ? 1.0 : m->get_ysize());
59 SGMaterialGlyph *glyph;
65 // standard panel height sizes
66 const double HT[5] = {0.460, 0.610, 0.760, 1.220, 0.760};
69 // translation table for "command" to "glyph name"
72 const char *glyph_name;
90 // see $FG_ROOT/Docs/README.scenery
93 SGMakeSign(SGMaterialLib *matlib, const string& path, const string& content)
95 double sign_height = 1.0; // meter
97 string newmat = "BlackSign";
99 vector<element_info *> elements;
100 element_info *close = 0;
101 double total_width = 0.0;
104 char oldtype = 0, newtype = 0;
106 osg::Group* object = new osg::Group;
107 object->setName(content);
109 SGMaterial *material;
110 Effect *lighted_state;
111 Effect *unlighted_state;
113 // Part I: parse & measure
114 for (const char *s = content.data(); *s; s++) {
120 SG_LOG(SG_TERRAIN, SG_ALERT, SIGN "unexpected { in sign contents");
124 } else if (*s == '}') {
126 SG_LOG(SG_TERRAIN, SG_ALERT, SIGN "unexpected } in sign contents");
139 if (s[1] == ',' || s[1] == '}' || s[1] == '=')
143 SG_LOG(SG_TERRAIN, SG_ALERT, SIGN "unclosed { in sign contents");
144 } else if (s[1] == '=') {
145 for (s += 2; *s; s++) {
147 if (s[1] == ',' || s[1] == '}')
151 SG_LOG(SG_TERRAIN, SG_ALERT, SIGN "unclosed { in sign contents");
154 if (name == "@size") {
155 sign_height = strtod(value.data(), 0);
159 if (name == "@light") {
160 lighted = (value != "0" && value != "no" && value != "off" && value != "false");
164 if (name == "@material") {
165 newmat = value.data();
168 } else if (name.size() == 2 || name.size() == 3) {
170 if (n.size() == 3 && n[2] >= '1' && n[2] <= '5') {
177 sign_height = HT[size < 0 ? 2 : size];
178 newmat = "YellowSign";
183 } else if (n == "@R") {
185 sign_height = HT[size < 0 ? 2 : size];
191 } else if (n == "@L") {
193 sign_height = HT[size < 0 ? 2 : size];
194 newmat = "FramedSign";
199 } else if (n == "@B") {
200 if (size < 0 || size == 3 || size == 4) {
201 sign_height = HT[size < 0 ? 3 : size];
202 newmat = "BlackSign";
209 for (int i = 0; cmds[i].keyword; i++) {
210 if (name == cmds[i].keyword) {
211 name = cmds[i].glyph_name;
216 if (name[0] == '@') {
217 SG_LOG(SG_TERRAIN, SG_ALERT, SIGN "ignoring unknown command `" << name << '\'');
223 material = matlib->find(newmat);
225 SG_LOG(SG_TERRAIN, SG_ALERT, SIGN "ignoring unknown material `" << newmat << '\'');
229 // set material states (lighted & unlighted)
230 lighted_state = material->get_effect();
231 string u = newmat + ".unlighted";
233 SGMaterial *m = matlib->find(u);
235 unlighted_state = m->get_effect();
237 SG_LOG(SG_TERRAIN, SG_ALERT, SIGN "ignoring unknown material `" << u << '\'');
238 unlighted_state = lighted_state;
243 SGMaterialGlyph *glyph = material->get_glyph(name);
245 SG_LOG( SG_TERRAIN, SG_ALERT, SIGN "unsupported glyph `" << *s << '\'');
249 // in managed mode push frame stop and frame start first
250 Effect *state = lighted ? lighted_state : unlighted_state;
252 if (newtype && newtype != oldtype) {
254 elements.push_back(close);
255 total_width += close->glyph->get_width() * close->scale;
259 SGMaterialGlyph *g = material->get_glyph("stop-frame");
261 close = new element_info(material, state, g, sign_height);
262 g = material->get_glyph("start-frame");
264 e = new element_info(material, state, g, sign_height);
265 elements.push_back(e);
266 total_width += e->glyph->get_width() * e->scale;
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;
275 // in managed mode close frame
277 elements.push_back(close);
278 total_width += close->glyph->get_width() * close->scale;
284 double hpos = -total_width / 2;
285 const double dist = 0.25; // hard-code distance from surface for now
287 for (unsigned int i = 0; i < elements.size(); i++) {
288 element_info *element = elements[i];
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;
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));
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));
310 osg::Vec3Array* nl = new osg::Vec3Array;
311 nl->push_back(osg::Vec3(0, -1, 0));
314 osg::Vec4Array* cl = new osg::Vec4Array;
315 cl->push_back(osg::Vec4(1, 1, 1, 1));
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);
329 object->addChild(geode);
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));
342 osg::Vec3Array* nl = new osg::Vec3Array;
343 nl->push_back(osg::Vec3(0, 1, 0));
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");
354 geode->setEffect(mat->get_effect());
355 object->addChild(geode);
361 SGMakeRunwaySign(SGMaterialLib *matlib, const string& path, const string& name)
363 // for demo purposes we assume each element (letter) is 1x1 meter.
364 // Sign is placed 0.25 meters above the ground
366 float width = name.length() / 3.0;
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);
378 geode->setEffect(mat->get_effect());