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"
32 #include "CanvasSystemAdapter.hxx"
34 #include <simgear/debug/logstream.hxx>
35 #include <simgear/scene/util/RenderConstants.hxx>
37 #include <osg/Texture2D>
38 #include <osg/AlphaFunc>
39 #include <osg/BlendFunc>
42 #include <osg/PolygonMode>
43 #include <osg/ShadeModel>
44 #include <osg/StateSet>
45 #include <osg/FrameBufferObject> // for GL_DEPTH_STENCIL_EXT on Windows
46 #include <osgUtil/RenderBin>
56 public osgUtil::RenderBin
62 PreOrderBin( const RenderBin& rhs,
63 const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY ):
64 RenderBin(rhs, copyop)
67 virtual osg::Object* cloneType() const
69 return new PreOrderBin();
71 virtual osg::Object* clone(const osg::CopyOp& copyop) const
73 return new PreOrderBin(*this,copyop);
75 virtual bool isSameKindAs(const osg::Object* obj) const
77 return dynamic_cast<const PreOrderBin*>(obj) != 0L;
79 virtual const char* className() const
86 // Do not sort to keep traversal order...
90 #ifndef OSG_INIT_SINGLETON_PROXY
92 * http://svn.openscenegraph.org/osg/OpenSceneGraph/trunk/include/osg/Object
94 * Helper macro that creates a static proxy object to call singleton function
95 * on it's construction, ensuring that the singleton gets initialized at
98 # define OSG_INIT_SINGLETON_PROXY(ProxyName, Func)\
99 static struct ProxyName{ ProxyName() { Func; } } s_##ProxyName;
102 OSG_INIT_SINGLETON_PROXY(
104 (osgUtil::RenderBin::addRenderBinPrototype("PreOrderBin", new PreOrderBin))
107 //----------------------------------------------------------------------------
114 _coverage_samples( 0 ),
120 //----------------------------------------------------------------------------
126 //----------------------------------------------------------------------------
127 void ODGauge::setSystemAdapter(const SystemAdapterPtr& system_adapter)
129 _system_adapter = system_adapter;
132 //----------------------------------------------------------------------------
133 void ODGauge::setSize(int size_x, int size_y)
136 _size_y = size_y < 0 ? size_x : size_y;
141 texture->setTextureSize(_size_x, _size_y);
144 //----------------------------------------------------------------------------
145 void ODGauge::setViewSize(int width, int height)
148 _view_height = height < 0 ? width : height;
151 updateCoordinateFrame();
154 //----------------------------------------------------------------------------
155 osg::Vec2s ODGauge::getViewSize() const
157 return osg::Vec2s(_view_width, _view_height);
160 //----------------------------------------------------------------------------
161 void ODGauge::useImageCoords(bool use)
163 if( updateFlag(USE_IMAGE_COORDS, use) && texture )
164 updateCoordinateFrame();
167 //----------------------------------------------------------------------------
168 void ODGauge::useStencil(bool use)
170 if( updateFlag(USE_STENCIL, use) && texture )
174 //----------------------------------------------------------------------------
175 void ODGauge::useAdditiveBlend(bool use)
177 if( updateFlag(USE_ADDITIVE_BLEND, use) && camera )
181 //----------------------------------------------------------------------------
182 void ODGauge::setSampling( bool mipmapping,
183 int coverage_samples,
186 if( !updateFlag(USE_MIPMAPPING, mipmapping)
187 && _coverage_samples == coverage_samples
188 && _color_samples == color_samples )
191 if( color_samples > coverage_samples )
197 "ODGauge::setSampling: color_samples > coverage_samples not allowed!"
199 color_samples = coverage_samples;
202 _coverage_samples = coverage_samples;
203 _color_samples = color_samples;
208 //----------------------------------------------------------------------------
209 void ODGauge::setRender(bool render)
211 // Only the far camera should trigger this texture to be rendered.
212 camera->setNodeMask(render ? simgear::BACKGROUND_BIT : 0);
215 //----------------------------------------------------------------------------
216 bool ODGauge::serviceable() const
218 return _flags & AVAILABLE;
221 //----------------------------------------------------------------------------
222 void ODGauge::allocRT(osg::NodeCallback* camera_cull_callback)
224 camera = new osg::Camera;
225 camera->setDataVariance(osg::Object::DYNAMIC);
226 camera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
227 camera->setRenderOrder(osg::Camera::PRE_RENDER);
228 camera->setClearColor(osg::Vec4(0.0f, 0.0f, 0.0f , 0.0f));
229 camera->setClearStencil(0);
230 camera->setRenderTargetImplementation( osg::Camera::FRAME_BUFFER_OBJECT,
231 osg::Camera::FRAME_BUFFER );
233 if( camera_cull_callback )
234 camera->setCullCallback(camera_cull_callback);
237 updateCoordinateFrame();
240 osg::StateSet* stateSet = camera->getOrCreateStateSet();
241 stateSet->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
242 stateSet->setMode(GL_CULL_FACE, osg::StateAttribute::OFF);
243 stateSet->setMode(GL_FOG, osg::StateAttribute::OFF);
244 stateSet->setMode(GL_DEPTH_TEST, osg::StateAttribute::OFF);
245 stateSet->setAttributeAndModes(
246 new osg::PolygonMode( osg::PolygonMode::FRONT_AND_BACK,
247 osg::PolygonMode::FILL ),
248 osg::StateAttribute::ON );
249 stateSet->setAttributeAndModes(
250 new osg::AlphaFunc(osg::AlphaFunc::GREATER, 0.001f),
251 osg::StateAttribute::ON );
252 stateSet->setAttribute(new osg::ShadeModel(osg::ShadeModel::FLAT));
256 texture = new osg::Texture2D;
257 texture->setResizeNonPowerOfTwoHint(false);
258 texture->setTextureSize(_size_x, _size_y);
259 texture->setInternalFormat(GL_RGBA);
265 if( _system_adapter )
266 _system_adapter->addCamera(camera.get());
271 //----------------------------------------------------------------------------
272 void ODGauge::reinit()
274 osg::NodeCallback* cull_callback = camera ? camera->getCullCallback() : 0;
276 allocRT(cull_callback);
279 //----------------------------------------------------------------------------
280 void ODGauge::clear()
282 if( camera.valid() && _system_adapter )
283 _system_adapter->removeCamera(camera.get());
287 _flags &= ~AVAILABLE;
290 //----------------------------------------------------------------------------
291 bool ODGauge::updateFlag(Flags flag, bool enable)
293 if( bool(_flags & flag) == enable )
300 //----------------------------------------------------------------------------
301 void ODGauge::updateCoordinateFrame()
305 if( _view_width < 0 )
306 _view_width = _size_x;
307 if( _view_height < 0 )
308 _view_height = _size_y;
310 camera->setViewport(0, 0, _size_x, _size_y);
312 if( _flags & USE_IMAGE_COORDS )
313 camera->setProjectionMatrix(
314 osg::Matrix::ortho2D(0, _view_width, _view_height, 0)
317 camera->setProjectionMatrix(
318 osg::Matrix::ortho2D( -_view_width/2., _view_width/2.,
319 -_view_height/2., _view_height/2. )
323 //----------------------------------------------------------------------------
324 void ODGauge::updateStencil()
328 GLbitfield mask = GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT;
330 if( _flags & USE_STENCIL )
332 camera->attach( osg::Camera::PACKED_DEPTH_STENCIL_BUFFER,
333 GL_DEPTH_STENCIL_EXT );
334 mask |= GL_STENCIL_BUFFER_BIT;
338 camera->detach(osg::Camera::PACKED_DEPTH_STENCIL_BUFFER);
341 camera->setClearMask(mask);
344 //----------------------------------------------------------------------------
345 void ODGauge::updateSampling()
351 osg::Texture2D::MIN_FILTER,
352 (_flags & USE_MIPMAPPING) ? osg::Texture2D::LINEAR_MIPMAP_LINEAR
353 : osg::Texture2D::LINEAR
356 osg::Camera::COLOR_BUFFER,
359 _flags & USE_MIPMAPPING,
365 //----------------------------------------------------------------------------
366 void ODGauge::updateBlendMode()
370 camera->getOrCreateStateSet()
371 ->setAttributeAndModes
373 (_flags & USE_ADDITIVE_BLEND)
374 ? new osg::BlendFunc( osg::BlendFunc::SRC_ALPHA,
375 osg::BlendFunc::ONE_MINUS_SRC_ALPHA,
377 osg::BlendFunc::ONE )
378 : new osg::BlendFunc( osg::BlendFunc::SRC_ALPHA,
379 osg::BlendFunc::ONE_MINUS_SRC_ALPHA )
383 } // namespace canvas
384 } // namespace simgear