00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 
00030 
00031 
00032 
00033 
00034 
00041 #ifndef __RENDERTEXTURE2_HPP__
00042 #define __RENDERTEXTURE2_HPP__
00043 
00044 
00045 
00046 
00047 
00048 
00049 
00050 
00051 #include <simgear/compiler.h>
00052 
00053 #include <osg/GL>
00054 
00055 #if defined( __APPLE__)
00056 #  include <OpenGL/OpenGL.h>
00057 #endif
00058 
00059 #if !defined( _WIN32 ) && !defined( __APPLE__ )
00060 #  include <X11/Xlib.h>
00061 #  include <GL/glx.h>
00062 #endif
00063 
00064 #include <string>
00065 #include <vector>
00066 
00067 
00068 
00069 
00070 
00071 
00072 
00073 
00074 
00075 
00076 
00077 
00078 
00079 
00080 
00081 
00082 
00083 
00084 
00085 
00086 
00087 
00088 
00089 
00090 
00091 
00092 
00093 
00094 
00095 
00096 
00097 
00098 
00099 
00100 
00101 
00102 
00103 
00104 
00105 
00106 
00107 
00108 
00109 
00110 
00111 
00112 
00113 
00114 
00115 
00116 
00117 
00118 
00119 
00120 
00121 
00122 
00123 
00124 
00125 
00126 
00127 
00128 
00129 
00130 
00131 
00132 
00133 
00134 
00135 
00136 
00137 
00138 
00139 
00140 
00141 
00142 class RenderTexture
00143 {
00144 public: 
00145     enum UpdateMode
00146     {
00147         RT_RENDER_TO_TEXTURE,
00148         RT_COPY_TO_TEXTURE
00149     };
00150     
00151 public: 
00152     
00153     RenderTexture(const char *strMode="rgb tex2D");
00154     ~RenderTexture();
00155     
00159     bool Initialize(int width, int height, 
00160                     bool shareObjects=true, 
00161                     bool copyContext=false);
00162 
00163     
00164     bool Reset(const char* strMode,...);
00165     
00166     bool Resize(int width, int height);
00167     
00168     
00169     bool BeginCapture();
00170     
00171     bool BeginCapture(RenderTexture* current);
00172     
00173     bool EndCapture();
00174     
00175     
00176     void Bind() const;
00177 
00178     
00179     void BindDepth() const; 
00180 
00181     
00182     bool BindBuffer( int iBuffer );
00183 
00185     void EnableTextureTarget() const 
00186     { if (_bInitialized) glEnable(_iTextureTarget); }
00188     void DisableTextureTarget() const 
00189     { if (_bInitialized) glDisable(_iTextureTarget); }
00190     
00192     unsigned int GetTextureID() const  { return _iTextureID; }
00194     unsigned int GetDepthTextureID() const { return _iDepthTextureID; }
00196     unsigned int GetTextureTarget() const { return _iTextureTarget; }
00198     operator unsigned int()const{return _iTextureID;}     
00199     
00201     int GetWidth() const            { return _iWidth;  } 
00203     int GetHeight() const           { return _iHeight; }
00205     int GetMaxS() const      { return IsRectangleTexture() ? _iWidth : 1; }                  
00207     int GetMaxT() const      { return IsRectangleTexture() ? _iHeight : 1; }                  
00208     
00210     int GetRedBits() const          { return _iNumColorBits[0]; }
00212     int GetGreenBits() const        { return _iNumColorBits[1]; }
00214     int GetBlueBits() const         { return _iNumColorBits[2]; }
00216     int GetAlphaBits() const        { return _iNumColorBits[3]; }
00217 
00219     int GetDepthBits() const        { return _iNumDepthBits; }
00221     int GetStencilBits() const      { return _iNumStencilBits; }
00222     
00224     bool IsInitialized() const      { return _bInitialized; }
00226     bool IsTexture() const          { return _bIsTexture; }
00228     bool IsDepthTexture() const     { return _bIsDepthTexture; }
00230     bool IsFloatTexture() const     { return _bFloat; }
00232     bool IsDoubleBuffered() const   { return _bDoubleBuffered; }
00234     bool IsRectangleTexture() const { return _bRectangle; }
00237     bool HasDepth() const           { return (_iNumDepthBits > 0); }
00239     bool HasStencil() const         { return (_iNumStencilBits > 0); }
00241     bool IsMipmapped() const        { return _bMipmap; }
00242 
00250     static bool IsPowerOfTwo(int n) { return ((n&(n-1))==0); }
00251 
00252 
00254     
00255     
00256     
00257     RenderTexture(int width, int height,
00258                    bool bIsTexture = true,
00259                    bool bIsDepthTexture = false);
00260     
00261     
00262     
00263     
00264     bool Initialize(bool bShare              = true, 
00265                     bool bDepth              = false, 
00266                     bool bStencil            = false,
00267                     bool bMipmap             = false, 
00268                     bool bAnisoFilter        = false,
00269                     unsigned int iRBits      = 8,
00270                     unsigned int iGBits      = 8,
00271                     unsigned int iBBits      = 8,
00272                     unsigned int iABits      = 8,
00273 
00274 #ifdef _WIN32
00275                     UpdateMode   updateMode = RT_RENDER_TO_TEXTURE
00276 #else
00277                     UpdateMode   updateMode = RT_COPY_TO_TEXTURE
00278 #endif
00279                     );
00280     
00281     bool Reset(int iWidth, int iHeight);
00282     
00284 
00285 
00286 protected: 
00287     bool         _Invalidate();
00288 
00289     typedef std::pair<std::string, std::string> KeyVal;
00290 
00291     void _ParseModeString(const char *modeString, 
00292                           std::vector<int> &pixelFormatAttribs, 
00293                           std::vector<int> &pbufferAttribs);
00294 
00295     std::vector<int> _ParseBitVector(std::string bitVector);
00296     KeyVal _GetKeyValuePair(std::string token);
00297 
00298 
00299     bool _VerifyExtensions();
00300     bool _InitializeTextures();
00301     
00302     void _MaybeCopyBuffer();
00303     bool _ReleaseBoundBuffers();
00304     bool _MakeCurrent();
00305     bool _BindDepthBuffer( ) const;
00306 
00307 protected: 
00308     int          _iWidth;     
00309     int          _iHeight;    
00310     
00311     bool         _bIsTexture;
00312     bool         _bIsDepthTexture;
00313     bool         _bHasARBDepthTexture; 
00314     
00315     UpdateMode   _eUpdateMode;
00316         
00317     bool         _bInitialized;
00318     
00319     unsigned int _iNumAuxBuffers;
00320     bool         _bIsBufferBound;
00321     int          _iCurrentBoundBuffer;
00322     
00323     unsigned int _iNumComponents;
00324     unsigned int _iNumColorBits[4];
00325     unsigned int _iNumDepthBits;
00326     unsigned int _iNumStencilBits;
00327 
00328     
00329     bool         _bFloat;
00330     bool         _bDoubleBuffered;
00331     bool         _bPowerOf2;
00332     bool         _bRectangle;
00333     bool         _bMipmap;
00334     
00335     bool         _bShareObjects;
00336     bool         _bCopyContext;
00337     
00338 #ifdef _WIN32
00339     HDC          _hDC;        
00340     HGLRC        _hGLContext; 
00341     HPBUFFERARB  _hPBuffer;   
00342     
00343     HDC          _hPreviousDC;
00344     HGLRC        _hPreviousContext;
00345 #elif defined( __MACH__ )
00346     CGLContextObj      _hGLContext;
00347     CGLPBufferObj   _hPBuffer;
00348    
00349     CGLContextObj      _hPreviousContext;
00350 #else
00351     Display     *_pDisplay;
00352     GLXContext   _hGLContext;
00353     GLXPbuffer   _hPBuffer;
00354     
00355     GLXDrawable  _hPreviousDrawable;
00356     GLXContext   _hPreviousContext;
00357 #endif
00358     
00359     
00360     GLenum       _iTextureTarget;
00361     GLuint       _iTextureID;
00362     GLuint       _iDepthTextureID;
00363     
00364     unsigned short* _pPoorDepthTexture; 
00365 
00366     std::vector<int> _pixelFormatAttribs;
00367     std::vector<int> _pbufferAttribs;
00368 
00369 private:
00370     
00371     RenderTexture(const RenderTexture&);
00372     RenderTexture& operator=(const RenderTexture&);
00373 };
00374 
00375 #endif //__RENDERTEXTURE2_HPP__