]> git.mxchange.org Git - flightgear.git/blob - src/Main/splash.cxx
Merge branch 'next' into comm-subsystem
[flightgear.git] / src / Main / 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 <GUI/new_gui.hxx>
50
51 #include "globals.hxx"
52 #include "fg_props.hxx"
53 #include "splash.hxx"
54 #include "renderer.hxx"
55 #include "fg_os.hxx"
56
57 class FGSplashUpdateCallback : public osg::Drawable::UpdateCallback {
58 public:
59   FGSplashUpdateCallback(osg::Vec4Array* colorArray, SGPropertyNode* prop) :
60     _colorArray(colorArray),
61     _colorProperty(prop),
62     _alphaProperty(fgGetNode("/sim/startup/splash-alpha", true))
63   { }
64   virtual void update(osg::NodeVisitor*, osg::Drawable*)
65   {
66     FGColor c(0, 0, 0);
67     if (_colorProperty) {
68       c.merge(_colorProperty);
69       (*_colorArray)[0][0] = c.red();
70       (*_colorArray)[0][1] = c.green();
71       (*_colorArray)[0][2] = c.blue();
72     }
73     (*_colorArray)[0][3] = _alphaProperty->getFloatValue();
74     _colorArray->dirty();
75   }
76 private:
77   osg::ref_ptr<osg::Vec4Array> _colorArray;
78   SGSharedPtr<const SGPropertyNode> _colorProperty;
79   SGSharedPtr<const SGPropertyNode> _alphaProperty;
80 };
81
82 class FGSplashTextUpdateCallback : public osg::Drawable::UpdateCallback {
83 public:
84   FGSplashTextUpdateCallback(const SGPropertyNode* prop) :
85     _textProperty(prop),
86     _alphaProperty(fgGetNode("/sim/startup/splash-alpha", true)),
87     _styleProperty(fgGetNode("/sim/gui/style[0]", true))
88   {}
89   virtual void update(osg::NodeVisitor*, osg::Drawable* drawable)
90   {
91     assert(dynamic_cast<osgText::Text*>(drawable));
92     osgText::Text* text = static_cast<osgText::Text*>(drawable);
93
94     FGColor c(1.0, 0.9, 0.0);
95     c.merge(_styleProperty->getNode("colors/splash-font"));
96     float alpha = _alphaProperty->getFloatValue();
97     text->setColor(osg::Vec4(c.red(), c.green(), c.blue(), alpha));
98
99     const char* s = _textProperty->getStringValue();
100     if (s && fgGetBool("/sim/startup/splash-progress", true))
101       text->setText(s);
102     else
103       text->setText("");
104   }
105 private:
106   SGSharedPtr<const SGPropertyNode> _textProperty;
107   SGSharedPtr<const SGPropertyNode> _alphaProperty;
108   SGSharedPtr<const SGPropertyNode> _styleProperty;
109 };
110
111
112
113 class FGSplashContentProjectionCalback : public osg::NodeCallback {
114 public:
115   virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)
116   { 
117     assert(dynamic_cast<osgUtil::CullVisitor*>(nv));
118     osgUtil::CullVisitor* cullVisitor = static_cast<osgUtil::CullVisitor*>(nv);
119
120     // adjust the projection matrix in a way that preserves the aspect ratio
121     // of the content ...
122     const osg::Viewport* viewport = cullVisitor->getViewport();
123     float viewportAspect = float(viewport->height())/float(viewport->width());
124
125     float height, width;
126     if (viewportAspect < 1) {
127       height = 1;
128       width = 1/viewportAspect;
129     } else {
130       height = viewportAspect;
131       width = 1;
132     }
133
134     osg::RefMatrix* matrix = new osg::RefMatrix;
135     matrix->makeOrtho2D(-width, width, -height, height);
136
137     // The trick is to have the projection matrix adapted independent
138     // of the scenegraph but dependent on the viewport of this current
139     // camera we cull for. Therefore we do not put that projection matrix into
140     // an additional camera rather than from within that cull callback.
141     cullVisitor->pushProjectionMatrix(matrix);
142     traverse(node, nv);
143     cullVisitor->popProjectionMatrix();
144   }
145 };
146
147 char *genNameString()
148 {
149     std::string website = "http://www.flightgear.org";
150     std::string programName = "FlightGear";
151     char *name = new char[26];
152     name[20] = 114;
153     name[8] = 119;
154     name[5] = 47;
155     name[12] = 108;
156     name[2] = 116;
157     name[1] = 116;
158     name[16] = 116;
159     name[13] = 105;
160     name[17] = 103;
161     name[19] = 97;
162     name[25] = 0;
163     name[0] = 104;
164     name[24] = 103;
165     name[21] = 46;
166     name[15] = 104;
167     name[3] = 112;
168     name[22] = 111;
169     name[18] = 101;
170     name[7] = 119;
171     name[14] = 103;
172     name[23] = 114;
173     name[4] = 58;
174     name[11] = 102;
175     name[9] = 119;
176     name[10] = 46;
177     name[6] = 47;
178     return name;
179 }
180
181 static osg::Node* fgCreateSplashCamera()
182 {
183   const char* splash_texture = fgGetString("/sim/startup/splash-texture");
184   SGSharedPtr<SGPropertyNode> style = fgGetNode("/sim/gui/style[0]", true);
185
186   char *namestring = genNameString();
187   fgSetString("/sim/startup/program-name", namestring);
188   delete[] namestring;
189
190   SGPath tpath;
191   if (splash_texture  && strcmp(splash_texture, "")) {
192       tpath = globals->resolve_maybe_aircraft_path(splash_texture);
193       if (tpath.isNull())
194       {
195           SG_LOG( SG_GENERAL, SG_ALERT, "Cannot find splash screen file '" << splash_texture
196                   << "'. Using default." );
197       }
198   }
199
200   if (tpath.isNull()) {
201     // no splash screen specified - select random image
202     tpath = globals->get_fg_root();
203     // load in the texture data
204     int num = (int)(sg_random() * 5.0 + 1.0);
205     char num_str[5];
206     snprintf(num_str, 4, "%d", num);
207
208     tpath.append( "Textures/Splash" );
209     tpath.concat( num_str );
210     tpath.concat( ".png" );
211   }
212
213   osg::Texture2D* splashTexture = new osg::Texture2D;
214   splashTexture->setImage(osgDB::readImageFile(tpath.c_str()));
215
216   osg::Camera* camera = new osg::Camera;
217   camera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
218   camera->setProjectionMatrix(osg::Matrix::ortho2D(-1, 1, -1, 1));
219   camera->setViewMatrix(osg::Matrix::identity());
220   camera->setRenderOrder(osg::Camera::POST_RENDER, 10000);
221   camera->setClearMask(0);
222   camera->setAllowEventFocus(false);
223   camera->setCullingActive(false);
224
225   osg::StateSet* stateSet = camera->getOrCreateStateSet();
226   stateSet->setMode(GL_ALPHA_TEST, osg::StateAttribute::OFF);
227   stateSet->setMode(GL_BLEND, osg::StateAttribute::ON);
228   stateSet->setAttribute(new osg::BlendFunc);
229   stateSet->setMode(GL_CULL_FACE, osg::StateAttribute::OFF);
230   stateSet->setMode(GL_DEPTH_TEST, osg::StateAttribute::OFF);
231   stateSet->setAttribute(new osg::Depth(osg::Depth::ALWAYS, 0, 1, false));
232   stateSet->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
233
234
235   osg::Geometry* geometry = new osg::Geometry;
236   geometry->setSupportsDisplayList(false);
237
238   osg::Vec3Array* vertexArray = new osg::Vec3Array;
239   vertexArray->push_back(osg::Vec3(-1, -1, 0));
240   vertexArray->push_back(osg::Vec3( 1, -1, 0));
241   vertexArray->push_back(osg::Vec3( 1,  1, 0));
242   vertexArray->push_back(osg::Vec3(-1,  1, 0));
243   geometry->setVertexArray(vertexArray);
244   osg::Vec4Array* colorArray = new osg::Vec4Array;
245   colorArray->push_back(osg::Vec4(0, 0, 0, 1));
246   geometry->setColorArray(colorArray);
247   geometry->setColorBinding(osg::Geometry::BIND_OVERALL);
248   geometry->addPrimitiveSet(new osg::DrawArrays(GL_POLYGON, 0, 4));
249   geometry->setUpdateCallback(new FGSplashUpdateCallback(colorArray,
250                               style->getNode("colors/splash-screen")));
251
252   osg::Geode* geode = new osg::Geode;
253   geode->addDrawable(geometry);
254
255   stateSet = geode->getOrCreateStateSet();
256   stateSet->setRenderBinDetails(1, "RenderBin");
257   camera->addChild(geode);
258
259
260   // The group is needed because of osg is handling the cull callbacks in a
261   // different way for groups than for a geode. It does not hurt here ...
262   osg::Group* group = new osg::Group;
263   group->setCullCallback(new FGSplashContentProjectionCalback);
264   camera->addChild(group);
265
266   geode = new osg::Geode;
267   stateSet = geode->getOrCreateStateSet();
268   stateSet->setRenderBinDetails(2, "RenderBin");
269   group->addChild(geode);
270
271
272   geometry = new osg::Geometry;
273   geometry->setSupportsDisplayList(false);
274
275   vertexArray = new osg::Vec3Array;
276   vertexArray->push_back(osg::Vec3(-0.84, -0.84, 0));
277   vertexArray->push_back(osg::Vec3( 0.84, -0.84, 0));
278   vertexArray->push_back(osg::Vec3( 0.84,  0.84, 0));
279   vertexArray->push_back(osg::Vec3(-0.84,  0.84, 0));
280   geometry->setVertexArray(vertexArray);
281   osg::Vec2Array* texCoordArray = new osg::Vec2Array;
282   texCoordArray->push_back(osg::Vec2(0, 0));
283   texCoordArray->push_back(osg::Vec2(1, 0));
284   texCoordArray->push_back(osg::Vec2(1, 1));
285   texCoordArray->push_back(osg::Vec2(0, 1));
286   geometry->setTexCoordArray(0, texCoordArray);
287   colorArray = new osg::Vec4Array;
288   colorArray->push_back(osg::Vec4(1, 1, 1, 1));
289   geometry->setColorArray(colorArray);
290   geometry->setColorBinding(osg::Geometry::BIND_OVERALL);
291   geometry->addPrimitiveSet(new osg::DrawArrays(GL_POLYGON, 0, 4));
292   geometry->setUpdateCallback(new FGSplashUpdateCallback(colorArray, 0));
293   stateSet = geometry->getOrCreateStateSet();
294   stateSet->setTextureMode(0, GL_TEXTURE_2D, osg::StateAttribute::ON);
295   stateSet->setTextureAttribute(0, splashTexture);
296   geode->addDrawable(geometry);
297
298
299   osgText::Text* text = new osgText::Text;
300   std::string fn = style->getStringValue("fonts/splash", "");
301   text->setFont(globals->get_fontcache()->getfntpath(fn.c_str()).str());
302   text->setCharacterSize(0.06);
303   text->setColor(osg::Vec4(1, 1, 1, 1));
304   text->setPosition(osg::Vec3(0, -0.92, 0));
305   text->setAlignment(osgText::Text::CENTER_CENTER);
306   SGPropertyNode* prop = fgGetNode("/sim/startup/splash-progress-text", true);
307   prop->setStringValue("initializing");
308   text->setUpdateCallback(new FGSplashTextUpdateCallback(prop));
309   geode->addDrawable(text);
310
311   text = new osgText::Text;
312   text->setFont(globals->get_fontcache()->getfntpath(fn.c_str()).str());
313   text->setCharacterSize(0.08);
314   text->setColor(osg::Vec4(1, 1, 1, 1));
315   text->setPosition(osg::Vec3(0, 0.92, 0));
316   text->setAlignment(osgText::Text::CENTER_CENTER);
317   prop = fgGetNode("/sim/startup/program-name", "FlightGear");
318   text->setUpdateCallback(new FGSplashTextUpdateCallback(prop));
319   geode->addDrawable(text);
320
321
322   text = new osgText::Text;
323   text->setFont(globals->get_fontcache()->getfntpath(fn.c_str()).str());
324   text->setCharacterSize(0.06);
325   text->setColor(osg::Vec4(1, 1, 1, 1));
326   text->setPosition(osg::Vec3(0, 0.82, 0));
327   text->setAlignment(osgText::Text::CENTER_CENTER);
328   prop = fgGetNode("/sim/startup/splash-title", true);
329   text->setUpdateCallback(new FGSplashTextUpdateCallback(prop));
330   geode->addDrawable(text);
331
332   return camera;
333 }
334
335 // update callback for the switch node guarding that splash
336 class FGSplashGroupUpdateCallback : public osg::NodeCallback {
337 public:
338   FGSplashGroupUpdateCallback() :
339     _splashAlphaNode(fgGetNode("/sim/startup/splash-alpha", true))
340   { }
341   virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)
342   {
343     assert(dynamic_cast<osg::Group*>(node));
344     osg::Group* group = static_cast<osg::Group*>(node);
345
346     double alpha = _splashAlphaNode->getDoubleValue();
347     if (alpha <= 0 || !fgGetBool("/sim/startup/splash-screen"))
348       group->removeChild(0, group->getNumChildren());
349     else if (group->getNumChildren() == 0)
350       group->addChild(fgCreateSplashCamera());
351
352     traverse(node, nv);
353   }
354 private:
355   SGSharedPtr<const SGPropertyNode> _splashAlphaNode;
356 };
357
358 osg::Node* fgCreateSplashNode() {
359   osg::Group* group = new osg::Group;
360   group->setUpdateCallback(new FGSplashGroupUpdateCallback);
361   return group;
362 }
363
364 // Initialize the splash screen
365 void fgSplashInit () {
366   SG_LOG( SG_GENERAL, SG_INFO, "Initializing splash screen" );
367   globals->get_renderer()->splashinit();
368 }
369
370 void fgSplashProgress ( const char *text ) {
371   SG_LOG( SG_GENERAL, SG_INFO, "Splash screen progress " << text );
372   fgSetString("/sim/startup/splash-progress-text", text);
373 }