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 vtkBindFramebuffer(unsigned int target, vtkOpenGLFramebufferObject* fo);
116  void vtkDrawBuffers(unsigned int n, unsigned int*, vtkOpenGLFramebufferObject*);
117  void vtkReadBuffer(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  //
125  void ResetGLClearColorState();
126  void ResetGLClearDepthState();
127  void ResetGLDepthFuncState();
128  void ResetGLDepthMaskState();
129  void ResetGLColorMaskState();
130  void ResetGLViewportState();
131  void ResetGLScissorState();
132  void ResetGLBlendFuncState();
133  void ResetGLBlendEquationState();
134  void ResetGLCullFaceState();
135  void ResetGLActiveTexture();
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 
187  void ActivateTexture(vtkTextureObject*);
188 
192  void DeactivateTexture(vtkTextureObject*);
193 
197  int GetTextureUnitForTexture(vtkTextureObject*);
198 
202  void VerifyNoActiveTextures();
203 
205 
209  {
210  this->PushDrawFramebufferBinding();
211  this->PushReadFramebufferBinding();
212  }
213  void PushDrawFramebufferBinding();
214  void PushReadFramebufferBinding();
215 
217  {
218  this->PopReadFramebufferBinding();
219  this->PopDrawFramebufferBinding();
220  }
221  void PopDrawFramebufferBinding();
222  void PopReadFramebufferBinding();
223 
224  void ResetFramebufferBindings();
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
vtkOpenGLState::GLState::BlendFunc
std::array< unsigned int, 4 > BlendFunc
Definition: vtkOpenGLState.h:386
vtkOpenGLState::ReadBindings
std::list< BufferBindingState > ReadBindings
Definition: vtkOpenGLState.h:370
vtkOpenGLState::InitializeTextureInternalFormats
void InitializeTextureInternalFormats()
vtkOpenGLState::BufferBindingState::Framebuffer
vtkOpenGLFramebufferObject * Framebuffer
Definition: vtkOpenGLState.h:360
vtkOpenGLState::GLState
Definition: vtkOpenGLState.h:372
vtkOpenGLState::ScopedglClearColor
Definition: vtkOpenGLState.h:233
vtkOpenGLState::ScopedValue::Value
T Value
Definition: vtkOpenGLState.h:180
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:403
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:173
vtkOpenGLState::BufferBindingState
Definition: vtkOpenGLState.h:354
vtkOpenGLState::GLState::BlendEquationValue1
unsigned int BlendEquationValue1
Definition: vtkOpenGLState.h:378
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:379
vtkOpenGLState::PushFramebufferBindings
void PushFramebufferBindings()
Store/Restore the current framebuffer bindings and buffers.
Definition: vtkOpenGLState.h:208
vtkOpenGLState::GLState::MinorVersion
int MinorVersion
Definition: vtkOpenGLState.h:395
vtkOpenGLState::ScopedglEnableDisable::Value
bool Value
Definition: vtkOpenGLState.h:290
vtkOpenGLState::GLState::ScissorTest
bool ScissorTest
Definition: vtkOpenGLState.h:389
vtkOpenGLState::GLState::DepthMask
unsigned char DepthMask
Definition: vtkOpenGLState.h:376
vtkOpenGLFramebufferObject
Internal class which encapsulates OpenGL FramebufferObject.
Definition: vtkOpenGLFramebufferObject.h:181
vtkOpenGLState::GLState::MaxTextureSize
int MaxTextureSize
Definition: vtkOpenGLState.h:393
vtkOpenGLState::CurrentState
GLState CurrentState
Definition: vtkOpenGLState.h:401
vtkObject
abstract base class for most VTK objects
Definition: vtkObject.h:62
vtkOpenGLState::ScopedglEnableDisable::State
vtkOpenGLState * State
Definition: vtkOpenGLState.h:288
vtkOpenGLState::ScopedValue
Definition: vtkOpenGLState.h:170
vtkOpenGLState::ScopedglEnableDisable::ScopedglEnableDisable
ScopedglEnableDisable(vtkOpenGLState *state, unsigned int name)
Definition: vtkOpenGLState.h:274
vtkOpenGLState::GLState::CullFace
bool CullFace
Definition: vtkOpenGLState.h:388
vtkOpenGLState::GLState::ActiveTexture
unsigned int ActiveTexture
Definition: vtkOpenGLState.h:381
vtkOpenGLState::TextureUnitManager
vtkTextureUnitManager * TextureUnitManager
Definition: vtkOpenGLState.h:342
vtkOpenGLState::GLState::MajorVersion
int MajorVersion
Definition: vtkOpenGLState.h:394
vtkOpenGLState::ClearColor
void ClearColor(std::array< float, 4 > val)
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:289
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:343
vtkOpenGLState::ShaderCache
vtkOpenGLShaderCache * ShaderCache
Definition: vtkOpenGLState.h:404
vtkTextureUnitManager
allocate/free texture units.
Definition: vtkTextureUnitManager.h:38
vtkOpenGLState::ScopedglActiveTexture
Definition: vtkOpenGLState.h:265
vtkOpenGLState::ScopedglDepthFunc
Definition: vtkOpenGLState.h:260
vtkOpenGLState::ScopedglColorMask
Definition: vtkOpenGLState.h:238
vtkX3D::height
@ height
Definition: vtkX3D.h:260
vtkOpenGLState::GLState::DrawBinding
BufferBindingState DrawBinding
Definition: vtkOpenGLState.h:396
vtkOpenGLState::GLState::DepthTest
bool DepthTest
Definition: vtkOpenGLState.h:387
vtkOpenGLState::DrawBindings
std::list< BufferBindingState > DrawBindings
Definition: vtkOpenGLState.h:369
vtkOpenGLState::GLState::GLState
GLState()
Definition: vtkOpenGLState.h:398
vtkTextureObject
abstracts an OpenGL texture object.
Definition: vtkTextureObject.h:40
vtkOpenGLState::GLState::Viewport
std::array< int, 4 > Viewport
Definition: vtkOpenGLState.h:384
vtkOpenGLState::GLState::Blend
bool Blend
Definition: vtkOpenGLState.h:391
vtkOpenGLState::GLState::DepthFunc
unsigned int DepthFunc
Definition: vtkOpenGLState.h:377
vtkOpenGLState::GLState::Scissor
std::array< int, 4 > Scissor
Definition: vtkOpenGLState.h:385
vtkOpenGLState::ScopedglEnableDisable::~ScopedglEnableDisable
~ScopedglEnableDisable()
Definition: vtkOpenGLState.h:282
vtkOpenGLShaderCache
manage Shader Programs within a context
Definition: vtkOpenGLShaderCache.h:35
vtkOpenGLState::vtkOpenGLState
vtkOpenGLState()
vtkOpenGLState::BufferBindingState::ReadBuffer
unsigned int ReadBuffer
Definition: vtkOpenGLState.h:363
vtkX3D::name
@ name
Definition: vtkX3D.h:225
vtkObject.h
vtkOpenGLState::PopFramebufferBindings
void PopFramebufferBindings()
Definition: vtkOpenGLState.h:216
vtkOpenGLState::ScopedglViewport
Definition: vtkOpenGLState.h:249
vtkOpenGLState::ScopedglDepthMask
Definition: vtkOpenGLState.h:228
vtkOpenGLState::GLState::MultiSample
bool MultiSample
Definition: vtkOpenGLState.h:392
vtkOpenGLState::Viewport
void Viewport(std::array< int, 4 > val)
vtkOpenGLState::GLState::ReadBinding
BufferBindingState ReadBinding
Definition: vtkOpenGLState.h:397
vtkOpenGLState::SetVBOCache
void SetVBOCache(vtkOpenGLVertexBufferObjectCache *val)
vtkOpenGLState::ScopedglBlendFuncSeparate
Definition: vtkOpenGLState.h:254
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:244
vtkOpenGLRenderWindow
OpenGL rendering window.
Definition: vtkOpenGLRenderWindow.h:51
vtkOpenGLState::GLState::ClearDepth
double ClearDepth
Definition: vtkOpenGLState.h:375
vtkOpenGLState::ScopedglEnableDisable
Definition: vtkOpenGLState.h:271
vtkOpenGLState::ScopedValue::State
vtkOpenGLState * State
Definition: vtkOpenGLState.h:179
vtkOpenGLState::GLState::ColorMask
std::array< unsigned char, 4 > ColorMask
Definition: vtkOpenGLState.h:383
vtkOpenGLState::GLState::StencilTest
bool StencilTest
Definition: vtkOpenGLState.h:390
vtkOpenGLState
OpenGL state storage.
Definition: vtkOpenGLState.h:79
vtkOpenGLState::GLState::ClearColor
std::array< float, 4 > ClearColor
Definition: vtkOpenGLState.h:382
vtkOpenGLState::BufferBindingState::Binding
unsigned int Binding
Definition: vtkOpenGLState.h:362
vtkOpenGLState::GLState::CullFaceMode
unsigned int CullFaceMode
Definition: vtkOpenGLState.h:380
vtkOpenGLState::Scissor
void Scissor(std::array< int, 4 > val)
vtkOpenGLState::BlendFuncSeparate
void BlendFuncSeparate(std::array< unsigned int, 4 > val)