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
vtkOpenGLState::GLState::BlendFunc
std::array< unsigned int, 4 > BlendFunc
Definition: vtkOpenGLState.h:413
vtkOpenGLState::ReadBindings
std::list< BufferBindingState > ReadBindings
Definition: vtkOpenGLState.h:382
vtkOpenGLState::InitializeTextureInternalFormats
void InitializeTextureInternalFormats()
vtkOpenGLState::BufferBindingState::Framebuffer
vtkOpenGLFramebufferObject * Framebuffer
Definition: vtkOpenGLState.h:372
vtkOpenGLState::GLState
Definition: vtkOpenGLState.h:384
vtkOpenGLState::ScopedglClearColor
Definition: vtkOpenGLState.h:245
vtkOpenGLState::ScopedValue::Value
T Value
Definition: vtkOpenGLState.h:192
vtkOpenGLState::ColorMask
void ColorMask(std::array< unsigned char, 4 > val)
vtkX3D::alpha
@ alpha
Definition: vtkX3D.h:256
vtkOpenGLState::~vtkOpenGLState
~vtkOpenGLState() override
vtkOpenGLState::VBOCache
vtkOpenGLVertexBufferObjectCache * VBOCache
Definition: vtkOpenGLState.h:431
vtkOpenGLState::GetDefaultTextureInternalFormat
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...
vtkX3D::value
@ value
Definition: vtkX3D.h:226
vtkOpenGLState::GetTextureUnitManager
vtkTextureUnitManager * GetTextureUnitManager()
Returns its texture unit manager object.
vtkOpenGLState::ScopedValue::~ScopedValue
~ScopedValue()
Definition: vtkOpenGLState.h:185
vtkOpenGLState::BufferBindingState
Definition: vtkOpenGLState.h:366
vtkOpenGLState::GLState::UnpackRowLength
int UnpackRowLength
Definition: vtkOpenGLState.h:406
vtkOpenGLState::GLState::BlendEquationValue1
unsigned int BlendEquationValue1
Definition: vtkOpenGLState.h:390
VTK_UNICODE_STRING
#define VTK_UNICODE_STRING
Definition: vtkType.h:79
vtkObject::New
static vtkObject * New()
Create an object with Debug turned off, modified time initialized to zero, and reference counting on.
vtkOpenGLState::GLState::BlendEquationValue2
unsigned int BlendEquationValue2
Definition: vtkOpenGLState.h:391
vtkOpenGLState::PushFramebufferBindings
void PushFramebufferBindings()
Store/Restore the current framebuffer bindings and buffers.
Definition: vtkOpenGLState.h:220
vtkOpenGLState::GLState::UnpackImageHeight
int UnpackImageHeight
Definition: vtkOpenGLState.h:407
vtkOpenGLState::GLState::MinorVersion
int MinorVersion
Definition: vtkOpenGLState.h:423
vtkOpenGLState::ScopedglEnableDisable::Value
bool Value
Definition: vtkOpenGLState.h:302
vtkOpenGLState::GLState::StencilMaskFront
unsigned int StencilMaskFront
Definition: vtkOpenGLState.h:397
vtkOpenGLState::GLState::ScissorTest
bool ScissorTest
Definition: vtkOpenGLState.h:416
vtkOpenGLState::GLState::DepthMask
unsigned char DepthMask
Definition: vtkOpenGLState.h:388
vtkOpenGLFramebufferObject
Internal class which encapsulates OpenGL FramebufferObject.
Definition: vtkOpenGLFramebufferObject.h:181
vtkOpenGLState::GLState::MaxTextureSize
int MaxTextureSize
Definition: vtkOpenGLState.h:421
vtkOpenGLState::GLState::StencilFuncFront
std::array< unsigned int, 3 > StencilFuncFront
Definition: vtkOpenGLState.h:399
vtkOpenGLState::CurrentState
GLState CurrentState
Definition: vtkOpenGLState.h:429
vtkObject
abstract base class for most VTK objects
Definition: vtkObject.h:62
vtkOpenGLState::ScopedglEnableDisable::State
vtkOpenGLState * State
Definition: vtkOpenGLState.h:300
vtkOpenGLState::ScopedValue
Definition: vtkOpenGLState.h:182
vtkOpenGLState::ScopedglEnableDisable::ScopedglEnableDisable
ScopedglEnableDisable(vtkOpenGLState *state, unsigned int name)
Definition: vtkOpenGLState.h:286
vtkOpenGLState::GLState::CullFace
bool CullFace
Definition: vtkOpenGLState.h:415
vtkOpenGLState::GLState::UnpackAlignment
int UnpackAlignment
Definition: vtkOpenGLState.h:405
vtkOpenGLState::GLState::ActiveTexture
unsigned int ActiveTexture
Definition: vtkOpenGLState.h:393
vtkOpenGLState::TextureUnitManager
vtkTextureUnitManager * TextureUnitManager
Definition: vtkOpenGLState.h:354
vtkOpenGLState::GLState::MajorVersion
int MajorVersion
Definition: vtkOpenGLState.h:422
vtkOpenGLState::ClearColor
void ClearColor(std::array< float, 4 > val)
vtkOpenGLState::GLState::LineSmooth
bool LineSmooth
Definition: vtkOpenGLState.h:420
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::SetTextureUnitManager
void SetTextureUnitManager(vtkTextureUnitManager *textureUnitManager)
Set the texture unit manager.
vtkOpenGLState::ScopedglEnableDisable::Name
unsigned int Name
Definition: vtkOpenGLState.h:301
target
boost::graph_traits< vtkGraph * >::vertex_descriptor target(boost::graph_traits< vtkGraph * >::edge_descriptor e, vtkGraph *)
Definition: vtkBoostGraphAdapter.h:965
vtkOpenGLState::TextureResourceIds
std::map< const vtkTextureObject *, int > TextureResourceIds
Definition: vtkOpenGLState.h:355
vtkOpenGLState::ShaderCache
vtkOpenGLShaderCache * ShaderCache
Definition: vtkOpenGLState.h:432
vtkTextureUnitManager
allocate/free texture units.
Definition: vtkTextureUnitManager.h:38
vtkOpenGLState::ScopedglActiveTexture
Definition: vtkOpenGLState.h:277
vtkOpenGLState::ScopedglDepthFunc
Definition: vtkOpenGLState.h:272
vtkOpenGLState::GLState::PackAlignment
int PackAlignment
Definition: vtkOpenGLState.h:404
vtkOpenGLState::ScopedglColorMask
Definition: vtkOpenGLState.h:250
vtkX3D::height
@ height
Definition: vtkX3D.h:260
vtkOpenGLState::GLState::DrawBinding
BufferBindingState DrawBinding
Definition: vtkOpenGLState.h:424
vtkOpenGLState::GLState::DepthTest
bool DepthTest
Definition: vtkOpenGLState.h:414
vtkOpenGLState::GLState::StencilFuncBack
std::array< unsigned int, 3 > StencilFuncBack
Definition: vtkOpenGLState.h:400
vtkOpenGLState::DrawBindings
std::list< BufferBindingState > DrawBindings
Definition: vtkOpenGLState.h:381
vtkOpenGLState::GLState::GLState
GLState()
Definition: vtkOpenGLState.h:426
vtkTextureObject
abstracts an OpenGL texture object.
Definition: vtkTextureObject.h:40
vtkOpenGLState::GLState::Viewport
std::array< int, 4 > Viewport
Definition: vtkOpenGLState.h:411
vtkOpenGLState::GLState::Blend
bool Blend
Definition: vtkOpenGLState.h:418
vtkOpenGLState::GLState::DepthFunc
unsigned int DepthFunc
Definition: vtkOpenGLState.h:389
vtkOpenGLState::GLState::Scissor
std::array< int, 4 > Scissor
Definition: vtkOpenGLState.h:412
vtkOpenGLState::ScopedglEnableDisable::~ScopedglEnableDisable
~ScopedglEnableDisable()
Definition: vtkOpenGLState.h:294
vtkOpenGLShaderCache
manage Shader Programs within a context
Definition: vtkOpenGLShaderCache.h:35
vtkOpenGLState::vtkOpenGLState
vtkOpenGLState()
vtkOpenGLState::BufferBindingState::ReadBuffer
unsigned int ReadBuffer
Definition: vtkOpenGLState.h:375
vtkX3D::name
@ name
Definition: vtkX3D.h:225
vtkObject.h
vtkOpenGLState::PopFramebufferBindings
void PopFramebufferBindings()
Definition: vtkOpenGLState.h:228
vtkOpenGLState::GLState::PointSize
float PointSize
Definition: vtkOpenGLState.h:395
vtkOpenGLState::ScopedglViewport
Definition: vtkOpenGLState.h:261
vtkOpenGLState::GLState::LineWidth
float LineWidth
Definition: vtkOpenGLState.h:396
vtkOpenGLState::ScopedglDepthMask
Definition: vtkOpenGLState.h:240
vtkOpenGLState::GLState::StencilOpFront
std::array< unsigned int, 3 > StencilOpFront
Definition: vtkOpenGLState.h:401
vtkOpenGLState::GLState::MultiSample
bool MultiSample
Definition: vtkOpenGLState.h:419
vtkOpenGLState::Viewport
void Viewport(std::array< int, 4 > val)
vtkOpenGLState::GLState::ReadBinding
BufferBindingState ReadBinding
Definition: vtkOpenGLState.h:425
vtkOpenGLState::GLState::StencilMaskBack
unsigned int StencilMaskBack
Definition: vtkOpenGLState.h:398
vtkOpenGLState::SetVBOCache
void SetVBOCache(vtkOpenGLVertexBufferObjectCache *val)
vtkOpenGLState::GLState::StencilOpBack
std::array< unsigned int, 3 > StencilOpBack
Definition: vtkOpenGLState.h:402
vtkOpenGLState::ScopedglBlendFuncSeparate
Definition: vtkOpenGLState.h:266
vtkOpenGLVertexBufferObjectCache
manage vertex buffer objects shared within a context
Definition: vtkOpenGLVertexBufferObjectCache.h:38
vtkOpenGLState::vtkglBlendFunc
void vtkglBlendFunc(unsigned int sfactor, unsigned int dfactor)
Definition: vtkOpenGLState.h:99
vtkOpenGLState::ScopedglScissor
Definition: vtkOpenGLState.h:256
vtkOpenGLRenderWindow
OpenGL rendering window.
Definition: vtkOpenGLRenderWindow.h:51
vtkOpenGLState::GLState::ClearDepth
double ClearDepth
Definition: vtkOpenGLState.h:387
vtkOpenGLState::ScopedglEnableDisable
Definition: vtkOpenGLState.h:283
vtkOpenGLState::ScopedValue::State
vtkOpenGLState * State
Definition: vtkOpenGLState.h:191
vtkOpenGLState::GLState::ColorMask
std::array< unsigned char, 4 > ColorMask
Definition: vtkOpenGLState.h:410
vtkOpenGLState::GLState::StencilTest
bool StencilTest
Definition: vtkOpenGLState.h:417
vtkOpenGLState
OpenGL state storage.
Definition: vtkOpenGLState.h:79
vtkOpenGLState::GLState::ClearColor
std::array< float, 4 > ClearColor
Definition: vtkOpenGLState.h:409
vtkOpenGLState::BufferBindingState::Binding
unsigned int Binding
Definition: vtkOpenGLState.h:374
vtkOpenGLState::GLState::CullFaceMode
unsigned int CullFaceMode
Definition: vtkOpenGLState.h:392
vtkOpenGLState::Scissor
void Scissor(std::array< int, 4 > val)
vtkOpenGLState::BlendFuncSeparate
void BlendFuncSeparate(std::array< unsigned int, 4 > val)