2 Cafu Engine, http://www.cafu.de/
3 Copyright (c) Carsten Fuchs and other contributors.
4 This project is licensed under the terms of the MIT license.
11 #ifndef CAFU_MATSYS_OPENGLSTATE_HPP_INCLUDED
12 #define CAFU_MATSYS_OPENGLSTATE_HPP_INCLUDED
14 // Required for #include <GL/gl.h> with MS VC++.
15 #if defined(_WIN32) && defined(_MSC_VER)
16 #define WIN32_LEAN_AND_MEAN
26 /// This class encapsulates the OpenGL state, with the intention of minimizing actual OpenGL state changes.
33 void ActiveTextureUnit(GLenum texUnit
);
34 void AlphaFunc(GLenum func
, GLclampf ref
);
35 void BlendFunc(GLenum sfactor
, GLenum dfactor
);
36 void DepthFunc(GLenum func
);
37 void ActiveStencilFace(GLenum mode
);
38 void StencilFunc(GLenum func
, GLint ref
, GLuint mask
);
39 void StencilOp(GLenum fail
, GLenum zfail
, GLenum zpass
);
40 void ColorMask(GLboolean red
, GLboolean green
, GLboolean blue
, GLboolean alpha
);
41 void DepthMask(GLboolean flag
);
42 void CullFace(GLenum mode
);
43 void FrontFace(GLenum mode
);
44 void PolygonMode(/*GLenum face=GL_FRONT_AND_BACK,*/ GLenum mode
);
45 void PolygonOffset(GLfloat factor
, GLfloat units
);
46 void Enable(GLenum cap
);
47 void Disable(GLenum cap
);
50 // Matrix related functions.
51 enum MatrixModeT
{ MODELVIEW
, PROJECTION
, TEXTURE
, MATRIX0
}; // Simply use MATRIX0+i for more.
53 // Note that this method (in combination with DepRelMatrixTs) subsumes *all* OpenGL matrix functions!
54 // (That is, there is *intentionally* no glMatrixMode(), glLoadIdentity(), glTranslate(), glRotate(), ...)
55 void LoadMatrix(MatrixModeT MatrixMode
, const DepRelMatrixT
& Matrix
);
58 // Functions that only affect the current texture unit.
59 void BindTexture(GLenum target
, GLuint texture
);
60 void TexEnv(GLenum target
, GLenum pname
, GLint param
);
61 void TexEnv(GLenum target
, GLenum pname
, GLfloat param
);
62 void TexEnv(GLenum target
, GLenum pname
, GLint
* param
);
63 void TexEnv(GLenum target
, GLenum pname
, GLfloat
* param
);
66 // void TexParameter*();
69 static const GLenum MeshToOpenGLType
[]; ///< Translates from MeshT primitive types to OpenGL rendering primitives.
70 static const GLenum WindingToOpenGL
[]; ///< Translates from MeshT windings to OpenGL windings (cw or ccw).
71 static const GLenum BlendFactorToOpenGL
[]; ///< Translates from MaterialT blend factors to OpenGL blend factors.
72 static const GLenum PolygonModeToOpenGL
[]; ///< Translates from MaterialT::PolygonModeT to OpenGL polygon modes.
73 static const GLenum PolygonModeToOpenGL_Offset
[]; ///< Translates from MaterialT::PolygonModeT to OpenGL depth offset polygon modes.
74 static const GLenum MatrixModeToOpenGL
[]; ///< Translates from our MatrixModeT modes to OpenGL matrix modes.
76 static OpenGLStateT
* GetInstance();
88 // OpenGL permits to enable multiple texturing targets simultaneously, and uses in such cases the one with the highest dimensionality enabled.
89 // See the "OpenGL Programming Guide (Red Book)", page 357 for details.
90 // I change the semantic of calls to OpenGLStateT::Enable(GL_TEXTURE_*) such that only one target is ever enabled at any one time.
91 bool Texturing_IsEnabled
;
92 GLenum Texturing_Target
; ///< One of GL_TEXTURE_1D, GL_TEXTURE_2D, GL_TEXTURE_3D, GL_TEXTURE_CUBE_MAP_ARB.
94 bool IsEnabled_TextureGenQ
;
95 bool IsEnabled_TextureGenR
;
96 bool IsEnabled_TextureGenS
;
97 bool IsEnabled_TextureGenT
;
100 GLenum ActiveTexUnit_Unit
;
101 unsigned long ActiveTexUnit_Index
;
102 GLenum AlphaFunc_Func
;
103 GLclampf AlphaFunc_Ref
;
104 GLenum BlendFunc_sfactor
;
105 GLenum BlendFunc_dfactor
;
106 GLenum DepthFunc_Func
;
107 GLenum ActiveStencilFace_Mode
;
108 GLenum StencilFunc_Func
[2];
109 GLint StencilFunc_ref
[2];
110 GLuint StencilFunc_mask
[2];
111 GLenum StencilOp_fail
[2];
112 GLenum StencilOp_zfail
[2];
113 GLenum StencilOp_zpass
[2];
114 GLboolean ColorMask_FlagRed
;
115 GLboolean ColorMask_FlagGreen
;
116 GLboolean ColorMask_FlagBlue
;
117 GLboolean ColorMask_FlagAlpha
;
118 GLboolean DepthMask_Flag
;
119 GLenum CullFace_mode
;
120 GLenum FrontFace_mode
;
121 GLenum PolygonMode_mode
;
122 GLfloat PolygonOffset_factor
;
123 GLfloat PolygonOffset_units
;
124 bool IsEnabled_AlphaTest
;
125 bool IsEnabled_AutoNormal
;
126 bool IsEnabled_Blend
;
127 // bool IsEnabled_ClipPlanei;
128 bool IsEnabled_ColorMaterial
;
129 bool IsEnabled_CullFace
;
130 bool IsEnabled_DepthTest
;
131 bool IsEnabled_Dither
;
133 // bool IsEnabled_Lighti;
134 bool IsEnabled_Lighting
;
135 bool IsEnabled_LineSmooth
;
136 bool IsEnabled_LineStipple
;
137 bool IsEnabled_LogicOp
;
138 bool IsEnabled_Normalize
;
139 bool IsEnabled_PointSmooth
;
140 bool IsEnabled_PolygonSmooth
;
141 bool IsEnabled_PolygonStipple
;
142 bool IsEnabled_PolygonOffsetFill
;
143 bool IsEnabled_PolygonOffsetLine
;
144 bool IsEnabled_PolygonOffsetPoint
;
145 bool IsEnabled_ScissorTest
;
146 bool IsEnabled_StencilTest
;
147 bool IsEnabled_StencilTestTwoSide
;
148 unsigned long LoadedMatrixID
[3+32]; ///< MatrixID[MM] stores the ID of the DepRelMatrixT that is bound to (loaded at) the corresponding OpenGL matrix mode.
149 unsigned long LoadedMatrixAge
[3+32]; ///< MatrixID[MM] stores the age of the DepRelMatrixT that is bound to (loaded at) the corresponding OpenGL matrix mode.
150 TexUnitStateT TexUnitStates
[8];
152 /// The constructor. Private because this is a singleton class.