📄 rendertexture.cpp
字号:
{
_wglGetLastError();
return false;
}
}
if (_bIsDepthTexture && RT_RENDER_TO_TEXTURE == _eUpdateMode)
{
glBindTexture(_iTextureTarget, _iDepthTextureID);
if (wglBindTexImageARB(_hPBuffer, WGL_DEPTH_COMPONENT_NV) == FALSE)
{
_wglGetLastError();
return false;
}
}
// Determine the actual width and height we were able to create.
wglQueryPbufferARB( _hPBuffer, WGL_PBUFFER_WIDTH_ARB, &_iWidth );
wglQueryPbufferARB( _hPBuffer, WGL_PBUFFER_HEIGHT_ARB, &_iHeight );
_bInitialized = true;
// get the actual number of bits allocated:
int attrib = WGL_RED_BITS_ARB;
int value;
_iBits[0] = (wglGetPixelFormatAttribivARB(_hDC, iFormat, 0, 1, &attrib, &value)) ? value : iRBits;
attrib = WGL_GREEN_BITS_ARB;
_iBits[1] = (wglGetPixelFormatAttribivARB(_hDC, iFormat, 0, 1, &attrib, &value)) ? value : iGBits;
attrib = WGL_BLUE_BITS_ARB;
_iBits[2] = (wglGetPixelFormatAttribivARB(_hDC, iFormat, 0, 1, &attrib, &value)) ? value : iBBits;
attrib = WGL_ALPHA_BITS_ARB;
_iBits[3] = (wglGetPixelFormatAttribivARB(_hDC, iFormat, 0, 1, &attrib, &value)) ? value : iABits;
attrib = WGL_DEPTH_BITS_ARB;
_iBits[4] = (wglGetPixelFormatAttribivARB(_hDC, iFormat, 0, 1, &attrib, &value)) ? value : 0;
#if defined(_DEBUG) | defined(DEBUG)
fprintf(stderr, "Created a %dx%d RenderTexture with BPP(%d, %d, %d, %d)\n",
_iWidth, _iHeight, _iBits[0], _iBits[1], _iBits[2], _iBits[3]);
#endif
return true;
}
//------------------------------------------------------------------------------
// Function : RenderTexture::_Invalidate
// Description :
//------------------------------------------------------------------------------
/**
* @fn RenderTexture::_Invalidate()
* @brief Returns the pbuffer memory to the graphics device.
*
*/
bool RenderTexture::_Invalidate()
{
_bFloat = false;
_bRectangle = false;
_bInitialized = false;
_bHasDepth = false;
_bHasStencil = false;
_bMipmap = false;
_bAnisoFilter = false;
_iBits[0] = _iBits[1] = _iBits[2] = _iBits[3] = 0;
if (_bIsTexture)
glDeleteTextures(1, &_iTextureID);
if (_bIsDepthTexture)
glDeleteTextures(1, &_iDepthTextureID);
if ( _hPBuffer )
{
// Check if we are currently rendering in the pbuffer
if (wglGetCurrentContext() == _hGLContext)
wglMakeCurrent(0,0);
wglDeleteContext( _hGLContext);
wglReleasePbufferDCARB( _hPBuffer, _hDC);
wglDestroyPbufferARB( _hPBuffer );
_hPBuffer = 0;
return true;
}
return false;
}
//------------------------------------------------------------------------------
// Function : RenderTexture::Reset
// Description :
//------------------------------------------------------------------------------
/**
* @fn RenderTexture::Reset(int iWidth, int iHeight, unsigned int iMode, bool bIsTexture, bool bIsDepthTexture)
* @brief Resets the resolution of the offscreen buffer.
*
* Causes the buffer to delete itself. User must call Initialize() again
* before use.
*/
bool RenderTexture::Reset(int iWidth, int iHeight, bool bIsTexture /* = true */,
bool bIsDepthTexture /* = false */)
{
if (!_Invalidate())
{
fprintf(stderr, "RenderTexture::Reset(): failed to invalidate.\n");
return false;
}
_iWidth = iWidth;
_iHeight = iHeight;
_bIsTexture = bIsTexture;
_bIsDepthTexture = bIsDepthTexture;
return true;
}
//------------------------------------------------------------------------------
// Function : RenderTexture::BeginCapture
// Description :
//------------------------------------------------------------------------------
/**
* @fn RenderTexture::BeginCapture()
* @brief Activates rendering to the RenderTexture.
*/
bool RenderTexture::BeginCapture()
{
if (!_bInitialized)
{
fprintf(stderr, "RenderTexture::BeginCapture(): Texture is not initialized!\n");
return false;
}
// cache the current context so we can reset it when EndCapture() is called.
_hPreviousDC = wglGetCurrentDC();
if (NULL == _hPreviousDC)
_wglGetLastError();
_hPreviousContext = wglGetCurrentContext();
if (NULL == _hPreviousContext)
_wglGetLastError();
if (_bIsTexture && RT_RENDER_TO_TEXTURE == _eUpdateMode)
{
glBindTexture(_iTextureTarget, _iTextureID);
// release the pbuffer from the render texture object
if (FALSE == wglReleaseTexImageARB(_hPBuffer, WGL_FRONT_LEFT_ARB))
{
_wglGetLastError();
return false;
}
}
if (_bIsDepthTexture && RT_RENDER_TO_TEXTURE == _eUpdateMode)
{
glBindTexture(_iTextureTarget, _iDepthTextureID);
// release the pbuffer from the render texture object
if (FALSE == wglReleaseTexImageARB(_hPBuffer, WGL_DEPTH_COMPONENT_NV))
{
_wglGetLastError();
return false;
}
}
// make the pbuffer's rendering context current.
if (FALSE == wglMakeCurrent( _hDC, _hGLContext))
{
_wglGetLastError();
return false;
}
return true;
}
//------------------------------------------------------------------------------
// Function : RenderTexture::EndCapture
// Description :
//------------------------------------------------------------------------------
/**
* @fn RenderTexture::EndCapture()
* @brief Ends rendering to the RenderTexture.
*/
bool RenderTexture::EndCapture()
{
bool bContextReset = false;
if (!_bInitialized)
{
fprintf(stderr, "RenderTexture::EndCapture(): Texture is not initialized!\n");
return false;
}
if (_bIsTexture)
{
if (RT_RENDER_TO_TEXTURE == _eUpdateMode)
{
// make the previous rendering context current
if (FALSE == wglMakeCurrent( _hPreviousDC, _hPreviousContext))
{
_wglGetLastError();
return false;
}
bContextReset = true;
// bind the pbuffer to the render texture object
glBindTexture(_iTextureTarget, _iTextureID);
if (FALSE == wglBindTexImageARB(_hPBuffer, WGL_FRONT_LEFT_ARB))
{
_wglGetLastError();
return false;
}
}
else
{
glBindTexture(_iTextureTarget, _iTextureID);
glCopyTexSubImage2D(_iTextureTarget, 0, 0, 0, 0, 0, _iWidth, _iHeight);
}
}
if (_bIsDepthTexture)
{
if (RT_RENDER_TO_TEXTURE == _eUpdateMode)
{
// make the previous rendering context current
if(!bContextReset)
{
if (FALSE == wglMakeCurrent( _hPreviousDC, _hPreviousContext))
{
_wglGetLastError();
return false;
}
bContextReset = true;
}
// bind the pbuffer to the render texture object
glBindTexture(_iTextureTarget, _iDepthTextureID);
if (FALSE == wglBindTexImageARB(_hPBuffer, WGL_DEPTH_COMPONENT_NV))
{
_wglGetLastError();
return false;
}
}
else
{
glBindTexture(_iTextureTarget, _iDepthTextureID);
// HOW TO COPY DEPTH TEXTURE??? Supposedly this just magically works...
glCopyTexSubImage2D(_iTextureTarget, 0, 0, 0, 0, 0, _iWidth, _iHeight);
}
}
if(!bContextReset)
{
// make the previous rendering context current
if (FALSE == wglMakeCurrent( _hPreviousDC, _hPreviousContext))
{
_wglGetLastError();
return false;
}
}
return true;
}
void RenderTexture::QueryFormatInfo(int format,int *pformat,unsigned int nformats,int *iattributes)
{
FILE *fp = fopen( "debug.txt", "w" );
if ( !fp )
{
AfxMessageBox("Couldn't open file: debug.txt\n" );
exit( -1 );
}
fprintf( fp, "nformats = %d\n\n", nformats );
int values[MAX_ATTRIBS];
int iatr[MAX_ATTRIBS] = { WGL_PIXEL_TYPE_ARB, WGL_COLOR_BITS_ARB,
WGL_RED_BITS_ARB, WGL_GREEN_BITS_ARB, WGL_BLUE_BITS_ARB,
WGL_ALPHA_BITS_ARB, WGL_DEPTH_BITS_ARB, WGL_STENCIL_BITS_ARB, WGL_ACCUM_BITS_ARB,
WGL_DOUBLE_BUFFER_ARB, WGL_SUPPORT_OPENGL_ARB, WGL_ACCELERATION_ARB };
int niatr = 12;
for ( int j = 0; j < MAX_ATTRIBS; j++ )
{
values[j] = false;
iattributes[j] = iattributes[2*j];
}
for ( unsigned int i = 0; i < nformats; i++ )
{
if ( !wglGetPixelFormatAttribivARB( _hPreviousDC, pformat[i], 0, niatr, iatr, values ) )
{
AfxMessageBox("pbuffer creation error: wglGetPixelFormatAttribiv() failed\n" );
exit( -1 );
}
fprintf( fp, "%d. pformat = %d\n", i, pformat[i] );
fprintf( fp, "--------------------\n" );
for ( int k = 0; k < niatr; k++ )
{
if ( iatr[k] == WGL_PIXEL_TYPE_ARB )
{
if ( values[k] == WGL_TYPE_COLORINDEX_ARB )
fprintf( fp, " Pixel type = WGL_TYPE_COLORINDEX_ARB\n" );
if ( values[k] == WGL_TYPE_RGBA_ARB )
fprintf( fp, " Pixel type = WGL_TYPE_RGBA_ARB\n" );
}
if ( iatr[k] == WGL_COLOR_BITS_ARB )
{
fprintf( fp, " Color bits = %d\n", values[k] );
}
if ( iatr[k] == WGL_RED_BITS_ARB )
{
fprintf( fp, " red %d\n", values[k] );
}
if ( iatr[k] == WGL_GREEN_BITS_ARB )
{
fprintf( fp, " green %d\n", values[k] );
}
if ( iatr[k] == WGL_BLUE_BITS_ARB )
{
fprintf( fp, " blue %d\n", values[k] );
}
if ( iatr[k] == WGL_ALPHA_BITS_ARB )
{
fprintf( fp, " alpha %d\n", values[k] );
}
if ( iatr[k] == WGL_DEPTH_BITS_ARB )
{
fprintf( fp, " Depth bits = %d\n", values[k] );
}
if ( iatr[k] == WGL_STENCIL_BITS_ARB )
{
fprintf( fp, " Stencil bits = %d\n", values[k] );
}
if ( iatr[k] == WGL_ACCUM_BITS_ARB )
{
fprintf( fp, " Accum bits = %d\n", values[k] );
}
if ( iatr[k] == WGL_DOUBLE_BUFFER_ARB )
{
fprintf( fp, " Double Buffer = %d\n", values[k] );
}
if ( iatr[k] == WGL_SUPPORT_OPENGL_ARB )
{
fprintf( fp, " Support OpenGL = %d\n", values[k] );
}
if ( iatr[k] == WGL_ACCELERATION_ARB )
{
if ( values[k] == WGL_FULL_ACCELERATION_ARB )
fprintf( fp, " Acceleration = WGL_FULL_ACCELERATION_ARB\n" );
if ( values[k] == WGL_GENERIC_ACCELERATION_ARB )
fprintf( fp, " Acceleration = WGL_GENERIC_ACCELERATION_ARB\n" );
}
}
fprintf( fp, "\n" );
}
fprintf( fp, "selected pformat = %d\n", format );
fprintf(fp, "Created a %d x %d pbuffer\n", _iWidth, _iHeight );
fclose( fp );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -