• OpenGL ES入门教程(一)编写第一个OpenGL程序


    OpenGL ES入门教程(一)编写第一个OpenGL程序

    前言

    从本文开始我将参考学习OpenGL ES应用开发实践指南 Android卷 [(美)KevinBrothaler著](提取码: 394m),并基于自己的理解以更加通俗易懂的方式讲解如何应用OpenGL ES。本系列教程的目标是应用OpenGL,所以不会涉及太多的理论知识,主要讲解方式是基于简单功能的代码拆解,学会对OpenGL ES的应用。原著文章的代码都是在eclipse工具实现,本系列教程采用Android studio工具进行实现。

    既然你都看到这篇文章了,想必已经知道什么是OpenGL ES了,我也就不做赘述,本篇教你编写第一个Android上的OpenGL程序,万事开头难,但头开了,就成功了一半了。

    文章内容都是个人理解,必然存在表述不专业甚至错误的情况,如有错误,还请各位博友积极指出,感谢。

    1. 新建android项目

    新建一个空的项目,默认Activity的代码框架如下:

    public class MainActivity extends AppCompatActivity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
             super.onCreate(savedInstanceState);
             setContentView(R.layout.activity_main);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    2. 初始化OpenGL

    上面我们显示的页面是xml布局的页面,如果采用OpenGL绘制页面,就需要用OpenGL自己的载体,而不必采用xml,OpenGL绘制的载体是GLSurfaceView类,首先初始化该类,示例代码如下:

    public class MainActivity extends AppCompatActivity {
        private GLSurfaceView mGLSurfaceView;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            mGLSurfaceView = new GLSurfaceView(this);
            setContentView(mGLSurfaceView);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    3. 判断是否支持OpenGL ES 2.0

    我们采用OpenGL ES 2.0的接口,因此在程序运行前需要先判断是否支持,完善是否支持OpenGL ES 2.0后的代码如下:

    public class MainActivity extends AppCompatActivity {
        private GLSurfaceView mGLSurfaceView;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            mGLSurfaceView = new GLSurfaceView(this);
    
            // Check if the system supports OpenGL ES 2.0.
            ActivityManager activityManager =
                    (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
            ConfigurationInfo configurationInfo = activityManager
                    .getDeviceConfigurationInfo();
                    
            final boolean supportsEs2 = configurationInfo.reqGlEsVersion >= 0x20000
            if (supportsEs2)
            {
                //设置渲染器
                //...
            }
            else
            {
                Toast.makeText(this, "This device does not support OpenGL ES 2.0.",
                        Toast.LENGTH_LONG).show();
                return;
            }
    
            setContentView(mGLSurfaceView);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    4. 编写渲染类,并可视化OpenGL

    OpenGL的绘制操作都在渲染类GLSurfaceView.Renderer中,因此,我们需要自定义一个类继承自GLSurfaceView.Renderer,然后编写绘制操作,主要重写三个方法,实现绘制红色屏幕的示例代码如下:

    public class AirHockeyRenderer implements GLSurfaceView.Renderer {
        @Override
        public void onSurfaceCreated(GL10 glUnused, EGLConfig config) {
            glClearColor(1.0f, 0.0f, 0.0f, 0.0f);//设置清除背景颜色为红色,即调用glClear方法时,背景颜色设置为红色
        }
    
        @Override
        public void onSurfaceChanged(GL10 glUnused, int width, int height) {
            // Set the OpenGL viewport to fill the entire surface.
            glViewport(0, 0, width, height);
        }
    
        @Override
        public void onDrawFrame(GL10 glUnused) {
            // Clear the rendering surface.
            glClear(GL_COLOR_BUFFER_BIT);
        }
    }
    
    public class MainActivity extends AppCompatActivity {
        private GLSurfaceView mGLSurfaceView;
        private boolean mIsRendererSet=false;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            mGLSurfaceView = new GLSurfaceView(this);
    
            // Check if the system supports OpenGL ES 2.0.
            ActivityManager activityManager =
                    (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
            ConfigurationInfo configurationInfo = activityManager
                    .getDeviceConfigurationInfo();
    
            final boolean supportsEs2 = configurationInfo.reqGlEsVersion >= 0x20000;
            if (supportsEs2)
            {
                mGLSurfaceView.setEGLContextClientVersion(2);//设置OpenGL版本为2.0
                mGLSurfaceView.setRenderer(new AirHockeyRenderer(this));//设置渲染类
                //GLSurfaceView也具有类似Activity的生命周期,
                //需要在Activity对应的生命周期,执行GLSurfaceView的生命周期,避免一些奇怪的bug。
                mIsRendererSet = true;
            }
            else
            {
                Toast.makeText(this, "This device does not support OpenGL ES 2.0.",
                        Toast.LENGTH_LONG).show();
                return;
            }
    
            setContentView(mGLSurfaceView);
        }
    
        @Override
        protected void onPause() {
            super.onPause();
            if (mIsRendererSet)
            {
                mGLSurfaceView.onPause();
            }
        }
    
        @Override
        protected void onResume() {
            super.onResume();
    
            if (mIsRendererSet)
            {
                mGLSurfaceView.onResume();
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72

    5. 程序运行效果

    在这里插入图片描述

  • 相关阅读:
    COI实验室技能——自动切换显示器画面以及实现采集大量实验数据的方法(采集数据可供深度学习训练)
    文件系统考古2:1984 - BSD Fast Filing System
    【web-攻击用户】(9.4)跨域捕获数据——通过注入HTML捕获数据、注入CSS捕获数据、JavaScript劫持
    Flask框架学习:蓝图的使用
    焊死,这38条命令还不会?难怪你的Windows那么费劲
    01-http概述
    零依赖监控解决方案:TDengine+Grafana落地实施
    【博学谷学习记录】超强总结,用心分享|架构师-Kafka优化手段
    HTML5的新特性有哪些?
    Pr 入门系列之四:编辑(基础篇)
  • 原文地址:https://blog.csdn.net/qq_34720818/article/details/134150303