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
OpenGL rendering window.
std::array< int, 4 > Viewport
boost::graph_traits< vtkGraph * >::vertex_descriptor target(boost::graph_traits< vtkGraph *>::edge_descriptor e, vtkGraph *)
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
void PushFramebufferBindings()
Store/Restore the current framebuffer bindings and buffers.
manage vertex buffer objects shared within a context
vtkOpenGLVertexBufferObjectCache * VBOCache
#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.
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.
std::array< unsigned int, 4 > BlendFunc
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.