]> git.mxchange.org Git - simgear.git/blob - simgear/canvas/ODGauge.cxx
ODGauge/Canvas: add option "additive-blend" to use additive alpha blending
[simgear.git] / simgear / canvas / ODGauge.cxx
1 // Owner Drawn Gauge helper class
2 //
3 // Written by Harald JOHNSEN, started May 2005.
4 //
5 // Copyright (C) 2005  Harald JOHNSEN
6 //
7 // Ported to OSG by Tim Moore - Jun 2007
8 //
9 // Heavily modified to be usable for the 2d Canvas by Thomas Geymayer - April 2012
10 // Supports now multisampling/mipmapping, usage of the stencil buffer and placing
11 // the texture in the scene by certain filter criteria
12 //
13 // This library is free software; you can redistribute it and/or
14 // modify it under the terms of the GNU Library General Public
15 // License as published by the Free Software Foundation; either
16 // version 2 of the License, or (at your option) any later version.
17 //
18 // This library is distributed in the hope that it will be useful,
19 // but WITHOUT ANY WARRANTY; without even the implied warranty of
20 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21 // Library General Public License for more details.
22 //
23 // You should have received a copy of the GNU Library General Public
24 // License along with this library; if not, write to the Free Software
25 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301, USA
26
27 #ifdef HAVE_CONFIG_H
28 #  include <simgear_config.h>
29 #endif
30
31 #include "ODGauge.hxx"
32 #include "CanvasSystemAdapter.hxx"
33
34 #include <simgear/debug/logstream.hxx>
35 #include <simgear/scene/util/RenderConstants.hxx>
36
37 #include <osg/Texture2D>
38 #include <osg/AlphaFunc>
39 #include <osg/BlendFunc>
40 #include <osg/Camera>
41 #include <osg/Matrix>
42 #include <osg/PolygonMode>
43 #include <osg/ShadeModel>
44 #include <osg/StateSet>
45 #include <osg/FrameBufferObject> // for GL_DEPTH_STENCIL_EXT on Windows
46
47 #include <cassert>
48
49 namespace simgear
50 {
51 namespace canvas
52 {
53
54   //----------------------------------------------------------------------------
55   ODGauge::ODGauge():
56     _size_x( -1 ),
57     _size_y( -1 ),
58     _view_width( -1 ),
59     _view_height( -1 ),
60     _flags(0),
61     _coverage_samples( 0 ),
62     _color_samples( 0 )
63   {
64
65   }
66
67   //----------------------------------------------------------------------------
68   ODGauge::~ODGauge()
69   {
70     clear();
71   }
72
73   //----------------------------------------------------------------------------
74   void ODGauge::setSystemAdapter(const SystemAdapterPtr& system_adapter)
75   {
76     _system_adapter = system_adapter;
77   }
78
79   //----------------------------------------------------------------------------
80   void ODGauge::setSize(int size_x, int size_y)
81   {
82     _size_x = size_x;
83     _size_y = size_y < 0 ? size_x : size_y;
84
85     if( serviceable() )
86       reinit();
87     else if( texture )
88       texture->setTextureSize(_size_x, _size_y);
89   }
90
91   //----------------------------------------------------------------------------
92   void ODGauge::setViewSize(int width, int height)
93   {
94     _view_width = width;
95     _view_height = height < 0 ? width : height;
96
97     if( camera )
98       updateCoordinateFrame();
99   }
100
101   //----------------------------------------------------------------------------
102   void ODGauge::useImageCoords(bool use)
103   {
104     if( updateFlag(USE_IMAGE_COORDS, use) && texture )
105       updateCoordinateFrame();
106   }
107
108   //----------------------------------------------------------------------------
109   void ODGauge::useStencil(bool use)
110   {
111     if( updateFlag(USE_STENCIL, use) && texture )
112       updateStencil();
113   }
114
115   //----------------------------------------------------------------------------
116   void ODGauge::useAdditiveBlend(bool use)
117   {
118     if( updateFlag(USE_ADDITIVE_BLEND, use) && camera )
119       updateBlendMode();
120   }
121
122   //----------------------------------------------------------------------------
123   void ODGauge::setSampling( bool mipmapping,
124                              int coverage_samples,
125                              int color_samples )
126   {
127     if(    !updateFlag(USE_MIPMAPPING, mipmapping)
128         && _coverage_samples == coverage_samples
129         && _color_samples == color_samples )
130       return;
131
132     if( color_samples > coverage_samples )
133     {
134       SG_LOG
135       (
136         SG_GL,
137         SG_WARN,
138         "ODGauge::setSampling: color_samples > coverage_samples not allowed!"
139       );
140       color_samples = coverage_samples;
141     }
142
143     _coverage_samples = coverage_samples;
144     _color_samples = color_samples;
145
146     updateSampling();
147   }
148
149   //----------------------------------------------------------------------------
150   void ODGauge::setRender(bool render)
151   {
152     // Only the far camera should trigger this texture to be rendered.
153     camera->setNodeMask(render ? simgear::BACKGROUND_BIT : 0);
154   }
155
156   //----------------------------------------------------------------------------
157   bool ODGauge::serviceable(void)
158   {
159     return _flags & AVAILABLE;
160   }
161
162   //----------------------------------------------------------------------------
163   void ODGauge::allocRT(osg::NodeCallback* camera_cull_callback)
164   {
165     camera = new osg::Camera;
166     camera->setDataVariance(osg::Object::DYNAMIC);
167     camera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
168     camera->setRenderOrder(osg::Camera::PRE_RENDER);
169     camera->setClearColor(osg::Vec4(0.0f, 0.0f, 0.0f , 0.0f));
170     camera->setClearStencil(0);
171     camera->setRenderTargetImplementation( osg::Camera::FRAME_BUFFER_OBJECT,
172                                                osg::Camera::FRAME_BUFFER );
173
174     if( camera_cull_callback )
175       camera->setCullCallback(camera_cull_callback);
176
177     setRender(true);
178     updateCoordinateFrame();
179     updateStencil();
180
181     osg::StateSet* stateSet = camera->getOrCreateStateSet();
182     stateSet->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
183     stateSet->setMode(GL_CULL_FACE, osg::StateAttribute::OFF);
184     stateSet->setMode(GL_FOG, osg::StateAttribute::OFF);
185     stateSet->setMode(GL_DEPTH_TEST, osg::StateAttribute::OFF);
186     stateSet->setAttributeAndModes(
187       new osg::PolygonMode( osg::PolygonMode::FRONT_AND_BACK,
188                             osg::PolygonMode::FILL ),
189       osg::StateAttribute::ON );
190     stateSet->setAttributeAndModes(
191       new osg::AlphaFunc(osg::AlphaFunc::GREATER, 0.001f),
192       osg::StateAttribute::ON );
193     stateSet->setAttribute(new osg::ShadeModel(osg::ShadeModel::FLAT));
194
195     if( !texture )
196     {
197       texture = new osg::Texture2D;
198       texture->setTextureSize(_size_x, _size_y);
199       texture->setInternalFormat(GL_RGBA);
200     }
201
202     updateSampling();
203     updateBlendMode();
204
205     if( _system_adapter )
206       _system_adapter->addCamera(camera.get());
207
208     _flags |= AVAILABLE;
209   }
210
211   //----------------------------------------------------------------------------
212   void ODGauge::reinit()
213   {
214     osg::NodeCallback* cull_callback = camera ? camera->getCullCallback() : 0;
215     clear();
216     allocRT(cull_callback);
217   }
218
219   //----------------------------------------------------------------------------
220   void ODGauge::clear()
221   {
222     if( camera.valid() && _system_adapter )
223       _system_adapter->removeCamera(camera.get());
224     camera.release();
225     texture.release();
226
227     _flags &= ~AVAILABLE;
228   }
229
230   //----------------------------------------------------------------------------
231   bool ODGauge::updateFlag(Flags flag, bool enable)
232   {
233     if( (_flags & flag) == enable )
234       return false;
235
236     _flags ^= flag;
237     return true;
238   }
239
240   //----------------------------------------------------------------------------
241   void ODGauge::updateCoordinateFrame()
242   {
243     assert( camera );
244
245     if( _view_width < 0 )
246       _view_width = _size_x;
247     if( _view_height < 0 )
248       _view_height = _size_y;
249
250     camera->setViewport(0, 0, _size_x, _size_y);
251
252     if( _flags & USE_IMAGE_COORDS )
253       camera->setProjectionMatrix(
254         osg::Matrix::ortho2D(0, _view_width, _view_height, 0)
255       );
256     else
257       camera->setProjectionMatrix(
258         osg::Matrix::ortho2D( -_view_width/2.,  _view_width/2.,
259                               -_view_height/2., _view_height/2. )
260       );
261   }
262
263   //----------------------------------------------------------------------------
264   void ODGauge::updateStencil()
265   {
266     assert( camera );
267
268     GLbitfield mask = GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT;
269
270     if( _flags & USE_STENCIL )
271     {
272       camera->attach( osg::Camera::PACKED_DEPTH_STENCIL_BUFFER,
273                        GL_DEPTH_STENCIL_EXT );
274       mask |= GL_STENCIL_BUFFER_BIT;
275     }
276     else
277     {
278       camera->detach(osg::Camera::PACKED_DEPTH_STENCIL_BUFFER);
279     }
280
281     camera->setClearMask(mask);
282   }
283
284   //----------------------------------------------------------------------------
285   void ODGauge::updateSampling()
286   {
287     assert( camera );
288     assert( texture );
289
290     texture->setFilter(
291       osg::Texture2D::MIN_FILTER,
292       (_flags & USE_MIPMAPPING) ? osg::Texture2D::LINEAR_MIPMAP_LINEAR
293                                 : osg::Texture2D::LINEAR
294     );
295     camera->attach(
296       osg::Camera::COLOR_BUFFER,
297       texture.get(),
298       0, 0,
299       _flags & USE_MIPMAPPING,
300       _coverage_samples,
301       _color_samples
302     );
303   }
304
305   //----------------------------------------------------------------------------
306   void ODGauge::updateBlendMode()
307   {
308     assert( camera );
309
310     camera->getOrCreateStateSet()
311       ->setAttributeAndModes
312       (
313         (_flags & USE_ADDITIVE_BLEND)
314           ? new osg::BlendFunc( osg::BlendFunc::SRC_ALPHA,
315                                 osg::BlendFunc::ONE_MINUS_SRC_ALPHA,
316                                 osg::BlendFunc::ONE,
317                                 osg::BlendFunc::ONE )
318           : new osg::BlendFunc( osg::BlendFunc::SRC_ALPHA,
319                                 osg::BlendFunc::ONE_MINUS_SRC_ALPHA )
320       );
321   }
322
323 } // namespace canvas
324 } // namespace simgear