2012-08-03 72 views
0

我開始了一個基於microsoft提供的基本效果示例的項目。然後我附加了鍵輸入來移動相機。最後一步是從DXF文件加載測試3d數​​據。我自己寫了DXF導入,只是代表3d三角形的簡單3dface對象。我調整了基本效果示例中的代碼以讀取變體頂點數。但是我得到一個錯誤Normal0缺失 - 即使頂點聲明和頂點緩衝區與正確設置的頂點一致。我認爲問題出現在頂點聲明中。原始樣本只有12個三角形,我的第一個DXF樣本大約有一千個。 我認爲這個問題是在這裏:xna 4頂點緩衝區和用於變化三角形計數的頂點聲明

private void CreateVertexBuffer() 
{ 
vertexDeclaration = new VertexDeclaration(new VertexElement[] 
    { 
     new VertexElement(0, VertexElementFormat.Vector3, VertexElementUsage.Position, 0), 
     new VertexElement(12, VertexElementFormat.Vector3, VertexElementUsage.Normal, 0), 
     new VertexElement(24, VertexElementFormat.Vector2, VertexElementUsage.TextureCoordinate, 0) 
    } 
); 
vertexBuffer = new VertexBuffer(
    graphics.GraphicsDevice, 
    vertexDeclaration, 
    number_of_vertices, 
    BufferUsage.None 
    ); 
Vertices = new VertexPositionNormalTexture[number_of_vertices]; 
InitializeVerts(); 
vertexBuffer.SetData<VertexPositionNormalTexture>(Vertices); 
graphics.GraphicsDevice.SetVertexBuffer(vertexBuffer); 

}

頂點元素12 ...頂點元素24 ......難道這些有問題的地方?

+0

我剛剛刪除了立方體示例中的聲明...這不是問題..代碼工作沒有dec ...像這樣..vertexBuffer = new VertexBuffer( graphics.GraphicsDevice, typeof(VertexPositionNormalTexture), number_of_vertices, BufferUsage.None ); – zeppeldep 2012-08-03 07:54:28

+0

...但我仍然得到Normal0缺少錯誤在變種頂點項目...我設置每個頂點的法線爲0,1,0 – zeppeldep 2012-08-03 07:56:41

回答

0

如果您要聲明自定義頂點類型,那麼您將無法使用VertexNormalTexture類作爲頂點類型的數據。您需要創建一個新結構來保存來自IVertexType類的數據。在結構中,通過方法使頂點聲明可用。

然後,使用新結構作爲Type字段的參數構造緩衝區。

請參閱this MSDN article

+0

:(我已經嘗試過,它只適用於如果你不包括光在BassicEffect - 我需要這些不是自定義頂點,它們是VertexPositionNormalTexture頂點。 Vertices [i4] = new VertexPositionNormalTexture(v1, – zeppeldep 2012-08-03 09:09:24

+0

但是,如果您打算使用'VertexPositionNormalTexture'爲什麼要打造自己的'VertexDeclaration' ?如果您需要頂點聲明中的信息,您可以使用'VertexPositionNormalTexture.VertexDeclaration'作爲靜態成員來訪問該類的頂點聲明。 – Dervall 2012-08-03 09:14:40