Android的OpenGL学习笔记(6)

绘制多个3D模型,让你深刻体会3D空间,这里开启了深度测试功能:

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. importandroid.util.Log;
  10. publicclassVortexRendererimplementsGLSurfaceView.Renderer{
  11. privatestaticfinalStringLOG_TAG=VortexRenderer.class.getSimpleName();
  12. //arawbuffertoholdindicesallowingareuseofpoints.
  13. privateShortBuffer_indexBuffer;
  14. //arawbuffertoholdthevertices
  15. privateFloatBuffer_vertexBuffer;
  16. //arawbuffertoholdthecolors
  17. privateFloatBuffer_colorBuffer;
  18. privateint_nrOfVertices=0;
  19. privatefloat_xAngle;
  20. privatefloat_yAngle;
  21. privatefloat_width=320f;
  22. privatefloat_height=480f;
  23. @Override
  24. publicvoidonSurfaceCreated(GL10gl,EGLConfigconfig){
  25. //preparation
  26. Log.i(LOG_TAG,"onSurfaceCreated()");
  27. gl.glMatrixMode(GL10.GL_PROJECTION);//透视模型
  28. floatsize=.01f*(float)Math.tan(Math.toRadians(45.0)/2);//角度制转换为弧度制,然后除以2,这里不是取整。然后求正切值
  29. floatratio=_width/_height;
  30. //perspective:
  31. gl.glFrustumf(-size,size,-size/ratio,size/ratio,0.01f,100.0f);
  32. //orthographic:
  33. //gl.glOrthof(-1,1,-1/ratio,1/ratio,0.01f,100.0f);
  34. gl.glViewport(0,0,(int)_width,(int)_height);
  35. gl.glMatrixMode(GL10.GL_MODELVIEW);//视图模型
  36. gl.glEnable(GL10.GL_DEPTH_TEST);//允许深度测试
  37. //definethecolorwewanttobedisplayedasthe"clippingwall"
  38. gl.glClearColor(0f,0f,0f,1.0f);
  39. //enablethedifferentiationofwhichsidemaybevisible
  40. gl.glEnable(GL10.GL_CULL_FACE);
  41. //whichisthefront?theonewhichisdrawncounterclockwise
  42. gl.glFrontFace(GL10.GL_CCW);
  43. //whichoneshouldNOTbedrawn
  44. gl.glCullFace(GL10.GL_BACK);
  45. gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
  46. gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
  47. initTriangle();
  48. }
  49. @Override
  50. publicvoidonSurfaceChanged(GL10gl,intw,inth){
  51. Log.i(LOG_TAG,"onSurfaceChanged()");
  52. _width=w;
  53. _height=h;
  54. gl.glViewport(0,0,w,h);
  55. }
  56. publicvoidsetXAngle(floatangle){
  57. _xAngle=angle;
  58. }
  59. publicfloatgetXAngle(){
  60. return_xAngle;
  61. }
  62. publicvoidsetYAngle(floatangle){
  63. _yAngle=angle;
  64. }
  65. publicfloatgetYAngle(){
  66. return_yAngle;
  67. }
  68. @Override
  69. publicvoidonDrawFrame(GL10gl){
  70. //clearthecolorbufferandthedepthbuffer
  71. gl.glClear(GL10.GL_COLOR_BUFFER_BIT|GL10.GL_DEPTH_BUFFER_BIT);
  72. gl.glVertexPointer(3,GL10.GL_FLOAT,0,_vertexBuffer);
  73. gl.glColorPointer(4,GL10.GL_FLOAT,0,_colorBuffer);
  74. for(inti=1;i<=10;i++){
  75. gl.glLoadIdentity();
  76. gl.glTranslatef(0.0f,-1f,-1.0f+-1.5f*i);
  77. //setrotation
  78. gl.glRotatef(-_xAngle,1f,0f,0f);
  79. gl.glRotatef(-_yAngle,0f,1f,0f);
  80. gl.glDrawElements(GL10.GL_TRIANGLES,_nrOfVertices,GL10.GL_UNSIGNED_SHORT,_indexBuffer);
  81. }
  82. }
  83. privatevoidinitTriangle(){
  84. float[]coords={
  85. -0.5f,-0.5f,0.5f,//0
  86. 0.5f,-0.5f,0.5f,//1
  87. 0f,-0.5f,-0.5f,//2
  88. 0f,0.5f,0f,//3
  89. };
  90. _nrOfVertices=coords.length;
  91. float[]colors={
  92. 1f,0f,0f,1f,//point0red
  93. 0f,1f,0f,1f,//point1green
  94. 0f,0f,1f,1f,//point2blue
  95. 1f,1f,1f,1f,//point3white
  96. };
  97. short[]indices=newshort[]{
  98. 0,1,3,//rwg
  99. 0,2,1,//rbg
  100. 0,3,2,//rbw
  101. 1,2,3,//bwg
  102. };
  103. //floathas4bytes,coordinate*4bytes
  104. ByteBuffervbb=ByteBuffer.allocateDirect(coords.length*4);
  105. vbb.order(ByteOrder.nativeOrder());
  106. _vertexBuffer=vbb.asFloatBuffer();
  107. //shorthas2bytes,indices*2bytes
  108. ByteBufferibb=ByteBuffer.allocateDirect(indices.length*2);
  109. ibb.order(ByteOrder.nativeOrder());
  110. _indexBuffer=ibb.asShortBuffer();
  111. //floathas4bytes,colors(RGBA)*4bytes
  112. ByteBuffercbb=ByteBuffer.allocateDirect(colors.length*4);
  113. cbb.order(ByteOrder.nativeOrder());
  114. _colorBuffer=cbb.asFloatBuffer();
  115. _vertexBuffer.put(coords);
  116. _indexBuffer.put(indices);
  117. _colorBuffer.put(colors);
  118. _vertexBuffer.position(0);
  119. _indexBuffer.position(0);
  120. _colorBuffer.position(0);
  121. }
  122. }

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. publicVortexView(Contextcontext){
  11. super(context);
  12. _renderer=newVortexRenderer();
  13. setRenderer(_renderer);
  14. }
  15. publicbooleanonTouchEvent(finalMotionEventevent){
  16. if(event.getAction()==MotionEvent.ACTION_DOWN){
  17. _x=event.getX();
  18. _y=event.getY();
  19. }
  20. if(event.getAction()==MotionEvent.ACTION_MOVE){
  21. finalfloatxdiff=(_x-event.getX());
  22. finalfloatydiff=(_y-event.getY());
  23. queueEvent(newRunnable(){
  24. publicvoidrun(){
  25. _renderer.setXAngle(_renderer.getXAngle()+ydiff);
  26. _renderer.setYAngle(_renderer.getYAngle()+xdiff);
  27. }
  28. });
  29. _x=event.getX();
  30. _y=event.getY();
  31. }
  32. returntrue;
  33. }
  34. }

更多相关文章

  1. Android软件安全风险及规范
  2. Android事件处理的两种模型
  3. 新书出版:《Android深度探索(卷1):HAL与驱动开发》
  4. Android安全模型之Android安全机制(进程通信)
  5. 浅析Android线程模型
  6. 新书出版:《Android深度探索(卷1):HAL与驱动开发》
  7. 将tensorflow训练好的模型移植到android
  8. Android(安卓)中的消息模型(Message,MessageQueue,handle,looper
  9. 【android】布局之盒模型、对齐方式、填充(类比web理解)

随机推荐

  1. Android(安卓)Audio简述
  2. Camera服务之--JNI部分
  3. android -- Can't create handler inside
  4. apk 反编译 教程 (不支持混淆,Android)
  5. Android2.3 x86 安装实战
  6. 自定义View--单行上下滚动广告
  7. [android]adb remount失败的解决方法
  8. android调用系统发短信传递电话号码和短
  9. Android点击事件的四种实现方式
  10. Android(安卓)资源文件错误排查 Process