Android的OpenGL学习笔记(5)

                                                                  Android的OpenGL学习笔记(5)

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

VortexRenderer.java代码:

Code:
  1. package com.droidnova.android.games.vortex;  
  2.   
  3. import java.nio.ByteBuffer;  
  4. import java.nio.ByteOrder;  
  5. import java.nio.FloatBuffer;  
  6. import java.nio.ShortBuffer;  
  7.   
  8. import javax.microedition.khronos.egl.EGLConfig;  
  9. import javax.microedition.khronos.opengles.GL10;  
  10.   
  11. import android.opengl.GLSurfaceView;  
  12.   
  13. public class VortexRenderer implements GLSurfaceView.Renderer {  
  14.     private static final String LOG_TAG = VortexRenderer.class.getSimpleName();  
  15.   
  16.     // a raw buffer to hold indices allowing a reuse of points.  
  17.     private ShortBuffer _indexBuffer;  
  18.   
  19.     // a raw buffer to hold the vertices  
  20.     private FloatBuffer _vertexBuffer;  
  21.   
  22.     // a raw buffer to hold the colors  
  23.     private FloatBuffer _colorBuffer;  
  24.   
  25.     private int _nrOfVertices = 0;  
  26.   
  27.     private float _xAngle;  
  28.     private float _yAngle;  
  29.   
  30.     @Override  
  31.     public void onSurfaceCreated(GL10 gl, EGLConfig config) {  
  32.         // preparation  
  33.         // enable the differentiation of which side may be visible  
  34.         gl.glEnable(GL10.GL_CULL_FACE);// enable了culling面,以保证只有一面。  
  35.         // which is the front? the one which is drawn counter clockwise  
  36.         gl.glFrontFace(GL10.GL_CCW);// GL_CCW表示逆时针,定义了哪种顺序为前面。GL_CW表示逆时针  
  37.         // which one should NOT be drawn  
  38.         gl.glCullFace(GL10.GL_BACK);// 设置其为GL_BACK以保证只显示正面.这或许有点迷糊偶,  
  39.         // 你可以看看如果用GL_FRONT_AND_BACK会发生什么……你将什么也看不到。  
  40.   
  41.         gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);//允许设置顶点  
  42.         gl.glEnableClientState(GL10.GL_COLOR_ARRAY);//允许设置顶点颜色  
  43.   
  44.         initTriangle();  
  45.     }  
  46.   
  47.     @Override  
  48.     public void onSurfaceChanged(GL10 gl, int w, int h) {  
  49.         gl.glViewport(00, w, h);  
  50.     }  
  51.   
  52.     public void setXAngle(float angle) {  
  53.         _xAngle = angle;  
  54.     }  
  55.   
  56.     public float getXAngle() {  
  57.         return _xAngle;  
  58.     }  
  59.   
  60.     public void setYAngle(float angle) {  
  61.         _yAngle = angle;  
  62.     }  
  63.   
  64.     public float getYAngle() {  
  65.         return _yAngle;  
  66.     }  
  67.       
  68.     @Override  
  69.     public void onDrawFrame(GL10 gl) {  
  70.         // define the color we want to be displayed as the "clipping wall"  
  71.         gl.glClearColor(0f, 0f, 0f, 1.0f);  
  72.   
  73.         // reset the matrix - good to fix the rotation to a static angle  
  74.         gl.glLoadIdentity();  
  75.   
  76.         // clear the color buffer and the depth buffer to show the ClearColor  
  77.         // we called above...  
  78.         gl.glClear(GL10.GL_COLOR_BUFFER_BIT);  
  79.   
  80.         // set rotation  
  81.         gl.glRotatef(_xAngle, 1f, 0f, 0f);  
  82.         gl.glRotatef(_yAngle, 0f, 1f, 0f);  
  83.   
  84.         // gl.glColor4f(0.5f, 0f, 0f, 0.5f);  
  85.         gl.glVertexPointer(3, GL10.GL_FLOAT, 0, _vertexBuffer);  
  86.         gl.glColorPointer(4, GL10.GL_FLOAT, 0, _colorBuffer);  
  87.         gl.glDrawElements(GL10.GL_TRIANGLES, _nrOfVertices,  
  88.                 GL10.GL_UNSIGNED_SHORT, _indexBuffer);  
  89.     }  
  90.   
  91.     private void initTriangle() {  
  92.         float[] coords = {   
  93.                 -0.5f, -0.5f, 0.5f, // 0  
  94.                 0.5f, -0.5f, 0.5f, // 1  
  95.                 0f, -0.5f, -0.5f, // 2  
  96.                 0f, 0.5f, 0f, // 3  
  97.         };  
  98.         _nrOfVertices = coords.length;  
  99.   
  100.         float[] colors = {   
  101.                 1f, 0f, 0f, 1f, // point 0 red  
  102.                 0f, 1f, 0f, 1f, // point 1 green  
  103.                 0f, 0f, 1f, 1f, // point 2 blue  
  104.                 1f, 1f, 1f, 1f, // point 3 white  
  105.         };  
  106.   
  107.         short[] indices = new short[] {   
  108.                 013// rwg  
  109.                 021// rbg  
  110.                 032// rbw  
  111.                 123// bwg  
  112.         };  
  113.   
  114.         // float has 4 bytes, coordinate * 4 bytes  
  115.         ByteBuffer vbb = ByteBuffer.allocateDirect(coords.length * 4);  
  116.         vbb.order(ByteOrder.nativeOrder());  
  117.         _vertexBuffer = vbb.asFloatBuffer();  
  118.   
  119.         // short has 2 bytes, indices * 2 bytes  
  120.         ByteBuffer ibb = ByteBuffer.allocateDirect(indices.length * 2);  
  121.         ibb.order(ByteOrder.nativeOrder());  
  122.         _indexBuffer = ibb.asShortBuffer();  
  123.   
  124.         // float has 4 bytes, colors (RGBA) * 4 bytes  
  125.         ByteBuffer cbb = ByteBuffer.allocateDirect(colors.length * 4);  
  126.         cbb.order(ByteOrder.nativeOrder());  
  127.         _colorBuffer = cbb.asFloatBuffer();  
  128.   
  129.         _vertexBuffer.put(coords);  
  130.         _indexBuffer.put(indices);  
  131.         _colorBuffer.put(colors);  
  132.   
  133.         _vertexBuffer.position(0);  
  134.         _indexBuffer.position(0);  
  135.         _colorBuffer.position(0);  
  136.     }  
  137. }  

VortexView.java代码:

Code:
  1. package com.droidnova.android.games.vortex;  
  2.   
  3. import android.content.Context;  
  4. import android.opengl.GLSurfaceView;  
  5. import android.view.MotionEvent;  
  6.   
  7. public class VortexView extends GLSurfaceView {  
  8.     private static final String LOG_TAG = VortexView.class.getSimpleName();  
  9.     private VortexRenderer _renderer;  
  10.     private float _x = 0;  
  11.     private float _y = 0;  
  12.     private float _z = 0;  
  13.       
  14.     public VortexView(Context context) {  
  15.         super(context);  
  16.         _renderer = new VortexRenderer();  
  17.         setRenderer(_renderer);  
  18.     }  
  19.       
  20.     public boolean onTouchEvent(final MotionEvent event) {  
  21.         if (event.getAction() == MotionEvent.ACTION_DOWN) {  
  22.             _x = event.getX();  
  23.             _y = event.getY();  
  24.         }  
  25.         if (event.getAction() == MotionEvent.ACTION_MOVE) {  
  26.             final float xdiff = (_x - event.getX());  
  27.             final float ydiff = (_y - event.getY());  
  28.             queueEvent(new Runnable() {  
  29.                 public void run() {  
  30.                     _renderer.setXAngle(_renderer.getXAngle() + ydiff);  
  31.                     _renderer.setYAngle(_renderer.getYAngle() + xdiff);  
  32.                 }  
  33.             });  
  34.             _x = event.getX();  
  35.             _y = event.getY();  
  36.         }  
  37.         return true;  
  38.     }  
  39. }  

最终效果图:

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
学习OpenGL ES(Embedded Systems)可以让你在Android平台上构建高性能的2D和3D图形应用程序。以下是一些学习OpenGL ES的步骤和建议: 1. 了解OpenGL ES的基础知识:OpenGL ES是一个跨平台的图形API,用于在移动设备上进行高性能的2D和3D绘图。在学习OpenGL ES之前,建议先了解OpenGL ES的基础知识,例如图形管线、着色器、顶点缓冲区对象(VBO)等。 2. 学习OpenGL ES的编程语言:OpenGL ES支持多种编程语言,包括C、C++、Java等。对于Android开发者来说,Java是最常用的编程语言。 3. 下载OpenGL ES开发工具:为了开始学习OpenGL ES,需要安装一个开发环境。Android Studio是一个常用的Android开发工具,可以通过安装Android Studio来获取OpenGL ES的开发环境。 4. 学习OpenGL ES的API:OpenGL ES有许多的API可以使用,例如OpenGL ES 1.0、OpenGL ES 2.0、OpenGL ES 3.0等。建议从OpenGL ES 2.0开始学习,因为它支持现代的图形管线和着色器编程。 5. 掌握OpenGL ES的基本概念和技术:学习OpenGL ES的一些基本概念和技术包括着色器编程、渲染缓冲区对象(RBO)、帧缓冲区对象(FBO)等。 6. 实践:最好的学习方法是通过实践来掌握OpenGL ES。可以通过编写简单的图形应用程序来加深对OpenGL ES的理解和掌握。 7. 学习OpenGL ES的高级技术:一旦掌握了基本概念和技术,可以开始学习OpenGL ES的高级技术,例如纹理映射、光照、阴影等。 总之,学习OpenGL ES需要掌握基本概念和技术,并通过实践来加深理解。此外,需要耐心和毅力,因为OpenGL ES是一个复杂的主题,需要花费时间和精力来学习

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值