【问题标题】:Android OpenGL ES, shape not showing on device, but work fine on emulator?Android OpenGL ES,形状未在设备上显示,但在模拟器上工作正常?
【发布时间】:2020-04-25 06:53:41
【问题描述】:

我遵循了 android 团队的教程,展示了如何使用 OpenGL 创建简单的三角形。这是教程的link。在我开始应用projection 之前,一切都在安卓设备和模拟器上运行良好。

一旦我将字符串 vertexShaderCode 更改为:

private val vertexShaderCode =
        "uniform mat4 uMVPMatrix;" +
        "attribute vec4 vPosition;" +
        "void main() {" +
        "  gl_Position = uMVPMatrix * vPosition;" +
        "}"

三角形未显示在我的 Android 设备上,即带有 Android 6.0 (API 23) 的华为 Y2 和带有 Android 4.4.4 的 SONY。知道为什么会发生这种情况,我怀疑以某种方式添加相机视图会改变视口外某处的三角形位置。我该如何解决?

【问题讨论】:

    标签: android opengl-es


    【解决方案1】:

    我在不同设备上遇到了类似的显示问题。您可以尝试在 frustumM() 方法中增加 'far' 的值:

    Matrix.frustumM(projectionMatrix, 0, left, right, bottom, top, near, far)
    

    增加这个参数的值为我解决了这个问题。

    一般来说,对于平截头体,您可以为纵向和横向使用不同的参数值,例如:

    protected val aspect = widthScreen.toFloat() / heightScreen.toFloat()
    ...
    private fun setPerspectiveProjection() {
        var left = -1.0f; var right = 1.0f
        var bottom = -1.0f; var top = 1.0f
        val near = 1.0f; val far = 145.0f
        if (widthScreen < heightScreen) { // portrait orientation 
            bottom /= aspect
            top /= aspect
        } else { // landscape orientation
            left *= aspect
            right *= aspect
        }
        Matrix.frustumM(projectionMatrix, 0, left, right, bottom, top, near, far)
    }
    

    【讨论】:

    • 我确实增加了“远”参数,但没有成功,其他一切对我来说都是一样的。然后我尝试将 'near' 参数更改为 1f 并解决了问题。 Matrix.frustumM(projectionMatrix, 0, -ratio, ratio, -1f, 1f, 1f, 7f) 正如我所怀疑的,问题出在投影矩阵上,感谢您提供的线索,我设法修复了它,所以非常感谢我!!!
    • @slaviboy 我的荣幸。是的,如果 near 和 far 值有一些余量会更好。一般来说,对于平截头体,您可以为纵向和横向使用不同的参数值。添加到答案中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-24
    • 2012-01-12
    • 1970-01-01
    相关资源
    最近更新 更多