3

Just a quick question on drawing quads. I'm currently using:

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

This draws my quads perfectly fine but the only way I can make it work is to use six (6) vertices for my quads (drawing them as two triangles). I'm just wondering if it is possible to feed the GPU with four (4) vertices and still retain my dynamic vertex buffer.

I know that I can do so with DrawUserIndexedPrimitives() but I want my buffer! ;)

Edit: Do I need, and if so where do I tell my GPU that I'm feeding it four vertices per two triangles? I'm currently storing my quads as nQuads * 6 vertices in a vertex buffer and my GPU uses every three vertices as a triangle. So just switching to four vertices means:

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

Which is not a good thing since triangle number two uses one vertex from the first quad, number three uses two from the second quad, and so on and so forth.

Edit 2: I'm sorry, I'm actually using a dynamic vertex buffer.

Posting some code on how I do my quads with six vertices:

        // ## 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, there are a bit more to it because I use a circular queue and a bit other stuff, but this would be enough for understandability.

Robert Kaufmann
  • 768
  • 1
  • 9
  • 21
  • I don't understand the part after the Edit. Why "nQuads * 6". What do You mean by times 6? And what's not a good thing? – Dave O. Jul 07 '10 at 16:17
  • If I want to draw 10 quads, that would mean 60 vertices since I've 6 vertices per quad. Technically my 60 vertices is 20 triangles. Therefor I can imagine my quad consisting of six vertices when it is actually two triangles consisting of three vertices. If i suddenly only use four vertices instead of six, a quad will consist of 4/3 triangles thus overlapping each other. – Robert Kaufmann Jul 07 '10 at 16:29
  • Are Your quads connected to each other or do You want to draw a single quad? – Dave O. Jul 07 '10 at 16:35
  • They are not connected visually, but they are part of the same buffer, yes. As I understand it, the GPU is taking that buffer and uses vertex 1-3, 4-6, 7-9 etc. to draw triangles. I edited in some code if that would clear things up :\ – Robert Kaufmann Jul 07 '10 at 16:47

1 Answers1

2

Yes that's possible. You'll need to use TriangleStrip as the PrimitiveType. In Your VertexBuffer, You define Your vertices in the following order:

1---3
|\  |
| \ |
|  \|
0---2
EDIT to adress Your modified question

I don't know, how Your code looks like, but in general the information about the primitve count is used in the following places:

  • Buffer creation: public VertexBuffer ( GraphicsDevice graphicsDevice, int sizeInBytes, BufferUsage usage ) //sizeInBytes depends on the primitive count.
  • Draw call: public void DrawPrimitives ( PrimitiveType primitiveType, int startVertex, int primitiveCount )

Note, the information about the size of Your vertex format is needed in a couple more places but shouldn't be a problem as the size shouldn't change.

Update

Thx for the clarification, now I know, what You want to achieve. The correct way to handle Your problem is to use a vertex buffer in conjunction with a index buffer. The vertex buffer holds the vertices, the index buffer holds the order in which to connect the vertices to form triangles. Imagine You want to draw the following quad on Your screen:

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

What You did earlier was this:

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

What You do now is this:

//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.*/

You see, You save 2 vertices per quad but have instead to use 6 indices which specify the order in which to build triangle from the vertices. So this method is only useful if Your vertices are big (cary many information like normals, texture coordinates etc) and are shared by many triangles.

Riemers.net has a really nice, short and easy to understand tutorial on vertex and index buffers.

Dave O.
  • 2,231
  • 3
  • 21
  • 25
  • Would that mean, just changing my current: - Primitive type. - Vertices from 6 to 4 (using your order) - Changing calculations based on nVertices. Because if so, I've got lines all over the place. As if my GPU does not know that I want 4 verts to be two triangles :/ – Robert Kaufmann Jul 07 '10 at 15:39
  • Which kind of PrimitiveType did You use before? I guess TriangleList? Then You would need to change the PrimitiveType and the Vertex definition(/creation). The only place that I know of where XNA wants to know the primitive count is at buffer creation and the draw call - what do You mean by all over? – Dave O. Jul 07 '10 at 16:02
  • Thank you so much for bearing with me! Exactly the answer I was looking for! My own custom vertex is pretty hefty so this is perfectly what I need for optimization. Thank you again! :) – Robert Kaufmann Jul 07 '10 at 17:32