XNA - using a dynamic vertex buffer and only four (4) vertices

Just a quick question about drawing quads. I am currently using:

GraphicsDevice.DrawPrimitives(PrimitiveType primitiveType,
                        int startVertex, int primitiveCount);

This makes my squares perfectly perfect, but the only way to make it work is to use six (6) vertices for my squares (drawing them like two triangles). I'm just wondering if it's possible to combine a GPU with four (4) vertices and keep my dynamic vertex buffer.

I know I can do this with DrawUserIndexedPrimitives (), but I need my buffer !;)

Edit: Do I need, and if so, where will I tell my GPU that I feed it four vertices into two triangles? I currently save my quadrants as nQuads * 6 vertices in the vertex buffer, and my GPU uses every three vertices as a triangle. So just going to four peaks means:

Quads: {v1,v2,v3,v4} {v5,v6,v7,v8} ...
Triangles: {v1,v2,v3} {v4,v5,v6} {v7,v8 ...}

This is not good, because triangle number two uses one vertex from the first ATV, number three uses two from the second quad, etc. etc.

Edit 2: Sorry, I actually use a dynamic vertex buffer.

Posting some code on how I execute my six-vertex quads:

        // ## CONSTRUCTION
        // Setting up buffer and vertex holder.
        particleVertexBuffer = new DynamicVertexBuffer(graphicsDevice,
            ParticleQuad.VerticesSizeInBytes * nMaxParticles,
            BufferUsage.WriteOnly);
        particleVertices = new ParticleVertex[nMaxParticles * 6];

        // ## ADD VERTICES
        particleVertices[i].Set();
        particleVertices[i+1].Set();
        particleVertices[i+2].Set();
        particleVertices[i+3].Set();
        particleVertices[i+4].Set();
        particleVertices[i+5].Set();


        // ## SET BUFFER
        particleVertexBuffer.SetData(particleVertices, 0, nMaxParticles * 6, SetDataOptions.NoOverwrite);
        graphicsDevice.Vertices[0].SetSource(particleVertexBuffer, 0, ParticleVertex.SizeInBytes);

        // ## DRAW
        graphicsDevice.DrawPrimitives(PrimitiveType.TriangleList,
                    FirstUsedParticle * 6, ((LastUsedParticle - FirstUsedParticle)+1)* 2);

Now this is a little more, because I use a circular queue and a little different, but that would be enough for understanding.

+3
1

, . TriangleStrip PrimitiveType. VertexBuffer :

1---3
|\  |
| \ |
|  \|
0---2
EDIT

, , primitve :

  • : public VertexBuffer ( GraphicsDevice graphicsDevice, int sizeInBytes, BufferUsage usage )//sizeInBytes .
  • : public void DrawPrimitives ( PrimitiveType primitiveType, int startVertex, int primitiveCount )

, size , , .

Update

, , . - . , , . , :

 (0,1)________(1,1)
     |        |
     |        |
     |________|
 (0,0)        (1,0)

:

Vector3 v0, v1, v2, v3;
v0 = new Vector3(0, 0, 0);
v1 = new Vector3(1, 0, 0);
v2 = new Vector3(0, 1, 0);
v3 = new Vector3(1, 1, 0);

List vertices = new List();
//first triangle
vertices.add(v0);
vertices.add(v2);
vertices.add(v1);

//second triangle
vertices.add(v2);
vertices.add(v1);
vertices.add(v3);

VertexBuffer vb = new VertexBuffer(...);
vb.setData(verticesToDraw);

//draw the scene using PrimitiveType.TriangleList and primitive count 2

:

//vertices stay the same as in the example above!
List vertices = new List();
vertices.add(v0);
vertices.add(v1);
vertices.add(v2);
vertices.add(v3);

int[] indices = new int[6];
//first triangle
indices[0] = 0;
indices[1] = 2; 
indices[2] = 1;

//second triangle
indices[3] = 2;
indices[4] = 1;
indices[5] = 3;

VertexBuffer vb = new VertexBuffer(...);
IndexBuffer ib = new IndexBuffer(...);

vb.setData(vertices);
ib.setData(indices);

/*draw using the DrawIndexedPrimitives() method rather than the
DrawPrimitives() method and use PrimitiveType.TriangleList and
primitive count 2.*/

, 2 , 6 , . , , (cary many information, , ..) .

Riemers.net , .

+2

Source: https://habr.com/ru/post/1753537/


All Articles