CrystalSpace

Public API Reference

csplugincommon/opengl/glstates.h

Go to the documentation of this file.
00001 /*
00002     Copyright (C) 2002 by Anders Stenberg
00003     Written by Anders Stenberg
00004 
00005     This library is free software; you can redistribute it and/or
00006     modify it under the terms of the GNU Library General Public
00007     License as published by the Free Software Foundation; either
00008     version 2 of the License, or (at your option) any later version.
00009 
00010     This library is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013     Library General Public License for more details.
00014 
00015     You should have received a copy of the GNU Library General Public
00016     License along with this library; if not, write to the Free
00017     Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00018 */
00019 
00020 #ifndef __CS_GLSTATES_H__
00021 #define __CS_GLSTATES_H__
00022 
00027 #if defined(CS_OPENGL_PATH)
00028 #include CS_HEADER_GLOBAL(CS_OPENGL_PATH,gl.h)
00029 #else
00030 #include <GL/gl.h>
00031 #endif
00032 
00033 #include "csextern_gl.h"
00034 #include "glextmanager.h"
00035 
00039 // Set to 'true' to force state changing commands. For debugging.
00040 #define FORCE_STATE_CHANGE                        false/*true*/
00041 
00042 #define DECLARE_CACHED_BOOL(name) \
00043   bool enabled_##name;
00044 
00045 #define IMPLEMENT_CACHED_BOOL(name) \
00046   void Enable_##name () \
00047   { \
00048     if (!currentContext->enabled_##name || FORCE_STATE_CHANGE) \
00049     { \
00050       currentContext->enabled_##name = true;  \
00051       glEnable (name); \
00052     } \
00053   } \
00054   void Disable_##name () \
00055   { \
00056     if (currentContext->enabled_##name || FORCE_STATE_CHANGE) { \
00057       currentContext->enabled_##name = false;  \
00058       glDisable (name); \
00059     } \
00060   } \
00061   bool IsEnabled_##name () const \
00062   { \
00063     return currentContext->enabled_##name; \
00064   }
00065 
00066 #define CS_GL_MAX_LAYER 16
00067 
00068 #define DECLARE_CACHED_BOOL_CURRENTLAYER(name) \
00069   bool enabled_##name[CS_GL_MAX_LAYER];
00070 
00071 #define IMPLEMENT_CACHED_BOOL_CURRENTLAYER(name) \
00072   void Enable_##name () \
00073   { \
00074     if (!currentContext->enabled_##name[currentContext->currentUnit] || FORCE_STATE_CHANGE) \
00075     { \
00076       ActivateTU (); \
00077       currentContext->enabled_##name[currentContext->currentUnit] = true;  \
00078       glEnable (name); \
00079     } \
00080   } \
00081   void Disable_##name () \
00082   { \
00083     if (currentContext->enabled_##name[currentContext->currentUnit] || FORCE_STATE_CHANGE) \
00084     { \
00085       ActivateTU (); \
00086       currentContext->enabled_##name[currentContext->currentUnit] = false;  \
00087       glDisable (name); \
00088     } \
00089   } \
00090   bool IsEnabled_##name () const \
00091   { \
00092     return currentContext->enabled_##name[currentContext->currentUnit]; \
00093   }
00094 
00095 #define DECLARE_CACHED_PARAMETER_1(func, name, type1, param1) \
00096   type1 parameter_##param1;
00097 
00098 #define IMPLEMENT_CACHED_PARAMETER_1(func, name, type1, param1) \
00099   void Set##name (type1 param1, bool forced = false) \
00100   { \
00101     if (forced || (param1 != currentContext->parameter_##param1) || FORCE_STATE_CHANGE) \
00102     { \
00103       currentContext->parameter_##param1 = param1;  \
00104       func (param1); \
00105     } \
00106   } \
00107   void Get##name (type1 & param1) const\
00108   { \
00109     param1 = currentContext->parameter_##param1;  \
00110   }
00111 
00112 #define DECLARE_CACHED_PARAMETER_2(func, name, type1, param1, type2, param2) \
00113   type1 parameter_##param1; \
00114   type2 parameter_##param2;
00115 
00116 #define IMPLEMENT_CACHED_PARAMETER_2(func, name, type1, param1, type2, param2) \
00117   void Set##name (type1 param1, type2 param2, bool forced = false) \
00118   { \
00119     if (forced || (param1 != currentContext->parameter_##param1) || \
00120         (param2 != currentContext->parameter_##param2) || FORCE_STATE_CHANGE) \
00121     { \
00122       currentContext->parameter_##param1 = param1;  \
00123       currentContext->parameter_##param2 = param2;  \
00124       func (param1, param2); \
00125     } \
00126   } \
00127   void Get##name (type1 & param1, type2 & param2) const\
00128   { \
00129     param1 = currentContext->parameter_##param1;  \
00130     param2 = currentContext->parameter_##param2;  \
00131   }
00132 
00133 #define DECLARE_CACHED_PARAMETER_3(func, name, type1, param1, type2, param2, type3, param3) \
00134   type1 parameter_##param1; \
00135   type2 parameter_##param2; \
00136   type3 parameter_##param3;
00137 
00138 #define IMPLEMENT_CACHED_PARAMETER_3(func, name, type1, param1, type2, param2, type3, param3) \
00139   void Set##name (type1 param1, type2 param2, type3 param3, bool forced = false) \
00140   { \
00141     if (forced || (param1 != currentContext->parameter_##param1) \
00142         || (param2 != currentContext->parameter_##param2) \
00143         || (param3 != currentContext->parameter_##param3) || FORCE_STATE_CHANGE) \
00144     { \
00145       currentContext->parameter_##param1 = param1;  \
00146       currentContext->parameter_##param2 = param2;  \
00147       currentContext->parameter_##param3 = param3;  \
00148       func (param1, param2, param3); \
00149     } \
00150   } \
00151   void Get##name (type1 &param1, type2 & param2, type3 & param3) const\
00152   { \
00153     param1 = currentContext->parameter_##param1;  \
00154     param2 = currentContext->parameter_##param2;  \
00155     param3 = currentContext->parameter_##param3;  \
00156   }
00157 
00158 #define DECLARE_CACHED_PARAMETER_4(func, name, type1, param1, \
00159   type2, param2, type3, param3, type4, param4) \
00160   type1 parameter_##param1; \
00161   type2 parameter_##param2; \
00162   type3 parameter_##param3; \
00163   type4 parameter_##param4;
00164 
00165 #define IMPLEMENT_CACHED_PARAMETER_4(func, name, type1, param1, \
00166     type2, param2, type3, param3, type4, param4) \
00167   void Set##name (type1 param1, type2 param2, type3 param3, type4 param4, \
00168     bool forced = false) \
00169   { \
00170     if (forced || (param1 != currentContext->parameter_##param1) || \
00171       (param2 != currentContext->parameter_##param2) || \
00172       (param3 != currentContext->parameter_##param3) || \
00173       (param4 != currentContext->parameter_##param4) || FORCE_STATE_CHANGE) \
00174     { \
00175       currentContext->parameter_##param1 = param1;  \
00176       currentContext->parameter_##param2 = param2;  \
00177       currentContext->parameter_##param3 = param3;  \
00178       currentContext->parameter_##param4 = param4;  \
00179       func (param1, param2, param3, param4); \
00180     } \
00181   } \
00182   void Get##name (type1 &param1, type2 & param2, type3 & param3, type4& param4) const\
00183   { \
00184     param1 = currentContext->parameter_##param1;  \
00185     param2 = currentContext->parameter_##param2;  \
00186     param3 = currentContext->parameter_##param3;  \
00187     param4 = currentContext->parameter_##param4;  \
00188   }
00189 
00190 #define DECLARE_CACHED_CLIENT_STATE(name)             \
00191   bool enabled_##name;
00192 
00193 #define IMPLEMENT_CACHED_CLIENT_STATE(name)           \
00194   void Enable_##name () \
00195   { \
00196     if (!currentContext->enabled_##name || FORCE_STATE_CHANGE) \
00197     { \
00198       currentContext->enabled_##name = true;  \
00199       glEnableClientState (name); \
00200     } \
00201   } \
00202   void Disable_##name () \
00203   { \
00204     if (currentContext->enabled_##name || FORCE_STATE_CHANGE) { \
00205       currentContext->enabled_##name = false;  \
00206       glDisableClientState (name); \
00207     } \
00208   } \
00209   bool IsEnabled_##name () const \
00210   { \
00211     return currentContext->enabled_##name; \
00212   }
00213 
00214 #define DECLARE_CACHED_CLIENT_STATE_LAYER(name)       \
00215   bool enabled_##name[CS_GL_MAX_LAYER];
00216 
00217 #define IMPLEMENT_CACHED_CLIENT_STATE_LAYER(name)             \
00218   void Enable_##name () \
00219   { \
00220     if (!currentContext->enabled_##name[currentContext->currentUnit] \
00221         || FORCE_STATE_CHANGE) \
00222     { \
00223       ActivateTU (); \
00224       currentContext->enabled_##name[currentContext->currentUnit] = true;  \
00225       glEnableClientState (name); \
00226     } \
00227   } \
00228   void Disable_##name () \
00229   { \
00230     if (currentContext->enabled_##name[currentContext->currentUnit] \
00231         || FORCE_STATE_CHANGE) { \
00232       ActivateTU (); \
00233       currentContext->enabled_##name[currentContext->currentUnit] = false;  \
00234       glDisableClientState (name); \
00235     } \
00236   } \
00237   bool IsEnabled_##name () const \
00238   { \
00239     return currentContext->enabled_##name[currentContext->currentUnit]; \
00240   }
00241 
00242 #define DECLARE_CACHED_PARAMETER_1_LAYER(func, name, type1, param1) \
00243   type1 parameter_##param1[CS_GL_MAX_LAYER];
00244 
00245 #define IMPLEMENT_CACHED_PARAMETER_1_LAYER(func, name, type1, param1) \
00246   void Set##name (type1 param1, bool forced = false) \
00247   { \
00248     if (forced || \
00249         (param1 != currentContext->parameter_##param1[currentContext->currentUnit] \
00250         || FORCE_STATE_CHANGE)) \
00251     { \
00252       ActivateTU (); \
00253       currentContext->parameter_##param1[currentContext->currentUnit] = param1;  \
00254       func (param1); \
00255     } \
00256   } \
00257   void Get##name (type1 &param1) const\
00258   { \
00259     param1 = currentContext->parameter_##param1[currentContext->currentUnit];  \
00260   }
00261 
00262 #define DECLARE_CACHED_PARAMETER_2_LAYER(func, name, type1, param1, \
00263   type2, param2) \
00264   type1 parameter_##param1[CS_GL_MAX_LAYER]; \
00265   type2 parameter_##param2[CS_GL_MAX_LAYER];
00266 
00267 #define IMPLEMENT_CACHED_PARAMETER_2_LAYER(func, name, type1, param1, \
00268   type2, param2) \
00269   void Set##name (type1 param1, type2 param2, bool forced = false) \
00270   { \
00271     if (forced || (param1 != currentContext->parameter_##param1[ \
00272                     currentContext->currentUnit]) || \
00273                   (param2 != currentContext->parameter_##param2[ \
00274                     currentContext->currentUnit]) \
00275                || FORCE_STATE_CHANGE) \
00276     { \
00277       ActivateTU (); \
00278       currentContext->parameter_##param1[currentContext->currentUnit] = param1;  \
00279       currentContext->parameter_##param2[currentContext->currentUnit] = param2;  \
00280       func (param1, param2); \
00281     } \
00282   } \
00283   void Get##name (type1 &param1, type2 & param2) const\
00284   { \
00285     param1 = currentContext->parameter_##param1[currentContext->currentUnit];  \
00286     param2 = currentContext->parameter_##param2[currentContext->currentUnit];  \
00287   }
00288 
00289 #define DECLARE_CACHED_PARAMETER_3_LAYER(func, name, type1, param1, \
00290   type2, param2, type3, param3) \
00291   type1 parameter_##param1[CS_GL_MAX_LAYER]; \
00292   type2 parameter_##param2[CS_GL_MAX_LAYER]; \
00293   type3 parameter_##param3[CS_GL_MAX_LAYER];
00294 
00295 
00296 #define IMPLEMENT_CACHED_PARAMETER_3_LAYER(func, name, type1, param1, \
00297   type2, param2, type3, param3) \
00298   void Set##name (type1 param1, type2 param2, type3 param3,\
00299     bool forced = false) \
00300   { \
00301     if (forced || (param1 != currentContext->parameter_##param1[ \
00302                     currentContext->currentUnit]) || \
00303                   (param2 != currentContext->parameter_##param2[ \
00304                     currentContext->currentUnit]) || \
00305                   (param3 != currentContext->parameter_##param3[ \
00306                     currentContext->currentUnit]) \
00307                || FORCE_STATE_CHANGE) \
00308     { \
00309       ActivateTU (); \
00310       currentContext->parameter_##param1[currentContext->currentUnit] = param1;  \
00311       currentContext->parameter_##param2[currentContext->currentUnit] = param2;  \
00312       currentContext->parameter_##param3[currentContext->currentUnit] = param3;  \
00313       func (param1, param2, param3); \
00314     } \
00315   } \
00316   void Get##name (type1 &param1, type2 & param2, type3 & param3) const\
00317   { \
00318     param1 = currentContext->parameter_##param1[currentContext->currentUnit];  \
00319     param2 = currentContext->parameter_##param2[currentContext->currentUnit];  \
00320     param3 = currentContext->parameter_##param3[currentContext->currentUnit];  \
00321   }
00322 
00323 
00324 #define DECLARE_CACHED_PARAMETER_4_LAYER(func, name, type1, param1, \
00325   type2, param2, type3, param3, type4, param4) \
00326   type1 parameter_##param1[CS_GL_MAX_LAYER]; \
00327   type2 parameter_##param2[CS_GL_MAX_LAYER]; \
00328   type3 parameter_##param3[CS_GL_MAX_LAYER]; \
00329   type4 parameter_##param4[CS_GL_MAX_LAYER];
00330 
00331 #define IMPLEMENT_CACHED_PARAMETER_4_LAYER(func, name, type1, param1, \
00332     type2, param2, type3, param3, type4, param4) \
00333   void Set##name (type1 param1, type2 param2, type3 param3, type4 param4, \
00334     bool forced = false) \
00335   { \
00336     if (forced || (param1 != currentContext->parameter_##param1[ \
00337                       currentContext->currentUnit]) || \
00338                   (param2 != currentContext->parameter_##param2[ \
00339                       currentContext->currentUnit]) || \
00340                   (param3 != currentContext->parameter_##param3[ \
00341                       currentContext->currentUnit]) || \
00342                   (param4 != currentContext->parameter_##param4[ \
00343                       currentContext->currentUnit]) \
00344               || FORCE_STATE_CHANGE) \
00345     { \
00346       ActivateTU (); \
00347       currentContext->parameter_##param1[currentContext->currentUnit] = param1;  \
00348       currentContext->parameter_##param2[currentContext->currentUnit] = param2;  \
00349       currentContext->parameter_##param3[currentContext->currentUnit] = param3;  \
00350       currentContext->parameter_##param4[currentContext->currentUnit] = param4;  \
00351       func (param1, param2, param3, param4); \
00352     } \
00353   } \
00354   void Get##name (type1 &param1, type2 & param2, type3 & param3, type4& param4) const\
00355   { \
00356     param1 = currentContext->parameter_##param1[currentContext->currentUnit];  \
00357     param2 = currentContext->parameter_##param2[currentContext->currentUnit];  \
00358     param3 = currentContext->parameter_##param3[currentContext->currentUnit];  \
00359     param4 = currentContext->parameter_##param4[currentContext->currentUnit];  \
00360   }
00361 
00362 
00363 CS_CSPLUGINCOMMON_GL_EXPORT class csGLStateCacheContext
00364 {
00365 public:
00366   csGLExtensionManager* extmgr;
00367 
00368   // Special caches
00369   GLuint boundtexture[CS_GL_MAX_LAYER]; // 32 max texture layers
00370   int currentUnit, activeUnit;
00371   GLuint currentBufferID, currentIndexID;
00372 
00373   // Standardized caches
00374   DECLARE_CACHED_BOOL (GL_DEPTH_TEST)
00375   DECLARE_CACHED_BOOL (GL_BLEND)
00376   DECLARE_CACHED_BOOL (GL_DITHER)
00377   DECLARE_CACHED_BOOL (GL_STENCIL_TEST)
00378   DECLARE_CACHED_BOOL (GL_CULL_FACE)
00379   DECLARE_CACHED_BOOL (GL_POLYGON_OFFSET_FILL)
00380   DECLARE_CACHED_BOOL (GL_LIGHTING)
00381   DECLARE_CACHED_BOOL (GL_ALPHA_TEST)
00382   DECLARE_CACHED_BOOL (GL_SCISSOR_TEST)
00383   DECLARE_CACHED_BOOL (GL_TEXTURE_GEN_S)
00384   DECLARE_CACHED_BOOL (GL_TEXTURE_GEN_T)
00385   DECLARE_CACHED_BOOL (GL_TEXTURE_GEN_R)
00386   DECLARE_CACHED_BOOL (GL_FOG)
00387   DECLARE_CACHED_BOOL (GL_COLOR_SUM_EXT)
00388   DECLARE_CACHED_BOOL_CURRENTLAYER (GL_TEXTURE_1D)
00389   DECLARE_CACHED_BOOL_CURRENTLAYER (GL_TEXTURE_2D)
00390   DECLARE_CACHED_BOOL_CURRENTLAYER (GL_TEXTURE_3D)
00391   DECLARE_CACHED_BOOL_CURRENTLAYER (GL_TEXTURE_CUBE_MAP)
00392   DECLARE_CACHED_BOOL_CURRENTLAYER (GL_TEXTURE_RECTANGLE_ARB)
00393   DECLARE_CACHED_PARAMETER_2 (glAlphaFunc, AlphaFunc, GLenum, alpha_func, GLclampf, alpha_ref)
00394   DECLARE_CACHED_PARAMETER_2 (glBlendFunc, BlendFunc, GLenum, blend_source, GLenum, blend_destination)
00395   DECLARE_CACHED_PARAMETER_1 (glCullFace, CullFace, GLenum, cull_mode)
00396   DECLARE_CACHED_PARAMETER_1 (glDepthFunc, DepthFunc, GLenum, depth_func)
00397   DECLARE_CACHED_PARAMETER_1 (glDepthMask, DepthMask, GLboolean, depth_mask)
00398   DECLARE_CACHED_PARAMETER_1 (glShadeModel, ShadeModel, GLenum, shade_model)
00399   DECLARE_CACHED_PARAMETER_3 (glStencilFunc, StencilFunc, GLenum, stencil_func, GLint, stencil_ref, GLuint, stencil_mask)
00400   DECLARE_CACHED_PARAMETER_3 (glStencilOp, StencilOp, GLenum, stencil_fail, GLenum, stencil_zfail, GLenum, stencil_zpass)
00401   DECLARE_CACHED_PARAMETER_1 (glStencilMask, StencilMask, GLuint, maskl)
00402   DECLARE_CACHED_PARAMETER_4 (glColorMask, ColorMask, GLboolean, wmRed, \
00403     GLboolean, wmGreen, GLboolean, wmBlue, GLboolean, wmAlpha)
00404 
00405   DECLARE_CACHED_CLIENT_STATE (GL_VERTEX_ARRAY)
00406   DECLARE_CACHED_CLIENT_STATE (GL_COLOR_ARRAY)
00407   DECLARE_CACHED_CLIENT_STATE (GL_SECONDARY_COLOR_ARRAY_EXT)
00408   DECLARE_CACHED_CLIENT_STATE (GL_NORMAL_ARRAY)
00409   DECLARE_CACHED_CLIENT_STATE_LAYER (GL_TEXTURE_COORD_ARRAY)
00410 
00411   DECLARE_CACHED_PARAMETER_1 (glMatrixMode, MatrixMode, GLenum, matrixMode)
00412   
00413   DECLARE_CACHED_PARAMETER_4 (glVertexPointer, VertexPointer, GLint, vsize,
00414     GLenum, vtype, GLsizei, vstride, GLvoid*, vpointer)
00415   DECLARE_CACHED_PARAMETER_3 (glNormalPointer, NormalPointer, GLenum, ntype,
00416     GLsizei, nstride, GLvoid*, npointer)
00417   DECLARE_CACHED_PARAMETER_4 (glColorPointer, ColorPointer, GLint, csize,
00418     GLenum, ctype, GLsizei, cstride, GLvoid*, cpointer)
00419   DECLARE_CACHED_PARAMETER_4 (extmgr->glSecondaryColorPointerEXT, 
00420     SecondaryColorPointerEXT, GLint, scsize, GLenum, sctype, GLsizei, scstride, 
00421     GLvoid*, scpointer);
00422   DECLARE_CACHED_PARAMETER_4_LAYER (glTexCoordPointer, TexCoordPointer, GLint, tsize,
00423     GLenum, ttype, GLsizei, tstride, GLvoid*, tpointer)
00424   
00425   csGLStateCacheContext (csGLExtensionManager* extmgr)
00426   {
00427     csGLStateCacheContext::extmgr = extmgr;
00428   }
00429 
00430 
00432   void InitCache()
00433   {
00434     int i;
00435     glGetIntegerv (GL_ALPHA_TEST_FUNC, (GLint*)&parameter_alpha_func);
00436     glGetFloatv (GL_ALPHA_TEST_REF, &parameter_alpha_ref);
00437     glGetIntegerv (GL_BLEND_SRC, (GLint*)&parameter_blend_source);
00438     glGetIntegerv (GL_BLEND_DST, (GLint*)&parameter_blend_destination);
00439     glGetIntegerv (GL_CULL_FACE_MODE, (GLint*)&parameter_cull_mode);
00440     glGetIntegerv (GL_DEPTH_FUNC, (GLint*)&parameter_depth_func);
00441     glGetBooleanv (GL_DEPTH_WRITEMASK, &parameter_depth_mask);
00442     glGetIntegerv (GL_SHADE_MODEL, (GLint*)&parameter_shade_model);
00443     glGetIntegerv (GL_STENCIL_BITS, (GLint*)&parameter_maskl);
00444     glGetIntegerv (GL_STENCIL_FUNC, (GLint*)&parameter_stencil_func);
00445     glGetIntegerv (GL_STENCIL_VALUE_MASK, (GLint*)&parameter_stencil_mask);
00446     glGetIntegerv (GL_STENCIL_REF, &parameter_stencil_ref);
00447     glGetIntegerv (GL_STENCIL_FAIL, (GLint*)&parameter_stencil_fail);
00448     glGetIntegerv (GL_STENCIL_PASS_DEPTH_FAIL, (GLint*)&parameter_stencil_zfail);
00449     glGetIntegerv (GL_STENCIL_PASS_DEPTH_PASS, (GLint*)&parameter_stencil_zpass);
00450     glGetIntegerv (GL_MATRIX_MODE, (GLint*)&parameter_matrixMode);
00451     GLboolean writemask[4];
00452     glGetBooleanv (GL_COLOR_WRITEMASK, writemask);
00453     parameter_wmRed = writemask[0];
00454     parameter_wmGreen = writemask[1];
00455     parameter_wmBlue = writemask[2];
00456     parameter_wmAlpha = writemask[3];
00457     enabled_GL_DEPTH_TEST = (glIsEnabled (GL_DEPTH_TEST) == GL_TRUE);
00458     enabled_GL_BLEND = (glIsEnabled (GL_BLEND) == GL_TRUE);
00459     enabled_GL_DITHER = (glIsEnabled (GL_DITHER) == GL_TRUE);
00460     enabled_GL_STENCIL_TEST = (glIsEnabled (GL_STENCIL_TEST) == GL_TRUE);
00461     enabled_GL_CULL_FACE = (glIsEnabled (GL_CULL_FACE) == GL_TRUE);
00462     enabled_GL_POLYGON_OFFSET_FILL = (glIsEnabled (GL_POLYGON_OFFSET_FILL) == GL_TRUE);
00463     enabled_GL_LIGHTING = (glIsEnabled (GL_LIGHTING) == GL_TRUE);
00464     enabled_GL_ALPHA_TEST = (glIsEnabled (GL_ALPHA_TEST) == GL_TRUE);
00465     enabled_GL_TEXTURE_GEN_S = (glIsEnabled (GL_TEXTURE_GEN_S) == GL_TRUE);
00466     enabled_GL_TEXTURE_GEN_T = (glIsEnabled (GL_TEXTURE_GEN_T) == GL_TRUE);
00467     enabled_GL_TEXTURE_GEN_R = (glIsEnabled (GL_TEXTURE_GEN_R) == GL_TRUE);
00468     enabled_GL_FOG = (glIsEnabled (GL_FOG) == GL_TRUE);
00469 
00470     if (extmgr->CS_GL_ARB_multitexture)
00471     {
00472       for (i = 0 ; i < CS_GL_MAX_LAYER; i++)
00473       {
00474         extmgr->glActiveTextureARB (GL_TEXTURE0_ARB + i);
00475         extmgr->glClientActiveTextureARB (GL_TEXTURE0_ARB + i);
00476         enabled_GL_TEXTURE_1D[i] = (glIsEnabled (GL_TEXTURE_1D) == GL_TRUE);
00477         enabled_GL_TEXTURE_2D[i] = (glIsEnabled (GL_TEXTURE_2D) == GL_TRUE);
00478         enabled_GL_TEXTURE_3D[i] = (glIsEnabled (GL_TEXTURE_3D) == GL_TRUE);
00479         enabled_GL_TEXTURE_CUBE_MAP[i] = (glIsEnabled (GL_TEXTURE_CUBE_MAP) == GL_TRUE);
00480         enabled_GL_TEXTURE_COORD_ARRAY[i] = (glIsEnabled (GL_TEXTURE_COORD_ARRAY) == GL_TRUE);
00481         if (extmgr->CS_GL_ARB_texture_rectangle
00482           || extmgr->CS_GL_EXT_texture_rectangle
00483           || extmgr->CS_GL_NV_texture_rectangle)
00484           enabled_GL_TEXTURE_RECTANGLE_ARB[i] = (glIsEnabled (GL_TEXTURE_RECTANGLE_ARB) == GL_TRUE);
00485         else
00486           enabled_GL_TEXTURE_RECTANGLE_ARB[i] = false;
00487         glGetIntegerv (GL_TEXTURE_COORD_ARRAY_SIZE, (GLint*)&parameter_tsize[i]);
00488         glGetIntegerv (GL_TEXTURE_COORD_ARRAY_STRIDE, (GLint*)&parameter_tstride[i]);
00489         glGetIntegerv (GL_TEXTURE_COORD_ARRAY_TYPE, (GLint*)&parameter_ttype[i]);
00490         glGetPointerv (GL_TEXTURE_COORD_ARRAY_POINTER, &parameter_tpointer[i]);
00491       }
00492     } 
00493     else 
00494     {
00495       enabled_GL_TEXTURE_1D[0] = (glIsEnabled (GL_TEXTURE_1D) == GL_TRUE);
00496       enabled_GL_TEXTURE_2D[0] = (glIsEnabled (GL_TEXTURE_2D) == GL_TRUE);
00497       enabled_GL_TEXTURE_3D[0] = (glIsEnabled (GL_TEXTURE_3D) == GL_TRUE);
00498       enabled_GL_TEXTURE_CUBE_MAP[0] = (glIsEnabled (GL_TEXTURE_CUBE_MAP) == GL_TRUE);
00499       enabled_GL_TEXTURE_COORD_ARRAY[0] = (glIsEnabled (GL_TEXTURE_COORD_ARRAY) == GL_TRUE);
00500         if (extmgr->CS_GL_ARB_texture_rectangle
00501           || extmgr->CS_GL_EXT_texture_rectangle
00502           || extmgr->CS_GL_NV_texture_rectangle)
00503           enabled_GL_TEXTURE_RECTANGLE_ARB[0] = (glIsEnabled (GL_TEXTURE_RECTANGLE_ARB) == GL_TRUE);
00504         else
00505           enabled_GL_TEXTURE_RECTANGLE_ARB[0] = false;
00506       glGetIntegerv (GL_TEXTURE_COORD_ARRAY_SIZE, (GLint*)&parameter_tsize[0]);
00507       glGetIntegerv (GL_TEXTURE_COORD_ARRAY_STRIDE, (GLint*)&parameter_tstride[0]);
00508       glGetIntegerv (GL_TEXTURE_COORD_ARRAY_TYPE, (GLint*)&parameter_ttype[0]);
00509       glGetPointerv (GL_TEXTURE_COORD_ARRAY_POINTER, &parameter_tpointer[0]);
00510       for (i = 1 ; i < CS_GL_MAX_LAYER; i++)
00511       {
00512         enabled_GL_TEXTURE_1D[i] = enabled_GL_TEXTURE_1D[0];
00513         enabled_GL_TEXTURE_2D[i] = enabled_GL_TEXTURE_2D[0];
00514         enabled_GL_TEXTURE_3D[i] = enabled_GL_TEXTURE_3D[0];
00515         enabled_GL_TEXTURE_CUBE_MAP[i] = enabled_GL_TEXTURE_CUBE_MAP[0];
00516         enabled_GL_TEXTURE_COORD_ARRAY[i] = enabled_GL_TEXTURE_COORD_ARRAY[0];
00517         enabled_GL_TEXTURE_RECTANGLE_ARB[i] = enabled_GL_TEXTURE_RECTANGLE_ARB[0];
00518         parameter_tsize[i] = parameter_tsize[0];
00519         parameter_tstride[i] = parameter_tstride[0];
00520         parameter_ttype[i] = parameter_ttype[0];
00521         parameter_tpointer[i] = parameter_tpointer[0];
00522       }
00523     }
00524     enabled_GL_SCISSOR_TEST = (glIsEnabled (GL_SCISSOR_TEST) == GL_TRUE);
00525     enabled_GL_VERTEX_ARRAY = (glIsEnabled (GL_VERTEX_ARRAY) == GL_TRUE);
00526     enabled_GL_COLOR_ARRAY = (glIsEnabled (GL_COLOR_ARRAY) == GL_TRUE);
00527     if (extmgr->CS_GL_EXT_secondary_color)
00528       enabled_GL_SECONDARY_COLOR_ARRAY_EXT = 
00529         (glIsEnabled (GL_SECONDARY_COLOR_ARRAY_EXT) == GL_TRUE);
00530     else
00531       enabled_GL_SECONDARY_COLOR_ARRAY_EXT = false;
00532     enabled_GL_NORMAL_ARRAY = (glIsEnabled (GL_NORMAL_ARRAY) == GL_TRUE);
00533 
00534     if (extmgr->CS_GL_ARB_multitexture)
00535     {
00536       extmgr->glActiveTextureARB (GL_TEXTURE0_ARB);
00537       extmgr->glClientActiveTextureARB (GL_TEXTURE0_ARB);
00538     }
00539     memset (boundtexture, 0, CS_GL_MAX_LAYER * sizeof (GLuint));
00540     currentUnit = 0;
00541     activeUnit = 0;
00542     currentBufferID = 0;
00543     currentIndexID = 0;
00544 
00545     glGetIntegerv (GL_VERTEX_ARRAY_SIZE, (GLint*)&parameter_vsize);
00546     glGetIntegerv (GL_VERTEX_ARRAY_STRIDE, (GLint*)&parameter_vstride);
00547     glGetIntegerv (GL_VERTEX_ARRAY_TYPE, (GLint*)&parameter_vtype);
00548     glGetPointerv (GL_VERTEX_ARRAY_POINTER, &parameter_vpointer);
00549 
00550     glGetIntegerv (GL_NORMAL_ARRAY_STRIDE, (GLint*)&parameter_nstride);
00551     glGetIntegerv (GL_NORMAL_ARRAY_TYPE, (GLint*)&parameter_ntype);
00552     glGetPointerv (GL_NORMAL_ARRAY_POINTER, &parameter_npointer);
00553 
00554     glGetIntegerv (GL_COLOR_ARRAY_SIZE, (GLint*)&parameter_csize);
00555     glGetIntegerv (GL_COLOR_ARRAY_STRIDE, (GLint*)&parameter_cstride);
00556     glGetIntegerv (GL_COLOR_ARRAY_TYPE, (GLint*)&parameter_ctype);
00557     glGetPointerv (GL_COLOR_ARRAY_POINTER, &parameter_cpointer);
00558     
00559     if (extmgr->CS_GL_EXT_secondary_color)
00560     {
00561       glGetIntegerv (GL_SECONDARY_COLOR_ARRAY_SIZE_EXT, 
00562         (GLint*)&parameter_scsize);
00563       glGetIntegerv (GL_SECONDARY_COLOR_ARRAY_STRIDE_EXT, 
00564         (GLint*)&parameter_scstride);
00565       glGetIntegerv (GL_SECONDARY_COLOR_ARRAY_TYPE_EXT, 
00566         (GLint*)&parameter_sctype);
00567       glGetPointerv (GL_SECONDARY_COLOR_ARRAY_POINTER_EXT, 
00568         &parameter_scpointer);
00569       enabled_GL_COLOR_SUM_EXT = glIsEnabled (GL_COLOR_SUM_EXT) != GL_FALSE;
00570     }
00571     else
00572     {
00573       parameter_scsize = 0;
00574       parameter_scstride = 0;
00575       parameter_sctype = 0;
00576       parameter_scpointer = 0;
00577       enabled_GL_COLOR_SUM_EXT = false;
00578     }
00579   }
00580 };
00581 
00582 
00593 CS_CSPLUGINCOMMON_GL_EXPORT class csGLStateCache
00594 {
00595 public:
00596   csGLExtensionManager* extmgr;
00597   csGLStateCacheContext* currentContext;
00598 
00599   csGLStateCache (csGLExtensionManager* extmgr)
00600   {
00601     csGLStateCache::extmgr = extmgr;
00602     currentContext = 0;
00603   }
00604 
00605   void SetContext (csGLStateCacheContext *context)
00606   {
00607     currentContext = context;
00608   }
00609 
00610   // Standardized caches
00611   IMPLEMENT_CACHED_BOOL (GL_DEPTH_TEST)
00612   IMPLEMENT_CACHED_BOOL (GL_BLEND)
00613   IMPLEMENT_CACHED_BOOL (GL_DITHER)
00614   IMPLEMENT_CACHED_BOOL (GL_STENCIL_TEST)
00615   IMPLEMENT_CACHED_BOOL (GL_CULL_FACE)
00616   IMPLEMENT_CACHED_BOOL (GL_POLYGON_OFFSET_FILL)
00617   IMPLEMENT_CACHED_BOOL (GL_LIGHTING)
00618   IMPLEMENT_CACHED_BOOL (GL_ALPHA_TEST)
00619   IMPLEMENT_CACHED_BOOL (GL_SCISSOR_TEST)
00620   IMPLEMENT_CACHED_BOOL (GL_TEXTURE_GEN_S)
00621   IMPLEMENT_CACHED_BOOL (GL_TEXTURE_GEN_T)
00622   IMPLEMENT_CACHED_BOOL (GL_TEXTURE_GEN_R)
00623   IMPLEMENT_CACHED_BOOL (GL_FOG)
00624   IMPLEMENT_CACHED_BOOL (GL_COLOR_SUM_EXT)
00625   IMPLEMENT_CACHED_BOOL_CURRENTLAYER (GL_TEXTURE_1D)
00626   IMPLEMENT_CACHED_BOOL_CURRENTLAYER (GL_TEXTURE_2D)
00627   IMPLEMENT_CACHED_BOOL_CURRENTLAYER (GL_TEXTURE_3D)
00628   IMPLEMENT_CACHED_BOOL_CURRENTLAYER (GL_TEXTURE_CUBE_MAP)
00629   IMPLEMENT_CACHED_BOOL_CURRENTLAYER (GL_TEXTURE_RECTANGLE_ARB)
00630   IMPLEMENT_CACHED_PARAMETER_2 (glAlphaFunc, AlphaFunc, GLenum, alpha_func, GLclampf, alpha_ref)
00631   IMPLEMENT_CACHED_PARAMETER_2 (glBlendFunc, BlendFunc, GLenum, blend_source, GLenum, blend_destination)
00632   IMPLEMENT_CACHED_PARAMETER_1 (glCullFace, CullFace, GLenum, cull_mode)
00633   IMPLEMENT_CACHED_PARAMETER_1 (glDepthFunc, DepthFunc, GLenum, depth_func)
00634   IMPLEMENT_CACHED_PARAMETER_1 (glDepthMask, DepthMask, GLboolean, depth_mask)
00635   IMPLEMENT_CACHED_PARAMETER_1 (glShadeModel, ShadeModel, GLenum, shade_model)
00636   IMPLEMENT_CACHED_PARAMETER_3 (glStencilFunc, StencilFunc, GLenum, stencil_func, GLint, stencil_ref, GLuint, stencil_mask)
00637   IMPLEMENT_CACHED_PARAMETER_3 (glStencilOp, StencilOp, GLenum, stencil_fail, GLenum, stencil_zfail, GLenum, stencil_zpass)
00638   IMPLEMENT_CACHED_PARAMETER_1 (glStencilMask, StencilMask, GLuint, maskl)
00639   IMPLEMENT_CACHED_PARAMETER_4 (glColorMask, ColorMask, GLboolean, wmRed, \
00640     GLboolean, wmGreen, GLboolean, wmBlue, GLboolean, wmAlpha)
00641 
00642   IMPLEMENT_CACHED_CLIENT_STATE (GL_VERTEX_ARRAY)
00643   IMPLEMENT_CACHED_CLIENT_STATE (GL_COLOR_ARRAY)
00644   IMPLEMENT_CACHED_CLIENT_STATE (GL_SECONDARY_COLOR_ARRAY_EXT)
00645   IMPLEMENT_CACHED_CLIENT_STATE (GL_NORMAL_ARRAY)
00646   IMPLEMENT_CACHED_CLIENT_STATE_LAYER (GL_TEXTURE_COORD_ARRAY)
00647 
00648   IMPLEMENT_CACHED_PARAMETER_1 (glMatrixMode, MatrixMode, GLenum, matrixMode)
00649   
00650   IMPLEMENT_CACHED_PARAMETER_4 (glVertexPointer, VertexPointer, GLint, vsize,
00651     GLenum, vtype, GLsizei, vstride, GLvoid*, vpointer);
00652   IMPLEMENT_CACHED_PARAMETER_3 (glNormalPointer, NormalPointer, GLenum, ntype,
00653     GLsizei, nstride, GLvoid*, npointer);
00654   IMPLEMENT_CACHED_PARAMETER_4 (glColorPointer, ColorPointer, GLint, csize,
00655     GLenum, ctype, GLsizei, cstride, GLvoid*, cpointer);
00656   IMPLEMENT_CACHED_PARAMETER_4 (extmgr->glSecondaryColorPointerEXT, 
00657     SecondaryColorPointerExt, GLint, scsize, GLenum, sctype, GLsizei, scstride, 
00658     GLvoid*, scpointer);
00659   IMPLEMENT_CACHED_PARAMETER_4_LAYER (glTexCoordPointer, TexCoordPointer, GLint, tsize,
00660     GLenum, ttype, GLsizei, tstride, GLvoid*, tpointer);
00661   
00662   // Special caches
00663   void SetTexture (GLenum target, GLuint texture)
00664   {
00665     if (texture != currentContext->boundtexture[currentContext->currentUnit])
00666     {
00667       ActivateTU ();
00668       currentContext->boundtexture[currentContext->currentUnit] = texture;
00669       glBindTexture (target, texture);
00670     }
00671   }
00672   GLuint GetTexture (GLenum /*target*/)
00673   {
00674     return currentContext->boundtexture[currentContext->currentUnit];
00675   }
00676   GLuint GetTexture (GLenum /*target*/, int unit)
00677   {
00678     return currentContext->boundtexture[unit];
00679   }
00684   void SetActiveTU (int unit)
00685   {
00686     currentContext->currentUnit = unit;   
00687   }
00688   int GetActiveTU ()
00689   {
00690     return currentContext->currentUnit;
00691   }
00692   void ActivateTU ()
00693   {
00694     if (currentContext->activeUnit != currentContext->currentUnit && extmgr->CS_GL_ARB_multitexture)
00695     {
00696       extmgr->glActiveTextureARB (GL_TEXTURE0_ARB + currentContext->currentUnit);
00697       extmgr->glClientActiveTextureARB (GL_TEXTURE0_ARB + currentContext->currentUnit);
00698     }
00699     currentContext->activeUnit = currentContext->currentUnit;
00700   }
00701 
00702   //VBO buffers
00703   void SetBufferARB (GLenum target, GLuint id)
00704   {
00705     if (target == GL_ELEMENT_ARRAY_BUFFER_ARB)
00706     {
00707       if (id != currentContext->currentIndexID)
00708       {
00709         extmgr->glBindBufferARB (target, id);
00710         currentContext->currentIndexID = id;
00711       }
00712     } 
00713     else 
00714     {
00715       if (id != currentContext->currentBufferID)
00716       {
00717         extmgr->glBindBufferARB (target, id);
00718         currentContext->currentBufferID = id;
00719         currentContext->parameter_vpointer = (GLvoid*)~0; //invalidate vertexpointer
00720         currentContext->parameter_npointer = (GLvoid*)~0; //invalidate vertexpointer
00721         currentContext->parameter_cpointer = (GLvoid*)~0; //invalidate vertexpointer
00722         memset(&currentContext->parameter_tpointer, ~0, sizeof(GLvoid*)*CS_GL_MAX_LAYER);
00723       }
00724     }
00725   }
00726 
00727   GLuint GetBufferARB (GLenum target)
00728   {
00729     if (target == GL_ELEMENT_ARRAY_BUFFER_ARB)
00730     {
00731       return currentContext->currentIndexID;
00732     } 
00733     else 
00734     {
00735       return currentContext->currentBufferID;
00736     }
00737   }
00738 };
00739 
00740 #undef IMPLEMENT_CACHED_BOOL
00741 #undef IMPLEMENT_CACHED_BOOL_CURRENTLAYER
00742 #undef IMPLEMENT_CACHED_PARAMETER_1
00743 #undef IMPLEMENT_CACHED_PARAMETER_2
00744 #undef IMPLEMENT_CACHED_PARAMETER_3
00745 #undef IMPLEMENT_CACHED_PARAMETER_4
00746 #undef IMPLEMENT_CACHED_PARAMETER_1_LAYER
00747 #undef IMPLEMENT_CACHED_PARAMETER_2_LAYER
00748 #undef IMPLEMENT_CACHED_PARAMETER_3_LAYER
00749 #undef IMPLEMENT_CACHED_PARAMETER_4_LAYER
00750 #undef IMPLEMENT_CACHED_CLIENT_STATE
00751 #undef IMPLEMENT_CACHED_CLIENT_STATE_LAYER
00752 
00753 #undef DECLARE_CACHED_BOOL
00754 #undef DECLARE_CACHED_BOOL_CURRENTLAYER
00755 #undef DECLARE_CACHED_PARAMETER_1
00756 #undef DECLARE_CACHED_PARAMETER_2
00757 #undef DECLARE_CACHED_PARAMETER_3
00758 #undef DECLARE_CACHED_PARAMETER_4
00759 #undef DECLARE_CACHED_PARAMETER_1_LAYER
00760 #undef DECLARE_CACHED_PARAMETER_2_LAYER
00761 #undef DECLARE_CACHED_PARAMETER_3_LAYER
00762 #undef DECLARE_CACHED_PARAMETER_4_LAYER
00763 #undef DECLARE_CACHED_CLIENT_STATE
00764 #undef DECLARE_CACHED_CLIENT_STATE_LAYER
00765 
00766 #undef FORCE_STATE_CHANGE
00767 
00770 #endif // __CS_GLSTATES_H__

Generated for Crystal Space by doxygen 1.4.6