From e81e0f1c2417644d94f193bf0fbbfea5d9755afb Mon Sep 17 00:00:00 2001 From: Eduardo Bart Date: Mon, 16 May 2011 15:30:05 -0300 Subject: [PATCH] fix gl bug, textures sizes must be a power of 2 --- src/framework/graphics/animatedtexture.cpp | 36 ++------------ src/framework/graphics/animatedtexture.h | 2 +- src/framework/graphics/graphics.cpp | 2 +- src/framework/graphics/texture.cpp | 57 +++++++++++++++++++--- src/framework/graphics/texture.h | 6 ++- 5 files changed, 60 insertions(+), 43 deletions(-) diff --git a/src/framework/graphics/animatedtexture.cpp b/src/framework/graphics/animatedtexture.cpp index e1c677a8..0610a72b 100644 --- a/src/framework/graphics/animatedtexture.cpp +++ b/src/framework/graphics/animatedtexture.cpp @@ -26,7 +26,7 @@ #include #include -AnimatedTexture::AnimatedTexture(int width, int height, int components, int numFrames, uchar *framesPixels, int *framesDelay) : +AnimatedTexture::AnimatedTexture(int width, int height, int channels, int numFrames, uchar *framesPixels, int *framesDelay) : Texture(), m_numFrames(numFrames) { @@ -35,39 +35,11 @@ AnimatedTexture::AnimatedTexture(int width, int height, int components, int numF m_framesTextureId = new uint[numFrames]; m_framesDelay = new int[numFrames]; - GLenum format = 0; - switch(components) { - case 4: - format = GL_RGBA; - break; - case 3: - format = GL_RGB; - break; - case 2: - format = GL_LUMINANCE_ALPHA; - break; - case 1: - format = GL_LUMINANCE; - break; - } - - glGenTextures(numFrames, m_framesTextureId); - for(int i=0;igetSize(); + const Size& textureSize = texture->getGlSize(); float textureRight = 0.0f; float textureBottom = 1.0f; diff --git a/src/framework/graphics/texture.cpp b/src/framework/graphics/texture.cpp index 937db370..54bf9a99 100644 --- a/src/framework/graphics/texture.cpp +++ b/src/framework/graphics/texture.cpp @@ -25,16 +25,45 @@ #include #include -Texture::Texture(int width, int height, int components, uchar *pixels) +Texture::Texture(int width, int height, int channels, const uchar *pixels) { + // generate opengl texture + m_textureId = internalLoadGLTexture(pixels, channels, width, height); +} + +uint Texture::internalLoadGLTexture(const uchar *pixels, int channels, int width, int height) +{ + // generate gl texture + GLuint id; + glGenTextures(1, &id); + glBindTexture(GL_TEXTURE_2D, id); + + // convert texture size to power of 2 + int glWidth = 1; + while(glWidth < width) + glWidth = glWidth << 1; + + int glHeight = 1; + while(glHeight < height) + glHeight = glHeight << 1; + m_size.setSize(width, height); + m_glSize.setSize(glWidth, glHeight); - // generate opengl texture - glGenTextures(1, &m_textureId); - glBindTexture(GL_TEXTURE_2D, m_textureId); + uchar *out = NULL; + if(m_size != m_glSize) { + out = new uchar[glHeight*glWidth*channels]; + bzero(out, glHeight*glWidth*channels); + if(pixels) + for(int y=0;y { public: /// Create a texture, width and height must be a multiple of 2 - Texture(int width, int height, int components, uchar *pixels = NULL); + Texture(int width, int height, int channels, const uchar *pixels = NULL); virtual ~Texture(); /// Enable texture bilinear filter (smooth scaled textures) @@ -44,11 +44,15 @@ public: uchar *getPixels(); const Size& getSize() const { return m_size; } + const Size& getGlSize() const { return m_glSize; } protected: Texture() { } + uint internalLoadGLTexture(const uchar *pixels, int channels, int w, int h); + uint m_textureId; Size m_size; + Size m_glSize; }; typedef boost::shared_ptr TexturePtr;