Convert an android.graphics.Matrix to a GL mat4?

元气小坏坏 提交于 2021-01-28 10:47:29

问题


I have a somewhat different issue than How to convert a 3x3 rotation matrix into 4x4 matrix?: I'm writing 2D OpenGL code that manipulates textures on a flat (z always equals 0) geometry. The android.graphics.Matrix class has many handy, debugged convenience methods that can do things like rotate about an explicit pivot point.

Isn't converting a 3x3 matrix (that can do 2D affine transformations) to a 4x4 matrix a matter of

a11 a12 a13    a11 a12   0 a13
a21 a22 a23    a21 a22   0 a23
a31 a32 a33      0   0   1   0
               a31 a32   0 a33

... with, perhaps, a transpose to make it column-major?


回答1:


If you only do affine transformations, it will work that way. In that case, a31 and a32 can always be zero and a33 should be one anyways. You can get away with a 2x3 matrix in these situations, and with modern shader-based GL, you could even directly work with that 3x3 or 2x3 matrix without any conversion.

Note that if you do projective transforms, extending the matrix to 4x4 will still work, but introduces distortions in the z dimension.




回答2:


You should convert the translate X and Y values from Graphic coordinate system to a OpenGL coordinate system.

Here is a working example:

You can also check this question which is practically the same, but with explanation: OpenGL ES how to apply transformations from android.graphics.Matrix



来源:https://stackoverflow.com/questions/17797890/convert-an-android-graphics-matrix-to-a-gl-mat4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!