简体   繁体   中英

Creating a 2D vertex array in C# for use with XNA

We are currently being directed to draw a cube, using the following rough structure to store the vertices:

VertexPositionTexture[] vert;
vert = new VertexPositionTexture[24];

However, I thought it would be better organisation, and allow easier manipulation, if I broke the vertices up into a 2D array like this:

public class Square
{
    public VertexPositionTexture[] vert
}

Square[] cubeSide;
cubeSide = new Square[6];

however, I can instantiate the cubeSide, but not the verts inside each Square.

I tried creating a constructor inside the Square class, but realised that I have the option of new Square[] or new Square() , but not both. I could have one square with four vertices, or size squares with one vertex.

I have tried VertextPositionTexture[] vert = new VertexPositionTexture[4] in the Square class, itself, but this does not work either.

To add to my confusion, the last time we were taught XNA, the teachers drilled it into us that arrays had to be declared at the start, with the exact number of elements we wanted. Ie, you can not have VertextPositionTexture[] vert , and instead, should have VertextPositionTexture[4] vert . They were also quite adamant that an array, once set, could never have its capacity altered.

How should I go about having a 2D vertex array, in which I am collecting 24 vertices into groups of 4, to represent faces in a cube?

We are being directed to store each face separately, ie having 24 vertices is a set requirement.

VertexPositionTexture[4] Vertices; isn't valid C# code. Using VertexPositionTexture Vertices = new VertexPositionTexture[4]; in your Square class will work, but that only instantiates an array of references, not an object for each element. The below will get you started on constructing your vertices.

public class Square
{
    public VertexPositionTexture[] Vertices;

    public Square()
    {
        Vertices = new VertexPositionTexture[4];
    }
}
Square side = new Square[6];
for (int i = 0; i < 6; i++)
{
    side[i] = new Square();
}

side[0].Vertices[0] = new VertexPositionTexture(Vector3, Vector2);
....

Now you can define each of your vertices contained within individual Square objects.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM