认识GLSurfaceView & Renderer
public void setRenderer(GLSurfaceView.Renderer renderer) 提供了渲染方法
7 GLSurfaceView.Renderer
普通的渲染接口,当你创建自己的render时需要实现这个接口同时重写一些方法。然后通过GLSurfaceView的setRenderer(GLSurfaceView.Renderer)将自己的render渲染到GLSurfaceView中,实现GLSurfaceView.Renderer时,你需要实现以下三个方法:
该方法在渲染开始前调用,OpenGL ES 的绘制上下文被重建 时也会被调用。当 activity 暂停时绘制上下文会丢失,当 activity 继续 时,绘制上下文会被重建。通俗的来说在渲染的过程中一些不经常变换的,可以在这个方法里面设置,比方说:创建长期存在的 OpenGL 资源(如 texture)往往也在这里进行。
通常这个方法中就是实际绘画的地方,每帧都通过该方法进行绘制。绘制时通常先调用 glClear 函数来清空 framebuffer,然后在调用 OpenGL ES 的起它的接口进行绘制。
surface 的尺寸发生改变时该方法被调用(横竖屏切换时)。往往在 这里设置 viewport。若你的 camera 是固定的,也可以在这里设置 camera。
8 应用示例
了解完GLSurfaceView和GLSurfaceView.Render之后,接下来我们开始实际操刀:
package se.jayway.opengl.tutorial; import android.app.Activity; import android.opengl.GLSurfaceView; import android.os.Bundle; public class TutorialPartI extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); GLSurfaceView view = new GLSurfaceView(this); view.setRenderer(new OpenGLRenderer()); setContentView(view); } }
package se.jayway.opengl.tutorial; import javax.microedition.khronos.egl.EGLConfig; import javax.microedition.khronos.opengles.GL10; import android.opengl.GLU; import android.opengl.GLSurfaceView.Renderer; public class OpenGLRenderer implements Renderer { /* * @see * android.opengl.GLSurfaceView.Renderer#onSurfaceCreated(javax. * microedition.khronos.opengles.GL10, javax.microedition.khronos. * egl.EGLConfig) */ public void onSurfaceCreated(GL10 gl, EGLConfig config) { // Set the background color to black ( rgba ). gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f); // OpenGL docs. // Enable Smooth Shading, default not really needed. gl.glShadeModel(GL10.GL_SMOOTH);// OpenGL docs. // Depth buffer setup. gl.glClearDepthf(1.0f);// OpenGL docs. // Enables depth testing. gl.glEnable(GL10.GL_DEPTH_TEST);// OpenGL docs. // The type of depth testing to do. gl.glDepthFunc(GL10.GL_LEQUAL);// OpenGL docs. // Really nice perspective calculations. gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, // OpenGL docs. GL10.GL_NICEST); } /* * @see * android.opengl.GLSurfaceView.Renderer#onDrawFrame(javax. * microedition.khronos.opengles.GL10) */ public void onDrawFrame(GL10 gl) { // Clears the screen and depth buffer. gl.glClear(GL10.GL_COLOR_BUFFER_BIT | // OpenGL docs. GL10.GL_DEPTH_BUFFER_BIT); } /* * @see * android.opengl.GLSurfaceView.Renderer#onSurfaceChanged(javax. * microedition.khronos.opengles.GL10, int, int) */ public void onSurfaceChanged(GL10 gl, int width, int height) { // Sets the current view port to the new size. gl.glViewport(0, 0, width, height);// OpenGL docs. // Select the projection matrix gl.glMatrixMode(GL10.GL_PROJECTION);// OpenGL docs. // Reset the projection matrix gl.glLoadIdentity();// OpenGL docs. // Calculate the aspect ratio of the window GLU.gluPerspective(gl, 45.0f, (float) width / (float) height, 0.1f, 100.0f); // Select the modelview matrix gl.glMatrixMode(GL10.GL_MODELVIEW);// OpenGL docs. // Reset the modelview matrix gl.glLoadIdentity();// OpenGL docs. } }