Java 如何画一组水平线?
我是OpenGL新手,作为学习练习,我决定从包含顶点位置的m x n矩阵网格中绘制一组水平线 这就是我所拥有的 如果我用线带 使用顶点数组和索引的代码片段将非常棒,我似乎无法仅从我需要查看和使用代码示例的教科书中获得这个概念 任何帮助都将不胜感激强>Java 如何画一组水平线?,java,opengl,processing,lines,Java,Opengl,Processing,Lines,我是OpenGL新手,作为学习练习,我决定从包含顶点位置的m x n矩阵网格中绘制一组水平线 这就是我所拥有的 如果我用线带 使用顶点数组和索引的代码片段将非常棒,我似乎无法仅从我需要查看和使用代码示例的教科书中获得这个概念 任何帮助都将不胜感激 @托马斯 让它与以下代码一起工作 totalPoints = GRID_ROWS * 2 * (GRID_COLUMNS - 1); indices = new int[totalPoints]; points = new GLModel(th
@托马斯 让它与以下代码一起工作
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();
totalPoints=网格行*2*(网格列-1);
指数=新整数[总点数];
点=新的GLModel(此,totalPoints,LINES,GLModel.DYNAMIC);
int n=0;
points.beginUpdate顶点();
对于(int row=0;row
然后,我更新和绘制通过这样做
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();
points.beginUpdate顶点();
对于(int n=0;n
这就是结果
通过更改嵌套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;
}
}
for(int col=0;col
现在我可以有任意数量的行和列
再次感谢 您需要为每个段绘制一条线,然后重新使用索引,即对于第一部分,您需要为(0,1)、(1,2)、(2,3)等绘制一条线 编辑: 假设您有一个4x5数组(4条线,每条线5个顶点)。然后可以像这样计算索引(伪代码):
Vertex[]v=新顶点[20];//网格中的20个顶点
对于(int row=0;row
然后对
numrows*(numcols-1)
线段(GL_线)发出绘制调用,即示例中的16。请注意,addLineIndexs
将是一个函数,它将一个线段的索引对添加到索引数组中,然后该数组将提供给draw调用。您需要为每个线段绘制一条线并重新使用索引,即对于第一部分,您将为(0,1)、(1,2)、(2,3)等绘制一条线
编辑:
假设您有一个4x5数组(4条线,每条线5个顶点)。然后可以像这样计算索引(伪代码):
Vertex[]v=新顶点[20];//网格中的20个顶点
对于(int row=0;row
然后对
numrows*(numcols-1)
线段(GL_线)发出绘制调用,即示例中的16。请注意,addLineIndexs
将是一个函数,它将一个线段的索引对添加到索引数组中,然后将该数组提供给绘图调用。非常感谢您的工作,但它适用于nxn网格,但不适用于mxn,因此如果行数不同于列数,绘图会变得混乱,有什么想法吗?这对nxm也适用。例如,如果numcols为5,则rowoffset应为0、5、10、15。我怀疑你的计算或顶点数组有问题。可以为您的问题添加一些代码吗?非常感谢您的努力,但它适用于nxn网格,而不适用于mxn,因此如果行数不同于列数,图形会变得混乱,有什么想法吗?这也适用于nxm。例如,如果numcols为5,则rowoffset应为0、5、10、15。我怀疑你的计算或顶点数组有问题。可以为你的问题添加一些代码吗?
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
}
}