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 
00070 #define CS_GL_MAX_LAYER 16
00071 
00072 #define DECLARE_CACHED_BOOL_CURRENTLAYER(name) \
00073   bool enabled_##name[CS_GL_MAX_LAYER];
00074 
00075 #define IMPLEMENT_CACHED_BOOL_CURRENTLAYER(name)                              \
00076   void Enable_##name ()                                                       \
00077   {                                                                           \
00078     const int currentUnit = currentContext->currentUnit;                      \
00079     if (!currentContext->enabled_##name[currentUnit] || FORCE_STATE_CHANGE)   \
00080     {                                                                         \
00081       ActivateTU (activateTexEnable);                                         \
00082       currentContext->enabled_##name[currentUnit] = true;                     \
00083       glEnable (name);                                                        \
00084     }                                                                         \
00085   }                                                                           \
00086   void Disable_##name ()                                                      \
00087   {                                                                           \
00088     const int currentUnit = currentContext->currentUnit;                      \
00089     if (currentContext->enabled_##name[currentUnit] || FORCE_STATE_CHANGE)    \
00090     {                                                                         \
00091       ActivateTU (activateTexEnable);                                         \
00092       currentContext->enabled_##name[currentUnit] = false;                    \
00093       glDisable (name);                                                       \
00094     }                                                                         \
00095   }                                                                           \
00096   bool IsEnabled_##name () const                                              \
00097   {                                                                           \
00098     const int currentUnit = currentContext->currentUnit;                      \
00099     return currentContext->enabled_##name[currentUnit];                       \
00100   }
00101 
00102 #define DECLARE_CACHED_PARAMETER_1(func, name, type1, param1) \
00103   type1 parameter_##param1;
00104 
00105 #define IMPLEMENT_CACHED_PARAMETER_1(func, name, type1, param1) \
00106   void Set##name (type1 param1, bool forced = false) \
00107   { \
00108     if (forced || (param1 != currentContext->parameter_##param1) || FORCE_STATE_CHANGE) \
00109     { \
00110       currentContext->parameter_##param1 = param1;  \
00111       func (param1); \
00112     } \
00113   } \
00114   void Get##name (type1 & param1) const\
00115   { \
00116     param1 = currentContext->parameter_##param1;  \
00117   }
00118 
00119 #define DECLARE_CACHED_PARAMETER_2(func, name, type1, param1, type2, param2) \
00120   type1 parameter_##param1; \
00121   type2 parameter_##param2;
00122 
00123 #define IMPLEMENT_CACHED_PARAMETER_2(func, name, type1, param1, type2, param2) \
00124   void Set##name (type1 param1, type2 param2, bool forced = false) \
00125   { \
00126     if (forced || (param1 != currentContext->parameter_##param1) || \
00127         (param2 != currentContext->parameter_##param2) || FORCE_STATE_CHANGE) \
00128     { \
00129       currentContext->parameter_##param1 = param1;  \
00130       currentContext->parameter_##param2 = param2;  \
00131       func (param1, param2); \
00132     } \
00133   } \
00134   void Get##name (type1 & param1, type2 & param2) const\
00135   { \
00136     param1 = currentContext->parameter_##param1;  \
00137     param2 = currentContext->parameter_##param2;  \
00138   }
00139 
00140 #define DECLARE_CACHED_PARAMETER_3(func, name, type1, param1, type2, param2, type3, param3) \
00141   type1 parameter_##param1; \
00142   type2 parameter_##param2; \
00143   type3 parameter_##param3;
00144 
00145 #define IMPLEMENT_CACHED_PARAMETER_3(func, name, type1, param1, type2, param2, type3, param3) \
00146   void Set##name (type1 param1, type2 param2, type3 param3, bool forced = false) \
00147   { \
00148     if (forced || (param1 != currentContext->parameter_##param1) \
00149         || (param2 != currentContext->parameter_##param2) \
00150         || (param3 != currentContext->parameter_##param3) || FORCE_STATE_CHANGE) \
00151     { \
00152       currentContext->parameter_##param1 = param1;  \
00153       currentContext->parameter_##param2 = param2;  \
00154       currentContext->parameter_##param3 = param3;  \
00155       func (param1, param2, param3); \
00156     } \
00157   } \
00158   void Get##name (type1 &param1, type2 & param2, type3 & param3) const\
00159   { \
00160     param1 = currentContext->parameter_##param1;  \
00161     param2 = currentContext->parameter_##param2;  \
00162     param3 = currentContext->parameter_##param3;  \
00163   }
00164 
00165 #define DECLARE_CACHED_PARAMETER_4(func, name, type1, param1, \
00166   type2, param2, type3, param3, type4, param4) \
00167   type1 parameter_##param1; \
00168   type2 parameter_##param2; \
00169   type3 parameter_##param3; \
00170   type4 parameter_##param4;
00171 
00172 #define IMPLEMENT_CACHED_PARAMETER_4(func, name, type1, param1, \
00173     type2, param2, type3, param3, type4, param4) \
00174   void Set##name (type1 param1, type2 param2, type3 param3, type4 param4, \
00175     bool forced = false) \
00176   { \
00177     if (forced || (param1 != currentContext->parameter_##param1) || \
00178       (param2 != currentContext->parameter_##param2) || \
00179       (param3 != currentContext->parameter_##param3) || \
00180       (param4 != currentContext->parameter_##param4) || FORCE_STATE_CHANGE) \
00181     { \
00182       currentContext->parameter_##param1 = param1;  \
00183       currentContext->parameter_##param2 = param2;  \
00184       currentContext->parameter_##param3 = param3;  \
00185       currentContext->parameter_##param4 = param4;  \
00186       func (param1, param2, param3, param4); \
00187     } \
00188   } \
00189   void Get##name (type1 &param1, type2 & param2, type3 & param3, type4& param4) const\
00190   { \
00191     param1 = currentContext->parameter_##param1;  \
00192     param2 = currentContext->parameter_##param2;  \
00193     param3 = currentContext->parameter_##param3;  \
00194     param4 = currentContext->parameter_##param4;  \
00195   }
00196 
00197 #define DECLARE_CACHED_CLIENT_STATE(name)             \
00198   bool enabled_##name;
00199 
00200 #define IMPLEMENT_CACHED_CLIENT_STATE(name)           \
00201   void Enable_##name () \
00202   { \
00203     if (!currentContext->enabled_##name || FORCE_STATE_CHANGE) \
00204     { \
00205       currentContext->enabled_##name = true;  \
00206       glEnableClientState (name); \
00207     } \
00208   } \
00209   void Disable_##name () \
00210   { \
00211     if (currentContext->enabled_##name || FORCE_STATE_CHANGE) { \
00212       currentContext->enabled_##name = false;  \
00213       glDisableClientState (name); \
00214     } \
00215   } \
00216   bool IsEnabled_##name () const \
00217   { \
00218     return currentContext->enabled_##name; \
00219   }
00220 
00221 #define DECLARE_CACHED_CLIENT_STATE_LAYER(name)                               \
00222   bool enabled_##name[CS_GL_MAX_LAYER];
00223 
00224 #define IMPLEMENT_CACHED_CLIENT_STATE_LAYER(name)                             \
00225   void Enable_##name ()                                                       \
00226   {                                                                           \
00227     const int currentUnit = currentContext->currentUnit;                      \
00228     if (!currentContext->enabled_##name[currentUnit] || FORCE_STATE_CHANGE)   \
00229     {                                                                         \
00230       ActivateTU (activateTexCoord);                                          \
00231       currentContext->enabled_##name[currentUnit] = true;                     \
00232       glEnableClientState (name);                                             \
00233     }                                                                         \
00234   }                                                                           \
00235   void Disable_##name ()                                                      \
00236   {                                                                           \
00237     const int currentUnit = currentContext->currentUnit;                      \
00238     if (currentContext->enabled_##name[currentUnit] || FORCE_STATE_CHANGE)    \
00239     {                                                                         \
00240       ActivateTU (activateTexCoord);                                          \
00241       currentContext->enabled_##name[currentUnit] = false;                    \
00242       glDisableClientState (name);                                            \
00243     }                                                                         \
00244   }                                                                           \
00245   bool IsEnabled_##name () const                                              \
00246   {                                                                           \
00247     const int currentUnit = currentContext->currentUnit;                      \
00248     return currentContext->enabled_##name[currentUnit];                       \
00249   }
00250 
00251 #define DECLARE_CACHED_PARAMETER_1_LAYER(func, name, type1, param1) \
00252   type1 parameter_##param1[CS_GL_MAX_LAYER];
00253 
00254 #define IMPLEMENT_CACHED_PARAMETER_1_LAYER(func, name, type1, param1) \
00255   void Set##name (type1 param1, bool forced = false) \
00256   { \
00257     if (forced || \
00258         (param1 != currentContext->parameter_##param1[currentContext->currentUnit] \
00259         || FORCE_STATE_CHANGE)) \
00260     { \
00261       ActivateTU (); \
00262       currentContext->parameter_##param1[currentContext->currentUnit] = param1;  \
00263       func (param1); \
00264     } \
00265   } \
00266   void Get##name (type1 &param1) const\
00267   { \
00268     param1 = currentContext->parameter_##param1[currentContext->currentUnit];  \
00269   }
00270 
00271 #define DECLARE_CACHED_PARAMETER_2_LAYER(func, name, type1, param1, \
00272   type2, param2) \
00273   type1 parameter_##param1[CS_GL_MAX_LAYER]; \
00274   type2 parameter_##param2[CS_GL_MAX_LAYER];
00275 
00276 #define IMPLEMENT_CACHED_PARAMETER_2_LAYER(func, name, type1, param1, \
00277   type2, param2) \
00278   void Set##name (type1 param1, type2 param2, bool forced = false) \
00279   { \
00280     if (forced || (param1 != currentContext->parameter_##param1[ \
00281                     currentContext->currentUnit]) || \
00282                   (param2 != currentContext->parameter_##param2[ \
00283                     currentContext->currentUnit]) \
00284                || FORCE_STATE_CHANGE) \
00285     { \
00286       ActivateTU (); \
00287       currentContext->parameter_##param1[currentContext->currentUnit] = param1;  \
00288       currentContext->parameter_##param2[currentContext->currentUnit] = param2;  \
00289       func (param1, param2); \
00290     } \
00291   } \
00292   void Get##name (type1 &param1, type2 & param2) const\
00293   { \
00294     param1 = currentContext->parameter_##param1[currentContext->currentUnit];  \
00295     param2 = currentContext->parameter_##param2[currentContext->currentUnit];  \
00296   }
00297 
00298 #define DECLARE_CACHED_PARAMETER_3_LAYER(func, name, type1, param1, \
00299   type2, param2, type3, param3) \
00300   type1 parameter_##param1[CS_GL_MAX_LAYER]; \
00301   type2 parameter_##param2[CS_GL_MAX_LAYER]; \
00302   type3 parameter_##param3[CS_GL_MAX_LAYER];
00303 
00304 
00305 #define IMPLEMENT_CACHED_PARAMETER_3_LAYER(func, name, type1, param1, \
00306   type2, param2, type3, param3) \
00307   void Set##name (type1 param1, type2 param2, type3 param3,\
00308     bool forced = false) \
00309   { \
00310     if (forced || (param1 != currentContext->parameter_##param1[ \
00311                     currentContext->currentUnit]) || \
00312                   (param2 != currentContext->parameter_##param2[ \
00313                     currentContext->currentUnit]) || \
00314                   (param3 != currentContext->parameter_##param3[ \
00315                     currentContext->currentUnit]) \
00316                || FORCE_STATE_CHANGE) \
00317     { \
00318       ActivateTU (); \
00319       currentContext->parameter_##param1[currentContext->currentUnit] = param1;  \
00320       currentContext->parameter_##param2[currentContext->currentUnit] = param2;  \
00321       currentContext->parameter_##param3[currentContext->currentUnit] = param3;  \
00322       func (param1, param2, param3); \
00323     } \
00324   } \
00325   void Get##name (type1 &param1, type2 & param2, type3 & param3) const\
00326   { \
00327     param1 = currentContext->parameter_##param1[currentContext->currentUnit];  \
00328     param2 = currentContext->parameter_##param2[currentContext->currentUnit];  \
00329     param3 = currentContext->parameter_##param3[currentContext->currentUnit];  \
00330   }
00331 
00332 
00333 #define DECLARE_CACHED_PARAMETER_4_LAYER(func, name, type1, param1,           \
00334   type2, param2, type3, param3, type4, param4)                                \
00335   type1 parameter_##param1[CS_GL_MAX_LAYER];                                  \
00336   type2 parameter_##param2[CS_GL_MAX_LAYER];                                  \
00337   type3 parameter_##param3[CS_GL_MAX_LAYER];                                  \
00338   type4 parameter_##param4[CS_GL_MAX_LAYER];
00339 
00340 #define IMPLEMENT_CACHED_PARAMETER_4_LAYER(func, name, type1, param1,         \
00341     type2, param2, type3, param3, type4, param4)                              \
00342   void Set##name (type1 param1, type2 param2, type3 param3, type4 param4,     \
00343     bool forced = false)                                                      \
00344   {                                                                           \
00345     const int currentUnit = currentContext->currentUnit;                      \
00346     if (forced                                                                \
00347       || (param1 != currentContext->parameter_##param1[currentUnit])          \
00348       || (param2 != currentContext->parameter_##param2[currentUnit])          \
00349       || (param3 != currentContext->parameter_##param3[currentUnit])          \
00350       || (param4 != currentContext->parameter_##param4[currentUnit])          \
00351       || FORCE_STATE_CHANGE)                                                  \
00352     {                                                                         \
00353       ActivateTU (activateTexCoord);                                          \
00354       currentContext->parameter_##param1[currentUnit] = param1;               \
00355       currentContext->parameter_##param2[currentUnit] = param2;               \
00356       currentContext->parameter_##param3[currentUnit] = param3;               \
00357       currentContext->parameter_##param4[currentUnit] = param4;               \
00358       func (param1, param2, param3, param4);                                  \
00359     }                                                                         \
00360   }                                                                           \
00361   void Get##name (type1 &param1, type2 & param2, type3 & param3,              \
00362     type4& param4) const                                                      \
00363   {                                                                           \
00364     const int currentUnit = currentContext->currentUnit;                      \
00365     param1 = currentContext->parameter_##param1[currentUnit];                 \
00366     param2 = currentContext->parameter_##param2[currentUnit];                 \
00367     param3 = currentContext->parameter_##param3[currentUnit];                 \
00368     param4 = currentContext->parameter_##param4[currentUnit];                 \
00369   }
00370 
00371 
00372 class CS_CSPLUGINCOMMON_GL_EXPORT csGLStateCacheContext
00373 {
00374 public:
00375   csGLExtensionManager* extmgr;
00376 
00377   // Special caches
00378   GLuint boundtexture[CS_GL_MAX_LAYER]; // 32 max texture layers
00379   int currentUnit;
00380   int activeUnit[2];
00381   GLuint currentBufferID, currentIndexID;
00382 
00383   // Standardized caches
00384   DECLARE_CACHED_BOOL (GL_DEPTH_TEST)
00385   DECLARE_CACHED_BOOL (GL_BLEND)
00386   DECLARE_CACHED_BOOL (GL_DITHER)
00387   DECLARE_CACHED_BOOL (GL_STENCIL_TEST)
00388   DECLARE_CACHED_BOOL (GL_CULL_FACE)
00389   DECLARE_CACHED_BOOL (GL_POLYGON_OFFSET_FILL)
00390   DECLARE_CACHED_BOOL (GL_LIGHTING)
00391   DECLARE_CACHED_BOOL (GL_ALPHA_TEST)
00392   DECLARE_CACHED_BOOL (GL_SCISSOR_TEST)
00393   DECLARE_CACHED_BOOL (GL_TEXTURE_GEN_S)
00394   DECLARE_CACHED_BOOL (GL_TEXTURE_GEN_T)
00395   DECLARE_CACHED_BOOL (GL_TEXTURE_GEN_R)
00396   DECLARE_CACHED_BOOL (GL_TEXTURE_GEN_Q)
00397   DECLARE_CACHED_BOOL (GL_FOG)
00398   DECLARE_CACHED_BOOL (GL_COLOR_SUM_EXT)
00399   DECLARE_CACHED_BOOL_CURRENTLAYER (GL_TEXTURE_1D)
00400   DECLARE_CACHED_BOOL_CURRENTLAYER (GL_TEXTURE_2D)
00401   DECLARE_CACHED_BOOL_CURRENTLAYER (GL_TEXTURE_3D)
00402   DECLARE_CACHED_BOOL_CURRENTLAYER (GL_TEXTURE_CUBE_MAP)
00403   DECLARE_CACHED_BOOL_CURRENTLAYER (GL_TEXTURE_RECTANGLE_ARB)
00404   DECLARE_CACHED_PARAMETER_2 (glAlphaFunc, AlphaFunc, GLenum, alpha_func, GLclampf, alpha_ref)
00405   DECLARE_CACHED_PARAMETER_2 (glBlendFunc, BlendFunc, GLenum, blend_source, GLenum, blend_destination)
00406   DECLARE_CACHED_PARAMETER_1 (glCullFace, CullFace, GLenum, cull_mode)
00407   DECLARE_CACHED_PARAMETER_1 (glDepthFunc, DepthFunc, GLenum, depth_func)
00408   DECLARE_CACHED_PARAMETER_1 (glDepthMask, DepthMask, GLboolean, depth_mask)
00409   DECLARE_CACHED_PARAMETER_1 (glShadeModel, ShadeModel, GLenum, shade_model)
00410   DECLARE_CACHED_PARAMETER_3 (glStencilFunc, StencilFunc, GLenum, stencil_func, GLint, stencil_ref, GLuint, stencil_mask)
00411   DECLARE_CACHED_PARAMETER_3 (glStencilOp, StencilOp, GLenum, stencil_fail, GLenum, stencil_zfail, GLenum, stencil_zpass)
00412   DECLARE_CACHED_PARAMETER_1 (glStencilMask, StencilMask, GLuint, maskl)
00413   DECLARE_CACHED_PARAMETER_4 (glColorMask, ColorMask, GLboolean, wmRed, \
00414     GLboolean, wmGreen, GLboolean, wmBlue, GLboolean, wmAlpha)
00415 
00416   DECLARE_CACHED_CLIENT_STATE (GL_VERTEX_ARRAY)
00417   DECLARE_CACHED_CLIENT_STATE (GL_COLOR_ARRAY)
00418   DECLARE_CACHED_CLIENT_STATE (GL_SECONDARY_COLOR_ARRAY_EXT)
00419   DECLARE_CACHED_CLIENT_STATE (GL_NORMAL_ARRAY)
00420   DECLARE_CACHED_CLIENT_STATE_LAYER (GL_TEXTURE_COORD_ARRAY)
00421 
00422   DECLARE_CACHED_PARAMETER_1 (glMatrixMode, MatrixMode, GLenum, matrixMode)
00423   
00424   DECLARE_CACHED_PARAMETER_4 (glVertexPointer, VertexPointer, GLint, vsize,
00425     GLenum, vtype, GLsizei, vstride, GLvoid*, vpointer)
00426   DECLARE_CACHED_PARAMETER_3 (glNormalPointer, NormalPointer, GLenum, ntype,
00427     GLsizei, nstride, GLvoid*, npointer)
00428   DECLARE_CACHED_PARAMETER_4 (glColorPointer, ColorPointer, GLint, csize,
00429     GLenum, ctype, GLsizei, cstride, GLvoid*, cpointer)
00430   DECLARE_CACHED_PARAMETER_4 (extmgr->glSecondaryColorPointerEXT, 
00431     SecondaryColorPointerEXT, GLint, scsize, GLenum, sctype, GLsizei, scstride, 
00432     GLvoid*, scpointer);
00433   DECLARE_CACHED_PARAMETER_4_LAYER (glTexCoordPointer, TexCoordPointer, GLint, tsize,
00434     GLenum, ttype, GLsizei, tstride, GLvoid*, tpointer)
00435   
00436   csGLStateCacheContext (csGLExtensionManager* extmgr)
00437   {
00438     csGLStateCacheContext::extmgr = extmgr;
00439   }
00440 
00441 
00443   void InitCache()
00444   {
00445     int i;
00446     glGetIntegerv (GL_ALPHA_TEST_FUNC, (GLint*)&parameter_alpha_func);
00447     glGetFloatv (GL_ALPHA_TEST_REF, &parameter_alpha_ref);
00448     glGetIntegerv (GL_BLEND_SRC, (GLint*)&parameter_blend_source);
00449     glGetIntegerv (GL_BLEND_DST, (GLint*)&parameter_blend_destination);
00450     glGetIntegerv (GL_CULL_FACE_MODE, (GLint*)&parameter_cull_mode);
00451     glGetIntegerv (GL_DEPTH_FUNC, (GLint*)&parameter_depth_func);
00452     glGetBooleanv (GL_DEPTH_WRITEMASK, &parameter_depth_mask);
00453     glGetIntegerv (GL_SHADE_MODEL, (GLint*)&parameter_shade_model);
00454     glGetIntegerv (GL_STENCIL_BITS, (GLint*)&parameter_maskl);
00455     glGetIntegerv (GL_STENCIL_FUNC, (GLint*)&parameter_stencil_func);
00456     glGetIntegerv (GL_STENCIL_VALUE_MASK, (GLint*)&parameter_stencil_mask);
00457     glGetIntegerv (GL_STENCIL_REF, &parameter_stencil_ref);
00458     glGetIntegerv (GL_STENCIL_FAIL, (GLint*)&parameter_stencil_fail);
00459     glGetIntegerv (GL_STENCIL_PASS_DEPTH_FAIL, (GLint*)&parameter_stencil_zfail);
00460     glGetIntegerv (GL_STENCIL_PASS_DEPTH_PASS, (GLint*)&parameter_stencil_zpass);
00461     glGetIntegerv (GL_MATRIX_MODE, (GLint*)&parameter_matrixMode);
00462     GLboolean writemask[4];
00463     glGetBooleanv (GL_COLOR_WRITEMASK, writemask);
00464     parameter_wmRed = writemask[0];
00465     parameter_wmGreen = writemask[1];
00466     parameter_wmBlue = writemask[2];
00467     parameter_wmAlpha = writemask[3];
00468     enabled_GL_DEPTH_TEST = (glIsEnabled (GL_DEPTH_TEST) == GL_TRUE);
00469     enabled_GL_BLEND = (glIsEnabled (GL_BLEND) == GL_TRUE);
00470     enabled_GL_DITHER = (glIsEnabled (GL_DITHER) == GL_TRUE);
00471     enabled_GL_STENCIL_TEST = (glIsEnabled (GL_STENCIL_TEST) == GL_TRUE);
00472     enabled_GL_CULL_FACE = (glIsEnabled (GL_CULL_FACE) == GL_TRUE);
00473     enabled_GL_POLYGON_OFFSET_FILL = (glIsEnabled (GL_POLYGON_OFFSET_FILL) == GL_TRUE);
00474     enabled_GL_LIGHTING = (glIsEnabled (GL_LIGHTING) == GL_TRUE);
00475     enabled_GL_ALPHA_TEST = (glIsEnabled (GL_ALPHA_TEST) == GL_TRUE);
00476     enabled_GL_TEXTURE_GEN_S = (glIsEnabled (GL_TEXTURE_GEN_S) == GL_TRUE);
00477     enabled_GL_TEXTURE_GEN_T = (glIsEnabled (GL_TEXTURE_GEN_T) == GL_TRUE);
00478     enabled_GL_TEXTURE_GEN_R = (glIsEnabled (GL_TEXTURE_GEN_R) == GL_TRUE);
00479     enabled_GL_TEXTURE_GEN_Q = (glIsEnabled (GL_TEXTURE_GEN_Q) == GL_TRUE);
00480     enabled_GL_FOG = (glIsEnabled (GL_FOG) == GL_TRUE);
00481 
00482     if (extmgr->CS_GL_ARB_multitexture)
00483     {
00484       for (i = 0 ; i < CS_GL_MAX_LAYER; i++)
00485       {
00486         extmgr->glActiveTextureARB (GL_TEXTURE0_ARB + i);
00487         extmgr->glClientActiveTextureARB (GL_TEXTURE0_ARB + i);
00488         enabled_GL_TEXTURE_1D[i] = (glIsEnabled (GL_TEXTURE_1D) == GL_TRUE);
00489         enabled_GL_TEXTURE_2D[i] = (glIsEnabled (GL_TEXTURE_2D) == GL_TRUE);
00490         enabled_GL_TEXTURE_3D[i] = (glIsEnabled (GL_TEXTURE_3D) == GL_TRUE);
00491         enabled_GL_TEXTURE_CUBE_MAP[i] = (glIsEnabled (GL_TEXTURE_CUBE_MAP) == GL_TRUE);
00492         enabled_GL_TEXTURE_COORD_ARRAY[i] = (glIsEnabled (GL_TEXTURE_COORD_ARRAY) == GL_TRUE);
00493         if (extmgr->CS_GL_ARB_texture_rectangle
00494           || extmgr->CS_GL_EXT_texture_rectangle
00495           || extmgr->CS_GL_NV_texture_rectangle)
00496           enabled_GL_TEXTURE_RECTANGLE_ARB[i] = (glIsEnabled (GL_TEXTURE_RECTANGLE_ARB) == GL_TRUE);
00497         else
00498           enabled_GL_TEXTURE_RECTANGLE_ARB[i] = false;
00499         glGetIntegerv (GL_TEXTURE_COORD_ARRAY_SIZE, (GLint*)&parameter_tsize[i]);
00500         glGetIntegerv (GL_TEXTURE_COORD_ARRAY_STRIDE, (GLint*)&parameter_tstride[i]);
00501         glGetIntegerv (GL_TEXTURE_COORD_ARRAY_TYPE, (GLint*)&parameter_ttype[i]);
00502         glGetPointerv (GL_TEXTURE_COORD_ARRAY_POINTER, &parameter_tpointer[i]);
00503       }
00504     } 
00505     else 
00506     {
00507       enabled_GL_TEXTURE_1D[0] = (glIsEnabled (GL_TEXTURE_1D) == GL_TRUE);
00508       enabled_GL_TEXTURE_2D[0] = (glIsEnabled (GL_TEXTURE_2D) == GL_TRUE);
00509       enabled_GL_TEXTURE_3D[0] = (glIsEnabled (GL_TEXTURE_3D) == GL_TRUE);
00510       enabled_GL_TEXTURE_CUBE_MAP[0] = (glIsEnabled (GL_TEXTURE_CUBE_MAP) == GL_TRUE);
00511       enabled_GL_TEXTURE_COORD_ARRAY[0] = (glIsEnabled (GL_TEXTURE_COORD_ARRAY) == GL_TRUE);
00512         if (extmgr->CS_GL_ARB_texture_rectangle
00513           || extmgr->CS_GL_EXT_texture_rectangle
00514           || extmgr->CS_GL_NV_texture_rectangle)
00515           enabled_GL_TEXTURE_RECTANGLE_ARB[0] = (glIsEnabled (GL_TEXTURE_RECTANGLE_ARB) == GL_TRUE);
00516         else
00517           enabled_GL_TEXTURE_RECTANGLE_ARB[0] = false;
00518       glGetIntegerv (GL_TEXTURE_COORD_ARRAY_SIZE, (GLint*)&parameter_tsize[0]);
00519       glGetIntegerv (GL_TEXTURE_COORD_ARRAY_STRIDE, (GLint*)&parameter_tstride[0]);
00520       glGetIntegerv (GL_TEXTURE_COORD_ARRAY_TYPE, (GLint*)&parameter_ttype[0]);
00521       glGetPointerv (GL_TEXTURE_COORD_ARRAY_POINTER, &parameter_tpointer[0]);
00522       for (i = 1 ; i < CS_GL_MAX_LAYER; i++)
00523       {
00524         enabled_GL_TEXTURE_1D[i] = enabled_GL_TEXTURE_1D[0];
00525         enabled_GL_TEXTURE_2D[i] = enabled_GL_TEXTURE_2D[0];
00526         enabled_GL_TEXTURE_3D[i] = enabled_GL_TEXTURE_3D[0];
00527         enabled_GL_TEXTURE_CUBE_MAP[i] = enabled_GL_TEXTURE_CUBE_MAP[0];
00528         enabled_GL_TEXTURE_COORD_ARRAY[i] = enabled_GL_TEXTURE_COORD_ARRAY[0];
00529         enabled_GL_TEXTURE_RECTANGLE_ARB[i] = enabled_GL_TEXTURE_RECTANGLE_ARB[0];
00530         parameter_tsize[i] = parameter_tsize[0];
00531         parameter_tstride[i] = parameter_tstride[0];
00532         parameter_ttype[i] = parameter_ttype[0];
00533         parameter_tpointer[i] = parameter_tpointer[0];
00534       }
00535     }
00536     enabled_GL_SCISSOR_TEST = (glIsEnabled (GL_SCISSOR_TEST) == GL_TRUE);
00537     enabled_GL_VERTEX_ARRAY = (glIsEnabled (GL_VERTEX_ARRAY) == GL_TRUE);
00538     enabled_GL_COLOR_ARRAY = (glIsEnabled (GL_COLOR_ARRAY) == GL_TRUE);
00539     if (extmgr->CS_GL_EXT_secondary_color)
00540       enabled_GL_SECONDARY_COLOR_ARRAY_EXT = 
00541         (glIsEnabled (GL_SECONDARY_COLOR_ARRAY_EXT) == GL_TRUE);
00542     else
00543       enabled_GL_SECONDARY_COLOR_ARRAY_EXT = false;
00544     enabled_GL_NORMAL_ARRAY = (glIsEnabled (GL_NORMAL_ARRAY) == GL_TRUE);
00545 
00546     if (extmgr->CS_GL_ARB_multitexture)
00547     {
00548       extmgr->glActiveTextureARB (GL_TEXTURE0_ARB);
00549       extmgr->glClientActiveTextureARB (GL_TEXTURE0_ARB);
00550     }
00551     memset (boundtexture, 0, CS_GL_MAX_LAYER * sizeof (GLuint));
00552     currentUnit = 0;
00553     memset (activeUnit, 0, sizeof (activeUnit));
00554     currentBufferID = 0;
00555     currentIndexID = 0;
00556 
00557     glGetIntegerv (GL_VERTEX_ARRAY_SIZE, (GLint*)&parameter_vsize);
00558     glGetIntegerv (GL_VERTEX_ARRAY_STRIDE, (GLint*)&parameter_vstride);
00559     glGetIntegerv (GL_VERTEX_ARRAY_TYPE, (GLint*)&parameter_vtype);
00560     glGetPointerv (GL_VERTEX_ARRAY_POINTER, &parameter_vpointer);
00561 
00562     glGetIntegerv (GL_NORMAL_ARRAY_STRIDE, (GLint*)&parameter_nstride);
00563     glGetIntegerv (GL_NORMAL_ARRAY_TYPE, (GLint*)&parameter_ntype);
00564     glGetPointerv (GL_NORMAL_ARRAY_POINTER, &parameter_npointer);
00565 
00566     glGetIntegerv (GL_COLOR_ARRAY_SIZE, (GLint*)&parameter_csize);
00567     glGetIntegerv (GL_COLOR_ARRAY_STRIDE, (GLint*)&parameter_cstride);
00568     glGetIntegerv (GL_COLOR_ARRAY_TYPE, (GLint*)&parameter_ctype);
00569     glGetPointerv (GL_COLOR_ARRAY_POINTER, &parameter_cpointer);
00570     
00571     if (extmgr->CS_GL_EXT_secondary_color)
00572     {
00573       glGetIntegerv (GL_SECONDARY_COLOR_ARRAY_SIZE_EXT, 
00574         (GLint*)&parameter_scsize);
00575       glGetIntegerv (GL_SECONDARY_COLOR_ARRAY_STRIDE_EXT, 
00576         (GLint*)&parameter_scstride);
00577       glGetIntegerv (GL_SECONDARY_COLOR_ARRAY_TYPE_EXT, 
00578         (GLint*)&parameter_sctype);
00579       glGetPointerv (GL_SECONDARY_COLOR_ARRAY_POINTER_EXT, 
00580         &parameter_scpointer);
00581       enabled_GL_COLOR_SUM_EXT = glIsEnabled (GL_COLOR_SUM_EXT) != GL_FALSE;
00582     }
00583     else
00584     {
00585       parameter_scsize = 0;
00586       parameter_scstride = 0;
00587       parameter_sctype = 0;
00588       parameter_scpointer = 0;
00589       enabled_GL_COLOR_SUM_EXT = false;
00590     }
00591   }
00592 };
00593 
00594 
00605 class CS_CSPLUGINCOMMON_GL_EXPORT csGLStateCache
00606 {
00607   enum
00608   {
00609     texServer = 0,
00610     texClient = 1
00611   };
00612 public:
00613   csGLExtensionManager* extmgr;
00614   csGLStateCacheContext* currentContext;
00615 
00616   csGLStateCache (csGLExtensionManager* extmgr)
00617   {
00618     csGLStateCache::extmgr = extmgr;
00619     currentContext = 0;
00620   }
00621 
00622   void SetContext (csGLStateCacheContext *context)
00623   {
00624     currentContext = context;
00625   }
00626 
00627   // Standardized caches
00628   IMPLEMENT_CACHED_BOOL (GL_DEPTH_TEST)
00629   IMPLEMENT_CACHED_BOOL (GL_BLEND)
00630   IMPLEMENT_CACHED_BOOL (GL_DITHER)
00631   IMPLEMENT_CACHED_BOOL (GL_STENCIL_TEST)
00632   IMPLEMENT_CACHED_BOOL (GL_CULL_FACE)
00633   IMPLEMENT_CACHED_BOOL (GL_POLYGON_OFFSET_FILL)
00634   IMPLEMENT_CACHED_BOOL (GL_LIGHTING)
00635   IMPLEMENT_CACHED_BOOL (GL_ALPHA_TEST)
00636   IMPLEMENT_CACHED_BOOL (GL_SCISSOR_TEST)
00637   IMPLEMENT_CACHED_BOOL (GL_TEXTURE_GEN_S)
00638   IMPLEMENT_CACHED_BOOL (GL_TEXTURE_GEN_T)
00639   IMPLEMENT_CACHED_BOOL (GL_TEXTURE_GEN_R)
00640   IMPLEMENT_CACHED_BOOL (GL_TEXTURE_GEN_Q)
00641   IMPLEMENT_CACHED_BOOL (GL_FOG)
00642   IMPLEMENT_CACHED_BOOL (GL_COLOR_SUM_EXT)
00643   IMPLEMENT_CACHED_BOOL_CURRENTLAYER (GL_TEXTURE_1D)
00644   IMPLEMENT_CACHED_BOOL_CURRENTLAYER (GL_TEXTURE_2D)
00645   IMPLEMENT_CACHED_BOOL_CURRENTLAYER (GL_TEXTURE_3D)
00646   IMPLEMENT_CACHED_BOOL_CURRENTLAYER (GL_TEXTURE_CUBE_MAP)
00647   IMPLEMENT_CACHED_BOOL_CURRENTLAYER (GL_TEXTURE_RECTANGLE_ARB)
00648   IMPLEMENT_CACHED_PARAMETER_2 (glAlphaFunc, AlphaFunc, GLenum, alpha_func, GLclampf, alpha_ref)
00649   IMPLEMENT_CACHED_PARAMETER_2 (glBlendFunc, BlendFunc, GLenum, blend_source, GLenum, blend_destination)
00650   IMPLEMENT_CACHED_PARAMETER_1 (glCullFace, CullFace, GLenum, cull_mode)
00651   IMPLEMENT_CACHED_PARAMETER_1 (glDepthFunc, DepthFunc, GLenum, depth_func)
00652   IMPLEMENT_CACHED_PARAMETER_1 (glDepthMask, DepthMask, GLboolean, depth_mask)
00653   IMPLEMENT_CACHED_PARAMETER_1 (glShadeModel, ShadeModel, GLenum, shade_model)
00654   IMPLEMENT_CACHED_PARAMETER_3 (glStencilFunc, StencilFunc, GLenum, stencil_func, GLint, stencil_ref, GLuint, stencil_mask)
00655   IMPLEMENT_CACHED_PARAMETER_3 (glStencilOp, StencilOp, GLenum, stencil_fail, GLenum, stencil_zfail, GLenum, stencil_zpass)
00656   IMPLEMENT_CACHED_PARAMETER_1 (glStencilMask, StencilMask, GLuint, maskl)
00657   IMPLEMENT_CACHED_PARAMETER_4 (glColorMask, ColorMask, GLboolean, wmRed, \
00658     GLboolean, wmGreen, GLboolean, wmBlue, GLboolean, wmAlpha)
00659 
00660   IMPLEMENT_CACHED_CLIENT_STATE (GL_VERTEX_ARRAY)
00661   IMPLEMENT_CACHED_CLIENT_STATE (GL_COLOR_ARRAY)
00662   IMPLEMENT_CACHED_CLIENT_STATE (GL_SECONDARY_COLOR_ARRAY_EXT)
00663   IMPLEMENT_CACHED_CLIENT_STATE (GL_NORMAL_ARRAY)
00664   IMPLEMENT_CACHED_CLIENT_STATE_LAYER (GL_TEXTURE_COORD_ARRAY)
00665 
00666   IMPLEMENT_CACHED_PARAMETER_1 (glMatrixMode, MatrixMode, GLenum, matrixMode)
00667   
00668   IMPLEMENT_CACHED_PARAMETER_4 (glVertexPointer, VertexPointer, GLint, vsize,
00669     GLenum, vtype, GLsizei, vstride, GLvoid*, vpointer);
00670   IMPLEMENT_CACHED_PARAMETER_3 (glNormalPointer, NormalPointer, GLenum, ntype,
00671     GLsizei, nstride, GLvoid*, npointer);
00672   IMPLEMENT_CACHED_PARAMETER_4 (glColorPointer, ColorPointer, GLint, csize,
00673     GLenum, ctype, GLsizei, cstride, GLvoid*, cpointer);
00674   IMPLEMENT_CACHED_PARAMETER_4 (extmgr->glSecondaryColorPointerEXT, 
00675     SecondaryColorPointerExt, GLint, scsize, GLenum, sctype, GLsizei, scstride, 
00676     GLvoid*, scpointer);
00677   IMPLEMENT_CACHED_PARAMETER_4_LAYER (glTexCoordPointer, TexCoordPointer, GLint, tsize,
00678     GLenum, ttype, GLsizei, tstride, GLvoid*, tpointer);
00679   
00680   // Special caches
00681   void SetTexture (GLenum target, GLuint texture)
00682   {
00683     const int currentUnit = currentContext->currentUnit;
00684     if (texture != currentContext->boundtexture[currentUnit])
00685     {
00686       ActivateTU (activateImage);
00687       currentContext->boundtexture[currentUnit] = texture;
00688       glBindTexture (target, texture);
00689     }
00690   }
00691   GLuint GetTexture (GLenum /*target*/)
00692   {
00693     const int currentUnit = currentContext->currentUnit;
00694     return currentContext->boundtexture[currentUnit];
00695   }
00696   GLuint GetTexture (GLenum /*target*/, int unit)
00697   {
00698     return currentContext->boundtexture[unit];
00699   }
00705   void SetCurrentTU (int unit)
00706   {
00707     currentContext->currentUnit = unit;   
00708   }
00710   int GetCurrentTU ()
00711   {
00712     return currentContext->currentUnit;
00713   }
00715   static const int activateTexCoord = 1 << texClient;
00717   static const int activateImage = 1 << texServer;
00719   static const int activateMatrix = 1 << texServer;
00721   static const int activateTexEnv = 1 << texServer;
00726   static const int activateTexGen = 1 << texServer;
00728   static const int activateTexEnable = 1 << texServer;
00736   void ActivateTU (uint usage)
00737   {
00738     int currentUnit = currentContext->currentUnit;
00739     for (int i = 0; i < 2; i++)
00740     {
00741       if (currentContext->activeUnit[i] != currentUnit)
00742       {
00743         GLuint tu = GL_TEXTURE0_ARB + currentUnit;
00744         if (usage & (1 << i))
00745         {
00746           if (i == texClient)
00747             extmgr->glClientActiveTextureARB (tu);
00748           else
00749             extmgr->glActiveTextureARB (tu);
00750           currentContext->activeUnit[i] = currentUnit;
00751         }
00752       }
00753     }
00754   }
00755 
00756   //VBO buffers
00757   void SetBufferARB (GLenum target, GLuint id)
00758   {
00759     if (target == GL_ELEMENT_ARRAY_BUFFER_ARB)
00760     {
00761       if (id != currentContext->currentIndexID)
00762       {
00763         extmgr->glBindBufferARB (target, id);
00764         currentContext->currentIndexID = id;
00765       }
00766     } 
00767     else 
00768     {
00769       if (id != currentContext->currentBufferID)
00770       {
00771         extmgr->glBindBufferARB (target, id);
00772         currentContext->currentBufferID = id;
00773         currentContext->parameter_vpointer = (GLvoid*)~0; //invalidate vertexpointer
00774         currentContext->parameter_npointer = (GLvoid*)~0; //invalidate vertexpointer
00775         currentContext->parameter_cpointer = (GLvoid*)~0; //invalidate vertexpointer
00776         memset(&currentContext->parameter_tpointer, ~0, sizeof(GLvoid*)*CS_GL_MAX_LAYER);
00777       }
00778     }
00779   }
00780 
00781   GLuint GetBufferARB (GLenum target)
00782   {
00783     if (target == GL_ELEMENT_ARRAY_BUFFER_ARB)
00784     {
00785       return currentContext->currentIndexID;
00786     } 
00787     else 
00788     {
00789       return currentContext->currentBufferID;
00790     }
00791   }
00792 };
00793 
00794 #undef IMPLEMENT_CACHED_BOOL
00795 #undef IMPLEMENT_CACHED_BOOL_CURRENTLAYER
00796 #undef IMPLEMENT_CACHED_PARAMETER_1
00797 #undef IMPLEMENT_CACHED_PARAMETER_2
00798 #undef IMPLEMENT_CACHED_PARAMETER_3
00799 #undef IMPLEMENT_CACHED_PARAMETER_4
00800 #undef IMPLEMENT_CACHED_PARAMETER_1_LAYER
00801 #undef IMPLEMENT_CACHED_PARAMETER_2_LAYER
00802 #undef IMPLEMENT_CACHED_PARAMETER_3_LAYER
00803 #undef IMPLEMENT_CACHED_PARAMETER_4_LAYER
00804 #undef IMPLEMENT_CACHED_CLIENT_STATE
00805 #undef IMPLEMENT_CACHED_CLIENT_STATE_LAYER
00806 
00807 #undef DECLARE_CACHED_BOOL
00808 #undef DECLARE_CACHED_BOOL_CURRENTLAYER
00809 #undef DECLARE_CACHED_PARAMETER_1
00810 #undef DECLARE_CACHED_PARAMETER_2
00811 #undef DECLARE_CACHED_PARAMETER_3
00812 #undef DECLARE_CACHED_PARAMETER_4
00813 #undef DECLARE_CACHED_PARAMETER_1_LAYER
00814 #undef DECLARE_CACHED_PARAMETER_2_LAYER
00815 #undef DECLARE_CACHED_PARAMETER_3_LAYER
00816 #undef DECLARE_CACHED_PARAMETER_4_LAYER
00817 #undef DECLARE_CACHED_CLIENT_STATE
00818 #undef DECLARE_CACHED_CLIENT_STATE_LAYER
00819 
00820 #undef FORCE_STATE_CHANGE
00821 
00824 #endif // __CS_GLSTATES_H__

Generated for Crystal Space 1.0.2 by doxygen 1.4.7