开发者

Beginners question about VertexBuffers

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


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜