使用光线投射的体积渲染 - 流过体积

时间:2016-04-05 20:40:24

标签: c# opentk raycasting volume-rendering

我正在处理我的卷渲染应用程序(C#+ OpenTK)。 使用光线投射渲染音量,我在这个网站上找到了很多灵感:  http://graphicsrunner.blogspot.sk/2009/01/volume-rendering-101.html,即使我的应用程序与OpenGL一起使用,使用3D纹理和其他东西的主要思想也是一样的。 应用程序工作正常,但在我“流入卷”(意味着在边界框内)之后,一切都消失了,我想阻止它。那么有一些简单的方法可以做到这一点吗? - >我将能够流过音量或移动音量。

以下是片段着色器的代码:

#version 330

in vec3 EntryPoint;
in vec4 ExitPointCoord;

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

void main()
{
//gl_FragCoord --> http://www.txutxi.com/?p=182
vec3 exitPoint = texture(ExitPoints, gl_FragCoord.st/ScreenSize).xyz;

//background need no raycasting
if (EntryPoint == exitPoint)
    discard;

vec3 rayDirection = normalize(exitPoint - EntryPoint);
vec4 currentPosition = vec4(EntryPoint, 0.0f);
vec4 colorSum = vec4(.0f,.0f,.0f,.0f);
vec4 color = vec4(0.0f,0.0f,0.0f,0.0f);
vec4 value = vec4(0.0f);

vec3 Step = rayDirection * StepSize;
float stepLength= length(Step);
float LengthSum = 0.0f;
float Length = length(exitPoint - EntryPoint);

for(int i=0; i < 16000; i++)
{
    currentPosition.w = 0.0f;
    value = texture(VolumeTex, currentPosition.xyz);
    color = texture(TransferFunc, value.a);

    //reduce the alpha to have a more transparent result
    color.a *= AlphaReduce;

    //Front to back blending
    color.rgb *= color.a;
    colorSum = (1.0f - colorSum.a) * color + colorSum;

    //accumulate length
    LengthSum += stepLength;

    //break from the loop when alpha gets high enough
    if(colorSum.a >= .95f)
        break;

    //advance the current position
    currentPosition.xyz += Step;

    //break if the ray is outside of the bounding box
    if(LengthSum >= Length)
        break;
}
FragColor = colorSum;
}

以下代码基于https://github.com/toolchainX/Volume_Rendering_Using_GLSL

Display()函数:

    public void Display()
    {
        // the color of the vertex in the back face is also the location
        // of the vertex
        // save the back face to the user defined framebuffer bound
        // with a 2D texture named `g_bfTexObj`
        // draw the front face of the box
        // in the rendering process, i.e. the ray marching process
        // loading the volume `g_volTexObj` as well as the `g_bfTexObj`
        // after vertex shader processing we got the color as well as the location of
        // the vertex (in the object coordinates, before transformation).
        // and the vertex assemblied into primitives before entering
        // fragment shader processing stage.
        // in fragment shader processing stage. we got `g_bfTexObj`
        // (correspond to 'VolumeTex' in glsl)and `g_volTexObj`(correspond to 'ExitPoints')
        // as well as the location of primitives.

        // draw the back face of the box
        GL.Enable(EnableCap.DepthTest);

        //"vykreslim" front || back face objemu do framebuffru --> teda do 2D textury s ID bfTexID 
        //(pomocou backface.frag &.vert)
        GL.BindFramebuffer(FramebufferTarget.Framebuffer, frameBufferID);
        GL.Viewport(0, 0, width, height);
        LinkShader(spMain.GetProgramHandle(), bfVertShader.GetShaderHandle(), bfFragShader.GetShaderHandle());
        spMain.UseProgram();
        //cull front face
        Render(CullFaceMode.Front);
        spMain.UseProgram(0);
        //klasicky framebuffer --> "obrazovka"
        GL.BindFramebuffer(FramebufferTarget.Framebuffer, 0);

        GL.Viewport(0, 0, width, height);
        LinkShader(spMain.GetProgramHandle(), rcVertShader.GetShaderHandle(), rcFragShader.GetShaderHandle());
        spMain.UseProgram();
        SetUniforms();
        Render(CullFaceMode.Back);
        spMain.UseProgram(0);

        GL.Disable(EnableCap.DepthTest);
    }

    private void DrawBox(CullFaceMode mode)
    {
        // --> Face culling allows non-visible triangles of closed surfaces to be culled before expensive Rasterization and Fragment Shader operations.
        GL.Enable(EnableCap.CullFace);
        GL.CullFace(mode);
        GL.BindVertexArray(VAO);
        GL.DrawElements(PrimitiveType.Triangles, 36, DrawElementsType.UnsignedInt, 0);
        GL.BindVertexArray(0);
        GL.Disable(EnableCap.CullFace);
        spMain.UseProgram(0);//zapnuty bol v Render() ktora DrawBox zavolala
    }

    private void Render(CullFaceMode mode)
    {
        GL.ClearColor(0.0f, 0.0f, 0.0f, 1.0f);
        GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
        spMain.UseProgram();
        spMain.SetUniform("modelViewMatrix", Current);
        spMain.SetUniform("projectionMatrix", projectionMatrix);
        DrawBox(mode);
    }

问题是(我认为)当我向音量移动时(我不移动相机,只是缩放音量),如果比例因子> 2.7something,我在音量中,它意味着“在最后一张照片被渲染的平面之后”,所以一个看不到任何东西。 我能想到的解决方案(也许)是这样的: 如果我达到比例因子= 2.7something:

1。) - &gt;不要缩放音量

2。) - &gt;不知何故告诉片段着色器将EntryPoint移向 RayDirection一段时间(可能基于比例因子)。

现在,我尝试了这种“方法”,似乎它可以工作:

vec3 entryPoint = EntryPoint + some_value * rayDirection;

some_value必须夹在[0,1 [interval(或[0,1]?)之间 ,但也许无关紧要感谢:

if (EntryPoint == exitPoint)
discard;

所以现在,也许(如果我的解决方案不是那么糟糕),我可以改变我的答案: 如何计算some_value(基于我发送给片段着色器的比例因子)?

if(scale_factor < 2.7something)
    work like before;
else
{
    compute some_value; //(I need help with this part)
    change entry point;
    work like before;
}

(我不是英语本地人,所以如果文中有一些重大错误你不明白,请告诉我,我会尝试修复这些错误)

感谢的。

2 个答案:

答案 0 :(得分:1)

我解决了我的问题。它并没有使“被音量所包围”的错觉,但现在,我可以流过音量而没有任何消失。 这是我的解决方案添加到片段着色器的代码:

<LinearLayout
                 android:background="#80FF80AB"
                 android:paddingLeft="10dp"
                 android:layout_weight="1"
                 android:orientation="vertical"
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content">

但是如果你知道或者可以考虑更好的解决方案,使“被音量所包围”效果,如果你让我知道,我会很高兴。

谢谢。

答案 1 :(得分:0)

如果理解正确,我认为您应该使用 Plane Clipping 浏览该卷。 (如果您附加此解决方案,我可以根据您的代码为您提供一个简单的示例。将整个C ++项目转换为C#太耗时。)