2013-03-29 60 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來啓動。