OpenglES2.0 for Android:第一个OpenglES应用
首先我们新建一个Android工程:com.opengl.openglestest
打开MainActivity,定义一个GLSurfaceView对象,GLSurfaceView是SurfaceView视图的子类,GLSurfaceView主要用于管理着色器(Render)。
然后调用其setEGLContextClientVersion方法选择OpenglES2.0的上下文,最后添加着色器以及setContentView,代码如下(MainActivity.java ):
<span style="font-size:14px;">package com.cumt.openglestwo_test_one;
import android.app.Activity;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
public class MainActivity extends Activity {
private GLSurfaceView glSurfaceView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
glSurfaceView = new GLSurfaceView(this);
glSurfaceView.setEGLContextClientVersion(2); // Pick an OpenGL ES 2.0 context.
glSurfaceView.setRenderer(new MyRender());
setContentView(glSurfaceView);
}
}</span>
最后还要注意GLSurfaceView的生命周期,完整代码如下(MainActivity.java):
<span style="font-size:14px;">package com.cumt.openglestwo_test_one;
import com.cumt.render.MyRender;
import android.app.Activity;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
public class MainActivity extends Activity {
private GLSurfaceView glSurfaceView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
glSurfaceView = new GLSurfaceView(this);
glSurfaceView.setEGLContextClientVersion(2); // Pick an OpenGL ES 2.0 context.
glSurfaceView.setRenderer(new MyRender());
setContentView(glSurfaceView);
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
glSurfaceView.onPause();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
glSurfaceView.onResume();
}
}</span>
这里我们还要自己实现着色器程序:
新建一个包render,在该包下新建类MyRender引入Renderer接口。此时代码如下(MyRender.java):
<span style="font-size:14px;">package com.cumt.render;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import android.opengl.GLSurfaceView.Renderer;
public class MyRender implements Renderer {
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
// TODO Auto-generated method stub
}
public void onSurfaceChanged(GL10 gl, int width, int height) {
// TODO Auto-generated method stub
}
public void onDrawFrame(GL10 gl) {
// TODO Auto-generated method stub
}
}</span>
第一个函数onSurfaceCreated当Surface被绘制或者重新绘制的时候调用,我们常常在该类中定义物体对象,纹理对象等。
第二个函数onSurfaceChanged当Surface改变size时调用或者一开始在onSurfaceCreated调用之后被调用。我们常常在该类中设置相机,投影等。
第三个函数onDrawFrame当物体被绘制时调用,此函数一直处于被调用状态。
我们来测试一下调用过程:代码如下(MyRender.java):
<span style="font-size:14px;">package com.cumt.render;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import android.opengl.GLSurfaceView.Renderer;
import android.util.Log;
public class MyRender implements Renderer {
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
// TODO Auto-generated method stub
Log.w("render","onSurfaceCreated");
}
public void onSurfaceChanged(GL10 gl, int width, int height) {
// TODO Auto-generated method stub
Log.w("render","onSurfaceChanged");
}
public void onDrawFrame(GL10 gl) {
// TODO Auto-generated method stub
Log.w("render","onDrawFrame");
}
}</span>
Logcat输出结果:
此时运行一下发现整个手机屏幕都是黑色的,看不到任何东西,下面我们来设置一个颜色(MyRender.java):
<span style="font-size:14px;">package com.cumt.render;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import android.opengl.GLSurfaceView.Renderer;
import static android.opengl.GLES20.glClear;
import static android.opengl.GLES20.glClearColor;
import static android.opengl.GLES20.glViewport;
import static android.opengl.GLES20.GL_COLOR_BUFFER_BIT;
public class MyRender implements Renderer {
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
// TODO Auto-generated method stub
//First:设置清空屏幕用的颜色,前三个参数对应红绿蓝,最后一个对应alpha
glClearColor(1.0f, 0.0f, 0.0f, 0.0f);
}
public void onSurfaceChanged(GL10 gl, int width, int height) {
// TODO Auto-generated method stub
//Second:设置视口尺寸,即告诉opengl可以用来渲染的surface大小
glViewport(0,0,width,height);
}
public void onDrawFrame(GL10 gl) {
// TODO Auto-generated method stub
//Third:清空屏幕,擦除屏幕上所有的颜色,并用之前glClearColor定义的颜色填充整个屏幕
glClear(GL_COLOR_BUFFER_BIT);
}
}</span>
运行效果:
至此第一个opengl es for android的小demo以及完成了。