我目前在大脑中有一个关于如何正确地逐行初始化GLFloats 数组的结。 据我所知,数组可以这样写:
int a[2][2] = {{1,2},{3,4}}
在我的情况下,我有数组
GLfloat targetVertices[10][72];
表示立方体六边的顶点(十个立方体,确切地说)。立方体坐标存储在
中float targetX[10], targetY[10], targetZ[10];
阵列。我想像这样初始化它:
for(iii = 0; iii <= 9; iii++) {
targetVertices[iii] = { /*Side 1*/
targetX[iii]-1, targetY[iii]-1, targetZ[iii]-1,
targetX[iii]-1, targetY[iii]-1, targetZ[iii]+1,
targetX[iii]-1, targetY[iii]+1, targetZ[iii]+1,
targetX[iii]-1, targetY[iii]+1, targetZ[iii]-1,
/*Side 2*/
targetX[iii]+1, targetY[iii]-1, targetZ[iii]-1,
targetX[iii]+1, targetY[iii]-1, targetZ[iii]+1,
targetX[iii]+1, targetY[iii]+1, targetZ[iii]+1,
targetX[iii]+1, targetY[iii]+1, targetZ[iii]-1,
/*Side 3*/
targetX[iii]-1, targetY[iii]-1, targetZ[iii]-1,
targetX[iii]-1, targetY[iii]-1, targetZ[iii]+1,
targetX[iii]+1, targetY[iii]-1, targetZ[iii]+1,
targetX[iii]+1, targetY[iii]-1, targetZ[iii]-1,
/*Side 4*/
targetX[iii]-1, targetY[iii]+1, targetZ[iii]-1,
targetX[iii]-1, targetY[iii]+1, targetZ[iii]+1,
targetX[iii]+1, targetY[iii]+1, targetZ[iii]+1,
targetX[iii]+1, targetY[iii]+1, targetZ[iii]-1,
/*Side 5*/
targetX[iii]-1, targetY[iii]-1, targetZ[iii]-1,
targetX[iii]-1, targetY[iii]+1, targetZ[iii]-1,
targetX[iii]+1, targetY[iii]+1, targetZ[iii]-1,
targetX[iii]+1, targetY[iii]-1, targetZ[iii]-1,
/*Side 6*/
targetX[iii]-1, targetY[iii]-1, targetZ[iii]+1,
targetX[iii]-1, targetY[iii]+1, targetZ[iii]+1,
targetX[iii]+1, targetY[iii]+1, targetZ[iii]+1,
targetX[iii]+1, targetY[iii]-1, targetZ[iii]+1
};
}
不幸的是,我总是被告知有关第一个波浪形支架(C2059和C2143,但那些对我没有任何意义)的语法错误。 我希望我的问题很明确,有人可以帮助我。我在这个或任何其他网站上找不到与我的问题类似的东西。
提前致谢!
答案 0 :(得分:1)
你的语法问题是说targetVertices [iii]你引用的是数组的iiith索引,而不是整个列。 2D数组存储为1D数组,但提供[] []以进行简单访问。您可以将它们称为单维或双维。
这是一种更可重用的方法。
void addVertex(std::vector<GLfloat> &data, GLfloat x, GLfloat y, GLfloat z)
{
data.push_back(x);
data.push_back(y);
data.push_back(z);
}
void addCube(std::vector<GLfloat> &data, GLfloat x, GLfloat y, GLfloat z)
{
addVertex(data, x-1, y-1, z-1);
addVertex(data, x-1, y-1, z+1);
addVertex(data, x-1, y+1, z+1);
addVertex(data, x-1, y+1, z-1);
addVertex(data, x+1, y-1, z-1);
addVertex(data, x+1, y-1, z+1);
addVertex(data, x+1, y+1, z+1);
addVertex(data, x+1, y+1, z-1);
addVertex(data, x-1, y-1, z-1);
addVertex(data, x-1, y-1, z+1);
addVertex(data, x+1, y-1, z+1);
addVertex(data, x+1, y-1, z-1);
addVertex(data, x-1, y+1, z-1);
addVertex(data, x-1, y+1, z+1);
addVertex(data, x+1, y+1, z+1);
addVertex(data, x+1, y+1, z-1);
addVertex(data, x-1, y-1, z-1);
addVertex(data, x-1, y+1, z-1);
addVertex(data, x+1, y+1, z-1);
addVertex(data, x+1, y-1, z-1);
addVertex(data, x-1, y-1, z+1);
addVertex(data, x-1, y+1, z+1);
addVertex(data, x+1, y+1, z+1);
addVertex(data, x+1, y-1, z+1);
}
void makeCubes(std::vector<GLfloat> &data)
{
float targetX[10], targetY[10], targetZ[10];
data.clear();
data.reserve(720);
//...initialize the above arrays...
for (int i=0; i<10; i++)
{
std::vector<GLfloat> newData;
addCube(newData, targetX[i], targetY[i], targetZ[i]);
data.insert(data.begin(), newData.begin(), newData.end());
}
}
您可以通过引用第一个元素轻松将其转换为C数组:
std::vector<GLfloat> data;
makeCubes(data);
GLfloat *cArray = &data[0];
可以像这样调用glBufferData:
glBufferData(GL_ARRAY_BUFFER,
data.size()*sizeof(GLfloat),
&data[0],
GL_STREAM_DRAW);