Back to index

nux  3.0.0
GLPBuffer.h
Go to the documentation of this file.
00001 /*
00002  * Copyright 2010 Inalogic® Inc.
00003  *
00004  * This program is free software: you can redistribute it and/or modify it
00005  * under the terms of the GNU Lesser General Public License, as
00006  * published by the  Free Software Foundation; either version 2.1 or 3.0
00007  * of the License.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranties of
00011  * MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
00012  * PURPOSE.  See the applicable version of the GNU Lesser General Public
00013  * License for more details.
00014  *
00015  * You should have received a copy of both the GNU Lesser General Public
00016  * License along with this program. If not, see <http://www.gnu.org/licenses/>
00017  *
00018  * Authored by: Jay Taoko <jaytaoko@inalogic.com>
00019  *
00020  */
00021 
00022 
00023 #ifndef __PBUFFERS_H__
00024 #define __PBUFFERS_H__
00025 
00026 #ifndef NUX_OPENGLES_20
00027 
00028 #if defined(WIN32)
00029 #include "GLResource.h"
00030 
00031 #  pragma warning (disable : 4786)
00032 #elif defined(UNIX)
00033 #  include <GL/glx.h>
00034 #  include <GL/glxext.h>
00035 #elif defined(__APPLE__)
00036 #  include <AGL/agl.h>
00037 #endif
00038 
00039 #include <string>
00040 #include <vector>
00041 
00042 // The pixel format for the pbuffer is controlled by the mode string passed
00043 // into the PBuffer constructor. This string can have the following attributes:
00044 //
00045 // r                 - r pixel format (for float buffer).
00046 // rg                - rg pixel format (for float buffer).
00047 // rgb          - rgb pixel format. 8 bit or 16/32 bit in float buffer mode
00048 // rgba         - same as "rgb alpha" string
00049 // alpha        - must have alpha channel
00050 // depth        - must have a depth buffer
00051 // depth=n      - must have n-bit depth buffer
00052 // stencil      - must have a stencil buffer
00053 // double       - must support double buffered rendering
00054 // samples=n    - must support n-sample antialiasing (n can be 2 or 4)
00055 // float=n      - must support n-bit per channel floating point
00056 //
00057 // texture2D
00058 // textureRECT
00059 // textureCUBE  - must support binding pbuffer as texture to specified target
00060 //              - binding the depth buffer is also supporting by specifying
00061 //                '=depth' like so: texture2D=depth or textureRECT=depth
00062 //              - the internal format of the texture will be rgba by default or
00063 //                float if pbuffer is floating point
00064 //
00065 
00066 namespace nux
00067 {
00068 
00069   class PBuffer
00070   {
00071   public:
00072     // see above for documentation on strMode format
00073     // set managed to true if you want the class to cleanup OpenGL objects in destructor
00074     PBuffer (const char *strMode, bool managed = false);
00075     ~PBuffer();
00076 
00077     bool Initialize (int iWidth, int iHeight, bool bShareContexts, bool bShareObjects);
00078     void Destroy();
00079 
00080     void Activate (PBuffer *current = NULL); // to switch between pbuffers, pass active pbuffer as argument
00081     void Deactivate();
00082 
00083 #if defined(WIN32)
00084     int Bind (int iBuffer);
00085     int Release (int iBuffer);
00086 
00087     void HandleModeSwitch();
00088 #endif
00089 
00090     unsigned int GetSizeInBytes();
00091     unsigned int CopyToBuffer (void *ptr, int w = -1, int h = -1);
00092 
00093     inline int GetNumComponents()
00094     {
00095       return m_iNComponents;
00096     }
00097 
00098     inline int GetBitsPerComponent()
00099     {
00100       return m_iBitsPerComponent;
00101     }
00102 
00103     inline int GetWidth()
00104     {
00105       return m_iWidth;
00106     }
00107 
00108     inline int GetHeight()
00109     {
00110       return m_iHeight;
00111     }
00112 
00113     inline bool IsSharedContext()
00114     {
00115       return m_bSharedContext;
00116     }
00117 
00118 #if defined(WIN32)
00119     inline bool IsTexture()
00120     {
00121       return m_bIsTexture;
00122     }
00123 #endif
00124 
00125   protected:
00126 #if defined(NUX_OS_WINDOWS)
00127     HDC         m_hDC;     
00128     HGLRC       m_hGLRC;   
00129     HPBUFFERARB m_hPBuffer;
00130 
00131     HGLRC       m_hOldGLRC;
00132     HDC         m_hOldDC;
00133 
00134     std::vector<int> m_pfAttribList;
00135     std::vector<int> m_pbAttribList;
00136 
00137     bool m_bIsTexture;
00138 #elif defined(NUX_OS_LINUX)
00139     Display    *m_pDisplay;
00140     GLXPbuffer  m_glxPbuffer;
00141     GLXContext  m_glxContext;
00142 
00143     Display    *m_pOldDisplay;
00144     GLXPbuffer  m_glxOldDrawable;
00145     GLXContext  m_glxOldContext;
00146 
00147     std::vector<int> m_pfAttribList;
00148     std::vector<int> m_pbAttribList;
00149 #elif defined(NUX_OS_MACOSX)
00150     AGLContext  m_context;
00151     WindowPtr   m_window;
00152     std::vector<int> m_pfAttribList;
00153 #endif
00154 
00155     int m_iWidth;
00156     int m_iHeight;
00157     int m_iNComponents;
00158     int m_iBitsPerComponent;
00159 
00160     const char *m_strMode;
00161     bool m_bSharedContext;
00162     bool m_bShareObjects;
00163 
00164   private:
00165     std::string getStringValue (std::string token);
00166     int getIntegerValue (std::string token);
00167 #if defined(NUX_OS_WINDOWS) || defined(NUX_OS_LINUX)
00168     void parseModeString (const char *modeString, std::vector<int> *pfAttribList, std::vector<int> *pbAttribList);
00169 
00170     bool m_bIsBound;
00171     bool m_bIsActive;
00172     bool m_bManaged;
00173 #endif
00174   };
00175 }
00176 
00177 #endif // NUX_OPENGLES_20
00178 
00179 #endif // __PBUFFERS_H__