VTK  9.4.20241219
vtkVolumeStateRAII.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
2// SPDX-License-Identifier: BSD-3-Clause
3#ifndef vtkVolumeStateRAII_h
4#define vtkVolumeStateRAII_h
6#include "vtkOpenGLState.h"
7
8// Only these states can be queries via glIsEnabled:
9// http://www.khronos.org/opengles/sdk/docs/man/
10
11VTK_ABI_NAMESPACE_BEGIN
13{
14public:
15 vtkVolumeStateRAII(vtkOpenGLState* ostate, bool noOp = false)
16 : NoOp(noOp)
17 {
18 this->State = ostate;
19
20 if (this->NoOp)
21 {
22 return;
23 }
24
25 this->DepthTestEnabled = ostate->GetEnumState(GL_DEPTH_TEST);
26
27 this->BlendEnabled = ostate->GetEnumState(GL_BLEND);
28
29 this->CullFaceEnabled = ostate->GetEnumState(GL_CULL_FACE);
30 ostate->vtkglGetIntegerv(GL_CULL_FACE_MODE, &this->CullFaceMode);
31
32 GLboolean depthMaskWrite = GL_TRUE;
33 ostate->vtkglGetBooleanv(GL_DEPTH_WRITEMASK, &depthMaskWrite);
34 this->DepthMaskEnabled = (depthMaskWrite == GL_TRUE);
35
36 // Enable depth_sampler test
37 ostate->vtkglEnable(GL_DEPTH_TEST);
38
39 // Set the over blending function
40 // NOTE: It is important to choose GL_ONE vs GL_SRC_ALPHA as our colors
41 // will be premultiplied by the alpha value (doing front to back blending)
42 ostate->vtkglBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
43
44 ostate->vtkglEnable(GL_BLEND);
45
46 // Enable cull face and set cull face mode
47 ostate->vtkglCullFace(GL_BACK);
48
49 ostate->vtkglEnable(GL_CULL_FACE);
50
51 // Disable depth mask writing
52 ostate->vtkglDepthMask(GL_FALSE);
53 }
54
56 {
57 glBindVertexArray(0);
58 glBindBuffer(GL_ARRAY_BUFFER, 0);
59 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
60
61 if (this->NoOp)
62 {
63 return;
64 }
65
66 this->State->vtkglCullFace(this->CullFaceMode);
67 this->State->SetEnumState(GL_CULL_FACE, this->CullFaceEnabled);
68 this->State->vtkglBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
69
70 // this does not actually restore the state always
71 // but a test fails if I change it so either the original
72 // test was wrong or it is itended
73 if (!this->BlendEnabled)
74 {
75 this->State->vtkglDisable(GL_BLEND);
76 }
77
78 this->State->SetEnumState(GL_DEPTH_TEST, this->DepthTestEnabled);
79
80 if (this->DepthMaskEnabled)
81 {
82 this->State->vtkglDepthMask(GL_TRUE);
83 }
84 }
85
86private:
87 bool NoOp;
88 bool DepthTestEnabled;
89 bool BlendEnabled;
90 bool CullFaceEnabled;
91 GLint CullFaceMode;
92 bool DepthMaskEnabled;
93 vtkOpenGLState* State;
94};
95
96VTK_ABI_NAMESPACE_END
97#endif // vtkVolumeStateRAII_h
98// 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)