VTK  9.1.0
vtkVolumeStateRAII.h
Go to the documentation of this file.
1/*=========================================================================
2
3 Program: Visualization Toolkit
4 Module: vtkVolumeStateRAII.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=========================================================================*/
15#ifndef vtkVolumeStateRAII_h
16#define vtkVolumeStateRAII_h
18#include "vtkOpenGLState.h"
19
20// Only these states can be queries via glIsEnabled:
21// http://www.khronos.org/opengles/sdk/docs/man/
22
24{
25public:
26 vtkVolumeStateRAII(vtkOpenGLState* ostate, bool noOp = false)
27 : NoOp(noOp)
28 {
29 this->State = ostate;
30
31 if (this->NoOp)
32 {
33 return;
34 }
35
36 this->DepthTestEnabled = ostate->GetEnumState(GL_DEPTH_TEST);
37
38 this->BlendEnabled = ostate->GetEnumState(GL_BLEND);
39
40 this->CullFaceEnabled = ostate->GetEnumState(GL_CULL_FACE);
41 ostate->vtkglGetIntegerv(GL_CULL_FACE_MODE, &this->CullFaceMode);
42
43 GLboolean depthMaskWrite = GL_TRUE;
44 ostate->vtkglGetBooleanv(GL_DEPTH_WRITEMASK, &depthMaskWrite);
45 this->DepthMaskEnabled = (depthMaskWrite == GL_TRUE);
46
47 // Enable depth_sampler test
48 ostate->vtkglEnable(GL_DEPTH_TEST);
49
50 // Set the over blending function
51 // NOTE: It is important to choose GL_ONE vs GL_SRC_ALPHA as our colors
52 // will be premultiplied by the alpha value (doing front to back blending)
53 ostate->vtkglBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
54
55 ostate->vtkglEnable(GL_BLEND);
56
57 // Enable cull face and set cull face mode
58 ostate->vtkglCullFace(GL_BACK);
59
60 ostate->vtkglEnable(GL_CULL_FACE);
61
62 // Disable depth mask writing
63 ostate->vtkglDepthMask(GL_FALSE);
64 }
65
67 {
68 glBindVertexArray(0);
69 glBindBuffer(GL_ARRAY_BUFFER, 0);
70 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
71
72 if (this->NoOp)
73 {
74 return;
75 }
76
77 this->State->vtkglCullFace(this->CullFaceMode);
78 this->State->SetEnumState(GL_CULL_FACE, this->CullFaceEnabled);
79 this->State->vtkglBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
80
81 // this does not actually restore the state always
82 // but a test fails if I change it so either the original
83 // test was wrong or it is itended
84 if (!this->BlendEnabled)
85 {
86 this->State->vtkglDisable(GL_BLEND);
87 }
88
89 this->State->SetEnumState(GL_DEPTH_TEST, this->DepthTestEnabled);
90
91 if (this->DepthMaskEnabled)
92 {
93 this->State->vtkglDepthMask(GL_TRUE);
94 }
95 }
96
97private:
98 bool NoOp;
99 bool DepthTestEnabled;
100 bool BlendEnabled;
101 bool CullFaceEnabled;
102 GLint CullFaceMode;
103 bool DepthMaskEnabled;
104 vtkOpenGLState* State;
105};
106
107#endif // vtkVolumeStateRAII_h
108// VTK-HeaderTest-Exclude: vtkVolumeStateRAII.h
OpenGL state storage.
void vtkglGetIntegerv(unsigned int pname, int *params)
bool GetEnumState(unsigned int name)
void vtkglCullFace(unsigned int val)
void vtkglDisable(unsigned int cap)
void vtkglBlendFunc(unsigned int sfactor, unsigned int dfactor)
void vtkglEnable(unsigned int cap)
void vtkglDepthMask(unsigned char flag)
void SetEnumState(unsigned int name, bool value)
void vtkglGetBooleanv(unsigned int pname, unsigned char *params)
vtkVolumeStateRAII(vtkOpenGLState *ostate, bool noOp=false)