XNA Library DrawPrimitives抛出primitiveCount错误

时间:2014-07-01 11:33:29

标签: silverlight xna

已经在gamedev上发布了这个但是没有太多回复,所以我想尝试在这里发帖。

我从这里http://silverlight.bayprince.com/tutorials.php?tutorial=13下载了项目,并尝试添加一个新的OBJ文件,其中列出了几千个顶点。

经典的茶壶可以工作但是当我尝试加载包含更多顶点的不同模型时,它会在调用DrawPrimitives时突然抛出这个primitiveCount错误。

代码包含一个将从OBJ文件中读取数据的类。然后,这将返回一个VertexBuffer对象回到主程序。调用Draw事件时绘图开始。

从OBJ文件创建了417936个顶点,因为我使用三角形列表,所以我将顶点除以3以得到总基元数。

这里是绘制事件的代码:

private void DrawingSurface_Draw(object sender, DrawEventArgs e)
        {
            GraphicsDevice device = GraphicsDeviceManager.Current.GraphicsDevice;

            device.Clear(ClearOptions.Target | ClearOptions.DepthBuffer, new Microsoft.Xna.Framework.Color(0, 0, 0, 0), 10.0f, 0);

            device.RasterizerState = new RasterizerState()
            {
                CullMode = CullMode.None
            };

            device.SetVertexBuffer(_vertexBuffer);

            foreach (EffectPass pass in _effect.CurrentTechnique.Passes)
            {

                    pass.Apply();
                    device.SamplerStates[0] = SamplerState.LinearClamp;
                    device.DrawPrimitives(PrimitiveType.TriangleList, 0, _vertexBuffer.VertexCount / 3);
            }

            // set camera
            _effect.World = Matrix.Identity;
            _effect.View = Matrix.CreateLookAt(new Vector3(_x, _y, _z), Vector3.Zero, Vector3.Up);
            _effect.Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, 2.0f, 1.0f, 100.0f);

            // move camera along a circumference
            _x = (float)(_radius * Math.Sin(_hAngle * (Math.PI / 180)));
            _z = (float)(_radius * Math.Cos(_hAngle * (Math.PI / 180)));

            e.InvalidateSurface();
        }

然后在DrawPrimitives上发生错误。

关于此的任何线索?

1 个答案:

答案 0 :(得分:0)

我认为DrawPrimitives在65k时具有原始或顶点限制......

更改代码以绘制基元块...

      var start_vertex = 0;
      var part_count = 3 * 20000;
      var total_count = _vertexBuffer.VertexCount;
      while (total_count>0)
      {
          var count = Math.Min(total_count, part_count);
          device.DrawPrimitives(PrimitiveType.TriangleList, start_vertex, count / 3); 
          total_count -= count;
          start_vertex += count;
      } 

您可以使用“part_count”大小来查找限制。