第二十四节 OpenGL ES - 矩阵旋转中的总结

2018-12-06  本文已影响6人  最美下雨天

关于下载的课件需要修改的地方:
1、这儿的宽高得是实际的宽高,课件中应该是屏幕的宽高,注意宽高不要写反了

 //手机的宽高,其实可以动态获取的
        GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D, 0, GLES20.GL_RGBA, 1440, 2560, 0, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, null);

2、修改下关于正交投影的逻辑,修改的更灵活,正交投影主要是解决图片变形的问题

public void onSurfaceChanged(int width, int height) {
        GLES20.glViewport(0, 0, width, height);
        fboRender.onChange(width, height);

        int w=bitmap.getWidth();
        int h=bitmap.getHeight();
        //bitmap不再使用了
        bitmap.recycle();
        bitmap = null;
        float sWH=w/(float)h;
        float sWidthHeight=width/(float)height;
        if(width>height){
            if(sWH>sWidthHeight){
                Matrix.orthoM(mProjectMatrix, 0, -sWidthHeight*sWH,sWidthHeight*sWH, -1,1, 3, 7);
            }else{
                Matrix.orthoM(mProjectMatrix, 0, -sWidthHeight/sWH,sWidthHeight/sWH, -1,1, 3, 7);
            }
        }else{
            if(sWH>sWidthHeight){
                Matrix.orthoM(mProjectMatrix, 0, -1, 1, -1/sWidthHeight*sWH, 1/sWidthHeight*sWH,3, 7);
            }else{
                Matrix.orthoM(mProjectMatrix, 0, -1, 1, -sWH/sWidthHeight, sWH/sWidthHeight,3, 7);
            }
        }

//        Matrix.rotateM(mProjectMatrix, 0, 180, 1, 0, 0);
//        Matrix.rotateM(mProjectMatrix, 0, 180, 0, 0, 1);

    }

我们要绘制的原图是这样的:


image.png

代码演示效果:


image.png

仔细对比下两张图的区别,我们就看下“投影矩阵”中的“投”字,发现其实绕着x轴旋转180度就会显示正确
所以,将这行代码放开

      Matrix.rotateM(mProjectMatrix, 0, 180, 1, 0, 0);
上一篇 下一篇

猜你喜欢

热点阅读