public class GlsurfaceviewActivity extends Activity {
private myGLSurfaceView mGLSurfaceView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mGLSurfaceView = new myGLSurfaceView(this);
setContentView(mGLSurfaceView);
//setContentView(R.layout.main);
}
}
public class myGLSurfaceView extends GLSurfaceView {
private myGLRenderer mrender;
public myGLSurfaceView(Context context) {
super(context);
// TODO Auto-generated constructor stub
mrender = new myGLRenderer();
setRenderer(mrender); //指定mrender对象,该mrender对象将会完成GLSurfaceView里3D图形的绘制。
}
}
public class myGLRenderer implements Renderer {//实例化opengl.GLSurfaceView.Renderer需要实现3个方法,
public void onDrawFrame(GL10 gl) ;绘制GLSurfaceView的当前帧。
public void onSurfaceChanged(GL10 gl, int width, int height) :GL10就是画笔,当GLSurfaceView大小改变时回调。
public void onSurfaceCreated(GL10 gl, EGLConfig config) :当GLSurfaceView创建时回调。
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
// TODO Auto-generated method stub
loadGif("data/app/girl.gif");
gl.glEnable(GL11.GL_DITHER);//启用抗抖动
gl.glDisable(GL11.GL_LIGHTING);
//设置系统对某方面的修正,这里对透视修正。
gl.glHint(GL10.GL_PERSPCTIVE_CORRECTION_HINT,gGL10.GL_FASTEST);
// Enable textures.启用纹理贴图
gl.glEnable(GL11.GL_TEXTURE_2D);
gl.glTexEnvf(GL11.GL_TEXTURE_ENV, GL11.GL_TEXTURE_ENV_COLOR, GL11.GL_REPLACE);
// Set up state for multitexture operations. Since multitexture is
// currently used
// only for layered crossfades the needed state can be factored out into
// one-time
// initialization. This section may need to be folded into drawMixed2D()
// if multitexture
// is used for other effects.
// Enable Vertex Arrays
gl.glEnableClientState(GL11.GL_VERTEX_ARRAY);
// Enable depth test.
gl.glEnable(GL11.GL_DEPTH_TEST);
gl.glDepthFunc(GL11.GL_LEQUAL);
// Set the blend function for premultiplied alpha.
gl.glEnable(GL10.GL_BLEND);
gl.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
// Set the background color.
gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
gl.glClear(GL11.GL_COLOR_BUFFER_BIT);
}
public void onSurfaceChanged(GL10 gl, int width, int height) {
// TODO Auto-generated method stub
final float zNear = 0.1f;
final float zFar = 100.0f;
gl.glViewport(0, 0, width, height);
gl.glMatrixMode(GL11.GL_PROJECTION);
gl.glLoadIdentity();
GLU.gluPerspective(gl, 45.0f, (float) width / height, zNear, zFar);
gl.glMatrixMode(GL11.GL_MODELVIEW);
}