OpenGL Learning (1) Basic Overview

(1) Rewrite Rendar (renderer), rewrite three methods:

A, onSurfaceCreated(GL10 GL, EGLConfig config); B, onSurfaceChanged(GL10 GL, int width, int height) C, onDrawFrame (GL10 gl);Copy the code
package com.example.mychapter2; import javax.microedition.khronos.egl.EGLConfig; import javax.microedition.khronos.opengles.GL10; import android.opengl.GLU; import android.opengl.GLSurfaceView.Renderer; public class GLRender implements Renderer { public void onDrawFrame(GL10 gl) { // TODO Auto-generated method stub // First clear the screen gl. GlClear (GL10 GL_COLOR_BUFFER_BIT | GL10. GL_DEPTH_BUFFER_BIT); Gl.glmatrixmode (gl10.gl_modelView); // reset the matrix gl.glloadidEntity (); Glu. gluLookAt(gl, 0, 0, 3, 0, 0, 0, 0, 1, 0); / /... } public void onSurfaceChanged(GL10 gl, int width, int height) { // TODO Auto-generated method stub float ratio = (float) width / height; // Set the viewport (OpenGL scene size) gl.glviewPort (0, 0, width, height); // Set the projection matrix to perspective projection gl.glMatrixMode(gl10.gl_projection); Gl.glloadidentity (); // Reset projection matrix (set to identity matrix) gl.glloadidEntity (); Gl.glfrustumf (-ratio, ratio, -1, 1, 1, 10); // Create a perspective projection matrix (set viewport size) gl.glfrustumf (-ratio, ratio, -1, 1, 1, 10); } public void onSurfaceCreated(GL10 gl, Gl. glHint(gl10.gl_correction_hint) {// TODO auto-generated method stub GL10.GL_NICEST); // Set the clean screen color to gl.glclearColor (0, 0, 0, 1); Gl.glenable (gl10.gl_depth_test); }}Copy the code

Create a GLSurfaceView

(a) create a GLSurfaceView instance mGLSurfaceView, (b) set the renderer mGLSurfaceView. SetRenderer (Render);

package com.example.mychapter2; import android.app.Activity; import android.opengl.GLSurfaceView; import android.os.Bundle; public class ChapterMain extends Activity { private GLSurfaceView mGLSurfaceView; /** Called when the activity is first created. */ public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); MGLSurfaceView = new GLSurfaceView(this); / / set the renderer mGLSurfaceView. SetRenderer (new GLRender ()); setContentView(mGLSurfaceView); } protected void onResume() { super.onResume(); mGLSurfaceView.onResume(); } protected void onPause() { super.onPause(); mGLSurfaceView.onPause(); }}Copy the code