将两个图像堆栈的非电源加载到 3D 纹理 OpenGL C++



我试图将.png图像文件的堆栈加载到glTexImage3D中以进行体积渲染。但是,我只是无法让它工作,因为它不断崩溃。顺便说一句,问题似乎与pData有关.

以下是供您参考的代码:

GLubyte * pData = new GLubyte[XDIM*YDIM*ZDIM];
ifstream volumeData;
getFileNameWithSpecificExtension(dir_path);
sort(begin(fileIndex), end(fileIndex));
for (int i = 0; i < 201; i ++)
{
    volumeData.open(FrontPart + to_string(fileIndex[i]) + ".png", ios::binary);
}
volumeData.read(reinterpret_cast<char*>(pData), XDIM*YDIM*ZDIM*sizeof(GLubyte));
volumeData.close();

glGenTextures(1, &textureID);
glBindTexture(GL_TEXTURE_3D, textureID);
// set the texture parameters
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
//set the mipmap levels (base and max)
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_BASE_LEVEL, 0);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAX_LEVEL, 4);
//allocate data with internal format and foramt as (GL_RED) 
glTexImage3D(GL_TEXTURE_3D, 0, GL_RED, XDIM, YDIM, ZDIM, 0, GL_RED, GL_UNSIGNED_BYTE, pData);
GL_CHECK_ERRORS
//generate mipmaps
glGenerateMipmap(GL_TEXTURE_3D);
//delete the volume data allocated on heap
delete[] pData;

Non-of-2 不是问题,因为 OpenGL-2.0 删除了该约束。要将每个图像切片读取到纹理中,请使用带有 NULL 指针的 glTexImage3D 来初始化纹理,然后在循环迭代文件时读取每个文件,对其进行解码并使用 glTexSubImage3D 将其加载到正确的切片中。也不要使用显式newdelete[]而是std::vector.

正确实现"foreach file"循环和"ImageFileReader"留给读者作为练习。我建议查看一个特定候选者的通用图像库(http://sourceforge.net/adobe/genimglib/home/Home/)。另请记住,将任何图像尺寸硬编码到代码中是一个非常糟糕的主意。

glGenTextures(1, &textureID);
glBindTexture(GL_TEXTURE_3D, textureID);
// set the texture parameters
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
//set the mipmap levels (base and max)
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_BASE_LEVEL, 0);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAX_LEVEL, 4);
//allocate data with internal format and foramt as (GL_RED) 
glTexImage3D(GL_TEXTURE_3D, 0, GL_RED, XDIM, YDIM, ZDIM, 0, GL_RED, GL_UNSIGNED_BYTE, NULL);
GL_CHECK_ERRORS
GLubyte * pData = new GLubyte[XDIM*YDIM*ZDIM];
std::vector<GLubyte> vData(XDIM*YDIM*ZDIM);
foreach file in filenames (...)
{
    ImageFileReader ifr(FrontPart + to_string(fileIndex[i]) + ".png")
    ifr.decode(vData);
    glTexSubImage3D(GL_TEXTURE_3D, 0,
        0 /* x offset */,
        0 /* y offset */,
        z,
        XDIM, YDIM, 1,
        GL_RED, GL_UNSIGNED_BYTE,
        &vData[0] );
}
//generate mipmaps
glGenerateMipmap(GL_TEXTURE_3D);

最新更新