其他分享
首页 > 其他分享> > android – 在Z轴上旋转纹理时,Opengles图像倾斜

android – 在Z轴上旋转纹理时,Opengles图像倾斜

作者:互联网

嗨,我正在开发一个基于OpenGLES的Android应用程序,我需要实时旋转z轴上的纹理.但是当我向模型矩阵添加旋转时,图像会偏斜.

下面的第一个图像是原始图像,第二个图像是在z轴上旋转后的倾斜图像.

enter image description here

enter image description here

这是我的着色器:

private final String mVertexShader = "" +
        "attribute vec4 position;\n" +
        "attribute vec2 textureCoordinate;\n" +
        "varying vec2 textureCoordinateVarying;\n" +
        "uniform mat4 modelMat;\n" +
        "uniform mat4 viewMat;\n" +
        "uniform mat4 projectionMat;\n" +
        "" +
        "void main() {\n" +
        "    gl_Position = projectionMat * viewMat * modelMat*position ;\n" +
        "    textureCoordinateVarying = textureCoordinate;\n" +
        "}";
private final String mFragmentShader = "precision highp float;\n" +
        "uniform sampler2D textureBackground;\n" +
        "uniform sampler2D textureNumbers;" +
        "varying highp vec2 textureCoordinateVarying;\n" +
        "void main() {\n" +
        "    vec4 background = texture2D(textureBackground, textureCoordinateVarying);" +
        "    vec4 foreground = texture2D(textureNumbers, textureCoordinateVarying);" +
        "    gl_FragColor = mix(background, foreground, 0.2);\n" +
        "}\n";

关于投影,视图和模型矩阵,这里是它们的init函数:

    Matrix.setIdentityM(model, 0);
    Matrix.setIdentityM(rotation, 0);
    Matrix.setIdentityM(view, 0);
    Matrix.setIdentityM(projection, 0);

    Matrix.translateM(view, 0,0,0,-3.0f);
    Matrix.perspectiveM(projection,0,45,(float)mBackground.getHeight()/(float)mBackground.getWidth(), 0.1f, 100f);

在onDraw函数中,我将更新模型矩阵的值;

public void onDrawFrame(GL10 gl) {
    //some function

    //update model matrix, and rotation is just a 4*4 identity matrix, and the degree is the z-axis rotation degree.
    Matrix.setRotateM(rotation, 0, degree, 0, 0, 1);
    Matrix.multiplyMM(model, 0, rotation, 0, model, 0);
    Matrix.setIdentityM(rotation,0);

    //draw my texture
}

我现在非常绝望,任何帮助都将受到高度赞赏.

解决方法:

看来我必须自己解决这个问题,实际上我只是犯了一个愚蠢的错误,投影矩阵的比例应该是表面视图的宽度除以同一个表面视图的高度,这不应该是你的比例纹理图像宽度/高度.

标签:android,graphics,game-engine,opengl-es
来源: https://codeday.me/bug/20190710/1426715.html