-
Notifications
You must be signed in to change notification settings - Fork 20
/
GLRenderState.cpp
128 lines (110 loc) · 4.53 KB
/
GLRenderState.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/***********************************************************************
GLRenderState - Class to track changes to OpenGL state during rendering
of 3D Visualizer's visualization elements.
Copyright (c) 2012 Oliver Kreylos
This file is part of the 3D Data Visualizer (Visualizer).
The 3D Data Visualizer is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as published
by the Free Software Foundation; either version 2 of the License, or (at
your option) any later version.
The 3D Data Visualizer is distributed in the hope that it will be
useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License along
with the 3D Data Visualizer; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
***********************************************************************/
#include <GLRenderState.h>
#include <GL/GLLightTracker.h>
#include <GL/GLContextData.h>
/**************************************
Static elements of class GLRenderState:
**************************************/
const GLenum GLRenderState::textureLevelEnums[3]={GL_TEXTURE_1D,GL_TEXTURE_2D,GL_TEXTURE_3D};
const GLenum GLRenderState::matrixModeEnums[3]={GL_PROJECTION,GL_MODELVIEW,GL_TEXTURE};
/******************************
Methods of class GLRenderState:
******************************/
GLRenderState::GLRenderState(GLContextData& sContextData)
:contextData(sContextData)
{
/* Get the light tracker: */
GLLightTracker& lt=*(contextData.getLightTracker());
/* Initialize render state from OpenGL context: */
glGetFloatv(GL_POINT_SIZE,&savedPointSize);
pointSize=savedPointSize;
glGetFloatv(GL_LINE_WIDTH,&savedLineWidth);
lineWidth=savedLineWidth;
cullingEnabled=savedCullingEnabled=glIsEnabled(GL_CULL_FACE);
glGetIntegerv(GL_CULL_FACE_MODE,&savedCulledFace);
culledFace=savedCulledFace;
lightingEnabled=lt.isLightingEnabled();
twoSidedLightingEnabled=lt.isLightingTwoSided();
colorMaterialEnabled=lt.isColorMaterials();
colorMaterialFace=lt.getColorMaterialFace();
colorMaterialProperty=lt.getColorMaterialProperty();
/* Get enable states for the three texture levels: */
for(int i=0;i<3;++i)
savedTextureEnableds[i]=glIsEnabled(textureLevelEnums[i]);
/* Remember the highest enabled texture level, and disable all lower levels: */
int tli;
for(tli=2;tli>=0&&!savedTextureEnableds[tli];--tli)
;
textureLevel=tli+1;
for(--tli;tli>=0;--tli)
if(savedTextureEnableds[tli])
glDisable(textureLevelEnums[tli]);
glGetTexEnviv(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,&savedTextureMode);
textureMode=savedTextureMode;
separateSpecularColorEnabled=lt.isSpecularColorSeparate();
/* Save the texture objects currently bound to the three texture levels: */
glGetIntegerv(GL_TEXTURE_BINDING_1D,&(savedBoundTextures[0]));
glGetIntegerv(GL_TEXTURE_BINDING_2D,&(savedBoundTextures[1]));
glGetIntegerv(GL_TEXTURE_BINDING_3D,&(savedBoundTextures[2]));
for(int i=0;i<3;++i)
boundTextures[i]=savedBoundTextures[i];
/* Save the current matrix mode and initialize the matrix version numbers: */
glGetIntegerv(GL_MATRIX_MODE,&savedMatrixMode);
for(matrixModeIndex=0;matrixModeIndex<3&&GLenum(savedMatrixMode)==matrixModeEnums[matrixModeIndex];++matrixModeIndex)
;
for(int i=0;i<3;++i)
matrixVersions[i]=0;
}
GLRenderState::~GLRenderState(void)
{
/* Get the light tracker: */
GLLightTracker& lt=*(contextData.getLightTracker());
/* Reset OpenGL context to original state: */
setPointSize(savedPointSize);
setLineWidth(savedLineWidth);
if(savedCullingEnabled)
enableCulling(savedCulledFace);
else
disableCulling();
setLighting(lt.isLightingEnabled());
setTwoSidedLighting(lt.isLightingTwoSided());
if(lt.isColorMaterials())
enableColorMaterial(lt.getColorMaterialFace(),lt.getColorMaterialProperty());
else
disableColorMaterial();
setSeparateSpecularColor(lt.isSpecularColorSeparate());
for(int i=0;i<3;++i)
if(savedTextureEnableds[i])
{
if(textureLevel!=i+1)
glEnable(textureLevelEnums[i]);
}
else
{
if(textureLevel==i+1)
glDisable(textureLevelEnums[i]);
}
setTextureMode(savedTextureMode);
setSeparateSpecularColor(lt.isSpecularColorSeparate());
for(int i=0;i<3;++i)
if(GLuint(savedBoundTextures[i])!=boundTextures[i])
glBindTexture(textureLevelEnums[i],savedBoundTextures[i]);
if(GLenum(savedMatrixMode)!=matrixModeEnums[matrixModeIndex])
glMatrixMode(savedMatrixMode);
}