2013-03-29 33 views
1

我的项目有一个非常非常奇怪的问题。我试图用我的顶点发送索引号,并在HLSL着色器中使用此数字。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(); 
     } 

这是我创造我的顶点。除了索引值以外,一切都可以正常工作。我知道颜色是正确的,我得到的是正常的,我的位置正确。

下面是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,甚至当我把负数放进去时,它只是不在乎我给予什么)。

我得到这样的画面: Asteroid

任何人有任何想法,我犯了一个错误?我只是无法找到我错位的地方。尝试了大量的顶点声明,改变了着色器内的所有内容 - 现在我正在从想法中跑出来。

任何帮助表示赞赏。谢谢:)

+0

您应该提供您的绘图代码以及如何创建输入布局 – MHGameWork

+0

您是否使用DX9或更高版本?如果更高,也许它的填充问题(如http://stackoverflow.com/questions/14782061/d3d10-constant-buffer-not-working/14784040#14784040)。在你的顶点声明中有一个小错误,你的索引在你的着色器中被作为float1键入为int。 – Gnietschow

回答

1

在DirectX中,纹理坐标总是浮点数,通常是float2,但有时候是float3或float4(你可以指定一个float1,但是如果你确实会在程序集中得到一个float2,运行时将丢弃第二个通道)。你在CPU端输入它作为int,然后在顶点描述中作为float1输入,最后在着色器中作为int输入。我建议将所有这些输入为float2来启动。