views:

76

answers:

1

Hiya!

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.

A: 

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[0] = 2; 
indices[0] = 1;

//second triangle
indices[0] = 2;
indices[0] = 1;
indices[0] = 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
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 :/
Robelirobban
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
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! :)
Robelirobban