Android OpenGL ES 开发教程(9):绘制点Point

jerry OpenGL ES 2015年11月25日 收藏

上一篇介绍了OpenGL ES能够绘制的几种基本几何图形:点,线,三角形。将分别介绍这几种基本几何图形的例子。为方便起见,暂时在同一平面上绘制这些几何图形,在后面介绍完OpenGL ES的坐标系统和坐标变换后,再介绍真正的3D图形绘制方法。

Android OpenGL ES 开发教程(7):创建实例应用OpenGLDemos程序框架 创建了示例应用的程序框架,并提供了一个“Hello World?示例。

为避免一些重复代码,这里定义一个所有示例代码的基类OpenGLESActivity,其定义如下:

  1. public class OpenGLESActivity 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().setFlags(
  10. 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(0.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 onResume() and onPause()
  29. // to take appropriate action when the activity looses focus
  30. super.onResume();
  31. mGLSurfaceView.onResume();
  32. }
  33.  
  34. @Override
  35. protected void onPause() {
  36. // Ideally a game should implement onResume() and onPause()
  37. // to take appropriate action when the activity looses focus
  38. super.onPause();
  39. mGLSurfaceView.onPause();
  40. }
  41.  
  42. protected GLSurfaceView mGLSurfaceView;
  43.  
  44. }
  • 在onCreate 方法中创建一个GLSurfaceView mGLSurfaceView,并将屏幕设置为全屏。并为mGLSurfaceView设置Render.
  • onResume ,onPause 处理GLSurfaceView 的暂停和恢复。
  • DrawScene 使用黑色清空屏幕。

OpenGL ES 内部存放图形数据的Buffer有COLOR ,DEPTH (深度信息)等,在绘制图形只前一般需要清空COLOR 和 DEPTH Buffer。

在屏幕上使用红色绘制3个点。创建一个DrawPoint 作为 OpenGLESActivity 的子类,并定义3个顶点的坐标:

  1. public class DrawPoint extends OpenGLESActivity
  2. implements IOpenGLDemo{
  3.  
  4. float[] vertexArray = new float[]{
  5. -0.8f , -0.4f * 1.732f , 0.0f ,
  6. 0.8f , -0.4f * 1.732f , 0.0f ,
  7. 0.0f , 0.4f * 1.732f , 0.0f ,
  8. };
  9.  
  10.  
  11. /** Called when the activity is first created. */
  12. @Override
  13. public void onCreate(Bundle savedInstanceState) {
  14. super.onCreate(savedInstanceState);
  15.  
  16. }
  17.  
  18. ...
  19.  
  20. }

下面为DrawPoint 的DrawScene 的实现:

  1. public void DrawScene(GL10 gl) {
  2. super.DrawScene(gl);
  3.  
  4. ByteBuffer vbb
  5. = ByteBuffer.allocateDirect(vertexArray.length*4);
  6. vbb.order(ByteOrder.nativeOrder());
  7. FloatBuffer vertex = vbb.asFloatBuffer();
  8. vertex.put(vertexArray);
  9. vertex.position(0);
  10.  
  11. gl.glColor4f(1.0f, 0.0f, 0.0f, 1.0f);
  12. gl.glPointSize(8f);
  13. gl.glLoadIdentity();
  14. gl.glTranslatef(0, 0, -4);
  15.  
  16. gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
  17.  
  18. gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertex);
  19. gl.glDrawArrays(GL10.GL_POINTS, 0, 3);
  20.  
  21. gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
  22.  
  23. }
  • 首先是使用FloatBuffer 存放三个顶点坐标。
  • 使用glColor4f(float red, float green, float blue, float alpha) 将当前颜色设为红色。
  • glPointSize(float size) 可以用来设置绘制点的大小。
  • 使用glEnableClientState 打开Pipeline 的Vectex 顶点“开关”
  • 使用glVertexPointer 通知OpenGL ES图形库顶点坐标。
  • 使用GL_POINTS 模式使用glDrawArrays绘制3个顶点。

下载