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 OSG_INIT_SINGLETON_PROXY(
92 (osgUtil::RenderBin::addRenderBinPrototype("PreOrderBin", new PreOrderBin))
95 //----------------------------------------------------------------------------
102 _coverage_samples( 0 ),
108 //----------------------------------------------------------------------------
114 //----------------------------------------------------------------------------
115 void ODGauge::setSystemAdapter(const SystemAdapterPtr& system_adapter)
117 _system_adapter = system_adapter;
120 //----------------------------------------------------------------------------
121 void ODGauge::setSize(int size_x, int size_y)
124 _size_y = size_y < 0 ? size_x : size_y;
129 texture->setTextureSize(_size_x, _size_y);
132 //----------------------------------------------------------------------------
133 void ODGauge::setViewSize(int width, int height)
136 _view_height = height < 0 ? width : height;
139 updateCoordinateFrame();
142 //----------------------------------------------------------------------------
143 osg::Vec2s ODGauge::getViewSize() const
145 return osg::Vec2s(_view_width, _view_height);
148 //----------------------------------------------------------------------------
149 void ODGauge::useImageCoords(bool use)
151 if( updateFlag(USE_IMAGE_COORDS, use) && texture )
152 updateCoordinateFrame();
155 //----------------------------------------------------------------------------
156 void ODGauge::useStencil(bool use)
158 if( updateFlag(USE_STENCIL, use) && texture )
162 //----------------------------------------------------------------------------
163 void ODGauge::useAdditiveBlend(bool use)
165 if( updateFlag(USE_ADDITIVE_BLEND, use) && camera )
169 //----------------------------------------------------------------------------
170 void ODGauge::setSampling( bool mipmapping,
171 int coverage_samples,
174 if( !updateFlag(USE_MIPMAPPING, mipmapping)
175 && _coverage_samples == coverage_samples
176 && _color_samples == color_samples )
179 if( color_samples > coverage_samples )
185 "ODGauge::setSampling: color_samples > coverage_samples not allowed!"
187 color_samples = coverage_samples;
190 _coverage_samples = coverage_samples;
191 _color_samples = color_samples;
196 //----------------------------------------------------------------------------
197 void ODGauge::setRender(bool render)
199 // Only the far camera should trigger this texture to be rendered.
200 camera->setNodeMask(render ? simgear::BACKGROUND_BIT : 0);
203 //----------------------------------------------------------------------------
204 bool ODGauge::serviceable() const
206 return _flags & AVAILABLE;
209 //----------------------------------------------------------------------------
210 void ODGauge::allocRT(osg::NodeCallback* camera_cull_callback)
212 camera = new osg::Camera;
213 camera->setDataVariance(osg::Object::DYNAMIC);
214 camera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
215 camera->setRenderOrder(osg::Camera::PRE_RENDER);
216 camera->setClearColor(osg::Vec4(0.0f, 0.0f, 0.0f , 0.0f));
217 camera->setClearStencil(0);
218 camera->setRenderTargetImplementation( osg::Camera::FRAME_BUFFER_OBJECT,
219 osg::Camera::FRAME_BUFFER );
221 if( camera_cull_callback )
222 camera->setCullCallback(camera_cull_callback);
225 updateCoordinateFrame();
228 osg::StateSet* stateSet = camera->getOrCreateStateSet();
229 stateSet->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
230 stateSet->setMode(GL_CULL_FACE, osg::StateAttribute::OFF);
231 stateSet->setMode(GL_FOG, osg::StateAttribute::OFF);
232 stateSet->setMode(GL_DEPTH_TEST, osg::StateAttribute::OFF);
233 stateSet->setAttributeAndModes(
234 new osg::PolygonMode( osg::PolygonMode::FRONT_AND_BACK,
235 osg::PolygonMode::FILL ),
236 osg::StateAttribute::ON );
237 stateSet->setAttributeAndModes(
238 new osg::AlphaFunc(osg::AlphaFunc::GREATER, 0.001f),
239 osg::StateAttribute::ON );
240 stateSet->setAttribute(new osg::ShadeModel(osg::ShadeModel::FLAT));
244 texture = new osg::Texture2D;
245 texture->setResizeNonPowerOfTwoHint(false);
246 texture->setTextureSize(_size_x, _size_y);
247 texture->setInternalFormat(GL_RGBA);
253 if( _system_adapter )
254 _system_adapter->addCamera(camera.get());
259 //----------------------------------------------------------------------------
260 void ODGauge::reinit()
262 osg::NodeCallback* cull_callback = camera ? camera->getCullCallback() : 0;
264 allocRT(cull_callback);
267 //----------------------------------------------------------------------------
268 void ODGauge::clear()
270 if( camera.valid() && _system_adapter )
271 _system_adapter->removeCamera(camera.get());
275 _flags &= ~AVAILABLE;
278 //----------------------------------------------------------------------------
279 bool ODGauge::updateFlag(Flags flag, bool enable)
281 if( bool(_flags & flag) == enable )
288 //----------------------------------------------------------------------------
289 void ODGauge::updateCoordinateFrame()
293 if( _view_width < 0 )
294 _view_width = _size_x;
295 if( _view_height < 0 )
296 _view_height = _size_y;
298 camera->setViewport(0, 0, _size_x, _size_y);
300 if( _flags & USE_IMAGE_COORDS )
301 camera->setProjectionMatrix(
302 osg::Matrix::ortho2D(0, _view_width, _view_height, 0)
305 camera->setProjectionMatrix(
306 osg::Matrix::ortho2D( -_view_width/2., _view_width/2.,
307 -_view_height/2., _view_height/2. )
311 //----------------------------------------------------------------------------
312 void ODGauge::updateStencil()
316 GLbitfield mask = GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT;
318 if( _flags & USE_STENCIL )
320 camera->attach( osg::Camera::PACKED_DEPTH_STENCIL_BUFFER,
321 GL_DEPTH_STENCIL_EXT );
322 mask |= GL_STENCIL_BUFFER_BIT;
326 camera->detach(osg::Camera::PACKED_DEPTH_STENCIL_BUFFER);
329 camera->setClearMask(mask);
332 //----------------------------------------------------------------------------
333 void ODGauge::updateSampling()
339 osg::Texture2D::MIN_FILTER,
340 (_flags & USE_MIPMAPPING) ? osg::Texture2D::LINEAR_MIPMAP_LINEAR
341 : osg::Texture2D::LINEAR
344 osg::Camera::COLOR_BUFFER,
347 _flags & USE_MIPMAPPING,
353 //----------------------------------------------------------------------------
354 void ODGauge::updateBlendMode()
358 camera->getOrCreateStateSet()
359 ->setAttributeAndModes
361 (_flags & USE_ADDITIVE_BLEND)
362 ? new osg::BlendFunc( osg::BlendFunc::SRC_ALPHA,
363 osg::BlendFunc::ONE_MINUS_SRC_ALPHA,
365 osg::BlendFunc::ONE )
366 : new osg::BlendFunc( osg::BlendFunc::SRC_ALPHA,
367 osg::BlendFunc::ONE_MINUS_SRC_ALPHA )
371 } // namespace canvas
372 } // namespace simgear