fix gl bug, textures sizes must be a power of 2
This commit is contained in:
parent
ef065cbb74
commit
e81e0f1c24
|
@ -26,7 +26,7 @@
|
||||||
#include <core/engine.h>
|
#include <core/engine.h>
|
||||||
#include <core/dispatcher.h>
|
#include <core/dispatcher.h>
|
||||||
|
|
||||||
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(),
|
Texture(),
|
||||||
m_numFrames(numFrames)
|
m_numFrames(numFrames)
|
||||||
{
|
{
|
||||||
|
@ -35,39 +35,11 @@ AnimatedTexture::AnimatedTexture(int width, int height, int components, int numF
|
||||||
m_framesTextureId = new uint[numFrames];
|
m_framesTextureId = new uint[numFrames];
|
||||||
m_framesDelay = new int[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;i<numFrames;++i) {
|
for(int i=0;i<numFrames;++i) {
|
||||||
glBindTexture(GL_TEXTURE_2D, m_framesTextureId[i]);
|
uchar *framePixels = framesPixels + (i*height*width* channels);
|
||||||
|
uint id = internalLoadGLTexture(framePixels, channels, width, height);
|
||||||
// load the pixels into opengl memory
|
|
||||||
uchar *framePixels = framesPixels + (i*height*width*components);
|
|
||||||
glTexImage2D(GL_TEXTURE_2D, 0, components, width, height, 0, format, GL_UNSIGNED_BYTE, framePixels);
|
|
||||||
|
|
||||||
// disable texture border
|
|
||||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
|
||||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
|
||||||
|
|
||||||
// nearest filtering
|
|
||||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
|
||||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
|
||||||
|
|
||||||
|
m_framesTextureId[i] = id;
|
||||||
m_framesDelay[i] = framesDelay[i];
|
m_framesDelay[i] = framesDelay[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -31,7 +31,7 @@
|
||||||
class AnimatedTexture : public Texture
|
class AnimatedTexture : public Texture
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
AnimatedTexture(int width, int height, int components, int numFrames, uchar *framesPixels, int *framesDelay);
|
AnimatedTexture(int width, int height, int channels, int numFrames, uchar *framesPixels, int *framesDelay);
|
||||||
virtual ~AnimatedTexture();
|
virtual ~AnimatedTexture();
|
||||||
|
|
||||||
void enableBilinearFilter();
|
void enableBilinearFilter();
|
||||||
|
|
|
@ -140,7 +140,7 @@ void Graphics::drawTexturedRect(const Rect& screenCoords, const TexturePtr& text
|
||||||
int bottom = screenCoords.bottom() + 1;
|
int bottom = screenCoords.bottom() + 1;
|
||||||
int top = screenCoords.top();
|
int top = screenCoords.top();
|
||||||
int left = screenCoords.left();
|
int left = screenCoords.left();
|
||||||
const Size& textureSize = texture->getSize();
|
const Size& textureSize = texture->getGlSize();
|
||||||
|
|
||||||
float textureRight = 0.0f;
|
float textureRight = 0.0f;
|
||||||
float textureBottom = 1.0f;
|
float textureBottom = 1.0f;
|
||||||
|
|
|
@ -25,16 +25,45 @@
|
||||||
#include <prerequisites.h>
|
#include <prerequisites.h>
|
||||||
#include <graphics/texture.h>
|
#include <graphics/texture.h>
|
||||||
|
|
||||||
Texture::Texture(int width, int height, int components, uchar *pixels)
|
Texture::Texture(int width, int height, int channels, const uchar *pixels)
|
||||||
{
|
{
|
||||||
m_size.setSize(width, height);
|
|
||||||
|
|
||||||
// generate opengl texture
|
// generate opengl texture
|
||||||
glGenTextures(1, &m_textureId);
|
m_textureId = internalLoadGLTexture(pixels, channels, width, height);
|
||||||
glBindTexture(GL_TEXTURE_2D, m_textureId);
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
|
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<height;++y)
|
||||||
|
for(int x=0;x<width;++x)
|
||||||
|
for(int i=0;i<channels;++i)
|
||||||
|
out[y*glWidth*channels+x*channels+i] = pixels[y*width*channels+x*channels+i];
|
||||||
|
}
|
||||||
|
|
||||||
|
// detect pixels GL format
|
||||||
GLenum format = 0;
|
GLenum format = 0;
|
||||||
switch(components) {
|
switch(channels) {
|
||||||
case 4:
|
case 4:
|
||||||
format = GL_RGBA;
|
format = GL_RGBA;
|
||||||
break;
|
break;
|
||||||
|
@ -49,8 +78,8 @@ Texture::Texture(int width, int height, int components, uchar *pixels)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
// load the pixels into opengl memory
|
// load pixels into gl memory
|
||||||
glTexImage2D(GL_TEXTURE_2D, 0, components, width, height, 0, format, GL_UNSIGNED_BYTE, pixels);
|
glTexImage2D(GL_TEXTURE_2D, 0, channels, glWidth, glHeight, 0, format, GL_UNSIGNED_BYTE, out != NULL ? out : pixels);
|
||||||
|
|
||||||
// disable texture border
|
// disable texture border
|
||||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
||||||
|
@ -59,6 +88,11 @@ Texture::Texture(int width, int height, int components, uchar *pixels)
|
||||||
// nearest filtering
|
// nearest filtering
|
||||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
||||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
||||||
|
|
||||||
|
// free
|
||||||
|
if(out)
|
||||||
|
delete[] out;
|
||||||
|
return id;
|
||||||
}
|
}
|
||||||
|
|
||||||
Texture::~Texture()
|
Texture::~Texture()
|
||||||
|
@ -77,9 +111,16 @@ void Texture::enableBilinearFilter()
|
||||||
uchar *Texture::getPixels()
|
uchar *Texture::getPixels()
|
||||||
{
|
{
|
||||||
// copy pixels from opengl memory
|
// copy pixels from opengl memory
|
||||||
uchar *pixels = new uchar[m_size.area()*4];
|
uchar *pixels = new uchar[m_glSize.area()*4];
|
||||||
glBindTexture(GL_TEXTURE_2D, m_textureId);
|
glBindTexture(GL_TEXTURE_2D, m_textureId);
|
||||||
glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
|
glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
|
||||||
|
|
||||||
|
// convert pixels to the real texture size
|
||||||
|
if(m_size != m_glSize)
|
||||||
|
for(int y=0;y<m_size.height();++y)
|
||||||
|
for(int x=0;x<m_size.width();++x)
|
||||||
|
for(int i=0;i<4;++i)
|
||||||
|
pixels[y*m_size.width()*4+x*4+i] = pixels[y*m_glSize.width()*4+x*4+i];
|
||||||
return pixels;
|
return pixels;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -31,7 +31,7 @@ class Texture : public boost::enable_shared_from_this<Texture>
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
/// Create a texture, width and height must be a multiple of 2
|
/// 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();
|
virtual ~Texture();
|
||||||
|
|
||||||
/// Enable texture bilinear filter (smooth scaled textures)
|
/// Enable texture bilinear filter (smooth scaled textures)
|
||||||
|
@ -44,11 +44,15 @@ public:
|
||||||
uchar *getPixels();
|
uchar *getPixels();
|
||||||
|
|
||||||
const Size& getSize() const { return m_size; }
|
const Size& getSize() const { return m_size; }
|
||||||
|
const Size& getGlSize() const { return m_glSize; }
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
Texture() { }
|
Texture() { }
|
||||||
|
uint internalLoadGLTexture(const uchar *pixels, int channels, int w, int h);
|
||||||
|
|
||||||
uint m_textureId;
|
uint m_textureId;
|
||||||
Size m_size;
|
Size m_size;
|
||||||
|
Size m_glSize;
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef boost::shared_ptr<Texture> TexturePtr;
|
typedef boost::shared_ptr<Texture> TexturePtr;
|
||||||
|
|
Loading…
Reference in New Issue