CrystalSpace

Public API Reference

Main Page | Modules | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members | Related Pages

glstates.h

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

Generated for Crystal Space by doxygen 1.3.9.1