Android OpenGL ES 开发(一)— 初识OpenGL ES

2,286 阅读3分钟
原文链接: blog.csdn.net

我的视频课程(基础):《(NDK)FFmpeg打造Android万能音频播放器》

我的视频课程(进阶):《(NDK)FFmpeg打造Android视频播放器》


       对于Open**系列相信大家也不陌生,我在前面博客也介绍了OpenSL ES的一些知识和使用方法,让我们对OpenSL ES有了一定的了解。从今天开始我将给大家带来一系列的Android中OpenGL ES的使用方法(主要是基于2D平面图形的)。

一、OpenGL ES是什么?

我总结的就是:OpenGL ES 是一个嵌入式的(2D/3D)图形处理库。详细请见百度百科

二、OpenGL ES能做什么?

       就拿我们的手机显示系统来说,我们的屏幕显示一个UI界面,其绘制就是OpenGL ES来完成的。由CPU计算每个控件的绘制坐标和颜色等,当这些计算完了后就交给OpenGL ES来调用GPU(显卡),通过GLSL(着色器程序)来绘制画面,这样就把我们的UI界面绘制出来了。其他的还有:游戏、图片滤镜、视频等都可以用OpenGL来处理。

三、Android中的OpenGL ES

       在Android中使用OpenGL ES有2中方法:

       1、一个是在Java层调用OpenGL ES库;

       2、另一个就是在C/C++层调用OpenGL ES库。

不过这2中方法调用OpenGL ES 的方式和过程都基本一致,所以博客主要以Java方式来讲解OpenGL ES的使用,后面如果有需要可能会讲一下C/C++的实现方法。

四、Android中使用OpenGL ES的方式(先作介绍,后续都会讲解,概念不懂可以暂时忽略)

       在Android中使用OpenGL ES的方式也有2中:

       1、自定义类直接继承GLSurfaceView,然后我们重点就主要写Render方法就行。

       2、自己搭建EGL(是OpenGL ES和本地窗口系统的接口,不同平台上EGL配置是不一样的,而OpenGL的调用方式是一致的,就是说:OpenGL跨平台就是依赖于EGL接口)环境,然后开启自己的EGL线程,后续使用和继承GLSurfaceView基本是一致的。

注意:OpenGL ES整个其实就是一个大的状态机,我们通过改变其状态(调用相关的API)就能控制其后续的渲染操作。

五、Android中OpenGL ES的初体验(v2.0)

1、自定义类继承GLSurfaceView

2、实现接口GLSurfaceView.Renderer,其有3个实现方法,分别是:

2.1:void onSurfaceCreated(GL10 gl, EGLConfig config); 用于我们的Surface创建时初始化一些设置。

2.2:void onSurfaceChanged(GL10 gl, int width, int height); 用于我们的Surface大小发生改变时来更改我们自己的绘制大小。

2.3:void onDrawFrame(GL10 gl); 用于我们实际绘制操作。

注意:GLSurfaceView.Renderer的方法都是在自己的EGL线程中实现的,超出这个线程绘制是不起作用的

六、实例-把我们的窗口绘制成红色(rgb:f00,在OpenGL中f就是1,0也是0,所以红色就是:rgb:100)

1、自定义WlGLSurfaceView类继承GLSurfaceView类:

package com.ywl5320.opengldemo;

import android.content.Context;
import android.opengl.GLSurfaceView;
import android.util.AttributeSet;

public class WlGLSurfaceView extends GLSurfaceView{

    public WlGLSurfaceView(Context context) {
        this(context, null);
    }

    public WlGLSurfaceView(Context context, AttributeSet attrs) {
        super(context, attrs);
        setEGLContextClientVersion(2);//设置opengl es版本为2.0
        setRenderer(new WlRender());//为glsurfaceview设置render
    }
}

2、实现Render接口:

package com.ywl5320.opengldemo;

import android.opengl.GLES20;
import android.opengl.GLSurfaceView;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

public class WlRender implements GLSurfaceView.Renderer{

    @Override
    public void onSurfaceCreated(GL10 gl, EGLConfig config) {

    }

    @Override
    public void onSurfaceChanged(GL10 gl, int width, int height) {
        GLES20.glViewport(0, 0, width, height);
    }

    @Override
    public void onDrawFrame(GL10 gl) {
        GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
        GLES20.glClearColor(1.0f, 0.0f, 0.0f, 1.0f);//用红色来清屏(屏幕将变成红色)
    }
}

3、Activity中布局调用:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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">

    <com.ywl5320.opengldemo.WlGLSurfaceView
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</android.support.constraint.ConstraintLayout>

4、运行结果:


好了,OpenGL ES介绍就到这里,实例下载地址:

GitHub:Android-OpenGL-ES