简体   繁体   English

C++ 使用 for 循环绘制三角形带 - 顶点、索引

[英]C++ Draw triangle strip using for loop - vertices, indices

I'm trying to draw a plane using triangle strips.我正在尝试使用三角形条带绘制平面。 I understand how to do it manually, but I'm really struggling to do it using a for loop.我了解如何手动执行此操作,但我真的很难使用 for 循环来执行此操作。 So far, the code below draws two triangles.到目前为止,下面的代码绘制了两个三角形。 在此处输入图像描述

//vertices for triangle strip
vertices.push_back(Point3(0,0,0));
vertices.push_back(Point3(1,0,0));
vertices.push_back(Point3(1,1,0));
vertices.push_back(Point3(0,1,0));
vertices.push_back(Point3(-1,1,0));
vertices.push_back(Point3(-1,0,0));


// indices into the arrays above for the first triangle
indices.push_back(0);
indices.push_back(1);
indices.push_back(2);    
// indices for the second triangle
indices.push_back(0);
indices.push_back(2);
indices.push_back(3);    
//indices for the third triangle
indices.push_back(5);
indices.push_back(0);
indices.push_back(3);    
//indices for the fourth triangle
indices.push_back(5);
indices.push_back(3);
indices.push_back(4);

I have to draw 100 going from -pi to pi on the x, and -pi/2 to pi/2 on the y.我必须在 x 上从 -pi 到 pi 绘制 100,在 y 上从 -pi/2 到 pi/2 绘制 100。 Is there an easier way to loop through these values and get the vertices and indices?有没有更简单的方法来遍历这些值并获取顶点和索引? Thank you for any help!感谢您的任何帮助!

Edited to add: I went left to right manually, but it doesn't matter either way.编辑添加:我手动从左到右,但无论哪种方式都没有关系。

在此处输入图像描述

First, you generate your coordinates in a regular grid in the interval [-pi.. +pi], [-pi/2.. +pi/2]首先,您在间隔 [-pi.. +pi]、[-pi/2.. +pi/2] 的规则网格中生成坐标

size_t sizeX = 10; 
size_t sizeY = 10;

//Generate 10x10 coordinates in interval [-pi .. +pi] , [-pi/2 .. +pi/2] , 0
std::vector<Point3>vertices(sizeX * sizeY);
for (size_t j=0; j< sizeY; j++)
   for (size_t i=0; i< sizeX; i++)
      vertices[j*sizeX+i] = Point3(-pi + 2*i*pi/(sizeX-1), -pi/2 + j*pi/(sizeY-1), 0);

In this grid of vertex, each row has sizeX elements.在这个顶点网格中,每一行都有 sizeX 个元素。 This mean that for a vertex with index IDX:这意味着对于索引 IDX 的顶点:

  • The vertex at its right will have index IDX+1其右侧的顶点将具有索引 IDX+1
  • The vertex over it, will have index IDX+sizeX.它上面的顶点将具有索引 IDX+sizeX。

Now let's go for the triangles.现在让我们为三角形使用 go。 It is easier to use a schema based in quads.使用基于四边形的模式更容易。 A quad with lower-left corner in vertex IDX will connect to vertex IDX+1, IDX+sizeX, IDX+sizeX+1.顶点 IDX 左下角的四边形将连接到顶点 IDX+1、IDX+sizeX、IDX+sizeX+1。

After understanding this, you divide the quad into two triangles using same 4 corners.理解这一点后,您使用相同的 4 个角将四边形分成两个三角形。 So you program a nested for to iterate over quads, but you create two triangles instead of one quad.所以你编写了一个嵌套的 for 来迭代四边形,但是你创建了两个三角形而不是一个四边形。 Beware of triangle orientation.注意三角形方向。

在此处输入图像描述

//Generate triangles. Use a schema based in quads
std::vector<size_t>indices;
indices.reserve(2*(sizeX-1) * (sizeY-1));

for (size_t j=0; j< sizeY-1; j++)
{
    //Set idx to point at first vertex of row j
    size_t idx=j*sizeX

    for (size_t i=0; i< sizeX-1; i++)
    {        
        //Bottom triangle of the quad
        indices.push_back(idx);
        indices.push_back(idx+1);
        indices.push_back(idx+sizeX);
        //Top triangle of the quad
        indices.push_back(idx+1);
        indices.push_back(idx+sizeX+1);
        indices.push_back(idx+sizeX);
        //Move one vertex to the right
        idx++;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM