片段着色器中的体积渲染计算深度值

时间:2015-05-08 13:36:34

标签: opengl glsl rendering

我有一个由OpenGL和GLSL编写的体积渲染程序。现在我需要计算一个片段的深度值。以下代码是我的片段着色器程序

#version 120
 //
 some needed variable declaration
//
 float calculateDepthValue(float t, float entryPointsDepth, float exitPointsDepth)
    {
    // assign front value given in windows coordinates
    float zw_front = entryPointsDepth;
    // and convert it into eye coordinates
    float ze_front = 1.0 / ((zw_front - const_to_z_e_1)*const_to_z_e_2);

    // assign back value given in windows coordinates
    float zw_back = exitPointsDepth;
    // and convert it into eye coordinates
    float ze_back = 1.0 / ((zw_back - const_to_z_e_1)*const_to_z_e_2);

    // interpolate in eye coordinates
    float ze_current = ze_front + t*(ze_back - ze_front);

    // convert back to window coordinates
    float zw_current = (1.0 / ze_current)*const_to_z_w_1 + const_to_z_w_2;

    return zw_current;
}


float getDepthValue(float t, float tEnd, float entryPointsDepth, float exitPointsDepth)
{
    if (t >= 0.0)
        return calculateDepthValue(t / tEnd, entryPointsDepth, exitPointsDepth);
    else
        return 1.0;
}


vec4 compositeDVR(in vec4 curResult, in vec4 color,in float t, in float pvar,inout float tDepth)
{
    vec4 result = curResult;

    // apply opacity correction to accomodate for variable sampling intervals
    color.a = 1.0 - pow(1.0 - color.a, pvar);

    result.rgb = result.rgb + (1.0 - result.a) * color.a * color.rgb;
    result.a = result.a + (1.0 - result.a) * color.a;
    // save first hit ray parameter for depth value calculation

    if (tDepth < 0.0)
        tDepth = t;

    return result;
}

void main()
{
    vec2 p = gl_FragCoord.xy * screenDimRCP_;

    vec3 start = texture2D(frontTex, p).rgb;
    vec3 end = texture2D(texBack, p).rgb;

    float entryDepth = texture2D(entryPointsDepth_, p).z;
    float exitDepth = texture2D(exitPointsDepth_, p).z;
//
    some needed code
  //  

        lookup = texture1D(globalTex, sample);

        if (lookup.a > 0.0)
        {
            lookup.rgb = phongShading(N, texToPhysicalPos, position_, cPos, lookup.rgb, lookup.rgb, vec3(1.0, 1.0, 1.0));

            //compositeDVR
            result = compositeDVR(result, lookup, t, powAlp, tDepth);
        }


    gl_FragDepth = getDepthValue(tDepth, tEnd, entryDepth, exitDepth);
    gl_FragColor = result;
  }

在片段着色器中,某些功能来自Voreen的代码。我的程序的结果是gl_FragDepth都等于1,这是错误的。有人可以帮助我吗?

0 个答案:

没有答案