
I found this book and shared it with you. Link here
package com.example.android.apis.graphics; import javax.microedition.khronos.egl.EGLConfig; import javax.microedition.khronos.opengles.GL10; import android.app.Activity; import android.opengl.GLSurfaceView; import android.os.Bundle; public class ClearActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mGLView = new GLSurfaceView(this); mGLView.setRenderer(new ClearRenderer()); setContentView(mGLView); } @Override protected void onPause() { super.onPause(); mGLView.onPause(); } @Override protected void onResume() { super.onResume(); mGLView.onResume(); } private GLSurfaceView mGLView; } class ClearRenderer implements GLSurfaceView.Renderer { public void onSurfaceCreated(GL10 gl, EGLConfig config) { // Do nothing special. } public void onSurfaceChanged(GL10 gl, int w, int h) { gl.glViewport(0, 0, w, h); } public void onDrawFrame(GL10 gl) { gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT); } } ClearRenderer.onDrawFrame.
GLSurfaceView.Renderinterface có ba phương thức:
onSurfaceCreated()được gọi để bắt đầu rendering, và bất cứ khi nào OpenGL ES đang vẽ ngữ cảnh (drawing context) phải khởi tạo lại. (Việc vẽ ngữ cảnh là thường mất và khở tạo lại khi Activity paused và re sumed).
OnSurfaceCreated()là một nơi tốt để tạo ra tài nguyên OpenGL giống kết cấu.
onSurfaceChanged()được gọi khi bở mặt thay đổi kích thước. Nó là một nơi tốt để thiết lập Viewport OpenGL của bạn. Bạn có thể cũng thiết lập camera ở đây, nếu nó là cố định camera thì sẽ không di chuyển xung quanh khung cảnh.
onDrawFrame()được gọi ở mỗi frame, và có trách nhiệm cho việc vẽ quang cảnh. Bạn sẽ bắt đầu gọi
glClearđể xóa framebuffer, được theo ở OpenGL ES khác gọi để vẽ quang cảnh hiện tại
package com.google.android.ClearTest; import javax.microedition.khronos.egl.EGLConfig; import javax.microedition.khronos.opengles.GL10; import android.app.Activity; import android.content.Context; import android.opengl.GLSurfaceView; import android.os.Bundle; import android.view.MotionEvent; public class ClearActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mGLView = new ClearGLSurfaceView(this); setContentView(mGLView); } @Override protected void onPause() { super.onPause(); mGLView.onPause(); } @Override protected void onResume() { super.onResume(); mGLView.onResume(); } private GLSurfaceView mGLView; } class ClearGLSurfaceView extends GLSurfaceView { public ClearGLSurfaceView(Context context) { super(context); mRenderer = new ClearRenderer(); setRenderer(mRenderer); } public boolean onTouchEvent(final MotionEvent event) { queueEvent(new Runnable(){ public void run() { mRenderer.setColor(event.getX() / getWidth(), event.getY() / getHeight(), 1.0f); }}); return true; } ClearRenderer mRenderer; } class ClearRenderer implements GLSurfaceView.Renderer { public void onSurfaceCreated(GL10 gl, EGLConfig config) { // Do nothing special. } public void onSurfaceChanged(GL10 gl, int w, int h) { gl.glViewport(0, 0, w, h); } public void onDrawFrame(GL10 gl) { gl.glClearColor(mRed, mGreen, mBlue, 1.0f); gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT); } public void setColor(float r, float g, float b) { mRed = r; mGreen = g; mBlue = b; } private float mRed; private float mGreen; private float mBlue; } | Trích từ Android blog setEGLConfigChooser(boolean needDepth) Choose a config that's closest to R5G6B5 with or without a 16-bit framebuffer setEGLConfigChooser(int redSize, int greenSize,int blueSize, int alphaSize,int depthSize, int stencilSize) Choose the config with the fewest number of bits per pixel that has at least as many bits-per-channel as specified in the constructor. setEGLConfigChooser(EGLConfigChooser configChooser) Allow total control over choosing a configuration. You pass in your own implementation of EGLConfigChooser, which gets to inspect the device's capabilities and choose a configuration. |
public ClearGLSurfaceView(Context context) { super(context); // Turn on error-checking and logging setDebugFlags(DEBUG_CHECK_GL_ERROR | DEBUG_LOG_GL_CALLS); mRenderer = new ClearRenderer(); setRenderer(mRenderer); }