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 wglGetExtensionsStringARBProc wglGetExtensionsStringARBPtr = (wglGetExtensionsStringARBProc)wglGetProcAddress( "wglGetExtensionsStringARB" );
1652 if ( wglGetExtensionsStringARBPtr == 0 )
1654 PrintExtensionError("WGL_ARB_extensions_string");
1657 string wglExtensionsString = wglGetExtensionsStringARBPtr( wglGetCurrentDC() );
1658 if ( SGSearchExtensionsString( wglExtensionsString.c_str(), "WGL_ARB_pixel_format" ) )
1660 wglChoosePixelFormatARBPtr = (wglChoosePixelFormatARBProc)SGLookupFunction("wglChoosePixelFormatARB");
1661 wglGetPixelFormatAttribivARBPtr = (wglGetPixelFormatAttribivARBProc)SGLookupFunction("wglGetPixelFormatAttribivARB");
1665 PrintExtensionError("WGL_ARB_pixel_format");
1668 if ( SGSearchExtensionsString( wglExtensionsString.c_str(), "WGL_ARB_pbuffer" ) )
1670 wglCreatePbufferARBPtr = (wglCreatePbufferARBProc)SGLookupFunction("wglCreatePbufferARB");
1671 wglGetPbufferDCARBPtr = (wglGetPbufferDCARBProc)SGLookupFunction("wglGetPbufferDCARB");
1672 wglQueryPbufferARBPtr = (wglQueryPbufferARBProc)SGLookupFunction("wglQueryPbufferARB");
1673 wglReleasePbufferDCARBPtr = (wglReleasePbufferDCARBProc)SGLookupFunction("wglReleasePbufferDCARB");
1674 wglDestroyPbufferARBPtr = (wglDestroyPbufferARBProc)SGLookupFunction("wglDestroyPbufferARB");
1678 PrintExtensionError("WGL_ARB_pbuffer");
1681 if ( SGSearchExtensionsString( wglExtensionsString.c_str(), "WGL_ARB_render_texture" ) )
1683 wglBindTexImageARBPtr = (wglBindTexImageARBProc)SGLookupFunction("wglBindTexImageARB");
1684 wglReleaseTexImageARBPtr = (wglReleaseTexImageARBProc)SGLookupFunction("wglReleaseTexImageARB");
1686 else if ( _bIsTexture )
1688 PrintExtensionError("WGL_ARB_render_texture");
1691 if (_bRectangle && !SGIsOpenGLExtensionSupported( "GL_NV_texture_rectangle" ))
1693 PrintExtensionError("GL_NV_texture_rectangle");
1696 if (_bFloat && !(SGIsOpenGLExtensionSupported( "GL_NV_float_buffer" ) || SGSearchExtensionsString( wglExtensionsString.c_str(), "WGL_ATI_pixel_format_float" )))
1698 PrintExtensionError("GL_NV_float_buffer or GL_ATI_pixel_format_float");
1702 if (_bFloat && _bIsTexture && !(SGIsOpenGLExtensionSupported( "GL_NV_float_buffer" ) || SGIsOpenGLExtensionSupported( "GL_ATI_texture_float" )))
1704 PrintExtensionError("NV_float_buffer or ATI_texture_float");
1706 if (_bIsDepthTexture && !SGIsOpenGLExtensionSupported( "GL_ARB_depth_texture" ))
1709 #if defined(_DEBUG) | defined(DEBUG)
1711 "RenderTexture Warning: "
1712 "OpenGL extension GL_ARB_depth_texture not available.\n"
1713 " Using glReadPixels() to emulate behavior.\n");
1715 _bHasARBDepthTexture = false;
1716 //PrintExtensionError("GL_ARB_depth_texture");
1723 if (!GLX_SGIX_pbuffer)
1725 PrintExtensionError("GL_SGIX_pbuffer");
1728 if (!GLX_SGIX_fbconfig)
1730 PrintExtensionError("GL_SGIX_fbconfig");
1733 if (_bIsDepthTexture && !GL_ARB_depth_texture)
1735 PrintExtensionError("GL_ARB_depth_texture");
1738 if (_bFloat && _bIsTexture && !GL_NV_float_buffer)
1740 PrintExtensionError("GL_NV_float_buffer");
1743 if (_eUpdateMode == RT_RENDER_TO_TEXTURE)
1745 PrintExtensionError("Some GLX render texture extension: FIXME!");
1753 //---------------------------------------------------------------------------
1754 // Function : RenderTexture::_InitializeTextures
1756 //---------------------------------------------------------------------------
1758 * @fn RenderTexture::_InitializeTextures()
1759 * @brief Initializes the state of textures used by the RenderTexture.
1761 bool RenderTexture::_InitializeTextures()
1763 // Determine the appropriate texture formats and filtering modes.
1764 if (_bIsTexture || _bIsDepthTexture)
1766 if (_bRectangle && GL_NV_texture_rectangle)
1767 _iTextureTarget = GL_TEXTURE_RECTANGLE_NV;
1769 _iTextureTarget = GL_TEXTURE_2D;
1774 glGenTextures(1, &_iTextureID);
1775 glBindTexture(_iTextureTarget, _iTextureID);
1777 // Use clamp to edge as the default texture wrap mode for all tex
1778 glTexParameteri(_iTextureTarget, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1779 glTexParameteri(_iTextureTarget, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1780 // Use NEAREST as the default texture filtering mode.
1781 glTexParameteri(_iTextureTarget, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1782 glTexParameteri(_iTextureTarget, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1785 if (RT_COPY_TO_TEXTURE == _eUpdateMode)
1787 GLuint iInternalFormat;
1795 "RenderTexture Error: mipmapped float textures not "
1800 switch(_iNumComponents)
1803 if (GL_NV_float_buffer)
1805 iInternalFormat = (_iNumColorBits[0] > 16) ?
1806 GL_FLOAT_R32_NV : GL_FLOAT_R16_NV;
1808 else if (GL_ATI_texture_float)
1810 iInternalFormat = (_iNumColorBits[0] > 16) ?
1811 GL_LUMINANCE_FLOAT32_ATI :
1812 GL_LUMINANCE_FLOAT16_ATI;
1814 iFormat = GL_LUMINANCE;
1817 if (GL_NV_float_buffer)
1819 iInternalFormat = (_iNumColorBits[0] > 16) ?
1820 GL_FLOAT_RG32_NV : GL_FLOAT_RG16_NV;
1822 else if (GL_ATI_texture_float)
1824 iInternalFormat = (_iNumColorBits[0] > 16) ?
1825 GL_LUMINANCE_ALPHA_FLOAT32_ATI :
1826 GL_LUMINANCE_ALPHA_FLOAT16_ATI;
1828 iFormat = GL_LUMINANCE_ALPHA;
1831 if (GL_NV_float_buffer)
1833 iInternalFormat = (_iNumColorBits[0] > 16) ?
1834 GL_FLOAT_RGB32_NV : GL_FLOAT_RGB16_NV;
1836 else if (GL_ATI_texture_float)
1838 iInternalFormat = (_iNumColorBits[0] > 16) ?
1839 GL_RGB_FLOAT32_ATI : GL_RGB_FLOAT16_ATI;
1844 if (GL_NV_float_buffer)
1846 iInternalFormat = (_iNumColorBits[0] > 16) ?
1847 GL_FLOAT_RGBA32_NV : GL_FLOAT_RGBA16_NV;
1849 else if (GL_ATI_texture_float)
1851 iInternalFormat = (_iNumColorBits[0] > 16) ?
1852 GL_RGBA_FLOAT32_ATI : GL_RGBA_FLOAT16_ATI;
1857 printf("RenderTexture Error: "
1858 "Invalid number of components: %d\n",
1865 if (4 == _iNumComponents)
1867 iInternalFormat = GL_RGBA8;
1872 iInternalFormat = GL_RGB8;
1877 // Allocate the texture image (but pass it no data for now).
1878 glTexImage2D(_iTextureTarget, 0, iInternalFormat,
1879 _iWidth, _iHeight, 0, iFormat, GL_FLOAT, NULL);
1883 if (_bIsDepthTexture)
1885 glGenTextures(1, &_iDepthTextureID);
1886 glBindTexture(_iTextureTarget, _iDepthTextureID);
1888 // Use clamp to edge as the default texture wrap mode for all tex
1889 glTexParameteri(_iTextureTarget, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1890 glTexParameteri(_iTextureTarget, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1891 // Use NEAREST as the default texture filtering mode.
1892 glTexParameteri(_iTextureTarget, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1893 glTexParameteri(_iTextureTarget, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1895 if (RT_COPY_TO_TEXTURE == _eUpdateMode)
1898 if (_bHasARBDepthTexture)
1900 // Allocate the texture image (but pass it no data for now).
1901 glTexImage2D(_iTextureTarget, 0, GL_DEPTH_COMPONENT,
1902 _iWidth, _iHeight, 0, GL_DEPTH_COMPONENT,
1907 // allocate memory for depth texture
1908 // Since this is slow, we warn the user in debug mode. (above)
1909 _pPoorDepthTexture = new unsigned short[_iWidth * _iHeight];
1910 glTexImage2D(_iTextureTarget, 0, GL_LUMINANCE16,
1911 _iWidth, _iHeight, 0, GL_LUMINANCE,
1912 GL_UNSIGNED_SHORT, _pPoorDepthTexture);
1922 //---------------------------------------------------------------------------
1923 // Function : RenderTexture::_MaybeCopyBuffer
1925 //---------------------------------------------------------------------------
1927 * @fn RenderTexture::_MaybeCopyBuffer()
1928 * @brief Does the actual copying for RenderTextures with RT_COPY_TO_TEXTURE
1930 void RenderTexture::_MaybeCopyBuffer()
1933 if (RT_COPY_TO_TEXTURE == _eUpdateMode)
1937 glBindTexture(_iTextureTarget, _iTextureID);
1938 glCopyTexSubImage2D(_iTextureTarget,
1939 0, 0, 0, 0, 0, _iWidth, _iHeight);
1941 if (_bIsDepthTexture)
1943 glBindTexture(_iTextureTarget, _iDepthTextureID);
1944 // HOW TO COPY DEPTH TEXTURE??? Supposedly this just magically works...
1946 if (_bHasARBDepthTexture)
1948 glCopyTexSubImage2D(_iTextureTarget, 0, 0, 0, 0, 0,
1953 // no 'real' depth texture available, so behavior has to be emulated
1954 // using glReadPixels (beware, this is (naturally) slow ...)
1955 glReadPixels(0, 0, _iWidth, _iHeight, GL_DEPTH_COMPONENT,
1956 GL_UNSIGNED_SHORT, _pPoorDepthTexture);
1957 glTexImage2D(_iTextureTarget, 0, GL_LUMINANCE16,
1958 _iWidth, _iHeight, 0, GL_LUMINANCE,
1959 GL_UNSIGNED_SHORT, _pPoorDepthTexture);
1968 glBindTexture(_iTextureTarget, _iTextureID);
1969 glCopyTexSubImage2D(_iTextureTarget, 0, 0, 0, 0, 0, _iWidth, _iHeight);
1971 if (_bIsDepthTexture)
1973 glBindTexture(_iTextureTarget, _iDepthTextureID);
1974 assert(_bHasARBDepthTexture);
1975 glCopyTexSubImage2D(_iTextureTarget, 0, 0, 0, 0, 0, _iWidth, _iHeight);
1981 //---------------------------------------------------------------------------
1982 // Function : RenderTexture::_ReleaseBoundBuffers
1984 //---------------------------------------------------------------------------
1986 * @fn RenderTexture::_ReleaseBoundBuffers()
1987 * @brief Releases buffer bindings on RenderTextures with RT_RENDER_TO_TEXTURE
1989 bool RenderTexture::_ReleaseBoundBuffers()
1992 if (_bIsTexture && RT_RENDER_TO_TEXTURE == _eUpdateMode)
1994 glBindTexture(_iTextureTarget, _iTextureID);
1996 // release the pbuffer from the render texture object
1997 if (0 != _iCurrentBoundBuffer && _bIsBufferBound)
1999 if (FALSE == wglReleaseTexImageARBPtr(_hPBuffer, _iCurrentBoundBuffer))
2004 _bIsBufferBound = false;
2008 if (_bIsDepthTexture && RT_RENDER_TO_TEXTURE == _eUpdateMode)
2010 glBindTexture(_iTextureTarget, _iDepthTextureID);
2012 // release the pbuffer from the render texture object
2013 if (FALSE == wglReleaseTexImageARBPtr(_hPBuffer, WGL_DEPTH_COMPONENT_NV))
2021 // textures can't be bound in Linux
2026 //---------------------------------------------------------------------------
2027 // Function : RenderTexture::_MakeCurrent
2029 //---------------------------------------------------------------------------
2031 * @fn RenderTexture::_MakeCurrent()
2032 * @brief Makes the RenderTexture's context current
2035 bool RenderTexture::_MakeCurrent()
2038 // make the pbuffer's rendering context current.
2039 if (FALSE == wglMakeCurrent( _hDC, _hGLContext))
2045 if (false == glXMakeCurrent(_pDisplay, _hPBuffer, _hGLContext))
2054 /////////////////////////////////////////////////////////////////////////////
2056 // Begin Deprecated Interface
2058 /////////////////////////////////////////////////////////////////////////////
2060 //---------------------------------------------------------------------------
2061 // Function : RenderTexture::RenderTexture
2063 //---------------------------------------------------------------------------
2065 * @fn RenderTexture::RenderTexture()
2066 * @brief Constructor.
2068 RenderTexture::RenderTexture(int width, int height,
2069 bool bIsTexture /* = true */,
2070 bool bIsDepthTexture /* = false */)
2073 _bIsTexture(bIsTexture),
2074 _bIsDepthTexture(bIsDepthTexture),
2075 _bHasARBDepthTexture(true), // [Redge]
2076 _eUpdateMode(RT_RENDER_TO_TEXTURE),
2077 _bInitialized(false),
2079 _iCurrentBoundBuffer(0),
2081 _iNumStencilBits(0),
2082 _bDoubleBuffered(false),
2087 _bShareObjects(false),
2088 _bCopyContext(false),
2094 _hPreviousContext(0),
2099 _hPreviousContext(0),
2100 _hPreviousDrawable(0),
2102 _iTextureTarget(GL_NONE),
2104 _iDepthTextureID(0),
2105 _pPoorDepthTexture(0) // [Redge]
2107 assert(width > 0 && height > 0);
2108 #if defined DEBUG || defined _DEBUG
2110 "RenderTexture Warning: Deprecated Contructor interface used.\n");
2113 _iNumColorBits[0] = _iNumColorBits[1] =
2114 _iNumColorBits[2] = _iNumColorBits[3] = 0;
2115 _bPowerOf2 = IsPowerOfTwo(width) && IsPowerOfTwo(height);
2118 //------------------------------------------------------------------------------
2119 // Function : RenderTexture::Initialize
2121 //------------------------------------------------------------------------------
2123 * @fn RenderTexture::Initialize(bool bShare, bool bDepth, bool bStencil, bool bMipmap, unsigned int iRBits, unsigned int iGBits, unsigned int iBBits, unsigned int iABits);
2124 * @brief Initializes the RenderTexture, sharing display lists and textures if specified.
2126 * This function actually does the creation of the p-buffer. It can only be called
2127 * once a GL context has already been created. Note that if the texture is not
2128 * power of two dimensioned, or has more than 8 bits per channel, enabling mipmapping
2129 * will cause an error.
2131 bool RenderTexture::Initialize(bool bShare /* = true */,
2132 bool bDepth /* = false */,
2133 bool bStencil /* = false */,
2134 bool bMipmap /* = false */,
2135 bool bAnisoFilter /* = false */,
2136 unsigned int iRBits /* = 8 */,
2137 unsigned int iGBits /* = 8 */,
2138 unsigned int iBBits /* = 8 */,
2139 unsigned int iABits /* = 8 */,
2140 UpdateMode updateMode /* = RT_RENDER_TO_TEXTURE */)
2142 if (0 == _iWidth || 0 == _iHeight)
2145 #if defined DEBUG || defined _DEBUG
2147 "RenderTexture Warning: Deprecated Initialize() interface used.\n");
2150 // create a mode string.
2153 mode.append("depth ");
2155 mode.append("stencil ");
2157 mode.append("mipmap ");
2158 if (iRBits + iGBits + iBBits + iABits > 0)
2169 char bitVector[100];
2171 "%d%s,%d%s,%d%s,%d%s",
2172 iRBits, (iRBits >= 16) ? "f" : "",
2173 iGBits, (iGBits >= 16) ? "f" : "",
2174 iBBits, (iBBits >= 16) ? "f" : "",
2175 iABits, (iABits >= 16) ? "f" : "");
2176 mode.append(bitVector);
2181 if (GL_NV_texture_rectangle &&
2182 ((!IsPowerOfTwo(_iWidth) || !IsPowerOfTwo(_iHeight))
2183 || iRBits >= 16 || iGBits > 16 || iBBits > 16 || iABits >= 16))
2184 mode.append("texRECT ");
2186 mode.append("tex2D ");
2188 if (_bIsDepthTexture)
2190 if (GL_NV_texture_rectangle &&
2191 ((!IsPowerOfTwo(_iWidth) || !IsPowerOfTwo(_iHeight))
2192 || iRBits >= 16 || iGBits > 16 || iBBits > 16 || iABits >= 16))
2193 mode.append("texRECT ");
2195 mode.append("tex2D ");
2197 if (RT_COPY_TO_TEXTURE == updateMode)
2200 _pixelFormatAttribs.clear();
2201 _pbufferAttribs.clear();
2204 _pixelFormatAttribs.push_back(WGL_DRAW_TO_PBUFFER_ARB);
2205 _pixelFormatAttribs.push_back(true);
2206 _pixelFormatAttribs.push_back(WGL_SUPPORT_OPENGL_ARB);
2207 _pixelFormatAttribs.push_back(true);
2209 _pbufferAttribs.push_back(WGL_PBUFFER_LARGEST_ARB);
2210 _pbufferAttribs.push_back(true);
2212 _pixelFormatAttribs.push_back(GLX_RENDER_TYPE_SGIX);
2213 _pixelFormatAttribs.push_back(GLX_RGBA_BIT_SGIX);
2214 _pixelFormatAttribs.push_back(GLX_DRAWABLE_TYPE_SGIX);
2215 _pixelFormatAttribs.push_back(GLX_PBUFFER_BIT_SGIX);
2218 _ParseModeString(mode.c_str(), _pixelFormatAttribs, _pbufferAttribs);
2221 _pixelFormatAttribs.push_back(0);
2222 _pbufferAttribs.push_back(0);
2224 _pixelFormatAttribs.push_back(None);
2227 Initialize(_iWidth, _iHeight, bShare);
2233 //---------------------------------------------------------------------------
2234 // Function : RenderTexture::Reset
2236 //---------------------------------------------------------------------------
2238 * @fn RenderTexture::Reset(int iWidth, int iHeight, unsigned int iMode, bool bIsTexture, bool bIsDepthTexture)
2239 * @brief Resets the resolution of the offscreen buffer.
2241 * Causes the buffer to delete itself. User must call Initialize() again
2244 bool RenderTexture::Reset(int iWidth, int iHeight)
2247 "RenderTexture Warning: Deprecated Reset() interface used.\n");
2251 fprintf(stderr, "RenderTexture::Reset(): failed to invalidate.\n");