00001
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include "stdopengl.h"
00027
00028
00029
00030
00031
00032
00033 namespace NL3D
00034 {
00035
00036
00037 CDriverGLStates::CDriverGLStates()
00038 {
00039 _TextureCubeMapSupported= false;
00040 }
00041
00042
00043
00044 void CDriverGLStates::init(bool supportTextureCubeMap)
00045 {
00046 _TextureCubeMapSupported= supportTextureCubeMap;
00047
00048
00049 _VertexArrayEnabled= false;
00050 _NormalArrayEnabled= false;
00051 _WeightArrayEnabled= false;
00052 _ColorArrayEnabled= false;
00053 _SecondaryColorArrayEnabled= false;
00054 uint i;
00055 for(i=0; i<IDRV_MAT_MAXTEXTURES; i++)
00056 {
00057 _TexCoordArrayEnabled[i]= false;
00058 }
00059 for(i=0; i<CVertexBuffer::NumValue; i++)
00060 {
00061 _VertexAttribArrayEnabled[i]= false;
00062 }
00063 }
00064
00065
00066
00067 void CDriverGLStates::forceDefaults(uint nbStages)
00068 {
00069
00070 _CurBlend= false;
00071 _CurCullFace= true;
00072 _CurAlphaTest= false;
00073 _CurLighting= false;
00074 _CurZWrite= true;
00075
00076 glDisable(GL_BLEND);
00077 glEnable(GL_CULL_FACE);
00078 glDisable(GL_ALPHA_TEST);
00079 glDisable(GL_LIGHTING);
00080 glDepthMask(GL_TRUE);
00081
00082
00083 _CurBlendSrc= GL_SRC_ALPHA;
00084 _CurBlendDst= GL_ONE_MINUS_SRC_ALPHA;
00085 _CurDepthFunc= GL_LEQUAL;
00086 _CurAlphaTestThreshold= 0.5f;
00087
00088 glBlendFunc(_CurBlendSrc, _CurBlendDst);
00089 glDepthFunc(_CurDepthFunc);
00090 glAlphaFunc(GL_GREATER, _CurAlphaTestThreshold);
00091
00092
00093 uint32 packedOne= (CRGBA(255,255,255,255)).getPacked();
00094 uint32 packedZero= (CRGBA(0,0,0,255)).getPacked();
00095 _CurEmissive= packedZero;
00096 _CurAmbient= packedOne;
00097 _CurDiffuse= packedOne;
00098 _CurSpecular= packedZero;
00099 _CurShininess= 1;
00100
00101
00102 _VertexColorLighted=false;
00103 glDisable(GL_COLOR_MATERIAL);
00104
00105
00106 static const GLfloat one[4]= {1,1,1,1};
00107 static const GLfloat zero[4]= {0,0,0,1};
00108 glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, zero);
00109 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, one);
00110 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, one);
00111 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, zero);
00112 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, _CurShininess);
00113
00114
00115
00116 uint stage;
00117 for(stage=0;stage<nbStages; stage++)
00118 {
00119
00120 nglActiveTextureARB(GL_TEXTURE0_ARB+stage);
00121 glDisable(GL_TEXTURE_2D);
00122 if(_TextureCubeMapSupported)
00123 glDisable(GL_TEXTURE_CUBE_MAP_ARB);
00124 _TextureMode[stage]= TextureDisabled;
00125
00126
00127 _TexGen[stage] = false;
00128 _TexGenMode[stage] = GL_SPHERE_MAP;
00129 glTexGeni( GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
00130 glTexGeni( GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
00131 glTexGeni( GL_R, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
00132 glDisable( GL_TEXTURE_GEN_S );
00133 glDisable( GL_TEXTURE_GEN_T );
00134 glDisable( GL_TEXTURE_GEN_R );
00135 }
00136
00137
00138 nglActiveTextureARB(GL_TEXTURE0_ARB);
00139 _CurrentActiveTextureARB= 0;
00140 nglClientActiveTextureARB(GL_TEXTURE0_ARB);
00141 _CurrentClientActiveTextureARB= 0;
00142
00143
00144 _CurZRangeDelta = 0;
00145 glDepthRange (0, 1);
00146 }
00147
00148
00149
00150
00151 void CDriverGLStates::enableBlend(uint enable)
00152 {
00153
00154 bool enabled= (enable!=0);
00155 #ifndef NL3D_GLSTATE_DISABLE_CACHE
00156 if( enabled != _CurBlend )
00157 #endif
00158 {
00159
00160 _CurBlend= enabled;
00161
00162 if(_CurBlend)
00163 glEnable(GL_BLEND);
00164 else
00165 glDisable(GL_BLEND);
00166 }
00167 }
00168
00169
00170 void CDriverGLStates::enableCullFace(uint enable)
00171 {
00172
00173 bool enabled= (enable!=0);
00174 #ifndef NL3D_GLSTATE_DISABLE_CACHE
00175 if( enabled != _CurCullFace )
00176 #endif
00177 {
00178
00179 _CurCullFace= enabled;
00180
00181 if(_CurCullFace)
00182 glEnable(GL_CULL_FACE);
00183 else
00184 glDisable(GL_CULL_FACE);
00185 }
00186 }
00187
00188
00189 void CDriverGLStates::enableAlphaTest(uint enable)
00190 {
00191
00192 bool enabled= (enable!=0);
00193 #ifndef NL3D_GLSTATE_DISABLE_CACHE
00194 if( enabled != _CurAlphaTest )
00195 #endif
00196 {
00197
00198 _CurAlphaTest= enabled;
00199
00200
00201 if(_CurAlphaTest)
00202 {
00203 glEnable(GL_ALPHA_TEST);
00204 }
00205 else
00206 {
00207 glDisable(GL_ALPHA_TEST);
00208 }
00209 }
00210 }
00211
00212
00213 void CDriverGLStates::enableLighting(uint enable)
00214 {
00215
00216 bool enabled= (enable!=0);
00217 #ifndef NL3D_GLSTATE_DISABLE_CACHE
00218 if( enabled != _CurLighting )
00219 #endif
00220 {
00221
00222 _CurLighting= enabled;
00223
00224 if(_CurLighting)
00225 glEnable(GL_LIGHTING);
00226 else
00227 glDisable(GL_LIGHTING);
00228 }
00229 }
00230
00231
00232 void CDriverGLStates::enableZWrite(uint enable)
00233 {
00234
00235 bool enabled= (enable!=0);
00236 #ifndef NL3D_GLSTATE_DISABLE_CACHE
00237 if( enabled != _CurZWrite )
00238 #endif
00239 {
00240
00241 _CurZWrite= enabled;
00242
00243 if(_CurZWrite)
00244 glDepthMask(GL_TRUE);
00245 else
00246 glDepthMask(GL_FALSE);
00247 }
00248 }
00249
00250
00251
00252
00253 void CDriverGLStates::blendFunc(GLenum src, GLenum dst)
00254 {
00255
00256 #ifndef NL3D_GLSTATE_DISABLE_CACHE
00257 if( src!= _CurBlendSrc || dst!=_CurBlendDst )
00258 #endif
00259 {
00260
00261 _CurBlendSrc= src;
00262 _CurBlendDst= dst;
00263
00264 glBlendFunc(_CurBlendSrc, _CurBlendDst);
00265 }
00266 }
00267
00268
00269 void CDriverGLStates::depthFunc(GLenum zcomp)
00270 {
00271
00272 #ifndef NL3D_GLSTATE_DISABLE_CACHE
00273 if( zcomp != _CurDepthFunc )
00274 #endif
00275 {
00276
00277 _CurDepthFunc= zcomp;
00278
00279 glDepthFunc(_CurDepthFunc);
00280 }
00281 }
00282
00283
00284
00285 void CDriverGLStates::alphaFunc(float threshold)
00286 {
00287 #ifndef NL3D_GLSTATE_DISABLE_CACHE
00288 if(threshold != _CurAlphaTestThreshold)
00289 #endif
00290 {
00291
00292 _CurAlphaTestThreshold= threshold;
00293
00294 glAlphaFunc(GL_GREATER, _CurAlphaTestThreshold);
00295 }
00296 }
00297
00298
00299
00300 void CDriverGLStates::setEmissive(uint32 packedColor, const GLfloat color[4])
00301 {
00302 #ifndef NL3D_GLSTATE_DISABLE_CACHE
00303 if( packedColor!=_CurEmissive )
00304 #endif
00305 {
00306 _CurEmissive= packedColor;
00307 glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, color);
00308 }
00309 }
00310
00311
00312 void CDriverGLStates::setAmbient(uint32 packedColor, const GLfloat color[4])
00313 {
00314 #ifndef NL3D_GLSTATE_DISABLE_CACHE
00315 if( packedColor!=_CurAmbient )
00316 #endif
00317 {
00318 _CurAmbient= packedColor;
00319 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, color);
00320 }
00321 }
00322
00323
00324 void CDriverGLStates::setDiffuse(uint32 packedColor, const GLfloat color[4])
00325 {
00326 #ifndef NL3D_GLSTATE_DISABLE_CACHE
00327 if( packedColor!=_CurDiffuse )
00328 #endif
00329 {
00330 _CurDiffuse= packedColor;
00331 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, color);
00332 }
00333 }
00334
00335
00336 void CDriverGLStates::setSpecular(uint32 packedColor, const GLfloat color[4])
00337 {
00338 #ifndef NL3D_GLSTATE_DISABLE_CACHE
00339 if( packedColor!=_CurSpecular )
00340 #endif
00341 {
00342 _CurSpecular= packedColor;
00343 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, color);
00344 }
00345 }
00346
00347
00348 void CDriverGLStates::setShininess(float shin)
00349 {
00350 #ifndef NL3D_GLSTATE_DISABLE_CACHE
00351 if( shin != _CurShininess )
00352 #endif
00353 {
00354 _CurShininess= shin;
00355 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, shin);
00356 }
00357 }
00358
00359
00360
00361 static void convColor(CRGBA col, GLfloat glcol[4])
00362 {
00363 static const float OO255= 1.0f/255;
00364 glcol[0]= col.R*OO255;
00365 glcol[1]= col.G*OO255;
00366 glcol[2]= col.B*OO255;
00367 glcol[3]= col.A*OO255;
00368 }
00369
00370
00371 void CDriverGLStates::setVertexColorLighted(bool enable)
00372 {
00373 #ifndef NL3D_GLSTATE_DISABLE_CACHE
00374 if( enable != _VertexColorLighted)
00375 #endif
00376 {
00377 _VertexColorLighted= enable;
00378
00379 if (_VertexColorLighted)
00380 {
00381 glEnable (GL_COLOR_MATERIAL);
00382 glColorMaterial (GL_FRONT_AND_BACK, GL_DIFFUSE);
00383 }
00384 else
00385 {
00386 glDisable (GL_COLOR_MATERIAL);
00387
00388 CRGBA diffCol;
00389 diffCol.R= (uint8)((_CurDiffuse >> 24) & 255);
00390 diffCol.G= (uint8)((_CurDiffuse >> 16) & 255);
00391 diffCol.B= (uint8)((_CurDiffuse >> 8) & 255);
00392 diffCol.A= (uint8)((_CurDiffuse ) & 255);
00393 GLfloat glColor[4];
00394 convColor(diffCol, glColor);
00395 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, glColor);
00396 }
00397 }
00398 }
00399
00400
00401 void CDriverGLStates::setDepthRange (float zDelta)
00402 {
00403 #ifndef NL3D_GLSTATE_DISABLE_CACHE
00404 if (zDelta != _CurZRangeDelta)
00405 #endif
00406 {
00407 _CurZRangeDelta = zDelta;
00408
00409
00410 glDepthRange (zDelta, 1+zDelta);
00411 }
00412 }
00413
00414
00415 void CDriverGLStates::enableTexGen (uint stage, bool enable)
00416 {
00417 #ifndef NL3D_GLSTATE_DISABLE_CACHE
00418 if (enable != _TexGen[stage])
00419 #endif
00420 {
00421 _TexGen[stage] = enable;
00422
00423
00424 if (_TexGen[stage])
00425 {
00426 glEnable( GL_TEXTURE_GEN_S );
00427 glEnable( GL_TEXTURE_GEN_T );
00428 glEnable( GL_TEXTURE_GEN_R );
00429 }
00430 else
00431 {
00432 glDisable( GL_TEXTURE_GEN_S );
00433 glDisable( GL_TEXTURE_GEN_T );
00434 glDisable( GL_TEXTURE_GEN_R );
00435 }
00436 }
00437 }
00438
00439
00440 void CDriverGLStates::setTexGenMode (uint stage, GLint mode)
00441 {
00442 #ifndef NL3D_GLSTATE_DISABLE_CACHE
00443 if (mode != _TexGenMode[stage])
00444 #endif
00445 {
00446 _TexGenMode[stage] = mode;
00447
00448 glTexGeni( GL_S, GL_TEXTURE_GEN_MODE, mode);
00449 glTexGeni( GL_T, GL_TEXTURE_GEN_MODE, mode);
00450 glTexGeni( GL_R, GL_TEXTURE_GEN_MODE, mode);
00451 }
00452 }
00453
00454
00455
00456
00457
00458 void CDriverGLStates::resetTextureMode()
00459 {
00460 glDisable(GL_TEXTURE_2D);
00461 if (_TextureCubeMapSupported)
00462 {
00463 glDisable(GL_TEXTURE_CUBE_MAP_ARB);
00464 }
00465 _TextureMode[_CurrentActiveTextureARB]= TextureDisabled;
00466 }
00467
00468
00469
00470 void CDriverGLStates::setTextureMode(TTextureMode texMode)
00471 {
00472 TTextureMode oldTexMode = _TextureMode[_CurrentActiveTextureARB];
00473 if(oldTexMode != texMode)
00474 {
00475
00476 if(oldTexMode == Texture2D)
00477 glDisable(GL_TEXTURE_2D);
00478 else if(oldTexMode == TextureCubeMap)
00479 {
00480 if(_TextureCubeMapSupported)
00481 glDisable(GL_TEXTURE_CUBE_MAP_ARB);
00482 else
00483 glDisable(GL_TEXTURE_2D);
00484 }
00485
00486
00487 if(texMode == Texture2D)
00488 glEnable(GL_TEXTURE_2D);
00489 else if(texMode == TextureCubeMap)
00490 {
00491 if(_TextureCubeMapSupported)
00492 glEnable(GL_TEXTURE_CUBE_MAP_ARB);
00493 else
00494 glDisable(GL_TEXTURE_2D);
00495 }
00496
00497
00498 _TextureMode[_CurrentActiveTextureARB]= texMode;
00499 }
00500 }
00501
00502
00503
00504 void CDriverGLStates::activeTextureARB(uint stage)
00505 {
00506 if( _CurrentActiveTextureARB != stage )
00507 {
00508 nglActiveTextureARB(GL_TEXTURE0_ARB+stage);
00509 _CurrentActiveTextureARB= stage;
00510 }
00511 }
00512
00513
00514 void CDriverGLStates::forceActiveTextureARB(uint stage)
00515 {
00516 nglActiveTextureARB(GL_TEXTURE0_ARB+stage);
00517 _CurrentActiveTextureARB= stage;
00518 }
00519
00520
00521
00522 void CDriverGLStates::enableVertexArray(bool enable)
00523 {
00524 if(_VertexArrayEnabled != enable)
00525 {
00526 if(enable)
00527 glEnableClientState(GL_VERTEX_ARRAY);
00528 else
00529 glDisableClientState(GL_VERTEX_ARRAY);
00530 _VertexArrayEnabled= enable;
00531 }
00532 }
00533
00534 void CDriverGLStates::enableNormalArray(bool enable)
00535 {
00536 if(_NormalArrayEnabled != enable)
00537 {
00538 if(enable)
00539 glEnableClientState(GL_NORMAL_ARRAY);
00540 else
00541 glDisableClientState(GL_NORMAL_ARRAY);
00542 _NormalArrayEnabled= enable;
00543 }
00544 }
00545
00546 void CDriverGLStates::enableWeightArray(bool enable)
00547 {
00548 if(_WeightArrayEnabled != enable)
00549 {
00550 if(enable)
00551 glEnableClientState(GL_VERTEX_WEIGHTING_EXT);
00552 else
00553 glDisableClientState(GL_VERTEX_WEIGHTING_EXT);
00554 _WeightArrayEnabled= enable;
00555 }
00556 }
00557
00558 void CDriverGLStates::enableColorArray(bool enable)
00559 {
00560 if(_ColorArrayEnabled != enable)
00561 {
00562 if(enable)
00563 glEnableClientState(GL_COLOR_ARRAY);
00564 else
00565 glDisableClientState(GL_COLOR_ARRAY);
00566 _ColorArrayEnabled= enable;
00567 }
00568 }
00569
00570
00571
00572 void CDriverGLStates::enableSecondaryColorArray(bool enable)
00573 {
00574 if(_SecondaryColorArrayEnabled != enable)
00575 {
00576 if(enable)
00577 glEnableClientState(GL_SECONDARY_COLOR_ARRAY_EXT);
00578 else
00579 glDisableClientState(GL_SECONDARY_COLOR_ARRAY_EXT);
00580 _SecondaryColorArrayEnabled= enable;
00581 }
00582 }
00583
00584
00585 void CDriverGLStates::clientActiveTextureARB(uint stage)
00586 {
00587 if( _CurrentClientActiveTextureARB != stage )
00588 {
00589 nglClientActiveTextureARB(GL_TEXTURE0_ARB+stage);
00590 _CurrentClientActiveTextureARB= stage;
00591 }
00592 }
00593
00594
00595 void CDriverGLStates::enableTexCoordArray(bool enable)
00596 {
00597 if(_TexCoordArrayEnabled[_CurrentClientActiveTextureARB] != enable)
00598 {
00599 if(enable)
00600 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
00601 else
00602 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
00603 _TexCoordArrayEnabled[_CurrentClientActiveTextureARB]= enable;
00604 }
00605 }
00606
00607
00608 void CDriverGLStates::enableVertexAttribArray(uint glIndex, bool enable)
00609 {
00610 if(_VertexAttribArrayEnabled[glIndex] != enable)
00611 {
00612 if(enable)
00613 glEnableClientState(glIndex+GL_VERTEX_ATTRIB_ARRAY0_NV);
00614 else
00615 glDisableClientState(glIndex+GL_VERTEX_ATTRIB_ARRAY0_NV);
00616 _VertexAttribArrayEnabled[glIndex]= enable;
00617 }
00618 }
00619
00620
00621
00622 void CDriverGLStates::enableVertexAttribArrayForEXTVertexShader(uint glIndex, bool enable, uint *variants)
00623 {
00624 if(_VertexAttribArrayEnabled[glIndex] != enable)
00625 {
00626 switch(glIndex)
00627 {
00628 case 0:
00629 enableVertexArray(enable);
00630 break;
00631 case 1:
00632 if(enable)
00633 nglEnableVariantClientStateEXT(variants[CDriverGL::EVSSkinWeightVariant]);
00634 else
00635 nglDisableVariantClientStateEXT(variants[CDriverGL::EVSSkinWeightVariant]);
00636 break;
00637 case 2:
00638 enableNormalArray(enable);
00639 break;
00640 case 3:
00641 enableColorArray(enable);
00642 break;
00643 case 4:
00644 if(enable)
00645 nglEnableVariantClientStateEXT(variants[CDriverGL::EVSSecondaryColorVariant]);
00646 else
00647 nglDisableVariantClientStateEXT(variants[CDriverGL::EVSSecondaryColorVariant]);
00648 break;
00649 case 5:
00650 if(enable)
00651 nglEnableVariantClientStateEXT(variants[CDriverGL::EVSFogCoordsVariant]);
00652 else
00653 nglDisableVariantClientStateEXT(variants[CDriverGL::EVSFogCoordsVariant]);
00654 break;
00655 case 6:
00656 if(enable)
00657 nglEnableVariantClientStateEXT(variants[CDriverGL::EVSPaletteSkinVariant]);
00658 else
00659 nglDisableVariantClientStateEXT(variants[CDriverGL::EVSPaletteSkinVariant]);
00660 break;
00661 case 7:
00662 nlstop
00663 break;
00664 case 8:
00665 case 9:
00666 case 10:
00667 case 11:
00668 case 12:
00669 case 13:
00670 case 14:
00671 case 15:
00672 clientActiveTextureARB(glIndex - 8);
00673 enableTexCoordArray(enable);
00674 break;
00675 default:
00676 nlstop;
00677 break;
00678 }
00679 _VertexAttribArrayEnabled[glIndex]= enable;
00680 }
00681 }
00682
00683
00684
00685 }