VTK  9.0.1
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 =========================================================================*/
63 #ifndef vtkOpenGLState_h
64 #define vtkOpenGLState_h
65 
66 #include "vtkObject.h"
67 #include "vtkRenderingOpenGL2Module.h" // For export macro
68 #include <array> // for ivar
69 #include <list> // for ivar
70 #include <map> // for ivar
71 
76 class vtkTextureObject;
78 
79 class VTKRENDERINGOPENGL2_EXPORT vtkOpenGLState : public vtkObject
80 {
81 public:
82  static vtkOpenGLState* New();
83  vtkTypeMacro(vtkOpenGLState, vtkObject);
84 
86  // cached OpenGL methods. By calling these the context will check
87  // the current value prior to making the OpenGL call. This can reduce
88  // the burden on the driver.
89  //
90  void vtkglClearColor(float red, float green, float blue, float alpha);
91  void vtkglClearDepth(double depth);
92  void vtkglDepthFunc(unsigned int val);
93  void vtkglDepthMask(unsigned char flag);
94  void vtkglColorMask(unsigned char r, unsigned char g, unsigned char b, unsigned char a);
95  void vtkglViewport(int x, int y, int width, int height);
96  void vtkglScissor(int x, int y, int width, int height);
97  void vtkglEnable(unsigned int cap);
98  void vtkglDisable(unsigned int cap);
99  void vtkglBlendFunc(unsigned int sfactor, unsigned int dfactor)
100  {
101  this->vtkglBlendFuncSeparate(sfactor, dfactor, sfactor, dfactor);
102  }
103  void vtkglBlendFuncSeparate(unsigned int sfactorRGB, unsigned int dfactorRGB,
104  unsigned int sfactorAlpha, unsigned int dfactorAlpha);
105  void vtkglBlendEquation(unsigned int val);
106  void vtkglBlendEquationSeparate(unsigned int col, unsigned int alpha);
107  void vtkglCullFace(unsigned int val);
108  void vtkglActiveTexture(unsigned int);
109 
110  void vtkglBindFramebuffer(unsigned int target, unsigned int fb);
111  void vtkglDrawBuffer(unsigned int);
112  void vtkglDrawBuffers(unsigned int n, unsigned int*);
113  void vtkglReadBuffer(unsigned int);
114 
115  void vtkglPointSize(float);
116  void vtkglLineWidth(float);
117  void vtkglStencilMaskSeparate(unsigned int face, unsigned int mask);
118  void vtkglStencilMask(unsigned int mask);
120  unsigned int face, unsigned int sfail, unsigned int dpfail, unsigned int dppass);
121  void vtkglStencilOp(unsigned int sfail, unsigned int dpfail, unsigned int dppass);
122  void vtkglStencilFuncSeparate(unsigned int face, unsigned int func, int ref, unsigned int mask);
123  void vtkglStencilFunc(unsigned int func, int ref, unsigned int mask);
124 
126  void vtkDrawBuffers(unsigned int n, unsigned int*, vtkOpenGLFramebufferObject*);
128 
129  void vtkglPixelStorei(unsigned int, int);
131 
133  // Methods to reset the state to the current OpenGL context value.
134  // These methods are useful when interfacing with third party code
135  // that may have changed the opengl state.
136  //
149 
151  // OpenGL functions that we provide an API for even though they may
152  // not hold any state.
153  void vtkglClear(unsigned int mask);
155 
157  // Get methods that can be used to query state if the state is not cached
158  // they fall through and call the underlying opengl functions
159  void vtkglGetBooleanv(unsigned int pname, unsigned char* params);
160  void vtkglGetIntegerv(unsigned int pname, int* params);
161  void vtkglGetDoublev(unsigned int pname, double* params);
162  void vtkglGetFloatv(unsigned int pname, float* params);
164 
165  // convenience to get all 4 values at once
166  void GetBlendFuncState(int*);
167 
168  // convenience to return a bool
169  // as opposed to a unsigned char
170  bool GetEnumState(unsigned int name);
171 
172  // convenience method to set a enum (glEnable/glDisable)
173  void SetEnumState(unsigned int name, bool value);
174 
178  void ResetEnumState(unsigned int name);
179 
180  // superclass for Scoped subclasses
181  template <typename T>
182  class VTKRENDERINGOPENGL2_EXPORT ScopedValue
183  {
184  public:
185  ~ScopedValue() // restore value
186  {
187  ((*this->State).*(this->Method))(this->Value);
188  }
189 
190  protected:
192  T Value;
193  void (vtkOpenGLState::*Method)(T);
194  };
195 
200 
205 
210 
215 
217 
221  {
222  this->PushDrawFramebufferBinding();
223  this->PushReadFramebufferBinding();
224  }
227 
229  {
230  this->PopReadFramebufferBinding();
231  this->PopDrawFramebufferBinding();
232  }
235 
238 
239  // Scoped classes you can use to save state
240  class VTKRENDERINGOPENGL2_EXPORT ScopedglDepthMask : public ScopedValue<unsigned char>
241  {
242  public:
244  };
245  class VTKRENDERINGOPENGL2_EXPORT ScopedglClearColor : public ScopedValue<std::array<float, 4> >
246  {
247  public:
249  };
250  class VTKRENDERINGOPENGL2_EXPORT ScopedglColorMask
251  : public ScopedValue<std::array<unsigned char, 4> >
252  {
253  public:
255  };
256  class VTKRENDERINGOPENGL2_EXPORT ScopedglScissor : public ScopedValue<std::array<int, 4> >
257  {
258  public:
260  };
261  class VTKRENDERINGOPENGL2_EXPORT ScopedglViewport : public ScopedValue<std::array<int, 4> >
262  {
263  public:
265  };
266  class VTKRENDERINGOPENGL2_EXPORT ScopedglBlendFuncSeparate
267  : public ScopedValue<std::array<unsigned int, 4> >
268  {
269  public:
271  };
272  class VTKRENDERINGOPENGL2_EXPORT ScopedglDepthFunc : public ScopedValue<unsigned int>
273  {
274  public:
276  };
277  class VTKRENDERINGOPENGL2_EXPORT ScopedglActiveTexture : public ScopedValue<unsigned int>
278  {
279  public:
281  };
282 
284  {
285  public:
287  {
288  this->State = state;
289  this->Name = name;
290  unsigned char val;
291  this->State->vtkglGetBooleanv(name, &val);
292  this->Value = val == 1;
293  }
294  ~ScopedglEnableDisable() // restore value
295  {
296  this->State->SetEnumState(this->Name, this->Value);
297  }
298 
299  protected:
301  unsigned int Name;
302  bool Value;
303  };
304 
309 
313  void SetTextureUnitManager(vtkTextureUnitManager* textureUnitManager);
314 
320 
321  // get the shader program cache for this context
322  vtkGetObjectMacro(ShaderCache, vtkOpenGLShaderCache);
323 
324  // get the vbo buffer cache for this context
325  vtkGetObjectMacro(VBOCache, vtkOpenGLVertexBufferObjectCache);
326 
327  // set the VBO Cache to use for this state
328  // this allows two contexts to share VBOs
329  // basically this is OPenGL's support for shared
330  // lists
332 
339  int vtktype, int numComponents, bool needInteger, bool needFloat, bool needSRGB);
340 
341 protected:
342  vtkOpenGLState(); // set initial values
343  ~vtkOpenGLState() override;
344 
345  void BlendFuncSeparate(std::array<unsigned int, 4> val);
346  void ClearColor(std::array<float, 4> val);
347  void ColorMask(std::array<unsigned char, 4> val);
348  void Scissor(std::array<int, 4> val);
349  void Viewport(std::array<int, 4> val);
350 
351  int TextureInternalFormats[VTK_UNICODE_STRING][3][5];
353 
355  std::map<const vtkTextureObject*, int> TextureResourceIds;
356 
361  void CheckState();
362 
363  // framebuffers hold state themselves
364  // specifically they hold their draw and read buffers
365  // and when bound they reinstate those buffers
366  class VTKRENDERINGOPENGL2_EXPORT BufferBindingState
367  {
368  public:
370  // bool operator==(const BufferBindingState& a, const BufferBindingState& b);
371  // either this holds a vtkOpenGLFramebufferObject
373  // or the handle to an unknown OpenGL FO
374  unsigned int Binding;
375  unsigned int ReadBuffer;
376  unsigned int DrawBuffers[10];
377  unsigned int GetBinding();
378  unsigned int GetDrawBuffer(unsigned int);
379  unsigned int GetReadBuffer();
380  };
381  std::list<BufferBindingState> DrawBindings;
382  std::list<BufferBindingState> ReadBindings;
383 
384  class VTKRENDERINGOPENGL2_EXPORT GLState
385  {
386  public:
387  double ClearDepth;
388  unsigned char DepthMask;
389  unsigned int DepthFunc;
390  unsigned int BlendEquationValue1;
391  unsigned int BlendEquationValue2;
392  unsigned int CullFaceMode;
393  unsigned int ActiveTexture;
394 
395  float PointSize;
396  float LineWidth;
397  unsigned int StencilMaskFront;
398  unsigned int StencilMaskBack;
399  std::array<unsigned int, 3> StencilFuncFront;
400  std::array<unsigned int, 3> StencilFuncBack;
401  std::array<unsigned int, 3> StencilOpFront;
402  std::array<unsigned int, 3> StencilOpBack;
403 
408 
409  std::array<float, 4> ClearColor;
410  std::array<unsigned char, 4> ColorMask;
411  std::array<int, 4> Viewport;
412  std::array<int, 4> Scissor;
413  std::array<unsigned int, 4> BlendFunc;
414  bool DepthTest;
415  bool CullFace;
418  bool Blend;
426  GLState() {}
427  };
428 
430 
433 
434 private:
435  vtkOpenGLState(const vtkOpenGLState&) = delete;
436  void operator=(const vtkOpenGLState&) = delete;
437 };
438 
439 #endif
440 
441 // VTK-HeaderTest-Exclude: vtkOpenGLState.h
abstract base class for most VTK objects
Definition: vtkObject.h:63
Internal class which encapsulates OpenGL FramebufferObject.
OpenGL rendering window.
manage Shader Programs within a context
unsigned int GetDrawBuffer(unsigned int)
vtkOpenGLFramebufferObject * Framebuffer
BufferBindingState ReadBinding
BufferBindingState DrawBinding
unsigned int BlendEquationValue1
std::array< unsigned int, 4 > BlendFunc
unsigned int StencilMaskFront
unsigned int StencilMaskBack
std::array< unsigned int, 3 > StencilFuncFront
std::array< unsigned char, 4 > ColorMask
std::array< unsigned int, 3 > StencilFuncBack
unsigned int BlendEquationValue2
std::array< int, 4 > Viewport
std::array< unsigned int, 3 > StencilOpFront
std::array< int, 4 > Scissor
std::array< float, 4 > ClearColor
std::array< unsigned int, 3 > StencilOpBack
ScopedglActiveTexture(vtkOpenGLState *state)
ScopedglBlendFuncSeparate(vtkOpenGLState *state)
ScopedglClearColor(vtkOpenGLState *state)
ScopedglColorMask(vtkOpenGLState *state)
ScopedglDepthFunc(vtkOpenGLState *state)
ScopedglDepthMask(vtkOpenGLState *state)
ScopedglEnableDisable(vtkOpenGLState *state, unsigned int name)
ScopedglScissor(vtkOpenGLState *state)
ScopedglViewport(vtkOpenGLState *state)
OpenGL state storage.
void Scissor(std::array< int, 4 > val)
void vtkglViewport(int x, int y, int width, int height)
void vtkReadBuffer(unsigned int, vtkOpenGLFramebufferObject *)
void vtkglLineWidth(float)
void vtkglGetIntegerv(unsigned int pname, int *params)
bool GetEnumState(unsigned int name)
void vtkglStencilOpSeparate(unsigned int face, unsigned int sfail, unsigned int dpfail, unsigned int dppass)
void vtkDrawBuffers(unsigned int n, unsigned int *, vtkOpenGLFramebufferObject *)
void CheckState()
Check that this OpenGL state has consistent values with the current OpenGL context.
std::list< BufferBindingState > DrawBindings
void ResetGLBlendEquationState()
void ResetGLScissorState()
void SetVBOCache(vtkOpenGLVertexBufferObjectCache *val)
void vtkglActiveTexture(unsigned int)
void vtkBindFramebuffer(unsigned int target, vtkOpenGLFramebufferObject *fo)
void Initialize(vtkOpenGLRenderWindow *)
Initialize OpenGL context using current state.
vtkOpenGLShaderCache * ShaderCache
void ResetGLActiveTexture()
void vtkglStencilFuncSeparate(unsigned int face, unsigned int func, int ref, unsigned int mask)
std::map< const vtkTextureObject *, int > TextureResourceIds
void PopFramebufferBindings()
void ResetEnumState(unsigned int name)
convenience method to reset an enum state from current openGL context
~vtkOpenGLState() override
void ColorMask(std::array< unsigned char, 4 > val)
void PopDrawFramebufferBinding()
void SetTextureUnitManager(vtkTextureUnitManager *textureUnitManager)
Set the texture unit manager.
void vtkglDrawBuffers(unsigned int n, unsigned int *)
void vtkglCullFace(unsigned int val)
void Viewport(std::array< int, 4 > val)
void vtkglReadBuffer(unsigned int)
void vtkglBlendFuncSeparate(unsigned int sfactorRGB, unsigned int dfactorRGB, unsigned int sfactorAlpha, unsigned int dfactorAlpha)
void vtkglBindFramebuffer(unsigned int target, unsigned int fb)
void ResetGLDepthMaskState()
void ActivateTexture(vtkTextureObject *)
Activate a texture unit for this texture.
GLState CurrentState
void vtkglDisable(unsigned int cap)
void vtkglGetDoublev(unsigned int pname, double *params)
void ResetGLDepthFuncState()
void vtkglStencilOp(unsigned int sfail, unsigned int dpfail, unsigned int dppass)
static vtkOpenGLState * New()
void BlendFuncSeparate(std::array< unsigned int, 4 > val)
void vtkglBlendFunc(unsigned int sfactor, unsigned int dfactor)
void vtkglEnable(unsigned int cap)
void PushFramebufferBindings()
Store/Restore the current framebuffer bindings and buffers.
void ResetGLClearDepthState()
int GetDefaultTextureInternalFormat(int vtktype, int numComponents, bool needInteger, bool needFloat, bool needSRGB)
Get a mapping of vtk data types to native texture formats for this window we put this on the RenderWi...
void vtkglStencilMaskSeparate(unsigned int face, unsigned int mask)
void vtkglClearDepth(double depth)
void ResetGLColorMaskState()
void PopReadFramebufferBinding()
void vtkglDrawBuffer(unsigned int)
void InitializeTextureInternalFormats()
void vtkglGetFloatv(unsigned int pname, float *params)
void PushReadFramebufferBinding()
void vtkglScissor(int x, int y, int width, int height)
void VerifyNoActiveTextures()
Check to make sure no textures have been left active.
void vtkglColorMask(unsigned char r, unsigned char g, unsigned char b, unsigned char a)
void vtkglDepthMask(unsigned char flag)
void vtkglPointSize(float)
void vtkglBlendEquationSeparate(unsigned int col, unsigned int alpha)
void vtkglClearColor(float red, float green, float blue, float alpha)
void DeactivateTexture(vtkTextureObject *)
Deactivate a previously activated texture.
std::list< BufferBindingState > ReadBindings
void vtkglDepthFunc(unsigned int val)
void PushDrawFramebufferBinding()
void vtkglClear(unsigned int mask)
void SetEnumState(unsigned int name, bool value)
void vtkglPixelStorei(unsigned int, int)
void ResetGLViewportState()
void ResetGLClearColorState()
void ClearColor(std::array< float, 4 > val)
void vtkglBlendEquation(unsigned int val)
void ResetGLCullFaceState()
void vtkglGetBooleanv(unsigned int pname, unsigned char *params)
vtkTextureUnitManager * GetTextureUnitManager()
Returns its texture unit manager object.
void ResetFramebufferBindings()
vtkOpenGLVertexBufferObjectCache * VBOCache
void ResetGLBlendFuncState()
void vtkglStencilFunc(unsigned int func, int ref, unsigned int mask)
void GetBlendFuncState(int *)
int GetTextureUnitForTexture(vtkTextureObject *)
Get the texture unit for a given texture object.
vtkTextureUnitManager * TextureUnitManager
void vtkglStencilMask(unsigned int mask)
manage vertex buffer objects shared within a context
abstracts an OpenGL texture object.
allocate/free texture units.
@ value
Definition: vtkX3D.h:226
@ alpha
Definition: vtkX3D.h:256
@ height
Definition: vtkX3D.h:260
@ name
Definition: vtkX3D.h:225
boost::graph_traits< vtkGraph * >::vertex_descriptor target(boost::graph_traits< vtkGraph * >::edge_descriptor e, vtkGraph *)
#define VTK_UNICODE_STRING
Definition: vtkType.h:79