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:
181  ScopedglEnableDisable(vtkOpenGLState *state, unsigned int name)
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 
202  void Initialize(vtkOpenGLRenderWindow*);
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
OpenGL rendering window.
std::array< int, 4 > Viewport
std::array< float, 4 > ClearColor
std::array< unsigned char, 4 > ColorMask
std::array< int, 4 > Scissor
void vtkglBlendFunc(unsigned int sfactor, unsigned int dfactor)
GLState CurrentState
unsigned int BlendEquationValue2
unsigned int BlendEquationValue1
OpenGL state storage.
ScopedglEnableDisable(vtkOpenGLState *state, unsigned int name)
std::array< unsigned int, 4 > BlendFunc