Training—Building an OpenGL ES Environment

阅读:https://developer.android.com/training/graphics/opengl/environment.html

 

接下来学学怎么使用OpenGL。

首先,在manifest里声明:

<uses-feature android:glEsVersion="0x00020000" android:required="true" />

还要声明支持,不支持的手机不会运行你的程序:

<supports-gl-texture android:name="GL_OES_compressed_ETC1_RGB8_texture" />
<supports-gl-texture android:name="GL_OES_compressed_paletted_texture" />

代码:

public class OpenGLES20 extends Activity {

    private GLSurfaceView mGLView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Create a GLSurfaceView instance and set it
        // as the ContentView for this Activity.
        mGLView = new MyGLSurfaceView(this);
        setContentView(mGLView);
    }
}

用的是GLSurfaceView ,当然这玩意要自己去写,下面是最简单的:

class MyGLSurfaceView extends GLSurfaceView {

    public MyGLSurfaceView(Context context){
        super(context);

        // Set the Renderer for drawing on the GLSurfaceView
        setRenderer(new MyRenderer());
    }
}

声明用的是GL 2.0 :setEGLContextClientVersion(2);

让系统知道只有当数据变化的时候才绘图:setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);

那接下来就是Renderer class这个东西了:

public class MyGL20Renderer implements GLSurfaceView.Renderer {

    public void onSurfaceCreated(GL10 unused, EGLConfig config) {
        // Set the background frame color
        GLES20.glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
    }

    public void onDrawFrame(GL10 unused) {
        // Redraw background color
        GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
    }

    public void onSurfaceChanged(GL10 unused, int width, int height) {
        GLES20.glViewport(0, 0, width, height);
    }
}
  • onSurfaceCreated() - Called once to set up the view's OpenGL ES environment.
  • onDrawFrame() - Called for each redraw of the view.
  • onSurfaceChanged() - Called if the geometry of the view changes, for example when the device's screen orientation changes.

有三个方法比较常用,上面的代码仅仅是最简单的东西,描绘了一个背景色为灰色的东西,没了。

整个文档比较长,由于个人没有GL基础,就不再深入,有兴趣的请前往官网阅读。

 

 

 

 

 

 

 

 

 

 

 

 

 

posted @ 2013-11-05 20:11  yutoulck  阅读(188)  评论(0编辑  收藏  举报