Android OpenGL ES详解——绘制圆角矩形

1、绘制矩形

代码如下:

renderer类:

Kotlin 复制代码
package com.example.roundrect

import android.content.Context
import android.opengl.GLES30
import android.opengl.GLSurfaceView.Renderer
import com.opengllib.data.VertexArray
import com.opengllib.programs.ShaderProgram
import javax.microedition.khronos.egl.EGLConfig
import javax.microedition.khronos.opengles.GL10

class RoundRectRenderer(context: Context) : Renderer {
    private var mVertices = floatArrayOf(
        -0.5f, -0.5f,
        0.5f, 0.5f,
        -0.5f, 0.5f,
        -0.5f, -0.5f,
        0.5f, -0.5f,
        0.5f, 0.5f,
    )
    private var mShaderProgram: ShaderProgram? = null
    private var mContext = context
    private var mVertexArray: VertexArray? = null

    override fun onSurfaceCreated(gl: GL10?, config: EGLConfig?) {
        GLES30.glClearColor(0.6f, 0.7f, 0.8f, 1.0f)
        mVertexArray = VertexArray(mVertices)

        mShaderProgram = ShaderProgram(
            mContext,
            R.raw.roundrect_vertex_shader,
            R.raw.roundrect_fragment_shader
        )
        mShaderProgram?.useProgram()
        mVertexArray?.setVertexAttribPointer(
            0,
            mShaderProgram?.getPositionAttributeLocation()!!,
            2,
            0
        )

    }

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

    override fun onDrawFrame(gl: GL10?) {
        GLES30.glClear(GLES30.GL_COLOR_BUFFER_BIT)
        GLES30.glDrawArrays(GLES30.GL_TRIANGLES,0 ,mVertices.size / 2)
    }
}

顶点着色器文件roundrect_vertex_shader.glsl代码如下:

cpp 复制代码
attribute vec4 a_Position;

void main()
{
    gl_Position = a_Position;
}

片元着色器文件roundrect_fragment_shader.glsl代码如下:

cpp 复制代码
precision mediump float;

void main()
{
	gl_FragColor = vec4(0.8, 0.5, 0.3, 1.0);
}

2、绘制圆形

3、绘制圆角矩形

参考文章

使用OpenGL实现圆角效果-腾讯云开发者社区-腾讯云

相关推荐
郝学胜-神的一滴6 天前
[简化版 Games 101] 计算机图形学 05:二维变换下
c++·unity·图形渲染·three.js·opengl·unreal
AIminminHu7 天前
OpenGL渲染与几何内核那点事-项目实践理论补充(一-3-(4):你敢说你真的懂OpenGL?一个老师傅眼中的“图形API进化史”)
渲染·opengl·渲染管线
梵尔纳多9 天前
OpenGL 骨骼动画
c++·图形渲染·opengl
AIminminHu9 天前
OpenGL渲染与几何内核那点事-项目实践理论补充(一-3-(2):当你的CAD学会“偷懒”:从“一笔一画”到“一键生成”的OpenGL渲染进化史)
opengl
郝学胜-神的一滴10 天前
中级OpenGL教程 001:从Main函数到相机操控的完整实现
c++·程序人生·unity·图形渲染·unreal engine·opengl
AIminminHu10 天前
OpenGL渲染与几何内核那点事-项目实践理论补充(一-3-(1):你的 CAD 终于能联网协作了,但渲染的“内功心法”到底是什么?)
人工智能·opengl
zhooyu14 天前
利用叉乘判断OpenGL中的左右关系
c++·3d·opengl
zhooyu1 个月前
GLM中lerp实现线性插值
c++·opengl
智算菩萨1 个月前
【OpenGL】10 完整游戏开发实战:基于OpenGL的2D/3D游戏框架、物理引擎集成与AI辅助编程指南
人工智能·python·游戏·3d·矩阵·pygame·opengl
智算菩萨1 个月前
【OpenGL】6 真实感光照渲染实战:Phong模型、材质系统与PBR基础
开发语言·python·游戏引擎·游戏程序·pygame·材质·opengl