本文主要是介绍使用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.GLSurfaceViewandroid: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.suppersurfaceimport 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.suppersurfaceimport 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.GL10class 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 = 0var av_Position: Int = 0var af_Position: Int = 0var textureId: Int = 0init {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' }
这篇关于使用GLSurfaceView绘制图片的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!