]> git.mxchange.org Git - simgear.git/blob - simgear/canvas/ODGauge.hxx
canvas::Layout: clear parent/canvas after calling onRemove.
[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 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.
17 //
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.
22 //
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
26
27 #ifndef _SG_OD_GAUGE_HXX
28 #define _SG_OD_GAUGE_HXX
29
30 #include "canvas_fwd.hxx"
31
32 #include <osg/NodeCallback>
33 #include <osg/Group>
34
35 namespace osg
36 {
37   class Camera;
38   class Texture2D;
39 }
40
41 namespace simgear
42 {
43 namespace canvas
44 {
45
46   /**
47    * Owner Drawn Gauge (aka render-to-texture) helper class
48    */
49   class ODGauge
50   {
51     public:
52
53       ODGauge();
54       virtual ~ODGauge();
55
56       /**
57        * Set the size of the render target.
58        *
59        * @param size_x    X size
60        * @param size_y    Y size. Defaults to size_x if not specified
61        */
62       void setSize(int size_x, int size_y = -1);
63
64       /**
65        * Set the size of the viewport
66        *
67        * @param width
68        * @param height    Defaults to width if not specified
69        */
70       void setViewSize(int width, int height = -1);
71
72       osg::Vec2s getViewSize() const;
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        * Enable/Disable additive alpha blending (Can improve results with
96        * transparent background)
97        */
98       void useAdditiveBlend(bool use = true);
99
100       /**
101        * Set sampling parameters for mipmapping and coverage sampling
102        * antialiasing.
103        *
104        * @note color_samples is not allowed to be higher than coverage_samples
105        *
106        */
107       void setSampling( bool mipmapping,
108                         int coverage_samples = 0,
109                         int color_samples = 0 );
110
111       /**
112        * Enable/Disable updating the texture (If disabled the contents of the
113        * texture remains with the outcome of the last rendering pass)
114        */
115       void setRender(bool render);
116
117       /**
118        * Say if we can render to a texture.
119        * @return true if rtt is available
120        */
121       bool serviceable() const;
122
123       /**
124        * Get the OSG camera for drawing this gauge.
125        */
126       osg::Camera* getCamera() const { return camera.get(); }
127
128       osg::Texture2D* getTexture() const { return texture.get(); }
129
130       // Real initialization function. Bad name.
131       void allocRT(osg::NodeCallback* camera_cull_callback = 0);
132       void reinit();
133       void clear();
134
135     protected:
136
137       int _size_x,
138           _size_y,
139           _view_width,
140           _view_height;
141
142       enum Flags
143       {
144         AVAILABLE           = 1,
145         USE_IMAGE_COORDS    = AVAILABLE << 1,
146         USE_STENCIL         = USE_IMAGE_COORDS << 1,
147         USE_MIPMAPPING      = USE_STENCIL << 1,
148         USE_ADDITIVE_BLEND  = USE_MIPMAPPING << 1
149       };
150
151       uint32_t _flags;
152
153       // Multisampling parameters
154       int  _coverage_samples,
155            _color_samples;
156
157       osg::ref_ptr<osg::Camera> camera;
158       osg::ref_ptr<osg::Texture2D> texture;
159
160       bool updateFlag(Flags flag, bool enable);
161
162       void updateCoordinateFrame();
163       void updateStencil();
164       void updateSampling();
165       void updateBlendMode();
166
167   };
168
169 } // namespace canvas
170 } // namespace simgear
171
172 #endif // _SG_OD_GAUGE_HXX