VTK  9.1.0
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#include <stack> // for ivar
72#include <string> // for ivar
73
80
81class VTKRENDERINGOPENGL2_EXPORT vtkOpenGLState : public vtkObject
82{
83public:
85 vtkTypeMacro(vtkOpenGLState, vtkObject);
86 void PrintSelf(ostream& os, vtkIndent indent) override;
87
89 // cached OpenGL methods. By calling these the context will check
90 // the current value prior to making the OpenGL call. This can reduce
91 // the burden on the driver.
92 //
93 void vtkglClearColor(float red, float green, float blue, float alpha);
94 void vtkglClearDepth(double depth);
95 void vtkglDepthFunc(unsigned int val);
96 void vtkglDepthMask(unsigned char flag);
97 void vtkglColorMask(unsigned char r, unsigned char g, unsigned char b, unsigned char a);
98 void vtkglViewport(int x, int y, int width, int height);
99 void vtkglScissor(int x, int y, int width, int height);
100 void vtkglEnable(unsigned int cap);
101 void vtkglDisable(unsigned int cap);
102 void vtkglBlendFunc(unsigned int sfactor, unsigned int dfactor)
103 {
104 this->vtkglBlendFuncSeparate(sfactor, dfactor, sfactor, dfactor);
105 }
106 void vtkglBlendFuncSeparate(unsigned int sfactorRGB, unsigned int dfactorRGB,
107 unsigned int sfactorAlpha, unsigned int dfactorAlpha);
108 void vtkglBlendEquation(unsigned int val);
109 void vtkglBlendEquationSeparate(unsigned int col, unsigned int alpha);
110 void vtkglCullFace(unsigned int val);
111 void vtkglActiveTexture(unsigned int);
112
113 void vtkglBindFramebuffer(unsigned int target, unsigned int fb);
114 void vtkglDrawBuffer(unsigned int);
115 void vtkglDrawBuffers(unsigned int n, unsigned int*);
116 void vtkglReadBuffer(unsigned int);
117
118 void vtkglPointSize(float);
119 void vtkglLineWidth(float);
120 void vtkglStencilMaskSeparate(unsigned int face, unsigned int mask);
121 void vtkglStencilMask(unsigned int mask);
123 unsigned int face, unsigned int sfail, unsigned int dpfail, unsigned int dppass);
124 void vtkglStencilOp(unsigned int sfail, unsigned int dpfail, unsigned int dppass);
125 void vtkglStencilFuncSeparate(unsigned int face, unsigned int func, int ref, unsigned int mask);
126 void vtkglStencilFunc(unsigned int func, int ref, unsigned int mask);
127
129 void vtkDrawBuffers(unsigned int n, unsigned int*, vtkOpenGLFramebufferObject*);
131
132 void vtkglPixelStorei(unsigned int, int);
134
136 // Methods to reset the state to the current OpenGL context value.
137 // These methods are useful when interfacing with third party code
138 // that may have changed the opengl state.
139 //
152
154 // OpenGL functions that we provide an API for even though they may
155 // not hold any state.
156 void vtkglClear(unsigned int mask);
158
160 // Get methods that can be used to query state if the state is not cached
161 // they fall through and call the underlying opengl functions
162 void vtkglGetBooleanv(unsigned int pname, unsigned char* params);
163 void vtkglGetIntegerv(unsigned int pname, int* params);
164 void vtkglGetDoublev(unsigned int pname, double* params);
165 void vtkglGetFloatv(unsigned int pname, float* params);
167
168 // convenience to get all 4 values at once
170
171 // convenience to return a bool
172 // as opposed to a unsigned char
173 bool GetEnumState(unsigned int name);
174
175 // convenience method to set a enum (glEnable/glDisable)
176 void SetEnumState(unsigned int name, bool value);
177
181 void ResetEnumState(unsigned int name);
182
183 // superclass for Scoped subclasses
184 template <typename T>
185 class VTKRENDERINGOPENGL2_EXPORT ScopedValue
186 {
187 public:
188 ~ScopedValue() // restore value
189 {
190 ((*this->State).*(this->Method))(this->Value);
191 }
192
193 protected:
196 void (vtkOpenGLState::*Method)(T);
197 };
198
203
208
213
218
220
224 {
225 this->PushDrawFramebufferBinding();
226 this->PushReadFramebufferBinding();
227 }
230
232 {
233 this->PopReadFramebufferBinding();
234 this->PopDrawFramebufferBinding();
235 }
238
241
242 // Scoped classes you can use to save state
243 class VTKRENDERINGOPENGL2_EXPORT ScopedglDepthMask : public ScopedValue<unsigned char>
244 {
245 public:
247 };
248 class VTKRENDERINGOPENGL2_EXPORT ScopedglClearColor : public ScopedValue<std::array<float, 4>>
249 {
250 public:
252 };
253 class VTKRENDERINGOPENGL2_EXPORT ScopedglColorMask
254 : public ScopedValue<std::array<unsigned char, 4>>
255 {
256 public:
258 };
259 class VTKRENDERINGOPENGL2_EXPORT ScopedglScissor : public ScopedValue<std::array<int, 4>>
260 {
261 public:
263 };
264 class VTKRENDERINGOPENGL2_EXPORT ScopedglViewport : public ScopedValue<std::array<int, 4>>
265 {
266 public:
268 };
269 class VTKRENDERINGOPENGL2_EXPORT ScopedglBlendFuncSeparate
270 : public ScopedValue<std::array<unsigned int, 4>>
271 {
272 public:
274 };
275 class VTKRENDERINGOPENGL2_EXPORT ScopedglDepthFunc : public ScopedValue<unsigned int>
276 {
277 public:
279 };
280 class VTKRENDERINGOPENGL2_EXPORT ScopedglActiveTexture : public ScopedValue<unsigned int>
281 {
282 public:
284 };
285
287 {
288 public:
289 ScopedglEnableDisable(vtkOpenGLState* state, unsigned int name)
290 {
291 this->State = state;
292 this->Name = name;
293 unsigned char val;
294 this->State->vtkglGetBooleanv(name, &val);
295 this->Value = val == 1;
296 }
297 ~ScopedglEnableDisable() // restore value
298 {
299 this->State->SetEnumState(this->Name, this->Value);
300 }
301
302 protected:
304 unsigned int Name;
305 bool Value;
306 };
307
312
317
323
324 // get the shader program cache for this context
325 vtkGetObjectMacro(ShaderCache, vtkOpenGLShaderCache);
326
327 // get the vbo buffer cache for this context
328 vtkGetObjectMacro(VBOCache, vtkOpenGLVertexBufferObjectCache);
329
330 // set the VBO Cache to use for this state
331 // this allows two contexts to share VBOs
332 // basically this is OPenGL's support for shared
333 // lists
335
342 int vtktype, int numComponents, bool needInteger, bool needFloat, bool needSRGB);
343
347 void GetCurrentDrawFramebufferState(unsigned int& drawBinding, unsigned int& drawBuffer);
348
353 void vtkglBlitFramebuffer(int, int, int, int, int, int, int, int, unsigned int, unsigned int);
354
369 void Reset();
370
376 void Push();
377
382 void Pop();
383
387 std::string const& GetVersion() { return this->Version; }
388
392 std::string const& GetVendor() { return this->Vendor; }
393
398 std::string const& GetRenderer() { return this->Renderer; }
399
400protected:
401 vtkOpenGLState(); // set initial values
402 ~vtkOpenGLState() override;
403
404 void BlendFuncSeparate(std::array<unsigned int, 4> val);
405 void ClearColor(std::array<float, 4> val);
406 void ColorMask(std::array<unsigned char, 4> val);
407 void Scissor(std::array<int, 4> val);
408 void Viewport(std::array<int, 4> val);
409
410 int TextureInternalFormats[VTK_UNICODE_STRING][3][5];
412
414 std::map<const vtkTextureObject*, int> TextureResourceIds;
415
421
422 // framebuffers hold state themselves
423 // specifically they hold their draw and read buffers
424 // and when bound they reinstate those buffers
425 class VTKRENDERINGOPENGL2_EXPORT BufferBindingState
426 {
427 public:
429 unsigned int Binding;
430 unsigned int ReadBuffer;
431 unsigned int DrawBuffers[10];
432 unsigned int GetBinding();
433 unsigned int GetDrawBuffer(unsigned int);
434 unsigned int GetReadBuffer();
435 };
436 std::list<BufferBindingState> DrawBindings;
437 std::list<BufferBindingState> ReadBindings;
438
439 // static opengl properties
443 std::string Vendor;
444 std::string Renderer;
445 std::string Version;
446
447 class VTKRENDERINGOPENGL2_EXPORT GLState
448 {
449 public:
451 unsigned char DepthMask;
452 unsigned int DepthFunc;
455 unsigned int CullFaceMode;
456 unsigned int ActiveTexture;
457
460 unsigned int StencilMaskFront;
461 unsigned int StencilMaskBack;
462 std::array<unsigned int, 3> StencilFuncFront;
463 std::array<unsigned int, 3> StencilFuncBack;
464 std::array<unsigned int, 3> StencilOpFront;
465 std::array<unsigned int, 3> StencilOpBack;
466
471
472 std::array<float, 4> ClearColor;
473 std::array<unsigned char, 4> ColorMask;
474 std::array<int, 4> Viewport;
475 std::array<int, 4> Scissor;
476 std::array<unsigned int, 4> BlendFunc;
481 bool Blend;
491 GLState() = default;
492 };
493
494 std::stack<GLState> Stack;
495
498
499private:
500 vtkOpenGLState(const vtkOpenGLState&) = delete;
501 void operator=(const vtkOpenGLState&) = delete;
502};
503
504#endif
a simple class to control print indentation
Definition: vtkIndent.h:113
abstract base class for most VTK objects
Definition: vtkObject.h:82
Internal class which encapsulates OpenGL FramebufferObject.
OpenGL rendering window.
manage Shader Programs within a context
unsigned int GetDrawBuffer(unsigned int)
BufferBindingState ReadBinding
BufferBindingState DrawBinding
unsigned int BlendEquationValue1
std::array< unsigned int, 4 > BlendFunc
unsigned int StencilMaskFront
unsigned int StencilMaskBack
std::array< unsigned int, 3 > StencilFuncFront
std::array< unsigned char, 4 > ColorMask
std::array< unsigned int, 3 > StencilFuncBack
unsigned int BlendEquationValue2
std::array< int, 4 > Viewport
std::array< unsigned int, 3 > StencilOpFront
std::array< int, 4 > Scissor
std::array< float, 4 > ClearColor
std::array< unsigned int, 3 > StencilOpBack
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 vtkglLineWidth(float)
void vtkglGetIntegerv(unsigned int pname, int *params)
bool GetEnumState(unsigned int name)
void vtkglStencilOpSeparate(unsigned int face, unsigned int sfail, unsigned int dpfail, unsigned int dppass)
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)
std::string const & GetRenderer()
Return the opengl renderer for this context.
void vtkBindFramebuffer(unsigned int target, vtkOpenGLFramebufferObject *fo)
void Initialize(vtkOpenGLRenderWindow *)
Initialize OpenGL context using current state.
vtkOpenGLShaderCache * ShaderCache
void ResetGLActiveTexture()
void vtkglStencilFuncSeparate(unsigned int face, unsigned int func, int ref, unsigned int mask)
std::map< const vtkTextureObject *, int > TextureResourceIds
void PopFramebufferBindings()
Store/Restore the current framebuffer bindings and buffers.
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()
Store/Restore the current framebuffer bindings and buffers.
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.
void Push()
Push all the recorded state onto the stack.
void vtkglDisable(unsigned int cap)
static vtkOpenGLState * New()
void vtkglGetDoublev(unsigned int pname, double *params)
std::string Version
void ResetGLDepthFuncState()
void vtkglStencilOp(unsigned int sfail, unsigned int dpfail, unsigned int dppass)
void Pop()
Pop the state stack to restore a previous state.
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 Reset()
Record the OpenGL state into this class.
std::stack< GLState > Stack
void vtkglStencilMaskSeparate(unsigned int face, unsigned int mask)
void vtkglClearDepth(double depth)
void ResetGLColorMaskState()
void PopReadFramebufferBinding()
Store/Restore the current framebuffer bindings and buffers.
void vtkglDrawBuffer(unsigned int)
void InitializeTextureInternalFormats()
std::string const & GetVersion()
Return the opengl version for this context.
std::string const & GetVendor()
Return the opengl vendor for this context.
void vtkglGetFloatv(unsigned int pname, float *params)
void GetCurrentDrawFramebufferState(unsigned int &drawBinding, unsigned int &drawBuffer)
Get the current stored state of the draw buffer and binding.
void vtkglBlitFramebuffer(int, int, int, int, int, int, int, int, unsigned int, unsigned int)
Perform a blit but handle some driver bugs safely.
void PushReadFramebufferBinding()
Store/Restore the current framebuffer bindings and buffers.
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)
vtkTextureUnitManager * GetTextureUnitManager()
Returns its texture unit manager object.
void vtkglPointSize(float)
void vtkglBlendEquationSeparate(unsigned int col, unsigned int alpha)
void vtkglClearColor(float red, float green, float blue, float alpha)
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
void DeactivateTexture(vtkTextureObject *)
Deactivate a previously activated texture.
std::list< BufferBindingState > ReadBindings
std::string Vendor
std::string Renderer
void vtkglDepthFunc(unsigned int val)
void PushDrawFramebufferBinding()
Store/Restore the current framebuffer bindings and buffers.
void vtkglClear(unsigned int mask)
void SetEnumState(unsigned int name, bool value)
void vtkglPixelStorei(unsigned int, int)
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)
void ResetFramebufferBindings()
Store/Restore the current framebuffer bindings and buffers.
vtkOpenGLVertexBufferObjectCache * VBOCache
void ResetGLBlendFuncState()
void vtkglStencilFunc(unsigned int func, int ref, unsigned int mask)
void GetBlendFuncState(int *)
int GetTextureUnitForTexture(vtkTextureObject *)
Get the texture unit for a given texture object.
vtkTextureUnitManager * TextureUnitManager
void vtkglStencilMask(unsigned int mask)
manage vertex buffer objects shared within a context
abstracts an OpenGL texture object.
allocate/free texture units.
boost::graph_traits< vtkGraph * >::vertex_descriptor target(boost::graph_traits< vtkGraph * >::edge_descriptor e, vtkGraph *)
#define VTK_UNICODE_STRING
Definition: vtkType.h:71