我folloing如何從電網建設GL_TRIANGLE_STRIP獲得指數這個小教程網http://dan.lecocq.us/wordpress/2009/12/25/triangle-strip-for-grids-a-construction/幫助從電網獲取頂點索引
我得到的一些指標以正確的順序,但我可以「T鍛鍊上的頂點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
UPDATE: 繼意見,我鍛鍊另一種算法來獲得指標,這是我到目前爲止有:
// 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);
}
}
}
}
我看,我的for循環都是錯誤的,我所需要做的就是構建並排列這個序列,好吧,我會試試看...我是opengl的新手,所以這一切都證明了有點難以消化 –