1 //---------------------------------------------------------------------------
2 // File : RenderTexture.cpp
3 //---------------------------------------------------------------------------
4 // Copyright (c) 2002-2004 Mark J. Harris
5 //---------------------------------------------------------------------------
6 // This software is provided 'as-is', without any express or implied
7 // warranty. In no event will the authors be held liable for any
8 // damages arising from the use of this software.
10 // Permission is granted to anyone to use this software for any
11 // purpose, including commercial applications, and to alter it and
12 // redistribute it freely, subject to the following restrictions:
14 // 1. The origin of this software must not be misrepresented; you
15 // must not claim that you wrote the original software. If you use
16 // this software in a product, an acknowledgment in the product
17 // documentation would be appreciated but is not required.
19 // 2. Altered source versions must be plainly marked as such, and
20 // must not be misrepresented as being the original software.
22 // 3. This notice may not be removed or altered from any source
25 // --------------------------------------------------------------------------
27 // Original RenderTexture class: Mark J. Harris
28 // Original Render to Depth Texture support: Thorsten Scheuermann
29 // Linux Copy-to-texture: Eric Werness
30 // Various Bug Fixes: Daniel (Redge) Sperl
33 // --------------------------------------------------------------------------
35 * @file RenderTexture.cpp
37 * Implementation of class RenderTexture. A multi-format render to
40 #pragma warning(disable:4786)
45 * Jan. 2005, Removed GLEW dependencies, Erik Hofman, Fred Bouvier
49 # include <simgear_config.h>
56 #include <simgear/compiler.h>
57 #include <simgear/screen/extensions.hxx>
58 #include <simgear/screen/RenderTexture.h>
66 #pragma comment(lib, "gdi32.lib") // required for GetPixelFormat()
72 static bool fctPtrInited = false;
73 /* WGL_ARB_pixel_format */
74 static wglChoosePixelFormatARBProc wglChoosePixelFormatARBPtr = 0;
75 static wglGetPixelFormatAttribivARBProc wglGetPixelFormatAttribivARBPtr = 0;
77 static wglCreatePbufferARBProc wglCreatePbufferARBPtr = 0;
78 static wglGetPbufferDCARBProc wglGetPbufferDCARBPtr = 0;
79 static wglQueryPbufferARBProc wglQueryPbufferARBPtr = 0;
80 static wglReleasePbufferDCARBProc wglReleasePbufferDCARBPtr = 0;
81 static wglDestroyPbufferARBProc wglDestroyPbufferARBPtr = 0;
82 /* WGL_ARB_render_texture */
83 static wglBindTexImageARBProc wglBindTexImageARBPtr = 0;
84 static wglReleaseTexImageARBProc wglReleaseTexImageARBPtr = 0;
87 //---------------------------------------------------------------------------
88 // Function : RenderTexture::RenderTexture
90 //---------------------------------------------------------------------------
92 * @fn RenderTexture::RenderTexture()
93 * @brief Mode-string-based Constructor.
95 RenderTexture::RenderTexture(const char *strMode)
99 _bIsDepthTexture(false),
100 _bHasARBDepthTexture(true), // [Redge]
102 _eUpdateMode(RT_RENDER_TO_TEXTURE),
104 _eUpdateMode(RT_COPY_TO_TEXTURE),
106 _bInitialized(false),
108 _bIsBufferBound(false),
109 _iCurrentBoundBuffer(0),
113 _bDoubleBuffered(false),
117 _bShareObjects(false),
118 _bCopyContext(false),
124 _hPreviousContext(0),
129 _hPreviousContext(0),
130 _hPreviousDrawable(0),
132 _iTextureTarget(GL_NONE),
135 _pPoorDepthTexture(0) // [Redge]
137 _iNumColorBits[0] = _iNumColorBits[1] =
138 _iNumColorBits[2] = _iNumColorBits[3] = 0;
141 _pixelFormatAttribs.push_back(WGL_DRAW_TO_PBUFFER_ARB);
142 _pixelFormatAttribs.push_back(true);
143 _pixelFormatAttribs.push_back(WGL_SUPPORT_OPENGL_ARB);
144 _pixelFormatAttribs.push_back(true);
146 _pbufferAttribs.push_back(WGL_PBUFFER_LARGEST_ARB);
147 _pbufferAttribs.push_back(true);
149 _pbufferAttribs.push_back(GLX_RENDER_TYPE_SGIX);
150 _pbufferAttribs.push_back(GLX_RGBA_BIT_SGIX);
151 _pbufferAttribs.push_back(GLX_DRAWABLE_TYPE_SGIX);
152 _pbufferAttribs.push_back(GLX_PBUFFER_BIT_SGIX);
155 _ParseModeString(strMode, _pixelFormatAttribs, _pbufferAttribs);
158 _pixelFormatAttribs.push_back(0);
159 _pbufferAttribs.push_back(0);
161 _pixelFormatAttribs.push_back(None);
166 //---------------------------------------------------------------------------
167 // Function : RenderTexture::~RenderTexture
169 //---------------------------------------------------------------------------
171 * @fn RenderTexture::~RenderTexture()
174 RenderTexture::~RenderTexture()
180 //---------------------------------------------------------------------------
181 // Function : _wglGetLastError
183 //---------------------------------------------------------------------------
185 * @fn wglGetLastError()
186 * @brief Returns the last windows error generated.
189 void _wglGetLastError()
193 DWORD err = GetLastError();
196 case ERROR_INVALID_PIXEL_FORMAT:
198 "RenderTexture Win32 Error: ERROR_INVALID_PIXEL_FORMAT\n");
200 case ERROR_NO_SYSTEM_RESOURCES:
202 "RenderTexture Win32 Error: ERROR_NO_SYSTEM_RESOURCES\n");
204 case ERROR_INVALID_DATA:
206 "RenderTexture Win32 Error: ERROR_INVALID_DATA\n");
208 case ERROR_INVALID_WINDOW_HANDLE:
210 "RenderTexture Win32 Error: ERROR_INVALID_WINDOW_HANDLE\n");
212 case ERROR_RESOURCE_TYPE_NOT_FOUND:
214 "RenderTexture Win32 Error: ERROR_RESOURCE_TYPE_NOT_FOUND\n");
221 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
222 FORMAT_MESSAGE_FROM_SYSTEM |
223 FORMAT_MESSAGE_IGNORE_INSERTS,
226 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
231 fprintf(stderr, "RenderTexture Win32 Error %d: %s\n", err, lpMsgBuf);
232 LocalFree( lpMsgBuf );
241 //---------------------------------------------------------------------------
242 // Function : PrintExtensionError
244 //---------------------------------------------------------------------------
246 * @fn PrintExtensionError( char* strMsg, ... )
247 * @brief Prints an error about missing OpenGL extensions.
249 void PrintExtensionError( char* strMsg, ... )
252 "Error: RenderTexture requires the following unsupported "
253 "OpenGL extensions: \n");
256 va_start(args, strMsg);
258 _vsnprintf( strBuffer, 512, strMsg, args );
260 vsnprintf( strBuffer, 512, strMsg, args );
264 fprintf(stderr, strMsg);
267 //---------------------------------------------------------------------------
268 // Function : RenderTexture::Initialize
270 //---------------------------------------------------------------------------
272 * @fn RenderTexture::Initialize(int width, int height, bool shareObjects, bool copyContext);
273 * @brief Initializes the RenderTexture, sharing display lists and textures if specified.
275 * This function creates of the p-buffer. It can only be called once a GL
276 * context has already been created.
278 bool RenderTexture::Initialize(int width, int height,
279 bool shareObjects /* = true */,
280 bool copyContext /* = false */)
282 assert(width > 0 && height > 0);
284 _iWidth = width; _iHeight = height;
285 _bPowerOf2 = IsPowerOfTwo(width) && IsPowerOfTwo(height);
287 _bShareObjects = shareObjects;
288 _bCopyContext = copyContext;
290 // Check if this is an NVXX GPU and verify necessary extensions.
291 if (!_VerifyExtensions())
298 // Get the current context.
299 HDC hdc = wglGetCurrentDC();
302 HGLRC hglrc = wglGetCurrentContext();
307 unsigned int iNumFormats;
311 // Get the pixel format for the on-screen window.
312 iFormat = GetPixelFormat(hdc);
316 "RenderTexture Error: GetPixelFormat() failed.\n");
322 if (!wglChoosePixelFormatARBPtr(hdc, &_pixelFormatAttribs[0], NULL,
323 1, &iFormat, &iNumFormats))
326 "RenderTexture Error: wglChoosePixelFormatARB() failed.\n");
330 if ( iNumFormats <= 0 )
333 "RenderTexture Error: Couldn't find a suitable "
340 // Create the p-buffer.
341 _hPBuffer = wglCreatePbufferARBPtr(hdc, iFormat, _iWidth, _iHeight,
342 &_pbufferAttribs[0]);
346 "RenderTexture Error: wglCreatePbufferARB() failed.\n");
351 // Get the device context.
352 _hDC = wglGetPbufferDCARBPtr( _hPBuffer);
356 "RenderTexture Error: wglGetGetPbufferDCARB() failed.\n");
361 // Create a gl context for the p-buffer.
364 // Let's use the same gl context..
365 // Since the device contexts are compatible (i.e. same pixelformat),
366 // we should be able to use the same gl rendering context.
371 _hGLContext = wglCreateContext( _hDC );
375 "RenderTexture Error: wglCreateContext() failed.\n");
381 // Share lists, texture objects, and program objects.
384 if( !wglShareLists(hglrc, _hGLContext) )
387 "RenderTexture Error: wglShareLists() failed.\n");
393 // Determine the actual width and height we were able to create.
394 wglQueryPbufferARBPtr( _hPBuffer, WGL_PBUFFER_WIDTH_ARB, &_iWidth );
395 wglQueryPbufferARBPtr( _hPBuffer, WGL_PBUFFER_HEIGHT_ARB, &_iHeight );
397 _bInitialized = true;
399 // get the actual number of bits allocated:
400 int attrib = WGL_RED_BITS_ARB;
404 (wglGetPixelFormatAttribivARBPtr(_hDC, iFormat, 0, 1, &attrib, &value))
406 attrib = WGL_GREEN_BITS_ARB;
408 (wglGetPixelFormatAttribivARBPtr(_hDC, iFormat, 0, 1, &attrib, &value))
410 attrib = WGL_BLUE_BITS_ARB;
412 (wglGetPixelFormatAttribivARBPtr(_hDC, iFormat, 0, 1, &attrib, &value))
414 attrib = WGL_ALPHA_BITS_ARB;
416 (wglGetPixelFormatAttribivARBPtr(_hDC, iFormat, 0, 1, &attrib, &value))
418 attrib = WGL_DEPTH_BITS_ARB;
420 (wglGetPixelFormatAttribivARBPtr(_hDC, iFormat, 0, 1, &attrib, &value))
422 attrib = WGL_STENCIL_BITS_ARB;
424 (wglGetPixelFormatAttribivARBPtr(_hDC, iFormat, 0, 1, &attrib, &value))
426 attrib = WGL_DOUBLE_BUFFER_ARB;
428 (wglGetPixelFormatAttribivARBPtr(_hDC, iFormat, 0, 1, &attrib, &value))
429 ? (value?true:false) : false;
431 #if defined(_DEBUG) | defined(DEBUG)
432 fprintf(stderr, "Created a %dx%d RenderTexture with BPP(%d, %d, %d, %d)",
434 _iNumColorBits[0], _iNumColorBits[1],
435 _iNumColorBits[2], _iNumColorBits[3]);
436 if (_iNumDepthBits) fprintf(stderr, " depth=%d", _iNumDepthBits);
437 if (_iNumStencilBits) fprintf(stderr, " stencil=%d", _iNumStencilBits);
438 if (_bDoubleBuffered) fprintf(stderr, " double buffered");
439 fprintf(stderr, "\n");
443 _pDisplay = glXGetCurrentDisplay();
444 GLXContext context = glXGetCurrentContext();
445 int screen = DefaultScreen(_pDisplay);
446 XVisualInfo *visInfo;
452 GLXFBConfigSGIX *fbConfigs;
455 fbConfigs = glXChooseFBConfigSGIX(_pDisplay, screen,
456 &_pixelFormatAttribs[0], &nConfigs);
458 if (nConfigs == 0 || !fbConfigs)
461 "RenderTexture Error: Couldn't find a suitable pixel format.\n");
465 // Pick the first returned format that will return a pbuffer
466 for (int i=0;i<nConfigs;i++)
468 _hPBuffer = glXCreateGLXPbufferSGIX(_pDisplay, fbConfigs[i],
469 _iWidth, _iHeight, NULL);
472 _hGLContext = glXCreateContextWithConfigSGIX(_pDisplay,
475 _bShareObjects ? context : NULL,
484 "RenderTexture Error: glXCreateGLXPbufferSGIX() failed.\n");
491 _hGLContext = glXCreateContext(_pDisplay, visInfo,
492 _bShareObjects ? context : NULL, False);
496 "RenderTexture Error: glXCreateContext() failed.\n");
501 glXQueryGLXPbufferSGIX(_pDisplay, _hPBuffer, GLX_WIDTH_SGIX,
503 glXQueryGLXPbufferSGIX(_pDisplay, _hPBuffer, GLX_HEIGHT_SGIX,
506 _bInitialized = true;
508 // XXX Query the color format
513 // Now that the pbuffer is created, allocate any texture objects needed,
514 // and initialize them (for CTT updates only). These must be allocated
515 // in the context of the pbuffer, though, or the RT won't work without
518 if (false == wglMakeCurrent( _hDC, _hGLContext))
524 _hPreviousContext = glXGetCurrentContext();
525 _hPreviousDrawable = glXGetCurrentDrawable();
527 if (False == glXMakeCurrent(_pDisplay, _hPBuffer, _hGLContext))
533 bool result = _InitializeTextures();
535 BindBuffer(WGL_FRONT_LEFT_ARB);
541 // make the previous rendering context current
542 if (false == wglMakeCurrent( hdc, hglrc))
548 if (False == glXMakeCurrent(_pDisplay,
549 _hPreviousDrawable, _hPreviousContext))
559 //---------------------------------------------------------------------------
560 // Function : RenderTexture::_Invalidate
562 //---------------------------------------------------------------------------
564 * @fn RenderTexture::_Invalidate()
565 * @brief Returns the pbuffer memory to the graphics device.
568 bool RenderTexture::_Invalidate()
570 _iNumColorBits[0] = _iNumColorBits[1] =
571 _iNumColorBits[2] = _iNumColorBits[3] = 0;
573 _iNumStencilBits = 0;
576 glDeleteTextures(1, &_iTextureID);
577 if (_bIsDepthTexture)
580 if (!_bHasARBDepthTexture) delete[] _pPoorDepthTexture;
582 glDeleteTextures(1, &_iDepthTextureID);
588 // Check if we are currently rendering in the pbuffer
589 if (wglGetCurrentContext() == _hGLContext)
592 wglDeleteContext( _hGLContext);
593 wglReleasePbufferDCARBPtr( _hPBuffer, _hDC);
594 wglDestroyPbufferARBPtr( _hPBuffer );
601 if(glXGetCurrentContext() == _hGLContext)
602 // XXX I don't know if this is right at all
603 glXMakeCurrent(_pDisplay, _hPBuffer, 0);
604 glXDestroyGLXPbufferSGIX(_pDisplay, _hPBuffer);
610 // [WVB] do we need to call _ReleaseBoundBuffers() too?
615 //---------------------------------------------------------------------------
616 // Function : RenderTexture::Reset
618 //---------------------------------------------------------------------------
620 * @fn RenderTexture::Reset()
621 * @brief Resets the resolution of the offscreen buffer.
623 * Causes the buffer to delete itself. User must call Initialize() again
626 bool RenderTexture::Reset(const char *strMode, ...)
628 _iWidth = 0; _iHeight = 0;
629 _bIsTexture = false; _bIsDepthTexture = false,
630 _bHasARBDepthTexture = true;
632 _eUpdateMode = RT_RENDER_TO_TEXTURE;
634 _eUpdateMode = RT_COPY_TO_TEXTURE;
636 _bInitialized = false;
638 _bIsBufferBound = false;
639 _iCurrentBoundBuffer = 0;
640 _iNumDepthBits = 0; _iNumStencilBits = 0;
641 _bDoubleBuffered = false;
642 _bFloat = false; _bPowerOf2 = true;
643 _bRectangle = false; _bMipmap = false;
644 _bShareObjects = false; _bCopyContext = false;
645 _iTextureTarget = GL_NONE; _iTextureID = 0;
646 _iDepthTextureID = 0;
647 _pPoorDepthTexture = 0;
648 _pixelFormatAttribs.clear();
649 _pbufferAttribs.clear();
651 if (IsInitialized() && !_Invalidate())
653 fprintf(stderr, "RenderTexture::Reset(): failed to invalidate.\n");
657 _iNumColorBits[0] = _iNumColorBits[1] =
658 _iNumColorBits[2] = _iNumColorBits[3] = 0;
661 _pixelFormatAttribs.push_back(WGL_DRAW_TO_PBUFFER_ARB);
662 _pixelFormatAttribs.push_back(true);
663 _pixelFormatAttribs.push_back(WGL_SUPPORT_OPENGL_ARB);
664 _pixelFormatAttribs.push_back(true);
666 _pbufferAttribs.push_back(WGL_PBUFFER_LARGEST_ARB);
667 _pbufferAttribs.push_back(true);
669 _pbufferAttribs.push_back(GLX_RENDER_TYPE_SGIX);
670 _pbufferAttribs.push_back(GLX_RGBA_BIT_SGIX);
671 _pbufferAttribs.push_back(GLX_DRAWABLE_TYPE_SGIX);
672 _pbufferAttribs.push_back(GLX_PBUFFER_BIT_SGIX);
677 va_start(args,strMode);
679 _vsnprintf( strBuffer, 256, strMode, args );
681 vsnprintf( strBuffer, 256, strMode, args );
685 _ParseModeString(strBuffer, _pixelFormatAttribs, _pbufferAttribs);
688 _pixelFormatAttribs.push_back(0);
689 _pbufferAttribs.push_back(0);
691 _pixelFormatAttribs.push_back(None);
696 //------------------------------------------------------------------------------
697 // Function : RenderTexture::Resize
699 //------------------------------------------------------------------------------
701 * @fn RenderTexture::Resize(int iWidth, int iHeight)
702 * @brief Changes the size of the offscreen buffer.
704 * Like Reset() this causes the buffer to delete itself.
705 * But unlike Reset(), this call re-initializes the RenderTexture.
706 * Note that Resize() will not work after calling Reset(), or before
707 * calling Initialize() the first time.
709 bool RenderTexture::Resize(int iWidth, int iHeight)
711 if (!_bInitialized) {
712 fprintf(stderr, "RenderTexture::Resize(): must Initialize() first.\n");
715 if (iWidth == _iWidth && iHeight == _iHeight) {
719 // Do same basic work as _Invalidate, but don't reset all our flags
721 glDeleteTextures(1, &_iTextureID);
722 if (_bIsDepthTexture)
723 glDeleteTextures(1, &_iDepthTextureID);
727 // Check if we are currently rendering in the pbuffer
728 if (wglGetCurrentContext() == _hGLContext)
731 wglDeleteContext( _hGLContext);
732 wglReleasePbufferDCARBPtr( _hPBuffer, _hDC);
733 wglDestroyPbufferARBPtr( _hPBuffer );
740 if(glXGetCurrentContext() == _hGLContext)
741 // XXX I don't know if this is right at all
742 glXMakeCurrent(_pDisplay, _hPBuffer, 0);
743 glXDestroyGLXPbufferSGIX(_pDisplay, _hPBuffer);
748 fprintf(stderr, "RenderTexture::Resize(): failed to resize.\n");
751 _bInitialized = false;
752 return Initialize(iWidth, iHeight, _bShareObjects, _bCopyContext);
755 //---------------------------------------------------------------------------
756 // Function : RenderTexture::BeginCapture
758 //---------------------------------------------------------------------------
760 * @fn RenderTexture::BeginCapture()
761 * @brief Activates rendering to the RenderTexture.
763 bool RenderTexture::BeginCapture()
768 "RenderTexture::BeginCapture(): Texture is not initialized!\n");
772 // cache the current context so we can reset it when EndCapture() is called.
773 _hPreviousDC = wglGetCurrentDC();
774 if (NULL == _hPreviousDC)
776 _hPreviousContext = wglGetCurrentContext();
777 if (NULL == _hPreviousContext)
780 _hPreviousContext = glXGetCurrentContext();
781 _hPreviousDrawable = glXGetCurrentDrawable();
784 _ReleaseBoundBuffers();
786 return _MakeCurrent();
790 //---------------------------------------------------------------------------
791 // Function : RenderTexture::EndCapture
793 //---------------------------------------------------------------------------
795 * @fn RenderTexture::EndCapture()
796 * @brief Ends rendering to the RenderTexture.
798 bool RenderTexture::EndCapture()
803 "RenderTexture::EndCapture() : Texture is not initialized!\n");
810 // make the previous rendering context current
811 if (FALSE == wglMakeCurrent( _hPreviousDC, _hPreviousContext))
817 if (False == glXMakeCurrent(_pDisplay, _hPreviousDrawable,
824 // rebind the textures to a buffers for RTT
825 BindBuffer(_iCurrentBoundBuffer);
831 //---------------------------------------------------------------------------
832 // Function : RenderTexture::BeginCapture(RenderTexture*)
834 //---------------------------------------------------------------------------
836 * @fn RenderTexture::BeginCapture(RenderTexture* other)
837 * @brief Ends capture of 'other', begins capture on 'this'
839 * When performing a series of operations where you modify one texture after
840 * another, it is more efficient to use this method instead of the equivalent
841 * 'EndCapture'/'BeginCapture' pair. This method switches directly to the
842 * new context rather than changing to the default context, and then to the
845 * RenderTexture doesn't have any mechanism for determining if
846 * 'current' really is currently active, so no error will be thrown
849 bool RenderTexture::BeginCapture(RenderTexture* current)
851 bool bContextReset = false;
853 if (current == this) {
854 return true; // no switch necessary
857 // treat as normal Begin if current is 0.
858 return BeginCapture();
863 "RenderTexture::BeginCapture(RenderTexture*): Texture is not initialized!\n");
866 if (!current->_bInitialized)
869 "RenderTexture::BeginCapture(RenderTexture): 'current' texture is not initialized!\n");
873 // Sync current pbuffer with its CTT texture if necessary
874 current->_MaybeCopyBuffer();
876 // pass along the previous context so we can reset it when
877 // EndCapture() is called.
879 _hPreviousDC = current->_hPreviousDC;
880 if (NULL == _hPreviousDC)
882 _hPreviousContext = current->_hPreviousContext;
883 if (NULL == _hPreviousContext)
886 _hPreviousContext = current->_hPreviousContext;
887 _hPreviousDrawable = current->_hPreviousDrawable;
890 // Unbind textures before making context current
891 if (!_ReleaseBoundBuffers())
894 // Make the pbuffer context current
898 // Rebind buffers of initial RenderTexture
899 current->BindBuffer(_iCurrentBoundBuffer);
900 current->_BindDepthBuffer();
907 //---------------------------------------------------------------------------
908 // Function : RenderTexture::Bind
910 //---------------------------------------------------------------------------
912 * @fn RenderTexture::Bind()
913 * @brief Binds RGB texture.
915 void RenderTexture::Bind() const
917 if (_bInitialized && _bIsTexture)
919 glBindTexture(_iTextureTarget, _iTextureID);
924 //---------------------------------------------------------------------------
925 // Function : RenderTexture::BindDepth
927 //---------------------------------------------------------------------------
929 * @fn RenderTexture::BindDepth()
930 * @brief Binds depth texture.
932 void RenderTexture::BindDepth() const
934 if (_bInitialized && _bIsDepthTexture)
936 glBindTexture(_iTextureTarget, _iDepthTextureID);
941 //---------------------------------------------------------------------------
942 // Function : RenderTexture::BindBuffer
944 //---------------------------------------------------------------------------
946 * @fn RenderTexture::BindBuffer()
947 * @brief Associate the RTT texture id with 'iBuffer' (e.g. WGL_FRONT_LEFT_ARB)
949 bool RenderTexture::BindBuffer( int iBuffer )
951 // Must bind the texture too
952 if (_bInitialized && _bIsTexture)
954 glBindTexture(_iTextureTarget, _iTextureID);
957 if (RT_RENDER_TO_TEXTURE == _eUpdateMode && _bIsTexture &&
958 (!_bIsBufferBound || _iCurrentBoundBuffer != iBuffer))
960 if (FALSE == wglBindTexImageARBPtr(_hPBuffer, iBuffer))
962 // WVB: WGL API considers binding twice to the same buffer
963 // to be an error. But we don't want to
964 //_wglGetLastError();
968 _bIsBufferBound = true;
969 _iCurrentBoundBuffer = iBuffer;
977 //---------------------------------------------------------------------------
978 // Function : RenderTexture::BindBuffer
980 //---------------------------------------------------------------------------
982 * @fn RenderTexture::_BindDepthBuffer()
983 * @brief Associate the RTT depth texture id with the depth buffer
985 bool RenderTexture::_BindDepthBuffer() const
988 if (_bInitialized && _bIsDepthTexture &&
989 RT_RENDER_TO_TEXTURE == _eUpdateMode)
991 glBindTexture(_iTextureTarget, _iDepthTextureID);
992 if (FALSE == wglBindTexImageARBPtr(_hPBuffer, WGL_DEPTH_COMPONENT_NV))
1002 //---------------------------------------------------------------------------
1003 // Function : RenderTexture::_ParseModeString
1005 //---------------------------------------------------------------------------
1007 * @fn RenderTexture::_ParseModeString()
1008 * @brief Parses the user-specified mode string for RenderTexture parameters.
1010 void RenderTexture::_ParseModeString(const char *modeString,
1011 vector<int> &pfAttribs,
1012 vector<int> &pbAttribs)
1014 if (!modeString || strcmp(modeString, "") == 0)
1017 _iNumComponents = 0;
1019 _eUpdateMode = RT_RENDER_TO_TEXTURE;
1021 _eUpdateMode = RT_COPY_TO_TEXTURE;
1025 bool bHasStencil = false;
1026 bool bBind2D = false;
1027 bool bBindRECT = false;
1028 bool bBindCUBE = false;
1030 char *mode = strdup(modeString);
1033 vector<string> tokens;
1034 char *buf = strtok(mode, " ");
1037 tokens.push_back(buf);
1038 buf = strtok(NULL, " ");
1041 for (unsigned int i = 0; i < tokens.size(); i++)
1043 string token = tokens[i];
1045 KeyVal kv = _GetKeyValuePair(token);
1048 if (kv.first == "rgb" && (_iNumComponents <= 1))
1050 if (kv.second.find("f") != kv.second.npos)
1053 vector<int> bitVec = _ParseBitVector(kv.second);
1055 if (bitVec.size() < 3) // expand the scalar to a vector
1057 bitVec.push_back(bitVec[0]);
1058 bitVec.push_back(bitVec[0]);
1062 pfAttribs.push_back(WGL_RED_BITS_ARB);
1063 pfAttribs.push_back(bitVec[0]);
1064 pfAttribs.push_back(WGL_GREEN_BITS_ARB);
1065 pfAttribs.push_back(bitVec[1]);
1066 pfAttribs.push_back(WGL_BLUE_BITS_ARB);
1067 pfAttribs.push_back(bitVec[2]);
1070 pfAttribs.push_back(GLX_RED_SIZE);
1071 pfAttribs.push_back(bitVec[0]);
1072 pfAttribs.push_back(GLX_GREEN_SIZE);
1073 pfAttribs.push_back(bitVec[1]);
1074 pfAttribs.push_back(GLX_BLUE_SIZE);
1075 pfAttribs.push_back(bitVec[2]);
1078 _iNumComponents += 3;
1081 else if (kv.first == "rgb")
1083 "RenderTexture Warning: mistake in components definition "
1088 if (kv.first == "rgba" && (_iNumComponents == 0))
1090 if (kv.second.find("f") != kv.second.npos)
1093 vector<int> bitVec = _ParseBitVector(kv.second);
1095 if (bitVec.size() < 4) // expand the scalar to a vector
1097 bitVec.push_back(bitVec[0]);
1098 bitVec.push_back(bitVec[0]);
1099 bitVec.push_back(bitVec[0]);
1103 pfAttribs.push_back(WGL_RED_BITS_ARB);
1104 pfAttribs.push_back(bitVec[0]);
1105 pfAttribs.push_back(WGL_GREEN_BITS_ARB);
1106 pfAttribs.push_back(bitVec[1]);
1107 pfAttribs.push_back(WGL_BLUE_BITS_ARB);
1108 pfAttribs.push_back(bitVec[2]);
1109 pfAttribs.push_back(WGL_ALPHA_BITS_ARB);
1110 pfAttribs.push_back(bitVec[3]);
1113 pfAttribs.push_back(GLX_RED_SIZE);
1114 pfAttribs.push_back(bitVec[0]);
1115 pfAttribs.push_back(GLX_GREEN_SIZE);
1116 pfAttribs.push_back(bitVec[1]);
1117 pfAttribs.push_back(GLX_BLUE_SIZE);
1118 pfAttribs.push_back(bitVec[2]);
1119 pfAttribs.push_back(GLX_ALPHA_SIZE);
1120 pfAttribs.push_back(bitVec[3]);
1123 _iNumComponents = 4;
1126 else if (kv.first == "rgba")
1128 "RenderTexture Warning: mistake in components definition "
1132 if (kv.first == "r" && (_iNumComponents <= 1))
1134 if (kv.second.find("f") != kv.second.npos)
1137 vector<int> bitVec = _ParseBitVector(kv.second);
1140 pfAttribs.push_back(WGL_RED_BITS_ARB);
1141 pfAttribs.push_back(bitVec[0]);
1143 pfAttribs.push_back(GLX_RED_SIZE);
1144 pfAttribs.push_back(bitVec[0]);
1149 else if (kv.first == "r")
1151 "RenderTexture Warning: mistake in components definition "
1155 if (kv.first == "rg" && (_iNumComponents <= 1))
1157 if (kv.second.find("f") != kv.second.npos)
1160 vector<int> bitVec = _ParseBitVector(kv.second);
1162 if (bitVec.size() < 2) // expand the scalar to a vector
1164 bitVec.push_back(bitVec[0]);
1168 pfAttribs.push_back(WGL_RED_BITS_ARB);
1169 pfAttribs.push_back(bitVec[0]);
1170 pfAttribs.push_back(WGL_GREEN_BITS_ARB);
1171 pfAttribs.push_back(bitVec[1]);
1173 pfAttribs.push_back(GLX_RED_SIZE);
1174 pfAttribs.push_back(bitVec[0]);
1175 pfAttribs.push_back(GLX_GREEN_SIZE);
1176 pfAttribs.push_back(bitVec[1]);
1178 _iNumComponents += 2;
1181 else if (kv.first == "rg")
1183 "RenderTexture Warning: mistake in components definition "
1187 if (kv.first == "depth")
1189 if (kv.second == "")
1192 iDepthBits = strtol(kv.second.c_str(), 0, 10);
1196 if (kv.first == "stencil")
1200 pfAttribs.push_back(WGL_STENCIL_BITS_ARB);
1202 pfAttribs.push_back(GLX_STENCIL_SIZE);
1204 if (kv.second == "")
1205 pfAttribs.push_back(8);
1207 pfAttribs.push_back(strtol(kv.second.c_str(), 0, 10));
1211 if (kv.first == "samples")
1214 pfAttribs.push_back(WGL_SAMPLE_BUFFERS_ARB);
1215 pfAttribs.push_back(1);
1216 pfAttribs.push_back(WGL_SAMPLES_ARB);
1217 pfAttribs.push_back(strtol(kv.second.c_str(), 0, 10));
1219 pfAttribs.push_back(GL_SAMPLE_BUFFERS_ARB);
1220 pfAttribs.push_back(1);
1221 pfAttribs.push_back(GL_SAMPLES_ARB);
1222 pfAttribs.push_back(strtol(kv.second.c_str(), 0, 10));
1228 if (kv.first == "doublebuffer" || kv.first == "double")
1231 pfAttribs.push_back(WGL_DOUBLE_BUFFER_ARB);
1232 pfAttribs.push_back(true);
1234 pfAttribs.push_back(GL_DOUBLEBUFFER);
1235 pfAttribs.push_back(True);
1240 if (kv.first == "aux")
1243 pfAttribs.push_back(WGL_AUX_BUFFERS_ARB);
1245 pfAttribs.push_back(GL_AUX_BUFFERS);
1247 if (kv.second == "")
1248 pfAttribs.push_back(0);
1250 pfAttribs.push_back(strtol(kv.second.c_str(), 0, 10));
1254 if (token.find("tex") == 0)
1258 if ((kv.first == "texRECT") && GL_NV_texture_rectangle)
1263 else if (kv.first == "texCUBE")
1275 if (token.find("depthTex") == 0)
1277 _bIsDepthTexture = true;
1279 if ((kv.first == "depthTexRECT") && GL_NV_texture_rectangle)
1284 else if (kv.first == "depthTexCUBE")
1296 if (kv.first == "mipmap")
1302 if (kv.first == "rtt")
1304 _eUpdateMode = RT_RENDER_TO_TEXTURE;
1308 if (kv.first == "ctt")
1310 _eUpdateMode = RT_COPY_TO_TEXTURE;
1315 "RenderTexture Error: Unknown pbuffer attribute: %s\n",
1319 // Processing of some options must be last because of interactions.
1321 // Check for inconsistent texture targets
1322 if (_bIsTexture && _bIsDepthTexture && !(bBind2D ^ bBindRECT ^ bBindCUBE))
1325 "RenderTexture Warning: Depth and Color texture targets "
1329 // Apply default bit format if none specified
1331 if (0 == _iNumComponents)
1333 pfAttribs.push_back(WGL_RED_BITS_ARB);
1334 pfAttribs.push_back(8);
1335 pfAttribs.push_back(WGL_GREEN_BITS_ARB);
1336 pfAttribs.push_back(8);
1337 pfAttribs.push_back(WGL_BLUE_BITS_ARB);
1338 pfAttribs.push_back(8);
1339 pfAttribs.push_back(WGL_ALPHA_BITS_ARB);
1340 pfAttribs.push_back(8);
1341 _iNumComponents = 4;
1346 if (_bIsDepthTexture && !iDepthBits)
1350 pfAttribs.push_back(WGL_DEPTH_BITS_ARB);
1352 pfAttribs.push_back(GLX_DEPTH_SIZE);
1354 pfAttribs.push_back(iDepthBits); // default
1359 pfAttribs.push_back(WGL_STENCIL_BITS_ARB);
1360 pfAttribs.push_back(0);
1362 pfAttribs.push_back(GLX_STENCIL_SIZE);
1363 pfAttribs.push_back(0);
1367 if (_iNumComponents < 4)
1369 // Can't do this right now -- on NVIDIA drivers, currently get
1370 // a non-functioning pbuffer if ALPHA_BITS=0 and
1371 // WGL_BIND_TO_TEXTURE_RGB_ARB=true
1373 //pfAttribs.push_back(WGL_ALPHA_BITS_ARB);
1374 //pfAttribs.push_back(0);
1378 if (!WGL_NV_render_depth_texture && _bIsDepthTexture && (RT_RENDER_TO_TEXTURE == _eUpdateMode))
1380 #if defined(DEBUG) || defined(_DEBUG)
1381 fprintf(stderr, "RenderTexture Warning: No support found for "
1382 "render to depth texture.\n");
1384 _bIsDepthTexture = false;
1388 if ((_bIsTexture || _bIsDepthTexture) &&
1389 (RT_RENDER_TO_TEXTURE == _eUpdateMode))
1394 pbAttribs.push_back(WGL_TEXTURE_TARGET_ARB);
1395 pbAttribs.push_back(WGL_TEXTURE_RECTANGLE_NV);
1399 pbAttribs.push_back(WGL_TEXTURE_TARGET_ARB);
1400 pbAttribs.push_back(WGL_TEXTURE_CUBE_MAP_ARB);
1404 pbAttribs.push_back(WGL_TEXTURE_TARGET_ARB);
1405 pbAttribs.push_back(WGL_TEXTURE_2D_ARB);
1410 pbAttribs.push_back(WGL_MIPMAP_TEXTURE_ARB);
1411 pbAttribs.push_back(true);
1414 #elif defined(DEBUG) || defined(_DEBUG)
1415 printf("RenderTexture Error: Render to Texture not "
1416 "supported in Linux\n");
1420 // Set the pixel type
1424 if (WGL_NV_float_buffer)
1426 pfAttribs.push_back(WGL_PIXEL_TYPE_ARB);
1427 pfAttribs.push_back(WGL_TYPE_RGBA_ARB);
1429 pfAttribs.push_back(WGL_FLOAT_COMPONENTS_NV);
1430 pfAttribs.push_back(true);
1434 pfAttribs.push_back(WGL_PIXEL_TYPE_ARB);
1435 pfAttribs.push_back(WGL_TYPE_RGBA_FLOAT_ATI);
1438 if (GL_NV_float_buffer)
1440 pfAttribs.push_back(GLX_FLOAT_COMPONENTS_NV);
1441 pfAttribs.push_back(1);
1448 pfAttribs.push_back(WGL_PIXEL_TYPE_ARB);
1449 pfAttribs.push_back(WGL_TYPE_RGBA_ARB);
1453 // Set up texture binding for render to texture
1454 if (_bIsTexture && (RT_RENDER_TO_TEXTURE == _eUpdateMode))
1460 if (WGL_NV_float_buffer)
1462 switch(_iNumComponents)
1465 pfAttribs.push_back(WGL_BIND_TO_TEXTURE_RECTANGLE_FLOAT_R_NV);
1466 pfAttribs.push_back(true);
1468 pbAttribs.push_back(WGL_TEXTURE_FORMAT_ARB);
1469 pbAttribs.push_back(WGL_TEXTURE_FLOAT_R_NV);
1472 pfAttribs.push_back(WGL_BIND_TO_TEXTURE_RECTANGLE_FLOAT_RG_NV);
1473 pfAttribs.push_back(true);
1475 pbAttribs.push_back(WGL_TEXTURE_FORMAT_ARB);
1476 pbAttribs.push_back(WGL_TEXTURE_FLOAT_RG_NV);
1479 pfAttribs.push_back(WGL_BIND_TO_TEXTURE_RECTANGLE_FLOAT_RGB_NV);
1480 pfAttribs.push_back(true);
1482 pbAttribs.push_back(WGL_TEXTURE_FORMAT_ARB);
1483 pbAttribs.push_back(WGL_TEXTURE_FLOAT_RGB_NV);
1486 pfAttribs.push_back(WGL_BIND_TO_TEXTURE_RECTANGLE_FLOAT_RGBA_NV);
1487 pfAttribs.push_back(true);
1489 pbAttribs.push_back(WGL_TEXTURE_FORMAT_ARB);
1490 pbAttribs.push_back(WGL_TEXTURE_FLOAT_RGBA_NV);
1494 "RenderTexture Warning: Bad number of components "
1495 "(r=1,rg=2,rgb=3,rgba=4): %d.\n",
1502 if (4 == _iNumComponents)
1504 pfAttribs.push_back(WGL_BIND_TO_TEXTURE_RGBA_ARB);
1505 pfAttribs.push_back(true);
1507 pbAttribs.push_back(WGL_TEXTURE_FORMAT_ARB);
1508 pbAttribs.push_back(WGL_TEXTURE_RGBA_ARB);
1512 // standard ARB_render_texture only supports 3 or 4 channels
1513 pfAttribs.push_back(WGL_BIND_TO_TEXTURE_RGB_ARB);
1514 pfAttribs.push_back(true);
1516 pbAttribs.push_back(WGL_TEXTURE_FORMAT_ARB);
1517 pbAttribs.push_back(WGL_TEXTURE_RGB_ARB);
1524 switch(_iNumComponents)
1527 pfAttribs.push_back(WGL_BIND_TO_TEXTURE_RGB_ARB);
1528 pfAttribs.push_back(true);
1530 pbAttribs.push_back(WGL_TEXTURE_FORMAT_ARB);
1531 pbAttribs.push_back(WGL_TEXTURE_RGB_ARB);
1534 pfAttribs.push_back(WGL_BIND_TO_TEXTURE_RGBA_ARB);
1535 pfAttribs.push_back(true);
1537 pbAttribs.push_back(WGL_TEXTURE_FORMAT_ARB);
1538 pbAttribs.push_back(WGL_TEXTURE_RGBA_ARB);
1542 "RenderTexture Warning: Bad number of components "
1543 "(r=1,rg=2,rgb=3,rgba=4): %d.\n", _iNumComponents);
1547 #elif defined(DEBUG) || defined(_DEBUG)
1549 "RenderTexture Error: Render to Texture not supported in "
1554 if (_bIsDepthTexture && (RT_RENDER_TO_TEXTURE == _eUpdateMode))
1559 pfAttribs.push_back(WGL_BIND_TO_TEXTURE_RECTANGLE_DEPTH_NV);
1560 pfAttribs.push_back(true);
1562 pbAttribs.push_back(WGL_DEPTH_TEXTURE_FORMAT_NV);
1563 pbAttribs.push_back(WGL_TEXTURE_DEPTH_COMPONENT_NV);
1567 pfAttribs.push_back(WGL_BIND_TO_TEXTURE_DEPTH_NV);
1568 pfAttribs.push_back(true);
1570 pbAttribs.push_back(WGL_DEPTH_TEXTURE_FORMAT_NV);
1571 pbAttribs.push_back(WGL_TEXTURE_DEPTH_COMPONENT_NV);
1573 #elif defined(DEBUG) || defined(_DEBUG)
1574 printf("RenderTexture Error: Render to Texture not supported in "
1580 //---------------------------------------------------------------------------
1581 // Function : RenderTexture::_GetKeyValuePair
1583 //---------------------------------------------------------------------------
1585 * @fn RenderTexture::_GetKeyValuePair()
1586 * @brief Parses expressions of the form "X=Y" into a pair (X,Y).
1588 RenderTexture::KeyVal RenderTexture::_GetKeyValuePair(string token)
1590 string::size_type pos = 0;
1591 if ((pos = token.find("=")) != token.npos)
1593 string key = token.substr(0, pos);
1594 string value = token.substr(pos+1, token.length()-pos+1);
1595 return KeyVal(key, value);
1598 return KeyVal(token, "");
1601 //---------------------------------------------------------------------------
1602 // Function : RenderTexture::_ParseBitVector
1604 //---------------------------------------------------------------------------
1606 * @fn RenderTexture::_ParseBitVector()
1607 * @brief Parses expressions of the form "=r,g,b,a" into a vector: (r,g,b,a)
1609 vector<int> RenderTexture::_ParseBitVector(string bitVector)
1611 vector<string> pieces;
1614 if (bitVector == "")
1616 bits.push_back(8); // if a depth isn't specified, use default 8 bits
1620 string::size_type pos = 0;
1621 string::size_type nextpos = 0;
1624 nextpos = bitVector.find_first_of(", \n", pos);
1625 pieces.push_back(string(bitVector, pos, nextpos - pos));
1627 } while (nextpos != bitVector.npos );
1629 for ( vector<string>::iterator it = pieces.begin(); it != pieces.end(); it++)
1631 bits.push_back(strtol(it->c_str(), 0, 10));
1637 //---------------------------------------------------------------------------
1638 // Function : RenderTexture::_VerifyExtensions
1640 //---------------------------------------------------------------------------
1642 * @fn RenderTexture::_VerifyExtensions()
1643 * @brief Checks that the necessary extensions are available based on RT mode.
1645 bool RenderTexture::_VerifyExtensions()
1648 if ( !fctPtrInited )
1650 fctPtrInited = true;
1651 if ( SGIsOpenGLExtensionSupported("WGL_ARB_pixel_format" ) )
1653 wglChoosePixelFormatARBPtr = (wglChoosePixelFormatARBProc)SGLookupFunction("wglChoosePixelFormatARB");
1654 wglGetPixelFormatAttribivARBPtr = (wglGetPixelFormatAttribivARBProc)SGLookupFunction("wglGetPixelFormatAttribivARB");
1656 if ( SGIsOpenGLExtensionSupported("WGL_ARB_pbuffer" ) )
1658 wglCreatePbufferARBPtr = (wglCreatePbufferARBProc)SGLookupFunction("wglCreatePbufferARB");
1659 wglGetPbufferDCARBPtr = (wglGetPbufferDCARBProc)SGLookupFunction("wglGetPbufferDCARB");
1660 wglQueryPbufferARBPtr = (wglQueryPbufferARBProc)SGLookupFunction("wglQueryPbufferARB");
1661 wglReleasePbufferDCARBPtr = (wglReleasePbufferDCARBProc)SGLookupFunction("wglReleasePbufferDCARB");
1662 wglDestroyPbufferARBPtr = (wglDestroyPbufferARBProc)SGLookupFunction("wglDestroyPbufferARB");
1664 if ( SGIsOpenGLExtensionSupported("WGL_ARB_render_texture" ) )
1666 wglBindTexImageARBPtr = (wglBindTexImageARBProc)SGLookupFunction("wglBindTexImageARB");
1667 wglReleaseTexImageARBPtr = (wglReleaseTexImageARBProc)SGLookupFunction("wglReleaseTexImageARB");
1670 if (!WGL_ARB_pbuffer)
1672 PrintExtensionError("WGL_ARB_pbuffer");
1675 if (!WGL_ARB_pixel_format)
1677 PrintExtensionError("WGL_ARB_pixel_format");
1680 if (_bIsTexture && !WGL_ARB_render_texture)
1682 PrintExtensionError("WGL_ARB_render_texture");
1685 if (_bRectangle && !GL_NV_texture_rectangle)
1687 PrintExtensionError("GL_NV_texture_rectangle");
1690 if (_bFloat && !(GL_NV_float_buffer || WGL_ATI_pixel_format_float))
1692 PrintExtensionError("GL_NV_float_buffer or GL_ATI_pixel_format_float");
1696 if (_bFloat && _bIsTexture && !(GL_NV_float_buffer || GL_ATI_texture_float))
1698 PrintExtensionError("NV_float_buffer or ATI_texture_float");
1700 if (_bIsDepthTexture && !GL_ARB_depth_texture)
1703 #if defined(_DEBUG) | defined(DEBUG)
1705 "RenderTexture Warning: "
1706 "OpenGL extension GL_ARB_depth_texture not available.\n"
1707 " Using glReadPixels() to emulate behavior.\n");
1709 _bHasARBDepthTexture = false;
1710 //PrintExtensionError("GL_ARB_depth_texture");
1716 if (!GLX_SGIX_pbuffer)
1718 PrintExtensionError("GL_SGIX_pbuffer");
1721 if (!GLX_SGIX_fbconfig)
1723 PrintExtensionError("GL_SGIX_fbconfig");
1726 if (_bIsDepthTexture && !GL_ARB_depth_texture)
1728 PrintExtensionError("GL_ARB_depth_texture");
1731 if (_bFloat && _bIsTexture && !GL_NV_float_buffer)
1733 PrintExtensionError("GL_NV_float_buffer");
1736 if (_eUpdateMode == RT_RENDER_TO_TEXTURE)
1738 PrintExtensionError("Some GLX render texture extension: FIXME!");
1746 //---------------------------------------------------------------------------
1747 // Function : RenderTexture::_InitializeTextures
1749 //---------------------------------------------------------------------------
1751 * @fn RenderTexture::_InitializeTextures()
1752 * @brief Initializes the state of textures used by the RenderTexture.
1754 bool RenderTexture::_InitializeTextures()
1756 // Determine the appropriate texture formats and filtering modes.
1757 if (_bIsTexture || _bIsDepthTexture)
1759 if (_bRectangle && GL_NV_texture_rectangle)
1760 _iTextureTarget = GL_TEXTURE_RECTANGLE_NV;
1762 _iTextureTarget = GL_TEXTURE_2D;
1767 glGenTextures(1, &_iTextureID);
1768 glBindTexture(_iTextureTarget, _iTextureID);
1770 // Use clamp to edge as the default texture wrap mode for all tex
1771 glTexParameteri(_iTextureTarget, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1772 glTexParameteri(_iTextureTarget, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1773 // Use NEAREST as the default texture filtering mode.
1774 glTexParameteri(_iTextureTarget, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1775 glTexParameteri(_iTextureTarget, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1778 if (RT_COPY_TO_TEXTURE == _eUpdateMode)
1780 GLuint iInternalFormat;
1788 "RenderTexture Error: mipmapped float textures not "
1793 switch(_iNumComponents)
1796 if (GL_NV_float_buffer)
1798 iInternalFormat = (_iNumColorBits[0] > 16) ?
1799 GL_FLOAT_R32_NV : GL_FLOAT_R16_NV;
1801 else if (GL_ATI_texture_float)
1803 iInternalFormat = (_iNumColorBits[0] > 16) ?
1804 GL_LUMINANCE_FLOAT32_ATI :
1805 GL_LUMINANCE_FLOAT16_ATI;
1807 iFormat = GL_LUMINANCE;
1810 if (GL_NV_float_buffer)
1812 iInternalFormat = (_iNumColorBits[0] > 16) ?
1813 GL_FLOAT_RG32_NV : GL_FLOAT_RG16_NV;
1815 else if (GL_ATI_texture_float)
1817 iInternalFormat = (_iNumColorBits[0] > 16) ?
1818 GL_LUMINANCE_ALPHA_FLOAT32_ATI :
1819 GL_LUMINANCE_ALPHA_FLOAT16_ATI;
1821 iFormat = GL_LUMINANCE_ALPHA;
1824 if (GL_NV_float_buffer)
1826 iInternalFormat = (_iNumColorBits[0] > 16) ?
1827 GL_FLOAT_RGB32_NV : GL_FLOAT_RGB16_NV;
1829 else if (GL_ATI_texture_float)
1831 iInternalFormat = (_iNumColorBits[0] > 16) ?
1832 GL_RGB_FLOAT32_ATI : GL_RGB_FLOAT16_ATI;
1837 if (GL_NV_float_buffer)
1839 iInternalFormat = (_iNumColorBits[0] > 16) ?
1840 GL_FLOAT_RGBA32_NV : GL_FLOAT_RGBA16_NV;
1842 else if (GL_ATI_texture_float)
1844 iInternalFormat = (_iNumColorBits[0] > 16) ?
1845 GL_RGBA_FLOAT32_ATI : GL_RGBA_FLOAT16_ATI;
1850 printf("RenderTexture Error: "
1851 "Invalid number of components: %d\n",
1858 if (4 == _iNumComponents)
1860 iInternalFormat = GL_RGBA8;
1865 iInternalFormat = GL_RGB8;
1870 // Allocate the texture image (but pass it no data for now).
1871 glTexImage2D(_iTextureTarget, 0, iInternalFormat,
1872 _iWidth, _iHeight, 0, iFormat, GL_FLOAT, NULL);
1876 if (_bIsDepthTexture)
1878 glGenTextures(1, &_iDepthTextureID);
1879 glBindTexture(_iTextureTarget, _iDepthTextureID);
1881 // Use clamp to edge as the default texture wrap mode for all tex
1882 glTexParameteri(_iTextureTarget, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1883 glTexParameteri(_iTextureTarget, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1884 // Use NEAREST as the default texture filtering mode.
1885 glTexParameteri(_iTextureTarget, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1886 glTexParameteri(_iTextureTarget, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1888 if (RT_COPY_TO_TEXTURE == _eUpdateMode)
1891 if (_bHasARBDepthTexture)
1893 // Allocate the texture image (but pass it no data for now).
1894 glTexImage2D(_iTextureTarget, 0, GL_DEPTH_COMPONENT,
1895 _iWidth, _iHeight, 0, GL_DEPTH_COMPONENT,
1900 // allocate memory for depth texture
1901 // Since this is slow, we warn the user in debug mode. (above)
1902 _pPoorDepthTexture = new unsigned short[_iWidth * _iHeight];
1903 glTexImage2D(_iTextureTarget, 0, GL_LUMINANCE16,
1904 _iWidth, _iHeight, 0, GL_LUMINANCE,
1905 GL_UNSIGNED_SHORT, _pPoorDepthTexture);
1915 //---------------------------------------------------------------------------
1916 // Function : RenderTexture::_MaybeCopyBuffer
1918 //---------------------------------------------------------------------------
1920 * @fn RenderTexture::_MaybeCopyBuffer()
1921 * @brief Does the actual copying for RenderTextures with RT_COPY_TO_TEXTURE
1923 void RenderTexture::_MaybeCopyBuffer()
1926 if (RT_COPY_TO_TEXTURE == _eUpdateMode)
1930 glBindTexture(_iTextureTarget, _iTextureID);
1931 glCopyTexSubImage2D(_iTextureTarget,
1932 0, 0, 0, 0, 0, _iWidth, _iHeight);
1934 if (_bIsDepthTexture)
1936 glBindTexture(_iTextureTarget, _iDepthTextureID);
1937 // HOW TO COPY DEPTH TEXTURE??? Supposedly this just magically works...
1939 if (_bHasARBDepthTexture)
1941 glCopyTexSubImage2D(_iTextureTarget, 0, 0, 0, 0, 0,
1946 // no 'real' depth texture available, so behavior has to be emulated
1947 // using glReadPixels (beware, this is (naturally) slow ...)
1948 glReadPixels(0, 0, _iWidth, _iHeight, GL_DEPTH_COMPONENT,
1949 GL_UNSIGNED_SHORT, _pPoorDepthTexture);
1950 glTexImage2D(_iTextureTarget, 0, GL_LUMINANCE16,
1951 _iWidth, _iHeight, 0, GL_LUMINANCE,
1952 GL_UNSIGNED_SHORT, _pPoorDepthTexture);
1961 glBindTexture(_iTextureTarget, _iTextureID);
1962 glCopyTexSubImage2D(_iTextureTarget, 0, 0, 0, 0, 0, _iWidth, _iHeight);
1964 if (_bIsDepthTexture)
1966 glBindTexture(_iTextureTarget, _iDepthTextureID);
1967 assert(_bHasARBDepthTexture);
1968 glCopyTexSubImage2D(_iTextureTarget, 0, 0, 0, 0, 0, _iWidth, _iHeight);
1974 //---------------------------------------------------------------------------
1975 // Function : RenderTexture::_ReleaseBoundBuffers
1977 //---------------------------------------------------------------------------
1979 * @fn RenderTexture::_ReleaseBoundBuffers()
1980 * @brief Releases buffer bindings on RenderTextures with RT_RENDER_TO_TEXTURE
1982 bool RenderTexture::_ReleaseBoundBuffers()
1985 if (_bIsTexture && RT_RENDER_TO_TEXTURE == _eUpdateMode)
1987 glBindTexture(_iTextureTarget, _iTextureID);
1989 // release the pbuffer from the render texture object
1990 if (0 != _iCurrentBoundBuffer && _bIsBufferBound)
1992 if (FALSE == wglReleaseTexImageARBPtr(_hPBuffer, _iCurrentBoundBuffer))
1997 _bIsBufferBound = false;
2001 if (_bIsDepthTexture && RT_RENDER_TO_TEXTURE == _eUpdateMode)
2003 glBindTexture(_iTextureTarget, _iDepthTextureID);
2005 // release the pbuffer from the render texture object
2006 if (FALSE == wglReleaseTexImageARBPtr(_hPBuffer, WGL_DEPTH_COMPONENT_NV))
2014 // textures can't be bound in Linux
2019 //---------------------------------------------------------------------------
2020 // Function : RenderTexture::_MakeCurrent
2022 //---------------------------------------------------------------------------
2024 * @fn RenderTexture::_MakeCurrent()
2025 * @brief Makes the RenderTexture's context current
2028 bool RenderTexture::_MakeCurrent()
2031 // make the pbuffer's rendering context current.
2032 if (FALSE == wglMakeCurrent( _hDC, _hGLContext))
2038 if (false == glXMakeCurrent(_pDisplay, _hPBuffer, _hGLContext))
2047 /////////////////////////////////////////////////////////////////////////////
2049 // Begin Deprecated Interface
2051 /////////////////////////////////////////////////////////////////////////////
2053 //---------------------------------------------------------------------------
2054 // Function : RenderTexture::RenderTexture
2056 //---------------------------------------------------------------------------
2058 * @fn RenderTexture::RenderTexture()
2059 * @brief Constructor.
2061 RenderTexture::RenderTexture(int width, int height,
2062 bool bIsTexture /* = true */,
2063 bool bIsDepthTexture /* = false */)
2066 _bIsTexture(bIsTexture),
2067 _bIsDepthTexture(bIsDepthTexture),
2068 _bHasARBDepthTexture(true), // [Redge]
2069 _eUpdateMode(RT_RENDER_TO_TEXTURE),
2070 _bInitialized(false),
2072 _iCurrentBoundBuffer(0),
2074 _iNumStencilBits(0),
2075 _bDoubleBuffered(false),
2080 _bShareObjects(false),
2081 _bCopyContext(false),
2087 _hPreviousContext(0),
2092 _hPreviousContext(0),
2093 _hPreviousDrawable(0),
2095 _iTextureTarget(GL_NONE),
2097 _iDepthTextureID(0),
2098 _pPoorDepthTexture(0) // [Redge]
2100 assert(width > 0 && height > 0);
2101 #if defined DEBUG || defined _DEBUG
2103 "RenderTexture Warning: Deprecated Contructor interface used.\n");
2106 _iNumColorBits[0] = _iNumColorBits[1] =
2107 _iNumColorBits[2] = _iNumColorBits[3] = 0;
2108 _bPowerOf2 = IsPowerOfTwo(width) && IsPowerOfTwo(height);
2111 //------------------------------------------------------------------------------
2112 // Function : RenderTexture::Initialize
2114 //------------------------------------------------------------------------------
2116 * @fn RenderTexture::Initialize(bool bShare, bool bDepth, bool bStencil, bool bMipmap, unsigned int iRBits, unsigned int iGBits, unsigned int iBBits, unsigned int iABits);
2117 * @brief Initializes the RenderTexture, sharing display lists and textures if specified.
2119 * This function actually does the creation of the p-buffer. It can only be called
2120 * once a GL context has already been created. Note that if the texture is not
2121 * power of two dimensioned, or has more than 8 bits per channel, enabling mipmapping
2122 * will cause an error.
2124 bool RenderTexture::Initialize(bool bShare /* = true */,
2125 bool bDepth /* = false */,
2126 bool bStencil /* = false */,
2127 bool bMipmap /* = false */,
2128 bool bAnisoFilter /* = false */,
2129 unsigned int iRBits /* = 8 */,
2130 unsigned int iGBits /* = 8 */,
2131 unsigned int iBBits /* = 8 */,
2132 unsigned int iABits /* = 8 */,
2133 UpdateMode updateMode /* = RT_RENDER_TO_TEXTURE */)
2135 if (0 == _iWidth || 0 == _iHeight)
2138 #if defined DEBUG || defined _DEBUG
2140 "RenderTexture Warning: Deprecated Initialize() interface used.\n");
2143 // create a mode string.
2146 mode.append("depth ");
2148 mode.append("stencil ");
2150 mode.append("mipmap ");
2151 if (iRBits + iGBits + iBBits + iABits > 0)
2162 char bitVector[100];
2164 "%d%s,%d%s,%d%s,%d%s",
2165 iRBits, (iRBits >= 16) ? "f" : "",
2166 iGBits, (iGBits >= 16) ? "f" : "",
2167 iBBits, (iBBits >= 16) ? "f" : "",
2168 iABits, (iABits >= 16) ? "f" : "");
2169 mode.append(bitVector);
2174 if (GL_NV_texture_rectangle &&
2175 ((!IsPowerOfTwo(_iWidth) || !IsPowerOfTwo(_iHeight))
2176 || iRBits >= 16 || iGBits > 16 || iBBits > 16 || iABits >= 16))
2177 mode.append("texRECT ");
2179 mode.append("tex2D ");
2181 if (_bIsDepthTexture)
2183 if (GL_NV_texture_rectangle &&
2184 ((!IsPowerOfTwo(_iWidth) || !IsPowerOfTwo(_iHeight))
2185 || iRBits >= 16 || iGBits > 16 || iBBits > 16 || iABits >= 16))
2186 mode.append("texRECT ");
2188 mode.append("tex2D ");
2190 if (RT_COPY_TO_TEXTURE == updateMode)
2193 _pixelFormatAttribs.clear();
2194 _pbufferAttribs.clear();
2197 _pixelFormatAttribs.push_back(WGL_DRAW_TO_PBUFFER_ARB);
2198 _pixelFormatAttribs.push_back(true);
2199 _pixelFormatAttribs.push_back(WGL_SUPPORT_OPENGL_ARB);
2200 _pixelFormatAttribs.push_back(true);
2202 _pbufferAttribs.push_back(WGL_PBUFFER_LARGEST_ARB);
2203 _pbufferAttribs.push_back(true);
2205 _pixelFormatAttribs.push_back(GLX_RENDER_TYPE_SGIX);
2206 _pixelFormatAttribs.push_back(GLX_RGBA_BIT_SGIX);
2207 _pixelFormatAttribs.push_back(GLX_DRAWABLE_TYPE_SGIX);
2208 _pixelFormatAttribs.push_back(GLX_PBUFFER_BIT_SGIX);
2211 _ParseModeString(mode.c_str(), _pixelFormatAttribs, _pbufferAttribs);
2214 _pixelFormatAttribs.push_back(0);
2215 _pbufferAttribs.push_back(0);
2217 _pixelFormatAttribs.push_back(None);
2220 Initialize(_iWidth, _iHeight, bShare);
2226 //---------------------------------------------------------------------------
2227 // Function : RenderTexture::Reset
2229 //---------------------------------------------------------------------------
2231 * @fn RenderTexture::Reset(int iWidth, int iHeight, unsigned int iMode, bool bIsTexture, bool bIsDepthTexture)
2232 * @brief Resets the resolution of the offscreen buffer.
2234 * Causes the buffer to delete itself. User must call Initialize() again
2237 bool RenderTexture::Reset(int iWidth, int iHeight)
2240 "RenderTexture Warning: Deprecated Reset() interface used.\n");
2244 fprintf(stderr, "RenderTexture::Reset(): failed to invalidate.\n");