Opengl es 帮助从网格获取顶点索引
我正在学习这个小教程,学习如何从网格中获取索引以构建GL_三角形_条带网格 我以正确的顺序得到了一些索引,但我无法计算顶点7和8上的逻辑,如他在上一个图中所示 这是我的密码:Opengl es 帮助从网格获取顶点索引,opengl-es,gl-triangle-strip,Opengl Es,Gl Triangle Strip,我正在学习这个小教程,学习如何从网格中获取索引以构建GL_三角形_条带网格 我以正确的顺序得到了一些索引,但我无法计算顶点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
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;
更新:
在这些评论之后,我编制了另一个算法来获取索引,这是我到目前为止所做的:
// 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;
//网格索引
整数totalQuads=(cols-1)*(第1行);
int totalTriangles=totalQuads*2;
整数总指数=(cols*2)*(第1行);
你这样做和写出来的方式显然是错误的。请记住,您正在生成三角形条带,而不是单个三角形。我没有为您编写代码(但他很好地解释了这些原则),但是您必须以交替的方向遍历行,即从左到右的第一行和从右到左的下一行,正如他在文本和图中所解释的那样。顶点重复背后的逻辑(在本例中为7和8)是,您需要在这些顶点处更改条带的方向,因此您需要复制这些顶点(通过这种方式插入退化三角形,不会绘制任何像素)。第一行的顶点顺序实际上是
0 4 1 5 2 6 3 7
然后继续
7 11 6 10 5 9 4 8
8 12 9 13 10 14 11 15
三角形条带的剔除将反转每个三角形。你必须把7和8放进两次的原因是,在这些点上,剔除实际上不应该被逆转。唯一可以实现这一点的方法是反转消隐两次(实际上是渲染一个不可见的多边形),以便继续使用与以前相同的消隐方向。我知道,我的for循环都是错误的,我需要做的就是使用该序列构建和排列,好的,我将尝试一下。。。我是opengl新手,所以所有这些都很难理解
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);
}
}
}
}
0 4 1 5 2 6 3 7
7 11 6 10 5 9 4 8
8 12 9 13 10 14 11 15