c# - DrawUserIndexedPrimitives : Why do the indices have to be short instead of int?

标签 c# xna xna-3.0

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

使用 16 位索引时,一切正常。使用 32 位索引时,当 DrawUserIndexedPrimitive 被调用。当我在项目属性中启用“非托管代码调试”时,我得到了这些行

First-chance exception at 0x75a5b9bc in DrawUserPrimitives.exe: Microsoft C++ exception: long at memory location 0x0032e6b4..
First-chance exception at 0x75a5b9bc in DrawUserPrimitives.exe: Microsoft C++ exception: long at memory location 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);
    }
}

最佳答案

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

XNA Framework Reach profile does not support 32 bit indices. Use IndexElementSize.SixteenBits or a type that has a size of two bytes.

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

关于c# - DrawUserIndexedPrimitives : Why do the indices have to be short instead of int?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7090859/

相关文章:

XNA 3.0 中的 Xna 4.0 项目

c# - 字典中的 ToString c#

c# - 来自另一个项目的公共(public)静态类 "is inaccessible due to its protection level"

c# - 使用kinect使播放器剪切图像更平滑,特殊抗锯齿

c# - 为什么索引超出范围?

xna - 如何开始在 Zune 上进行游戏编程

c# - 在调试 session 之间保留 app.config 中的数据

c# - 将实时共享托管 ASP.NET MVC 站点迁移到 Windows Azure

c# - 为什么发生异常时不从 Mvc 设置 StatusCode