我不熟悉OpenGL作为学习练习我决定从包含顶点位置的m x n矩阵网格中绘制一组水平线
这就是我所拥有的
如果我使用LINE_STRIP
使用顶点数组和索引的代码片段会很棒,我似乎无法从我需要查看和使用代码示例的教科书中获取概念 任何帮助将不胜感激!
@Thomas 得到它使用以下代码
totalPoints = GRID_ROWS * 2 * (GRID_COLUMNS - 1);
indices = new int[totalPoints];
points = new GLModel(this, totalPoints, LINES, GLModel.DYNAMIC);
int n = 0;
points.beginUpdateVertices();
for ( int row = 0; row < GRID_ROWS; row++ ) {
for ( int col = 0; col < GRID_COLUMNS - 1; col++ ) {
int rowoffset = row * GRID_COLUMNS;
int n0 = rowoffset + col;
int n1 = rowoffset + col + 1;
points.updateVertex( n, pointsPos[n0].x, pointsPos[n0].y, pointsPos[n0].z );
indices[n] = n0;
n++;
points.updateVertex( n, pointsPos[n1].x, pointsPos[n1].y, pointsPos[n1].z );
indices[n] = n1;
n++;
}
}
points.endUpdateVertices();
然后我通过执行
更新和绘制points.beginUpdateVertices();
for ( int n = 0; n < totalPoints; n++ ) {
points.updateVertex( n, pointsPos[indices[n]].x, pointsPos[indices[n]].y, pointsPos[indices[n]].z );
}
points.endUpdateVertices();
这是结果
通过更改嵌套for循环来修复它
for ( int col = 0; col < GRID_COLUMNS; col++ ) {
for ( int row = 0; row < GRID_ROWS - 1; row++ ) {
int offset = col * GRID_ROWS;
int n0 = offset + row;
int n1 = offset + row + 1;
indices[n++] = n0;
indices[n++] = n1;
}
}
现在我可以拥有任意数量的行和列
感谢agin!
答案 0 :(得分:4)
您需要为每个段绘制一条线并重新使用索引,即对于第一部分,您将为(0,1),(1,2),(2,3)等绘制一条线。
编辑:
假设您有一个4x5阵列(4行,每行5个顶点)。然后你可以像这样计算索引(伪代码):
Vertex[] v = new Vertex[20]; // 20 vertices in the grid
for(int row = 0; row < numrows; row++) // numrows = 4
{
int rowoffset = row * numcols ; //0, 4, 8, 12
for(int col = 0; col < (numcols - 1); col++) //numcols = 5
{
addLineIndices(rowoffset + col, rowoffset + col +1); //adds (0,1), (1,2), (2,3) and (3, 4) for the first row
}
}
然后发出numrows * (numcols - 1)
linesegments(GL_LINES)的绘制调用,即示例中的16。请注意,addLineIndices
将是一个函数,它将一个线段的索引对添加到索引数组,然后将其提供给绘制调用。