0023632: Add support for NPOT mipmap textures in TKOpenGl
authorkgv <kgv@opencascade.com>
Tue, 18 Dec 2012 12:32:37 +0000 (16:32 +0400)
committerkgv <kgv@opencascade.com>
Fri, 28 Dec 2012 14:24:40 +0000 (18:24 +0400)
Use glGenerateMipmap instead of gluBuild2DMipmaps when available.

src/OpenGl/OpenGl_Context.cxx
src/OpenGl/OpenGl_ExtFBO.hxx
src/OpenGl/OpenGl_Texture.cxx

index 25fd6b0..c26001a 100644 (file)
@@ -575,7 +575,8 @@ void OpenGl_Context::init()
      || !FindProcShort (extFBO, glDeleteRenderbuffersEXT)
      || !FindProcShort (extFBO, glBindRenderbufferEXT)
      || !FindProcShort (extFBO, glRenderbufferStorageEXT)
-     || !FindProcShort (extFBO, glFramebufferRenderbufferEXT))
+     || !FindProcShort (extFBO, glFramebufferRenderbufferEXT)
+     || !FindProcShort (extFBO, glGenerateMipmapEXT))
     {
       delete extFBO;
       extFBO = NULL;
index 0b7f44d..2ce0094 100644 (file)
@@ -1,6 +1,6 @@
 // Created on: 2012-01-26
 // Created by: Kirill GAVRILOV
-// Copyright (c) 2012-2012 OPEN CASCADE SAS
+// Copyright (c) 2012 OPEN CASCADE SAS
 //
 // The content of this file is subject to the Open CASCADE Technology Public
 // License Version 6.5 (the "License"). You may not use the content of this file
@@ -17,7 +17,6 @@
 // purpose or non-infringement. Please see the License for the specific terms
 // and conditions governing the rights and limitations under the License.
 
-
 #ifndef _OpenGl_ExtFBO_H__
 #define _OpenGl_ExtFBO_H__
 
@@ -37,6 +36,7 @@ struct OpenGl_ExtFBO
   PFNGLBINDRENDERBUFFEREXTPROC        glBindRenderbufferEXT;
   PFNGLRENDERBUFFERSTORAGEEXTPROC     glRenderbufferStorageEXT;
   PFNGLFRAMEBUFFERRENDERBUFFEREXTPROC glFramebufferRenderbufferEXT;
+  PFNGLGENERATEMIPMAPEXTPROC          glGenerateMipmapEXT;
 
 };
 
index aa52cc8..08e7e10 100644 (file)
@@ -18,6 +18,7 @@
 
 #include <OpenGl_Texture.hxx>
 
+#include <OpenGl_ExtFBO.hxx>
 #include <OpenGl_Context.hxx>
 #include <Graphic3d_TextureParams.hxx>
 #include <Standard_Assert.hxx>
@@ -357,7 +358,6 @@ bool OpenGl_Texture::Init (const Handle(OpenGl_Context)& theCtx,
       if (aWidth != aWidthOut || aHeight != aHeightOut)
       {
         // scale texture
-
         if (!aCopy.InitTrash (theImage.Format(), Standard_Size(aWidthOut), Standard_Size(aHeightOut))
           || gluScaleImage (aPixelFormat,
                             aWidth,    aHeight,    aDataType, theImage.Data(),
@@ -398,11 +398,42 @@ bool OpenGl_Texture::Init (const Handle(OpenGl_Context)& theCtx,
       glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
       glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
 
-      bool isCreated = gluBuild2DMipmaps (GL_TEXTURE_2D, aTextureFormat,
-                                          aWidth, aHeight,
-                                          aPixelFormat, aDataType, theImage.Data()) == 0;
-      Unbind (theCtx);
-      return isCreated;
+      if (theCtx->extFBO != NULL
+       && aWidth == aWidthOut && aHeight == aHeightOut)
+      {
+        // use proxy to check texture could be created or not
+        glTexImage2D (GL_PROXY_TEXTURE_2D, 0, aTextureFormat,
+                      aWidthOut, aHeightOut, 0,
+                      aPixelFormat, aDataType, NULL);
+        glGetTexLevelParameteriv (GL_PROXY_TEXTURE_2D, 0, GL_TEXTURE_WIDTH,  &aTestWidth);
+        glGetTexLevelParameteriv (GL_PROXY_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &aTestHeight);
+        if (aTestWidth == 0 || aTestHeight == 0)
+        {
+          // no memory or broken input parameters
+          Unbind (theCtx);
+          return false;
+        }
+
+        // upload main picture
+        glTexImage2D (GL_TEXTURE_2D, 0, aTextureFormat,
+                      aWidthOut, aHeightOut, 0,
+                      aPixelFormat, aDataType, theImage.Data());
+
+        // generate mipmaps
+        //glHint (GL_GENERATE_MIPMAP_HINT, GL_NICEST);
+        theCtx->extFBO->glGenerateMipmapEXT (GL_TEXTURE_2D);
+
+        Unbind (theCtx);
+        return true;
+      }
+      else
+      {
+        bool isCreated = gluBuild2DMipmaps (GL_TEXTURE_2D, aTextureFormat,
+                                            aWidth, aHeight,
+                                            aPixelFormat, aDataType, theImage.Data()) == 0;
+        Unbind (theCtx);
+        return isCreated;
+      }
     }
     default:
     {