Following is part of the code. There is no problem when compiling, but it crashes when executed. And it breaks in the line of glTexImage3D. Qt version 4.5.3, and class "opengl" is inherited from QGLWidget.
void opengl::initializeGL()
{
GLenum err = glewInit();
create_volumetexture();
}
void opengl::create_volumetexture()
{
int w = 256, h = 256, d = 225;
size_t size = w * h * d;
if (dataRGBA)
{
delete dataRGBA;
dataRGBA=NULL;
}
dataRGBA=new GLubyte[4*size];
for (int i=0; i<size; i++)
{
dataRGBA[4*i]=200;
dataRGBA[4*i+1]=0;
dataRGBA[4*i+2]=0;
dataRGBA[4*i+3]=100;
}
glGenTextures(1, &volume_texture);
// bind 3D texture target
glBindTexture(GL_TEXTURE_3D, volume_texture);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
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_BORDER);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_BORDER);
glPixelStorei(GL_UNPACK_ALIGNMENT,1);
glTexImage3D(GL_TEXTURE_3D_EXT, 0, GL_RGBA, w, h, d, 1, /*GL_LUMINANCE*/GL_RGBA, GL_UNSIGNED_BYTE,dataRGBA);
}
You gave a non-zero value to the border parameter, but the buffer allocated for that doesn't account for it, so glTexImage3D does a buffer reading overrun.
Also d is not a power of two so there you got another problem. You can use glTexImage3D with a null pointer for data to initialize the texture and glTexSubImage3D to fill it with the actual content – the data passed to glTexSubImage may also be in non-power of 2 format (but the texture itself must be initialized with power of 2 dimensions… and borders then, too).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With