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
46 * Nov. 2005, Use the simgear logging facility, Erik Hofman
50 # include <simgear_config.h>
57 #include <simgear/compiler.h>
58 #include <simgear/debug/logstream.hxx>
59 #include <simgear/screen/extensions.hxx>
60 #include <simgear/screen/RenderTexture.h>
68 #pragma comment(lib, "gdi32.lib") // required for GetPixelFormat()
74 static bool fctPtrInited = false;
75 /* WGL_ARB_pixel_format */
76 static wglChoosePixelFormatARBProc wglChoosePixelFormatARBPtr = 0;
77 static wglGetPixelFormatAttribivARBProc wglGetPixelFormatAttribivARBPtr = 0;
79 static wglCreatePbufferARBProc wglCreatePbufferARBPtr = 0;
80 static wglGetPbufferDCARBProc wglGetPbufferDCARBPtr = 0;
81 static wglQueryPbufferARBProc wglQueryPbufferARBPtr = 0;
82 static wglReleasePbufferDCARBProc wglReleasePbufferDCARBPtr = 0;
83 static wglDestroyPbufferARBProc wglDestroyPbufferARBPtr = 0;
84 /* WGL_ARB_render_texture */
85 static wglBindTexImageARBProc wglBindTexImageARBPtr = 0;
86 static wglReleaseTexImageARBProc wglReleaseTexImageARBPtr = 0;
88 #elif defined( __APPLE__ )
90 static bool glXVersion1_3Present = false;
91 static glXChooseFBConfigProc glXChooseFBConfigPtr = 0;
92 static glXCreatePbufferProc glXCreatePbufferPtr = 0;
93 static glXCreateGLXPbufferProc glXCreateGLXPbufferPtr = 0;
94 static glXGetVisualFromFBConfigProc glXGetVisualFromFBConfigPtr = 0;
95 static glXCreateContextProc glXCreateContextPtr = 0;
96 static glXCreateContextWithConfigProc glXCreateContextWithConfigPtr = 0;
97 static glXDestroyPbufferProc glXDestroyPbufferPtr = 0;
98 static glXQueryDrawableProc glXQueryDrawablePtr = 0;
99 static glXQueryGLXPbufferSGIXProc glXQueryGLXPbufferSGIXPtr = 0;
102 //---------------------------------------------------------------------------
103 // Function : RenderTexture::RenderTexture
105 //---------------------------------------------------------------------------
107 * @fn RenderTexture::RenderTexture()
108 * @brief Mode-string-based Constructor.
110 RenderTexture::RenderTexture(const char *strMode)
114 _bIsDepthTexture(false),
115 _bHasARBDepthTexture(true), // [Redge]
117 _eUpdateMode(RT_RENDER_TO_TEXTURE),
119 _eUpdateMode(RT_COPY_TO_TEXTURE),
121 _bInitialized(false),
123 _bIsBufferBound(false),
124 _iCurrentBoundBuffer(0),
128 _bDoubleBuffered(false),
132 _bShareObjects(false),
133 _bCopyContext(false),
139 _hPreviousContext(0),
140 #elif defined( __APPLE__ )
145 _hPreviousContext(0),
146 _hPreviousDrawable(0),
148 _iTextureTarget(GL_NONE),
151 _pPoorDepthTexture(0) // [Redge]
153 _iNumColorBits[0] = _iNumColorBits[1] =
154 _iNumColorBits[2] = _iNumColorBits[3] = 0;
157 _pixelFormatAttribs.push_back(WGL_DRAW_TO_PBUFFER_ARB);
158 _pixelFormatAttribs.push_back(true);
159 _pixelFormatAttribs.push_back(WGL_SUPPORT_OPENGL_ARB);
160 _pixelFormatAttribs.push_back(true);
162 _pbufferAttribs.push_back(WGL_PBUFFER_LARGEST_ARB);
163 _pbufferAttribs.push_back(true);
164 #elif defined( __APPLE__ )
166 _pbufferAttribs.push_back(GLX_RENDER_TYPE_SGIX);
167 _pbufferAttribs.push_back(GLX_RGBA_BIT_SGIX);
168 _pbufferAttribs.push_back(GLX_DRAWABLE_TYPE_SGIX);
169 _pbufferAttribs.push_back(GLX_PBUFFER_BIT_SGIX);
172 _ParseModeString(strMode, _pixelFormatAttribs, _pbufferAttribs);
175 _pixelFormatAttribs.push_back(0);
176 _pbufferAttribs.push_back(0);
178 _pixelFormatAttribs.push_back(None);
183 //---------------------------------------------------------------------------
184 // Function : RenderTexture::~RenderTexture
186 //---------------------------------------------------------------------------
188 * @fn RenderTexture::~RenderTexture()
191 RenderTexture::~RenderTexture()
197 //---------------------------------------------------------------------------
198 // Function : _wglGetLastError
200 //---------------------------------------------------------------------------
202 * @fn wglGetLastError()
203 * @brief Returns the last windows error generated.
206 void _wglGetLastError()
210 DWORD err = GetLastError();
213 case ERROR_INVALID_PIXEL_FORMAT:
214 SG_LOG(SG_GL, SG_ALERT,
215 "RenderTexture Win32 Error: ERROR_INVALID_PIXEL_FORMAT");
217 case ERROR_NO_SYSTEM_RESOURCES:
218 SG_LOG(SG_GL, SG_ALERT,
219 "RenderTexture Win32 Error: ERROR_NO_SYSTEM_RESOURCES");
221 case ERROR_INVALID_DATA:
222 SG_LOG(SG_GL, SG_ALERT,
223 "RenderTexture Win32 Error: ERROR_INVALID_DATA");
225 case ERROR_INVALID_WINDOW_HANDLE:
226 SG_LOG(SG_GL, SG_ALERT,
227 "RenderTexture Win32 Error: ERROR_INVALID_WINDOW_HANDLE");
229 case ERROR_RESOURCE_TYPE_NOT_FOUND:
230 SG_LOG(SG_GL, SG_ALERT,
231 "RenderTexture Win32 Error: ERROR_RESOURCE_TYPE_NOT_FOUND");
238 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
239 FORMAT_MESSAGE_FROM_SYSTEM |
240 FORMAT_MESSAGE_IGNORE_INSERTS,
243 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
248 SG_LOG(SG_GL, SG_ALERT, "RenderTexture Win32 Error " << err << ":" << lpMsgBuf);
249 LocalFree( lpMsgBuf );
258 //---------------------------------------------------------------------------
259 // Function : PrintExtensionError
261 //---------------------------------------------------------------------------
263 * @fn PrintExtensionError( char* strMsg, ... )
264 * @brief Prints an error about missing OpenGL extensions.
266 void PrintExtensionError( char* strMsg, ... )
268 SG_LOG(SG_GL, SG_ALERT,
269 "Error: RenderTexture requires the following unsupported "
270 "OpenGL extensions: ");
273 va_start(args, strMsg);
274 #if defined _WIN32 && !defined __CYGWIN__
275 _vsnprintf( strBuffer, 512, strMsg, args );
277 vsnprintf( strBuffer, 512, strMsg, args );
281 SG_LOG(SG_GL, SG_ALERT, strMsg);
285 //---------------------------------------------------------------------------
286 // Function : RenderTexture::Initialize
288 //---------------------------------------------------------------------------
290 * @fn RenderTexture::Initialize(int width, int height, bool shareObjects, bool copyContext);
291 * @brief Initializes the RenderTexture, sharing display lists and textures if specified.
293 * This function creates of the p-buffer. It can only be called once a GL
294 * context has already been created.
296 bool RenderTexture::Initialize(int width, int height,
297 bool shareObjects /* = true */,
298 bool copyContext /* = false */)
300 assert(width > 0 && height > 0);
302 _iWidth = width; _iHeight = height;
303 _bPowerOf2 = IsPowerOfTwo(width) && IsPowerOfTwo(height);
305 _bShareObjects = shareObjects;
306 _bCopyContext = copyContext;
308 // Check if this is an NVXX GPU and verify necessary extensions.
309 if (!_VerifyExtensions())
316 // Get the current context.
317 HDC hdc = wglGetCurrentDC();
320 HGLRC hglrc = wglGetCurrentContext();
325 unsigned int iNumFormats;
329 // Get the pixel format for the on-screen window.
330 iFormat = GetPixelFormat(hdc);
333 SG_LOG(SG_GL, SG_ALERT,
334 "RenderTexture Error: GetPixelFormat() failed.");
340 if (!wglChoosePixelFormatARBPtr(hdc, &_pixelFormatAttribs[0], NULL,
341 1, &iFormat, &iNumFormats))
343 SG_LOG(SG_GL, SG_ALERT,
344 "RenderTexture Error: wglChoosePixelFormatARB() failed.");
348 if ( iNumFormats <= 0 )
350 SG_LOG(SG_GL, SG_ALERT,
351 "RenderTexture Error: Couldn't find a suitable "
358 // Create the p-buffer.
359 _hPBuffer = wglCreatePbufferARBPtr(hdc, iFormat, _iWidth, _iHeight,
360 &_pbufferAttribs[0]);
363 SG_LOG(SG_GL, SG_ALERT,
364 "RenderTexture Error: wglCreatePbufferARB() failed.");
369 // Get the device context.
370 _hDC = wglGetPbufferDCARBPtr( _hPBuffer);
373 SG_LOG(SG_GL, SG_ALERT,
374 "RenderTexture Error: wglGetGetPbufferDCARB() failed.");
379 // Create a gl context for the p-buffer.
382 // Let's use the same gl context..
383 // Since the device contexts are compatible (i.e. same pixelformat),
384 // we should be able to use the same gl rendering context.
389 _hGLContext = wglCreateContext( _hDC );
392 SG_LOG(SG_GL, SG_ALERT,
393 "RenderTexture Error: wglCreateContext() failed.");
399 // Share lists, texture objects, and program objects.
402 if( !wglShareLists(hglrc, _hGLContext) )
404 SG_LOG(SG_GL, SG_ALERT,
405 "RenderTexture Error: wglShareLists() failed.");
411 // Determine the actual width and height we were able to create.
412 wglQueryPbufferARBPtr( _hPBuffer, WGL_PBUFFER_WIDTH_ARB, &_iWidth );
413 wglQueryPbufferARBPtr( _hPBuffer, WGL_PBUFFER_HEIGHT_ARB, &_iHeight );
415 _bInitialized = true;
417 // get the actual number of bits allocated:
418 int attrib = WGL_RED_BITS_ARB;
422 (wglGetPixelFormatAttribivARBPtr(_hDC, iFormat, 0, 1, &attrib, &value))
424 attrib = WGL_GREEN_BITS_ARB;
426 (wglGetPixelFormatAttribivARBPtr(_hDC, iFormat, 0, 1, &attrib, &value))
428 attrib = WGL_BLUE_BITS_ARB;
430 (wglGetPixelFormatAttribivARBPtr(_hDC, iFormat, 0, 1, &attrib, &value))
432 attrib = WGL_ALPHA_BITS_ARB;
434 (wglGetPixelFormatAttribivARBPtr(_hDC, iFormat, 0, 1, &attrib, &value))
436 attrib = WGL_DEPTH_BITS_ARB;
438 (wglGetPixelFormatAttribivARBPtr(_hDC, iFormat, 0, 1, &attrib, &value))
440 attrib = WGL_STENCIL_BITS_ARB;
442 (wglGetPixelFormatAttribivARBPtr(_hDC, iFormat, 0, 1, &attrib, &value))
444 attrib = WGL_DOUBLE_BUFFER_ARB;
446 (wglGetPixelFormatAttribivARBPtr(_hDC, iFormat, 0, 1, &attrib, &value))
447 ? (value?true:false) : false;
449 #if defined(_DEBUG) | defined(DEBUG)
450 SG_LOG(SG_GL, SG_ALERT, "Created a " << _iWidth << "x" << _iHeight <<
451 " RenderTexture with BPP(" <<
452 _iNumColorBits[0] << "," << _iNumColorBits[1] << "," <<
453 _iNumColorBits[2] << "," << _iNumColorBits[3] << ")");
454 if (_iNumDepthBits) SG_LOG(SG_GL, SG_ALERT, " depth=" << _iNumDepthBits);
455 if (_iNumStencilBits) SG_LOG(SG_GL, SG_ALERT, " stencil=" << _iNumStencilBits);
456 if (_bDoubleBuffered) SG_LOG(SG_GL, SG_ALERT, " double buffered");
459 #elif defined( __APPLE__ )
461 _pDisplay = glXGetCurrentDisplay();
462 GLXContext context = glXGetCurrentContext();
463 int screen = DefaultScreen(_pDisplay);
464 XVisualInfo *visInfo;
466 GLXFBConfig *fbConfigs;
469 fbConfigs = glXChooseFBConfigPtr(_pDisplay, screen,
470 &_pixelFormatAttribs[0], &nConfigs);
472 if (nConfigs == 0 || !fbConfigs)
474 SG_LOG(SG_GL, SG_ALERT,
475 "RenderTexture Error: Couldn't find a suitable pixel format.");
479 // Pick the first returned format that will return a pbuffer
480 if (glXVersion1_3Present)
483 GLX_PBUFFER_WIDTH, _iWidth,
484 GLX_PBUFFER_HEIGHT, _iHeight,
485 GLX_LARGEST_PBUFFER, False,
488 for (int i=0;i<nConfigs;i++)
490 _hPBuffer = glXCreatePbufferPtr(_pDisplay, fbConfigs[i], pbufAttrib);
493 XVisualInfo *visInfo = glXGetVisualFromFBConfigPtr(_pDisplay, fbConfigs[i]);
495 _hGLContext = glXCreateContextPtr(_pDisplay, visInfo,
496 _bShareObjects ? context : NULL,
512 for (int i=0;i<nConfigs;i++)
514 _hPBuffer = glXCreateGLXPbufferPtr(_pDisplay, fbConfigs[i],
515 _iWidth, _iHeight, NULL);
518 _hGLContext = glXCreateContextWithConfigPtr(_pDisplay,
521 _bShareObjects ? context : NULL,
531 SG_LOG(SG_GL, SG_ALERT,
532 "RenderTexture Error: glXCreateGLXPbufferPtr() failed.");
539 _hGLContext = glXCreateContext(_pDisplay, visInfo,
540 _bShareObjects ? context : NULL, False);
543 SG_LOG(SG_GL, SG_ALERT,
544 "RenderTexture Error: glXCreateContext() failed.");
549 if (!glXVersion1_3Present)
551 glXQueryGLXPbufferSGIXPtr(_pDisplay, _hPBuffer, GLX_WIDTH_SGIX,
553 glXQueryGLXPbufferSGIXPtr(_pDisplay, _hPBuffer, GLX_HEIGHT_SGIX,
557 _bInitialized = true;
559 // XXX Query the color format
564 // Now that the pbuffer is created, allocate any texture objects needed,
565 // and initialize them (for CTT updates only). These must be allocated
566 // in the context of the pbuffer, though, or the RT won't work without
569 if (false == wglMakeCurrent( _hDC, _hGLContext))
574 #elif defined( __APPLE__ )
576 _hPreviousContext = glXGetCurrentContext();
577 _hPreviousDrawable = glXGetCurrentDrawable();
579 if (False == glXMakeCurrent(_pDisplay, _hPBuffer, _hGLContext))
585 bool result = _InitializeTextures();
587 BindBuffer(WGL_FRONT_LEFT_ARB);
593 // make the previous rendering context current
594 if (false == wglMakeCurrent( hdc, hglrc))
599 #elif defined( __APPLE__ )
601 if (False == glXMakeCurrent(_pDisplay,
602 _hPreviousDrawable, _hPreviousContext))
606 if (glXVersion1_3Present)
608 GLXDrawable draw = glXGetCurrentDrawable();
609 glXQueryDrawablePtr(_pDisplay, draw, GLX_WIDTH, (unsigned int*)&_iWidth);
610 glXQueryDrawablePtr(_pDisplay, draw, GLX_HEIGHT, (unsigned int*)&_iHeight);
618 //---------------------------------------------------------------------------
619 // Function : RenderTexture::_Invalidate
621 //---------------------------------------------------------------------------
623 * @fn RenderTexture::_Invalidate()
624 * @brief Returns the pbuffer memory to the graphics device.
627 bool RenderTexture::_Invalidate()
629 _iNumColorBits[0] = _iNumColorBits[1] =
630 _iNumColorBits[2] = _iNumColorBits[3] = 0;
632 _iNumStencilBits = 0;
635 glDeleteTextures(1, &_iTextureID);
636 if (_bIsDepthTexture)
639 if (!_bHasARBDepthTexture) delete[] _pPoorDepthTexture;
641 glDeleteTextures(1, &_iDepthTextureID);
647 // Check if we are currently rendering in the pbuffer
648 if (wglGetCurrentContext() == _hGLContext)
651 wglDeleteContext( _hGLContext);
652 wglReleasePbufferDCARBPtr( _hPBuffer, _hDC);
653 wglDestroyPbufferARBPtr( _hPBuffer );
657 #elif defined( __APPLE__ )
661 if(glXGetCurrentContext() == _hGLContext)
662 // XXX I don't know if this is right at all
663 glXMakeCurrent(_pDisplay, _hPBuffer, 0);
664 glXDestroyPbufferPtr(_pDisplay, _hPBuffer);
670 // [WVB] do we need to call _ReleaseBoundBuffers() too?
675 //---------------------------------------------------------------------------
676 // Function : RenderTexture::Reset
678 //---------------------------------------------------------------------------
680 * @fn RenderTexture::Reset()
681 * @brief Resets the resolution of the offscreen buffer.
683 * Causes the buffer to delete itself. User must call Initialize() again
686 bool RenderTexture::Reset(const char *strMode, ...)
688 _iWidth = 0; _iHeight = 0;
689 _bIsTexture = false; _bIsDepthTexture = false,
690 _bHasARBDepthTexture = true;
692 _eUpdateMode = RT_RENDER_TO_TEXTURE;
694 _eUpdateMode = RT_COPY_TO_TEXTURE;
696 _bInitialized = false;
698 _bIsBufferBound = false;
699 _iCurrentBoundBuffer = 0;
700 _iNumDepthBits = 0; _iNumStencilBits = 0;
701 _bDoubleBuffered = false;
702 _bFloat = false; _bPowerOf2 = true;
703 _bRectangle = false; _bMipmap = false;
704 _bShareObjects = false; _bCopyContext = false;
705 _iTextureTarget = GL_NONE; _iTextureID = 0;
706 _iDepthTextureID = 0;
707 _pPoorDepthTexture = 0;
708 _pixelFormatAttribs.clear();
709 _pbufferAttribs.clear();
711 if (IsInitialized() && !_Invalidate())
713 SG_LOG(SG_GL, SG_ALERT, "RenderTexture::Reset(): failed to invalidate.");
717 _iNumColorBits[0] = _iNumColorBits[1] =
718 _iNumColorBits[2] = _iNumColorBits[3] = 0;
721 _pixelFormatAttribs.push_back(WGL_DRAW_TO_PBUFFER_ARB);
722 _pixelFormatAttribs.push_back(true);
723 _pixelFormatAttribs.push_back(WGL_SUPPORT_OPENGL_ARB);
724 _pixelFormatAttribs.push_back(true);
726 _pbufferAttribs.push_back(WGL_PBUFFER_LARGEST_ARB);
727 _pbufferAttribs.push_back(true);
728 #elif defined( __APPLE__ )
730 _pbufferAttribs.push_back(GLX_RENDER_TYPE_SGIX);
731 _pbufferAttribs.push_back(GLX_RGBA_BIT_SGIX);
732 _pbufferAttribs.push_back(GLX_DRAWABLE_TYPE_SGIX);
733 _pbufferAttribs.push_back(GLX_PBUFFER_BIT_SGIX);
738 va_start(args,strMode);
739 #if defined _WIN32 && !defined __CYGWIN__
740 _vsnprintf( strBuffer, 256, strMode, args );
742 vsnprintf( strBuffer, 256, strMode, args );
746 _ParseModeString(strBuffer, _pixelFormatAttribs, _pbufferAttribs);
749 _pixelFormatAttribs.push_back(0);
750 _pbufferAttribs.push_back(0);
752 _pixelFormatAttribs.push_back(None);
757 //------------------------------------------------------------------------------
758 // Function : RenderTexture::Resize
760 //------------------------------------------------------------------------------
762 * @fn RenderTexture::Resize(int iWidth, int iHeight)
763 * @brief Changes the size of the offscreen buffer.
765 * Like Reset() this causes the buffer to delete itself.
766 * But unlike Reset(), this call re-initializes the RenderTexture.
767 * Note that Resize() will not work after calling Reset(), or before
768 * calling Initialize() the first time.
770 bool RenderTexture::Resize(int iWidth, int iHeight)
772 if (!_bInitialized) {
773 SG_LOG(SG_GL, SG_ALERT, "RenderTexture::Resize(): must Initialize() first.");
776 if (iWidth == _iWidth && iHeight == _iHeight) {
780 // Do same basic work as _Invalidate, but don't reset all our flags
782 glDeleteTextures(1, &_iTextureID);
783 if (_bIsDepthTexture)
784 glDeleteTextures(1, &_iDepthTextureID);
788 // Check if we are currently rendering in the pbuffer
789 if (wglGetCurrentContext() == _hGLContext)
792 wglDeleteContext( _hGLContext);
793 wglReleasePbufferDCARBPtr( _hPBuffer, _hDC);
794 wglDestroyPbufferARBPtr( _hPBuffer );
798 #elif defined( __APPLE__ )
802 if(glXGetCurrentContext() == _hGLContext)
803 // XXX I don't know if this is right at all
804 glXMakeCurrent(_pDisplay, _hPBuffer, 0);
805 glXDestroyPbufferPtr(_pDisplay, _hPBuffer);
810 SG_LOG(SG_GL, SG_ALERT, "RenderTexture::Resize(): failed to resize.");
813 _bInitialized = false;
814 return Initialize(iWidth, iHeight, _bShareObjects, _bCopyContext);
817 //---------------------------------------------------------------------------
818 // Function : RenderTexture::BeginCapture
820 //---------------------------------------------------------------------------
822 * @fn RenderTexture::BeginCapture()
823 * @brief Activates rendering to the RenderTexture.
825 bool RenderTexture::BeginCapture()
829 SG_LOG(SG_GL, SG_ALERT,
830 "RenderTexture::BeginCapture(): Texture is not initialized!");
834 // cache the current context so we can reset it when EndCapture() is called.
835 _hPreviousDC = wglGetCurrentDC();
836 if (NULL == _hPreviousDC)
838 _hPreviousContext = wglGetCurrentContext();
839 if (NULL == _hPreviousContext)
841 #elif defined( __APPLE__ )
843 _hPreviousContext = glXGetCurrentContext();
844 _hPreviousDrawable = glXGetCurrentDrawable();
847 _ReleaseBoundBuffers();
849 return _MakeCurrent();
853 //---------------------------------------------------------------------------
854 // Function : RenderTexture::EndCapture
856 //---------------------------------------------------------------------------
858 * @fn RenderTexture::EndCapture()
859 * @brief Ends rendering to the RenderTexture.
861 bool RenderTexture::EndCapture()
865 SG_LOG(SG_GL, SG_ALERT,
866 "RenderTexture::EndCapture() : Texture is not initialized!");
873 // make the previous rendering context current
874 if (FALSE == wglMakeCurrent( _hPreviousDC, _hPreviousContext))
879 #elif defined( __APPLE__ )
881 if (False == glXMakeCurrent(_pDisplay, _hPreviousDrawable,
888 // rebind the textures to a buffers for RTT
889 BindBuffer(_iCurrentBoundBuffer);
895 //---------------------------------------------------------------------------
896 // Function : RenderTexture::BeginCapture(RenderTexture*)
898 //---------------------------------------------------------------------------
900 * @fn RenderTexture::BeginCapture(RenderTexture* other)
901 * @brief Ends capture of 'other', begins capture on 'this'
903 * When performing a series of operations where you modify one texture after
904 * another, it is more efficient to use this method instead of the equivalent
905 * 'EndCapture'/'BeginCapture' pair. This method switches directly to the
906 * new context rather than changing to the default context, and then to the
909 * RenderTexture doesn't have any mechanism for determining if
910 * 'current' really is currently active, so no error will be thrown
913 bool RenderTexture::BeginCapture(RenderTexture* current)
915 bool bContextReset = false;
917 if (current == this) {
918 return true; // no switch necessary
921 // treat as normal Begin if current is 0.
922 return BeginCapture();
926 SG_LOG(SG_GL, SG_ALERT,
927 "RenderTexture::BeginCapture(RenderTexture*): Texture is not initialized!");
930 if (!current->_bInitialized)
932 SG_LOG(SG_GL, SG_ALERT,
933 "RenderTexture::BeginCapture(RenderTexture): 'current' texture is not initialized!");
937 // Sync current pbuffer with its CTT texture if necessary
938 current->_MaybeCopyBuffer();
940 // pass along the previous context so we can reset it when
941 // EndCapture() is called.
943 _hPreviousDC = current->_hPreviousDC;
944 if (NULL == _hPreviousDC)
946 _hPreviousContext = current->_hPreviousContext;
947 if (NULL == _hPreviousContext)
949 #elif defined( __APPLE__ )
951 _hPreviousContext = current->_hPreviousContext;
952 _hPreviousDrawable = current->_hPreviousDrawable;
955 // Unbind textures before making context current
956 if (!_ReleaseBoundBuffers())
959 // Make the pbuffer context current
963 // Rebind buffers of initial RenderTexture
964 current->BindBuffer(_iCurrentBoundBuffer);
965 current->_BindDepthBuffer();
972 //---------------------------------------------------------------------------
973 // Function : RenderTexture::Bind
975 //---------------------------------------------------------------------------
977 * @fn RenderTexture::Bind()
978 * @brief Binds RGB texture.
980 void RenderTexture::Bind() const
982 if (_bInitialized && _bIsTexture)
984 glBindTexture(_iTextureTarget, _iTextureID);
989 //---------------------------------------------------------------------------
990 // Function : RenderTexture::BindDepth
992 //---------------------------------------------------------------------------
994 * @fn RenderTexture::BindDepth()
995 * @brief Binds depth texture.
997 void RenderTexture::BindDepth() const
999 if (_bInitialized && _bIsDepthTexture)
1001 glBindTexture(_iTextureTarget, _iDepthTextureID);
1006 //---------------------------------------------------------------------------
1007 // Function : RenderTexture::BindBuffer
1009 //---------------------------------------------------------------------------
1011 * @fn RenderTexture::BindBuffer()
1012 * @brief Associate the RTT texture id with 'iBuffer' (e.g. WGL_FRONT_LEFT_ARB)
1014 bool RenderTexture::BindBuffer( int iBuffer )
1016 // Must bind the texture too
1017 if (_bInitialized && _bIsTexture)
1019 glBindTexture(_iTextureTarget, _iTextureID);
1022 if (RT_RENDER_TO_TEXTURE == _eUpdateMode && _bIsTexture &&
1023 (!_bIsBufferBound || _iCurrentBoundBuffer != iBuffer))
1025 if (FALSE == wglBindTexImageARBPtr(_hPBuffer, iBuffer))
1027 // WVB: WGL API considers binding twice to the same buffer
1028 // to be an error. But we don't want to
1029 //_wglGetLastError();
1033 _bIsBufferBound = true;
1034 _iCurrentBoundBuffer = iBuffer;
1042 //---------------------------------------------------------------------------
1043 // Function : RenderTexture::BindBuffer
1045 //---------------------------------------------------------------------------
1047 * @fn RenderTexture::_BindDepthBuffer()
1048 * @brief Associate the RTT depth texture id with the depth buffer
1050 bool RenderTexture::_BindDepthBuffer() const
1053 if (_bInitialized && _bIsDepthTexture &&
1054 RT_RENDER_TO_TEXTURE == _eUpdateMode)
1056 glBindTexture(_iTextureTarget, _iDepthTextureID);
1057 if (FALSE == wglBindTexImageARBPtr(_hPBuffer, WGL_DEPTH_COMPONENT_NV))
1067 //---------------------------------------------------------------------------
1068 // Function : RenderTexture::_ParseModeString
1070 //---------------------------------------------------------------------------
1072 * @fn RenderTexture::_ParseModeString()
1073 * @brief Parses the user-specified mode string for RenderTexture parameters.
1075 void RenderTexture::_ParseModeString(const char *modeString,
1076 vector<int> &pfAttribs,
1077 vector<int> &pbAttribs)
1079 if (!modeString || strcmp(modeString, "") == 0)
1082 _iNumComponents = 0;
1084 _eUpdateMode = RT_RENDER_TO_TEXTURE;
1086 _eUpdateMode = RT_COPY_TO_TEXTURE;
1090 bool bHasStencil = false;
1091 bool bBind2D = false;
1092 bool bBindRECT = false;
1093 bool bBindCUBE = false;
1095 char *mode = strdup(modeString);
1098 vector<string> tokens;
1099 char *buf = strtok(mode, " ");
1102 tokens.push_back(buf);
1103 buf = strtok(NULL, " ");
1106 for (unsigned int i = 0; i < tokens.size(); i++)
1108 string token = tokens[i];
1110 KeyVal kv = _GetKeyValuePair(token);
1113 if (kv.first == "rgb" && (_iNumComponents <= 1))
1115 if (kv.second.find("f") != kv.second.npos)
1118 vector<int> bitVec = _ParseBitVector(kv.second);
1120 if (bitVec.size() < 3) // expand the scalar to a vector
1122 bitVec.push_back(bitVec[0]);
1123 bitVec.push_back(bitVec[0]);
1127 pfAttribs.push_back(WGL_RED_BITS_ARB);
1128 pfAttribs.push_back(bitVec[0]);
1129 pfAttribs.push_back(WGL_GREEN_BITS_ARB);
1130 pfAttribs.push_back(bitVec[1]);
1131 pfAttribs.push_back(WGL_BLUE_BITS_ARB);
1132 pfAttribs.push_back(bitVec[2]);
1133 #elif defined( __APPLE__ )
1134 pfAttribs.push_back(AGL_RED_SIZE);
1135 pfAttribs.push_back(bitVec[0]);
1136 pfAttribs.push_back(AGL_GREEN_SIZE);
1137 pfAttribs.push_back(bitVec[1]);
1138 pfAttribs.push_back(AGL_BLUE_SIZE);
1139 pfAttribs.push_back(bitVec[2]);
1141 pfAttribs.push_back(GLX_RED_SIZE);
1142 pfAttribs.push_back(bitVec[0]);
1143 pfAttribs.push_back(GLX_GREEN_SIZE);
1144 pfAttribs.push_back(bitVec[1]);
1145 pfAttribs.push_back(GLX_BLUE_SIZE);
1146 pfAttribs.push_back(bitVec[2]);
1148 _iNumComponents += 3;
1151 else if (kv.first == "rgb")
1152 SG_LOG(SG_GL, SG_ALERT,
1153 "RenderTexture Warning: mistake in components definition "
1154 "(rgb + " << _iNumComponents << ").");
1156 if (kv.first == "rgba" && (_iNumComponents == 0))
1158 if (kv.second.find("f") != kv.second.npos)
1161 vector<int> bitVec = _ParseBitVector(kv.second);
1163 if (bitVec.size() < 4) // expand the scalar to a vector
1165 bitVec.push_back(bitVec[0]);
1166 bitVec.push_back(bitVec[0]);
1167 bitVec.push_back(bitVec[0]);
1171 pfAttribs.push_back(WGL_RED_BITS_ARB);
1172 pfAttribs.push_back(bitVec[0]);
1173 pfAttribs.push_back(WGL_GREEN_BITS_ARB);
1174 pfAttribs.push_back(bitVec[1]);
1175 pfAttribs.push_back(WGL_BLUE_BITS_ARB);
1176 pfAttribs.push_back(bitVec[2]);
1177 pfAttribs.push_back(WGL_ALPHA_BITS_ARB);
1178 pfAttribs.push_back(bitVec[3]);
1179 #elif defined( __APPLE__ )
1180 pfAttribs.push_back(AGL_RED_SIZE);
1181 pfAttribs.push_back(bitVec[0]);
1182 pfAttribs.push_back(AGL_GREEN_SIZE);
1183 pfAttribs.push_back(bitVec[1]);
1184 pfAttribs.push_back(AGL_BLUE_SIZE);
1185 pfAttribs.push_back(bitVec[2]);
1186 pfAttribs.push_back(AGL_ALPHA_SIZE);
1187 pfAttribs.push_back(bitVec[3]);
1189 pfAttribs.push_back(GLX_RED_SIZE);
1190 pfAttribs.push_back(bitVec[0]);
1191 pfAttribs.push_back(GLX_GREEN_SIZE);
1192 pfAttribs.push_back(bitVec[1]);
1193 pfAttribs.push_back(GLX_BLUE_SIZE);
1194 pfAttribs.push_back(bitVec[2]);
1195 pfAttribs.push_back(GLX_ALPHA_SIZE);
1196 pfAttribs.push_back(bitVec[3]);
1198 _iNumComponents = 4;
1201 else if (kv.first == "rgba")
1202 SG_LOG(SG_GL, SG_ALERT,
1203 "RenderTexture Warning: mistake in components definition "
1204 "(rgba + " << _iNumComponents << ").");
1206 if (kv.first == "r" && (_iNumComponents <= 1))
1208 if (kv.second.find("f") != kv.second.npos)
1211 vector<int> bitVec = _ParseBitVector(kv.second);
1214 pfAttribs.push_back(WGL_RED_BITS_ARB);
1215 pfAttribs.push_back(bitVec[0]);
1216 #elif defined( __APPLE__ )
1217 pfAttribs.push_back(AGL_RED_SIZE);
1218 pfAttribs.push_back(bitVec[0]);
1220 pfAttribs.push_back(GLX_RED_SIZE);
1221 pfAttribs.push_back(bitVec[0]);
1226 else if (kv.first == "r")
1227 SG_LOG(SG_GL, SG_ALERT,
1228 "RenderTexture Warning: mistake in components definition "
1229 "(r + " << _iNumComponents << ").");
1231 if (kv.first == "rg" && (_iNumComponents <= 1))
1233 if (kv.second.find("f") != kv.second.npos)
1236 vector<int> bitVec = _ParseBitVector(kv.second);
1238 if (bitVec.size() < 2) // expand the scalar to a vector
1240 bitVec.push_back(bitVec[0]);
1244 pfAttribs.push_back(WGL_RED_BITS_ARB);
1245 pfAttribs.push_back(bitVec[0]);
1246 pfAttribs.push_back(WGL_GREEN_BITS_ARB);
1247 pfAttribs.push_back(bitVec[1]);
1248 #elif defined( __APPLE__ )
1249 pfAttribs.push_back(AGL_RED_SIZE);
1250 pfAttribs.push_back(bitVec[0]);
1251 pfAttribs.push_back(AGL_GREEN_SIZE);
1252 pfAttribs.push_back(bitVec[1]);
1254 pfAttribs.push_back(GLX_RED_SIZE);
1255 pfAttribs.push_back(bitVec[0]);
1256 pfAttribs.push_back(GLX_GREEN_SIZE);
1257 pfAttribs.push_back(bitVec[1]);
1259 _iNumComponents += 2;
1262 else if (kv.first == "rg")
1263 SG_LOG(SG_GL, SG_ALERT,
1264 "RenderTexture Warning: mistake in components definition "
1265 "(rg + " << _iNumComponents << ").");
1267 if (kv.first == "depth")
1269 if (kv.second == "")
1272 iDepthBits = strtol(kv.second.c_str(), 0, 10);
1276 if (kv.first == "stencil")
1280 pfAttribs.push_back(WGL_STENCIL_BITS_ARB);
1281 #elif defined( __APPLE__ )
1282 pfAttribs.push_back(AGL_STENCIL_SIZE);
1284 pfAttribs.push_back(GLX_STENCIL_SIZE);
1286 if (kv.second == "")
1287 pfAttribs.push_back(8);
1289 pfAttribs.push_back(strtol(kv.second.c_str(), 0, 10));
1293 if (kv.first == "samples")
1296 pfAttribs.push_back(WGL_SAMPLE_BUFFERS_ARB);
1297 pfAttribs.push_back(1);
1298 pfAttribs.push_back(WGL_SAMPLES_ARB);
1299 pfAttribs.push_back(strtol(kv.second.c_str(), 0, 10));
1300 #elif defined( __APPLE__ )
1301 pfAttribs.push_back(AGL_SAMPLE_BUFFERS_ARB);
1302 pfAttribs.push_back(1);
1303 pfAttribs.push_back(AGL_SAMPLES_ARB);
1304 pfAttribs.push_back(strtol(kv.second.c_str(), 0, 10));
1306 pfAttribs.push_back(GLX_SAMPLE_BUFFERS_ARB);
1307 pfAttribs.push_back(1);
1308 pfAttribs.push_back(GLX_SAMPLES_ARB);
1309 pfAttribs.push_back(strtol(kv.second.c_str(), 0, 10));
1315 if (kv.first == "doublebuffer" || kv.first == "double")
1318 pfAttribs.push_back(WGL_DOUBLE_BUFFER_ARB);
1319 pfAttribs.push_back(true);
1320 #elif defined( __APPLE__ )
1321 pfAttribs.push_back(AGL_DOUBLEBUFFER);
1322 pfAttribs.push_back(True);
1324 pfAttribs.push_back(GLX_DOUBLEBUFFER);
1325 pfAttribs.push_back(True);
1330 if (kv.first == "aux")
1333 pfAttribs.push_back(WGL_AUX_BUFFERS_ARB);
1334 #elif defined( __APPLE__ )
1335 pfAttribs.push_back(AGL_AUX_BUFFERS);
1337 pfAttribs.push_back(GLX_AUX_BUFFERS);
1339 if (kv.second == "")
1340 pfAttribs.push_back(0);
1342 pfAttribs.push_back(strtol(kv.second.c_str(), 0, 10));
1346 if (token.find("tex") == 0)
1350 if ((kv.first == "texRECT") && GL_NV_texture_rectangle)
1355 else if (kv.first == "texCUBE")
1367 if (token.find("depthTex") == 0)
1369 _bIsDepthTexture = true;
1371 if ((kv.first == "depthTexRECT") && GL_NV_texture_rectangle)
1376 else if (kv.first == "depthTexCUBE")
1388 if (kv.first == "mipmap")
1394 if (kv.first == "rtt")
1396 _eUpdateMode = RT_RENDER_TO_TEXTURE;
1400 if (kv.first == "ctt")
1402 _eUpdateMode = RT_COPY_TO_TEXTURE;
1406 SG_LOG(SG_GL, SG_ALERT,
1407 "RenderTexture Error: Unknown pbuffer attribute: " <<
1411 // Processing of some options must be last because of interactions.
1413 // Check for inconsistent texture targets
1414 if (_bIsTexture && _bIsDepthTexture && !(bBind2D ^ bBindRECT ^ bBindCUBE))
1416 SG_LOG(SG_GL, SG_ALERT,
1417 "RenderTexture Warning: Depth and Color texture targets "
1421 // Apply default bit format if none specified
1423 if (0 == _iNumComponents)
1425 pfAttribs.push_back(WGL_RED_BITS_ARB);
1426 pfAttribs.push_back(8);
1427 pfAttribs.push_back(WGL_GREEN_BITS_ARB);
1428 pfAttribs.push_back(8);
1429 pfAttribs.push_back(WGL_BLUE_BITS_ARB);
1430 pfAttribs.push_back(8);
1431 pfAttribs.push_back(WGL_ALPHA_BITS_ARB);
1432 pfAttribs.push_back(8);
1433 _iNumComponents = 4;
1438 if (_bIsDepthTexture && !iDepthBits)
1442 pfAttribs.push_back(WGL_DEPTH_BITS_ARB);
1443 #elif defined( __APPLE__ )
1444 pfAttribs.push_back(AGL_DEPTH_SIZE);
1446 pfAttribs.push_back(GLX_DEPTH_SIZE);
1448 pfAttribs.push_back(iDepthBits); // default
1453 pfAttribs.push_back(WGL_STENCIL_BITS_ARB);
1454 pfAttribs.push_back(0);
1455 #elif defined( __APPLE__ )
1456 pfAttribs.push_back(AGL_STENCIL_SIZE);
1457 pfAttribs.push_back(0);
1459 pfAttribs.push_back(GLX_STENCIL_SIZE);
1460 pfAttribs.push_back(0);
1464 if (_iNumComponents < 4)
1466 // Can't do this right now -- on NVIDIA drivers, currently get
1467 // a non-functioning pbuffer if ALPHA_BITS=0 and
1468 // WGL_BIND_TO_TEXTURE_RGB_ARB=true
1470 //pfAttribs.push_back(WGL_ALPHA_BITS_ARB);
1471 //pfAttribs.push_back(0);
1475 if (!WGL_NV_render_depth_texture && _bIsDepthTexture && (RT_RENDER_TO_TEXTURE == _eUpdateMode))
1477 #if defined(DEBUG) || defined(_DEBUG)
1478 SG_LOG(SG_GL, SG_ALERT, "RenderTexture Warning: No support found for "
1479 "render to depth texture.");
1481 _bIsDepthTexture = false;
1485 if ((_bIsTexture || _bIsDepthTexture) &&
1486 (RT_RENDER_TO_TEXTURE == _eUpdateMode))
1491 pbAttribs.push_back(WGL_TEXTURE_TARGET_ARB);
1492 pbAttribs.push_back(WGL_TEXTURE_RECTANGLE_NV);
1496 pbAttribs.push_back(WGL_TEXTURE_TARGET_ARB);
1497 pbAttribs.push_back(WGL_TEXTURE_CUBE_MAP_ARB);
1501 pbAttribs.push_back(WGL_TEXTURE_TARGET_ARB);
1502 pbAttribs.push_back(WGL_TEXTURE_2D_ARB);
1507 pbAttribs.push_back(WGL_MIPMAP_TEXTURE_ARB);
1508 pbAttribs.push_back(true);
1511 #elif defined(DEBUG) || defined(_DEBUG)
1512 SG_LOG(SG_GL, SG_INFO, "RenderTexture Error: Render to Texture not "
1513 "supported in Linux or MacOS");
1517 // Set the pixel type
1521 if (WGL_NV_float_buffer)
1523 pfAttribs.push_back(WGL_PIXEL_TYPE_ARB);
1524 pfAttribs.push_back(WGL_TYPE_RGBA_ARB);
1526 pfAttribs.push_back(WGL_FLOAT_COMPONENTS_NV);
1527 pfAttribs.push_back(true);
1531 pfAttribs.push_back(WGL_PIXEL_TYPE_ARB);
1532 pfAttribs.push_back(WGL_TYPE_RGBA_FLOAT_ATI);
1534 #elif defined( __APPLE__ )
1536 if (GLX_NV_float_buffer)
1538 pfAttribs.push_back(GLX_FLOAT_COMPONENTS_NV);
1539 pfAttribs.push_back(1);
1546 pfAttribs.push_back(WGL_PIXEL_TYPE_ARB);
1547 pfAttribs.push_back(WGL_TYPE_RGBA_ARB);
1551 // Set up texture binding for render to texture
1552 if (_bIsTexture && (RT_RENDER_TO_TEXTURE == _eUpdateMode))
1558 if (WGL_NV_float_buffer)
1560 switch(_iNumComponents)
1563 pfAttribs.push_back(WGL_BIND_TO_TEXTURE_RECTANGLE_FLOAT_R_NV);
1564 pfAttribs.push_back(true);
1566 pbAttribs.push_back(WGL_TEXTURE_FORMAT_ARB);
1567 pbAttribs.push_back(WGL_TEXTURE_FLOAT_R_NV);
1570 pfAttribs.push_back(WGL_BIND_TO_TEXTURE_RECTANGLE_FLOAT_RG_NV);
1571 pfAttribs.push_back(true);
1573 pbAttribs.push_back(WGL_TEXTURE_FORMAT_ARB);
1574 pbAttribs.push_back(WGL_TEXTURE_FLOAT_RG_NV);
1577 pfAttribs.push_back(WGL_BIND_TO_TEXTURE_RECTANGLE_FLOAT_RGB_NV);
1578 pfAttribs.push_back(true);
1580 pbAttribs.push_back(WGL_TEXTURE_FORMAT_ARB);
1581 pbAttribs.push_back(WGL_TEXTURE_FLOAT_RGB_NV);
1584 pfAttribs.push_back(WGL_BIND_TO_TEXTURE_RECTANGLE_FLOAT_RGBA_NV);
1585 pfAttribs.push_back(true);
1587 pbAttribs.push_back(WGL_TEXTURE_FORMAT_ARB);
1588 pbAttribs.push_back(WGL_TEXTURE_FLOAT_RGBA_NV);
1591 SG_LOG(SG_GL, SG_ALERT,
1592 "RenderTexture Warning: Bad number of components "
1593 "(r=1,rg=2,rgb=3,rgba=4): " <<
1600 if (4 == _iNumComponents)
1602 pfAttribs.push_back(WGL_BIND_TO_TEXTURE_RGBA_ARB);
1603 pfAttribs.push_back(true);
1605 pbAttribs.push_back(WGL_TEXTURE_FORMAT_ARB);
1606 pbAttribs.push_back(WGL_TEXTURE_RGBA_ARB);
1610 // standard ARB_render_texture only supports 3 or 4 channels
1611 pfAttribs.push_back(WGL_BIND_TO_TEXTURE_RGB_ARB);
1612 pfAttribs.push_back(true);
1614 pbAttribs.push_back(WGL_TEXTURE_FORMAT_ARB);
1615 pbAttribs.push_back(WGL_TEXTURE_RGB_ARB);
1622 switch(_iNumComponents)
1625 pfAttribs.push_back(WGL_BIND_TO_TEXTURE_RGB_ARB);
1626 pfAttribs.push_back(true);
1628 pbAttribs.push_back(WGL_TEXTURE_FORMAT_ARB);
1629 pbAttribs.push_back(WGL_TEXTURE_RGB_ARB);
1632 pfAttribs.push_back(WGL_BIND_TO_TEXTURE_RGBA_ARB);
1633 pfAttribs.push_back(true);
1635 pbAttribs.push_back(WGL_TEXTURE_FORMAT_ARB);
1636 pbAttribs.push_back(WGL_TEXTURE_RGBA_ARB);
1639 SG_LOG(SG_GL, SG_ALERT,
1640 "RenderTexture Warning: Bad number of components "
1641 "(r=1,rg=2,rgb=3,rgba=4): " << _iNumComponents);
1645 #elif defined(DEBUG) || defined(_DEBUG)
1646 SG_LOG(SG_GL, SG_ALERT,
1647 "RenderTexture Error: Render to Texture not supported in "
1648 "Linux or MacOS\ n");
1652 if (_bIsDepthTexture && (RT_RENDER_TO_TEXTURE == _eUpdateMode))
1657 pfAttribs.push_back(WGL_BIND_TO_TEXTURE_RECTANGLE_DEPTH_NV);
1658 pfAttribs.push_back(true);
1660 pbAttribs.push_back(WGL_DEPTH_TEXTURE_FORMAT_NV);
1661 pbAttribs.push_back(WGL_TEXTURE_DEPTH_COMPONENT_NV);
1665 pfAttribs.push_back(WGL_BIND_TO_TEXTURE_DEPTH_NV);
1666 pfAttribs.push_back(true);
1668 pbAttribs.push_back(WGL_DEPTH_TEXTURE_FORMAT_NV);
1669 pbAttribs.push_back(WGL_TEXTURE_DEPTH_COMPONENT_NV);
1671 #elif defined(DEBUG) || defined(_DEBUG)
1672 SG_LOG(SG_GL, SG_INFO, "RenderTexture Error: Render to Texture not supported in "
1678 //---------------------------------------------------------------------------
1679 // Function : RenderTexture::_GetKeyValuePair
1681 //---------------------------------------------------------------------------
1683 * @fn RenderTexture::_GetKeyValuePair()
1684 * @brief Parses expressions of the form "X=Y" into a pair (X,Y).
1686 RenderTexture::KeyVal RenderTexture::_GetKeyValuePair(string token)
1688 string::size_type pos = 0;
1689 if ((pos = token.find("=")) != token.npos)
1691 string key = token.substr(0, pos);
1692 string value = token.substr(pos+1, token.length()-pos+1);
1693 return KeyVal(key, value);
1696 return KeyVal(token, "");
1699 //---------------------------------------------------------------------------
1700 // Function : RenderTexture::_ParseBitVector
1702 //---------------------------------------------------------------------------
1704 * @fn RenderTexture::_ParseBitVector()
1705 * @brief Parses expressions of the form "=r,g,b,a" into a vector: (r,g,b,a)
1707 vector<int> RenderTexture::_ParseBitVector(string bitVector)
1709 vector<string> pieces;
1712 if (bitVector == "")
1714 bits.push_back(8); // if a depth isn't specified, use default 8 bits
1718 string::size_type pos = 0;
1719 string::size_type nextpos = 0;
1722 nextpos = bitVector.find_first_of(", ", pos);
1723 pieces.push_back(string(bitVector, pos, nextpos - pos));
1725 } while (nextpos != bitVector.npos );
1727 for ( vector<string>::iterator it = pieces.begin(); it != pieces.end(); it++)
1729 bits.push_back(strtol(it->c_str(), 0, 10));
1735 //---------------------------------------------------------------------------
1736 // Function : RenderTexture::_VerifyExtensions
1738 //---------------------------------------------------------------------------
1740 * @fn RenderTexture::_VerifyExtensions()
1741 * @brief Checks that the necessary extensions are available based on RT mode.
1743 bool RenderTexture::_VerifyExtensions()
1746 // a second call to _VerifyExtensions will allways return true, causing a crash
1747 // if the extension is not supported
1748 if ( true || !fctPtrInited )
1750 fctPtrInited = true;
1751 wglGetExtensionsStringARBProc wglGetExtensionsStringARBPtr = (wglGetExtensionsStringARBProc)wglGetProcAddress( "wglGetExtensionsStringARB" );
1752 if ( wglGetExtensionsStringARBPtr == 0 )
1754 PrintExtensionError("WGL_ARB_extensions_string");
1757 string wglExtensionsString = wglGetExtensionsStringARBPtr( wglGetCurrentDC() );
1758 if ( SGSearchExtensionsString( wglExtensionsString.c_str(), "WGL_ARB_pixel_format" ) )
1760 wglChoosePixelFormatARBPtr = (wglChoosePixelFormatARBProc)SGLookupFunction("wglChoosePixelFormatARB");
1761 wglGetPixelFormatAttribivARBPtr = (wglGetPixelFormatAttribivARBProc)SGLookupFunction("wglGetPixelFormatAttribivARB");
1765 PrintExtensionError("WGL_ARB_pixel_format");
1768 if ( SGSearchExtensionsString( wglExtensionsString.c_str(), "WGL_ARB_pbuffer" ) )
1770 wglCreatePbufferARBPtr = (wglCreatePbufferARBProc)SGLookupFunction("wglCreatePbufferARB");
1771 wglGetPbufferDCARBPtr = (wglGetPbufferDCARBProc)SGLookupFunction("wglGetPbufferDCARB");
1772 wglQueryPbufferARBPtr = (wglQueryPbufferARBProc)SGLookupFunction("wglQueryPbufferARB");
1773 wglReleasePbufferDCARBPtr = (wglReleasePbufferDCARBProc)SGLookupFunction("wglReleasePbufferDCARB");
1774 wglDestroyPbufferARBPtr = (wglDestroyPbufferARBProc)SGLookupFunction("wglDestroyPbufferARB");
1778 PrintExtensionError("WGL_ARB_pbuffer");
1781 if ( SGSearchExtensionsString( wglExtensionsString.c_str(), "WGL_ARB_render_texture" ) )
1783 wglBindTexImageARBPtr = (wglBindTexImageARBProc)SGLookupFunction("wglBindTexImageARB");
1784 wglReleaseTexImageARBPtr = (wglReleaseTexImageARBProc)SGLookupFunction("wglReleaseTexImageARB");
1786 else if ( _bIsTexture )
1788 PrintExtensionError("WGL_ARB_render_texture");
1791 if (_bRectangle && !SGIsOpenGLExtensionSupported( "GL_NV_texture_rectangle" ))
1793 PrintExtensionError("GL_NV_texture_rectangle");
1796 if (_bFloat && !(SGIsOpenGLExtensionSupported( "GL_NV_float_buffer" ) || SGSearchExtensionsString( wglExtensionsString.c_str(), "WGL_ATI_pixel_format_float" )))
1798 PrintExtensionError("GL_NV_float_buffer or GL_ATI_pixel_format_float");
1802 if (_bFloat && _bIsTexture && !(SGIsOpenGLExtensionSupported( "GL_NV_float_buffer" ) || SGIsOpenGLExtensionSupported( "GL_ATI_texture_float" )))
1804 PrintExtensionError("NV_float_buffer or ATI_texture_float");
1806 if (_bIsDepthTexture && !SGIsOpenGLExtensionSupported( "GL_ARB_depth_texture" ))
1809 #if defined(_DEBUG) | defined(DEBUG)
1810 SG_LOG(SG_GL, SG_ALERT,
1811 "RenderTexture Warning: "
1812 "OpenGL extension GL_ARB_depth_texture not available."
1813 " Using glReadPixels() to emulate behavior.");
1815 _bHasARBDepthTexture = false;
1816 //PrintExtensionError("GL_ARB_depth_texture");
1822 #elif defined( __APPLE__ )
1825 // First try the glX version 1.3 functions.
1826 glXChooseFBConfigPtr = (glXChooseFBConfigProc)SGLookupFunction("glXChooseFBConfig");
1827 glXCreatePbufferPtr = (glXCreatePbufferProc)SGLookupFunction("glXCreatePbuffer");
1828 glXGetVisualFromFBConfigPtr = (glXGetVisualFromFBConfigProc)SGLookupFunction("glXGetVisualFromFBConfig");
1829 glXCreateContextPtr = (glXCreateContextProc)SGLookupFunction("glXCreateContext");
1830 glXDestroyPbufferPtr = (glXDestroyPbufferProc)SGLookupFunction("glXDestroyPbuffer");
1831 glXQueryDrawablePtr = (glXQueryDrawableProc)SGLookupFunction("glXQueryDrawable");
1833 if (glXChooseFBConfigPtr &&
1834 glXCreatePbufferPtr &&
1835 glXGetVisualFromFBConfigPtr &&
1836 glXCreateContextPtr &&
1837 glXDestroyPbufferPtr &&
1838 glXQueryDrawablePtr)
1839 glXVersion1_3Present = true;
1842 glXChooseFBConfigPtr = (glXChooseFBConfigProc)SGLookupFunction("glXChooseFBConfigSGIX");
1843 glXCreateGLXPbufferPtr = (glXCreateGLXPbufferProc)SGLookupFunction("glXCreateGLXPbufferSGIX");
1844 glXGetVisualFromFBConfigPtr = (glXGetVisualFromFBConfigProc)SGLookupFunction("glXGetVisualFromFBConfigSGIX");
1845 glXCreateContextWithConfigPtr = (glXCreateContextWithConfigProc)SGLookupFunction("glXCreateContextWithConfigSGIX");
1846 glXDestroyPbufferPtr = (glXDestroyPbufferProc)SGLookupFunction("glXDestroyGLXPbufferSGIX");
1847 glXQueryGLXPbufferSGIXPtr = (glXQueryGLXPbufferSGIXProc)SGLookupFunction("glXQueryGLXPbufferSGIX");
1850 if (!glXChooseFBConfigPtr ||
1851 !glXCreateGLXPbufferPtr ||
1852 !glXGetVisualFromFBConfigPtr ||
1853 !glXCreateContextWithConfigPtr ||
1854 !glXDestroyPbufferPtr ||
1855 !glXQueryGLXPbufferSGIXPtr)
1859 if (_bIsDepthTexture && !GL_ARB_depth_texture)
1861 PrintExtensionError("GL_ARB_depth_texture");
1864 if (_bFloat && _bIsTexture && !GLX_NV_float_buffer)
1866 PrintExtensionError("GLX_NV_float_buffer");
1869 if (_eUpdateMode == RT_RENDER_TO_TEXTURE)
1871 PrintExtensionError("Some GLX render texture extension: Please implement me!");
1879 //---------------------------------------------------------------------------
1880 // Function : RenderTexture::_InitializeTextures
1882 //---------------------------------------------------------------------------
1884 * @fn RenderTexture::_InitializeTextures()
1885 * @brief Initializes the state of textures used by the RenderTexture.
1887 bool RenderTexture::_InitializeTextures()
1889 // Determine the appropriate texture formats and filtering modes.
1890 if (_bIsTexture || _bIsDepthTexture)
1892 if (_bRectangle && GL_NV_texture_rectangle)
1893 _iTextureTarget = GL_TEXTURE_RECTANGLE_NV;
1895 _iTextureTarget = GL_TEXTURE_2D;
1900 glGenTextures(1, (GLuint*)&_iTextureID);
1901 glBindTexture(_iTextureTarget, _iTextureID);
1903 // Use clamp to edge as the default texture wrap mode for all tex
1904 glTexParameteri(_iTextureTarget, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1905 glTexParameteri(_iTextureTarget, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1906 // Use NEAREST as the default texture filtering mode.
1907 glTexParameteri(_iTextureTarget, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1908 glTexParameteri(_iTextureTarget, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1911 if (RT_COPY_TO_TEXTURE == _eUpdateMode)
1913 GLuint iInternalFormat;
1920 SG_LOG(SG_GL, SG_ALERT,
1921 "RenderTexture Error: mipmapped float textures not "
1926 switch(_iNumComponents)
1929 if (GL_NV_float_buffer)
1931 iInternalFormat = (_iNumColorBits[0] > 16) ?
1932 GL_FLOAT_R32_NV : GL_FLOAT_R16_NV;
1934 else if (GL_ATI_texture_float)
1936 iInternalFormat = (_iNumColorBits[0] > 16) ?
1937 GL_LUMINANCE_FLOAT32_ATI :
1938 GL_LUMINANCE_FLOAT16_ATI;
1940 iFormat = GL_LUMINANCE;
1943 if (GL_NV_float_buffer)
1945 iInternalFormat = (_iNumColorBits[0] > 16) ?
1946 GL_FLOAT_RG32_NV : GL_FLOAT_RG16_NV;
1948 else if (GL_ATI_texture_float)
1950 iInternalFormat = (_iNumColorBits[0] > 16) ?
1951 GL_LUMINANCE_ALPHA_FLOAT32_ATI :
1952 GL_LUMINANCE_ALPHA_FLOAT16_ATI;
1954 iFormat = GL_LUMINANCE_ALPHA;
1957 if (GL_NV_float_buffer)
1959 iInternalFormat = (_iNumColorBits[0] > 16) ?
1960 GL_FLOAT_RGB32_NV : GL_FLOAT_RGB16_NV;
1962 else if (GL_ATI_texture_float)
1964 iInternalFormat = (_iNumColorBits[0] > 16) ?
1965 GL_RGB_FLOAT32_ATI : GL_RGB_FLOAT16_ATI;
1970 if (GL_NV_float_buffer)
1972 iInternalFormat = (_iNumColorBits[0] > 16) ?
1973 GL_FLOAT_RGBA32_NV : GL_FLOAT_RGBA16_NV;
1975 else if (GL_ATI_texture_float)
1977 iInternalFormat = (_iNumColorBits[0] > 16) ?
1978 GL_RGBA_FLOAT32_ATI : GL_RGBA_FLOAT16_ATI;
1983 SG_LOG(SG_GL, SG_INFO, "RenderTexture Error: "
1984 "Invalid number of components: " <<
1991 if (4 == _iNumComponents)
1993 iInternalFormat = GL_RGBA8;
1998 iInternalFormat = GL_RGB8;
2003 // Allocate the texture image (but pass it no data for now).
2004 glTexImage2D(_iTextureTarget, 0, iInternalFormat,
2005 _iWidth, _iHeight, 0, iFormat, GL_FLOAT, NULL);
2009 if (_bIsDepthTexture)
2011 glGenTextures(1, (GLuint*)&_iDepthTextureID);
2012 glBindTexture(_iTextureTarget, _iDepthTextureID);
2014 // Use clamp to edge as the default texture wrap mode for all tex
2015 glTexParameteri(_iTextureTarget, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2016 glTexParameteri(_iTextureTarget, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
2017 // Use NEAREST as the default texture filtering mode.
2018 glTexParameteri(_iTextureTarget, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
2019 glTexParameteri(_iTextureTarget, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
2021 if (RT_COPY_TO_TEXTURE == _eUpdateMode)
2024 if (_bHasARBDepthTexture)
2026 // Allocate the texture image (but pass it no data for now).
2027 glTexImage2D(_iTextureTarget, 0, GL_DEPTH_COMPONENT,
2028 _iWidth, _iHeight, 0, GL_DEPTH_COMPONENT,
2033 // allocate memory for depth texture
2034 // Since this is slow, we warn the user in debug mode. (above)
2035 _pPoorDepthTexture = new unsigned short[_iWidth * _iHeight];
2036 glTexImage2D(_iTextureTarget, 0, GL_LUMINANCE16,
2037 _iWidth, _iHeight, 0, GL_LUMINANCE,
2038 GL_UNSIGNED_SHORT, _pPoorDepthTexture);
2048 //---------------------------------------------------------------------------
2049 // Function : RenderTexture::_MaybeCopyBuffer
2051 //---------------------------------------------------------------------------
2053 * @fn RenderTexture::_MaybeCopyBuffer()
2054 * @brief Does the actual copying for RenderTextures with RT_COPY_TO_TEXTURE
2056 void RenderTexture::_MaybeCopyBuffer()
2059 if (RT_COPY_TO_TEXTURE == _eUpdateMode)
2063 glBindTexture(_iTextureTarget, _iTextureID);
2064 glCopyTexSubImage2D(_iTextureTarget,
2065 0, 0, 0, 0, 0, _iWidth, _iHeight);
2067 if (_bIsDepthTexture)
2069 glBindTexture(_iTextureTarget, _iDepthTextureID);
2070 // HOW TO COPY DEPTH TEXTURE??? Supposedly this just magically works...
2072 if (_bHasARBDepthTexture)
2074 glCopyTexSubImage2D(_iTextureTarget, 0, 0, 0, 0, 0,
2079 // no 'real' depth texture available, so behavior has to be emulated
2080 // using glReadPixels (beware, this is (naturally) slow ...)
2081 glReadPixels(0, 0, _iWidth, _iHeight, GL_DEPTH_COMPONENT,
2082 GL_UNSIGNED_SHORT, _pPoorDepthTexture);
2083 glTexImage2D(_iTextureTarget, 0, GL_LUMINANCE16,
2084 _iWidth, _iHeight, 0, GL_LUMINANCE,
2085 GL_UNSIGNED_SHORT, _pPoorDepthTexture);
2094 glBindTexture(_iTextureTarget, _iTextureID);
2095 glCopyTexSubImage2D(_iTextureTarget, 0, 0, 0, 0, 0, _iWidth, _iHeight);
2097 if (_bIsDepthTexture)
2099 glBindTexture(_iTextureTarget, _iDepthTextureID);
2100 assert(_bHasARBDepthTexture);
2101 glCopyTexSubImage2D(_iTextureTarget, 0, 0, 0, 0, 0, _iWidth, _iHeight);
2107 //---------------------------------------------------------------------------
2108 // Function : RenderTexture::_ReleaseBoundBuffers
2110 //---------------------------------------------------------------------------
2112 * @fn RenderTexture::_ReleaseBoundBuffers()
2113 * @brief Releases buffer bindings on RenderTextures with RT_RENDER_TO_TEXTURE
2115 bool RenderTexture::_ReleaseBoundBuffers()
2118 if (_bIsTexture && RT_RENDER_TO_TEXTURE == _eUpdateMode)
2120 glBindTexture(_iTextureTarget, _iTextureID);
2122 // release the pbuffer from the render texture object
2123 if (0 != _iCurrentBoundBuffer && _bIsBufferBound)
2125 if (FALSE == wglReleaseTexImageARBPtr(_hPBuffer, _iCurrentBoundBuffer))
2130 _bIsBufferBound = false;
2134 if (_bIsDepthTexture && RT_RENDER_TO_TEXTURE == _eUpdateMode)
2136 glBindTexture(_iTextureTarget, _iDepthTextureID);
2138 // release the pbuffer from the render texture object
2139 if (FALSE == wglReleaseTexImageARBPtr(_hPBuffer, WGL_DEPTH_COMPONENT_NV))
2147 // textures can't be bound in Linux
2152 //---------------------------------------------------------------------------
2153 // Function : RenderTexture::_MakeCurrent
2155 //---------------------------------------------------------------------------
2157 * @fn RenderTexture::_MakeCurrent()
2158 * @brief Makes the RenderTexture's context current
2161 bool RenderTexture::_MakeCurrent()
2164 // make the pbuffer's rendering context current.
2165 if (FALSE == wglMakeCurrent( _hDC, _hGLContext))
2170 #elif defined( __APPLE__ )
2172 if (false == glXMakeCurrent(_pDisplay, _hPBuffer, _hGLContext))
2181 /////////////////////////////////////////////////////////////////////////////
2183 // Begin Deprecated Interface
2185 /////////////////////////////////////////////////////////////////////////////
2187 //---------------------------------------------------------------------------
2188 // Function : RenderTexture::RenderTexture
2190 //---------------------------------------------------------------------------
2192 * @fn RenderTexture::RenderTexture()
2193 * @brief Constructor.
2195 RenderTexture::RenderTexture(int width, int height,
2196 bool bIsTexture /* = true */,
2197 bool bIsDepthTexture /* = false */)
2200 _bIsTexture(bIsTexture),
2201 _bIsDepthTexture(bIsDepthTexture),
2202 _bHasARBDepthTexture(true), // [Redge]
2203 _eUpdateMode(RT_RENDER_TO_TEXTURE),
2204 _bInitialized(false),
2206 _iCurrentBoundBuffer(0),
2208 _iNumStencilBits(0),
2209 _bDoubleBuffered(false),
2214 _bShareObjects(false),
2215 _bCopyContext(false),
2221 _hPreviousContext(0),
2222 #elif defined( __APPLE__ )
2227 _hPreviousContext(0),
2228 _hPreviousDrawable(0),
2230 _iTextureTarget(GL_NONE),
2232 _iDepthTextureID(0),
2233 _pPoorDepthTexture(0) // [Redge]
2235 assert(width > 0 && height > 0);
2236 #if defined DEBUG || defined _DEBUG
2237 SG_LOG(SG_GL, SG_ALERT,
2238 "RenderTexture Warning: Deprecated Contructor interface used.");
2241 _iNumColorBits[0] = _iNumColorBits[1] =
2242 _iNumColorBits[2] = _iNumColorBits[3] = 0;
2243 _bPowerOf2 = IsPowerOfTwo(width) && IsPowerOfTwo(height);
2246 //------------------------------------------------------------------------------
2247 // Function : RenderTexture::Initialize
2249 //------------------------------------------------------------------------------
2251 * @fn RenderTexture::Initialize(bool bShare, bool bDepth, bool bStencil, bool bMipmap, unsigned int iRBits, unsigned int iGBits, unsigned int iBBits, unsigned int iABits);
2252 * @brief Initializes the RenderTexture, sharing display lists and textures if specified.
2254 * This function actually does the creation of the p-buffer. It can only be called
2255 * once a GL context has already been created. Note that if the texture is not
2256 * power of two dimensioned, or has more than 8 bits per channel, enabling mipmapping
2257 * will cause an error.
2259 bool RenderTexture::Initialize(bool bShare /* = true */,
2260 bool bDepth /* = false */,
2261 bool bStencil /* = false */,
2262 bool bMipmap /* = false */,
2263 bool bAnisoFilter /* = false */,
2264 unsigned int iRBits /* = 8 */,
2265 unsigned int iGBits /* = 8 */,
2266 unsigned int iBBits /* = 8 */,
2267 unsigned int iABits /* = 8 */,
2268 UpdateMode updateMode /* = RT_RENDER_TO_TEXTURE */)
2270 if (0 == _iWidth || 0 == _iHeight)
2273 #if defined DEBUG || defined _DEBUG
2274 SG_LOG(SG_GL, SG_ALERT,
2275 "RenderTexture Warning: Deprecated Initialize() interface used.");
2278 // create a mode string.
2281 mode.append("depth ");
2283 mode.append("stencil ");
2285 mode.append("mipmap ");
2286 if (iRBits + iGBits + iBBits + iABits > 0)
2297 char bitVector[100];
2298 snprintf( bitVector, 100,
2299 "%d%s,%d%s,%d%s,%d%s",
2300 iRBits, (iRBits >= 16) ? "f" : "",
2301 iGBits, (iGBits >= 16) ? "f" : "",
2302 iBBits, (iBBits >= 16) ? "f" : "",
2303 iABits, (iABits >= 16) ? "f" : "");
2304 mode.append(bitVector);
2309 if (GL_NV_texture_rectangle &&
2310 ((!IsPowerOfTwo(_iWidth) || !IsPowerOfTwo(_iHeight))
2311 || iRBits >= 16 || iGBits > 16 || iBBits > 16 || iABits >= 16))
2312 mode.append("texRECT ");
2314 mode.append("tex2D ");
2316 if (_bIsDepthTexture)
2318 if (GL_NV_texture_rectangle &&
2319 ((!IsPowerOfTwo(_iWidth) || !IsPowerOfTwo(_iHeight))
2320 || iRBits >= 16 || iGBits > 16 || iBBits > 16 || iABits >= 16))
2321 mode.append("texRECT ");
2323 mode.append("tex2D ");
2325 if (RT_COPY_TO_TEXTURE == updateMode)
2328 _pixelFormatAttribs.clear();
2329 _pbufferAttribs.clear();
2332 _pixelFormatAttribs.push_back(WGL_DRAW_TO_PBUFFER_ARB);
2333 _pixelFormatAttribs.push_back(true);
2334 _pixelFormatAttribs.push_back(WGL_SUPPORT_OPENGL_ARB);
2335 _pixelFormatAttribs.push_back(true);
2337 _pbufferAttribs.push_back(WGL_PBUFFER_LARGEST_ARB);
2338 _pbufferAttribs.push_back(true);
2339 #elif defined( __APPLE__ )
2341 _pixelFormatAttribs.push_back(GLX_RENDER_TYPE_SGIX);
2342 _pixelFormatAttribs.push_back(GLX_RGBA_BIT_SGIX);
2343 _pixelFormatAttribs.push_back(GLX_DRAWABLE_TYPE_SGIX);
2344 _pixelFormatAttribs.push_back(GLX_PBUFFER_BIT_SGIX);
2347 _ParseModeString(mode.c_str(), _pixelFormatAttribs, _pbufferAttribs);
2350 _pixelFormatAttribs.push_back(0);
2351 _pbufferAttribs.push_back(0);
2353 _pixelFormatAttribs.push_back(None);
2356 Initialize(_iWidth, _iHeight, bShare);
2362 //---------------------------------------------------------------------------
2363 // Function : RenderTexture::Reset
2365 //---------------------------------------------------------------------------
2367 * @fn RenderTexture::Reset(int iWidth, int iHeight, unsigned int iMode, bool bIsTexture, bool bIsDepthTexture)
2368 * @brief Resets the resolution of the offscreen buffer.
2370 * Causes the buffer to delete itself. User must call Initialize() again
2373 bool RenderTexture::Reset(int iWidth, int iHeight)
2375 SG_LOG(SG_GL, SG_ALERT,
2376 "RenderTexture Warning: Deprecated Reset() interface used.");
2380 SG_LOG(SG_GL, SG_ALERT, "RenderTexture::Reset(): failed to invalidate.");