使用HLSL几何着色器对二十面体进行多次细分

时间:2011-10-17 15:13:50

标签: directx hlsl directx-10 geometry-shader

目前,我通过使用以下几何着色器对一个二十面体进行细分:

[maxvertexcount(8)]
void gs(triangle VS_OUT gin[3], inout TriangleStream<GS_OUT> s)
{
    //        p1  
    //       /  \
    //      /    \
    //     m0 --- m1
    //    / \    / \
    //   /   \  /   \
    //  p0 -- m2 -- p2

    float3 m0 = .5f * (gin[0].pos_l + gin[1].pos_l),
           m1 = .5f * (gin[2].pos_l + gin[1].pos_l),
           m2 = .5f * (gin[0].pos_l + gin[2].pos_l);

    float3 v[6];
    v[0] = gin[0].pos_l;
    v[1] = m0;
    v[2] = m2;
    v[3] = m1;
    v[4] = gin[2].pos_l;
    v[5] = gin[1].pos_l;

    GS_OUT gout;
    for (int i = 0; i < 5; ++i)
    {
        gout.pos_h = mul(float4(v[i], 1.f), g_mat_wvp);
        s.Append(gout);
    }
    s.RestartStrip();

    gout.pos_h = mul(float4(v[1], 1.f), g_mat_wvp);
    s.Append(gout);

    gout.pos_h = mul(float4(v[5], 1.f), g_mat_wvp);
    s.Append(gout);

    gout.pos_h = mul(float4(v[3], 1.f), g_mat_wvp);
    s.Append(gout);
}

但是,我想指定细分级别。是否有可能使用输出的顶点再次调用Shader或者我是否需要遵循其他方法?

1 个答案:

答案 0 :(得分:1)

我已经通过以下方式解决了这个问题:

[maxvertexcount(128)]
void gs(triangle VS_OUT gin[3], inout TriangleStream<GS_OUT> s)
{
    if (g_subdivs != 0 && g_subdivs <= MAX_SUBDIVISION_LEVEL)
    {
        uint level_count = (uint)pow(2, g_subdivs);
        float3 u = (gin[1].pos_l - gin[0].pos_l) / level_count,
               w = (gin[2].pos_l - gin[0].pos_l) / level_count,
               v = gin[0].pos_l;

        for (int level = level_count; level > 0; --level)
        {            
            GS_OUT gout;
            gout.pos_h = mul(float4(normalize(v), 1.f), g_mat_wvp);
            s.Append(gout);

            float3 p = v;
            uint   vertex_count = level + 1;

            for (uint vertex = 1; vertex < vertex_count; ++vertex)
            {
                gout.pos_h = mul(float4(normalize(p + u), 1.f), g_mat_wvp);
                s.Append(gout);

                gout.pos_h = mul(float4(normalize(p += w), 1.f), g_mat_wvp);
                s.Append(gout);
            }

            v += u;
            s.RestartStrip();
        }
    }
    else
    {
        [unroll]
        for (uint i = 0; i < 3; ++i)
        {
            GS_OUT gout;
            gout.diff  = gin[i].diff;
            gout.pos_h = mul(float4(gin[i].pos_l, 1.f), g_mat_wvp);

            s.Append(gout);
        }
    }
}