I noticed an interesting feature in OpenGL related to vertex indexing, and so far I can’t understand why this is happening.

So, what is the point:

I have a clockwise order to bypass the vertices for visible faces. There are 4 vertices (the usual rectangle), they are set approximately as follows: the order of the vertices in the array

In the array, they are like this:

vertices[DefaultGeometryType::PLANE] = { { { (size / 2), (size / 2), 0.0f },{ 1.0f,0.0f,0.0f },{ 1.0f,1.0f } }, { { (size / 2), -(size / 2), 0.0f },{ 0.0f,1.0f,0.0f },{ 1.0f,0.0f } }, { { -(size / 2), -(size / 2), 0.0f },{ 0.0f,0.0f,1.0f },{ 0.0f,0.0f } }, { { -(size / 2), (size / 2), 0.0f },{ 1.0f,1.0f,0.0f },{ 0.0f,1.0f } }, }; 

Then I have an array of indices for this square. It looks like this:

  indices[DefaultGeometryType::PLANE] = { 0,1,2,0,2,3 }; 

Thus, it turns out that the square is divided into 2 triangles with common vertices 0 and 2 (diagonal 0-2 will be a common edge for these two triangles). As a result, I get this picture:

indexes in the first way

It seems to be all right. And then I decided to change the indices a bit, so that the common edge for the two triangles would not be a diagonal of 0-2 and 3-1. I changed the indices as follows:

 3,0,1,1,2,3 

And in the end, the square began to look like this:

indexes in the second way

Agree, the picture is completely different. It seems that the interpolation of colors is not entirely correct. I tried different versions of the order of the indices, but it always turned out that when a rectangle divides diagonally 0-2 everything is fine, and when 3-1 it is such a strange picture. But why? Maybe this is some kind of incorrect indexing of vertices? Are there any rules for correct indexing? Or am I missing something and really should be?

    1 answer 1

    The pictures are correct. Simply, you probably expect to get a gradient-to-gradient gradient, and in fact there are two separate gradients. Note that the color of the vertex that does not belong to this triangle is not used to calculate the color of the pixels of this triangle. To build a “right” gradient, you would first have to calculate the gradient for each pixel between vertices 0 - 1 and 2 - 3, and then draw a grid of rectangles one pixel high. In a more advanced form, this is implemented for example in Direct2d:

    enter image description here

    • 2
      It seems understood. That is, this happens not because the indices were set somehow incorrectly, but simply because only the colors of its vertices are used to calculate the colors of each triangle, and in different cases different colors fall on the triangles? But in general, there are no rules on how to form "correct meshes" (sequence and so on) - no? The main thing that the order of the established bypass (clockwise / counterclockwise)? Or are there any nuances? - Alex Nem
    • @AlexNem Order is important only if culling is used - VTT