From 1a7dfdb719e8d58d258d4c20db3fc71c74259b7c Mon Sep 17 00:00:00 2001 From: kgv Date: Tue, 18 Dec 2012 16:32:37 +0400 Subject: [PATCH] 0023632: Add support for NPOT mipmap textures in TKOpenGl Use glGenerateMipmap instead of gluBuild2DMipmaps when available. --- src/OpenGl/OpenGl_Context.cxx | 3 ++- src/OpenGl/OpenGl_ExtFBO.hxx | 4 ++-- src/OpenGl/OpenGl_Texture.cxx | 43 ++++++++++++++++++++++++++++++----- 3 files changed, 41 insertions(+), 9 deletions(-) diff --git a/src/OpenGl/OpenGl_Context.cxx b/src/OpenGl/OpenGl_Context.cxx index 25fd6b0d2a..c26001a62f 100644 --- a/src/OpenGl/OpenGl_Context.cxx +++ b/src/OpenGl/OpenGl_Context.cxx @@ -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; diff --git a/src/OpenGl/OpenGl_ExtFBO.hxx b/src/OpenGl/OpenGl_ExtFBO.hxx index 0b7f44de1d..2ce00943db 100644 --- a/src/OpenGl/OpenGl_ExtFBO.hxx +++ b/src/OpenGl/OpenGl_ExtFBO.hxx @@ -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; }; diff --git a/src/OpenGl/OpenGl_Texture.cxx b/src/OpenGl/OpenGl_Texture.cxx index aa52cc85ff..08e7e101be 100644 --- a/src/OpenGl/OpenGl_Texture.cxx +++ b/src/OpenGl/OpenGl_Texture.cxx @@ -18,6 +18,7 @@ #include +#include #include #include #include @@ -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: {