]> git.mxchange.org Git - flightgear.git/blob - src/Viewer/splash.cxx
Positioned/Cache tweaks to support PoIs.
[flightgear.git] / src / Viewer / splash.cxx
1 // splash.cxx -- draws the initial splash screen
2 //
3 // Written by Curtis Olson, started July 1998.  (With a little looking
4 // at Freidemann's panel code.) :-)
5 //
6 // Copyright (C) 1997  Michele F. America  - nomimarketing@mail.telepac.pt
7 //
8 // This program is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU General Public License as
10 // published by the Free Software Foundation; either version 2 of the
11 // License, or (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful, but
14 // WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 // General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 //
22 // $Id$
23
24
25 #ifdef HAVE_CONFIG_H
26 #  include <config.h>
27 #endif
28
29 #include <osg/BlendFunc>
30 #include <osg/Camera>
31 #include <osg/Depth>
32 #include <osg/Geometry>
33 #include <osg/Node>
34 #include <osg/NodeCallback>
35 #include <osg/NodeVisitor>
36 #include <osg/StateSet>
37 #include <osg/Switch>
38 #include <osg/Texture2D>
39 #include <osgUtil/CullVisitor>
40 #include <osgText/Text>
41 #include <osgDB/ReadFile>
42
43 #include <simgear/compiler.h>
44
45 #include <simgear/debug/logstream.hxx>
46 #include <simgear/math/sg_random.h>
47 #include <simgear/misc/sg_path.hxx>
48
49 #include <plib/fnt.h>
50
51 #include "GUI/FGFontCache.hxx"
52 #include "GUI/FGColor.hxx"
53
54 #include <Main/globals.hxx>
55 #include <Main/fg_props.hxx>
56 #include <Main/fg_os.hxx>
57 #include <Main/locale.hxx>
58 #include "splash.hxx"
59 #include "renderer.hxx"
60
61 class FGSplashUpdateCallback : public osg::Drawable::UpdateCallback {
62 public:
63   FGSplashUpdateCallback(osg::Vec4Array* colorArray, SGPropertyNode* prop) :
64     _colorArray(colorArray),
65     _colorProperty(prop),
66     _alphaProperty(fgGetNode("/sim/startup/splash-alpha", true))
67   { }
68   virtual void update(osg::NodeVisitor*, osg::Drawable*)
69   {
70     FGColor c(0, 0, 0);
71     if (_colorProperty) {
72       c.merge(_colorProperty);
73       (*_colorArray)[0][0] = c.red();
74       (*_colorArray)[0][1] = c.green();
75       (*_colorArray)[0][2] = c.blue();
76     }
77     (*_colorArray)[0][3] = _alphaProperty->getFloatValue();
78     _colorArray->dirty();
79   }
80 private:
81   osg::ref_ptr<osg::Vec4Array> _colorArray;
82   SGSharedPtr<const SGPropertyNode> _colorProperty;
83   SGSharedPtr<const SGPropertyNode> _alphaProperty;
84 };
85
86 class FGSplashTextUpdateCallback : public osg::Drawable::UpdateCallback {
87 public:
88   FGSplashTextUpdateCallback(const SGPropertyNode* prop) :
89     _textProperty(prop),
90     _alphaProperty(fgGetNode("/sim/startup/splash-alpha", true)),
91     _styleProperty(fgGetNode("/sim/gui/style[0]", true))
92   {}
93   virtual void update(osg::NodeVisitor*, osg::Drawable* drawable)
94   {
95     assert(dynamic_cast<osgText::Text*>(drawable));
96     osgText::Text* text = static_cast<osgText::Text*>(drawable);
97
98     FGColor c(1.0, 0.9, 0.0);
99     c.merge(_styleProperty->getNode("colors/splash-font"));
100     float alpha = _alphaProperty->getFloatValue();
101     text->setColor(osg::Vec4(c.red(), c.green(), c.blue(), alpha));
102
103     const char* s = _textProperty->getStringValue();
104     if (s && fgGetBool("/sim/startup/splash-progress", true))
105       text->setText(s);
106     else
107       text->setText("");
108   }
109 private:
110   SGSharedPtr<const SGPropertyNode> _textProperty;
111   SGSharedPtr<const SGPropertyNode> _alphaProperty;
112   SGSharedPtr<const SGPropertyNode> _styleProperty;
113 };
114
115
116
117 class FGSplashContentProjectionCalback : public osg::NodeCallback {
118 public:
119   virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)
120   { 
121     assert(dynamic_cast<osgUtil::CullVisitor*>(nv));
122     osgUtil::CullVisitor* cullVisitor = static_cast<osgUtil::CullVisitor*>(nv);
123
124     // adjust the projection matrix in a way that preserves the aspect ratio
125     // of the content ...
126     const osg::Viewport* viewport = cullVisitor->getViewport();
127     float viewportAspect = float(viewport->height())/float(viewport->width());
128
129     float height, width;
130     if (viewportAspect < 1) {
131       height = 1;
132       width = 1/viewportAspect;
133     } else {
134       height = viewportAspect;
135       width = 1;
136     }
137
138     osg::RefMatrix* matrix = new osg::RefMatrix;
139     matrix->makeOrtho2D(-width, width, -height, height);
140
141     // The trick is to have the projection matrix adapted independent
142     // of the scenegraph but dependent on the viewport of this current
143     // camera we cull for. Therefore we do not put that projection matrix into
144     // an additional camera rather than from within that cull callback.
145     cullVisitor->pushProjectionMatrix(matrix);
146     traverse(node, nv);
147     cullVisitor->popProjectionMatrix();
148   }
149 };
150
151 char *genNameString()
152 {
153     std::string website = "http://www.flightgear.org";
154     std::string programName = "FlightGear";
155     char *name = new char[26];
156     name[20] = 114;
157     name[8] = 119;
158     name[5] = 47;
159     name[12] = 108;
160     name[2] = 116;
161     name[1] = 116;
162     name[16] = 116;
163     name[13] = 105;
164     name[17] = 103;
165     name[19] = 97;
166     name[25] = 0;
167     name[0] = 104;
168     name[24] = 103;
169     name[21] = 46;
170     name[15] = 104;
171     name[3] = 112;
172     name[22] = 111;
173     name[18] = 101;
174     name[7] = 119;
175     name[14] = 103;
176     name[23] = 114;
177     name[4] = 58;
178     name[11] = 102;
179     name[9] = 119;
180     name[10] = 46;
181     name[6] = 47;
182     return name;
183 }
184
185 static osg::Node* fgCreateSplashCamera()
186 {
187   const char* splash_texture = fgGetString("/sim/startup/splash-texture");
188   SGSharedPtr<SGPropertyNode> style = fgGetNode("/sim/gui/style[0]", true);
189
190   char *namestring = genNameString();
191   fgSetString("/sim/startup/program-name", namestring);
192   delete[] namestring;
193
194   SGPath tpath;
195   if (splash_texture  && strcmp(splash_texture, "")) {
196       tpath = globals->resolve_maybe_aircraft_path(splash_texture);
197       if (tpath.isNull())
198       {
199           SG_LOG( SG_VIEW, SG_ALERT, "Cannot find splash screen file '" << splash_texture
200                   << "'. Using default." );
201       }
202   }
203
204   if (tpath.isNull()) {
205     // no splash screen specified - select random image
206     tpath = globals->get_fg_root();
207     // load in the texture data
208     int num = (int)(sg_random() * 5.0 + 1.0);
209     char num_str[5];
210     snprintf(num_str, 4, "%d", num);
211
212     tpath.append( "Textures/Splash" );
213     tpath.concat( num_str );
214     tpath.concat( ".png" );
215   }
216
217   osg::Texture2D* splashTexture = new osg::Texture2D;
218   splashTexture->setImage(osgDB::readImageFile(tpath.c_str()));
219
220   osg::Camera* camera = new osg::Camera;
221   camera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
222   camera->setProjectionMatrix(osg::Matrix::ortho2D(-1, 1, -1, 1));
223   camera->setViewMatrix(osg::Matrix::identity());
224   camera->setRenderOrder(osg::Camera::POST_RENDER, 10000);
225   camera->setClearMask(0);
226   camera->setAllowEventFocus(false);
227   camera->setCullingActive(false);
228
229   osg::StateSet* stateSet = camera->getOrCreateStateSet();
230   stateSet->setMode(GL_ALPHA_TEST, osg::StateAttribute::OFF);
231   stateSet->setMode(GL_BLEND, osg::StateAttribute::ON);
232   stateSet->setAttribute(new osg::BlendFunc);
233   stateSet->setMode(GL_CULL_FACE, osg::StateAttribute::OFF);
234   stateSet->setMode(GL_DEPTH_TEST, osg::StateAttribute::OFF);
235   stateSet->setAttribute(new osg::Depth(osg::Depth::ALWAYS, 0, 1, false));
236   stateSet->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
237
238
239   osg::Geometry* geometry = new osg::Geometry;
240   geometry->setSupportsDisplayList(false);
241
242   osg::Vec3Array* vertexArray = new osg::Vec3Array;
243   vertexArray->push_back(osg::Vec3(-1, -1, 0));
244   vertexArray->push_back(osg::Vec3( 1, -1, 0));
245   vertexArray->push_back(osg::Vec3( 1,  1, 0));
246   vertexArray->push_back(osg::Vec3(-1,  1, 0));
247   geometry->setVertexArray(vertexArray);
248   osg::Vec4Array* colorArray = new osg::Vec4Array;
249   colorArray->push_back(osg::Vec4(0, 0, 0, 1));
250   geometry->setColorArray(colorArray);
251   geometry->setColorBinding(osg::Geometry::BIND_OVERALL);
252   geometry->addPrimitiveSet(new osg::DrawArrays(GL_POLYGON, 0, 4));
253   geometry->setUpdateCallback(new FGSplashUpdateCallback(colorArray,
254                               style->getNode("colors/splash-screen")));
255
256   osg::Geode* geode = new osg::Geode;
257   geode->addDrawable(geometry);
258
259   stateSet = geode->getOrCreateStateSet();
260   stateSet->setRenderBinDetails(1, "RenderBin");
261   camera->addChild(geode);
262
263
264   // The group is needed because of osg is handling the cull callbacks in a
265   // different way for groups than for a geode. It does not hurt here ...
266   osg::Group* group = new osg::Group;
267   group->setCullCallback(new FGSplashContentProjectionCalback);
268   camera->addChild(group);
269
270   geode = new osg::Geode;
271   stateSet = geode->getOrCreateStateSet();
272   stateSet->setRenderBinDetails(2, "RenderBin");
273   group->addChild(geode);
274
275
276   geometry = new osg::Geometry;
277   geometry->setSupportsDisplayList(false);
278
279   vertexArray = new osg::Vec3Array;
280   vertexArray->push_back(osg::Vec3(-0.84, -0.84, 0));
281   vertexArray->push_back(osg::Vec3( 0.84, -0.84, 0));
282   vertexArray->push_back(osg::Vec3( 0.84,  0.84, 0));
283   vertexArray->push_back(osg::Vec3(-0.84,  0.84, 0));
284   geometry->setVertexArray(vertexArray);
285   osg::Vec2Array* texCoordArray = new osg::Vec2Array;
286   texCoordArray->push_back(osg::Vec2(0, 0));
287   texCoordArray->push_back(osg::Vec2(1, 0));
288   texCoordArray->push_back(osg::Vec2(1, 1));
289   texCoordArray->push_back(osg::Vec2(0, 1));
290   geometry->setTexCoordArray(0, texCoordArray);
291   colorArray = new osg::Vec4Array;
292   colorArray->push_back(osg::Vec4(1, 1, 1, 1));
293   geometry->setColorArray(colorArray);
294   geometry->setColorBinding(osg::Geometry::BIND_OVERALL);
295   geometry->addPrimitiveSet(new osg::DrawArrays(GL_POLYGON, 0, 4));
296   geometry->setUpdateCallback(new FGSplashUpdateCallback(colorArray, 0));
297   stateSet = geometry->getOrCreateStateSet();
298   stateSet->setTextureMode(0, GL_TEXTURE_2D, osg::StateAttribute::ON);
299   stateSet->setTextureAttribute(0, splashTexture);
300   geode->addDrawable(geometry);
301
302
303   osgText::Text* text = new osgText::Text;
304   std::string fn = style->getStringValue("fonts/splash", "");
305   text->setFont(globals->get_fontcache()->getfntpath(fn.c_str()).str());
306   text->setCharacterSize(0.06);
307   text->setColor(osg::Vec4(1, 1, 1, 1));
308   text->setPosition(osg::Vec3(0, -0.92, 0));
309   text->setAlignment(osgText::Text::CENTER_CENTER);
310   SGPropertyNode* prop = fgGetNode("/sim/startup/splash-progress-text", true);
311   prop->setStringValue("");
312   text->setUpdateCallback(new FGSplashTextUpdateCallback(prop));
313   geode->addDrawable(text);
314
315   osgText::Text* spinnertext = new osgText::Text;
316   spinnertext->setFont(globals->get_fontcache()->getfntpath(fn.c_str()).str());
317   spinnertext->setCharacterSize(0.06);
318   spinnertext->setColor(osg::Vec4(1, 1, 1, 1));
319   spinnertext->setPosition(osg::Vec3(0, -0.97, 0));
320   spinnertext->setAlignment(osgText::Text::CENTER_CENTER);
321   prop = fgGetNode("/sim/startup/splash-progress-spinner", true);
322   prop->setStringValue("");
323   spinnertext->setUpdateCallback(new FGSplashTextUpdateCallback(prop));
324   geode->addDrawable(spinnertext);
325
326   text = new osgText::Text;
327   text->setFont(globals->get_fontcache()->getfntpath(fn.c_str()).str());
328   text->setCharacterSize(0.08);
329   text->setColor(osg::Vec4(1, 1, 1, 1));
330   text->setPosition(osg::Vec3(0, 0.92, 0));
331   text->setAlignment(osgText::Text::CENTER_CENTER);
332   prop = fgGetNode("/sim/startup/program-name", "FlightGear");
333   text->setUpdateCallback(new FGSplashTextUpdateCallback(prop));
334   geode->addDrawable(text);
335
336
337   text = new osgText::Text;
338   text->setFont(globals->get_fontcache()->getfntpath(fn.c_str()).str());
339   text->setCharacterSize(0.06);
340   text->setColor(osg::Vec4(1, 1, 1, 1));
341   text->setPosition(osg::Vec3(0, 0.82, 0));
342   text->setAlignment(osgText::Text::CENTER_CENTER);
343   prop = fgGetNode("/sim/startup/splash-title", true);
344   text->setUpdateCallback(new FGSplashTextUpdateCallback(prop));
345   geode->addDrawable(text);
346
347   fgSplashProgress("init");
348
349   return camera;
350 }
351
352 // update callback for the switch node guarding that splash
353 class FGSplashGroupUpdateCallback : public osg::NodeCallback {
354 public:
355   FGSplashGroupUpdateCallback() :
356     _splashAlphaNode(fgGetNode("/sim/startup/splash-alpha", true))
357   { }
358   virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)
359   {
360     assert(dynamic_cast<osg::Group*>(node));
361     osg::Group* group = static_cast<osg::Group*>(node);
362
363     double alpha = _splashAlphaNode->getDoubleValue();
364     if (alpha <= 0 || !fgGetBool("/sim/startup/splash-screen"))
365       group->removeChild(0, group->getNumChildren());
366     else if (group->getNumChildren() == 0)
367       group->addChild(fgCreateSplashCamera());
368
369     traverse(node, nv);
370   }
371 private:
372   SGSharedPtr<const SGPropertyNode> _splashAlphaNode;
373 };
374
375 osg::Node* fgCreateSplashNode() {
376   osg::Group* group = new osg::Group;
377   group->setUpdateCallback(new FGSplashGroupUpdateCallback);
378   return group;
379 }
380
381 // Initialize the splash screen
382 void fgSplashInit () {
383   SG_LOG( SG_VIEW, SG_INFO, "Initializing splash screen" );
384   globals->get_renderer()->splashinit();
385 }
386
387 void fgSplashProgress( const char *identifier ) {
388   const char* spinChars = "-\\|/";
389   static int spin_count = 0;
390   string spin_status = string("");
391
392   if (identifier[0] != 0)
393       spin_status += spinChars[spin_count++ % 4];
394
395   fgSetString("/sim/startup/splash-progress-spinner", spin_status);
396
397   const char* text = "";
398   if (identifier[0] != 0)
399   {
400       string id = string("splash/") + identifier;
401       text = globals->get_locale()->getLocalizedString(id.c_str(),
402                                                        "sys", "<incomplete language resource>");
403   }
404
405   if (!strcmp(fgGetString("/sim/startup/splash-progress-text"), text)) {
406     return;
407   }
408   
409   SG_LOG( SG_VIEW, SG_INFO, "Splash screen progress " << identifier );
410   fgSetString("/sim/startup/splash-progress-text", text);
411 }