Create a cube with a 3d mesh, using a 2d array

时间:2015-10-30 23:41:43

标签: arrays opengl 3d mesh

I need to create a cube using a mesh which is using these properties: "A two-dimensional array containing the mesh vertices. Each entry of the array specifies the vertices of one row of the mesh. The arrays for all rows must have the same length. There must be at least two rows, and each row must have at least two vertices" If you think to a representation of a cube made of paper: you can see it as 2 meshes, the 3 orizontal square and the 3 vertical. But in this case I would have to use two meshes, while I need one. I'd like to do not have overlap, and I was not able to find a solution. The best option I've found consists in filling the array with 9 rows. Each rows contains two entry, and in this way it is creating the surface of the cube. I'm attaching few images to explain what it creates with the first rows, till when I reach the overlapping point: I guess this problem has no solution, but in case any of you have any idea I will be open to any proposal. (I'm sorry about the order that may not be accurate) var row0 = []; x, y, l x+t, y, l var row1=[] x, y, l+400 x+t, y, l+400 var row2=[] x, y+t, l+400 x+t, y+t, l+400 var row3=[] x, y+t, l x+t, y+t, l var row4=[] x, y, l x+t, y, l var row5=[] x+t, y, l x+t, y+t, l var row6=[] x+t, y, l+400 x+t, y+t, l+400 var row7=[] x, y+t, l x, y, l var row8=[] x, y+t, l+400 x, y, l+400 With "x,y" two random coordinates that are having a distance of 400."t" as a variable. While "l+40"0 indicates the distance in height from the "l". Source here

1 个答案:

答案 0 :(得分:1)

你完全没有想到这个问题。

让我们退一步:在3D空间中,每个位置由3个值指定;在笛卡尔坐标系中,通常称为X,Y和Z.

我们通常将这些标记为一行标量值:

x, y, z

一个立方体由6个面组成,每个面由4个点组成,每个点位置由3个面共享。假设一个立方体的中心位于原点,宽度为2,那么XY平面中的两个面将是

-1, -1, -1
 1, -1, -1
 1,  1, -1
-1,  1, -1
 1, -1,  1
-1, -1,  1
-1,  1,  1
 1,  1,  1
 …

还有4个面(两个在XZ中,两个在YZ中)来制作一个完整的立方体。对于你的问题,真正重要的是,如何写下这些值。查看我写的数字。你看到了什么?一个二维数组(一行中有3个数字),每个点由立方体的网格构成一行。这就是全部。

网格就是这样的顶点列表。不要把它想象成“用纸制成的可折叠网”。只是一个职位列表,您也可以在这里复制职位。您也可以只记下8个位置一次,然后使用第二个1D数组,该数组是如何从这些位置制作网格的列表。