]> git.mxchange.org Git - flightgear.git/blob - src/Canvas/window.cxx
Merge branch 'timoore/optimization' into next
[flightgear.git] / src / Canvas / window.cxx
1 // Window for placing a Canvas onto it (for dialogs, menus, etc.)
2 //
3 // Copyright (C) 2012  Thomas Geymayer <tomgey@gmail.com>
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License as
7 // published by the Free Software Foundation; either version 2 of the
8 // License, or (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful, but
11 // WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 // General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18
19 #include "window.hxx"
20 #include <Canvas/canvas.hxx>
21
22 #include <osg/BlendFunc>
23 #include <osg/Geometry>
24 #include <osg/Texture2D>
25 #include <osgGA/GUIEventHandler>
26
27 /**
28  * Callback to enable/disable rendering of canvas displayed inside windows
29  */
30 class CullCallback:
31   public osg::Drawable::CullCallback
32 {
33   public:
34     CullCallback(Canvas::CameraCullCallback* camera_cull);
35
36   private:
37     Canvas::CameraCullCallback *_camera_cull;
38
39     virtual bool cull( osg::NodeVisitor* nv,
40                        osg::Drawable* drawable,
41                        osg::RenderInfo* renderInfo ) const;
42 };
43
44 //------------------------------------------------------------------------------
45 CullCallback::CullCallback(Canvas::CameraCullCallback* camera_cull):
46   _camera_cull( camera_cull )
47 {
48
49 }
50
51 //------------------------------------------------------------------------------
52 bool CullCallback::cull( osg::NodeVisitor* nv,
53                          osg::Drawable* drawable,
54                          osg::RenderInfo* renderInfo ) const
55 {
56   _camera_cull->enableRendering();
57   return false;
58 }
59
60 namespace canvas
61 {
62   //----------------------------------------------------------------------------
63   Window::Window(SGPropertyNode* node):
64     PropertyBasedElement(node),
65     _dirty(true),
66     _geometry( new osg::Geometry ),
67     _vertices( new osg::Vec3Array(4) ),
68     _tex_coords( new osg::Vec2Array(4) ),
69     _x(node, "x"),
70     _y(node, "y"),
71     _width(node, "size[0]"),
72     _height(node, "size[1]")
73   {
74     _x = 50;
75     _y = 100;
76     _width = 400;
77     _height = 300;
78
79     _geometry->setVertexArray(_vertices);
80     _geometry->setTexCoordArray(0,_tex_coords);
81
82     osg::Vec4Array* colors = new osg::Vec4Array(1);
83     (*colors)[0].set(1.0f,1.0f,1.0,1.0f);
84     _geometry->setColorArray(colors);
85     _geometry->setColorBinding(osg::Geometry::BIND_OVERALL);
86
87     _geometry->addPrimitiveSet(
88       new osg::DrawArrays(osg::PrimitiveSet::QUADS,0,4)
89     );
90     _geometry->setDataVariance(osg::Object::DYNAMIC);
91
92     osg::StateSet* stateSet = _geometry->getOrCreateStateSet();
93     stateSet->setRenderBinDetails(1000, "RenderBin");
94
95     // speed optimization?
96     stateSet->setMode(GL_CULL_FACE, osg::StateAttribute::OFF);
97     stateSet->setAttribute(new osg::BlendFunc(
98       osg::BlendFunc::SRC_ALPHA,
99       osg::BlendFunc::ONE_MINUS_SRC_ALPHA)
100     );
101     stateSet->setMode(GL_BLEND, osg::StateAttribute::ON);
102     stateSet->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
103     stateSet->setMode(GL_FOG, osg::StateAttribute::OFF);
104     stateSet->setMode(GL_DEPTH_TEST, osg::StateAttribute::OFF);
105     stateSet->setTextureMode(0, GL_TEXTURE_2D, osg::StateAttribute::ON);
106   }
107
108   //----------------------------------------------------------------------------
109   Window::~Window()
110   {
111
112   }
113
114   //----------------------------------------------------------------------------
115   void Window::update(double delta_time_sec)
116   {
117     if( !_dirty )
118       return;
119     _dirty = false;
120
121     _region.set(_x, _y, _width, _height);
122
123     int z = 0; // TODO do we need to use z for depth ordering?
124
125     (*_vertices)[0].set(_region.l(), _region.t(), z);
126     (*_vertices)[1].set(_region.r(), _region.t(), z);
127     (*_vertices)[2].set(_region.r(), _region.b(), z);
128     (*_vertices)[3].set(_region.l(), _region.b(), z);
129
130     float l = 0, t = 1, b = 0, r = 1;
131     (*_tex_coords)[0].set(l,t);
132     (*_tex_coords)[1].set(r,t);
133     (*_tex_coords)[2].set(r,b);
134     (*_tex_coords)[3].set(l,b);
135
136     _geometry->dirtyDisplayList();
137   }
138
139   //----------------------------------------------------------------------------
140   void Window::valueChanged (SGPropertyNode * node)
141   {
142     if( node->getParent() != _node )
143       return;
144
145     const std::string& name = node->getNameString();
146     if(    name == "x" || name == "y" || name == "size" )
147       _dirty = true;
148   }
149
150   //----------------------------------------------------------------------------
151   void Window::setCanvas(CanvasPtr canvas)
152   {
153     _canvas = canvas;
154     _geometry->getOrCreateStateSet()
155              ->setTextureAttribute(0, canvas ? canvas->getTexture() : 0);
156     _geometry->dirtyDisplayList();
157     _geometry->setCullCallback(
158       canvas ? new CullCallback(canvas->getCameraCullCallback()) : 0
159     );
160   }
161
162   //----------------------------------------------------------------------------
163   CanvasWeakPtr Window::getCanvas() const
164   {
165     return _canvas;
166   }
167
168   //----------------------------------------------------------------------------
169   bool Window::handleMouseEvent(const MouseEvent& event)
170   {
171     if( !_canvas.expired() )
172       return _canvas.lock()->handleMouseEvent(event);
173     else
174       return false;
175   }
176
177 } // namespace canvas