2012-08-03 142 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

相关问题