【问题标题】:DrawUserIndexedPrimitives: Why do the indices have to be short instead of int?DrawUserIndexedPrimitives:为什么索引必须是short而不是int?
【发布时间】:2011-10-28 18:49:49
【问题描述】:

DrawUserIndexedPrimitive 有两种重载类型:Those accepting 16-bit indicesthose accepting 32-bit indices

使用 16 位索引时,一切正常。使用 32 位索引时,当调用 DrawUserIndexedPrimitive 时,我会收到带有“有用”错误文本 An unexpected error has occurred(无内部异常)的 InvalidOperationException。当我在项目属性中启用“非托管代码调试”时,我得到了这些行

DrawUserPrimitives.exe 中 0x75a5b9bc 处的第一次机会异常:Microsoft C++ 异常:内存位置 0x0032e6b4..
DrawUserPrimitives.exe 中 0x75a5b9bc 处的第一次机会异常:Microsoft C++ 异常:内存位置 0x0032e728..

在抛出异常之前的调试窗口中。

为什么会这样?由于两个重载都可用,我想它们都可以工作(或者如果只支持其中一个,至少会抛出一个有意义的异常)。这是一个完整的最小示例程序,可用于重现此问题。我正在使用 XNA 3.0。

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

public class GameTest : Microsoft.Xna.Framework.Game
{
    static void Main(string[] args)
    {
        using (var game = new GameTest())
        {
            game.Run();
        }
    }

    GraphicsDeviceManager manager;

    public GameTest() { manager = new GraphicsDeviceManager(this); }

    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.SteelBlue);
        GraphicsDevice.VertexDeclaration = new VertexDeclaration(GraphicsDevice, VertexPositionColor.VertexElements);

        var basicEffect = new BasicEffect(GraphicsDevice, null);
        basicEffect.VertexColorEnabled = true;
        basicEffect.View = Matrix.CreateLookAt(new Vector3(0.0f, 0.0f, 1.0f), Vector3.Zero, Vector3.Up);
        basicEffect.Projection = Matrix.CreateOrthographicOffCenter(-1, 2, 2, -1, 1.0f, 1000.0f);

        // two simple points
        var pointList = new VertexPositionColor[] {
            new VertexPositionColor(new Vector3(0,0,0), Color.White),
            new VertexPositionColor(new Vector3(0,1,0), Color.White)
        };

        // one simple line between those two points
        var lineListIndices = new short[] { 0, 1 };      // works fine
        // var lineListIndices = new int[] { 0, 1 };     // causes exception below

        basicEffect.Begin();
        foreach (EffectPass pass in basicEffect.CurrentTechnique.Passes)
        {
            pass.Begin();
            GraphicsDevice.DrawUserIndexedPrimitives<VertexPositionColor>(
                PrimitiveType.LineList, pointList, 0, 2, 
                lineListIndices, 0, 1);  // exception occurs here
            pass.End();
        }
        basicEffect.End();

        base.Draw(gameTime);
    }
}

【问题讨论】:

  • 你可能使用的是 64 位机器?
  • @Jethro:是的,但我正在将项目编译为 32 位。无论如何,在 C# 中,short总是 16 位,int总是 32 位。
  • 尝试将你的数组声明为 Int32[] lineListIndices = new Int32[] { 0, 1} 看看是否可行。我会感到惊讶,但由于您使用 var 声明变量,因此可能存在一些编译器混淆?
  • @Crappy:好吧,那将是编译器中的一个非常严重的错误。 ;-) 但是不,我试过了,但没有任何区别。
  • 是的,这是一个奇怪的 - 确实看起来你应该工作。如果您还没有,也可以尝试在 XNA 论坛上询问这个问题 - 也许其中一位框架开发人员会加入。

标签: c# xna xna-3.0


【解决方案1】:

神秘的错误消息似乎是 XNA 3.0 中的一个错误(感谢 Andrew 的提示)。在 XNA 4.0 中运行代码(稍作修改)会产生更有用的错误消息 (NotSupportedException):

XNA Framework Reach 配置文件不支持 32 位索引。使用 IndexElementSize.SixteenBits 或大小为两个字节的类型。

这是有道理的,因为我的显卡似乎不支持 HiDef 配置文件,该配置文件将支持 32 位索引。

【讨论】:

    猜你喜欢
    • 2011-06-14
    • 1970-01-01
    • 2019-08-12
    • 1970-01-01
    • 2015-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多