开发者

How to get vertices of a mesh that has been drawn in OpenGL ES?

Suppose I draw a cube like this:

gl.glVertexPointer(3, GL10.GL_FLOAT, 0, mVerticesBuffer);

        gl.glTranslatef(x, y, z);
        gl.glRotatef(rz, 0, 0, 1);
        gl.glRotatef(rx, 1, 0, 0);
        gl.glRot开发者_开发百科atef(ry, 0, 1, 0);

        gl.glDrawElements(GL10.GL_TRIANGLES, mNumOfIndices,
                GL10.GL_UNSIGNED_SHORT, mIndicesBuffer);
           float[] result = getVertices(mVerticesBuffer);

Where mVerticesBuffer is FloatBuffer that has the cube's vertices. After moving rotating the cube I expect the valuse in mVerticesBuffer to have changed and I should be able to get these values back as plain float array. When I try to get these values from mVerticesBuffer, I always get the values that would corresponds to location of the cube before any translations/rotations. Is there OpenGL API to get the correct values or am I missing something?


What you need is an exact case of Transform Feedback (TF): you want to get the transformed vertices back. That's perfectly fine for today's OpenGL. Here are the instructions to use it:

  1. Create a shader (with only vertex components) that performs necessary vertex transformation. Call glTransformFeedbackVaryings before linking tell GL about your varying outputs in a shader you want to record.
  2. Prepare a VBO to store result. Before doing TF, call glBindBufferBase to tell GL about the storage for resulting values.
  3. When doing TF, call glBeginTransformFeedback() and record a query of type GL_PRIMITIVES_WRITTEN.


OpenGL is not a math library. It will not do any modifications on data you pass to it. You can request contents of buffers and the like, but only on explicit request and not the way you mistakenly expect it to do.

The transformation defined by the modelview and projection matrix, which are manipulated using glLoadMatrix, glMultMatrix, glTranslate, glRotate, glScale, these transformations are applied in-situ to the vertex positions only for the processing of each vertex and are not stored persistently. There's transform feedback to retrieve the transformed vertex data, but this goes through a different OpenGL object and buffer.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜