体积渲染alpha混合

Volume Rendering alpha blending

本文关键字:alpha 混合      更新时间:2023-10-16

我找到了这个教程http://www.codeproject.com/Articles/352270/Getting-started-with-Volume-Rendering关于体渲染,并试图实现我自己的渲染器。在alpha测试中,我可以一次画出所有的切片,这或多或少给了我类似教程中的结果。现在,我想向前移动一步并应用alpha混合,但之后没有任何变化(即使有,它看起来也不像教程中那么漂亮)。

下面是我的渲染代码:

glClear(GL_COLOR_BUFFER_BIT  | GL_DEPTH_BUFFER_BIT);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_TEXTURE_2D);
for (int i = 0; i < z; ++i)
{
    glBindTexture(GL_TEXTURE_2D, texturesObjects[i]);
    glBegin(GL_QUADS);
    glTexCoord2f(0.0f, 0.0f);
    glVertex3f(-1.0, -1.0, 2 * i / z - 1);
    glTexCoord2f(1.0f, 0.0f);
    glVertex3f(1.0, -1.0, 2 * i / z - 1);
    glTexCoord2f(1.0f, 1.0f);
    glVertex3f(1.0, 1.0, 2 * i / z - 1);
    glTexCoord2f(0.0f, 1.0f);
    glVertex3f(-1.0, 1.0, 2 * i / z - 1);
    glEnd();
    glBindTexture(GL_TEXTURE_2D, 0);
}
glutSwapBuffers();

,其中z为切片数。纹理生成代码如下:

GLuint* loadXZslices(const VR::DataAccessor& accessor, int& x, int& y, int& z)
{
    x = accessor.getX();
    y = accessor.getY();
    z = accessor.getZ();
    GLuint* texturesObjects = new GLuint[accessor.getZ()];
    for (size_t i = 0; i < accessor.getZ(); ++i)
    {
        char* luminanceBuffer = accessor.getXYslice(i);
        char* rgbaBuffer = new char[accessor.getX() * accessor.getY() * 4];
        for (size_t j = 0; j < accessor.getX() * accessor.getY(); ++j)
        {
            rgbaBuffer[j * 4] = luminanceBuffer[j];
            rgbaBuffer[j * 4 + 1] = luminanceBuffer[j];
            rgbaBuffer[j * 4 + 2] = luminanceBuffer[j];
            rgbaBuffer[j * 4 + 3] = 255;
            if (luminanceBuffer[j] < 20)
            {
                rgbaBuffer[j * 4 + 3] = 0;
            }
        }
        glActiveTexture(GL_TEXTURE0);
        glGenTextures(1, &texturesObjects[i]);
        glBindTexture(GL_TEXTURE_2D, texturesObjects[i]);
        glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, accessor.getX(), accessor.getY(), 0, GL_RGBA, GL_UNSIGNED_BYTE, (GLvoid*)rgbaBuffer);
        delete[] luminanceBuffer;
        delete[] rgbaBuffer;
    }
    return texturesObjects;
}

DataAccessor当然是允许访问原始纹理数据的类。

我忘记在我的窗口设置东西还是我的代码有问题?

您的代码中有几个问题:

  1. 你没有设置你的纹理有部分透明度。在你构建纹理的地方,你将alpha组件设置为255,如果原始亮度小于20,则设置为0。为了得到实际的混合值,你需要使用alpha分量的亮度值:

    rgbaBuffer[j * 4 + 3] = luminanceBuffer[j];
    

    要微调结果,您可能必须使用从原始强度到alpha的其他映射。但它可能仍然是某种斜坡,而不是阶跃函数。

  2. 虽然代码没有显示z的声明,但这段代码看起来很可疑:

    glVertex3f(-1.0, -1.0, 2 * i / z - 1);
    

    从上下文中看,z可能是一个整数。如果这是真的,这将是一个整数除法,当2 * i小于z时,它将产生0,剩余的循环值为1。

  3. 必须将至少一个值转换为浮点数才能使其成为浮点除法。