Question

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?

Was it helpful?

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

OTHER TIPS

Without knowing exactly what you are using, I would suggest using x and y instead of width and height, since you are setting the 'width' to 0 which is used as an x coordinate. The labels can cause confusion about what you are actually working with, since it is unlikely you would want width or height to be 0.

If you graph on paper the coordinates of the points you are adding as vertices, it seems you are making a triangle. If height = 3 and width = 4, your vertex list is:

(0, 3) // up along y

(3, 4) // over along x

(0, 0) // back to 0 - triangle!

(4, 0) // lonely segment along x

It looks to me like the order you are pushing vertices should be topLeft->topRight->bottomRight->bottomLeft to make a clock-wise polygon.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top