OpenglES2.0 for Android:第一个OpenglES应用
<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>
<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><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><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输出结果:
<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>运行效果:
1454



被折叠的 条评论
为什么被折叠?



