A question from studying Example using Mesh

Hello, I’ve recently become interested in mesh, so I’m studying while looking at examples of openframeworks.

One of the examples is a code that gives index to vertex.

int width = vidGrabber.getWidth();
int height = vidGrabber.getHeight();

//add one vertex to the mesh for each pixel
for (int y = 0; y < height; y++){
	for (int x = 0; x < width; x++){

for (int y = 0; y<height-1; y++){
	for (int x=0; x<width-1; x++){
		mainMesh.addIndex(x+y*width);				// 0
		mainMesh.addIndex((x+1)+y*width);			// 1
		mainMesh.addIndex(x+(y+1)*width);			// 10
		mainMesh.addIndex((x+1)+y*width);			// 1
		mainMesh.addIndex((x+1)+(y+1)*width);		// 11
		mainMesh.addIndex(x+(y+1)*width);			// 10

There’s something I’m curious about in this code.

  1. Why are we doing “y<height-1” instead of “y<height”?

2.I don’t know why there are patterns of 0, 1, 10 and 1, 11, 10 in the process of giving index to vertex.
I’m curious about the mathematical reason why you get that pattern by multiplying your width.

I’d appreciate it if you could answer.


You’re effectively defining two triangular mesh faces per loop iteration. For that you need to pair the right indices of three vertices defining each face.

Screenshot 2021-10-02 at 11.45.28

Iterating by y, then by x, means going row by row.

Since you’re always looking ahead to find the next vertex in the appropriate row and column, you can’t really iterate the full width or height. Otherwise, you’d overshoot and end up with a weird, superfluous pair of faces (red) at the end of each row.


Here are a couple of quick additions to what diff-arch posted. OpenGL loves to draw triangles. The indices are the order in which the vertices are drawn. There is a handedness, or orientation, to the triangles. So, for the two triangles above, drawing (0,1,5) and (0,4,5) is not the same as drawing (0,1,5) and (0, 5, 4). The first pair have an opposite orientation, while the 2nd pair have the same handedness. The triangles should have the same orientation when they are drawn.

1 Like

Thank you so much for your answer. I understood it well.

But I don’t really understand what the red triangle is about.

Since index starts at 0, I know that it does -1, is this right?

If not, can you explain it in more detail?

Thank you for the supplementary explanation.
It helped me a lot.

Like I described above each row features four vertices in my example. The corresponding width is thus also 4. If you now loop from 0 to width - 1 this means that for the first row you visit the vertices 0, 1, 2. At each vertex index, you look one ahead in the row. This means that you can’t visit the last vertex in the row, otherwise you’ll end up in the next row, which you don’t want, and which results in the red triangles. The row ends with the last vertex. There is no triangle left to draw here. The vertex indices that the formulas spit out are 3, 4, 7 and 4, 8, 7, which produces the red triangles.

Oh, I totally understand.

Thank you so much.

If I make a triangle with the last vertex of the row, it looks weird.