gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 10:59:43 25 4
gpt4 key购买 nike

您好,我正在开发一个基于 OpenGLES 的安卓应用程序,我需要在 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";

关于投影、 View 和模型矩阵,这是它们的初始化函数:

    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
}

我现在非常绝望,我们将不胜感激任何帮助。

最佳答案

看来这道题得自己杀了,其实我只是犯了一个愚蠢的错误,投影矩阵的比例应该是surface view的宽度除以同一个surface view的高度,这不应该是纹理图像宽度/高度的比率。

关于android - 在 Z 轴上旋转纹理时 Opengles 图像倾斜,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46969802/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com