]> git.mxchange.org Git - simgear.git/blob - simgear/canvas/ODGauge.hxx
Link with ShivaVG
[simgear.git] / simgear / canvas / ODGauge.hxx
1 // Owner Drawn Gauge helper class
2 //
3 // Written by Harald JOHNSEN, started May 2005.
4 //
5 // Copyright (C) 2005  Harald JOHNSEN - hjohnsen@evc.net
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 program is free software; you can redistribute it and/or
14 // modify it under the terms of the GNU General Public License as
15 // published by the Free Software Foundation; either version 2 of the
16 // License, or (at your option) any later version.
17 //
18 // This program is distributed in the hope that it will be useful, but
19 // WITHOUT ANY WARRANTY; without even the implied warranty of
20 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21 // General Public License for more details.
22 //
23 // You should have received a copy of the GNU General Public License
24 // along with this program; if not, write to the Free Software
25 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
26 //
27 //
28
29 #ifndef _SG_OD_GAUGE_HXX
30 #define _SG_OD_GAUGE_HXX
31
32 #include <osg/NodeCallback>
33 #include <osg/Group>
34
35 #include <boost/function.hpp>
36
37 namespace osg
38 {
39   class Camera;
40   class Texture2D;
41 }
42
43 namespace simgear
44 {
45   /**
46    * Owner Drawn Gauge (aka render-to-texture) helper class
47    */
48   class ODGauge
49   {
50     public:
51
52       typedef boost::function<void(osg::Camera*)> CameraRegistrationCallback;
53
54       ODGauge( const CameraRegistrationCallback& cb_camera_add,
55                const CameraRegistrationCallback& cb_camera_remove );
56       virtual ~ODGauge();
57
58       /**
59        * Set the size of the render target.
60        *
61        * @param size_x    X size
62        * @param size_y    Y size. Defaults to size_x if not specified
63        */
64       void setSize(int size_x, int size_y = -1);
65
66       /**
67        * Set the size of the viewport
68        *
69        * @param width
70        * @param height    Defaults to width if not specified
71        */
72       void setViewSize(int width, int height = -1);
73
74       /**
75        * DEPRECATED
76        *
77        * Get size of squared texture
78        */
79       int size() const { return _size_x; }
80
81       /**
82        * Set whether to use image coordinates or not.
83        *
84        * Default: origin == center of texture
85        * Image Coords: origin == top left corner
86        */
87       void useImageCoords(bool use = true);
88
89       /**
90        * Enable/Disable using a stencil buffer
91        */
92       void useStencil(bool use = true);
93
94       /**
95        * Set sampling parameters for mipmapping and coverage sampling
96        * antialiasing.
97        *
98        * @note color_samples is not allowed to be higher than coverage_samples
99        *
100        */
101       void setSampling( bool mipmapping,
102                         int coverage_samples = 0,
103                         int color_samples = 0 );
104
105       /**
106        * Enable/Disable updating the texture (If disabled the contents of the
107        * texture remains with the outcome of the last rendering pass)
108        */
109       void setRender(bool render);
110
111       /**
112        * Say if we can render to a texture.
113        * @return true if rtt is available
114        */
115       bool serviceable(void);
116
117       /**
118        * Get the OSG camera for drawing this gauge.
119        */
120       osg::Camera* getCamera() const { return camera.get(); }
121
122       osg::Texture2D* getTexture() const { return texture.get(); }
123
124       // Real initialization function. Bad name.
125       void allocRT(osg::NodeCallback* camera_cull_callback = 0);
126
127     private:
128
129       int _size_x,
130           _size_y,
131           _view_width,
132           _view_height;
133       bool _use_image_coords,
134            _use_stencil,
135            _use_mipmapping;
136
137       // Multisampling parameters
138       int  _coverage_samples,
139            _color_samples;
140
141       bool rtAvailable;
142       osg::ref_ptr<osg::Camera> camera;
143       osg::ref_ptr<osg::Texture2D> texture;
144
145       CameraRegistrationCallback _cb_cam_add,
146                                  _cb_cam_remove;
147
148       void updateCoordinateFrame();
149       void updateStencil();
150       void updateSampling();
151
152   };
153
154 } // namespace simgear
155
156 #endif // _SG_OD_GAUGE_HXX