简体   繁体   English

在C#中创建2D顶点数组以用于XNA

[英]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: 但是,我认为如果将这些顶点分解成2D数组,则组织会更好,并且操作起来会更轻松:

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. 但是,我可以实例化cubeSide,但不能实例化每个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. 我尝试在Square类中创建一个构造函数,但意识到我可以选择new Square[]new Square() ,但不能同时选择两者。 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. 我已经在Square类本身中尝试过VertextPositionTexture[] vert = new VertexPositionTexture[4] ,但这也不起作用。

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. 令我感到困惑的是,上一次我们学习XNA时,老师向我们钻研了XNA,必须在开始时声明数组,并精确声明所需的元素数量。 Ie, you can not have VertextPositionTexture[] vert , and instead, should have VertextPositionTexture[4] vert . 即,您不能具有VertextPositionTexture[] vert ,而应该具有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? 我应该如何拥有一个2D顶点数组,在该数组中我将24个顶点收集为4个一组,以表示一个立方体中的面?

We are being directed to store each face separately, ie having 24 vertices is a set requirement. 我们指示分别存储每个面,即设置24个顶点。

VertexPositionTexture[4] Vertices; isn't valid C# code. 不是有效的C#代码。 Using VertexPositionTexture Vertices = new VertexPositionTexture[4]; 使用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. Square类中可以使用,但是只能实例化一个引用数组,而不是每个元素的对象。 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. 现在,您可以定义单个Square对象中包含的每个顶点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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