3

i have tried to load stack of .png image files into glTexImage3D for volume rendering purpose. However, i just cant make it work as it keeps crashing. BTW, the problem seems to be related with pData.

Below is the code for your reference:

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;
4

1 回答 1

4

由于 OpenGL-2.0 删除了该约束,因此非 2 的幂是没有问题的。要将每个图像切片读取到纹理中,请使用带有 NULL 指针的 glTexImage3D 来初始化纹理,然后在循环中遍历文件,读取每个文件,对其进行解码并使用 glTexSubImage3D 将其加载到正确的切片中。也不要使用明确的newanddelete[]而是使用std::vector.

正确实现“foreach 文件”循环和“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);
于 2015-03-11T09:15:11.683 回答