fix texture bug
This commit is contained in:
parent
6f3eb16ab5
commit
2d3add1b36
|
@ -24,42 +24,54 @@
|
||||||
|
|
||||||
#include <prerequisites.h>
|
#include <prerequisites.h>
|
||||||
#include <graphics/texture.h>
|
#include <graphics/texture.h>
|
||||||
|
#include "graphics.h"
|
||||||
|
|
||||||
Texture::Texture(int width, int height, int channels, const uchar *pixels)
|
Texture::Texture(int width, int height, int channels, uchar *pixels)
|
||||||
{
|
{
|
||||||
// generate opengl texture
|
// generate opengl texture
|
||||||
m_textureId = internalLoadGLTexture(pixels, channels, width, height);
|
m_textureId = internalLoadGLTexture(pixels, channels, width, height);
|
||||||
}
|
}
|
||||||
|
|
||||||
uint Texture::internalLoadGLTexture(const uchar *pixels, int channels, int width, int height)
|
uint Texture::internalLoadGLTexture(uchar *pixels, int channels, int width, int height)
|
||||||
{
|
{
|
||||||
|
GLint texSize;
|
||||||
|
glGetIntegerv(GL_MAX_TEXTURE_SIZE, &texSize);
|
||||||
|
if(width > texSize || height > texSize) {
|
||||||
|
flogError("loading texture with size %dx%d failed, the maximum size is %dx%d", width % height % texSize % texSize);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
// generate gl texture
|
// generate gl texture
|
||||||
GLuint id;
|
GLuint id;
|
||||||
glGenTextures(1, &id);
|
glGenTextures(1, &id);
|
||||||
glBindTexture(GL_TEXTURE_2D, 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_size.setSize(width, height);
|
||||||
m_glSize.setSize(glWidth, glHeight);
|
bool mustFree = false;
|
||||||
|
|
||||||
uchar *out = NULL;
|
if(!g_graphics.isExtensionSupported("GL_ARB_texture_non_power_of_two") && pixels) {
|
||||||
if(m_size != m_glSize) {
|
int glWidth = 1;
|
||||||
out = new uchar[glHeight*glWidth*channels];
|
while(glWidth < width)
|
||||||
memset(out, 0, glHeight*glWidth*channels);
|
glWidth = glWidth << 1;
|
||||||
if(pixels)
|
|
||||||
for(int y=0;y<height;++y)
|
int glHeight = 1;
|
||||||
for(int x=0;x<width;++x)
|
while(glHeight < height)
|
||||||
for(int i=0;i<channels;++i)
|
glHeight = glHeight << 1;
|
||||||
out[y*glWidth*channels+x*channels+i] = pixels[y*width*channels+x*channels+i];
|
|
||||||
}
|
if(m_size != m_glSize) {
|
||||||
|
uchar *tmp = new uchar[glHeight*glWidth*channels];
|
||||||
|
memset(tmp, 0, 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)
|
||||||
|
tmp[y*glWidth*channels+x*channels+i] = pixels[y*width*channels+x*channels+i];
|
||||||
|
pixels = tmp;
|
||||||
|
mustFree = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
m_glSize.setSize(glWidth, glHeight);
|
||||||
|
} else
|
||||||
|
m_glSize = m_size;
|
||||||
|
|
||||||
// detect pixels GL format
|
// detect pixels GL format
|
||||||
GLenum format = 0;
|
GLenum format = 0;
|
||||||
|
@ -79,7 +91,7 @@ uint Texture::internalLoadGLTexture(const uchar *pixels, int channels, int width
|
||||||
}
|
}
|
||||||
|
|
||||||
// load pixels into gl memory
|
// load pixels into gl memory
|
||||||
glTexImage2D(GL_TEXTURE_2D, 0, channels, glWidth, glHeight, 0, format, GL_UNSIGNED_BYTE, out != NULL ? out : pixels);
|
glTexImage2D(GL_TEXTURE_2D, 0, channels, m_glSize.width(), m_glSize.height(), 0, format, GL_UNSIGNED_BYTE, 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);
|
||||||
|
@ -89,9 +101,10 @@ uint Texture::internalLoadGLTexture(const uchar *pixels, int channels, int width
|
||||||
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
|
// free if needed
|
||||||
if(out)
|
if(mustFree)
|
||||||
delete[] out;
|
delete[] pixels;
|
||||||
|
|
||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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 channels, const uchar *pixels = NULL);
|
Texture(int width, int height, int channels, uchar *pixels = NULL);
|
||||||
virtual ~Texture();
|
virtual ~Texture();
|
||||||
|
|
||||||
/// Enable texture bilinear filter (smooth scaled textures)
|
/// Enable texture bilinear filter (smooth scaled textures)
|
||||||
|
@ -48,7 +48,7 @@ public:
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
Texture() { }
|
Texture() { }
|
||||||
uint internalLoadGLTexture(const uchar *pixels, int channels, int w, int h);
|
uint internalLoadGLTexture(uchar *pixels, int channels, int w, int h);
|
||||||
|
|
||||||
uint m_textureId;
|
uint m_textureId;
|
||||||
Size m_size;
|
Size m_size;
|
||||||
|
|
Loading…
Reference in New Issue