OpenGL入门

官网:https://developer.android.com/guide/topics/graphics/opengl.html

简介

Android开发中通过Open Graphics Library(开放式图形库 ),也就是OpenGL支持2D与3D图像的开发,其中涉及的关键点就是OpenGL ES API。OpenGL 具有跨平台性的图形处理的特性,是3D图形处理的一个典型的API。OpenGL ES(OpenGL for Embedded Systems)是OpenGL三维图形API的子集,针对手机、PDA和游戏主机等嵌入式设备而设计。Android支持以下版本的OpenGL ES API:

  • OpenGL ES 1.0 and 1.1 - This API specification is supported by Android 1.0 and higher.
  • OpenGL ES 2.0 - This API specification is supported by Android 2.2 (API level 8) and higher.
  • OpenGL ES 3.0 - This API specification is supported by Android 4.3 (API level 18) and higher.
  • OpenGL ES 3.1 - This API specification is supported by Android 5.0 (API level 21) and higher.

需要注意的是:设备上对OpenGL ES 3.0API的支持,需要由设备商提供图形流水线的实现。也就是说运行Android4.3或者更高系统的设备有可能是不支持这个API的,所以需要在开发之前检查这个设备的OpenGL ES的版本。

private static double glVersion = 3.0;

private static class ContextFactory implements GLSurfaceView.EGLContextFactory {

  private static int EGL_CONTEXT_CLIENT_VERSION = 0x3098;

  public EGLContext createContext(
          EGL10 egl, EGLDisplay display, EGLConfig eglConfig) {

      Log.w(TAG, "creating OpenGL ES " + glVersion + " context");
      int[] attrib_list = {EGL_CONTEXT_CLIENT_VERSION, (int) glVersion,
              EGL10.EGL_NONE };
      // 创建EGLContext对象
      EGLContext context = egl.eglCreateContext(
              display, eglConfig, EGL10.EGL_NO_CONTEXT, attrib_list);
      return context; // 如果返回的是null说明不支持3.0;
  }
}

基础

在OpenGL ESAPI中有两个基础类,我们需要首先认识和理解这两个类。

  1. GLSurfaceView
    这里写图片描述

这是一个View,类似于一个SurfaceView(其实它就是SurfaceView的子类),你可以通过与Renderer一起使用来创建它的实例。如果你需要捕捉触屏事件,需要创建一个类继承GLSurfaceView实现其监听的接口

2.GLSurfaceView.Renderer
这里写图片描述

可以看到这是一个接口,创建GLSurfaceView,你需要创建一个类实现该接口,然后通过GLSurfaceView的setRenderer()方法与GLSurfaceView联系起来。
该接口里面有三个方法:

下面是一个非常简单的例子:效果是一个红色的背景

public class OpenGLActivity extends AppCompatActivity {

    private static double glVersion = 3.0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        GLSurfaceView view = new GLSurfaceView(this);
        view.setRenderer(new OpenGLRenderer());
        setContentView(view);
    }


    private class OpenGLRenderer implements GLSurfaceView.Renderer {

        @Override
            public void onSurfaceCreated(GL10 gl10, EGLConfig eglConfig) {

            gl10.glClearColor(1.0f,0.0f,0.0f,0.0f);  // 设置背景颜色(rgba),取值范围是0-1
            gl10.glShadeModel(GL10.GL_SMOOTH);  // 选择恒定(GL10.GL_FLAT)或者光滑着色模式(GL10.GL_SMOOTH)
            gl10.glClearDepthf(1.0f); // 指明深度缓冲区的清理值,并通过glClear()方法清理深度缓冲区,取值范围为0-1,初始值为1
            gl10.glEnable(GL10.GL_DEPTH_TEST); // 启用服务端的GL功能。  GL10.GL_DEPTH_TEST:如果启用,做深度比较和更新深度缓存
            gl10.glDepthFunc(GL10.GL_LEQUAL); //
            gl10.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT,GL10.GL_NICEST); // 控制GL某些行为。
                                                                             // GL_NICEST:选择正确或者质量最好的选项。
                                                                             // GL_PERSPECTIVE_CORRECTION_HINT:表明颜色和纹理坐标插值的效果
        }

        @Override
        public void onSurfaceChanged(GL10 gl10, int width, int height) {

            gl10.glViewport(0,0,width,height); // 设置一个视口
                                                /*x——指明视口矩形的左下角x坐标,初始值为0。
                                                  y——指明视口矩形的左下角y坐标,初始值为0。
                                                  width——指明视口的宽,如果GL上下文首次附于一个surface则宽、高为这个surface大小。
                                                  height——指明视口的高,如果GL上下文首次附于一个surface则宽、高为这个surface大小。*/
            gl10.glMatrixMode(GL10.GL_PROJECTION); // 设置当前矩阵模式
                                                    // GL_PROJECTION——应用投射矩阵堆的后续矩阵操作。
            gl10.glLoadIdentity();  // 用特征矩阵代替当前矩阵。
            GLU.gluPerspective(gl10,45.0f,(float)width / (float)height,0.1f,100.0f); // Set up a perspective projection matrix

                    /*Parameters
                    gl  a GL10 interface
                    fovy  specifies the field of view angle, in degrees, in the Y direction.
                    aspect  specifies the aspect ration that determins the field of view in the x direction. The aspect ratio is the ratio of x (width) to y (height).
                    zNear  specifies the distance from the viewer to the near clipping plane (always positive).
                    zFar  specifies the distance from the viewer to the far clipping plane (always positive).*/

            gl10.glMatrixMode(GL10.GL_MODELVIEW); // 应用视图矩阵堆的后续矩阵操作。
            gl10.glLoadIdentity();

        }

        @Override
        public void onDrawFrame(GL10 gl10) {

            gl10.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT); // 清理缓冲区,并设置为预设值。
                                                                               // GL_COLOR_BUFFER_BIT:表明颜色缓冲区
                                                                               // GL_DEPTH_BUFFER_BIT:表明深度缓冲区
        }
    }
}

代码下载链接:

https://github.com/upperLucky/OpenGLDemo

对了,这里关于GL10函数的方法解析,推荐一篇文章:

http://blog.csdn.net/liujianminghero/article/details/6730090

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值