我正在关注这个关于如何从网格中获取索引以构建 GL_TRIANGLE_STRIP 网格的小教程http://dan.lecocq.us/wordpress/2009/12/25/triangle-strip-for-grids-a-construction/
我以正确的顺序获得了一些索引,但我无法在顶点 7 和 8 上锻炼逻辑,正如他在最后一个图中显示的那样
这是我的代码:
cols = 4;
rows = 4;
sizeW = 320.0f;
sizeH = 240.0f;
float spaceX = sizeW / cols;
float spaceY = sizeH / rows;
// Mesh indices
for ( int x = 0; x < cols-1; x++ ) {
for ( int y = 0; y < rows-1; y++ ) {
int i = y + x * rows;
cout << "{a, b, c}: " << i << ", " << i+4 << ", " << (i+4)-3;
cout << endl;
}
cout << "------------" << endl;
}
vboMesh.setMesh( mesh, GL_DYNAMIC_DRAW );
cout << "mesh number of vertices: " << mesh.getNumVertices() << endl;
这是我的输出:
0, 4, 1
1, 5, 2
2, 6, 3
--------
4, 8, 5
5, 9, 6
6, 10, 7
--------
8, 12, 9
9, 13, 10
10, 14, 11
更新: 在评论之后,我锻炼了另一种算法来获取索引,这就是我到目前为止所拥有的:
// Mesh indices
int totalQuads = (cols-1) * (rows-1);
int totalTriangles = totalQuads * 2;
int totalIndices = (cols*2) * (rows-1);
cout << "total number of quads: " << totalQuads << endl;
cout << "total number of triangles: " << totalTriangles << endl;
cout << "total number of indices: " << totalIndices << endl;
int n = 0;
int ind = 0;
vector<int> indices;
for ( int i = 0; i < totalIndices; i++ ) {
//cout << i % (cols*2) << ", ";
ind = i % (cols*2);
if ( i % (cols*2) == 0 ) {
n++;
cout << n << endl;
if ( n%2 == 1 ) {
cout << "forward" << endl;
}
else {
cout << "backward" << endl;
}
}
indices.push_back( ind );
}
//cout << endl;
这段代码告诉我什么时候需要前进,什么时候需要后退,通过执行i % (cols*2)我得到一个类似 0、1、2、3、4、5、6、7、0 的列表, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 4, 5, 6, 7,所以理论上我现在需要做的就是+4 -3前进和+4 - 5 后退
更新 2: 取得了一些进展,这些是新结果 0、4、1、5、2、6、3、7、7、11、6、10、5、9、4、8、14、18、15 、 19、16、20、17、21最后一组数字还是错的
// Mesh indices
int totalQuads = (cols-1) * (rows-1);
int totalTriangles = totalQuads * 2;
int totalIndices = (cols*2) * (rows-1);
cout << "total number of quads: " << totalQuads << endl;
cout << "total number of triangles: " << totalTriangles << endl;
cout << "total number of indices: " << totalIndices << endl;
bool isGoingBackwards = false;
int n = 0;
int ind = 0;
vector<int> indices;
for ( int i = 0; i < totalIndices; i++ ) {
if ( i % (cols*2) == 0 ) {
ind++;
if ( ind%2 == 1 ) {
n = ((cols*2) - 1) * (ind-1);
cout << "forward " << n << endl;
isGoingBackwards = false;
}
else {
n = ((cols*2) - 1) * (ind-1);
cout << "backward " << n << endl;
isGoingBackwards = true;
}
}
indices.push_back( n );
if ( i%2 == 0 ) {
n += 4;
}
else {
( isGoingBackwards ) ? n -= 5 : n -= 3;
}
}
更新 3:
我终于明白了!这是新代码
int n = 0;
int colSteps = cols * 2;
int rowSteps = rows - 1;
vector<int> indices;
for ( int r = 0; r < rowSteps; r++ ) {
for ( int c = 0; c < colSteps; c++ ) {
int t = c + r * colSteps;
if ( c == colSteps - 1 ) {
indices.push_back( n );
}
else {
indices.push_back( n );
if ( t%2 == 0 ) {
n += cols;
}
else {
(r%2 == 0) ? n -= (cols-1) : n -= (cols+1);
}
}
}
}