Search code examples
c++geometryvertices

Quad doesn't generate properly?


I have a height and width and I am trying to generate a quad with them. When I do this:

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

I get a triangle instead of a quad. But when I do this:

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

It works perfectly.What is going wrong?I think the bottom right?


Solution

  • This first segment produces two triangles overlapping with different winding, and the counterclockwise winding triangle is being culled. If you turn off culling, You'd see both triangles, but not in the arrangement you'd like.

    The second arrangement is completely different, two triangles have clockwise winding order which form a quad. If you replace the negative numbers with zeros you'll see that its not the same as the previous arrangement.

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