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