OpenGL ES ->帧缓冲对象(Frame Buffer Object)离屏渲染获取纹理贴图

XML文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- OpenGL渲染区域 -->
    <com.example.myapplication.MyGLSurfaceView
        android:id="@+id/gl_surface_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <!-- 用于显示FBO截图的ImageView -->
    <ImageView
        android:id="@+id/image_view"
        android:layout_width="120dp"
        android:layout_height="120dp"
        android:layout_alignParentEnd="true"
        android:layout_alignParentTop="true"
        android:layout_margin="16dp"
        android:background="#33000000"
        android:contentDescription="FBO截图预览" />

    <!-- 捕获FBO图像的按钮 -->
    <Button
        android:id="@+id/capture_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="16dp"
        android:text="捕获FBO图像"
        android:padding="12dp" />

</RelativeLayout>

Activity代码

class MainActivity : AppCompatActivity() {
   
   
    private lateinit var glSurfaceView: MyGLSurfaceView
    private lateinit var imageView: ImageView
    private lateinit var captureButton: Button

    override fun onCreate(savedInstanceState: Bundle?) {
   
   
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        glSurfaceView = findViewById(R.id.gl_surface_view)
        imageView = findViewById(R.id.image_view)
        captureButton = findViewById(R.id.capture_button)
        captureButton.setOnTouchListener(object : View.OnTouchListener {
   
   
            override fun onTouch(v: View?, event: MotionEvent?): Boolean {
   
   
                when(event?.action){
   
   
                    MotionEvent.ACTION_DOWN -> {
   
   
                        getFrameBufferBitmap()?.let {
   
   
                            imageView.setImageBitmap(it)
                        }
                    }
                    MotionEvent.ACTION_UP -> {
   
   
                        getFrameBufferBitmap()?.let {
   
   
                            imageView.setImageBitmap(null)
                        }
                    }
                    else -> {
   
   }
                }
                return true
            }
        })
    }

    private fun getFrameBufferBitmap() : Bitmap? {
   
   
        return glSurfaceView?.getFrameBufferBitmap()
    }
}

自定义GLSurfaceView代码

class MyGLSurfaceView(context: Context, attrs: AttributeSet) : GLSurfaceView(context, attrs) {
   
   
    private var mRenderer = MyGLRenderer(context)

    init {
   
   
        // 设置 OpenGL ES 3.0 版本
        setEGLContextClientVersion(3)
        setRenderer(mRenderer)
        // 设置渲染模式, 仅在需要重新绘制时才进行渲染,以节省资源
        renderMode = RENDERMODE_WHEN_DIRTY
    }

    fun getFrameBufferBitmap(): Bitmap? {
   
   
        return mRenderer?.getFrameBufferBitmap()
    }
}

自定义GLSurfaceView.Renderer代码

class MyGLRenderer(private val mContext: Context) : GLSurfaceView.Renderer {
   
   
    private var mDrawData: DrawData? = null
    private var mWidth = 0
    private var mHeight = 0
    override fun onSurfaceCreated(gl: GL10?, config: EGLConfig?) {
   
   
        // 当 Surface 创建时调用, 进行 OpenGL ES 环境的初始化操作, 设置清屏颜色为青蓝色 (Red=0, Green=0.5, Blue=0.5, Alpha=1)
        GLES30.glClearColor(0.0f, 0.5f, 0.5f, 1.0f)
        mDrawData = DrawData().apply {
   
   
            initShader()
            initVertexBuffer()
            initTexture0(mContext, R.drawable.pic)
            initTexture1(mContext, R.drawable.bitmap_shader)
        }
    }

    override fun onSurfaceChanged(gl: GL10?, width: Int, height: Int) {
   
   
        // 当 Surface 尺寸发生变化时调用,例如设备的屏幕方向发生改变, 设置视口为新的尺寸,视口是指渲染区域的大小
        GLES30.glViewport(0, 0, width
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值