1

我在使用 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);

}

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

4

1 回答 1

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 文件中读取的,它可以包含标签,告诉您应该用于传递函数的这些不同部分的值的范围。

于 2016-09-04T05:25:47.330 回答