Android的OpenGL学习笔记(4)

这次主要是给前几次笔记做出的三角形上色呵呵……

其实很简单,只需添加几句话就行了,主要变化在VortexRenderer.java代码中,现把代码贴出来:

Code:
  1. packagecom.droidnova.android.games.vortex;
  2. importjava.nio.ByteBuffer;
  3. importjava.nio.ByteOrder;
  4. importjava.nio.FloatBuffer;
  5. importjava.nio.ShortBuffer;
  6. importjavax.microedition.khronos.egl.EGLConfig;
  7. importjavax.microedition.khronos.opengles.GL10;
  8. importandroid.opengl.GLSurfaceView;
  9. publicclassVortexRendererimplementsGLSurfaceView.Renderer{
  10. privatestaticfinalStringLOG_TAG=VortexRenderer.class.getSimpleName();
  11. privatefloat_red=0f;
  12. privatefloat_green=0f;
  13. privatefloat_blue=0f;
  14. //arawbuffertoholdindicesallowingareuseofpoints.
  15. privateShortBuffer_indexBuffer;
  16. //arawbuffertoholdthevertices
  17. privateFloatBuffer_vertexBuffer;
  18. //arawbuffertoholdthecolors
  19. privateFloatBuffer_colorBuffer;
  20. privateshort[]_indicesArray={0,1,2};
  21. privateint_nrOfVertices=3;
  22. privatefloat_angle;
  23. @Override
  24. publicvoidonSurfaceCreated(GL10gl,EGLConfigconfig){
  25. //preparation
  26. gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);//允许设置顶点
  27. gl.glEnableClientState(GL10.GL_COLOR_ARRAY);//允许设置颜色
  28. initTriangle();
  29. }
  30. @Override
  31. publicvoidonSurfaceChanged(GL10gl,intw,inth){
  32. gl.glViewport(0,0,w,h);
  33. }
  34. publicvoidsetAngle(floatangle){
  35. _angle=angle;
  36. }
  37. @Override
  38. publicvoidonDrawFrame(GL10gl){
  39. //definethecolorwewanttobedisplayedasthe"clippingwall"
  40. gl.glClearColor(_red,_green,_blue,1.0f);
  41. //resetthematrix-goodtofixtherotationtoastaticangle
  42. gl.glLoadIdentity();
  43. //clearthecolorbuffertoshowtheClearColorwecalledabove...
  44. gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
  45. //setrotationforthenon-statictriangle
  46. gl.glRotatef(_angle,0f,1f,0f);
  47. //gl.glColor4f(0.5f,0f,0f,0.5f);
  48. gl.glVertexPointer(3,GL10.GL_FLOAT,0,_vertexBuffer);//设置顶点数组
  49. gl.glColorPointer(4,GL10.GL_FLOAT,0,_colorBuffer);//设置颜色数组
  50. gl.glDrawElements(GL10.GL_TRIANGLES,_nrOfVertices,GL10.GL_UNSIGNED_SHORT,_indexBuffer);
  51. }
  52. privatevoidinitTriangle(){
  53. //floathas4bytes
  54. ByteBuffervbb=ByteBuffer.allocateDirect(_nrOfVertices*3*4);
  55. vbb.order(ByteOrder.nativeOrder());
  56. _vertexBuffer=vbb.asFloatBuffer();
  57. //shorthas4bytes
  58. ByteBufferibb=ByteBuffer.allocateDirect(_nrOfVertices*2);
  59. ibb.order(ByteOrder.nativeOrder());
  60. _indexBuffer=ibb.asShortBuffer();
  61. //floathas4bytes,4colors(RGBA)*numberofvertices*4bytes
  62. ByteBuffercbb=ByteBuffer.allocateDirect(4*_nrOfVertices*4);
  63. cbb.order(ByteOrder.nativeOrder());
  64. _colorBuffer=cbb.asFloatBuffer();
  65. float[]coords={
  66. -0.5f,-0.5f,0f,//(x1,y1,z1)
  67. 0.5f,-0.5f,0f,//(x2,y2,z2)
  68. 0.5f,0.5f,0f//(x3,y3,z3)
  69. };
  70. float[]colors={
  71. 1f,0f,0f,1f,//point1
  72. 0f,1f,0f,1f,//point2
  73. 0f,0f,1f,1f,//point3
  74. };
  75. _vertexBuffer.put(coords);
  76. _indexBuffer.put(_indicesArray);
  77. _colorBuffer.put(colors);
  78. _vertexBuffer.position(0);
  79. _indexBuffer.position(0);
  80. _colorBuffer.position(0);
  81. }
  82. publicvoidsetColor(floatr,floatg,floatb){
  83. _red=r;
  84. _green=g;
  85. _blue=b;
  86. }
  87. }

最终效果图:

更多相关文章

  1. Android开发中遇到的坑
  2. Android]仿通讯录ListView小例子
  3. 在代码中设置RelativeLayout布局中标签的android:layout_toLeftO
  4. android 界面布局
  5. EditText属性
  6. Android入门——基础控件
  7. Android学习笔记(33):Android对话框
  8. Android参数设置: Preference
  9. android背景选择器selector用法汇总

随机推荐

  1. Android编程: 环境搭建、基本知识
  2. 杂七杂八2
  3. Android去除标题栏及自定义title栏
  4. android资源配置文件
  5. 背景图片显示问题 图片显示错误 android
  6. Cordova 入门AndroidStudio集成
  7. android动画效果处理
  8. ContentProvider-获取系统数据
  9. EditText 的常用属性与解释
  10. 阅读《Android(安卓)从入门到精通》(23)—