Я следую этому небольшому руководству о том, как получить индексы из сетки для построения сетки 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);
}
}
}
}