TAPs 0.7.7.3
TAPsPBuffer.cpp
Go to the documentation of this file.
00001 /******************************************************************************
00002 TAPsPBuffer.cpp
00003 
00004 Class PBuffer (Pixel Buffer) is a class for managing a pbuffer.
00005 
00006 SUKITTI PUNAK   (05/18/2005)
00007 UPDATE          (06/25/2005)
00008 ******************************************************************************/
00009 #include "TAPsPBuffer.hpp"
00010 // Using Inclusion Model (i.e. definitions are included in declarations)
00011 //                       (this name.cpp is included in name.hpp)
00012 // Each friend is defined directly inside its declaration.
00013 
00014 //-----------------------------------------------------------------------------
00015 // DEBUG ENABLE
00016 #ifdef TAPs_ENABLE_DEBUG
00017     #define DEBUG_MESSAGE_TAPs_PIXEL_BUFFER_HPP fprintf
00018     #define EXTRA_DEBUG_MESSAGE_TAPs_PIXEL_BUFFER_HPP
00019 #else
00020     #define DEBUG_MESSAGE_TAPs_PIXEL_BUFFER_HPP 
00021 #endif
00022 
00023 #ifndef DEBUG_MESSAGE_TAPs_PIXEL_BUFFER_HPP
00024     #define DEBUG_MESSAGE_TAPs_PIXEL_BUFFER_HPP fprintf
00025 #endif
00026 #ifndef
00027     #define EXTRA_DEBUG_MESSAGE_TAPs_PIXEL_BUFFER_HPP
00028 #endif
00029 //-----------------------------------------------------------------------------
00030 
00031 BEGIN_NAMESPACE_TAPs__OpenGL
00032 //******************************************************************************
00033 // For WIN32
00034 //******************************************************************************
00035 #if defined ( WIN32 )
00036 //=============================================================================
00037 // Constructor and Destructor
00038 //-----------------------------------------------------------------------------
00039 PBuffer::PBuffer ( const char *strMode, bool managed ) : 
00040     m_iWidth( 0 ), m_iHeight( 0 ),
00041     m_iNoOfComponents( 0 ),
00042     m_iBitsPerComponent( 8 ),
00043     m_cpMode( strMode ),
00044     m_bSharedContextStatus( false ),
00045     m_bShareObjectsStatus( false ),
00046     // WIN32 Specific
00047     m_hDC( 0 ), m_hGLRC( 0 ), m_hPBuffer( 0 ), m_hOldDC( 0 ), m_hOldGLRC( 0 ), 
00048     m_bIsTexture( false ),
00049     // Helper Data Members
00050     m_bIsBound( false ),
00051     m_bIsActive( false ),
00052     m_bManaged( managed )
00053 {
00054     //glewInit();
00055     //----------------------------------------------------------------
00056     // m_pfAttribList
00057     m_pfAttribList.push_back( WGL_DRAW_TO_PBUFFER_ARB );
00058     m_pfAttribList.push_back( true );
00059     m_pfAttribList.push_back( WGL_SUPPORT_OPENGL_ARB );
00060     m_pfAttribList.push_back( true );
00061     //----------------------------------------------------------------
00062     // m_pbAttribList
00063     m_pbAttribList.push_back( WGL_PBUFFER_LARGEST_ARB );
00064     m_pbAttribList.push_back( true );
00065     //----------------------------------------------------------------
00066     DEBUG_MESSAGE_TAPs_PIXEL_BUFFER_HPP ( stdout, 
00067         "Declare a PBuffer with \"%s\" parameters\n", 
00068         m_cpMode 
00069     );
00070     ParseModeString( m_cpMode, &m_pfAttribList, &m_pbAttribList );
00071     m_pfAttribList.push_back( 0 );
00072     m_pbAttribList.push_back( 0 );
00073 }
00074 //-----------------------------------------------------------------------------
00075 PBuffer::~PBuffer ()
00076 {
00077     if ( m_bManaged )   Destroy();
00078 }
00079 //-----------------------------------------------------------------------------
00080 // Create the PBuffer
00081 // This function creates the PBuffer.
00082 // It can only be called once a window has already been created.
00083 bool PBuffer::Create ( int iWidth, int iHeight, bool bShareContexts, bool bShareObjects )
00084 {
00085     //----------------------------------------------------------------
00086     HDC     hdc     = wglGetCurrentDC();
00087     HGLRC   hglrc   = wglGetCurrentContext();
00088     int format = 0;
00089     int nfAttribs = 0;
00090     int niAttribs = 0;
00091     m_iWidth = iWidth;
00092     m_iHeight = iHeight;
00093     m_bSharedContextStatus  = bShareContexts;
00094     m_bShareObjectsStatus   = bShareObjects;
00095     //----------------------------------------------------------------
00096     if ( m_bSharedContextStatus ) {
00097         // Get the pixel format for the on-screen window
00098         format = GetPixelFormat( hdc );
00099         if ( 0 == format ) {
00100             DEBUG_MESSAGE_TAPs_PIXEL_BUFFER_HPP ( stdout, 
00101                 "PBuffer Creation ERROR: GetPixelFormat() failed!\n"
00102             );
00103             return false;
00104         }
00105     }
00106     else {
00107         unsigned int nFormats;
00108         wglChoosePixelFormatARB( hdc, &m_pfAttribList[0], NULL, 1, &format, &nFormats );
00109         if ( 0 == nFormats ) {
00110             DEBUG_MESSAGE_TAPs_PIXEL_BUFFER_HPP ( stdout, 
00111                 "PBuffer Creation ERROR: Couldn't find a suitable pixel format!\n" 
00112             );
00113             return false;
00114         }
00115     }
00116     //----------------------------------------------------------------
00117     m_hPBuffer = wglCreatePbufferARB( hdc, format, m_iWidth, m_iHeight, &m_pbAttribList[0] );
00118     if ( !m_hPBuffer ) {
00119         DWORD err = GetLastError();
00120         DEBUG_MESSAGE_TAPs_PIXEL_BUFFER_HPP ( stdout, 
00121             "PBuffer Creation ERROR: call wglCreatePbufferARB() failed!\n" 
00122         );
00123         if ( ERROR_INVALID_PIXEL_FORMAT == err ) {
00124             DEBUG_MESSAGE_TAPs_PIXEL_BUFFER_HPP ( stdout, 
00125                 "  ==> ERROR_INVALID_PIXEL_FORMAT\n" 
00126             );
00127         }
00128         else if ( ERROR_NO_SYSTEM_RESOURCES == err ) {
00129             DEBUG_MESSAGE_TAPs_PIXEL_BUFFER_HPP ( stdout, 
00130                 "  ==> ERROR_NO_SYSTEM_RESOURCES\n" 
00131             );
00132         }
00133         else if ( ERROR_INVALID_DATA == err ) {
00134             DEBUG_MESSAGE_TAPs_PIXEL_BUFFER_HPP ( stdout, 
00135                 "  ==> ERROR_INVALID_DATA\n" 
00136             );
00137         }
00138         return false;
00139     }
00140     //----------------------------------------------------------------
00141     // Get the device context
00142     m_hDC = wglGetPbufferDCARB( m_hPBuffer );
00143     if ( !m_hDC ) {
00144         DEBUG_MESSAGE_TAPs_PIXEL_BUFFER_HPP ( stdout, 
00145             "PBuffer Creation ERROR: call wglGetPbufferDCARB() failed!\n" 
00146         );
00147         return false;
00148     }
00149     //----------------------------------------------------------------
00150     if ( m_bSharedContextStatus ) {
00151         // Let's use the same gl context
00152         // Since the device contexts are compatible (i.e. same pixelformat),
00153         // we should be able to use the same gl rendering context
00154         m_hGLRC = hglrc;
00155     }
00156     else {
00157         // Create a new gl context for the PBuffer
00158         m_hGLRC = wglCreateContext( m_hDC );
00159         if ( !m_hGLRC ) {
00160             DEBUG_MESSAGE_TAPs_PIXEL_BUFFER_HPP ( stdout, 
00161                 "PBuffer Creation ERROR: call wglCreateContext() failed!\n" 
00162             );
00163             return false;
00164         }
00165         if ( m_bShareObjectsStatus ) {
00166             if ( !wglShareLists( hglrc, m_hGLRC ) ) {
00167                 DEBUG_MESSAGE_TAPs_PIXEL_BUFFER_HPP ( stdout, 
00168                     "PBuffer Creation ERROR: call wglShareLists() failed!\n" 
00169                 );
00170                 return false;
00171             }
00172         }
00173     }
00174     //----------------------------------------------------------------
00175     // Check if PBuffer is a texture format
00176     GLint texFormat = WGL_NO_TEXTURE_ARB;
00177     wglQueryPbufferARB( m_hPBuffer, WGL_TEXTURE_FORMAT_ARB, &texFormat );
00178     if ( WGL_NO_TEXTURE_ARB != texFormat ) {
00179         m_bIsTexture = true;
00180     }
00181     //----------------------------------------------------------------
00182     // Determine the actual width and height of the created PBuffer
00183     wglQueryPbufferARB( m_hPBuffer, WGL_PBUFFER_WIDTH_ARB, &m_iWidth );
00184     wglQueryPbufferARB( m_hPBuffer, WGL_PBUFFER_HEIGHT_ARB, &m_iHeight );
00185     DEBUG_MESSAGE_TAPs_PIXEL_BUFFER_HPP ( stdout, 
00186         "(width) %d x (height) %d PBuffer is created\n", 
00187         m_iWidth, m_iHeight 
00188     );
00189     //----------------------------------------------------------------
00190 #ifdef EXTRA_DEBUG_MESSAGE_TAPs_PIXEL_BUFFER_HPP
00191     int iAttributes[] = {
00192         WGL_RED_BITS_ARB,
00193         WGL_GREEN_BITS_ARB,
00194         WGL_BLUE_BITS_ARB,
00195         WGL_ALPHA_BITS_ARB,
00196         WGL_FLOAT_COMPONENTS_NV,
00197         WGL_DEPTH_BITS_ARB,
00198         WGL_SAMPLES_EXT,
00199         WGL_AUX_BUFFERS_ARB
00200     };
00201     int iValues[ sizeof( iAttributes ) / sizeof( int ) ];
00202     if ( wglGetPixelFormatAttribivARB ( m_hDC, format, 0, 
00203             sizeof( iAttributes ) / sizeof ( int ), iAttributes, iValues ) ) {
00204         DEBUG_MESSAGE_TAPs_PIXEL_BUFFER_HPP ( stdout, 
00205             "The PBuffer has r:%d g:%d b:%d a:%d float:%d depth%d samples:%d aux:%d\n", 
00206             iValues[0], iValues[1], iValues[2], iValues[3], iValues[4], iValues[5], iValues[6], iValues[7] 
00207         );
00208     }
00209 #endif
00210     //----------------------------------------------------------------
00211     return true;
00212 }
00213 //-----------------------------------------------------------------------------
00214 // Destroy the PBuffer
00215 void PBuffer::Destroy ()
00216 {
00217     if ( m_hPBuffer ) {
00218         if ( !m_bSharedContextStatus )  wglDeleteContext( m_hGLRC );
00219         wglReleasePbufferDCARB( m_hPBuffer, m_hDC );
00220         wglDestroyPbufferARB( m_hPBuffer );
00221     }
00222 }
00223 //-----------------------------------------------------------------------------
00224 // Activate the PBuffer
00225 void PBuffer::Activate ( PBuffer *current /* = NULL */ )
00226 {
00227     //----------------------------------------------------------------
00228     if ( current == this ) {
00229         return; // no switch necessary
00230     }
00231     //----------------------------------------------------------------
00232     if ( NULL == current || !current->m_bIsActive ) {
00233         if ( m_bIsActive )  return;
00234         m_hOldGLRC  = wglGetCurrentContext();
00235         m_hOldDC    = wglGetCurrentDC();
00236     }
00237     else {
00238         m_hOldGLRC  = current->m_hOldGLRC;
00239         m_hOldDC    = current->m_hOldDC;
00240         current->m_hOldGLRC     = 0;
00241         current->m_bIsActive    = false;
00242     }
00243     //----------------------------------------------------------------
00244     if ( !wglMakeCurrent( m_hDC, m_hGLRC ) ) {
00245         DEBUG_MESSAGE_TAPs_PIXEL_BUFFER_HPP ( stdout, 
00246             "PBuffer ERROR: call PBuffer::Activate() failed!\n"
00247         );
00248     }
00249     m_bIsActive = true;
00250 }
00251 //-----------------------------------------------------------------------------
00252 // Deactivate the PBuffer
00253 void PBuffer::Deactivate ()
00254 {
00255     if ( !m_bIsActive ) return;
00256     if ( !wglMakeCurrent( m_hOldDC, m_hOldGLRC ) ) {
00257         DEBUG_MESSAGE_TAPs_PIXEL_BUFFER_HPP ( stdout, 
00258             "PBuffer ERROR: call PBuffer::Deactivate() failed!\n"
00259         );
00260     }
00261     m_hOldGLRC  = 0;
00262     m_hOldDC    = 0;
00263     m_bIsActive = false;
00264 }
00265 //-----------------------------------------------------------------------------
00266 // Bind as a texture
00267 //void PBuffer::Bind ( unsigned int buffer /* = GL_FRONT */ )
00268 int PBuffer::Bind ( int iBuffer )
00269 {
00270     if ( !m_bIsTexture ) {
00271         DEBUG_MESSAGE_TAPs_PIXEL_BUFFER_HPP ( stdout, 
00272             "PBuffer ERROR: call PBuffer::Bind() failed - PBuffer format does not support render to texture!\n" 
00273         );
00274         return 0;
00275     }
00276 #if 0
00277     // SGG - with MRT it is legal to bind different buffers of a PBuffer simultaneously
00278     if ( m_bIsBound ) {
00279         DEBUG_MESSAGE_TAPs_PIXEL_BUFFER_HPP ( stdout, 
00280             "PBuffer ERROR: call PBuffer::Bind() failed - PBuffer is already bound!\n" 
00281         );
00282         return 0;
00283     }
00284 #endif
00285     int ret = wglBindTexImageARB( m_hPBuffer, iBuffer );
00286     if ( !ret ) {
00287         DEBUG_MESSAGE_TAPs_PIXEL_BUFFER_HPP ( stdout, 
00288             "PBuffer ERROR: call PBuffer::Bind() failed!\n" 
00289         );
00290         return 0;
00291     }
00292     m_bIsBound = true;
00293     return ret;
00294 }
00295 //-----------------------------------------------------------------------------
00296 // Release the PBuffer from a texture
00297 //void PBuffer::Release ( unsigned int buffer /* = GL_FRONT */ )
00298 int PBuffer::Release ( int iBuffer )
00299 {
00300     if ( !m_bIsTexture ) {
00301         DEBUG_MESSAGE_TAPs_PIXEL_BUFFER_HPP ( stdout, 
00302             "PBuffer ERROR: call PBuffer::Release() failed - PBuffer format does not support render to texture!\n" 
00303         );
00304         return 0;
00305     }
00306 #if 0
00307     // SGG - with MRT it is legal to bind different buffers of a PBuffer simultaneously
00308     if ( !m_bIsBound ) {
00309         DEBUG_MESSAGE_TAPs_PIXEL_BUFFER_HPP ( stdout, 
00310             "PBuffer ERROR: call PBuffer::Release() failed - PBuffer is not bound!\n" 
00311         );
00312         return 0;
00313     }
00314 #endif
00315     int ret = wglReleaseTexImageARB( m_hPBuffer, iBuffer );
00316     if ( !ret ) {
00317         DEBUG_MESSAGE_TAPs_PIXEL_BUFFER_HPP ( stdout, 
00318             "PBuffer ERROR: call PBuffer::Release() failed!\n" 
00319         );
00320         return 0;
00321     }
00322     m_bIsBound = false;
00323     return ret;
00324 }
00325 //-----------------------------------------------------------------------------
00326 // Swap if applicable (flush if single)
00327 //void PBuffer::Swap ()
00328 //{}
00329 //-----------------------------------------------------------------------------
00330 // Check to see if the PBuffer was lost.
00331 //   If it was lost, destroy it and then recreate it.
00332 void PBuffer::HandleModeSwitch ()
00333 {
00334     int lost = 0;
00335     wglQueryPbufferARB( m_hPBuffer, WGL_PBUFFER_LOST_ARB, &lost );
00336     if ( lost ) {
00337         this->~PBuffer();
00338         Create( m_iWidth, m_iHeight, m_bSharedContextStatus, m_bShareObjectsStatus );
00339     }
00340 }
00341 //-----------------------------------------------------------------------------
00342 void PBuffer::ParseModeString (
00343     const char *modeString, 
00344     std::vector<int> *pfAttribList, 
00345     std::vector<int> *pbAttribList
00346 )
00347 {
00348     if ( !modeString || strcmp( modeString, "" ) == 0 ) return;
00349     //----------------------------------------------------------------
00350     m_iBitsPerComponent = 8;
00351     m_iNoOfComponents   = 0;
00352     bool bIsFloatBuffer     = false;
00353     bool bIsATIFloatBuffer  = false;
00354     bool bIsTexture         = false;
00355     bool bIsAlphaNeed       = false;
00356     //----------------------------------------------------------------
00357     char *mode = strdup( modeString );
00358     //----------------------------------------------------------------
00359     std::vector<std::string> vsTokens;
00360     char *buf = strtok( mode, " " );
00361     while ( buf != NULL ) {
00362         if      ( strstr( buf, "ati_float" ) != NULL )  bIsATIFloatBuffer   = true;
00363         else if ( strstr( buf, "float" ) != NULL )      bIsFloatBuffer      = true;
00364         if ( strstr( buf, "texture" ) != NULL ) bIsTexture      = true;
00365         if ( strstr( buf, "alpha" ) != NULL )   bIsAlphaNeed    = true;
00366         vsTokens.push_back( buf );
00367         buf = strtok( NULL, " " );  // next token
00368     }
00369     //----------------------------------------------------------------
00370     pfAttribList->push_back( WGL_PIXEL_TYPE_ARB );
00371 #ifdef WGL_ATI_pixel_format_float
00372     if ( bIsATIFloatBuffer )    pfAttribList->push_back( WGL_TYPE_RGBA_FLOAT_ATI );
00373     else
00374 #endif
00375     { pfAttribList->push_back( WGL_TYPE_RGBA_ARB ); }
00376     //----------------------------------------------------------------
00377     for ( unsigned int i = 0; i < vsTokens.size(); ++i ) {
00378         std::string sToken = vsTokens[i];
00379         //------------------------------------------------------
00380         if ( sToken == "rgb" && m_iNoOfComponents <= 1 ) {
00381             //pfAttribList->push_back( WGL_RED_BITS_ARB );
00382             //pfAttribList->push_back( m_iBitsPerComponent );
00383             //pfAttribList->push_back( WGL_GREEN_BITS_ARB );
00384             //pfAttribList->push_back( m_iBitsPerComponent );
00385             //pfAttribList->push_back( WGL_BLUE_BITS_ARB );
00386             //pfAttribList->push_back( m_iBitsPerComponent );
00387             m_iNoOfComponents += 3;
00388             continue;
00389         }
00390         else if ( sToken == "rgb" ) {
00391             DEBUG_MESSAGE_TAPs_PIXEL_BUFFER_HPP ( stdout, 
00392                 "PBuffer Warning: Mistake in components definition (rgb + %d)!\n", 
00393                 m_iNoOfComponents 
00394             );
00395         }
00396         //------------------------------------------------------
00397         if ( sToken == "rgba" && m_iNoOfComponents == 0 ) {
00398             //pfAttribList->push_back( WGL_RED_BITS_ARB );
00399             //pfAttribList->push_back( m_iBitsPerComponent );
00400             //pfAttribList->push_back( WGL_GREEN_BITS_ARB );
00401             //pfAttribList->push_back( m_iBitsPerComponent );
00402             //pfAttribList->push_back( WGL_BLUE_BITS_ARB );
00403             //pfAttribList->push_back( m_iBitsPerComponent );
00404             //pfAttribList->push_back( WGL_ALPHA_BITS_ARB );
00405             //pfAttribList->push_back( m_iBitsPerComponent );
00406             m_iNoOfComponents = 4;
00407             continue;
00408         }
00409         else if ( sToken == "rgba" ) {
00410             DEBUG_MESSAGE_TAPs_PIXEL_BUFFER_HPP ( stdout, 
00411                 "PBuffer Warning: Mistake in components definition (rgba + %d)!\n", 
00412                 m_iNoOfComponents 
00413             );
00414         }
00415         //------------------------------------------------------
00416         if ( sToken == "alpha" && m_iNoOfComponents <= 3 ) {
00417             //pfAttribList->push_back( WGL_ALPHA_BITS_ARB );
00418             //pfAttribList->push_back( m_iBitsPerComponent );
00419             ++m_iNoOfComponents;
00420             continue;
00421         }
00422         else if ( sToken == "alpha" ) {
00423             DEBUG_MESSAGE_TAPs_PIXEL_BUFFER_HPP ( stdout, 
00424                 "PBuffer Warning: Mistake in components definition (alpha + %d)!\n", 
00425                 m_iNoOfComponents 
00426             );
00427         }
00428         //------------------------------------------------------
00429         if ( sToken == "r" && m_iNoOfComponents <= 1 ) {
00430             //pfAttribList->push_back( WGL_RED_BITS_ARB );
00431             //pfAttribList->push_back( m_iBitsPerComponent );
00432             ++m_iNoOfComponents;
00433             continue;
00434         }
00435         else if ( sToken == "r" ) {
00436             DEBUG_MESSAGE_TAPs_PIXEL_BUFFER_HPP ( stdout, 
00437                 "PBuffer Warning: Mistake in components definition (r + %d)!\n", 
00438                 m_iNoOfComponents 
00439             );
00440         }
00441         //------------------------------------------------------
00442         if ( sToken == "rg" && m_iNoOfComponents <= 1 ) {
00443             //pfAttribList->push_back( WGL_RED_BITS_ARB );
00444             //pfAttribList->push_back( m_iBitsPerComponent );
00445             //pfAttribList->push_back( WGL_GREEN_BITS_ARB );
00446             //pfAttribList->push_back( m_iBitsPerComponent );
00447             m_iNoOfComponents += 2;
00448             continue;
00449         }
00450         else if ( sToken == "rg" ) {
00451             DEBUG_MESSAGE_TAPs_PIXEL_BUFFER_HPP ( stdout, 
00452                 "PBuffer Warning: Mistake in components definition (rg + %d)!\n", 
00453                 m_iNoOfComponents 
00454             );
00455         }
00456         //------------------------------------------------------
00457         if ( sToken.find( "depth" ) == 0 ) {
00458             pfAttribList->push_back( WGL_DEPTH_BITS_ARB );
00459             pfAttribList->push_back( GetIntValue( sToken ) );
00460             continue;
00461         }
00462         //------------------------------------------------------
00463         if ( sToken.find( "stencil" ) == 0 ) {
00464             pfAttribList->push_back( WGL_STENCIL_BITS_ARB );
00465             pfAttribList->push_back( 8 );
00466             continue;
00467         }
00468         //------------------------------------------------------
00469         if ( sToken.find( "samples" ) == 0 ) {
00470             pfAttribList->push_back( WGL_SAMPLE_BUFFERS_ARB );
00471             pfAttribList->push_back( 1 );
00472             pfAttribList->push_back( WGL_SAMPLES_ARB );
00473             pfAttribList->push_back( GetIntValue( sToken ) );
00474             continue;
00475         }
00476         //------------------------------------------------------
00477         if ( sToken.find( "aux" ) == 0 ) {
00478             pfAttribList->push_back( WGL_AUX_BUFFERS_ARB );
00479             pfAttribList->push_back( GetIntValue( sToken ) );
00480             continue;
00481         }
00482         //------------------------------------------------------
00483         if ( sToken == "double" ) {
00484             pfAttribList->push_back( WGL_DOUBLE_BUFFER_ARB );
00485             pfAttribList->push_back( true );
00486             continue;
00487         }
00488         //------------------------------------------------------
00489         if ( sToken.find( "ati_float" ) == 0 ) {
00490             m_iBitsPerComponent = GetIntValue( sToken );
00491             // Type is already set above
00492             continue;
00493         }
00494         else if ( sToken.find( "float" ) == 0 ) {
00495             m_iBitsPerComponent = GetIntValue( sToken );
00496             // bIsFloatBuffer = true; has been set earlier
00497             pfAttribList->push_back( WGL_FLOAT_COMPONENTS_NV );
00498             pfAttribList->push_back( true );
00499             continue;
00500         }
00501         //------------------------------------------------------
00502         if ( sToken.find( "texture" ) == 0 ) {
00503             if ( sToken.find( "textureRECT" ) == 0 || bIsFloatBuffer ) {
00504                 pbAttribList->push_back( WGL_TEXTURE_TARGET_ARB );
00505                 pbAttribList->push_back( WGL_TEXTURE_RECTANGLE_NV );
00506             }
00507             else if ( sToken.find( "textureCUBE" ) == 0 ) {
00508                 pbAttribList->push_back( WGL_TEXTURE_TARGET_ARB );
00509                 pbAttribList->push_back( WGL_TEXTURE_CUBE_MAP_ARB );
00510             }
00511             else {
00512                 pbAttribList->push_back( WGL_TEXTURE_TARGET_ARB );
00513                 pbAttribList->push_back( WGL_TEXTURE_2D_ARB );
00514             }
00515             //---------------------------------------------
00516             if ( bIsFloatBuffer || bIsATIFloatBuffer ) {
00517                 if ( m_iNoOfComponents == 0 ) {
00518                     DEBUG_MESSAGE_TAPs_PIXEL_BUFFER_HPP ( stdout, 
00519                         "PBuffer Warning: Components not specified! Assuming rgba...\n" 
00520                     );
00521                     pfAttribList->push_back( WGL_RED_BITS_ARB );
00522                     pfAttribList->push_back( m_iBitsPerComponent );
00523                     pfAttribList->push_back( WGL_GREEN_BITS_ARB );
00524                     pfAttribList->push_back( m_iBitsPerComponent );
00525                     pfAttribList->push_back( WGL_BLUE_BITS_ARB );
00526                     pfAttribList->push_back( m_iBitsPerComponent );
00527                     pfAttribList->push_back( WGL_ALPHA_BITS_ARB );
00528                     pfAttribList->push_back( m_iBitsPerComponent );
00529                     m_iNoOfComponents = 4;
00530                 }
00531             }
00532             //---------------------------------------------
00533             if ( bIsFloatBuffer ) {
00534                 switch ( m_iNoOfComponents ) {
00535                     case 1:
00536                         pfAttribList->push_back( WGL_BIND_TO_TEXTURE_RECTANGLE_FLOAT_R_NV );
00537                         pfAttribList->push_back( true );
00538                         pbAttribList->push_back( WGL_TEXTURE_FORMAT_ARB );
00539                         pbAttribList->push_back( WGL_TEXTURE_FLOAT_R_NV );
00540                         break;
00541                     case 2:
00542                         pfAttribList->push_back( WGL_BIND_TO_TEXTURE_RECTANGLE_FLOAT_RG_NV );
00543                         pfAttribList->push_back( true );
00544                         pbAttribList->push_back( WGL_TEXTURE_FORMAT_ARB );
00545                         pbAttribList->push_back( WGL_TEXTURE_FLOAT_RG_NV );
00546                         break;
00547                     case 3:
00548                         pfAttribList->push_back( WGL_BIND_TO_TEXTURE_RECTANGLE_FLOAT_RGB_NV );
00549                         pfAttribList->push_back( true );
00550                         pbAttribList->push_back( WGL_TEXTURE_FORMAT_ARB );
00551                         pbAttribList->push_back( WGL_TEXTURE_FLOAT_RGB_NV );
00552                         break;
00553                     case 4:
00554                         pfAttribList->push_back( WGL_BIND_TO_TEXTURE_RECTANGLE_FLOAT_RGBA_NV );
00555                         pfAttribList->push_back( true );
00556                         pbAttribList->push_back( WGL_TEXTURE_FORMAT_ARB );
00557                         pbAttribList->push_back( WGL_TEXTURE_FLOAT_RGBA_NV );
00558                         break;
00559                     default:
00560                         DEBUG_MESSAGE_TAPs_PIXEL_BUFFER_HPP ( stdout, 
00561                             "PBuffer ERROR: Bad Number of Components (r=1, rg=2, rgb=3, rgba=4): %d!\n", 
00562                             m_iNoOfComponents 
00563                         );
00564                 }
00565             }
00566             //---------------------------------------------
00567             else {
00568                 switch ( m_iNoOfComponents ) {
00569                     case 3:
00570                         pfAttribList->push_back( WGL_BIND_TO_TEXTURE_RGB_ARB );
00571                         pfAttribList->push_back( true );
00572                         pbAttribList->push_back( WGL_TEXTURE_FORMAT_ARB );
00573                         pbAttribList->push_back( WGL_TEXTURE_RGB_ARB );
00574                         break;
00575                     case 4:
00576                         pfAttribList->push_back( WGL_BIND_TO_TEXTURE_RGBA_ARB );
00577                         pfAttribList->push_back( true );
00578                         pbAttribList->push_back( WGL_TEXTURE_FORMAT_ARB );
00579                         pbAttribList->push_back( WGL_TEXTURE_RGBA_ARB );
00580                         break;
00581                     default:
00582                         DEBUG_MESSAGE_TAPs_PIXEL_BUFFER_HPP ( stdout, 
00583                             "PBuffer ERROR: Bad Number of Components (r=1, rg=2, rgb=3, rgba=4): %d!\n", 
00584                             m_iNoOfComponents 
00585                         );
00586                 }
00587             }
00588             //---------------------------------------------
00589             std::string sOption = GetStrValue( sToken );
00590             if ( sOption == "depth" ) {
00591                 pfAttribList->push_back( WGL_BIND_TO_TEXTURE_DEPTH_NV );
00592                 pfAttribList->push_back( true );
00593                 pbAttribList->push_back( WGL_DEPTH_TEXTURE_FORMAT_NV );
00594                 pbAttribList->push_back( WGL_TEXTURE_DEPTH_COMPONENT_NV );
00595             }
00596             continue;
00597         }
00598         //------------------------------------------------------
00599         if ( sToken.find( "mipmap" ) == 0 && bIsTexture ) {
00600             pbAttribList->push_back( WGL_MIPMAP_TEXTURE_ARB );
00601             pbAttribList->push_back( true );
00602             continue;
00603         }
00604         //------------------------------------------------------
00605         DEBUG_MESSAGE_TAPs_PIXEL_BUFFER_HPP ( stdout, 
00606             "PBuffer ERROR: Unknown PBuffer Attribute!\n" 
00607         );
00608         //------------------------------------------------------
00609     }
00610     //----------------------------------------------------------------
00611     if ( m_iNoOfComponents > 0 ) {
00612         pfAttribList->push_back( WGL_RED_BITS_ARB );
00613         pfAttribList->push_back( m_iBitsPerComponent );
00614     }
00615     if ( m_iNoOfComponents > 1 ) {
00616         pfAttribList->push_back( WGL_GREEN_BITS_ARB );
00617         pfAttribList->push_back( m_iBitsPerComponent );
00618     }
00619     if ( m_iNoOfComponents > 2 ) {
00620         pfAttribList->push_back( WGL_BLUE_BITS_ARB );
00621         pfAttribList->push_back( m_iBitsPerComponent );
00622     }
00623     if ( m_iNoOfComponents > 3 ) {
00624         pfAttribList->push_back( WGL_ALPHA_BITS_ARB );
00625         pfAttribList->push_back( m_iBitsPerComponent );
00626     }
00627     //----------------------------------------------------------------
00628 }
00629 //-----------------------------------------------------------------------------
00630 //******************************************************************************
00631 
00632 //******************************************************************************
00633 // For UNIX
00634 //******************************************************************************
00635 #elif defined ( UNIX )
00636 //-----------------------------------------------------------------------------
00637 // ADD CODE FOR UNIX HERE
00638 // SEE NVIDIA SDK 9.1 shared/pbuffer.cpp
00639 //-----------------------------------------------------------------------------
00640 //******************************************************************************
00641 
00642 //******************************************************************************
00643 // For MACOS
00644 //******************************************************************************
00645 #elif defined ( MACOS )
00646 //-----------------------------------------------------------------------------
00647 // ADD CODE FOR MACOS HERE
00648 // SEE NVIDIA SDK 9.1 shared/pbuffer.cpp
00649 //-----------------------------------------------------------------------------
00650 //******************************************************************************
00651 #endif
00652 
00653 //******************************************************************************
00654 // For All OS
00655 //******************************************************************************
00656 //-----------------------------------------------------------------------------
00657 // Return the total size in bytes of the PBuffer
00658 unsigned int PBuffer::GetSizeInBytes ()
00659 {
00660     return m_iWidth * m_iHeight * ( m_iNoOfComponents / 8 );
00661 }
00662 //-----------------------------------------------------------------------------
00663 // Make a copy of the entire PBuffer in the memory
00664 //   have to allocate this area (ptr).
00665 //   if want to read a smaller size : 
00666 //     specify it through w and h 
00667 //     otherwise w = h =-1
00668 unsigned int PBuffer::CopyToBuffer (void *ptr, int w /*= -1*/, int h /*= -1*/ )
00669 {
00670     GLenum format;
00671     GLenum type;
00672     //----------------------------------------------------------------
00673     switch ( m_iNoOfComponents ) {
00674         case 1:
00675             format = GL_LUMINANCE;  // Is it right to ask for Red only component?
00676             break;
00677         case 2:
00678             format = GL_LUMINANCE_ALPHA;    // How to ask for GL_RG?
00679             break;
00680         case 3:
00681             format = GL_RGB;
00682             break;
00683         case 4:
00684             format = GL_RGBA;
00685             break;
00686     }
00687     //----------------------------------------------------------------
00688     switch ( m_iBitsPerComponent ) {
00689         case 8:
00690             type = GL_UNSIGNED_BYTE;
00691             break;
00692         case 32:
00693             type = GL_FLOAT;
00694             break;
00695     #ifdef GL_NV_half_float
00696         case 16:
00697             type = GL_HALF_FLOAT_NV;
00698         break;
00699     #endif
00700         default:
00701             DEBUG_MESSAGE_TAPs_PIXEL_BUFFER_HPP ( stdout, 
00702                 "PBuffer ERROR: call PBuffer::CopyToBuffer() failed - Unknown Bits Per Component!\n" 
00703             );
00704         #if defined ( WIN32 )
00705             _asm { int 3 }
00706         #endif
00707     }
00708     //----------------------------------------------------------------
00709     Activate();
00710     if ( w < 0 || m_iWidth < w )    w = m_iWidth;
00711     if ( h < 0 || m_iHeight < h )   h = m_iHeight;
00712     glReadPixels( 0, 0, w, h, format, type, ptr );
00713     Deactivate();
00714     return w * h * ( m_iNoOfComponents / 8 );
00715 }
00716 //-----------------------------------------------------------------------------
00717 std::string PBuffer::GetStrValue ( std::string sToken )
00718 {
00719     size_t pos;
00720     if ( ( pos = sToken.find( "=" ) ) != sToken.npos ) {
00721         std::string sValue = sToken.substr( pos+1, sToken.length() - pos + 1 );
00722         return sValue;
00723     }
00724     else return "";
00725 }
00726 //-----------------------------------------------------------------------------
00727 int PBuffer::GetIntValue ( std::string sToken )
00728 {
00729     size_t pos;
00730     if ( ( pos = sToken.find( "=" ) ) != sToken.npos ) {
00731         std::string sValue = sToken.substr( pos+1, sToken.length() - pos + 1 );
00732         if ( sValue.empty() )   return 1;
00733         return atoi( sValue.c_str() );
00734     }
00735     else return 1;
00736 }
00737 //-----------------------------------------------------------------------------
00738 //=============================================================================
00739 END_NAMESPACE_TAPs__OpenGL
00740 //345678901234567890123456789012345678901234567890123456789012345678901234567890
00741 //--+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines