Android OpenGL ES 开发教程(7):创建实例应用OpenGLDemos程序框架

jerry OpenGL ES 2015年11月25日 收藏

有了前面关于Android OpenGL ES的介绍,可以开始创建示例程序OpenGLDemos。

使用Eclipse 创建一个Android项目

  • Project Name: OpenGLDemos
  • Build Target: Android 1.6 ( >1.5 即可)
  • Application Name: Android OpenGL ES Demos
  • Package Name: com.pstreets.opengl.demo
  • Create Activity:AndroidOpenGLDemo

采用Android ApiDemos 类似的方法,AndroidOpenGLDemo为一ListActivity ,可以使用PackageManager 读取所有Category为guidebee.intent.category.opengl.SAMPLE_CODE 的Activity。 Android ApiDemos示例解析(2): SimpleAdapter,ListActivity,PackageManager

创建一个OpenGLRenderer 实现 GLSurfaceView.Renderer接口:

  1. public class OpenGLRenderer implements Renderer {
  2.  
  3. private final IOpenGLDemo openGLDemo;
  4. public OpenGLRenderer(IOpenGLDemo demo){
  5. openGLDemo=demo;
  6. }
  7.  
  8. public void onSurfaceCreated(GL10 gl, EGLConfig config) {
  9. // Set the background color to black ( rgba ).
  10. gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
  11. // Enable Smooth Shading, default not really needed.
  12. gl.glShadeModel(GL10.GL_SMOOTH);
  13. // Depth buffer setup.
  14. gl.glClearDepthf(1.0f);
  15. // Enables depth testing.
  16. gl.glEnable(GL10.GL_DEPTH_TEST);
  17. // The type of depth testing to do.
  18. gl.glDepthFunc(GL10.GL_LEQUAL);
  19. // Really nice perspective calculations.
  20. gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT,
  21. GL10.GL_NICEST);
  22.  
  23. }
  24.  
  25. public void onDrawFrame(GL10 gl) {
  26. if(openGLDemo!=null){
  27. openGLDemo.DrawScene(gl);
  28. }
  29.  
  30. }
  31.  
  32. public void onSurfaceChanged(GL10 gl, int width, int height) {
  33. // Sets the current view port to the new size.
  34. gl.glViewport(0, 0, width, height);
  35. // Select the projection matrix
  36. gl.glMatrixMode(GL10.GL_PROJECTION);
  37. // Reset the projection matrix
  38. gl.glLoadIdentity();
  39. // Calculate the aspect ratio of the window
  40. GLU.gluPerspective(gl, 45.0f,
  41. (float) width / (float) height,
  42. 0.1f, 100.0f);
  43. // Select the modelview matrix
  44. gl.glMatrixMode(GL10.GL_MODELVIEW);
  45. // Reset the modelview matrix
  46. gl.glLoadIdentity();
  47. }
  48. }

为简洁起见,为所有的示例定义了一个接口IOpenGLDemo,

  1. public interface IOpenGLDemo {
  2. public void DrawScene(GL10 gl);
  3.  
  4. }

DrawScene 用于实际的GL绘图示例代码,其它的初始化工作基本就由GLSurfaceView 和OpenGLRenderer 完成,其中onSurfaceCreated 和 onSurfaceChanged 中的代码含义现在无需了解,后面会有具体介绍,只要知道它们是用来初始化GLSurfaceView就可以了。

最后使用一个简单的例子“Hello World?结束本篇,“Hello World” 使用红色背景刷新屏幕。

  1. public class HelloWorld extends Activity
  2. implements IOpenGLDemo{
  3.  
  4. /** Called when the activity is first created. */
  5. @Override
  6. public void onCreate(Bundle savedInstanceState) {
  7. super.onCreate(savedInstanceState);
  8. this.requestWindowFeature(Window.FEATURE_NO_TITLE);
  9. getWindow()
  10. .setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
  11. WindowManager.LayoutParams.FLAG_FULLSCREEN);
  12.  
  13. mGLSurfaceView = new GLSurfaceView(this);
  14. mGLSurfaceView.setRenderer(new OpenGLRenderer(this));
  15. setContentView(mGLSurfaceView);
  16. }
  17.  
  18. public void DrawScene(GL10 gl) {
  19. gl.glClearColor(1.0f, 0.0f, 0.0f, 0.0f);
  20. // Clears the screen and depth buffer.
  21. gl.glClear(GL10.GL_COLOR_BUFFER_BIT
  22. | GL10.GL_DEPTH_BUFFER_BIT);
  23.  
  24. }
  25.  
  26. @Override
  27. protected void onResume() {
  28. // Ideally a game should implement
  29. // onResume() and onPause()
  30. // to take appropriate action when the
  31. //activity looses focus
  32. super.onResume();
  33. mGLSurfaceView.onResume();
  34. }
  35.  
  36. @Override
  37. protected void onPause() {
  38. // Ideally a game should implement onResume()
  39. //and onPause()
  40. // to take appropriate action when the
  41. //activity looses focus
  42. super.onPause();
  43. mGLSurfaceView.onPause();
  44. }
  45.  
  46. private GLSurfaceView mGLSurfaceView;
  47.  
  48. }

其对应在AndroidManifest.xml中的定义如下:

<activity android:name=?.HelloWorld? android:label=?@string/activity_helloworld?>
<intent-filter>
<action android:name=?android.intent.action.MAIN? />
<category android:name=?guidebee.intent.category.opengl.SAMPLE_CODE? />
</intent-filter>
</activity>

下载