VTK
vtkOpenGLState.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Visualization Toolkit
4  Module: vtkOpenGLState.h
5 
6  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7  All rights reserved.
8  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9 
10  This software is distributed WITHOUT ANY WARRANTY; without even
11  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12  PURPOSE. See the above copyright notice for more information.
13 
14 =========================================================================*/
59 #ifndef vtkOpenGLState_h
60 #define vtkOpenGLState_h
61 
62 #include "vtkRenderingOpenGL2Module.h" // For export macro
63 #include <array> // for ivar
64 
66 
67 class VTKRENDERINGOPENGL2_EXPORT vtkOpenGLState
68 {
69 public:
70  vtkOpenGLState(); // set initial values
71 
73  // cached OpenGL methods. By calling these the context will check
74  // the current value prior to making the OpenGL call. This can reduce
75  // the burden on the driver.
76  //
77  void vtkglClearColor(float red, float green, float blue, float alpha);
78  void vtkglClearDepth(double depth);
79  void vtkglDepthFunc(unsigned int val);
80  void vtkglDepthMask(unsigned char flag);
81  void vtkglColorMask(unsigned char r, unsigned char g, unsigned char b, unsigned char a);
82  void vtkglViewport(int x, int y, int width, int height);
83  void vtkglScissor(int x, int y, int width, int height);
84  void vtkglEnable(unsigned int cap);
85  void vtkglDisable(unsigned int cap);
86  void vtkglBlendFunc(unsigned int sfactor, unsigned int dfactor) {
87  this->vtkglBlendFuncSeparate(sfactor, dfactor, sfactor, dfactor);
88  }
89  void vtkglBlendFuncSeparate(unsigned int sfactorRGB, unsigned int dfactorRGB,
90  unsigned int sfactorAlpha, unsigned int dfactorAlpha);
91  void vtkglBlendEquation(unsigned int val);
92  void vtkglBlendEquationSeparate(unsigned int col, unsigned int alpha);
93  void vtkglCullFace(unsigned int val);
95 
97  // Methods to reset the state to the current OpenGL context value.
98  //
99  void ResetGlClearColorState();
100  void ResetGlClearDepthState();
101  void ResetGlDepthFuncState();
102  void ResetGlDepthMaskState();
103  void ResetGlColorMaskState();
104  void ResetGlViewportState();
105  void ResetGlScissorState();
106  void ResetGlBlendFuncState();
107  void ResetGlBlendEquationState();
108  void ResetGlCullFaceState();
110 
112  // OpenGL functions that we provide an API for even though they may
113  // not hold any state.
114  void vtkglClear(unsigned int mask);
116 
117 
119  // Get methods that can be used to query state
120  // if the state is not cached they fall through
121  // and call the underlying opengl functions
122  void vtkglGetBooleanv(unsigned int pname, unsigned char *params);
123  void vtkglGetIntegerv(unsigned int pname, int *params);
124  void vtkglGetDoublev(unsigned int pname, double *params);
125  void vtkglGetFloatv(unsigned int pname, float *params);
127 
128  // convenience to get all 4 values at once
129  void GetBlendFuncState(int *);
130 
131  // convenience to return a bool
132  // as opposed to a unsigned char
133  bool GetEnumState(unsigned int name);
134 
135  // convenience method to set a enum (glEnable/glDisable)
136  void SetEnumState(unsigned int name, bool value);
137 
141  void ResetEnumState(unsigned int name);
142 
143  // superclass for Scoped subclasses
144  template <typename T>
145  class VTKRENDERINGOPENGL2_EXPORT ScopedValue
146  {
147  public:
148  ~ScopedValue() // restore value
149  {
150  ((*this->State).*(this->Method))(this->Value);
151  }
152  protected:
154  T Value;
155  void (vtkOpenGLState::*Method)(T);
156  };
157 
158  // Scoped classes you can use to save state
159  class VTKRENDERINGOPENGL2_EXPORT ScopedglDepthMask
160  : public ScopedValue<unsigned char> {
161  public: ScopedglDepthMask(vtkOpenGLState *state); };
162  class VTKRENDERINGOPENGL2_EXPORT ScopedglClearColor
163  : public ScopedValue<std::array<float, 4> > {
164  public: ScopedglClearColor(vtkOpenGLState *state); };
165  class VTKRENDERINGOPENGL2_EXPORT ScopedglColorMask
166  : public ScopedValue<std::array<unsigned char, 4> > {
167  public: ScopedglColorMask(vtkOpenGLState *state); };
168  class VTKRENDERINGOPENGL2_EXPORT ScopedglScissor
169  : public ScopedValue<std::array<int, 4> > {
170  public: ScopedglScissor(vtkOpenGLState *state); };
171  class VTKRENDERINGOPENGL2_EXPORT ScopedglViewport
172  : public ScopedValue<std::array<int, 4> > {
173  public: ScopedglViewport(vtkOpenGLState *state); };
174  class VTKRENDERINGOPENGL2_EXPORT ScopedglBlendFuncSeparate
175  : public ScopedValue<std::array<unsigned int, 4> > {
176  public: ScopedglBlendFuncSeparate(vtkOpenGLState *state); };
177 
179  {
180  public:
182  {
183  this->State = state;
184  this->Name = name;
185  unsigned char val;
186  this->State->vtkglGetBooleanv(name, &val);
187  this->Value = val == 1;
188  }
189  ~ScopedglEnableDisable() // restore value
190  {
191  this->State->SetEnumState(this->Name, this->Value);
192  }
193  protected:
195  unsigned int Name;
196  bool Value;
197  };
198 
203 
204 protected:
205  void BlendFuncSeparate(std::array<unsigned int, 4> val);
206  void ClearColor(std::array<float, 4> val);
207  void ColorMask(std::array<unsigned char, 4> val);
208  void Scissor(std::array<int, 4> val);
209  void Viewport(std::array<int, 4> val);
210 
215  void CheckState();
216 
217  class VTKRENDERINGOPENGL2_EXPORT GLState
218  {
219  public:
220  double ClearDepth;
221  unsigned char DepthMask;
222  unsigned int DepthFunc;
223  unsigned int BlendEquationValue1;
224  unsigned int BlendEquationValue2;
225  unsigned int CullFaceMode;
226  std::array<float, 4> ClearColor;
227  std::array<unsigned char, 4> ColorMask;
228  std::array<int, 4> Viewport;
229  std::array<int, 4> Scissor;
230  std::array<unsigned int, 4> BlendFunc;
231  bool DepthTest;
232  bool CullFace;
235  bool Blend;
241  }
242  };
243 
245 };
246 
247 #endif
248 
249 // VTK-HeaderTest-Exclude: vtkOpenGLState.h
vtkOpenGLState::GLState::BlendFunc
std::array< unsigned int, 4 > BlendFunc
Definition: vtkOpenGLState.h:230
vtkOpenGLState::GLState
Definition: vtkOpenGLState.h:217
vtkOpenGLState::ScopedglClearColor
Definition: vtkOpenGLState.h:162
vtkOpenGLState::ScopedValue::Value
T Value
Definition: vtkOpenGLState.h:154
vtkOpenGLState::ColorMask
void ColorMask(std::array< unsigned char, 4 > val)
vtkX3D::alpha
Definition: vtkX3D.h:250
vtkX3D::value
Definition: vtkX3D.h:220
vtkOpenGLState::ScopedValue::~ScopedValue
~ScopedValue()
Definition: vtkOpenGLState.h:148
vtkOpenGLState::GLState::BlendEquationValue1
unsigned int BlendEquationValue1
Definition: vtkOpenGLState.h:223
vtkOpenGLState::GLState::BlendEquationValue2
unsigned int BlendEquationValue2
Definition: vtkOpenGLState.h:224
vtkOpenGLState::GLState::MinorVersion
int MinorVersion
Definition: vtkOpenGLState.h:239
vtkOpenGLState::ScopedglEnableDisable::Value
bool Value
Definition: vtkOpenGLState.h:196
vtkOpenGLState::GLState::ScissorTest
bool ScissorTest
Definition: vtkOpenGLState.h:233
vtkOpenGLState::GLState::DepthMask
unsigned char DepthMask
Definition: vtkOpenGLState.h:221
vtkOpenGLState::GLState::MaxTextureSize
int MaxTextureSize
Definition: vtkOpenGLState.h:237
vtkOpenGLState::CurrentState
GLState CurrentState
Definition: vtkOpenGLState.h:244
vtkOpenGLState::ScopedglEnableDisable::State
vtkOpenGLState * State
Definition: vtkOpenGLState.h:194
vtkOpenGLState::ScopedValue
Definition: vtkOpenGLState.h:145
vtkOpenGLState::ScopedglEnableDisable::ScopedglEnableDisable
ScopedglEnableDisable(vtkOpenGLState *state, unsigned int name)
Definition: vtkOpenGLState.h:181
vtkOpenGLState::GLState::CullFace
bool CullFace
Definition: vtkOpenGLState.h:232
vtkOpenGLState::GLState::MajorVersion
int MajorVersion
Definition: vtkOpenGLState.h:238
vtkOpenGLState::ClearColor
void ClearColor(std::array< float, 4 > val)
vtkOpenGLState::CheckState
void CheckState()
Check that this OpenGL state has consistent values with the current OpenGL context.
vtkOpenGLState::Initialize
void Initialize(vtkOpenGLRenderWindow *)
Initialize OpenGL context using current state.
vtkOpenGLState::ScopedglEnableDisable::Name
unsigned int Name
Definition: vtkOpenGLState.h:195
vtkOpenGLState::ScopedglColorMask
Definition: vtkOpenGLState.h:165
vtkX3D::height
Definition: vtkX3D.h:254
vtkOpenGLState::GLState::DepthTest
bool DepthTest
Definition: vtkOpenGLState.h:231
vtkOpenGLState::GLState::GLState
GLState()
Definition: vtkOpenGLState.h:240
vtkOpenGLState::GLState::Viewport
std::array< int, 4 > Viewport
Definition: vtkOpenGLState.h:228
vtkOpenGLState::GLState::Blend
bool Blend
Definition: vtkOpenGLState.h:235
vtkOpenGLState::GLState::DepthFunc
unsigned int DepthFunc
Definition: vtkOpenGLState.h:222
vtkOpenGLState::GLState::Scissor
std::array< int, 4 > Scissor
Definition: vtkOpenGLState.h:229
vtkOpenGLState::ScopedglEnableDisable::~ScopedglEnableDisable
~ScopedglEnableDisable()
Definition: vtkOpenGLState.h:189
vtkX3D::name
Definition: vtkX3D.h:219
vtkOpenGLState::ScopedglViewport
Definition: vtkOpenGLState.h:171
vtkOpenGLState::ScopedglDepthMask
Definition: vtkOpenGLState.h:159
vtkOpenGLState::GLState::MultiSample
bool MultiSample
Definition: vtkOpenGLState.h:236
vtkOpenGLState::Viewport
void Viewport(std::array< int, 4 > val)
vtkOpenGLState::ScopedglBlendFuncSeparate
Definition: vtkOpenGLState.h:174
vtkOpenGLState::vtkglBlendFunc
void vtkglBlendFunc(unsigned int sfactor, unsigned int dfactor)
Definition: vtkOpenGLState.h:86
vtkOpenGLState::ScopedglScissor
Definition: vtkOpenGLState.h:168
vtkOpenGLRenderWindow
OpenGL rendering window.
Definition: vtkOpenGLRenderWindow.h:53
vtkOpenGLState::GLState::ClearDepth
double ClearDepth
Definition: vtkOpenGLState.h:220
vtkOpenGLState::ScopedglEnableDisable
Definition: vtkOpenGLState.h:178
vtkOpenGLState::ScopedValue::State
vtkOpenGLState * State
Definition: vtkOpenGLState.h:153
vtkOpenGLState::GLState::ColorMask
std::array< unsigned char, 4 > ColorMask
Definition: vtkOpenGLState.h:227
vtkOpenGLState::GLState::StencilTest
bool StencilTest
Definition: vtkOpenGLState.h:234
vtkOpenGLState
OpenGL state storage.
Definition: vtkOpenGLState.h:67
vtkOpenGLState::GLState::ClearColor
std::array< float, 4 > ClearColor
Definition: vtkOpenGLState.h:226
vtkOpenGLState::GLState::CullFaceMode
unsigned int CullFaceMode
Definition: vtkOpenGLState.h:225
vtkOpenGLState::Scissor
void Scissor(std::array< int, 4 > val)
vtkOpenGLState::BlendFuncSeparate
void BlendFuncSeparate(std::array< unsigned int, 4 > val)