Cafu Engine
OpenGLState.hpp
1 /*
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.
5 */
6 
7 /********************/
8 /*** OpenGL State ***/
9 /********************/
10 
11 #ifndef CAFU_MATSYS_OPENGLSTATE_HPP_INCLUDED
12 #define CAFU_MATSYS_OPENGLSTATE_HPP_INCLUDED
13 
14 // Required for #include <GL/gl.h> with MS VC++.
15 #if defined(_WIN32) && defined(_MSC_VER)
16 #define WIN32_LEAN_AND_MEAN
17 #include <windows.h>
18 #endif
19 
20 #include <GL/gl.h>
21 
22 
23 class DepRelMatrixT;
24 
25 
26 /// This class encapsulates the OpenGL state, with the intention of minimizing actual OpenGL state changes.
28 {
29  public:
30 
31  void Reset();
32 
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);
48 
49 
50  // Matrix related functions.
51  enum MatrixModeT { MODELVIEW, PROJECTION, TEXTURE, MATRIX0 }; // Simply use MATRIX0+i for more.
52 
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);
56 
57 
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);
64  // void TexGen*();
65  // void TexImage*();
66  // void TexParameter*();
67 
68 
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.
75 
76  static OpenGLStateT* GetInstance();
77 
78 
79  private:
80 
81  struct TexUnitStateT
82  {
83  GLuint TexObj1D;
84  GLuint TexObj2D;
85  GLuint TexObj3D;
86  GLuint TexObjCube;
87 
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.
93 
94  bool IsEnabled_TextureGenQ;
95  bool IsEnabled_TextureGenR;
96  bool IsEnabled_TextureGenS;
97  bool IsEnabled_TextureGenT;
98  };
99 
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;
132  bool IsEnabled_Fog;
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];
151 
152  /// The constructor. Private because this is a singleton class.
153  OpenGLStateT();
154 };
155 
156 #endif
static const GLenum MatrixModeToOpenGL[]
Translates from our MatrixModeT modes to OpenGL matrix modes.
Definition: OpenGLState.hpp:74
This class encapsulates the OpenGL state, with the intention of minimizing actual OpenGL state change...
Definition: OpenGLState.hpp:27
static const GLenum BlendFactorToOpenGL[]
Translates from MaterialT blend factors to OpenGL blend factors.
Definition: OpenGLState.hpp:71
static const GLenum PolygonModeToOpenGL_Offset[]
Translates from MaterialT::PolygonModeT to OpenGL depth offset polygon modes.
Definition: OpenGLState.hpp:73
static const GLenum MeshToOpenGLType[]
Translates from MeshT primitive types to OpenGL rendering primitives.
Definition: OpenGLState.hpp:69
A matrix class with which dependencies among matrices can be handled.
Definition: DepRelMatrix.hpp:22
static const GLenum WindingToOpenGL[]
Translates from MeshT windings to OpenGL windings (cw or ccw).
Definition: OpenGLState.hpp:70
static const GLenum PolygonModeToOpenGL[]
Translates from MaterialT::PolygonModeT to OpenGL polygon modes.
Definition: OpenGLState.hpp:72
void LoadMatrix(MatrixModeT MatrixMode, const DepRelMatrixT &Matrix)
This method loads a matrix.
Definition: OpenGLState.cpp:436