使用GLSL在体积渲染中TransferFunc的功能是什么?

时间:2016-09-04 03:07:38

标签: opengl glsl volume-rendering

我在使用GLSL进行体积渲染时遇到问题。源代码可以在以下链接https://github.com/toolchainX/Volume_Rendering_Using_GLSL中找到。在名为raycasting.frag的片段着色器中,出现sampler1D TransferFunc,但我不知道TransferFunc的实际功能(用法或含义)。以下是raycasting.frag的详细信息。

具体代码是: colorSample = texture(TransferFunc,intensity);

#version 400
in vec3 EntryPoint;
in vec4 ExitPointCoord;

uniform sampler2D ExitPoints;
uniform sampler3D VolumeTex;
uniform sampler1D TransferFunc;  
uniform float     StepSize;
uniform vec2      ScreenSize;
layout (location = 0) out vec4 FragColor;

void main()
{
    // ExitPointCoord 的坐标是设备规范化坐标
    // 出现了和纹理坐标有关的问题。
    vec3 exitPoint = texture(ExitPoints, gl_FragCoord.st/ScreenSize).xyz;
    // that will actually give you clip-space coordinates rather than
    // normalised device coordinates, since you're not performing the perspective
    // division which happens during the rasterisation process (between the vertex
    // shader and fragment shader
    // vec2 exitFragCoord = (ExitPointCoord.xy / ExitPointCoord.w + 1.0)/2.0;
    // vec3 exitPoint  = texture(ExitPoints, exitFragCoord).xyz;
    if (EntryPoint == exitPoint)
        //background need no raycasting
        discard;
    vec3 dir = exitPoint - EntryPoint;
    float len = length(dir); // the length from front to back is calculated and used to terminate the ray
    vec3 deltaDir = normalize(dir) * StepSize;
    float deltaDirLen = length(deltaDir);
    vec3 voxelCoord = EntryPoint;
    vec4 colorAcum = vec4(0.0); // The dest color
    float alphaAcum = 0.0;                // The  dest alpha for blending
    /* 定义颜色查找的坐标 */
    float intensity;
    float lengthAcum = 0.0;
    vec4 colorSample; // The src color 
    float alphaSample; // The src alpha
    // backgroundColor
    vec4 bgColor = vec4(1.0, 1.0, 1.0, 0.0);

    for(int i = 0; i < 1600; i++){
        // 获得体数据中的标量值scaler value
        intensity =  texture(VolumeTex, voxelCoord).x;
        // 查找传输函数中映射后的值
        // 依赖性纹理读取  
        colorSample = texture(TransferFunc, intensity);
        // modulate the value of colorSample.a
        // front-to-back integration
        if (colorSample.a > 0.0) {
            // accomodate for variable sampling rates (base interval defined by mod_compositing.frag)
            colorSample.a = 1.0 - pow(1.0 - colorSample.a, StepSize*200.0f);
            colorAcum.rgb += (1.0 - colorAcum.a) * colorSample.rgb * colorSample.a;
            colorAcum.a += (1.0 - colorAcum.a) * colorSample.a;
        }
        voxelCoord += deltaDir;
        lengthAcum += deltaDirLen;
        if (lengthAcum >= len ){    
            colorAcum.rgb = colorAcum.rgb*colorAcum.a + (1 - colorAcum.a)*bgColor.rgb;      
            break;  // terminate if opacity > 1 or the ray is outside the volume    
        }else if (colorAcum.a > 1.0){
            colorAcum.a = 1.0;
            break;
        }
    }
    FragColor = colorAcum;
    // for test
    // FragColor = vec4(EntryPoint, 1.0);
    // FragColor = vec4(exitPoint, 1.0);

}

我希望你能帮我解决问题。谢谢!

1 个答案:

答案 0 :(得分:0)

传递函数确定3D体积数据集中的强度如何映射到颜色。

许多3D数据集(例如来自医学成像)每个体素包含单个值。例如,对于CT扫描,这将是X射线吸收量(至少这是我认为的......)。

渲染3D数据集时,您希望将不同的强度编码为不同的颜色。这就是传递函数的作用。如果使用1D纹理对传递函数进行编码,则此纹理将包含每种可能强度值的RGBA颜色。

这些传递函数可以是您想要使结果图像看起来很好/有用的任何东西。非常典型的传递函数具有以下形式:

  • 完全透明(alpha = 0.0)低于某个强度值。
  • 线性斜坡,alpha增加从0.0到1.0,高于该范围的值。为获得最佳视觉效果,通常包括使用3种不同颜色。例如,alpha = 0.0时为黑色,alpha = 0.5时为红色,alpha = 1.0时为白色,这些值之间插有颜色。
  • 完全不透明(alpha = 1.0)以上。

如果从DICOM文件中读取图像数据,它可以包含标签,告诉您应该用于传递函数的这些不同部分的值范围。

相关问题