What would be the best algorithm to generate a list of vertices to draw a plane using triangle strips?
I'm looking for a function which receives the plane's width and height and returns a float array containing correctly indexed vertices.
width represents the number of vertices per row.
height represents the number of vertices per column.
float* getVertices( int width, int height ) {
...
}
void render() {
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, getVertices(width,heigth));
glDrawArrays(GL_TRIANGLE_STRIP, 0, width*height);
glDisableClientState(GL_VERTEX_ARRAY);
}
none of the code above gives a correct mesh generation. A very good article about how to make a strip of triangles on a simple plane: http://www.learnopengles.com/android-lesson-eight-an-introduction-to-index-buffer-objects-ibos/
Here is my test code that actually tested and fully working:
I was doing something similar and using the first two answers I have come up with this (tested, C#, XNA)
This is easily convertable to c++, just make
indices
an std::vector.The notable features for my solution are that: a) It doesn't need to change the winding order per substrip - adding two points creates two degenerate triangles, so the order is correct for the next substrip. b) You should conditionally add the first and last dg triangle vertices.
Here is some code that does this (not tested, but you get the idea at least):
The first loop sets up the vertex array in a standard rectangular grid. There are R*C vertices.
The second loop sets up the indices. In general, there are two vertices per square in the grid. Each vertex will cause a new triangle to be drawn (with the previous two vertices), so each square is drawn with two triangles.
The first and last vertex at the start and end of each row is duplicated. This means there are two triangles of zero area (degenerate triangles) between each row. This allows us to draw the entire grid in one big triangle strip. This technique is called stitching.
Thanks you all. I've coded this. Is it correct? Or is the generated strip somehow wrong?
With width=4 and height=4 this is what I got:
And here I'm modifying some vertex height: