如何绘制一组水平线?

发布于 2024-10-25 08:22:33 字数 1702 浏览 2 评论 0原文

我是 OpenGL 新手,作为学习练习我决定从包含顶点位置的 mxn 矩阵网格中绘制一组水平线

这就是我所拥有的 在此处输入图像描述

如果我使用 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;
  }
}

现在我可以拥有任意数量的行和列,

再次感谢!

I am new to OpenGL as learning exercise I decided to draw a set of horizontal lines from a grid of m x n matrix containing the vertices locations

This is what I have
enter image description here

and If I use LINE_STRIP
enter image description here

A code snippet using vertex arrays and indices will be great, I cant seem to be able to get the concept just from a text book I need to see and play with a code example
Any help will be much appreciated!


@Thomas
Got it working with the following code

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();

Then I update and draw by doing

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();

This is the result

enter image description here


Fix it by changing the nested for loop

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;
  }
}

Now I can have any number of rows and columns

Thanks agin!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

陌生 2024-11-01 08:22:33

您需要为每个段绘制一条线并重新使用索引,即对于第一部分,您将为 (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) 线段 (GL_LINES) 的绘制调用,即示例中的 16。请注意,addLineIndices 是一个函数,它将一条线段的索引对添加到索引数组中,然后将其提供给绘制调用。

You need to draw a line for each segment and resuse an index, i.e. for the first part you'd draw a line for (0,1), (1,2), (2,3) and so on.

Edit:

Suppose you have a 4x5 array (4 lines, 5 vertices per line). You could then calculate the indices like this (pseudo code):

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
  }
}

Then issue the draw call for numrows * (numcols - 1) linesegments (GL_LINES), i.e. 16 in the example. Note that addLineIndices would be a function that adds the index pair for one line segment to an index array which is then supplied to the draw call.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文