Android的OpenGL学习笔记(5)

接着前面的知识学习,现在可以做一个3D的图形了,和以往一样,必要的解释都在注释中:

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. //arawbuffertoholdindicesallowingareuseofpoints.
  12. privateShortBuffer_indexBuffer;
  13. //arawbuffertoholdthevertices
  14. privateFloatBuffer_vertexBuffer;
  15. //arawbuffertoholdthecolors
  16. privateFloatBuffer_colorBuffer;
  17. privateint_nrOfVertices=0;
  18. privatefloat_xAngle;
  19. privatefloat_yAngle;
  20. @Override
  21. publicvoidonSurfaceCreated(GL10gl,EGLConfigconfig){
  22. //preparation
  23. //enablethedifferentiationofwhichsidemaybevisible
  24. gl.glEnable(GL10.GL_CULL_FACE);//enable了culling面,以保证只有一面。
  25. //whichisthefront?theonewhichisdrawncounterclockwise
  26. gl.glFrontFace(GL10.GL_CCW);//GL_CCW表示逆时针,定义了哪种顺序为前面。GL_CW表示逆时针
  27. //whichoneshouldNOTbedrawn
  28. gl.glCullFace(GL10.GL_BACK);//设置其为GL_BACK以保证只显示正面.这或许有点迷糊偶,
  29. //你可以看看如果用GL_FRONT_AND_BACK会发生什么……你将什么也看不到。
  30. gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);//允许设置顶点
  31. gl.glEnableClientState(GL10.GL_COLOR_ARRAY);//允许设置顶点颜色
  32. initTriangle();
  33. }
  34. @Override
  35. publicvoidonSurfaceChanged(GL10gl,intw,inth){
  36. gl.glViewport(0,0,w,h);
  37. }
  38. publicvoidsetXAngle(floatangle){
  39. _xAngle=angle;
  40. }
  41. publicfloatgetXAngle(){
  42. return_xAngle;
  43. }
  44. publicvoidsetYAngle(floatangle){
  45. _yAngle=angle;
  46. }
  47. publicfloatgetYAngle(){
  48. return_yAngle;
  49. }
  50. @Override
  51. publicvoidonDrawFrame(GL10gl){
  52. //definethecolorwewanttobedisplayedasthe"clippingwall"
  53. gl.glClearColor(0f,0f,0f,1.0f);
  54. //resetthematrix-goodtofixtherotationtoastaticangle
  55. gl.glLoadIdentity();
  56. //clearthecolorbufferandthedepthbuffertoshowtheClearColor
  57. //wecalledabove...
  58. gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
  59. //setrotation
  60. gl.glRotatef(_xAngle,1f,0f,0f);
  61. gl.glRotatef(_yAngle,0f,1f,0f);
  62. //gl.glColor4f(0.5f,0f,0f,0.5f);
  63. gl.glVertexPointer(3,GL10.GL_FLOAT,0,_vertexBuffer);
  64. gl.glColorPointer(4,GL10.GL_FLOAT,0,_colorBuffer);
  65. gl.glDrawElements(GL10.GL_TRIANGLES,_nrOfVertices,
  66. GL10.GL_UNSIGNED_SHORT,_indexBuffer);
  67. }
  68. privatevoidinitTriangle(){
  69. float[]coords={
  70. -0.5f,-0.5f,0.5f,//0
  71. 0.5f,-0.5f,0.5f,//1
  72. 0f,-0.5f,-0.5f,//2
  73. 0f,0.5f,0f,//3
  74. };
  75. _nrOfVertices=coords.length;
  76. float[]colors={
  77. 1f,0f,0f,1f,//point0red
  78. 0f,1f,0f,1f,//point1green
  79. 0f,0f,1f,1f,//point2blue
  80. 1f,1f,1f,1f,//point3white
  81. };
  82. short[]indices=newshort[]{
  83. 0,1,3,//rwg
  84. 0,2,1,//rbg
  85. 0,3,2,//rbw
  86. 1,2,3,//bwg
  87. };
  88. //floathas4bytes,coordinate*4bytes
  89. ByteBuffervbb=ByteBuffer.allocateDirect(coords.length*4);
  90. vbb.order(ByteOrder.nativeOrder());
  91. _vertexBuffer=vbb.asFloatBuffer();
  92. //shorthas2bytes,indices*2bytes
  93. ByteBufferibb=ByteBuffer.allocateDirect(indices.length*2);
  94. ibb.order(ByteOrder.nativeOrder());
  95. _indexBuffer=ibb.asShortBuffer();
  96. //floathas4bytes,colors(RGBA)*4bytes
  97. ByteBuffercbb=ByteBuffer.allocateDirect(colors.length*4);
  98. cbb.order(ByteOrder.nativeOrder());
  99. _colorBuffer=cbb.asFloatBuffer();
  100. _vertexBuffer.put(coords);
  101. _indexBuffer.put(indices);
  102. _colorBuffer.put(colors);
  103. _vertexBuffer.position(0);
  104. _indexBuffer.position(0);
  105. _colorBuffer.position(0);
  106. }
  107. }

VortexView.java代码:

Code:
  1. packagecom.droidnova.android.games.vortex;
  2. importandroid.content.Context;
  3. importandroid.opengl.GLSurfaceView;
  4. importandroid.view.MotionEvent;
  5. publicclassVortexViewextendsGLSurfaceView{
  6. privatestaticfinalStringLOG_TAG=VortexView.class.getSimpleName();
  7. privateVortexRenderer_renderer;
  8. privatefloat_x=0;
  9. privatefloat_y=0;
  10. privatefloat_z=0;
  11. publicVortexView(Contextcontext){
  12. super(context);
  13. _renderer=newVortexRenderer();
  14. setRenderer(_renderer);
  15. }
  16. publicbooleanonTouchEvent(finalMotionEventevent){
  17. if(event.getAction()==MotionEvent.ACTION_DOWN){
  18. _x=event.getX();
  19. _y=event.getY();
  20. }
  21. if(event.getAction()==MotionEvent.ACTION_MOVE){
  22. finalfloatxdiff=(_x-event.getX());
  23. finalfloatydiff=(_y-event.getY());
  24. queueEvent(newRunnable(){
  25. publicvoidrun(){
  26. _renderer.setXAngle(_renderer.getXAngle()+ydiff);
  27. _renderer.setYAngle(_renderer.getYAngle()+xdiff);
  28. }
  29. });
  30. _x=event.getX();
  31. _y=event.getY();
  32. }
  33. returntrue;
  34. }
  35. }

最终效果图:

更多相关文章

  1. Android关机界面代码
  2. 搭建 android 代码镜像服务
  3. android 随手记 SQLITE代码 直接能用
  4. android draw bitmap 示例代码
  5. android edittext 显隐密码代码转换两种方式
  6. android制作一个简单登入界面的部分代码

随机推荐

  1. Android带进度条的文件上传,使用AsyncTask
  2. android中ImageView设置选中与不选中颜色
  3. 最新的智能移动终端ios,android等市场占
  4. Activity启动模式 及 Intent Flags 与 栈
  5. Android 页面自动跳转方法(比如进入app的
  6. Android底部导航BottomNavigationBar的使
  7. Android(安卓)自定义权限 ( ) 和 Android
  8. 0基础,安卓搭建环境,运行HelloWord
  9. Android Gradle 配置打包输出名称格式
  10. Android——组件之Service