Matrix.multiplyMM(float[] result, int resultOffset, float[] lhs, int lhsOffset, float[] rhs, int rhsOffset)
Multiply two 4x4 matrices together and store the result in a third 4x4 matrix. In matrix notation: result = lhs x rhs. Due to the way matrix multiplication works, the result matrix will have the same effect as first multiplying by the rhs matrix, then multiplying by the lhs matrix. This is the opposite of what you might expect. The same float array may be passed for result, lhs, and/or rhs. However, the result element values are undefined if the result elements overlap either the lhs or rhs elements.
将两个4x4矩阵相乘,把结果存到第三个4x4矩阵里。用矩阵符号表示:result = lhs x rhs 。由于矩阵相乘的工作方式,结果矩阵的效果相当于先被右边的矩阵乘,再被左边的矩阵乘。这跟你期望的情况是相反的。同一个float数组可以作为结果矩阵、左边矩阵、右边矩阵。然而,如果结果矩阵的元素与右边矩阵或左边矩阵的元素重叠,结果矩阵的元素就会无法确定。
奇怪的是,在Android的官方例子中看到如下的代码:
Matrix.multiplyMM(mMVPMatrix, 0, mProjMatrix, 0, mMVPMatrix, 0);
结果矩阵和右边矩阵都是mMVPMatrix,offset都是0。
做如下的测试:
float[] lhs = new float[16]; float[] rhs = new float[16]; float[] res = new float[16]; Matrix.setRotateM(lhs, 0, 45, 1, 1, 0); Matrix.setRotateM(rhs, 0, 30, 0, 1, 1); System.out.println("lhs"); MatrixUtil.printMatrix(lhs); System.out.println("rhs"); MatrixUtil.printMatrix(rhs); Matrix.multiplyMM(res, 0, lhs, 0, rhs, 0); System.out.println("res"); MatrixUtil.printMatrix(res); Matrix.setRotateM(lhs, 0, 45, 1, 1, 0); Matrix.setRotateM(rhs, 0, 30, 0, 1, 1); Matrix.multiplyMM(rhs, 0, lhs, 0, rhs, 0); System.out.println("overlap rhs res"); MatrixUtil.printMatrix(rhs); Matrix.setRotateM(lhs, 0, 45, 1, 1, 0); Matrix.setRotateM(rhs, 0, 30, 0, 1, 1); Matrix.multiplyMM(lhs, 0, lhs, 0, rhs, 0); System.out.println("overlap lhs res"); MatrixUtil.printMatrix(lhs);
运行结果:
lhs
0.8536 0.1464 0.5000 0.0000
0.1464 0.8536 -0.5000 0.0000
-0.5000 0.5000 0.7071 0.0000
0.0000 0.0000 0.0000 1.0000
rhs
0.8660 -0.3536 0.3536 0.0000
0.3536 0.9330 0.0670 0.0000
-0.3536 0.0670 0.9330 0.0000
0.0000 0.0000 0.0000 1.0000
res
0.6142 -0.1316 0.7781 0.0000
0.6054 0.7111 -0.3576 0.0000
-0.5062 0.6907 0.5165 0.0000
0.0000 0.0000 0.0000 1.0000
overlap rhs res
0.6142 -0.1316 0.7781 0.0000
0.6054 0.7111 -0.3576 0.0000
-0.5062 0.6907 0.5165 0.0000
0.0000 0.0000 0.0000 1.0000
overlap lhs res
0.6142 -0.0470 0.6805 0.0000
0.6054 0.5488 -0.2157 0.0000
-0.5062 0.6929 0.5272 0.0000
0.0000 0.0000 0.0000 1.0000
当结果矩阵与右边矩阵重叠时,运算结果是正确的。
当结果矩阵与左边矩阵重叠时,运算结果是不正确的。
这就可以理解为啥Matrix.multiplyMM(mMVPMatrix, 0, mProjMatrix, 0, mMVPMatrix, 0);工作正常了。当然为了避免出问题,最好还是不要重叠。
void android.opengl.Matrix.setRotateM(float[] rm, int rmOffset, float a, float x, float y, float z)
设置矩阵rm为绕axis (x, y, z)旋转角度a的旋转矩阵。
void android.opengl.Matrix.rotateM(float[] m, int mOffset, float a, float x, float y, float z)
Rotates matrix m in place by angle a (in degrees) around the axis (x, y, z)
让矩阵m右乘一个旋转矩阵。
作用到一个物体上时,效果是让物体先旋转,再被m作用。
void android.opengl.Matrix.translateM(float[] m, int mOffset, float x, float y, float z)
Translates matrix m by x, y, and z in place.
让矩阵m右乘一个平移矩阵。
作用到一个物体时,效果是让物体先平移,再被m作用。
相关推荐
OpenGL投影矩阵的一个小程序,可以随意设置参数,方便理解
Android 下OpenGL 开发示例,包括材质,sdk0.5下构建
1. Qt5.9 2. OpenGL 3.3 3.矩阵:缩放矩阵,旋转矩阵 移动矩阵. 4.shader :mat4x4
android OpenGL常用函数
opengl函数大全,包括了核心库和辅助库函数,内容挺多,基本包含了所有的OPENGL常见函数,是编程写作必不可少的参考资料。
android 摄像头 opengl 下绘制android 摄像头 opengl 下绘制 android 摄像头 opengl 下绘制 android 摄像头 opengl 下绘制 android 摄像头 opengl 下绘制
OpenGL绘图相关函数,很不错哦,给大家推荐一下!!!
【OpenGL】二十、OpenGL 矩阵变换 ( 矩阵缩放变换 | 矩阵旋转变换 | 矩阵平移变换 ) https://hanshuliang.blog.csdn.net/article/details/112859256 博客源码 ( 该源码是 Windows 桌面程序 , 使用 Visual Studio ...
OpenGL常用API函数详细的解释,希望对大家有用啊
包括OpenGl核心函数库,应用函数库等
Android OpenGL 游戏引擎 Android OpenGL 游戏引擎 Android OpenGL 游戏引擎 Android OpenGL 游戏引擎
The example code in this class uses the OpenGL ES 2.0 APIs, which is the recommended API version to use with current Android devices. For more information about versions of OpenGL ES, see the OpenGL ...
代码在android 平台,jni 层利用 mvp矩阵实现了 地球的绘制,旋转 移动,以及相机的旋转和移动 ,参考:https://blog.csdn.net/zhangpengzp/article/details/88749779
Android下Opengl ES导引 -第一部分 设置OpenGL ES观察 GLSurfaceView GLSurfaceView.Renderer onSurfaceCreated onDrawFrame onSurfaceChanged 把这些都穿连到一起 全屏显示 -第二部分 建立多边形 ...
该案例代码为Android 平台OpenGL ES实现举例,有两个作用: 1、在Android平台,使用OpenGL ES通过加载灰度图,构建山地图形渲染效果; 2、使用 OpenGLES 生成与使用Mipmap纹理,构建远处模糊,近处清晰的效果。 具体...
android-opengl图片3d旋转
在Android用opengl es3.0写的,由于模拟器不支持opengl es3.0 所以调试时用真机
Android通过opengl显示obj模型
第一部分(第2~9章)详细讲解创建一个简单的空气曲棍球游戏的触控、纹理和基本原理,包括如何成功地初始化OpenGL并将数据发送到屏幕上,如何使用基本的向量和矩阵数学创建三维世界,以及Android的许多特定细节,比如...