Change Encyclo button name and macros icon
[ryzomcore.git] / nel / src / 3d / driver / opengl / driver_opengl_states.cpp
blob5fd9e6416bde370d41e74d4e5e8cb4a1ca98b865
1 // NeL - MMORPG Framework <http://dev.ryzom.com/projects/nel/>
2 // Copyright (C) 2010 Winch Gate Property Limited
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Affero General Public License as
6 // published by the Free Software Foundation, either version 3 of the
7 // License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Affero General Public License for more details.
14 // You should have received a copy of the GNU Affero General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #include "stdopengl.h"
19 #include "driver_opengl.h"
21 // ***************************************************************************
22 // define it For Debug purpose only. Normal use is to hide this line
23 //#define NL3D_GLSTATE_DISABLE_CACHE
25 #ifdef DEBUG_NEW
26 #define new DEBUG_NEW
27 #endif
29 namespace NL3D {
31 #ifdef NL_STATIC
32 #ifdef USE_OPENGLES
33 namespace NLDRIVERGLES {
34 #else
35 namespace NLDRIVERGL {
36 #endif
37 #endif
39 // ***************************************************************************
40 CDriverGLStates::CDriverGLStates()
42 H_AUTO_OGL(CDriverGLStates_CDriverGLStates)
43 _TextureCubeMapSupported= false;
44 _CurrARBVertexBuffer = 0;
45 _DepthRangeNear = 0.f;
46 _DepthRangeFar = 1.f;
47 _ZBias = 0.f;
48 _MaxDriverLight= 0;
49 _CullMode = CCW;
53 // ***************************************************************************
54 void CDriverGLStates::init(bool supportTextureCubeMap, bool supportTextureRectangle, uint maxLight)
56 H_AUTO_OGL(CDriverGLStates_init)
57 _TextureCubeMapSupported= supportTextureCubeMap;
58 _TextureRectangleSupported= supportTextureRectangle;
59 _MaxDriverLight= maxLight;
60 _MaxDriverLight= std::min(_MaxDriverLight, uint(MaxLight));
62 // By default all arrays are disabled.
63 _VertexArrayEnabled= false;
64 _NormalArrayEnabled= false;
65 _WeightArrayEnabled= false;
66 _ColorArrayEnabled= false;
67 _SecondaryColorArrayEnabled= false;
68 uint i;
69 for(i=0; i<sizeof(_TexCoordArrayEnabled)/sizeof(_TexCoordArrayEnabled[0]); i++)
71 _TexCoordArrayEnabled[i]= false;
73 for(i=0; i<CVertexBuffer::NumValue; i++)
75 _VertexAttribArrayEnabled[i]= false;
77 _DepthRangeNear = 0.f;
78 _DepthRangeFar = 1.f;
79 _ZBias = 0.f;
80 // by default all lights are disabled (not reseted in forceDefaults)
81 for(i=0; i<MaxLight; i++)
83 _CurLight[i]= false;
88 // ***************************************************************************
89 void CDriverGLStates::forceDefaults(uint nbStages)
91 H_AUTO_OGL(CDriverGLStates_forceDefaults);
93 // Enable / disable.
94 _CurFog= false;
95 _CurBlend= false;
96 _CurCullFace= true;
97 _CurAlphaTest= false;
98 _CurLighting= false;
99 _CurZWrite= true;
100 _CurStencilTest=false;
101 _CurMultisample= false;
103 // setup GLStates.
104 glDisable(GL_FOG);
105 glDisable(GL_BLEND);
106 glEnable(GL_CULL_FACE);
107 glDisable(GL_ALPHA_TEST);
108 glDisable(GL_LIGHTING);
109 glDepthMask(GL_TRUE);
110 glDisable(GL_MULTISAMPLE_ARB);
112 // Func.
113 _CurBlendSrc= GL_SRC_ALPHA;
114 _CurBlendDst= GL_ONE_MINUS_SRC_ALPHA;
115 _CurDepthFunc= GL_LEQUAL;
116 _CurStencilFunc = GL_ALWAYS;
117 _CurStencilRef = 0;
118 _CurStencilMask = std::numeric_limits<GLuint>::max();
119 _CurStencilOpFail = GL_KEEP;
120 _CurStencilOpZFail = GL_KEEP;
121 _CurStencilOpZPass = GL_KEEP;
122 _CurStencilWriteMask = std::numeric_limits<GLuint>::max();
123 _CurAlphaTestThreshold= 0.5f;
125 // setup GLStates.
126 glBlendFunc(_CurBlendSrc, _CurBlendDst);
127 glDepthFunc(_CurDepthFunc);
128 glStencilFunc(_CurStencilFunc, _CurStencilRef, _CurStencilMask);
129 glStencilOp(_CurStencilOpFail, _CurStencilOpZFail, _CurStencilOpZPass);
130 glStencilMask(_CurStencilWriteMask);
131 glAlphaFunc(GL_GREATER, _CurAlphaTestThreshold);
135 // Materials.
136 uint32 packedOne= (CRGBA(255,255,255,255)).getPacked();
137 uint32 packedZero= (CRGBA(0,0,0,255)).getPacked();
138 _CurEmissive= packedZero;
139 _CurAmbient= packedOne;
140 _CurDiffuse= packedOne;
141 _CurSpecular= packedZero;
142 _CurShininess= 1;
144 // Lighted vertex color
145 _VertexColorLighted=false;
146 glDisable(GL_COLOR_MATERIAL);
148 // setup GLStates.
149 static const GLfloat one[4]= {1,1,1,1};
150 static const GLfloat zero[4]= {0,0,0,1};
151 glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, zero);
152 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, one);
153 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, one);
154 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, zero);
155 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, _CurShininess);
157 // TexModes
158 uint stage;
159 for(stage=0;stage<nbStages; stage++)
161 // disable texturing.
162 nglActiveTextureARB(GL_TEXTURE0_ARB+stage);
164 glDisable(GL_TEXTURE_2D);
166 if(_TextureCubeMapSupported)
168 glDisable(GL_TEXTURE_CUBE_MAP_ARB);
169 #ifdef USE_OPENGLES
170 glDisable(GL_TEXTURE_GEN_STR_OES);
171 #endif
174 _TextureMode[stage]= TextureDisabled;
176 // Tex gen init
177 _TexGenMode[stage] = 0;
179 #ifndef USE_OPENGLES
180 if(_TextureRectangleSupported)
181 glDisable(GL_TEXTURE_RECTANGLE_NV);
183 glDisable(GL_TEXTURE_GEN_S);
184 glDisable(GL_TEXTURE_GEN_T);
185 glDisable(GL_TEXTURE_GEN_R);
186 glDisable(GL_TEXTURE_GEN_Q);
187 #endif
190 // ActiveTexture current texture to 0.
191 nglActiveTextureARB(GL_TEXTURE0_ARB);
192 nglClientActiveTextureARB(GL_TEXTURE0_ARB);
194 _CurrentActiveTextureARB= 0;
195 _CurrentClientActiveTextureARB= 0;
197 // Depth range
198 _DepthRangeNear = 0.f;
199 _DepthRangeFar = 1.f;
200 _ZBias = 0.f;
202 #ifdef USE_OPENGLES
203 glDepthRangef (0.f, 1.f);
204 #else
205 glDepthRange (0, 1);
206 #endif
208 // Cull order
209 _CullMode = CCW;
210 glCullFace(GL_BACK);
213 // ***************************************************************************
214 void CDriverGLStates::enableBlend(uint enable)
216 H_AUTO_OGL(CDriverGLStates_enableBlend)
217 // If different from current setup, update.
218 bool enabled= (enable!=0);
219 #ifndef NL3D_GLSTATE_DISABLE_CACHE
220 if( enabled != _CurBlend )
221 #endif
223 // new state.
224 _CurBlend= enabled;
225 // Setup GLState.
226 if(_CurBlend)
227 glEnable(GL_BLEND);
228 else
229 glDisable(GL_BLEND);
233 // ***************************************************************************
234 void CDriverGLStates::enableCullFace(uint enable)
236 H_AUTO_OGL(CDriverGLStates_enableCullFace)
237 // If different from current setup, update.
238 bool enabled= (enable!=0);
239 #ifndef NL3D_GLSTATE_DISABLE_CACHE
240 if( enabled != _CurCullFace )
241 #endif
243 // new state.
244 _CurCullFace= enabled;
245 // Setup GLState.
246 if(_CurCullFace)
247 glEnable(GL_CULL_FACE);
248 else
249 glDisable(GL_CULL_FACE);
253 // ***************************************************************************
254 void CDriverGLStates::enableAlphaTest(uint enable)
256 H_AUTO_OGL(CDriverGLStates_enableAlphaTest)
257 // If different from current setup, update.
258 bool enabled= (enable!=0);
259 #ifndef NL3D_GLSTATE_DISABLE_CACHE
260 if( enabled != _CurAlphaTest )
261 #endif
263 // new state.
264 _CurAlphaTest= enabled;
266 // Setup GLState.
267 if(_CurAlphaTest)
269 glEnable(GL_ALPHA_TEST);
271 else
273 glDisable(GL_ALPHA_TEST);
280 // ***************************************************************************
281 void CDriverGLStates::enableLighting(uint enable)
283 H_AUTO_OGL(CDriverGLStates_enableLighting)
284 // If different from current setup, update.
285 bool enabled= (enable!=0);
286 #ifndef NL3D_GLSTATE_DISABLE_CACHE
287 if( enabled != _CurLighting )
288 #endif
290 // new state.
291 _CurLighting= enabled;
292 // Setup GLState.
293 if(_CurLighting)
294 glEnable(GL_LIGHTING);
295 else
297 glDisable(GL_LIGHTING);
302 // ***************************************************************************
303 void CDriverGLStates::enableLight(uint num, uint enable)
305 H_AUTO_OGL(CDriverGLStates_enableLight)
306 if(num>=_MaxDriverLight)
307 return;
309 // If different from current setup, update.
310 bool enabled= (enable!=0);
311 #ifndef NL3D_GLSTATE_DISABLE_CACHE
312 if( enabled != _CurLight[num] )
313 #endif
315 // new state.
316 _CurLight[num]= enabled;
317 // Setup GLState.
318 if(_CurLight[num])
319 glEnable ((GLenum)(GL_LIGHT0+num));
320 else
321 glDisable ((GLenum)(GL_LIGHT0+num));
325 // ***************************************************************************
326 bool CDriverGLStates::isLightEnabled(uint num) const
328 H_AUTO_OGL(CDriverGLStates_isLightEnabled)
329 if(num>=_MaxDriverLight)
330 return false;
331 else
332 return _CurLight[num];
336 // ***************************************************************************
337 void CDriverGLStates::enableZWrite(uint enable)
339 H_AUTO_OGL(CDriverGLStates_enableZWrite)
340 // If different from current setup, update.
341 bool enabled= (enable!=0);
342 #ifndef NL3D_GLSTATE_DISABLE_CACHE
343 if( enabled != _CurZWrite )
344 #endif
346 // new state.
347 _CurZWrite= enabled;
348 // Setup GLState.
349 if(_CurZWrite)
350 glDepthMask(GL_TRUE);
351 else
352 glDepthMask(GL_FALSE);
357 // ***************************************************************************
358 void CDriverGLStates::enableStencilTest(bool enable)
360 H_AUTO_OGL(CDriverGLStates_enableStencilTest);
362 // If different from current setup, update.
363 #ifndef NL3D_GLSTATE_DISABLE_CACHE
364 if( enable != _CurStencilTest )
365 #endif
367 // new state.
368 _CurStencilTest= enable;
369 // Setup GLState.
370 if(_CurStencilTest)
371 glEnable(GL_STENCIL_TEST);
372 else
373 glDisable(GL_STENCIL_TEST);
377 // ***************************************************************************
378 void CDriverGLStates::enableMultisample(bool enable)
380 H_AUTO_OGL(CDriverGLStates_enableMultisample);
382 // If different from current setup, update.
383 #ifndef NL3D_GLSTATE_DISABLE_CACHE
384 if( enable != _CurMultisample )
385 #endif
387 // new state.
388 _CurMultisample= enable;
390 // Setup GLState.
391 if(_CurMultisample)
392 glEnable(GL_MULTISAMPLE_ARB);
393 else
394 glDisable(GL_MULTISAMPLE_ARB);
398 // ***************************************************************************
399 void CDriverGLStates::blendFunc(GLenum src, GLenum dst)
401 H_AUTO_OGL(CDriverGLStates_blendFunc)
402 // If different from current setup, update.
403 #ifndef NL3D_GLSTATE_DISABLE_CACHE
404 if( src!= _CurBlendSrc || dst!=_CurBlendDst )
405 #endif
407 // new state.
408 _CurBlendSrc= src;
409 _CurBlendDst= dst;
410 // Setup GLState.
411 glBlendFunc(_CurBlendSrc, _CurBlendDst);
415 // ***************************************************************************
416 void CDriverGLStates::depthFunc(GLenum zcomp)
418 H_AUTO_OGL(CDriverGLStates_depthFunc)
419 // If different from current setup, update.
420 #ifndef NL3D_GLSTATE_DISABLE_CACHE
421 if( zcomp != _CurDepthFunc )
422 #endif
424 // new state.
425 _CurDepthFunc= zcomp;
426 // Setup GLState.
427 glDepthFunc(_CurDepthFunc);
432 // ***************************************************************************
433 void CDriverGLStates::alphaFunc(float threshold)
435 H_AUTO_OGL(CDriverGLStates_alphaFunc)
436 #ifndef NL3D_GLSTATE_DISABLE_CACHE
437 if(threshold != _CurAlphaTestThreshold)
438 #endif
440 // new state
441 _CurAlphaTestThreshold= threshold;
442 // setup function.
443 glAlphaFunc(GL_GREATER, _CurAlphaTestThreshold);
448 // ***************************************************************************
449 void CDriverGLStates::stencilFunc(GLenum func, GLint ref, GLuint mask)
451 H_AUTO_OGL(CDriverGLStates_stencilFunc)
452 #ifndef NL3D_GLSTATE_DISABLE_CACHE
453 if((func!=_CurStencilFunc) || (ref!=_CurStencilRef) || (mask!=_CurStencilMask))
454 #endif
456 // new state
457 _CurStencilFunc = func;
458 _CurStencilRef = ref;
459 _CurStencilMask = mask;
461 // setup function.
462 glStencilFunc(_CurStencilFunc, _CurStencilRef, _CurStencilMask);
467 // ***************************************************************************
468 void CDriverGLStates::stencilOp(GLenum fail, GLenum zfail, GLenum zpass)
470 H_AUTO_OGL(CDriverGLStates_stencilOp)
471 #ifndef NL3D_GLSTATE_DISABLE_CACHE
472 if((fail!=_CurStencilOpFail) || (zfail!=_CurStencilOpZFail) || (zpass!=_CurStencilOpZPass))
473 #endif
475 // new state
476 _CurStencilOpFail = fail;
477 _CurStencilOpZFail = zfail;
478 _CurStencilOpZPass = zpass;
480 // setup function.
481 glStencilOp(_CurStencilOpFail, _CurStencilOpZFail, _CurStencilOpZPass);
485 // ***************************************************************************
486 void CDriverGLStates::stencilMask(GLuint mask)
488 H_AUTO_OGL(CDriverGLStates_stencilMask)
489 #ifndef NL3D_GLSTATE_DISABLE_CACHE
490 if(mask!=_CurStencilWriteMask)
491 #endif
493 // new state
494 _CurStencilWriteMask = mask;
496 // setup function.
497 glStencilMask(_CurStencilWriteMask);
502 // ***************************************************************************
503 void CDriverGLStates::setEmissive(uint32 packedColor, const GLfloat color[4])
505 H_AUTO_OGL(CDriverGLStates_setEmissive)
506 #ifndef NL3D_GLSTATE_DISABLE_CACHE
507 if( packedColor!=_CurEmissive )
508 #endif
510 _CurEmissive= packedColor;
511 glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, color);
515 // ***************************************************************************
516 void CDriverGLStates::setAmbient(uint32 packedColor, const GLfloat color[4])
518 H_AUTO_OGL(CDriverGLStates_setAmbient)
519 #ifndef NL3D_GLSTATE_DISABLE_CACHE
520 if( packedColor!=_CurAmbient )
521 #endif
523 _CurAmbient= packedColor;
524 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, color);
528 // ***************************************************************************
529 void CDriverGLStates::setDiffuse(uint32 packedColor, const GLfloat color[4])
531 H_AUTO_OGL(CDriverGLStates_setDiffuse)
532 #ifndef NL3D_GLSTATE_DISABLE_CACHE
533 if( packedColor!=_CurDiffuse )
534 #endif
536 _CurDiffuse= packedColor;
537 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, color);
541 // ***************************************************************************
542 void CDriverGLStates::setSpecular(uint32 packedColor, const GLfloat color[4])
544 H_AUTO_OGL(CDriverGLStates_setSpecular)
545 #ifndef NL3D_GLSTATE_DISABLE_CACHE
546 if( packedColor!=_CurSpecular )
547 #endif
549 _CurSpecular= packedColor;
550 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, color);
554 // ***************************************************************************
555 void CDriverGLStates::setShininess(float shin)
557 H_AUTO_OGL(CDriverGLStates_setShininess)
558 #ifndef NL3D_GLSTATE_DISABLE_CACHE
559 if( shin != _CurShininess )
560 #endif
562 _CurShininess= shin;
563 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, shin);
568 // ***************************************************************************
569 static void convColor(CRGBA col, GLfloat glcol[4])
571 H_AUTO_OGL(convColor)
572 static const float OO255= 1.0f/255;
573 glcol[0]= col.R*OO255;
574 glcol[1]= col.G*OO255;
575 glcol[2]= col.B*OO255;
576 glcol[3]= col.A*OO255;
579 // ***************************************************************************
580 void CDriverGLStates::setVertexColorLighted(bool enable)
582 H_AUTO_OGL(CDriverGLStates_setVertexColorLighted)
583 #ifndef NL3D_GLSTATE_DISABLE_CACHE
584 if( enable != _VertexColorLighted)
585 #endif
587 _VertexColorLighted= enable;
589 if (_VertexColorLighted)
591 glEnable (GL_COLOR_MATERIAL);
592 #ifndef USE_OPENGLES
593 glColorMaterial (GL_FRONT_AND_BACK, GL_DIFFUSE);
594 #endif
596 else
598 glDisable (GL_COLOR_MATERIAL);
599 // Since we leave glColorMaterial mode, GL diffuse is now scracth. reset him to current value.
600 CRGBA diffCol;
601 diffCol.R= (uint8)((_CurDiffuse >> 24) & 255);
602 diffCol.G= (uint8)((_CurDiffuse >> 16) & 255);
603 diffCol.B= (uint8)((_CurDiffuse >> 8) & 255);
604 diffCol.A= (uint8)((_CurDiffuse ) & 255);
605 GLfloat glColor[4];
606 convColor(diffCol, glColor);
607 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, glColor);
613 // ***************************************************************************
614 void CDriverGLStates::updateDepthRange()
616 H_AUTO_OGL(CDriverGLStates_updateDepthRange);
618 float delta = _ZBias * (_DepthRangeFar - _DepthRangeNear);
620 glDepthRange(delta + _DepthRangeNear, delta + _DepthRangeFar);
623 // ***************************************************************************
624 void CDriverGLStates::setZBias(float zbias)
626 H_AUTO_OGL(CDriverGLStates_setZBias)
627 #ifndef NL3D_GLSTATE_DISABLE_CACHE
628 if (zbias != _ZBias)
629 #endif
631 _ZBias = zbias;
632 updateDepthRange();
637 // ***************************************************************************
638 void CDriverGLStates::setDepthRange(float znear, float zfar)
640 H_AUTO_OGL(CDriverGLStates_setDepthRange)
641 nlassert(znear != zfar);
642 #ifndef NL3D_GLSTATE_DISABLE_CACHE
643 if (znear != _DepthRangeNear || zfar != _DepthRangeFar)
644 #endif
646 _DepthRangeNear = znear;
647 _DepthRangeFar = zfar;
648 updateDepthRange();
652 // ***************************************************************************
653 void CDriverGLStates::setTexGenMode (uint stage, GLint mode)
655 H_AUTO_OGL(CDriverGLStates_setTexGenMode);
657 #ifndef NL3D_GLSTATE_DISABLE_CACHE
658 if (mode != _TexGenMode[stage])
659 #endif
661 _TexGenMode[stage] = mode;
663 if (!_TextureCubeMapSupported) return;
665 if(mode==0)
667 #ifdef USE_OPENGLES
668 glDisable(GL_TEXTURE_GEN_STR_OES);
669 #else
670 glDisable( GL_TEXTURE_GEN_S );
671 glDisable( GL_TEXTURE_GEN_T );
672 glDisable( GL_TEXTURE_GEN_R );
673 glDisable( GL_TEXTURE_GEN_Q );
674 #endif
676 else
678 #ifdef USE_OPENGLES
679 nglTexGeniOES(GL_TEXTURE_GEN_STR_OES, GL_TEXTURE_GEN_MODE_OES, mode);
680 #else
681 glTexGeni( GL_S, GL_TEXTURE_GEN_MODE, mode);
682 glTexGeni( GL_T, GL_TEXTURE_GEN_MODE, mode);
683 glTexGeni( GL_R, GL_TEXTURE_GEN_MODE, mode);
684 #endif
686 /* Object or Eye Space ? => enable W generation. VERY IMPORTANT because
687 was a bug with VegetableRender and ShadowRender:
688 - Vegetable use the TexCoord1.w in his VertexProgram
689 - Shadow Render don't use any TexCoord in VB (since projected)
690 => TexCoord1.w dirty!!
692 #ifdef USE_OPENGLES
693 // if(mode==GL_OBJECT_LINEAR || mode==GL_EYE_LINEAR)
694 // {
695 // nglTexGeniOES(GL_TEXTURE_GEN_STR_OES, GL_TEXTURE_GEN_MODE_OES, mode);
696 // glEnable(GL_TEXTURE_GEN_STR_OES);
697 // }
698 // else
699 // {
700 // glDisable(GL_TEXTURE_GEN_STR_OES);
701 // }
702 #else
703 if(mode==GL_OBJECT_LINEAR || mode==GL_EYE_LINEAR)
705 glTexGeni( GL_Q, GL_TEXTURE_GEN_MODE, mode);
706 glEnable( GL_TEXTURE_GEN_Q );
708 else
710 glDisable( GL_TEXTURE_GEN_Q );
712 #endif
714 // Enable All.
715 #ifdef USE_OPENGLES
716 glEnable(GL_TEXTURE_GEN_STR_OES);
717 #else
718 glEnable( GL_TEXTURE_GEN_S );
719 glEnable( GL_TEXTURE_GEN_T );
720 glEnable( GL_TEXTURE_GEN_R );
721 #endif
726 // ***************************************************************************
727 void CDriverGLStates::resetTextureMode()
729 H_AUTO_OGL(CDriverGLStates_resetTextureMode);
731 glDisable(GL_TEXTURE_2D);
733 if (_TextureCubeMapSupported)
735 glDisable(GL_TEXTURE_CUBE_MAP_ARB);
738 #ifndef USE_OPENGLES
739 if (_TextureRectangleSupported)
741 glDisable(GL_TEXTURE_RECTANGLE_NV);
743 #endif
745 _TextureMode[_CurrentActiveTextureARB]= TextureDisabled;
749 // ***************************************************************************
750 void CDriverGLStates::setTextureMode(TTextureMode texMode)
752 H_AUTO_OGL(CDriverGLStates_setTextureMode)
753 TTextureMode oldTexMode = _TextureMode[_CurrentActiveTextureARB];
754 if(oldTexMode != texMode)
756 // Disable first old mode.
757 if (oldTexMode == Texture2D)
759 glDisable(GL_TEXTURE_2D);
761 else if (oldTexMode == TextureRect)
763 #ifndef USE_OPENGLES
764 if(_TextureRectangleSupported)
766 glDisable(GL_TEXTURE_RECTANGLE_NV);
768 else
769 #endif
771 glDisable(GL_TEXTURE_2D);
774 else if (oldTexMode == TextureCubeMap)
776 if(_TextureCubeMapSupported)
778 glDisable(GL_TEXTURE_CUBE_MAP_ARB);
780 else
782 glDisable(GL_TEXTURE_2D);
786 // Enable new mode.
787 if(texMode == Texture2D)
789 glEnable(GL_TEXTURE_2D);
791 else if(texMode == TextureRect)
793 #ifndef USE_OPENGLES
794 if(_TextureRectangleSupported)
796 glEnable(GL_TEXTURE_RECTANGLE_NV);
798 else
799 #endif
801 glEnable(GL_TEXTURE_2D);
804 else if(texMode == TextureCubeMap)
806 if(_TextureCubeMapSupported)
808 glEnable(GL_TEXTURE_CUBE_MAP_ARB);
810 else
812 glEnable(GL_TEXTURE_2D);
816 // new mode.
817 _TextureMode[_CurrentActiveTextureARB]= texMode;
822 // ***************************************************************************
823 void CDriverGLStates::activeTextureARB(uint stage)
825 H_AUTO_OGL(CDriverGLStates_activeTextureARB);
827 if( _CurrentActiveTextureARB != stage )
829 nglActiveTextureARB(GL_TEXTURE0_ARB+stage);
831 _CurrentActiveTextureARB= stage;
835 // ***************************************************************************
836 void CDriverGLStates::forceActiveTextureARB(uint stage)
838 H_AUTO_OGL(CDriverGLStates_forceActiveTextureARB);
840 nglActiveTextureARB(GL_TEXTURE0_ARB+stage);
842 _CurrentActiveTextureARB= stage;
845 // ***************************************************************************
846 void CDriverGLStates::enableVertexArray(bool enable)
848 H_AUTO_OGL(CDriverGLStates_enableVertexArray);
850 if(_VertexArrayEnabled != enable)
852 if(enable)
853 glEnableClientState(GL_VERTEX_ARRAY);
854 else
855 glDisableClientState(GL_VERTEX_ARRAY);
857 _VertexArrayEnabled= enable;
860 // ***************************************************************************
861 void CDriverGLStates::enableNormalArray(bool enable)
863 H_AUTO_OGL(CDriverGLStates_enableNormalArray)
864 if(_NormalArrayEnabled != enable)
866 if(enable)
867 glEnableClientState(GL_NORMAL_ARRAY);
868 else
869 glDisableClientState(GL_NORMAL_ARRAY);
870 _NormalArrayEnabled= enable;
875 // ***************************************************************************
876 void CDriverGLStates::enableWeightArray(bool enable)
878 H_AUTO_OGL(CDriverGLStates_enableWeightArray);
880 if(_WeightArrayEnabled != enable)
882 #ifndef USE_OPENGLES
883 if(enable)
884 glEnableClientState(GL_VERTEX_WEIGHTING_EXT);
885 else
886 glDisableClientState(GL_VERTEX_WEIGHTING_EXT);
887 #endif
889 _WeightArrayEnabled= enable;
892 // ***************************************************************************
893 void CDriverGLStates::enableColorArray(bool enable)
895 H_AUTO_OGL(CDriverGLStates_enableColorArray);
897 if(_ColorArrayEnabled != enable)
899 if(enable)
900 glEnableClientState(GL_COLOR_ARRAY);
901 else
902 glDisableClientState(GL_COLOR_ARRAY);
903 _ColorArrayEnabled= enable;
910 // ***************************************************************************
911 void CDriverGLStates::enableSecondaryColorArray(bool enable)
913 H_AUTO_OGL(CDriverGLStates_enableSecondaryColorArray);
915 if(_SecondaryColorArrayEnabled != enable)
917 #ifndef USE_OPENGLES
918 if(enable)
919 glEnableClientState(GL_SECONDARY_COLOR_ARRAY_EXT);
920 else
921 glDisableClientState(GL_SECONDARY_COLOR_ARRAY_EXT);
922 #endif
924 _SecondaryColorArrayEnabled= enable;
926 #ifndef USE_OPENGLES
927 // If disable
928 if(!enable)
930 // GeForceFx Bug: Must reset Secondary color to 0 (if comes from a VP), else bugs
931 nglSecondaryColor3ubEXT(0,0,0);
933 #endif
937 // ***************************************************************************
938 void CDriverGLStates::clientActiveTextureARB(uint stage)
940 H_AUTO_OGL(CDriverGLStates_clientActiveTextureARB);
942 if( _CurrentClientActiveTextureARB != stage )
944 nglClientActiveTextureARB(GL_TEXTURE0_ARB+stage);
945 _CurrentClientActiveTextureARB= stage;
949 // ***************************************************************************
950 void CDriverGLStates::enableTexCoordArray(bool enable)
952 H_AUTO_OGL(CDriverGLStates_enableTexCoordArray);
954 if(_TexCoordArrayEnabled[_CurrentClientActiveTextureARB] != enable)
956 if(enable)
957 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
958 else
959 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
962 _TexCoordArrayEnabled[_CurrentClientActiveTextureARB]= enable;
966 // ***************************************************************************
967 void CDriverGLStates::enableVertexAttribArray(uint glIndex, bool enable)
969 H_AUTO_OGL(CDriverGLStates_enableVertexAttribArray);
971 if(_VertexAttribArrayEnabled[glIndex] != enable)
973 #ifndef USE_OPENGLES
974 if(enable)
975 glEnableClientState(glIndex+GL_VERTEX_ATTRIB_ARRAY0_NV);
976 else
977 glDisableClientState(glIndex+GL_VERTEX_ATTRIB_ARRAY0_NV);
978 #endif
980 _VertexAttribArrayEnabled[glIndex]= enable;
984 // ***************************************************************************
985 void CDriverGLStates::enableVertexAttribArrayARB(uint glIndex,bool enable)
987 H_AUTO_OGL(CDriverGLStates_enableVertexAttribArrayARB);
989 #ifndef NL3D_GLSTATE_DISABLE_CACHE
990 if(_VertexAttribArrayEnabled[glIndex] != enable)
991 #endif
993 #ifndef USE_OPENGLES
994 if(enable)
995 nglEnableVertexAttribArrayARB(glIndex);
996 else
997 nglDisableVertexAttribArrayARB(glIndex);
998 #endif
1000 _VertexAttribArrayEnabled[glIndex]= enable;
1004 // ***************************************************************************
1005 void CDriverGLStates::enableVertexAttribArrayForEXTVertexShader(uint glIndex, bool enable, uint *variants)
1007 H_AUTO_OGL(CDriverGLStates_enableVertexAttribArrayForEXTVertexShader);
1009 if(_VertexAttribArrayEnabled[glIndex] != enable)
1011 switch(glIndex)
1013 case 0: // position
1014 enableVertexArray(enable);
1015 break;
1016 case 1: // skin weight
1017 #ifndef USE_OPENGLES
1018 if(enable)
1019 nglEnableVariantClientStateEXT(variants[CDriverGL::EVSSkinWeightVariant]);
1020 else
1021 nglDisableVariantClientStateEXT(variants[CDriverGL::EVSSkinWeightVariant]);
1022 #endif
1023 break;
1024 case 2: // normal
1025 enableNormalArray(enable);
1026 break;
1027 case 3: // color
1028 enableColorArray(enable);
1029 break;
1030 case 4: // secondary color
1031 #ifndef USE_OPENGLES
1032 if(enable)
1033 nglEnableVariantClientStateEXT(variants[CDriverGL::EVSSecondaryColorVariant]);
1034 else
1035 nglDisableVariantClientStateEXT(variants[CDriverGL::EVSSecondaryColorVariant]);
1036 #endif
1037 break;
1038 case 5: // fog coordinate
1039 #ifndef USE_OPENGLES
1040 if(enable)
1041 nglEnableVariantClientStateEXT(variants[CDriverGL::EVSFogCoordsVariant]);
1042 else
1043 nglDisableVariantClientStateEXT(variants[CDriverGL::EVSFogCoordsVariant]);
1044 #endif
1045 break;
1046 case 6: // palette skin
1047 #ifndef USE_OPENGLES
1048 if(enable)
1049 nglEnableVariantClientStateEXT(variants[CDriverGL::EVSPaletteSkinVariant]);
1050 else
1051 nglDisableVariantClientStateEXT(variants[CDriverGL::EVSPaletteSkinVariant]);
1052 #endif
1053 break;
1054 case 7: // empty
1055 nlstop;
1056 break;
1057 case 8:
1058 case 9:
1059 case 10:
1060 case 11:
1061 case 12:
1062 case 13:
1063 case 14:
1064 case 15:
1065 clientActiveTextureARB(glIndex - 8);
1066 enableTexCoordArray(enable);
1067 break;
1068 default:
1069 nlstop; // invalid value
1070 break;
1072 _VertexAttribArrayEnabled[glIndex]= enable;
1078 // ***************************************************************************
1079 void CDriverGLStates::enableFog(uint enable)
1081 H_AUTO_OGL(CDriverGLStates_enableFog)
1082 // If different from current setup, update.
1083 bool enabled= (enable!=0);
1084 #ifndef NL3D_GLSTATE_DISABLE_CACHE
1085 if( enabled != _CurFog )
1086 #endif
1088 // new state.
1089 _CurFog= enabled;
1090 // Setup GLState.
1091 if(_CurFog)
1092 glEnable(GL_FOG);
1093 else
1094 glDisable(GL_FOG);
1098 // ***************************************************************************
1099 void CDriverGLStates::forceBindARBVertexBuffer(uint objectID)
1101 H_AUTO_OGL(CDriverGLStates_forceBindARBVertexBuffer)
1103 nglBindBufferARB(GL_ARRAY_BUFFER_ARB, objectID);
1105 _CurrARBVertexBuffer = objectID;
1108 // ***************************************************************************
1109 void CDriverGLStates::bindARBVertexBuffer(uint objectID)
1111 H_AUTO_OGL(CDriverGLStates_bindARBVertexBuffer)
1112 #ifndef NL3D_GLSTATE_DISABLE_CACHE
1113 if (objectID != _CurrARBVertexBuffer)
1114 #endif
1116 forceBindARBVertexBuffer(objectID);
1120 // ***************************************************************************
1121 void CDriverGLStates::setCullMode(TCullMode cullMode)
1123 H_AUTO_OGL(CDriverGLStates_setCullMode)
1124 #ifndef NL3D_GLSTATE_DISABLE_CACHE
1125 if (cullMode != _CullMode)
1126 #endif
1128 glCullFace(cullMode == CCW ? GL_BACK : GL_FRONT);
1129 _CullMode = cullMode;
1133 // ***************************************************************************
1134 CDriverGLStates::TCullMode CDriverGLStates::getCullMode() const
1136 H_AUTO_OGL(CDriverGLStates_CDriverGLStates)
1137 return _CullMode;
1140 #ifdef NL_STATIC
1141 } // NLDRIVERGL/ES
1142 #endif
1144 } // NL3D