Android OpenGL ES,形状未在设备上显示,但在模拟器上运行良好?

Android OpenGL ES, shape not showing on device, but work fine on emulator?

我已经按照 android 团队的教程学习,展示了如何使用 OpenGL 创建简单的三角形。这是一个link to the tutorial. Everything work fine on both android devices and emulator until i get to the part with applying 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 的索尼。知道为什么会发生这种情况,我怀疑以某种方式添加相机视图会改变视口外某处三角形的位置。我该如何解决?

我在不同的设备上遇到了类似的显示问题。您可以尝试在 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)
}