在OpenGL ES 2中渲染时拉伸的圆

问题描述 投票:0回答:1

我在设备上绘制了一个纯白色的圆圈,但它会根据屏幕尺寸在整个设备上按比例拉伸。搜寻2D示例和可能的答案,我无法正确缩放它。

我的渲染器类:

@Override
public void onSurfaceChanged(GL10 gl, int width, int height)
{
    float scaleX = (float) width / Screen.getScreenWidthPx();
    float scaleY = (float) height / Screen.getScreenHeightPx();
    final int vpWidth = (int)(Screen.getScreenWidthPx() * scaleX);
    final int vpHeight = (int)(Screen.getScreenHeightPx() * scaleY);

    GLES20.glViewport(0, 0, vpWidth, vpHeight);

    mCam.setProjection(Screen.getScreenWidthPx(), Screen.getScreenHeightPx());
}

我的相机类别:

public void setProjection(float width, float height)
{        
    final float ratio = width / height;
    Matrix.orthoM(mProjection, 0, 0, width, height, 0, -1, 1);
}

顶点着色器:

private String mVSCode =
        "attribute vec4 vPosition;" +
                "uniform float sWidth;" +
                "uniform float sHeight;" +
                "void main() {" +
                "gl_Position = vPosition;" +                  
                "}";

结果:

enter image description here

java android opengl-es-2.0
1个回答
0
投票

您必须通过顶点着色器中的正交投影矩阵来变换顶点坐标。

向顶点着色器添加矩阵统一(mat4 uProjection)。通过mProjection设置制服。将顶点坐标乘以顶点着色器中的uProjection

attribute vec4 vPosition;

uniform uProjection;

void main() {
    gl_Position = uProjection* vPosition;
}
© www.soinside.com 2019 - 2024. All rights reserved.