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);
119  void vtkglStencilOpSeparate(
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 
125  void vtkBindFramebuffer(unsigned int target, vtkOpenGLFramebufferObject* fo);
126  void vtkDrawBuffers(unsigned int n, unsigned int*, vtkOpenGLFramebufferObject*);
127  void vtkReadBuffer(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  //
137  void ResetGLClearColorState();
138  void ResetGLClearDepthState();
139  void ResetGLDepthFuncState();
140  void ResetGLDepthMaskState();
141  void ResetGLColorMaskState();
142  void ResetGLViewportState();
143  void ResetGLScissorState();
144  void ResetGLBlendFuncState();
145  void ResetGLBlendEquationState();
146  void ResetGLCullFaceState();
147  void ResetGLActiveTexture();
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 
199  void ActivateTexture(vtkTextureObject*);
200 
204  void DeactivateTexture(vtkTextureObject*);
205 
209  int GetTextureUnitForTexture(vtkTextureObject*);
210 
214  void VerifyNoActiveTextures();
215 
217 
221  {
222  this->PushDrawFramebufferBinding();
223  this->PushReadFramebufferBinding();
224  }
225  void PushDrawFramebufferBinding();
226  void PushReadFramebufferBinding();
227 
229  {
230  this->PopReadFramebufferBinding();
231  this->PopDrawFramebufferBinding();
232  }
233  void PopDrawFramebufferBinding();
234  void PopReadFramebufferBinding();
235 
236  void ResetFramebufferBindings();
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
OpenGL rendering window.
std::array< int, 4 > Viewport
abstract base class for most VTK objects
Definition: vtkObject.h:62
BufferBindingState DrawBinding
void ColorMask(std::array< unsigned char, 4 > val)
std::array< float, 4 > ClearColor
manage Shader Programs within a context
std::map< const vtkTextureObject *, int > TextureResourceIds
std::array< unsigned int, 3 > StencilOpFront
std::array< unsigned int, 3 > StencilFuncBack
void PushFramebufferBindings()
Store/Restore the current framebuffer bindings and buffers.
manage vertex buffer objects shared within a context
vtkOpenGLVertexBufferObjectCache * VBOCache
std::array< unsigned int, 3 > StencilOpBack
#define VTK_UNICODE_STRING
Definition: vtkType.h:79
std::array< unsigned char, 4 > ColorMask
std::array< int, 4 > Scissor
void vtkglBlendFunc(unsigned int sfactor, unsigned int dfactor)
GLState CurrentState
void Viewport(std::array< int, 4 > val)
~vtkOpenGLState() override
void InitializeTextureInternalFormats()
unsigned int BlendEquationValue2
unsigned int BlendEquationValue1
void BlendFuncSeparate(std::array< unsigned int, 4 > val)
std::list< BufferBindingState > ReadBindings
vtkOpenGLFramebufferObject * Framebuffer
BufferBindingState ReadBinding
OpenGL state storage.
void PopFramebufferBindings()
Store/Restore the current framebuffer bindings and buffers.
unsigned int StencilMaskBack
boost::graph_traits< vtkGraph * >::vertex_descriptor target(boost::graph_traits< vtkGraph * >::edge_descriptor e, vtkGraph *)
void CheckState()
Check that this OpenGL state has consistent values with the current OpenGL context.
ScopedglEnableDisable(vtkOpenGLState *state, unsigned int name)
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...
vtkTextureUnitManager * GetTextureUnitManager()
Returns its texture unit manager object.
Internal class which encapsulates OpenGL FramebufferObject.
vtkOpenGLShaderCache * ShaderCache
std::list< BufferBindingState > DrawBindings
allocate/free texture units.
abstracts an OpenGL texture object.
unsigned int StencilMaskFront
std::array< unsigned int, 4 > BlendFunc
std::array< unsigned int, 3 > StencilFuncFront
void SetVBOCache(vtkOpenGLVertexBufferObjectCache *val)
vtkTextureUnitManager * TextureUnitManager
void Scissor(std::array< int, 4 > val)
static vtkObject * New()
Create an object with Debug turned off, modified time initialized to zero, and reference counting on.
void SetTextureUnitManager(vtkTextureUnitManager *textureUnitManager)
Set the texture unit manager.
void ClearColor(std::array< float, 4 > val)
void Initialize(vtkOpenGLRenderWindow *)
Initialize OpenGL context using current state.