我正在尝试使用三角带绘制平面。我知道如何手动执行此操作,但是我真的很难使用for循环来执行此操作。到目前为止,下面的代码绘制了两个三角形。
//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);
我必须从x的-pi到pi绘制100,从y的-pi / 2到pi / 2绘制。有没有更简单的方法可以遍历这些值并获得顶点和索引?谢谢您的帮助!
编辑后添加:我手动从左到右移动,但这两种方式都没有关系。
答案 0 :(得分:0)
首先,您在[-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);
在此顶点网格中,每行都有sizeX元素。这意味着对于索引为IDX的顶点:
现在让我们去看看三角形。使用基于四边形的模式会更容易。顶点IDX左下角的四边形将连接到顶点IDX + 1,IDX + sizeX,IDX + sizeX + 1。
理解了这一点之后,您将四边形使用相同的4个角分成两个三角形。因此,您为嵌套进行编程以遍历四边形,但是创建了两个三角形而不是一个四边形。提防三角形方向。
//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++;
}
}