As I know, OpenGL has highly supported NPOT textures since 3.0. Now I’ m facing an bizarre phenomenon that NPOT texture cannot be allocated with precise corresponding data. Words are plain. My testing code is as below.
glGenTextures(1, &texVoxels);
glBindTexture(GL_TEXTURE_3D, texVoxels);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
int width = 513;
int height = 513;
int depth = 513;
unsigned char *data = new unsigned char[width*height*depth];
memset(data, 0, sizeof(unsigned char)*width*height*depth);
glTexImage3D(GL_TEXTURE_3D, 0, GL_LUMINANCE8, width, height,depth, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, data);
delete []data;
data = NULL;
As code shown above, I need to allocate an NPOT cubic texture of 2^N +1. But program will exit error on glTexImage3D(). The error, I think, is caused by “the size of data less than the texture needed”. Because if I allocate data larger enough, the program will pass.
How could I use NPOT texture and allocate data like this? Even though when the size of data is larger enough the program passes, I am really not sure about how data exactly corresponds to the texture location then.
PS. My graphic card is quadro fx 4800, which supports OpenGL 3.3.
You probably need to set pixel storage parameters. Most importantly the row alignment. Try adding a
before calling
glTexImage3D.