使用GLSurfaceView绘制图片

2023-12-29 23:58

本文主要是介绍使用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绘制图片的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/551090

相关文章

Spring中@Lazy注解的使用技巧与实例解析

《Spring中@Lazy注解的使用技巧与实例解析》@Lazy注解在Spring框架中用于延迟Bean的初始化,优化应用启动性能,它不仅适用于@Bean和@Component,还可以用于注入点,通过将... 目录一、@Lazy注解的作用(一)延迟Bean的初始化(二)与@Autowired结合使用二、实例解

SpringBoot使用Jasypt对YML文件配置内容加密的方法(数据库密码加密)

《SpringBoot使用Jasypt对YML文件配置内容加密的方法(数据库密码加密)》本文介绍了如何在SpringBoot项目中使用Jasypt对application.yml文件中的敏感信息(如数... 目录SpringBoot使用Jasypt对YML文件配置内容进行加密(例:数据库密码加密)前言一、J

Spring Boot 中正确地在异步线程中使用 HttpServletRequest的方法

《SpringBoot中正确地在异步线程中使用HttpServletRequest的方法》文章讨论了在SpringBoot中如何在异步线程中正确使用HttpServletRequest的问题,... 目录前言一、问题的来源:为什么异步线程中无法访问 HttpServletRequest?1. 请求上下文与线

在 Spring Boot 中使用异步线程时的 HttpServletRequest 复用问题记录

《在SpringBoot中使用异步线程时的HttpServletRequest复用问题记录》文章讨论了在SpringBoot中使用异步线程时,由于HttpServletRequest复用导致... 目录一、问题描述:异步线程操作导致请求复用时 Cookie 解析失败1. 场景背景2. 问题根源二、问题详细分

从零教你安装pytorch并在pycharm中使用

《从零教你安装pytorch并在pycharm中使用》本文详细介绍了如何使用Anaconda包管理工具创建虚拟环境,并安装CUDA加速平台和PyTorch库,同时在PyCharm中配置和使用PyTor... 目录背景介绍安装Anaconda安装CUDA安装pytorch报错解决——fbgemm.dll连接p

Vue项目的甘特图组件之dhtmlx-gantt使用教程和实现效果展示(推荐)

《Vue项目的甘特图组件之dhtmlx-gantt使用教程和实现效果展示(推荐)》文章介绍了如何使用dhtmlx-gantt组件来实现公司的甘特图需求,并提供了一个简单的Vue组件示例,文章还分享了一... 目录一、首先 npm 安装插件二、创建一个vue组件三、业务页面内 引用自定义组件:四、dhtmlx

使用Python创建一个能够筛选文件的PDF合并工具

《使用Python创建一个能够筛选文件的PDF合并工具》这篇文章主要为大家详细介绍了如何使用Python创建一个能够筛选文件的PDF合并工具,文中的示例代码讲解详细,感兴趣的小伙伴可以了解下... 目录背景主要功能全部代码代码解析1. 初始化 wx.Frame 窗口2. 创建工具栏3. 创建布局和界面控件4

一文详解如何在Python中使用Requests库

《一文详解如何在Python中使用Requests库》:本文主要介绍如何在Python中使用Requests库的相关资料,Requests库是Python中常用的第三方库,用于简化HTTP请求的发... 目录前言1. 安装Requests库2. 发起GET请求3. 发送带有查询参数的GET请求4. 发起PO

Java中的Cursor使用详解

《Java中的Cursor使用详解》本文介绍了Java中的Cursor接口及其在大数据集处理中的优势,包括逐行读取、分页处理、流控制、动态改变查询、并发控制和减少网络流量等,感兴趣的朋友一起看看吧... 最近看代码,有一段代码涉及到Cursor,感觉写法挺有意思的。注意是Cursor,而不是Consumer

Node.js net模块的使用示例

《Node.jsnet模块的使用示例》本文主要介绍了Node.jsnet模块的使用示例,net模块支持TCP通信,处理TCP连接和数据传输,具有一定的参考价值,感兴趣的可以了解一下... 目录简介引入 net 模块核心概念TCP (传输控制协议)Socket服务器TCP 服务器创建基本服务器服务器配置选项服