Android 未设置 OpenGL 渲染器 - 无错误

Android OpenGL Renderer is Not Set - No Errors

我正在使用 GlSurfaceView 和 OpenGL ES 3。当我为我的 OpenGlSurfaceView 设置 OpenGL 渲染器时,没有任何反应。我在渲染器的 onSurfaceCreated 和 onDrawFrame 中的日志没有显示在 logcat。

我唯一得到的是这个警告:W/GLSurfaceView: Warning, !readyToDraw() but waiting for draw finished! Early reporting draw finished.

GlSurfaceView:

class GameSurfaceView : GLSurfaceView
{
    constructor(context: Context?) : super(context)
    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs)

    lateinit var surfaceViewCallback: SurfaceViewCallback


    val renderer = Renderer()

    var isOpenGLSurfaceCreated = false
        private set

   
    init
    {
        isFocusable = true
        isFocusableInTouchMode = true
        isClickable = true
        requestFocus()

    }


    override fun surfaceCreated(var1: SurfaceHolder)
    {
        Timber.d("surface view created") //SHOWS UP ON LOGS
        instance = this
        debugFlags = DEBUG_LOG_GL_CALLS

        setRenderer(renderer)

    }
}

setRenderer 文档:

渲染器:

class Renderer() : GLSurfaceView.Renderer
{

    override fun onSurfaceCreated(p0: GL10?, p1: EGLConfig?)
    {
        Timber.v("Renderer: Surface Created") //never shows up on logs
        glClearColor(1.0f, 0.0f, 0.0f, 1.0f)

    }

    override fun onSurfaceChanged(p0: GL10?, width: Int, height: Int)
    {
        Timber.d("SURFACE CHANGED") //never shows up on logs
        glViewport(0, 0, width, height)
    }

    override fun onDrawFrame(p0: GL10?)
    {
        Timber.d("onDrawFrame Called") //never shows up on logs
        glClear(GLES30.GL_COLOR_BUFFER_BIT)
    }
}

GameFragment的布局(使用GameSurfaceView的地方):

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ui.game.GameFragment">


    <com.mypackage.appname.ui.game.GameSurfaceView
        android:id="@+id/game_surface_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/game_score_text_tv"
        style="@style/GameTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="32dp"
        android:text="@string/score"
        android:textStyle="bold"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.43"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/game_score_tv"
        style="@style/GameTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="4dp"
        android:text="0"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="@+id/game_score_text_tv"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toEndOf="@+id/game_score_text_tv"
        app:layout_constraintTop_toTopOf="@+id/game_score_text_tv" />


</androidx.constraintlayout.widget.ConstraintLayout>

清单:

  <uses-feature
        android:glEsVersion="0x00030000"
        android:required="true" />

OpenGL Android 文档:https://developer.android.com/guide/topics/graphics/opengl

如果您像我一样通过扩展 GLSurfaceView 制作自定义游戏 SurfaceView,不要重写回调方法,例如 onSurfaceCreated、onSurfaceChanged。