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
54 //----------------------------------------------------------------------------
61 _coverage_samples( 0 ),
67 //----------------------------------------------------------------------------
73 //----------------------------------------------------------------------------
74 void ODGauge::setSystemAdapter(const SystemAdapterPtr& system_adapter)
76 _system_adapter = system_adapter;
79 //----------------------------------------------------------------------------
80 void ODGauge::setSize(int size_x, int size_y)
83 _size_y = size_y < 0 ? size_x : size_y;
88 texture->setTextureSize(_size_x, _size_y);
91 //----------------------------------------------------------------------------
92 void ODGauge::setViewSize(int width, int height)
95 _view_height = height < 0 ? width : height;
98 updateCoordinateFrame();
101 //----------------------------------------------------------------------------
102 osg::Vec2s ODGauge::getViewSize() const
104 return osg::Vec2s(_view_width, _view_height);
107 //----------------------------------------------------------------------------
108 void ODGauge::useImageCoords(bool use)
110 if( updateFlag(USE_IMAGE_COORDS, use) && texture )
111 updateCoordinateFrame();
114 //----------------------------------------------------------------------------
115 void ODGauge::useStencil(bool use)
117 if( updateFlag(USE_STENCIL, use) && texture )
121 //----------------------------------------------------------------------------
122 void ODGauge::useAdditiveBlend(bool use)
124 if( updateFlag(USE_ADDITIVE_BLEND, use) && camera )
128 //----------------------------------------------------------------------------
129 void ODGauge::setSampling( bool mipmapping,
130 int coverage_samples,
133 if( !updateFlag(USE_MIPMAPPING, mipmapping)
134 && _coverage_samples == coverage_samples
135 && _color_samples == color_samples )
138 if( color_samples > coverage_samples )
144 "ODGauge::setSampling: color_samples > coverage_samples not allowed!"
146 color_samples = coverage_samples;
149 _coverage_samples = coverage_samples;
150 _color_samples = color_samples;
155 //----------------------------------------------------------------------------
156 void ODGauge::setRender(bool render)
158 // Only the far camera should trigger this texture to be rendered.
159 camera->setNodeMask(render ? simgear::BACKGROUND_BIT : 0);
162 //----------------------------------------------------------------------------
163 bool ODGauge::serviceable() const
165 return _flags & AVAILABLE;
168 //----------------------------------------------------------------------------
169 void ODGauge::allocRT(osg::NodeCallback* camera_cull_callback)
171 camera = new osg::Camera;
172 camera->setDataVariance(osg::Object::DYNAMIC);
173 camera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
174 camera->setRenderOrder(osg::Camera::PRE_RENDER);
175 camera->setClearColor(osg::Vec4(0.0f, 0.0f, 0.0f , 0.0f));
176 camera->setClearStencil(0);
177 camera->setRenderTargetImplementation( osg::Camera::FRAME_BUFFER_OBJECT,
178 osg::Camera::FRAME_BUFFER );
180 if( camera_cull_callback )
181 camera->setCullCallback(camera_cull_callback);
184 updateCoordinateFrame();
187 osg::StateSet* stateSet = camera->getOrCreateStateSet();
188 stateSet->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
189 stateSet->setMode(GL_CULL_FACE, osg::StateAttribute::OFF);
190 stateSet->setMode(GL_FOG, osg::StateAttribute::OFF);
191 stateSet->setMode(GL_DEPTH_TEST, osg::StateAttribute::OFF);
192 stateSet->setAttributeAndModes(
193 new osg::PolygonMode( osg::PolygonMode::FRONT_AND_BACK,
194 osg::PolygonMode::FILL ),
195 osg::StateAttribute::ON );
196 stateSet->setAttributeAndModes(
197 new osg::AlphaFunc(osg::AlphaFunc::GREATER, 0.001f),
198 osg::StateAttribute::ON );
199 stateSet->setAttribute(new osg::ShadeModel(osg::ShadeModel::FLAT));
203 texture = new osg::Texture2D;
204 texture->setResizeNonPowerOfTwoHint(false);
205 texture->setTextureSize(_size_x, _size_y);
206 texture->setInternalFormat(GL_RGBA);
212 if( _system_adapter )
213 _system_adapter->addCamera(camera.get());
218 //----------------------------------------------------------------------------
219 void ODGauge::reinit()
221 osg::NodeCallback* cull_callback = camera ? camera->getCullCallback() : 0;
223 allocRT(cull_callback);
226 //----------------------------------------------------------------------------
227 void ODGauge::clear()
229 if( camera.valid() && _system_adapter )
230 _system_adapter->removeCamera(camera.get());
234 _flags &= ~AVAILABLE;
237 //----------------------------------------------------------------------------
238 bool ODGauge::updateFlag(Flags flag, bool enable)
240 if( bool(_flags & flag) == enable )
247 //----------------------------------------------------------------------------
248 void ODGauge::updateCoordinateFrame()
252 if( _view_width < 0 )
253 _view_width = _size_x;
254 if( _view_height < 0 )
255 _view_height = _size_y;
257 camera->setViewport(0, 0, _size_x, _size_y);
259 if( _flags & USE_IMAGE_COORDS )
260 camera->setProjectionMatrix(
261 osg::Matrix::ortho2D(0, _view_width, _view_height, 0)
264 camera->setProjectionMatrix(
265 osg::Matrix::ortho2D( -_view_width/2., _view_width/2.,
266 -_view_height/2., _view_height/2. )
270 //----------------------------------------------------------------------------
271 void ODGauge::updateStencil()
275 GLbitfield mask = GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT;
277 if( _flags & USE_STENCIL )
279 camera->attach( osg::Camera::PACKED_DEPTH_STENCIL_BUFFER,
280 GL_DEPTH_STENCIL_EXT );
281 mask |= GL_STENCIL_BUFFER_BIT;
285 camera->detach(osg::Camera::PACKED_DEPTH_STENCIL_BUFFER);
288 camera->setClearMask(mask);
291 //----------------------------------------------------------------------------
292 void ODGauge::updateSampling()
298 osg::Texture2D::MIN_FILTER,
299 (_flags & USE_MIPMAPPING) ? osg::Texture2D::LINEAR_MIPMAP_LINEAR
300 : osg::Texture2D::LINEAR
303 osg::Camera::COLOR_BUFFER,
306 _flags & USE_MIPMAPPING,
312 //----------------------------------------------------------------------------
313 void ODGauge::updateBlendMode()
317 camera->getOrCreateStateSet()
318 ->setAttributeAndModes
320 (_flags & USE_ADDITIVE_BLEND)
321 ? new osg::BlendFunc( osg::BlendFunc::SRC_ALPHA,
322 osg::BlendFunc::ONE_MINUS_SRC_ALPHA,
324 osg::BlendFunc::ONE )
325 : new osg::BlendFunc( osg::BlendFunc::SRC_ALPHA,
326 osg::BlendFunc::ONE_MINUS_SRC_ALPHA )
330 } // namespace canvas
331 } // namespace simgear