]> git.mxchange.org Git - simgear.git/blobdiff - simgear/screen/RenderTexture.cpp
Comment out GLX code for MacOS and (hopefully) add some MacOS AGL compattible code...
[simgear.git] / simgear / screen / RenderTexture.cpp
index bb49f308c17c0378552e67568ca088a5c461e541..d31a61e6babf3f0a3dd55d6f1a10f4311f8bc788 100644 (file)
@@ -122,6 +122,7 @@ RenderTexture::RenderTexture(const char *strMode)
     _hPBuffer(NULL),
     _hPreviousDC(0),
     _hPreviousContext(0),
+#elif defined( __APPLE__ )
 #else
     _pDisplay(NULL),
     _hGLContext(NULL),
@@ -145,6 +146,7 @@ RenderTexture::RenderTexture(const char *strMode)
     
     _pbufferAttribs.push_back(WGL_PBUFFER_LARGEST_ARB);
     _pbufferAttribs.push_back(true);
+#elif defined( __APPLE__ )
 #else
     _pbufferAttribs.push_back(GLX_RENDER_TYPE_SGIX);
     _pbufferAttribs.push_back(GLX_RGBA_BIT_SGIX);
@@ -254,7 +256,7 @@ void PrintExtensionError( char* strMsg, ... )
     char strBuffer[512];
     va_list args;
     va_start(args, strMsg);
-#ifdef _WIN32
+#if defined _WIN32 && !defined __CYGWIN__
     _vsnprintf( strBuffer, 512, strMsg, args );
 #else
     vsnprintf( strBuffer, 512, strMsg, args );
@@ -264,6 +266,7 @@ void PrintExtensionError( char* strMsg, ... )
     fprintf(stderr, strMsg);
 }
 
+
 //---------------------------------------------------------------------------
 // Function            : RenderTexture::Initialize
 // Description     : 
@@ -294,7 +297,7 @@ bool RenderTexture::Initialize(int width, int height,
     if (_bInitialized)
         _Invalidate();
 
-#if _WIN32
+#ifdef _WIN32
     // Get the current context.
     HDC hdc = wglGetCurrentDC();
     if (NULL == hdc)
@@ -439,6 +442,7 @@ bool RenderTexture::Initialize(int width, int height,
     fprintf(stderr, "\n");
 #endif
 
+#elif defined( __APPLE__ )
 #else // !_WIN32
     _pDisplay = glXGetCurrentDisplay();
     GLXContext context = glXGetCurrentContext();
@@ -520,6 +524,7 @@ bool RenderTexture::Initialize(int width, int height,
         _wglGetLastError();
         return false;
     }
+#elif defined( __APPLE__ )
 #else
     _hPreviousContext = glXGetCurrentContext();
     _hPreviousDrawable = glXGetCurrentDrawable();
@@ -544,6 +549,7 @@ bool RenderTexture::Initialize(int width, int height,
         _wglGetLastError();
         return false;
     }
+#elif defined( __APPLE__ )
 #else
     if (False == glXMakeCurrent(_pDisplay, 
                                 _hPreviousDrawable, _hPreviousContext))
@@ -582,7 +588,7 @@ bool RenderTexture::_Invalidate()
         glDeleteTextures(1, &_iDepthTextureID);
     }
     
-#if _WIN32
+#ifdef _WIN32
     if ( _hPBuffer )
     {
         // Check if we are currently rendering in the pbuffer
@@ -595,6 +601,7 @@ bool RenderTexture::_Invalidate()
         _hPBuffer = 0;
         return true;
     }
+#elif defined( __APPLE__ )
 #else
     if ( _hPBuffer )
     {
@@ -665,6 +672,7 @@ bool RenderTexture::Reset(const char *strMode, ...)
     
     _pbufferAttribs.push_back(WGL_PBUFFER_LARGEST_ARB);
     _pbufferAttribs.push_back(true);
+#elif defined( __APPLE__ )
 #else
     _pbufferAttribs.push_back(GLX_RENDER_TYPE_SGIX);
     _pbufferAttribs.push_back(GLX_RGBA_BIT_SGIX);
@@ -675,7 +683,7 @@ bool RenderTexture::Reset(const char *strMode, ...)
     va_list args;
     char strBuffer[256];
     va_start(args,strMode);
-#ifdef _WIN32
+#if defined _WIN32 && !defined __CYGWIN__
     _vsnprintf( strBuffer, 256, strMode, args );
 #else
     vsnprintf( strBuffer, 256, strMode, args );
@@ -734,6 +742,7 @@ bool RenderTexture::Resize(int iWidth, int iHeight)
         _hPBuffer = 0;
         return true;
     }
+#elif defined( __APPLE__ )
 #else
     if ( _hPBuffer )
     {
@@ -776,6 +785,7 @@ bool RenderTexture::BeginCapture()
     _hPreviousContext = wglGetCurrentContext();
     if (NULL == _hPreviousContext)
         _wglGetLastError();
+#elif defined( __APPLE__ )
 #else
     _hPreviousContext = glXGetCurrentContext();
     _hPreviousDrawable = glXGetCurrentDrawable();
@@ -813,6 +823,7 @@ bool RenderTexture::EndCapture()
         _wglGetLastError();
         return false;
     }
+#elif defined( __APPLE__ )
 #else
     if (False == glXMakeCurrent(_pDisplay, _hPreviousDrawable, 
                                 _hPreviousContext))
@@ -882,6 +893,7 @@ bool RenderTexture::BeginCapture(RenderTexture* current)
     _hPreviousContext = current->_hPreviousContext;
     if (NULL == _hPreviousContext)
         _wglGetLastError();
+#elif defined( __APPLE__ )
 #else
     _hPreviousContext = current->_hPreviousContext;
     _hPreviousDrawable = current->_hPreviousDrawable;
@@ -953,7 +965,7 @@ bool RenderTexture::BindBuffer( int iBuffer )
     {
         glBindTexture(_iTextureTarget, _iTextureID);
         
-#if _WIN32
+#ifdef _WIN32
         if (RT_RENDER_TO_TEXTURE == _eUpdateMode && _bIsTexture &&
             (!_bIsBufferBound || _iCurrentBoundBuffer != iBuffer))
         {
@@ -1065,17 +1077,22 @@ void RenderTexture::_ParseModeString(const char *modeString,
             pfAttribs.push_back(bitVec[1]);
             pfAttribs.push_back(WGL_BLUE_BITS_ARB);
             pfAttribs.push_back(bitVec[2]);
+#elif defined( __APPLE__ )
+            pfAttribs.push_back(AGL_RED_SIZE);
+            pfAttribs.push_back(bitVec[0]);
+            pfAttribs.push_back(AGL_GREEN_SIZE);
+            pfAttribs.push_back(bitVec[1]);
+            pfAttribs.push_back(AGL_BLUE_SIZE);
+            pfAttribs.push_back(bitVec[2]);
 #else
-# ifndef sgi
             pfAttribs.push_back(GLX_RED_SIZE);
             pfAttribs.push_back(bitVec[0]);
             pfAttribs.push_back(GLX_GREEN_SIZE);
             pfAttribs.push_back(bitVec[1]);
             pfAttribs.push_back(GLX_BLUE_SIZE);
             pfAttribs.push_back(bitVec[2]);
-# endif
 #endif
-                       _iNumComponents += 3;
+           _iNumComponents += 3;
             continue;
         }
                else if (kv.first == "rgb") 
@@ -1108,8 +1125,16 @@ void RenderTexture::_ParseModeString(const char *modeString,
             pfAttribs.push_back(bitVec[2]);
             pfAttribs.push_back(WGL_ALPHA_BITS_ARB);
             pfAttribs.push_back(bitVec[3]);
+#elif defined( __APPLE__ )
+            pfAttribs.push_back(AGL_RED_SIZE);
+            pfAttribs.push_back(bitVec[0]);
+            pfAttribs.push_back(AGL_GREEN_SIZE);
+            pfAttribs.push_back(bitVec[1]);
+            pfAttribs.push_back(AGL_BLUE_SIZE);
+            pfAttribs.push_back(bitVec[2]);
+            pfAttribs.push_back(AGL_ALPHA_SIZE);
+            pfAttribs.push_back(bitVec[3]);
 #else
-# ifndef sgi
             pfAttribs.push_back(GLX_RED_SIZE);
             pfAttribs.push_back(bitVec[0]);
             pfAttribs.push_back(GLX_GREEN_SIZE);
@@ -1118,9 +1143,8 @@ void RenderTexture::_ParseModeString(const char *modeString,
             pfAttribs.push_back(bitVec[2]);
             pfAttribs.push_back(GLX_ALPHA_SIZE);
             pfAttribs.push_back(bitVec[3]);
-# endif
 #endif
-                       _iNumComponents = 4;
+           _iNumComponents = 4;
             continue;
         }
                else if (kv.first == "rgba") 
@@ -1139,6 +1163,9 @@ void RenderTexture::_ParseModeString(const char *modeString,
 #ifdef _WIN32
             pfAttribs.push_back(WGL_RED_BITS_ARB);
             pfAttribs.push_back(bitVec[0]);
+#elif defined( __APPLE__ )
+            pfAttribs.push_back(AGL_RED_SIZE);
+            pfAttribs.push_back(bitVec[0]);
 #else
             pfAttribs.push_back(GLX_RED_SIZE);
             pfAttribs.push_back(bitVec[0]);
@@ -1169,6 +1196,11 @@ void RenderTexture::_ParseModeString(const char *modeString,
             pfAttribs.push_back(bitVec[0]);
             pfAttribs.push_back(WGL_GREEN_BITS_ARB);
             pfAttribs.push_back(bitVec[1]);
+#elif defined( __APPLE__ )
+            pfAttribs.push_back(AGL_RED_SIZE);
+            pfAttribs.push_back(bitVec[0]);
+            pfAttribs.push_back(AGL_GREEN_SIZE);
+            pfAttribs.push_back(bitVec[1]);
 #else
             pfAttribs.push_back(GLX_RED_SIZE);
             pfAttribs.push_back(bitVec[0]);
@@ -1198,6 +1230,8 @@ void RenderTexture::_ParseModeString(const char *modeString,
             bHasStencil = true;
 #ifdef _WIN32
             pfAttribs.push_back(WGL_STENCIL_BITS_ARB);
+#elif defined( __APPLE__ )
+            pfAttribs.push_back(AGL_STENCIL_SIZE);
 #else
             pfAttribs.push_back(GLX_STENCIL_SIZE);
 #endif
@@ -1215,6 +1249,11 @@ void RenderTexture::_ParseModeString(const char *modeString,
             pfAttribs.push_back(1);
             pfAttribs.push_back(WGL_SAMPLES_ARB);
             pfAttribs.push_back(strtol(kv.second.c_str(), 0, 10));
+#elif defined( __APPLE__ )
+            pfAttribs.push_back(AGL_SAMPLE_BUFFERS_ARB);
+            pfAttribs.push_back(1);
+            pfAttribs.push_back(AGL_SAMPLES_ARB);
+            pfAttribs.push_back(strtol(kv.second.c_str(), 0, 10));
 #else
            pfAttribs.push_back(GL_SAMPLE_BUFFERS_ARB);
            pfAttribs.push_back(1);
@@ -1230,6 +1269,9 @@ void RenderTexture::_ParseModeString(const char *modeString,
 #ifdef _WIN32
             pfAttribs.push_back(WGL_DOUBLE_BUFFER_ARB);
             pfAttribs.push_back(true);
+#elif defined( __APPLE__ )
+            pfAttribs.push_back(AGL_DOUBLEBUFFER);
+            pfAttribs.push_back(True);
 #else
             pfAttribs.push_back(GL_DOUBLEBUFFER);
             pfAttribs.push_back(True);
@@ -1241,6 +1283,8 @@ void RenderTexture::_ParseModeString(const char *modeString,
         {
 #ifdef _WIN32
             pfAttribs.push_back(WGL_AUX_BUFFERS_ARB);
+#elif defined( __APPLE__ )
+            pfAttribs.push_back(AGL_AUX_BUFFERS);
 #else
             pfAttribs.push_back(GL_AUX_BUFFERS);
 #endif
@@ -1348,6 +1392,8 @@ void RenderTexture::_ParseModeString(const char *modeString,
 
 #ifdef _WIN32
     pfAttribs.push_back(WGL_DEPTH_BITS_ARB);
+#elif defined( __APPLE__ )
+    pfAttribs.push_back(AGL_DEPTH_SIZE);
 #else
     pfAttribs.push_back(GLX_DEPTH_SIZE);
 #endif
@@ -1358,6 +1404,9 @@ void RenderTexture::_ParseModeString(const char *modeString,
 #ifdef _WIN32
         pfAttribs.push_back(WGL_STENCIL_BITS_ARB);
         pfAttribs.push_back(0);
+#elif defined( __APPLE__ )
+        pfAttribs.push_back(AGL_STENCIL_SIZE);
+        pfAttribs.push_back(0);
 #else
         pfAttribs.push_back(GLX_STENCIL_SIZE);
         pfAttribs.push_back(0);
@@ -1413,7 +1462,7 @@ void RenderTexture::_ParseModeString(const char *modeString,
 
 #elif defined(DEBUG) || defined(_DEBUG)
         printf("RenderTexture Error: Render to Texture not "
-               "supported in Linux\n");
+               "supported in Linux or MacOS\n");
 #endif  
     }
 
@@ -1434,6 +1483,7 @@ void RenderTexture::_ParseModeString(const char *modeString,
             pfAttribs.push_back(WGL_PIXEL_TYPE_ARB);
             pfAttribs.push_back(WGL_TYPE_RGBA_FLOAT_ATI);
         }
+#elif defined( __APPLE__ )
 #else
         if (GL_NV_float_buffer)
         {
@@ -1547,7 +1597,7 @@ void RenderTexture::_ParseModeString(const char *modeString,
 #elif defined(DEBUG) || defined(_DEBUG)
         fprintf(stderr, 
                 "RenderTexture Error: Render to Texture not supported in "
-                "Linux\n");
+                "Linux or MacOS\ n");
 #endif  
     }
         
@@ -1572,7 +1622,7 @@ void RenderTexture::_ParseModeString(const char *modeString,
         }
 #elif defined(DEBUG) || defined(_DEBUG)
         printf("RenderTexture Error: Render to Texture not supported in "
-               "Linux\n");
+               "Linux or MacOS\n");
 #endif 
     }
 }
@@ -1648,12 +1698,24 @@ bool RenderTexture::_VerifyExtensions()
     if ( !fctPtrInited )
     {
         fctPtrInited = true;
-        if ( SGIsOpenGLExtensionSupported("WGL_ARB_pixel_format" ) )
+        wglGetExtensionsStringARBProc wglGetExtensionsStringARBPtr = (wglGetExtensionsStringARBProc)wglGetProcAddress( "wglGetExtensionsStringARB" );
+        if ( wglGetExtensionsStringARBPtr == 0 )
+        {
+            PrintExtensionError("WGL_ARB_extensions_string");
+            return false;
+        }
+        string wglExtensionsString = wglGetExtensionsStringARBPtr( wglGetCurrentDC() );
+        if ( SGSearchExtensionsString( wglExtensionsString.c_str(), "WGL_ARB_pixel_format" ) )
         {
             wglChoosePixelFormatARBPtr = (wglChoosePixelFormatARBProc)SGLookupFunction("wglChoosePixelFormatARB");
             wglGetPixelFormatAttribivARBPtr = (wglGetPixelFormatAttribivARBProc)SGLookupFunction("wglGetPixelFormatAttribivARB");
         }
-        if ( SGIsOpenGLExtensionSupported("WGL_ARB_pbuffer" ) )
+        else
+        {
+            PrintExtensionError("WGL_ARB_pixel_format");
+            return false;
+        }
+        if ( SGSearchExtensionsString( wglExtensionsString.c_str(), "WGL_ARB_pbuffer" ) )
         {
             wglCreatePbufferARBPtr = (wglCreatePbufferARBProc)SGLookupFunction("wglCreatePbufferARB");
             wglGetPbufferDCARBPtr = (wglGetPbufferDCARBProc)SGLookupFunction("wglGetPbufferDCARB");
@@ -1661,57 +1723,53 @@ bool RenderTexture::_VerifyExtensions()
             wglReleasePbufferDCARBPtr = (wglReleasePbufferDCARBProc)SGLookupFunction("wglReleasePbufferDCARB");
             wglDestroyPbufferARBPtr = (wglDestroyPbufferARBProc)SGLookupFunction("wglDestroyPbufferARB");
         }
-        if ( SGIsOpenGLExtensionSupported("WGL_ARB_render_texture" ) )
+        else
+        {
+            PrintExtensionError("WGL_ARB_pbuffer");
+            return false;
+        }
+        if ( SGSearchExtensionsString( wglExtensionsString.c_str(), "WGL_ARB_render_texture" ) )
         {
             wglBindTexImageARBPtr = (wglBindTexImageARBProc)SGLookupFunction("wglBindTexImageARB");
             wglReleaseTexImageARBPtr = (wglReleaseTexImageARBProc)SGLookupFunction("wglReleaseTexImageARB");
         }
-    }
-    if (!WGL_ARB_pbuffer)
-    {
-        PrintExtensionError("WGL_ARB_pbuffer");
-        return false;
-    }
-    if (!WGL_ARB_pixel_format)
-    {
-        PrintExtensionError("WGL_ARB_pixel_format");
-        return false;
-    }
-    if (_bIsTexture && !WGL_ARB_render_texture)
-    {
-        PrintExtensionError("WGL_ARB_render_texture");
-        return false;
-    }
-    if (_bRectangle && !GL_NV_texture_rectangle)
-    {
-        PrintExtensionError("GL_NV_texture_rectangle");
-        return false;
-    }
-    if (_bFloat && !(GL_NV_float_buffer || WGL_ATI_pixel_format_float))
-    {
-        PrintExtensionError("GL_NV_float_buffer or GL_ATI_pixel_format_float");
-        return false;
-    
-    }
-    if (_bFloat && _bIsTexture && !(GL_NV_float_buffer || GL_ATI_texture_float))
-    {
-        PrintExtensionError("NV_float_buffer or ATI_texture_float");
-    }
-    if (_bIsDepthTexture && !GL_ARB_depth_texture)
-    {
-        // [Redge]
+        else if ( _bIsTexture )
+        {
+            PrintExtensionError("WGL_ARB_render_texture");
+            return false;
+        }
+        if (_bRectangle && !SGIsOpenGLExtensionSupported( "GL_NV_texture_rectangle" ))
+        {
+            PrintExtensionError("GL_NV_texture_rectangle");
+            return false;
+        }
+        if (_bFloat && !(SGIsOpenGLExtensionSupported( "GL_NV_float_buffer" ) || SGSearchExtensionsString( wglExtensionsString.c_str(), "WGL_ATI_pixel_format_float" )))
+        {
+            PrintExtensionError("GL_NV_float_buffer or GL_ATI_pixel_format_float");
+            return false;
+        
+        }
+        if (_bFloat && _bIsTexture && !(SGIsOpenGLExtensionSupported( "GL_NV_float_buffer" ) || SGIsOpenGLExtensionSupported( "GL_ATI_texture_float" )))
+        {
+            PrintExtensionError("NV_float_buffer or ATI_texture_float");
+        }
+        if (_bIsDepthTexture && !SGIsOpenGLExtensionSupported( "GL_ARB_depth_texture" ))
+        {
+            // [Redge]
 #if defined(_DEBUG) | defined(DEBUG)
-        fprintf(stderr, 
-                "RenderTexture Warning: "
-                "OpenGL extension GL_ARB_depth_texture not available.\n"
-                "         Using glReadPixels() to emulate behavior.\n");
+            fprintf(stderr, 
+                    "RenderTexture Warning: "
+                    "OpenGL extension GL_ARB_depth_texture not available.\n"
+                    "         Using glReadPixels() to emulate behavior.\n");
 #endif   
-        _bHasARBDepthTexture = false;
-        //PrintExtensionError("GL_ARB_depth_texture");
-        //return false;
-        // [/Redge]
+            _bHasARBDepthTexture = false;
+            //PrintExtensionError("GL_ARB_depth_texture");
+            //return false;
+            // [/Redge]
+        }
+        SetLastError(0);
     }
-    SetLastError(0);
+#elif defined( __APPLE__ )
 #else
     if (!GLX_SGIX_pbuffer)
     {
@@ -2034,6 +2092,7 @@ bool RenderTexture::_MakeCurrent()
         _wglGetLastError();
         return false;
     }
+#elif defined( __APPLE__ )
 #else
     if (false == glXMakeCurrent(_pDisplay, _hPBuffer, _hGLContext)) 
     {
@@ -2085,6 +2144,7 @@ RenderTexture::RenderTexture(int width, int height,
     _hPBuffer(NULL),
     _hPreviousDC(0),
     _hPreviousContext(0),
+#elif defined( __APPLE__ )
 #else
     _pDisplay(NULL),
     _hGLContext(NULL),
@@ -2201,6 +2261,7 @@ bool RenderTexture::Initialize(bool         bShare       /* = true */,
     
     _pbufferAttribs.push_back(WGL_PBUFFER_LARGEST_ARB);
     _pbufferAttribs.push_back(true);
+#elif defined( __APPLE__ )
 #else
     _pixelFormatAttribs.push_back(GLX_RENDER_TYPE_SGIX);
     _pixelFormatAttribs.push_back(GLX_RGBA_BIT_SGIX);