Quad不能正常生成?

时间:2013-12-31 21:55:51

标签: c++ geometry vertices quad

我有一个高度和宽度,我正在尝试用它们生成四边形。 当我这样做时:

vector<Point2D> vertices;
vector<unsigned int> indices;

Point2D topLeft = Point2(0, height);
Point2D topRight = Point2(width, height);
Point2D bottomLeft = Point2(0, 0);
Point2D bottomRight= Point2(width, 0);

indices.push_back(0);
indices.push_back(1);
indices.push_back(2);
indices.push_back(0);
indices.push_back(2);
indices.push_back(3);

vertices.push_back(topLeft);    
vertices.push_back(topRight);
vertices.push_back(bottomLeft);
    vertices.push_back(bottomRight);

我得到一个三角形而不是四边形。 但是当我这样做时:

vector<Point2D> vertices;
vector<unsigned int> indices;

Point2D topLeft = Point2(-width, height);
Point2D topRight = Point2(width, height);
Point2D bottomLeft = Point2(width, -height);
Point2D bottomRight= Point2(-width, -height);

indices.push_back(0);
indices.push_back(1);
indices.push_back(2);
indices.push_back(0);
indices.push_back(2);
indices.push_back(3);

vertices.push_back(topLeft);    
vertices.push_back(topRight);
vertices.push_back(bottomLeft);
    vertices.push_back(bottomRight);

它完美无缺。出了什么问题?我认为右下角?

2 个答案:

答案 0 :(得分:1)

第一段产生两个与不同绕组重叠的三角形,逆时针绕组三角形被剔除。如果你关闭剔除,你会看到两个三角形,但不是你想要的排列。

第二种排列完全不同,两个三角形具有顺时针缠绕顺序,形成四边形。如果用零替换负数,您将看到它与之前的排列不同。

Point2D topLeft    = Point2(    0, height);
Point2D topRight   = Point2(width, height);
Point2D bottomLeft = Point2(width, 0);
Point2D bottomRight= Point2(0,     0);

答案 1 :(得分:0)

在不确切知道你使用的是什么的情况下,我建议使用x和y代替宽度和高度,因为你将'width'设置为0,用作x坐标。标签可能会导致您实际使用的内容混淆,因为您不太可能希望宽度或高度为0。

如果您在纸上绘制要添加为顶点的点的坐标,则表明您正在制作三角形。如果height = 3且width = 4,则顶点列表为:

(0,3)//沿着y

(3,4)//沿着x

(0,0)//回到0 - 三角形!

(4,0)//沿x的孤独段

在我看来,你推顶点的顺序应该是topLeft-&gt; topRight-&gt; bottomRight-&gt; bottomLeft来制作一个顺时针方向的多边形。