Android OpenGL ES 绘图 --简单绘图

来源:互联网 发布:java模拟器安卓版7.0 编辑:程序博客网 时间:2024/04/30 01:21

使用OpenGL 进行最简单绘图,只是把显示区域渲染成蓝色即可

一、创建GLSurfaceView

GLSurfaceView 与一般View 差不多,用法基本相同,可以在XML中创建,也可能在代码中创建,本例为了方便,使用直接在代码中创建

mGlSurfaceView = new GLSurfaceView(this);setContentView(mGlSurfaceView);

二、创建Renderer

新建一个类,继承并实现GLSurfaceView.Renderer接口的三个方法,三个方法具体作用在之前文章中已说明

 @Override    public void onSurfaceCreated(GL10 gl, EGLConfig config) {        gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f);        // Enable Smooth Shading, default not really needed.        gl.glShadeModel(GL10.GL_SMOOTH);        // Depth buffer setup.        gl.glClearDepthf(1.0f);        // Enables depth testing.        gl.glEnable(GL10.GL_DEPTH_TEST);        // The type of depth testing to do.        gl.glDepthFunc(GL10.GL_LEQUAL);        // Really nice perspective calculations.        gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT,                GL10.GL_NICEST);    }    @Override    public void onSurfaceChanged(GL10 gl, int width, int height) {        // Sets the current view port to the new size.        gl.glViewport(0, 0, width, height);        // Select the projection matrix        gl.glMatrixMode(GL10.GL_PROJECTION);        // Reset the projection matrix        gl.glLoadIdentity();// OpenGL docs.        // Select the modelview matrix        gl.glMatrixMode(GL10.GL_MODELVIEW);        // Reset the modelview matrix        gl.glLoadIdentity();    }    float angle = 90f;    @Override    public void onDrawFrame(GL10 gl) {        // 清除屏幕和深度缓存        gl.glClear(GL10.GL_COLOR_BUFFER_BIT |                GL10.GL_DEPTH_BUFFER_BIT);        // 设置颜色        gl.glClearColor(0.0f, 0.0f, 1.0f, 0.5f);    }

在onSurfaceCreated和onSurfaceChanged中的代码如尚未了解,可暂时不管,把它视为绘图的准备工作即可

三、结合GLSurfaceView与Renderer,使之绘画

        BaseRenderer renderer = new BaseRenderer();        mGlSurfaceView.setRenderer(renderer);        mGlSurfaceView.setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY);

OpenGL有两种绘制模式,分别为:

  • GLSurfaceView.RENDERMODE_WHEN_DIRTY //通过GLSurfaceView.requestRender() 请求更新UI
  • GLSurfaceView.RENDERMODE_CONTINUOUSLY //不断更新UI