使用GLSurfaceView绘制图片

<?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=".MainActivity">

    <android.opengl.GLSurfaceView
        android:id="@+id/sv_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
package com.nas.suppersurface

import android.opengl.GLSurfaceView
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        sv_content.setEGLContextClientVersion(2)
        sv_content.setRenderer(MyRender())
        sv_content.renderMode = GLSurfaceView.RENDERMODE_WHEN_DIRTY
    }

    override fun onResume() {
        super.onResume()
        sv_content.onResume()
    }

    override fun onPause() {
        super.onPause()
        sv_content.onPause()
    }
}

主要代码在render里

package com.nas.suppersurface

import android.graphics.BitmapFactory
import android.opengl.GLES10
import android.opengl.GLES20
import android.opengl.GLSurfaceView
import android.opengl.GLUtils
import com.rulerbug.bugutils.Utils.BugApp
import com.rulerbug.bugutils.Utils.BugLogUtils
import com.rulerbug.bugutils.Utils.BugShaderUtil
import java.nio.Buffer
import java.nio.ByteBuffer
import java.nio.ByteOrder
import javax.microedition.khronos.egl.EGLConfig
import javax.microedition.khronos.opengles.GL10

 
class MyRender : GLSurfaceView.Renderer {
    private val v_position = floatArrayOf(
        -1f, -1f,
        1f, -1f,
        -1f, 1f,
        1f, 1f
    )
    private val t_position = floatArrayOf(
        1f, 0f,
        0f, 0f,
        1f, 1f,
        0f, 1f
    )
    var v_buffer: Buffer? = null;
    var t_buffer: Buffer? = null;
    var program: Int = 0
    var av_Position: Int = 0
    var af_Position: Int = 0
    var textureId: Int = 0

    init {
        v_buffer = ByteBuffer.allocateDirect(v_position.size * 4)
            .order(ByteOrder.nativeOrder())
            .asFloatBuffer()
            .put(v_position)
        v_buffer!!.position(0)
        t_buffer = ByteBuffer.allocateDirect(t_position.size * 4)
            .order(ByteOrder.nativeOrder())
            .asFloatBuffer()
            .put(t_position)
        t_buffer!!.position(0)
    }


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


    override fun onSurfaceCreated(gl: GL10?, config: EGLConfig?) {
        val vertex_shader = BugShaderUtil.readRawTxt(R.raw.vertex_shader)
        val fragment_shader = BugShaderUtil.readRawTxt(R.raw.fragment_shader)
        BugLogUtils.e(vertex_shader)
        program = BugShaderUtil.createProgram(vertex_shader, fragment_shader)
        if (program > 0) {
            av_Position = GLES20.glGetAttribLocation(program, "av_Position")
            af_Position = GLES20.glGetAttribLocation(program, "af_Position")

            var textIds = intArrayOf(1)
            GLES20.glGenTextures(1, textIds, 0)
            textureId = textIds[0]
            if (textureId == 0) {
                return
            }
            GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureId)

            GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_REPEAT)
            GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_REPEAT)

            GLES20.glTexParameteri(
                GLES20.GL_TEXTURE_2D,
                GLES20.GL_TEXTURE_MIN_FILTER,
                GLES20.GL_LINEAR
            )
            GLES20.glTexParameteri(
                GLES20.GL_TEXTURE_2D,
                GLES20.GL_TEXTURE_MAG_FILTER,
                GLES20.GL_LINEAR
            )
            var bitmap = BitmapFactory.decodeResource(
                BugApp.getContext().resources,
                R.drawable.icon_title_back_white
            )
            GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0)
            bitmap.recycle()
            bitmap = null
        }
    }

    override fun onDrawFrame(gl: GL10?) {
        GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT)
        GLES20.glClearColor(1f, 1f, 1f, 1f)
        GLES20.glUseProgram(program)
        GLES20.glEnableVertexAttribArray(av_Position)
        GLES20.glVertexAttribPointer(av_Position, 2, GLES20.GL_FLOAT, false, 8, v_buffer)
        GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4)
        GLES20.glEnableVertexAttribArray(af_Position)
        GLES20.glVertexAttribPointer(af_Position, 2, GLES20.GL_FLOAT, false, 8, t_buffer)
        GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4)
    }
}

render的步骤主要是

1.在构造函数里把顶点塞进buffer里

2.在    onSurfaceCreated   里把脚本里的坐标做处理,有的是提取,有的提取后还得绑定

顶点坐标和纹理坐标比较好处理

3. onDrawFrame  在这里先清除缓存,再进行绘制

 

 

然后是脚本

 

vertex_shader
attribute vec4 av_Position;
attribute vec2 af_Position;
varying vec2 v_texPo;
void main(){
    v_texPo=af_Position;
    gl_Position=av_Position;
}

 

 

fragment_shader
precision mediump float;
varying vec2 v_texPo;
uniform sampler2D sTexture;
void main(){
       gl_FragColor=texture2D(sTexture,v_texPo);
}

 

 

 

依赖

    implementation 'com.github.JiJiBo:BugUtils:1.1.28'

 

 

        maven { url 'https://jitpack.io' }

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值