2012-08-03 140 views
0

最後我顯示了一些東西。切換到使用graphics.GraphicsDevice.DrawIndexedPrimitives ...下一個問題...只顯示一個三角形。數據集大約有200個三角形。我格式化了進來的數據,以確保每三個連續的矢量形成一個三角形面。這些是不規則的三角形,形成不規則的形狀。我不完全理解頂點的索引。看起來每3個指數形成一個三角形。如果是這樣的話,則指數匹配的數據進來我這樣做:不規則三角形頂點索引

int i4 = -1; 
     indices = new int[xData1.Count]; 
     for (int i2 = 0; i2 < xData1.Count; i2++) 
     { 
      i4++; 
      cubeVertices[i4].Position = new Vector3((float)xData1[i2][0], (float)xData1[i2][1], (float)xData1[i2][2]); 
      cubeVertices[i4].Color = Color.LawnGreen; 
      indices[i4] = i4; 
     } 

使得指數相匹配的頂點進來..然後我用賴默斯正常的鈣,以提供法線..這也許是錯誤的他的例子是使用每個索引6個頂點(我認爲!),如下所示:

for (int i = 0; i < cubeVertices.Length; i++) 
      cubeVertices[i].Normal = new Vector3(0, 0, 0); 

     for (int i = 0; i < indices.Length/3; i++) 
     { 
      int index1 = indices[i * 3]; 
      int index2 = indices[i * 3 + 1]; 
      int index3 = indices[i * 3 + 2]; 

      Vector3 side1 = cubeVertices[index1].Position - cubeVertices[index3].Position; 
      Vector3 side2 = cubeVertices[index1].Position - cubeVertices[index2].Position; 
      Vector3 normal = Vector3.Cross(side1, side2); 

      cubeVertices[index1].Normal += normal; 
      cubeVertices[index2].Normal += normal; 
      cubeVertices[index3].Normal += normal; 
     } 

     for (int i = 0; i < cubeVertices.Length; i++) 
      cubeVertices[i].Normal.Normalize(); 

需要多少東西來修復這裏?我只看到1出幾百三角形 :( THX的耐心

public struct VertexPositionColorNormal 
    { 
     public Vector3 Position; 
     public Color Color; 
     public Vector3 Normal; 

     public readonly static VertexDeclaration VertexDeclaration = new VertexDeclaration 
     (
      new VertexElement(0, VertexElementFormat.Vector3, VertexElementUsage.Position, 0), 
      new VertexElement(sizeof(float) * 3, VertexElementFormat.Color, VertexElementUsage.Color, 0), 
      new VertexElement(sizeof(float) * 3 + 4, VertexElementFormat.Vector3, VertexElementUsage.Normal, 0) 
     ); 
    } 

的...

private void CopyToBuffers() 
    { 
     vertexBuffer = new VertexBuffer(graphics.GraphicsDevice, VertexPositionColorNormal.VertexDeclaration, 
      cubeVertices.Length, BufferUsage.WriteOnly); 
     vertexBuffer.SetData(cubeVertices); 

     myIndexBuffer = new IndexBuffer(graphics.GraphicsDevice, typeof(int), indices.Length, BufferUsage.WriteOnly); 
     myIndexBuffer.SetData(indices); 
    } 

....

foreach (EffectPass pass in basicEffect.CurrentTechnique.Passes) 
     { 
      basicEffect.World = world; 
      basicEffect.View = view; 
      basicEffect.Projection = proj; 

      pass.Apply(); 

      graphics.GraphicsDevice.Indices = myIndexBuffer; 
      graphics.GraphicsDevice.SetVertexBuffer(vertexBuffer); 
      graphics.GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, 
       cubeVertices.Length, 0, indices.Length/3); 
+0

...這不是剔除模式...只是檢查...仍在搜索 – zeppeldep 2012-08-03 11:53:48

+0

您需要顯示您對「DrawIndexedPrimitives」的調用以及如何設置索引和頂點緩衝區。 – Dervall 2012-08-03 11:58:27

+0

好吧...包括...對不起,是一個痛苦... thx幫助.. – zeppeldep 2012-08-03 12:10:04

回答

0

你正常計算是正確的,即使它是錯誤的,唯一會發生的事情是你的三角形會收到錯誤的閃電。

您正在使用與進入的頂點順序完全匹配的索引,這本身就是多餘的。如果切換到完全不設置索引,並使用DrawPrimitives而不是原始計數,那麼它們會產生變化嗎?

除此之外,你確定你給的數據是有效的嗎?頂點位置是否正確設置?

+0

thx堆棧...是的,我試過DrawPrimitives ..它產生了相同的結果(沒有錯誤信息)。頂點位置是正確的。我列出了顯示提供檢查位置的spritebtach文本。至少我現在有一個三角形,比4小時前更好。 – zeppeldep 2012-08-03 12:30:19

+0

我想我會嘗試MeshBuilder ...但是..也有紫外線 - 我不知道如何設置這些不規則三角形的紫外線 – zeppeldep 2012-08-03 12:37:03

+0

我上傳了項目樣本DXF在這裏:http://www.4shared。 com/zip/cGgGVIAt/KBC3DI08.html ...請看看也許你會看到我做錯了:( – zeppeldep 2012-08-03 15:51:55

相關問題