关于 VertexBuffers 的初学者问题

发布于 2024-12-03 07:26:25 字数 1281 浏览 1 评论 0原文

这是我一生中第一次查看 XNA,我对我读到的示例感到困惑。它们包含我不明白的重复内容:

        protected override void LoadContent()
        {
            _verts1 = new VertexPositionTexture[6];
            _vertexBuffer1 = new VertexBuffer(
                GraphicsDevice,
                typeof(VertexPositionTexture),
                _verts1.Length,
                BufferUsage.None);
            _vertexBuffer1.SetData(_verts1);
        ...
        }

       protected override void Draw(GameTime gameTime)
       {
        ...
            GraphicsDevice.SetVertexBuffer(_vertexBuffer1);
            foreach (EffectPass pass in effect.CurrentTechnique.Passes)
            {
                pass.Apply();
                GraphicsDevice.DrawUserPrimitives<VertexPositionTexture>(
                    PrimitiveType.TriangleStrip,
                    _verts1,
                    0,
                    4);
            }
         ...
        }

我不明白为什么在绘图方法中同时使用 VertexBuffer 和 VertexPositionTexture。保留 VertexBuffer 还不够吗?

如果我删除对 GraphicsDevice.SetVertexBuffer 的调用并仅依赖于 GraphicsDevice.DrawUserPrimitives - 一切看起来都一样!那么:当我只使用 VertexPositionTexture[] 就可以相处时,拥有 VertexBuffer 有什么意义呢? :-)

(我确信这是一个要点 - 请帮我看看!:)

问题已解决:蹩脚的 XNA 书! 谢谢 @dowhilefor

I'm looking at XNA for the first time in my life, and I am puzzled by the examples I read. They contain a duplication I don't understand:

        protected override void LoadContent()
        {
            _verts1 = new VertexPositionTexture[6];
            _vertexBuffer1 = new VertexBuffer(
                GraphicsDevice,
                typeof(VertexPositionTexture),
                _verts1.Length,
                BufferUsage.None);
            _vertexBuffer1.SetData(_verts1);
        ...
        }

       protected override void Draw(GameTime gameTime)
       {
        ...
            GraphicsDevice.SetVertexBuffer(_vertexBuffer1);
            foreach (EffectPass pass in effect.CurrentTechnique.Passes)
            {
                pass.Apply();
                GraphicsDevice.DrawUserPrimitives<VertexPositionTexture>(
                    PrimitiveType.TriangleStrip,
                    _verts1,
                    0,
                    4);
            }
         ...
        }

I don't understand why BOTH a VertexBuffer and a VertexPositionTexture are used in the drawing method. Sholdn't it be enough to keep the VertexBuffer around?

If I remove the calls to GraphicsDevice.SetVertexBuffer and just relies on GraphicsDevice.DrawUserPrimitives - everything looks just the same! So: what's the point of having a VertexBuffer at all when I can get along just using a VertexPositionTexture[]? :-)

(I'm sure the IS a point - plaese help me see it!!! :)

Problem solved: crappy XNA book! Thanks @dowhilefor

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

顾北清歌寒 2024-12-10 07:26:25

首先,包含顶点的数组只能使用 DrawUserPrimitives 进行渲染,这要慢得多,因为您可以每帧更改数组,因此每次顶点都会被推送到显卡。顶点缓冲区是为您的顶点创建一个存储并将它们一次放在显卡上。这当然会使数据静态,但速度也会快得多。事实上,我猜想 DrawUserPrimitives 会自己为顶点数据创建一个 VertexBuffer。

所以你正在混合两种不同的东西。您创建了一个顶点缓冲区,设置了它,然后渲染了您的数组。查看 DrawPrimitives

First of all your array containing vertices can only be rendered with DrawUserPrimitives, which is much slower because you could change your array every frame thus your vertices are pushed to the graphic card every time. A Vertexbuffer is to create a storage for your vertices and put them on the graphic card once. This of course makes the data static but also it is much faster. In fact, i guess DrawUserPrimitives creates a VertexBuffer for your vertex data on its own.

So you are mixing two different things. You created a vertex buffer, set it but then rendered your array. Have a look at DrawPrimitives.

幸福丶如此 2024-12-10 07:26:25

VertexPositionTexture 顶点存储在主内存中,
顶点缓冲区将顶点存储在 GPU(图形)内存中。

当您必须绘制大量顶点时,将它们存储在 GPU 内存中会更有效。

VertexPositionTexture vertex are stored in main memory,
and the vertexbuffer store the vertex at gpu (graphics) memory.

When you have to draw a lot of vertex is more efficient to store them at gpu memory.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文