I'm trying to compute some of my vertices in a vertex (or geometry) shader. When I draw finally the vertices they lost the order I gave to the vertex array object (with GL_ELEMENT_ARRAY_BUFFER). The function glDrawElements doesn't work in this situation, instead I have to use glDrawArrays. Am I wrong?
How can I save the order of the triangles index for the Transform Feedback?
You are not wrong. Vertices are written to the transform feedback buffer in the order they are processed by the vertex (or geometry) shader. I don't see an easy way around it.
This means that if you want to draw the vertices in the captured buffer, you will use
glDrawArrays()
. Or in GL 4.0 and later, you can also useglDrawTransformFeedback()
.If you were drawing indexed, and need the vertices back in their original order, you will have to reshuffle them yourself. If your index buffer was filled with
n
indices from arrayidxA
, your input buffer with vertices from arrayinVertexA
, and the vertices from the captured buffer are in arraycapturedVertexA
, the transformed vertices in the original order could be written tooutVertexA
as:This will work as long as the drawing was with primitive type
GL_TRIANGLES
. If you were drawing with something likeGL_TRIANGLE_STRIP
, the index logic will be slightly more complex because transform feedback produces separate triangles.I guess you could let the GPU reshuffle the vertices by running the output through transform feedback again, and using an index array that is the reverse mapping of the original index array.
It looks kind of awkward in any case. You may want to think through your use case again, and make sure that you really need the vertices back in the original order.
Disclaimer: I have never used transform feedback, and have done only very minimal work implementing/fixing it in drivers. All of the above is my understanding from reading the spec.