jetpack Compose 使用 GLSurfaceView的例子

实现了一个 compose 测试 GL纹理的例子;

页面中包含两个按钮, 点击按钮1 ,GL中新增纹理,
点击按钮2 ,GL中释放纹理

package com.example.gltest

import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Matrix
import android.opengl.GLES20
import android.opengl.GLSurfaceView
import android.opengl.GLUtils
import android.os.Bundle
import android.util.Log
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Modifier
import androidx.compose.ui.viewinterop.AndroidView
import java.nio.ByteBuffer
import java.nio.ByteOrder
import java.nio.ShortBuffer
import javax.microedition.khronos.egl.EGLConfig
import javax.microedition.khronos.opengles.GL10


const val TAG = "MainActivity"

class MainActivity : ComponentActivity() {
    private val glView by lazy { MyGLView(this) }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setContent {
//            var count by remember { mutableStateOf(0) }
//
//
//            var textureList by remember {
//                mutableStateOf(0)
//            }
            var scope = rememberCoroutineScope()
            Box {
                AndroidView(factory = { glView }, modifier = Modifier.fillMaxSize())

                Column {

                    Button(
                        onClick = {
                            // 点击按钮1 - 在GL中新增一张图片纹理
                            val bitmap = BitmapFactory.decodeResource(resources, com.example.gltest.R.drawable.a) // 替换为您的大图片资源
                            glView.addTexture(bitmap)
                        }
                    ) {
                        Text("Add Texture")
                    }

                    Button(
                        onClick = {
                            // 点击按钮2 - 释放GL中一张图片纹理
                            glView.releaseTexture()
                        }
                    ) {
                        Text("Release Texture")
                    }

                    TextureInfoView(glView)

                }
            }
        }
    }
}

@Composable
fun TextureInfoView(glView: MyGLView) {
    val currentTextureId by remember(glView) { derivedStateOf { glView.textureList() } }
    Text(text = "生成的纹理礼拜: ")
    Text("Current Texture ID: ${currentTextureId.joinToString()}")
}

class MyGLView(activity: MainActivity) : GLSurfaceView(activity) {
    private val renderer: MyRenderer = MyRenderer()

    init {
        setEGLContextClientVersion(2)
        setRenderer(renderer)
    }


    fun addTexture(bitmap: Bitmap) {
        queueEvent {
            renderer.addTexture(bitmap)
            bitmap.recycle() // 确保释放位图资源
        }
    }


    fun releaseTexture() {
        queueEvent {
            renderer.releaseTexture()
        }

    }

    fun textureList(): MutableList<Int> {
        return renderer.textureList
    }

    private inner class MyRenderer : Renderer {
        val textureList = mutableListOf<Int>()

        override fun onSurfaceCreated(gl: GL10, config: EGLConfig) {
            GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f)
        }

        override fun onDrawFrame(gl: GL10) {
            GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT)
        }

        override fun onSurfaceChanged(gl: GL10, width: Int, height: Int) {
            GLES20.glViewport(0, 0, width, height)
        }

        fun addTexture(bitmap: Bitmap) {
            val textureIds = IntArray(1)
            GLES20.glGenTextures(1, textureIds, 0)

            // 绑定纹理
            GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureIds[0])

            // 设置纹理参数
            GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR.toFloat())
            GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR.toFloat())

            // 加载位图数据到纹理
            GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0)

            // 解绑纹理
            GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, 0)

            // 现在 `textureIds[0]` 包含了加载的纹理的ID
            textureList.add(textureIds[0])
            Log.e(TAG, "addTexture: ${textureIds[0]}")
            //画图片无效需要联调
//            drawImage(textureIds[0])
        }

        fun releaseTexture() {
            val textureId = textureList.removeLastOrNull() ?: return
            Log.e(TAG, "releaseTexture: $textureId")
            // 在这里执行释放纹理的操作,可以使用OpenGL的API
            val texturesToDelete = intArrayOf(textureId)
            GLES20.glDeleteTextures(1, texturesToDelete, 0)
        }
    }

    private fun drawImage(textureId:Int) {
        val vertices = floatArrayOf(
            -1.0f, 1.0f, 0.0f,  // 左上角
            -1.0f, -1.0f, 0.0f,  // 左下角
            1.0f, -1.0f, 0.0f,  // 右下角
            1.0f, 1.0f, 0.0f // 右上角
        )
        val textureCoordinates = floatArrayOf(
            0.0f, 0.0f,  // 左上角
            0.0f, 1.0f,  // 左下角
            1.0f, 1.0f,  // 右下角
            1.0f, 0.0f // 右上角
        )

        // Create an float array in opengles runtime (native) and put vertex data.

        // Create an float array in opengles runtime (native) and put vertex data.
        val mVertexBuffer = ByteBuffer.allocateDirect(vertices.size * 4)
            .order(ByteOrder.nativeOrder())
            .asFloatBuffer()
        mVertexBuffer.put(vertices)
        mVertexBuffer.position(0)
        val indices = shortArrayOf(0, 1, 2, 0, 2, 3)

        // Create an float array in opengles runtime (native) and put texture data.

        // Create an float array in opengles runtime (native) and put texture data.
        val mTextureBuffer = ByteBuffer.allocateDirect(textureCoordinates.size * 4)
            .order(ByteOrder.nativeOrder())
            .asFloatBuffer()
        mTextureBuffer.put(textureCoordinates)
        mTextureBuffer.position(0)

        val dlb = ByteBuffer.allocateDirect(indices.size * 2)
        dlb.order(ByteOrder.nativeOrder())
        val drawListBuffer = dlb.asShortBuffer()
        drawListBuffer.put(indices)
        drawListBuffer.position(0)

        val program = GLES20.glCreateProgram()
        // 使用顶点着色器和片段着色器进行绘制
        GLES20.glUseProgram(program)

        // 获取着色器中的属性和统一变量位置
        val positionHandle = GLES20.glGetAttribLocation(program, "aPosition")
        val textureCoordinateHandle = GLES20.glGetAttribLocation(program, "aTextureCoordinate")
        val mvpMatrixHandle = GLES20.glGetUniformLocation(program, "uMVPMatrix")
        val textureHandle = GLES20.glGetUniformLocation(program, "uTexture")

        // 启用属性数组
        GLES20.glEnableVertexAttribArray(positionHandle)
        GLES20.glEnableVertexAttribArray(textureCoordinateHandle)

        // 设置顶点坐标数据
        GLES20.glVertexAttribPointer(positionHandle, 3, GLES20.GL_FLOAT, false, 12, mVertexBuffer)

        // 设置纹理坐标数据
        GLES20.glVertexAttribPointer(textureCoordinateHandle, 2, GLES20.GL_FLOAT, false, 8, mTextureBuffer)

        // 设置纹理单元
        GLES20.glActiveTexture(GLES20.GL_TEXTURE0)
        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureId)
        GLES20.glUniform1i(textureHandle, 0)

        // 设置模型视图投影矩阵
        // 设置模型视图投影矩阵
        val mvpMatrix = FloatArray(16)

        // 设置模型视图投影矩阵
        GLES20.glUniformMatrix4fv(mvpMatrixHandle, 1, false, mvpMatrix, 0)

        // 绘制图形
        GLES20.glDrawElements(GLES20.GL_TRIANGLES, indices.size, GLES20.GL_UNSIGNED_SHORT, drawListBuffer)

        // 禁用属性数组
        GLES20.glDisableVertexAttribArray(positionHandle)
        GLES20.glDisableVertexAttribArray(textureCoordinateHandle)
    }


}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Jetpack Compose for Desktop 中使用 JNI(Java Native Interface)可以让你与本地代码进行交互,调用 C/C++ 函数或访问本地库。以下是使用 JNI 在 Jetpack Compose for Desktop 中进行 JNI 开发的基本步骤: 1. 创建本地代码库:首先,你需要使用 C/C++ 编写本地代码库。可以使用你喜欢的 C/C++ 编译器将代码编译成动态链接库(.so 或 .dll 文件)。 2. 创建 Java 接口:在你的 Jetpack Compose for Desktop 项目中,创建一个 Java 接口来声明与本地代码库交互的函数。这个接口将作为 Java 和本地代码之间的桥梁。 3. 加载本地库:在你的 Jetpack Compose for Desktop 项目中,使用 `System.loadLibrary()` 或 `System.load()` 方法加载你的本地库。 4. 实现 Java 接口:创建一个 Java 类来实现你在第二步创建的 Java 接口。在这个类中,使用 `native` 关键字标记与本地代码库交互的函数。 5. 生成头文件:使用 `javac -h` 命令生成头文件(.h 文件)。这个命令会根据你在第二步创建的 Java 接口自动生成对应的头文件。 6. 实现本地代码:使用你喜欢的 C/C++ 编辑器打开生成的头文件,并实现其中声明的函数。确保函数签名和参数类型与 Java 接口中的一致。 7. 编译本地代码:使用你喜欢的 C/C++ 编译器编译你的本地代码,并生成动态链接库文件。 8. 调用本地函数:在你的 Jetpack Compose for Desktop 项目中,可以通过调用你在第二步创建的 Java 接口实现的函数来调用本地代码。 需要注意的是,JNI 开发需要一定的 C/C++ 编程经验,以及对 Java 和本地代码交互的机制有一定的了解。在使用 JNI 进行开发时,还需要注意内存管理和线程安全性等问题。确保在使用完本地资源后正确释放它们,以避免内存泄漏或其他问题的发生。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值