本篇博客以代码为本,手把手教您建立第一个openGL的android项目。
现在看来没有多少代码:
/Users/fanyu/Desktop/FirstOpenGLProject/app/src/main/java/com/van/fanyu/firstopenglproject/MainActivity.java
package com.van.fanyu.firstopenglproject;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
给这个activity添加一个GLSurfaceView实例,这样就可以初始化OpenGL。
/Users/fanyu/Desktop/FirstOpenGLProject/app/src/main/java/com/van/fanyu/firstopenglproject/MainActivity.java
public class MainActivity extends AppCompatActivity {
private GLSurfaceView glSurfaceView;
private boolean rendererSet = false;
移除setContentView(),并初始化glSurfaceView。
/Users/fanyu/Desktop/FirstOpenGLProject/app/src/main/java/com/van/fanyu/firstopenglproject/MainActivity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
glSurfaceView = new GLSurfaceView(this);
/Users/fanyu/Desktop/FirstOpenGLProject/app/src/main/java/com/van/fanyu/firstopenglproject/MainActivity.java
//检查系统是否支持OpenGL2.0
final ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
final boolean supportsEs2 = configurationInfo.reqGlEsVersion>=0x20000;
/Users/fanyu/Desktop/FirstOpenGLProject/app/src/main/java/com/van/fanyu/firstopenglproject/MainActivity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
……
if (supportsEs2){
glSurfaceView.setEGLContextClientVersion(2);
glSurfaceView.setRenderer(new FirstOpenGLProjectRender());
}else {
Toast.makeText(this,"This device does not support OpenGL ES 2.0",Toast.LENGTH_LONG).show();
return;
}
setContentView(glSurfaceView);
以下代码比较重要,否则程序会崩溃:
/Users/fanyu/Desktop/FirstOpenGLProject/app/src/main/java/com/van/fanyu/firstopenglproject/MainActivity.java
@Override
protected void onPause() {
super.onPause();
if (rendererSet){
glSurfaceView.onPause();
}
}
@Override
protected void onResume() {
super.onResume();
if (rendererSet){
glSurfaceView.onResume();
}
}
在同一个包里创建一个FirstOpenGLProjectRender类,用来实现Renderer接口。
/Users/fanyu/Desktop/FirstOpenGLProject/app/src/main/java/com/van/fanyu/firstopenglproject/FirstOpenGLProjectRender.java
package com.van.fanyu.firstopenglproject;
import android.opengl.GLSurfaceView;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import static android.opengl.GLES20.GL_COLOR_BUFFER_BIT;
import static android.opengl.GLES20.glClear;
import static android.opengl.GLES20.glClearColor;
import static android.opengl.GLES20.glViewport;
/** * Created by fanyu on 16/2/21. */
public class FirstOpenGLProjectRender implements GLSurfaceView.Renderer {
//类似于activity的onCreate方法
@Override
public void onSurfaceCreated(GL10 glUnused, EGLConfig config) {
//设置清空屏幕用的颜色
//前三个参数分别对应红色、绿色和蓝色。最后一个参数被称为alpha,用来表示透明度。
glClearColor(1.0f,0.0f,0.0f,0.0f);
}
//屏幕尺寸变化时调用此方法
@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
//设置GLSurfaceView显示出来的尺寸
glViewport(0,0,width,height);
}
//当绘制一帧时,这个方法会被调用。在这个方法中,一定要绘制一些东西,即使只是清空屏幕,否则会显示混乱。
@Override
public void onDrawFrame(GL10 gl) {
//此操作会清空屏幕所有颜色,并用之前glClearColor()所定义的颜色填充整个屏幕
glClear(GL_COLOR_BUFFER_BIT);
}
}
本项目初始化了OpenGL、响应了Android Activity的生命周期,最后清空了屏幕。