【问题标题】:Quad doesn't generate properly?Quad 无法正确生成?
【发布时间】:2013-12-31 21:55:51
【问题描述】:

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

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

完美运行。出了什么问题?我认为右下角?

【问题讨论】:

  • 三角形有一个方向。如果你渲染一个背对你的三角形,它就会变成什么都没有。缠绕顺序很重要。
  • @IInspectable 但是当我没有将左下角设置为 0,0 时它起作用了,我只是将它们全部偏移了相同的值,现在完全不同了

标签: c++ geometry vertices


【解决方案1】:

这第一段产生了两个重叠不同缠绕的三角形,并且正在剔除逆时针缠绕的三角形。如果你关闭剔除,你会看到两个三角形,但不是你想要的排列。

第二种排列完全不同,两个三角形顺时针缠绕形成一个四边形。如果你用零替换负数,你会发现它和之前的排列不一样。

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

【讨论】:

  • 那么我需要以不同的顺序添加索引吗?
  • 是的,代码使两个三角形都正确面对屏幕,谢谢
【解决方案2】:

在不确切知道您使用的是什么的情况下,我建议使用 x 和 y 而不是宽度和高度,因为您将“宽度”设置为 0,用作 x 坐标。标签可能会导致您对实际使用的内容感到困惑,因为您不太可能希望宽度或高度为 0。

如果您在纸上绘制要添加为顶点的点的坐标,则似乎您正在制作一个三角形。如果高度 = 3 和宽度 = 4,您的顶点列表是:

(0, 3) // 沿 y 向上

(3, 4) // 在 x 上

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

(4, 0) // 沿 x 的孤立线段

在我看来,您推动顶点的顺序应该是 topLeft->topRight->bottomRight->bottomLeft 以制作顺时针多边形。

【讨论】:

  • 我正在制作一个四边形,基本上我希望左下角位于 x=0 和 y=0 的位置。所以我自然将左上角设为 (0, height) 等等上...我想我错了
猜你喜欢
  • 2017-03-17
  • 1970-01-01
  • 2020-07-12
  • 2018-02-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多