Android OpenGLES2.0(十)——OpenGL中的平移、旋转、缩放

在前面的博客中,所有的例子都是一个对象,类似绘制圆锥绘制圆柱,我们都是传入一个参数,然后去控制那个圆面的位置,如果我们要绘制几个个正方形,它的位置、大小、方向都是不相同的,按照那种方式该多麻烦啊。所以我们需要更好的办法——矩阵变换。

什么是矩阵
其实在之前的博客中,我们也基本都用到了矩阵,投影的设置,就是根据参数生成一个4*4的矩阵,我们用长度为16的浮点型数组来存储,相机的设置也是如此。那么矩阵到底该怎么定义?

矩阵(Matrix)是一个按照长方阵列排列的复数或实数集合,最早来自于方程组的系数及常数所构成的方阵。在物理学中,矩阵于电路学、力学、光学和量子物理中都有应用;计算机科学中,三维动画制作也需要用到矩阵。 矩阵的运算是数值分析领域的重要问题。

在大学高数课程中也有学习矩阵的只是,矩阵也可以进行加减和乘法运算。在OpenGL中使用的向量为列向量,我们通过利用矩阵与列向量(颜色、坐标都可看做列向量)相乘,得到一个新的列向量。利用这点,我们构建一个的矩阵,与图形所有的顶点坐标坐标相乘,得到新的顶点坐标集合,当这个矩阵构造恰当的话,新得到的顶点坐标集合形成的图形相对原图形就会出现平移、旋转、缩放或拉伸、抑或扭曲的效果。矩阵和顶点相乘示例如下(如果用长度为16的float数组存储,则m序号-1即为其下标),那么如何构造合适的矩阵呢? 
⎡⎣⎢⎢⎢x1y1z11⎤⎦⎥⎥⎥=⎡⎣⎢⎢⎢m1m2m3m4m5m6m7m8m9m10m11m12m13m14m15m16⎤⎦⎥⎥⎥⎡⎣⎢⎢⎢xyz1⎤⎦⎥⎥⎥=⎡⎣⎢⎢⎢m1∗x+m5∗y+m9∗z+m13m2∗x+m6∗y+m10∗z+m14m3∗x+m7∗y+m11∗z+m15m4∗x+m8∗y+m12∗z+m16⎤⎦⎥⎥⎥(1)
(1)[x1y1z11]=[m1m5m9m13m2m6m10m14m3m7m11m15m4m8m12m16][xyz1]=[m1∗x+m5∗y+m9∗z+m13m2∗x+m6∗y+m10∗z+m14m3∗x+m7∗y+m11∗z+m15m4∗x+m8∗y+m12∗z+m16]

我们在Android OpenGLES2.0(七)——着色器语言GLSL中提到了,OpenGLES中的矩阵有2*2、3*3和4*4三种矩阵。在本篇博客中只以4*4来进行分析。
OpenGL中常用矩阵
如(1)中公式所示,即为矩阵和顶点相乘的公式,如果矩阵取值恰当,则可实现图形的平移、旋转、缩放、拉伸等变换。在这里直接给出: 
⎡⎣⎢⎢⎢1000010000100001⎤⎦⎥⎥⎥(原矩阵,相乘结果不变)
(原矩阵,相乘结果不变)[1000010000100001]

⎡⎣⎢⎢⎢100001000010δXδYδZ1⎤⎦⎥⎥⎥(平移变换矩阵)
(平移变换矩阵)[100δX010δY001δZ0001]

⎡⎣⎢⎢⎢10000cosβsinβ00−sinβcosβ00001⎤⎦⎥⎥⎥(绕X轴变换矩阵)
(绕X轴变换矩阵)[10000cosβ−sinβ00sinβcosβ00001]

⎡⎣⎢⎢⎢cosβ0−sinβ00100sinβ0cosβ00001⎤⎦⎥⎥⎥(绕Y轴变换矩阵)
(绕Y轴变换矩阵)[cosβ0sinβ00100−sinβ0cosβ00001]

⎡⎣⎢⎢⎢cosβsinβ00−sinβcosβ0000100001⎤⎦⎥⎥⎥(绕Z轴变换矩阵)
(绕Z轴变换矩阵)[cosβ−sinβ00sinβcosβ0000100001]

⎡⎣⎢⎢⎢⎢Sx0000Sy0000Sz00001⎤⎦⎥⎥⎥⎥(缩放变换矩阵)
(缩放变换矩阵)[Sx0000Sy0000Sz00001]
平移旋转缩放实例
在Android OpenGLES的开发中,为了方便,我们可以将以上基本矩阵都用写个方法来传入参数直接生成,这样使用就方便了。实际上,这一步,Android已经帮我们做好了。之前我们使用投影、相机的时候都使用过Matrix这个工具类的方法,平移、旋转、缩放变化的方法同样在其中可以找到。以平移方法为例分析:

public static void translateM(float[] m, int mOffset,float x, float y, float z) {
    for (int i=0 ; i<4 ; i++) {
        int mi = mOffset + i;
        m[12 + mi] += m[mi] * x + m[4 + mi] * y + m[8 + mi] * z;
    }
}
1
2
3
4
5
6
我们可以看到,实际上就是将数组m从mOffset开始的位置,取16个值作为一个4*4的矩阵,按照(1)中的公式进行计算。得到新的结果,覆盖在原数组中。 
依据Matrix工具类,我们可以建立一个带有现场保护和恢复的新工具类:

public class VaryTools {

    private float[] mMatrixCamera=new float[16];    //相机矩阵
    private float[] mMatrixProjection=new float[16];    //投影矩阵
    private float[] mMatrixCurrent=     //原始矩阵
            {1,0,0,0,
            0,1,0,0,
            0,0,1,0,
            0,0,0,1};

    private Stack<float[]> mStack;      //变换矩阵堆栈

    public VaryTools(){
        mStack=new Stack<>();
    }

    //保护现场
    public void pushMatrix(){
        mStack.push(Arrays.copyOf(mMatrixCurrent,16));
    }

    //恢复现场
    public void popMatrix(){
        mMatrixCurrent=mStack.pop();
    }

    public void clearStack(){
        mStack.clear();
    }

    //平移变换
    public void translate(float x,float y,float z){
        Matrix.translateM(mMatrixCurrent,0,x,y,z);
    }

    //旋转变换
    public void rotate(float angle,float x,float y,float z){
        Matrix.rotateM(mMatrixCurrent,0,angle,x,y,z);
    }

    //缩放变换
    public void scale(float x,float y,float z){
        Matrix.scaleM(mMatrixCurrent,0,x,y,z);
    }

    //设置相机
    public void setCamera(float ex,float ey,float ez,float cx,float cy,float cz,float ux,float uy,float uz){
        Matrix.setLookAtM(mMatrixCamera,0,ex,ey,ez,cx,cy,cz,ux,uy,uz);
    }

    public void frustum(float left,float right,float bottom,float top,float near,float far){
        Matrix.frustumM(mMatrixProjection,0,left,right,bottom,top,near,far);
    }

    public void ortho(float left,float right,float bottom,float top,float near,float far){
        Matrix.orthoM(mMatrixProjection,0,left,right,bottom,top,near,far);
    }

    public float[] getFinalMatrix(){
        float[] ans=new float[16];
        Matrix.multiplyMM(ans,0,mMatrixCamera,0,mMatrixCurrent,0);
        Matrix.multiplyMM(ans,0,mMatrixProjection,0,ans,0);
        return ans;
    }

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
以此工具类,结合Android OpenGLES2.0(五)——绘制立方体来利用矩阵变换绘制多个不同的立方体或者长方体了,主要代码如下:

GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT| GLES20.GL_DEPTH_BUFFER_BIT);
cube.setMatrix(tools.getFinalMatrix());
cube.drawSelf();

//y轴正方形平移
tools.pushMatrix();
tools.translate(0,3,0);
cube.setMatrix(tools.getFinalMatrix());
cube.drawSelf();
tools.popMatrix();

//y轴负方向平移,然后按xyz->(0,0,0)到(1,1,1)旋转30度
tools.pushMatrix();
tools.translate(0,-3,0);
tools.rotate(30f,1,1,1);
cube.setMatrix(tools.getFinalMatrix());
cube.drawSelf();
tools.popMatrix();

//x轴负方向平移,然后按xyz->(0,0,0)到(1,-1,1)旋转120度,在放大到0.5倍
tools.pushMatrix();
tools.translate(-3,0,0);
tools.scale(0.5f,0.5f,0.5f);

//在以上变换的基础上再进行变换
tools.pushMatrix();
tools.translate(12,0,0);
tools.scale(1.0f,2.0f,1.0f);
tools.rotate(30f,1,2,1);
cube.setMatrix(tools.getFinalMatrix());
cube.drawSelf();
tools.popMatrix();

//接着被中断的地方执行
tools.rotate(30f,-1,-1,1);
cube.setMatrix(tools.getFinalMatrix());
cube.drawSelf();
tools.popMatrix();
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
最后绘制效果如下: 

这里写图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值