VTK  9.0.3
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 
116  void vtkDrawBuffers(unsigned int n, unsigned int*, vtkOpenGLFramebufferObject*);
119 
121  // Methods to reset the state to the current OpenGL context value.
122  // These methods are useful when interfacing with third party code
123  // that may have changed the opengl state.
124  //
137 
139  // OpenGL functions that we provide an API for even though they may
140  // not hold any state.
141  void vtkglClear(unsigned int mask);
143 
145  // Get methods that can be used to query state if the state is not cached
146  // they fall through and call the underlying opengl functions
147  void vtkglGetBooleanv(unsigned int pname, unsigned char* params);
148  void vtkglGetIntegerv(unsigned int pname, int* params);
149  void vtkglGetDoublev(unsigned int pname, double* params);
150  void vtkglGetFloatv(unsigned int pname, float* params);
152 
153  // convenience to get all 4 values at once
154  void GetBlendFuncState(int*);
155 
156  // convenience to return a bool
157  // as opposed to a unsigned char
158  bool GetEnumState(unsigned int name);
159 
160  // convenience method to set a enum (glEnable/glDisable)
161  void SetEnumState(unsigned int name, bool value);
162 
166  void ResetEnumState(unsigned int name);
167 
168  // superclass for Scoped subclasses
169  template <typename T>
170  class VTKRENDERINGOPENGL2_EXPORT ScopedValue
171  {
172  public:
173  ~ScopedValue() // restore value
174  {
175  ((*this->State).*(this->Method))(this->Value);
176  }
177 
178  protected:
180  T Value;
181  void (vtkOpenGLState::*Method)(T);
182  };
183 
188 
193 
198 
203 
205 
209  {
210  this->PushDrawFramebufferBinding();
211  this->PushReadFramebufferBinding();
212  }
215 
217  {
218  this->PopReadFramebufferBinding();
219  this->PopDrawFramebufferBinding();
220  }
223 
226 
227  // Scoped classes you can use to save state
228  class VTKRENDERINGOPENGL2_EXPORT ScopedglDepthMask : public ScopedValue<unsigned char>
229  {
230  public:
232  };
233  class VTKRENDERINGOPENGL2_EXPORT ScopedglClearColor : public ScopedValue<std::array<float, 4> >
234  {
235  public:
237  };
238  class VTKRENDERINGOPENGL2_EXPORT ScopedglColorMask
239  : public ScopedValue<std::array<unsigned char, 4> >
240  {
241  public:
243  };
244  class VTKRENDERINGOPENGL2_EXPORT ScopedglScissor : public ScopedValue<std::array<int, 4> >
245  {
246  public:
248  };
249  class VTKRENDERINGOPENGL2_EXPORT ScopedglViewport : public ScopedValue<std::array<int, 4> >
250  {
251  public:
253  };
254  class VTKRENDERINGOPENGL2_EXPORT ScopedglBlendFuncSeparate
255  : public ScopedValue<std::array<unsigned int, 4> >
256  {
257  public:
259  };
260  class VTKRENDERINGOPENGL2_EXPORT ScopedglDepthFunc : public ScopedValue<unsigned int>
261  {
262  public:
264  };
265  class VTKRENDERINGOPENGL2_EXPORT ScopedglActiveTexture : public ScopedValue<unsigned int>
266  {
267  public:
269  };
270 
272  {
273  public:
275  {
276  this->State = state;
277  this->Name = name;
278  unsigned char val;
279  this->State->vtkglGetBooleanv(name, &val);
280  this->Value = val == 1;
281  }
282  ~ScopedglEnableDisable() // restore value
283  {
284  this->State->SetEnumState(this->Name, this->Value);
285  }
286 
287  protected:
289  unsigned int Name;
290  bool Value;
291  };
292 
297 
301  void SetTextureUnitManager(vtkTextureUnitManager* textureUnitManager);
302 
308 
309  // get the shader program cache for this context
310  vtkGetObjectMacro(ShaderCache, vtkOpenGLShaderCache);
311 
312  // get the vbo buffer cache for this context
313  vtkGetObjectMacro(VBOCache, vtkOpenGLVertexBufferObjectCache);
314 
315  // set the VBO Cache to use for this state
316  // this allows two contexts to share VBOs
317  // basically this is OPenGL's support for shared
318  // lists
320 
327  int vtktype, int numComponents, bool needInteger, bool needFloat, bool needSRGB);
328 
329 protected:
330  vtkOpenGLState(); // set initial values
331  ~vtkOpenGLState() override;
332 
333  void BlendFuncSeparate(std::array<unsigned int, 4> val);
334  void ClearColor(std::array<float, 4> val);
335  void ColorMask(std::array<unsigned char, 4> val);
336  void Scissor(std::array<int, 4> val);
337  void Viewport(std::array<int, 4> val);
338 
339  int TextureInternalFormats[VTK_UNICODE_STRING][3][5];
341 
343  std::map<const vtkTextureObject*, int> TextureResourceIds;
344 
349  void CheckState();
350 
351  // framebuffers hold state themselves
352  // specifically they hold their draw and read buffers
353  // and when bound they reinstate those buffers
354  class VTKRENDERINGOPENGL2_EXPORT BufferBindingState
355  {
356  public:
358  // bool operator==(const BufferBindingState& a, const BufferBindingState& b);
359  // either this holds a vtkOpenGLFramebufferObject
361  // or the handle to an unknown OpenGL FO
362  unsigned int Binding;
363  unsigned int ReadBuffer;
364  unsigned int DrawBuffers[10];
365  unsigned int GetBinding();
366  unsigned int GetDrawBuffer(unsigned int);
367  unsigned int GetReadBuffer();
368  };
369  std::list<BufferBindingState> DrawBindings;
370  std::list<BufferBindingState> ReadBindings;
371 
372  class VTKRENDERINGOPENGL2_EXPORT GLState
373  {
374  public:
375  double ClearDepth;
376  unsigned char DepthMask;
377  unsigned int DepthFunc;
378  unsigned int BlendEquationValue1;
379  unsigned int BlendEquationValue2;
380  unsigned int CullFaceMode;
381  unsigned int ActiveTexture;
382  std::array<float, 4> ClearColor;
383  std::array<unsigned char, 4> ColorMask;
384  std::array<int, 4> Viewport;
385  std::array<int, 4> Scissor;
386  std::array<unsigned int, 4> BlendFunc;
387  bool DepthTest;
388  bool CullFace;
391  bool Blend;
398  GLState() {}
399  };
400 
402 
405 
406 private:
407  vtkOpenGLState(const vtkOpenGLState&) = delete;
408  void operator=(const vtkOpenGLState&) = delete;
409 };
410 
411 #endif
412 
413 // VTK-HeaderTest-Exclude: vtkOpenGLState.h
abstract base class for most VTK objects
Definition: vtkObject.h:54
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
std::array< unsigned char, 4 > ColorMask
unsigned int BlendEquationValue2
std::array< int, 4 > Viewport
std::array< int, 4 > Scissor
std::array< float, 4 > ClearColor
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 vtkglGetIntegerv(unsigned int pname, int *params)
bool GetEnumState(unsigned int name)
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()
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()
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 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 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 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 GetBlendFuncState(int *)
int GetTextureUnitForTexture(vtkTextureObject *)
Get the texture unit for a given texture object.
vtkTextureUnitManager * TextureUnitManager
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