1 // Owner Drawn Gauge helper class
3 // Written by Harald JOHNSEN, started May 2005.
5 // Copyright (C) 2005 Harald JOHNSEN
7 // Ported to OSG by Tim Moore - Jun 2007
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
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.
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.
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
28 # include <simgear_config.h>
31 #include "ODGauge.hxx"
33 #include "CanvasSystemAdapter.hxx"
35 #include <simgear/debug/logstream.hxx>
36 #include <simgear/scene/util/RenderConstants.hxx>
38 #include <osg/Texture2D>
39 #include <osg/AlphaFunc>
40 #include <osg/BlendFunc>
43 #include <osg/PolygonMode>
44 #include <osg/ShadeModel>
45 #include <osg/StateSet>
46 #include <osg/FrameBufferObject> // for GL_DEPTH_STENCIL_EXT on Windows
47 #include <osgUtil/RenderBin>
57 public osgUtil::RenderBin
63 PreOrderBin( const RenderBin& rhs,
64 const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY ):
65 RenderBin(rhs, copyop)
68 virtual osg::Object* cloneType() const
70 return new PreOrderBin();
72 virtual osg::Object* clone(const osg::CopyOp& copyop) const
74 return new PreOrderBin(*this,copyop);
76 virtual bool isSameKindAs(const osg::Object* obj) const
78 return dynamic_cast<const PreOrderBin*>(obj) != 0L;
80 virtual const char* className() const
87 // Do not sort to keep traversal order...
91 #ifndef OSG_INIT_SINGLETON_PROXY
93 * http://svn.openscenegraph.org/osg/OpenSceneGraph/trunk/include/osg/Object
95 * Helper macro that creates a static proxy object to call singleton function
96 * on it's construction, ensuring that the singleton gets initialized at
99 # define OSG_INIT_SINGLETON_PROXY(ProxyName, Func)\
100 static struct ProxyName{ ProxyName() { Func; } } s_##ProxyName;
103 OSG_INIT_SINGLETON_PROXY(
105 (osgUtil::RenderBin::addRenderBinPrototype("PreOrderBin", new PreOrderBin))
108 //----------------------------------------------------------------------------
115 _coverage_samples( 0 ),
121 //----------------------------------------------------------------------------
127 //----------------------------------------------------------------------------
128 void ODGauge::setSize(int size_x, int size_y)
131 _size_y = size_y < 0 ? size_x : size_y;
136 texture->setTextureSize(_size_x, _size_y);
139 //----------------------------------------------------------------------------
140 void ODGauge::setViewSize(int width, int height)
143 _view_height = height < 0 ? width : height;
146 updateCoordinateFrame();
149 //----------------------------------------------------------------------------
150 osg::Vec2s ODGauge::getViewSize() const
152 return osg::Vec2s(_view_width, _view_height);
155 //----------------------------------------------------------------------------
156 void ODGauge::useImageCoords(bool use)
158 if( updateFlag(USE_IMAGE_COORDS, use) && texture )
159 updateCoordinateFrame();
162 //----------------------------------------------------------------------------
163 void ODGauge::useStencil(bool use)
165 if( updateFlag(USE_STENCIL, use) && texture )
169 //----------------------------------------------------------------------------
170 void ODGauge::useAdditiveBlend(bool use)
172 if( updateFlag(USE_ADDITIVE_BLEND, use) && camera )
176 //----------------------------------------------------------------------------
177 void ODGauge::setSampling( bool mipmapping,
178 int coverage_samples,
181 if( !updateFlag(USE_MIPMAPPING, mipmapping)
182 && _coverage_samples == coverage_samples
183 && _color_samples == color_samples )
186 if( color_samples > coverage_samples )
192 "ODGauge::setSampling: color_samples > coverage_samples not allowed!"
194 color_samples = coverage_samples;
197 _coverage_samples = coverage_samples;
198 _color_samples = color_samples;
203 //----------------------------------------------------------------------------
204 void ODGauge::setRender(bool render)
206 // Only the far camera should trigger this texture to be rendered.
207 camera->setNodeMask(render ? simgear::BACKGROUND_BIT : 0);
210 //----------------------------------------------------------------------------
211 bool ODGauge::serviceable() const
213 return _flags & AVAILABLE;
216 //----------------------------------------------------------------------------
217 void ODGauge::allocRT(osg::NodeCallback* camera_cull_callback)
219 camera = new osg::Camera;
220 camera->setDataVariance(osg::Object::DYNAMIC);
221 camera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
222 camera->setRenderOrder(osg::Camera::PRE_RENDER);
223 camera->setClearColor(osg::Vec4(0.0f, 0.0f, 0.0f , 0.0f));
224 camera->setClearStencil(0);
225 camera->setRenderTargetImplementation( osg::Camera::FRAME_BUFFER_OBJECT,
226 osg::Camera::FRAME_BUFFER );
228 if( camera_cull_callback )
229 camera->setCullCallback(camera_cull_callback);
232 updateCoordinateFrame();
235 osg::StateSet* stateSet = camera->getOrCreateStateSet();
236 stateSet->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
237 stateSet->setMode(GL_CULL_FACE, osg::StateAttribute::OFF);
238 stateSet->setMode(GL_FOG, osg::StateAttribute::OFF);
239 stateSet->setMode(GL_DEPTH_TEST, osg::StateAttribute::OFF);
240 stateSet->setAttributeAndModes(
241 new osg::PolygonMode( osg::PolygonMode::FRONT_AND_BACK,
242 osg::PolygonMode::FILL ),
243 osg::StateAttribute::ON );
244 stateSet->setAttributeAndModes(
245 new osg::AlphaFunc(osg::AlphaFunc::GREATER, 0.001f),
246 osg::StateAttribute::ON );
247 stateSet->setAttribute(new osg::ShadeModel(osg::ShadeModel::FLAT));
251 texture = new osg::Texture2D;
252 texture->setResizeNonPowerOfTwoHint(false);
253 texture->setTextureSize(_size_x, _size_y);
254 texture->setInternalFormat(GL_RGBA);
260 if( Canvas::getSystemAdapter() )
261 Canvas::getSystemAdapter()->addCamera(camera.get());
266 //----------------------------------------------------------------------------
267 void ODGauge::reinit()
269 osg::NodeCallback* cull_callback = camera ? camera->getCullCallback() : 0;
271 allocRT(cull_callback);
274 //----------------------------------------------------------------------------
275 void ODGauge::clear()
277 if( camera.valid() && Canvas::getSystemAdapter() )
278 Canvas::getSystemAdapter()->removeCamera(camera.get());
282 _flags &= ~AVAILABLE;
285 //----------------------------------------------------------------------------
286 bool ODGauge::updateFlag(Flags flag, bool enable)
288 if( bool(_flags & flag) == enable )
295 //----------------------------------------------------------------------------
296 void ODGauge::updateCoordinateFrame()
300 if( _view_width < 0 )
301 _view_width = _size_x;
302 if( _view_height < 0 )
303 _view_height = _size_y;
305 camera->setViewport(0, 0, _size_x, _size_y);
307 if( _flags & USE_IMAGE_COORDS )
308 camera->setProjectionMatrix(
309 osg::Matrix::ortho2D(0, _view_width, _view_height, 0)
312 camera->setProjectionMatrix(
313 osg::Matrix::ortho2D( -_view_width/2., _view_width/2.,
314 -_view_height/2., _view_height/2. )
318 //----------------------------------------------------------------------------
319 void ODGauge::updateStencil()
323 GLbitfield mask = GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT;
325 if( _flags & USE_STENCIL )
327 camera->attach( osg::Camera::PACKED_DEPTH_STENCIL_BUFFER,
328 GL_DEPTH_STENCIL_EXT );
329 mask |= GL_STENCIL_BUFFER_BIT;
333 camera->detach(osg::Camera::PACKED_DEPTH_STENCIL_BUFFER);
336 camera->setClearMask(mask);
339 //----------------------------------------------------------------------------
340 void ODGauge::updateSampling()
346 osg::Texture2D::MIN_FILTER,
347 (_flags & USE_MIPMAPPING) ? osg::Texture2D::LINEAR_MIPMAP_LINEAR
348 : osg::Texture2D::LINEAR
351 osg::Camera::COLOR_BUFFER,
354 _flags & USE_MIPMAPPING,
360 //----------------------------------------------------------------------------
361 void ODGauge::updateBlendMode()
365 camera->getOrCreateStateSet()
366 ->setAttributeAndModes
368 (_flags & USE_ADDITIVE_BLEND)
369 ? new osg::BlendFunc( osg::BlendFunc::SRC_ALPHA,
370 osg::BlendFunc::ONE_MINUS_SRC_ALPHA,
372 osg::BlendFunc::ONE )
373 : new osg::BlendFunc( osg::BlendFunc::SRC_ALPHA,
374 osg::BlendFunc::ONE_MINUS_SRC_ALPHA )
378 } // namespace canvas
379 } // namespace simgear