2
\$\begingroup\$

I have been messing around with XNA for a long while now, but have never really done much more than just copy/pasting tutorials and editing those. Since a bit I have been trying to really start to learn XNA but encountered a problem (or challenge, I suppose) that I can't seem to find any information on anywhere (or its there but I just don't see it).

So my question is.. Is there any way to draw multiple (different) textures on a Vertex Buffer?

I make one large Vertex Buffer out of an array of cubes to make sure no cube-sides are drawn when they aren't visible as well as not having to call the drawing device a million times if I would want to draw a million cubes.

Example, having two cubes in a row would be:

 _ _
|_|_|

But obviously, the middle will never be seen (left cube's right side and right cube's left side) so they are not added to the Vertex Buffer.

So my question is, how do I get a different texture on both of these cubes even though they are part of the same Vertex Buffer?

A cube side is constructed like this, but will obviously have to have something in it that lets the buffer know this part will need this texture (instead of simply a color):

vertices.Add(new VertexPositionColor(new Vector3(x, y + 1, z + 1), Color.Purple));
vertices.Add(new VertexPositionColor(new Vector3(x, y, z + 1), Color.Purple));
vertices.Add(new VertexPositionColor(new Vector3(x, y, z), Color.Purple));
vertices.Add(new VertexPositionColor(new Vector3(x, y, z), Color.Purple));
vertices.Add(new VertexPositionColor(new Vector3(x, y + 1, z), Color.Purple));
vertices.Add(new VertexPositionColor(new Vector3(x, y + 1, z + 1), Color.Purple));

And the Vertex Buffer is filled like this:

if (vertices.Count > 0)
{
    chunkVertexBuffer = new VertexBuffer(device, VertexPositionColor.VertexDeclaration, vertices.Count, BufferUsage.WriteOnly);
    chunkVertexBuffer.SetData<VertexPositionColor>(vertices.ToArray());
}

Pretty standard stuff I believe, but I can't seem to work it out with textures as VertexPositionTexture will only let you add the texture when you assign the effect.. for the entire Vertex Buffer.

\$\endgroup\$

migrated from stackoverflow.com Jul 9 '13 at 17:54

This question came from our site for professional and enthusiast programmers.

3
\$\begingroup\$

While XNA does support multitexturing, what you're trying to accomplish could easily be accomplished with a single texture.

UV Coordinates - A Vertex has a Position in 3D space (X,Y,Z) . If it is part of a textured mesh, it also has a Position on the 2D texture (U,V). U,V are coordinates that go from (0,0) in the upper-left of the texture to (1,1) in the bottom right of the texture.

The Point of the VertexPositionTexture struct is that the TextureCoordinate is a UV-coordinate on the texture. For example, if you look at minecraft terrain packs, you'll see that all blocks are stored on the same texture. Then for each face each vertex will choose its UV coordinates based on which block texture you want to display. This is why you only need one texture.

\$\endgroup\$
  • \$\begingroup\$ Can't believe I haven't figured that one out on my own.. Thanks a lot! I am sure it would still have taken a day or two before I figured that one out on my own =P \$\endgroup\$ – Jokefish Jul 9 '13 at 19:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.