【问题标题】:Strange number error in HLSLHLSL中的奇怪数字错误
【发布时间】:2013-03-29 17:57:00
【问题描述】:

我的项目有一个非常非常奇怪的问题。我试图用我的顶点发送一个索引号,并在 HLSL 着色器中使用这个数字。

但是,当我将此数字设置为一个值(例如 1)时,着色器会从该数字获得非常宽的频谱 - 下降到负值,介于 0-1 和 1 以上。即使我给出无意义的数字, 比如 10000

(我使用 C# 和 SlimDX,这是使用像素着色器 2.0 制作的,我也尝试了 3.0。)

        int c = 0;

        int testValue = 10000;

        for (int i = 0; i < tris.Length; i++)
        {
            vertices[c].Position = tris[i].p0;
            vertices[c].Normal = tris[i].normal;
            vertices[c].Index = testValue;
            vertices[c++].Color = Color.LightBlue.ToArgb();

            vertices[c].Position = tris[i].p1;
            vertices[c].Normal = tris[i].normal;
            vertices[c].Index = testValue;
            vertices[c++].Color = Color.LightBlue.ToArgb();

            vertices[c].Position = tris[i].p2;
            vertices[c].Normal = tris[i].normal;
            vertices[c].Index = testValue;
            vertices[c++].Color = Color.LightBlue.ToArgb();
        }

这就是我创建顶点的方式。除了 Index 值之外,一切都按应有的方式工作。我得到正确的颜色,我得到正确的法线,我得到正确的位置。

这里是 HLSL 代码:

VertexToPixel IndexedRenderedVS( float4 inPos : POSITION, float4 inColor : COLOR0, float3 inNormal : NORMAL, int inIndex : TEXCOORD0)
{
VertexToPixel Output = (VertexToPixel)0;

float4x4 xWorldViewProjection = mul(xWorld, xViewProjection);

Output.Position = mul(inPos, xWorldViewProjection);

if (inIndex < 0)
    Output.Color = float4(1, 0, 0, 1);

 if (inIndex > 0 && inIndex < 1)
     Output.Color = float4(0, 1, 0, 1);

 if (inIndex > 1)
     Output.Color = float4(0, 0, 1, 1);

 //Output.Color = inColor;

return Output;    
}

 PixelToFrame IndexedRenderedPS(VertexToPixel PSIN)

 {
 PixelToFrame Output = (PixelToFrame)0;

 Output.Color = PSIN.Color;

 return Output;
 }

最后,我的 VertexFormat 结构体:

    internal Vector3 Position;
    internal int Color;
    internal Vector3 Normal;
    internal int Index;

    internal static VertexElement[] VertexElements = new VertexElement[]
    {
        new VertexElement(0, 0, DeclarationType.Float3, DeclarationMethod.Default, DeclarationUsage.Position, 0),
        new VertexElement(0, sizeof(float) * 3, DeclarationType.Color, DeclarationMethod.Default, DeclarationUsage.Color, 0),
        new VertexElement(0, sizeof(float) * 7, DeclarationType.Float3, DeclarationMethod.Default, DeclarationUsage.Normal, 0),
        new VertexElement(0, sizeof(float) * 10, DeclarationType.Float1, DeclarationMethod.Default, DeclarationUsage.TextureCoordinate, 0),
        VertexElement.VertexDeclarationEnd
    };

    internal static VertexFormat Format
    {
        get { return VertexFormat.Position | VertexFormat.Diffuse | VertexFormat.Normal | VertexFormat.Texture1; }
    }

使用此代码和一个疯狂的索引值(10000 - 我得到相同的图片,无论我给出什么值,0、1,甚至当我输入负数时。它只是不在乎我给出什么)。

我得到了这张照片:

有人知道我在哪里犯了错误吗?我根本找不到我放错了一些价值的地方。尝试了大量的顶点声明,更改了着色器中的所有内容 - 现在我正式没有想法了。

感谢任何帮助。谢谢你:)

【问题讨论】:

  • 你应该提供你的绘图代码,以及你如何创建你的输入布局
  • 您使用的是 DX9 或更高版本吗?如果更高,则可能是填充问题(例如stackoverflow.com/questions/14782061/…)。您的顶点声明中有一点错误,您的索引在着色器中输入为 float1 作为 int。

标签: c# hlsl slimdx


【解决方案1】:

在 DirectX 中,纹理坐标始终是浮点数,通常是 float2,但有时是 float3 或 float4(您可以指定 float1,但如果这样做,您实际上会在程序集中得到一个 float2,运行时会丢弃第二通道)。您在 CPU 端将其输入为 int,然后在顶点描述中输入为 float1,最后在着色器中输入为 int。我建议将所有这些都输入为 float2 开始。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-06
    • 2015-10-08
    相关资源
    最近更新 更多