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>
27 #include <simgear/debug/logstream.hxx>
28 #include <simgear/math/sg_types.hxx>
29 #include <simgear/scene/tgdb/leaf.hxx>
30 #include <simgear/scene/material/mat.hxx>
31 #include <simgear/scene/material/matlib.hxx>
33 #include "apt_signs.hxx"
35 #define TAXI "OBJECT_TAXI_SIGN: "
36 #define RWY "OBJECT_RUNWAY_SIGN: "
39 // for temporary storage of sign elements
41 element_info(SGMaterial *m, SGMaterialGlyph *g) : material(m), glyph(g) {}
43 SGMaterialGlyph *glyph;
47 // translation table for 'command' to glyph name
50 const char *glyph_name;
64 {"no-exit", "no-exit"},
69 // Y ... black on yellow -> direction, destination, boundary
70 // R ... white on red -> mandatory instruction
71 // L ... yellow on black with frame -> runway/taxiway location
72 // B ... white on black -> runway distance remaining
74 // u/d/l/r ... up/down/left/right arrow or two-letter combinations
75 // thereof (ur, dl, ...)
77 // Example: {l}E|{L}[T]|{Y,ur}L|E{r}
79 ssgBranch *sgMakeTaxiSign( SGMaterialLib *matlib,
80 const string path, const string content )
82 const double sign_height = 1.0; // meter TODO make configurable
84 vector<element_info *> elements;
85 double total_width = 0.0;
87 char *newmat = "YellowSign";
89 ssgBranch *object = new ssgBranch();
90 object->setName((char *)content.c_str());
91 SGMaterial *material = matlib->find(newmat);
93 for (const char *s = content.data(); *s; s++) {
95 const char *newmat = 0;
99 SG_LOG(SG_TERRAIN, SG_ALERT, TAXI "unexpected { in sign contents");
102 } else if (*s == '}') {
104 SG_LOG(SG_TERRAIN, SG_ALERT, TAXI "unexpected } in sign contents");
111 newmat = "YellowSign";
115 newmat = "FramedSign";
117 newmat = "BlackSign";
119 // find longest match of cmds[]
121 for (int i = 0; cmds[i].keyword; i++) {
122 len = strlen(cmds[i].keyword);
123 if (!strncmp(s, cmds[i].keyword, len)) {
125 name = cmds[i].glyph_name;
131 if (s[1] != ',' && s[1] != '}')
132 SG_LOG(SG_TERRAIN, SG_ALERT, TAXI "garbage after command `" << s << '\'');
135 material = matlib->find(newmat);
140 SG_LOG(SG_TERRAIN, SG_ALERT, TAXI "unknown command `" << s << '\'');
147 SG_LOG( SG_TERRAIN, SG_ALERT, TAXI "material doesn't exit");
151 SGMaterialGlyph *glyph = material->get_glyph(name);
153 SG_LOG( SG_TERRAIN, SG_ALERT, TAXI "unsupported character `" << *s << '\'');
157 elements.push_back(new element_info(material, glyph));
158 total_width += glyph->get_width() * material->get_xscale();
161 double hpos = -total_width / 2;
164 sgSetVec3(normal, 0, -1, 0);
167 sgSetVec4(color, 1.0, 1.0, 1.0, 1.0);
169 for (unsigned int i = 0; i < elements.size(); i++) {
170 element_info *element = elements[i];
172 double xoffset = element->glyph->get_left();
173 double width = element->glyph->get_width();
174 double abswidth = width * element->material->get_xscale();
177 ssgVertexArray *vl = new ssgVertexArray(4);
179 vl->add(hpos + abswidth, 0, 0);
180 vl->add(hpos, 0, sign_height);
181 vl->add(hpos + abswidth, 0, sign_height);
183 // texture coordinates
184 ssgTexCoordArray *tl = new ssgTexCoordArray(4);
186 tl->add(xoffset + width, 0);
188 tl->add(xoffset + width, 1);
191 ssgNormalArray *nl = new ssgNormalArray(1);
195 ssgColourArray *cl = new ssgColourArray(1);
198 ssgLeaf *leaf = new ssgVtxTable(GL_TRIANGLE_STRIP, vl, nl, tl, cl);
199 ssgSimpleState *state = element->material->get_state();
201 // state->setMaterial(GL_EMISSION, 0, 0, 0, 1);
202 leaf->setState(state);
204 object->addKid(leaf);
216 ssgBranch *sgMakeRunwaySign( SGMaterialLib *matlib,
217 const string path, const string name )
219 // for demo purposes we assume each element (letter) is 1x1 meter.
220 // Sign is placed 0.25 meters above the ground
222 ssgBranch *object = new ssgBranch();
223 object->setName( (char *)name.c_str() );
225 double width = name.length() / 3.0;
227 string material = name;
231 point_list texcoords;
232 int_list vertex_index;
233 int_list normal_index;
236 nodes.push_back( Point3D( -width, 0, 0.25 ) );
237 nodes.push_back( Point3D( width + 1, 0, 0.25 ) );
238 nodes.push_back( Point3D( -width, 0, 1.25 ) );
239 nodes.push_back( Point3D( width + 1, 0, 1.25 ) );
241 normals.push_back( Point3D( 0, -1, 0 ) );
243 texcoords.push_back( Point3D( 0, 0, 0 ) );
244 texcoords.push_back( Point3D( 1, 0, 0 ) );
245 texcoords.push_back( Point3D( 0, 1, 0 ) );
246 texcoords.push_back( Point3D( 1, 1, 0 ) );
248 vertex_index.push_back( 0 );
249 vertex_index.push_back( 1 );
250 vertex_index.push_back( 2 );
251 vertex_index.push_back( 3 );
253 normal_index.push_back( 0 );
254 normal_index.push_back( 0 );
255 normal_index.push_back( 0 );
256 normal_index.push_back( 0 );
258 tex_index.push_back( 0 );
259 tex_index.push_back( 1 );
260 tex_index.push_back( 2 );
261 tex_index.push_back( 3 );
263 ssgLeaf *leaf = sgMakeLeaf( path, GL_TRIANGLE_STRIP, matlib, material,
264 nodes, normals, texcoords,
265 vertex_index, normal_index, tex_index,
268 object->addKid( leaf );