1 // NeL - MMORPG Framework <http://dev.ryzom.com/projects/nel/>
2 // Copyright (C) 2010 Winch Gate Property Limited
4 // This source file has been modified by the following contributors:
5 // Copyright (C) 2010 Robert TIMM (rti) <mail@rtti.de>
7 // This program is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU Affero General Public License as
9 // published by the Free Software Foundation, either version 3 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU Affero General Public License for more details.
17 // You should have received a copy of the GNU Affero General Public License
18 // along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #ifndef NL_DRIVER_OPENGL_STATES_H
21 #define NL_DRIVER_OPENGL_STATES_H
23 #include "nel/misc/types_nl.h"
24 #include "nel/3d/vertex_buffer.h"
30 namespace NLDRIVERGLES
{
32 namespace NLDRIVERGL
{
36 // ***************************************************************************
38 * Class for optimizing calls to openGL states, by caching old ones.
39 * All following call with OpenGL must be done with only one instance of this class:
40 - glEnable() glDisable() with:
46 - GL_TEXTURE_2D or GL_TEXTURE_CUBE_MAP_ARB/OES.
47 - GL_TEXTURE_GEN_S, GL_TEXTURE_GEN_T, GL_TEXTURE_GEN_R
51 - glActiveTextureARB()
52 - glClientActiveTextureARB()
53 - glEnableClientState() glDisableClientState() with:
56 - GL_VERTEX_WEIGHTING_EXT
58 - GL_SECONDARY_COLOR_ARRAY_EXT
59 - GL_TEXTURE_COORD_ARRAY
60 - GL_VERTEX_ATTRIB_ARRAY0_NV + i.
65 - glMaterialf() and glMaterialfv() for:
74 * \author Lionel Berenguier
75 * \author Nevrax France
81 /// Constructor. no-op.
83 // init. Do it just after setDisplay()
84 void init(bool supportTextureCubeMap
, bool supportTextureRectangle
, uint maxLight
);
86 /// Reset all OpenGL states of interest to default, and update caching. This don't apply to light.
87 void forceDefaults(uint nbTextureStages
);
89 /// \name enable if !0
91 void enableBlend(uint enable
);
92 void enableFog(uint enable
);
93 void enableCullFace(uint enable
);
94 /// enable and set good AlphaFunc.
95 void enableAlphaTest(uint enable
);
96 void enableZWrite(uint enable
);
97 // overall lighting enabled
98 void enableLighting(uint enable
);
99 bool isLightingEnabled() const { return _CurLighting
; }
100 /// enable/disable specific light. num must be < "maxLight" param set in init()
101 void enableLight(uint num
, uint enable
);
102 bool isLightEnabled(uint num
) const;
103 /// enable/disable stencil test
104 void enableStencilTest(bool enable
);
105 bool isStencilTestEnabled() const { return _CurStencilTest
; }
106 /// enable/disable multisample
107 void enableMultisample(bool enable
);
108 bool isMultisampleEnabled() const { return _CurMultisample
; }
112 void blendFunc(GLenum src
, GLenum dst
);
114 void depthFunc(GLenum zcomp
);
116 void alphaFunc(float threshold
);
118 void stencilFunc(GLenum stencilFunc
, GLint ref
, GLuint mask
);
120 void stencilOp(GLenum fail
, GLenum zfail
, GLenum zpass
);
122 void stencilMask(uint mask
);
124 /// \name Material setting.
125 /// Each f() get an uint32 for fast comparison, and OpenGL colors.
127 void setEmissive(uint32 packedColor
, const GLfloat color
[4]);
128 void setAmbient(uint32 packedColor
, const GLfloat color
[4]);
129 void setDiffuse(uint32 packedColor
, const GLfloat color
[4]);
130 void setSpecular(uint32 packedColor
, const GLfloat color
[4]);
131 void setShininess(float shin
);
132 void setVertexColorLighted(bool enable
);
133 void setDepthRange (float znear
, float zfar
);
134 void getDepthRange(float &znear
, float &zfar
) const { znear
= _DepthRangeNear
; zfar
= _DepthRangeFar
; }
136 * NB : this is done in window coordinate, not in world coordinate as with CMaterial
138 void setZBias(float zbias
);
139 // NB: set 0 to reset TexGen.
140 void setTexGenMode (uint stage
, GLint mode
);
145 /// \name Texture Mode setting.
147 enum TTextureMode
{TextureDisabled
, Texture2D
, TextureRect
, TextureCubeMap
, TextureModeCount
};
148 /// same as glActiveTextureARB(). useful for setTextureMode.
149 void activeTextureARB(uint stage
);
150 /// same as active texture arb, but with no cache check
151 void forceActiveTextureARB(uint stage
);
152 /// get active texture
153 uint
getActiveTextureARB() const { return _CurrentActiveTextureARB
; }
154 /** change if needed the texture mode of the current active Texture ARB.
155 * NB: if CubeMap extension not supported, TextureCubeMap <=> TextureDisabled.
157 void setTextureMode(TTextureMode texMode
);
158 TTextureMode
getTextureMode() const { return _TextureMode
[_CurrentActiveTextureARB
]; }
159 // reset texture mode to the default (disabled) for the current stage. It forces the state (useful after texture shaders)
160 void resetTextureMode();
163 /// \name Vertex Array enabling.
164 /// equivalent to glEnableClientState() / glDisableClientState(). NB: Not modified by forceDefaults()
166 void enableVertexArray(bool enable
);
167 void enableNormalArray(bool enable
);
168 void enableWeightArray(bool enable
);
169 void enableColorArray(bool enable
);
170 void enableSecondaryColorArray(bool enable
);
171 /// same as glClientActiveTextureARB(). useful for enableTexCoordArray.
172 void clientActiveTextureARB(uint stage
);
173 /// NB: caller must call correct clientActiveTextureARB() before.
174 void enableTexCoordArray(bool enable
);
175 /** For vertexProgram. do not check if supported or not.
177 void enableVertexAttribArray(uint glIndex
, bool enable
);
178 /** Same as enableVertexAttribArray, but for EXTVertexShader (need variant ID)
179 * \param firstVariantSymbol the first variant symbol
181 void enableVertexAttribArrayForEXTVertexShader(uint glIndex
, bool enable
, uint
*variants
);
183 // special version for ARB_vertex_program used with ARB_vertex_buffer or ATI_vertex_attrib_array_object
184 void enableVertexAttribArrayARB(uint glIndex
, bool enable
);
190 // ARB_vertex_buffer_object buffer binding
191 void bindARBVertexBuffer(uint objectID
);
192 void forceBindARBVertexBuffer(uint objectID
);
193 uint
getCurrBoundARBVertexBuffer() const { return _CurrARBVertexBuffer
; }
195 enum TCullMode
{ CCW
= 0, CW
};
196 void setCullMode(TCullMode cullMode
);
197 TCullMode
getCullMode() const;
206 bool _CurStencilTest
;
207 bool _CurMultisample
;
211 GLenum _CurDepthFunc
;
212 GLenum _CurStencilFunc
;
213 GLint _CurStencilRef
;
214 GLuint _CurStencilMask
;
215 GLenum _CurStencilOpFail
;
216 GLenum _CurStencilOpZFail
;
217 GLenum _CurStencilOpZPass
;
218 GLuint _CurStencilWriteMask
;
219 float _CurAlphaTestThreshold
;
226 bool _VertexColorLighted
;
228 bool _TextureCubeMapSupported
;
229 bool _TextureRectangleSupported
;
230 uint _CurrentActiveTextureARB
;
231 TTextureMode _TextureMode
[8];
233 bool _VertexArrayEnabled
;
234 bool _NormalArrayEnabled
;
235 bool _WeightArrayEnabled
;
236 bool _ColorArrayEnabled
;
237 bool _SecondaryColorArrayEnabled
;
238 uint _CurrentClientActiveTextureARB
;
239 bool _TexCoordArrayEnabled
[8];
240 bool _VertexAttribArrayEnabled
[CVertexBuffer::NumValue
];
242 GLint _TexGenMode
[8];
244 uint _CurrARBVertexBuffer
;
246 float _DepthRangeNear
;
247 float _DepthRangeFar
;
248 float _ZBias
; // NB : zbias is in window coordinates
253 void updateDepthRange();
255 // Mirror of glEnable() and GL_LIGHT0+i
257 uint _MaxDriverLight
;
258 bool _CurLight
[MaxLight
];
268 #endif // NL_DRIVER_OPENGL_STATES_H
270 /* End of driver_opengl_states.h */