Android的OpenGL学习笔记(4)

来源:互联网 发布:域名cname 编辑:程序博客网 时间:2024/04/30 05:54

                                                                   Android的OpenGL学习笔记(4)

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

其实很简单,只需添加几句话就行了,主要变化在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.     private float _red = 0f;  
  17.     private float _green = 0f;  
  18.     private float _blue = 0f;  
  19.   
  20.     // a raw buffer to hold indices allowing a reuse of points.  
  21.     private ShortBuffer _indexBuffer;  
  22.       
  23.     // a raw buffer to hold the vertices  
  24.     private FloatBuffer _vertexBuffer;  
  25.       
  26.     // a raw buffer to hold the colors  
  27.     private FloatBuffer _colorBuffer;  
  28.       
  29.     private short[] _indicesArray = {012};  
  30.     private int _nrOfVertices = 3;  
  31.   
  32.     private float _angle;  
  33.       
  34.     @Override  
  35.     public void onSurfaceCreated(GL10 gl, EGLConfig config) {  
  36.         // preparation  
  37.         gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);//允许设置顶点  
  38.         gl.glEnableClientState(GL10.GL_COLOR_ARRAY);//允许设置颜色  
  39.         initTriangle();  
  40.     }  
  41.   
  42.     @Override  
  43.     public void onSurfaceChanged(GL10 gl, int w, int h) {  
  44.         gl.glViewport(00, w, h);  
  45.     }  
  46.       
  47.     public void setAngle(float angle) {  
  48.         _angle = angle;  
  49.     }  
  50.   
  51.     @Override  
  52.     public void onDrawFrame(GL10 gl) {  
  53.         // define the color we want to be displayed as the "clipping wall"  
  54.         gl.glClearColor(_red, _green, _blue, 1.0f);  
  55.           
  56.         // reset the matrix - good to fix the rotation to a static angle  
  57.         gl.glLoadIdentity();  
  58.           
  59.         // clear the color buffer to show the ClearColor we called above...  
  60.         gl.glClear(GL10.GL_COLOR_BUFFER_BIT);  
  61.       
  62.         // set rotation for the non-static triangle  
  63.         gl.glRotatef(_angle, 0f, 1f, 0f);  
  64.       
  65.         // gl.glColor4f(0.5f, 0f, 0f, 0.5f);  
  66.         gl.glVertexPointer(3, GL10.GL_FLOAT, 0, _vertexBuffer);//设置顶点数组  
  67.         gl.glColorPointer(4, GL10.GL_FLOAT, 0, _colorBuffer);//设置颜色数组  
  68.         gl.glDrawElements(GL10.GL_TRIANGLES, _nrOfVertices, GL10.GL_UNSIGNED_SHORT, _indexBuffer);  
  69.     }  
  70.       
  71.     private void initTriangle() {  
  72.         // float has 4 bytes  
  73.         ByteBuffer vbb = ByteBuffer.allocateDirect(_nrOfVertices * 3 * 4);  
  74.         vbb.order(ByteOrder.nativeOrder());  
  75.         _vertexBuffer = vbb.asFloatBuffer();  
  76.           
  77.         // short has 4 bytes  
  78.         ByteBuffer ibb = ByteBuffer.allocateDirect(_nrOfVertices * 2);  
  79.         ibb.order(ByteOrder.nativeOrder());  
  80.         _indexBuffer = ibb.asShortBuffer();  
  81.           
  82.         // float has 4 bytes, 4 colors (RGBA) * number of vertices * 4 bytes  
  83.         ByteBuffer cbb = ByteBuffer.allocateDirect(4 * _nrOfVertices * 4);  
  84.         cbb.order(ByteOrder.nativeOrder());  
  85.         _colorBuffer = cbb.asFloatBuffer();  
  86.           
  87.         float[] coords = {  
  88.             -0.5f, -0.5f, 0f, // (x1, y1, z1)  
  89.             0.5f, -0.5f, 0f, // (x2, y2, z2)  
  90.             0.5f, 0.5f, 0f // (x3, y3, z3)  
  91.         };  
  92.           
  93.         float[] colors = {  
  94.             1f, 0f, 0f, 1f, // point 1  
  95.             0f, 1f, 0f, 1f, // point 2  
  96.             0f, 0f, 1f, 1f, // point 3  
  97.         };  
  98.           
  99.         _vertexBuffer.put(coords);  
  100.         _indexBuffer.put(_indicesArray);  
  101.         _colorBuffer.put(colors);  
  102.           
  103.         _vertexBuffer.position(0);  
  104.         _indexBuffer.position(0);  
  105.         _colorBuffer.position(0);  
  106.     }  
  107.       
  108.     public void setColor(float r, float g, float b) {  
  109.         _red = r;  
  110.         _green = g;  
  111.         _blue = b;  
  112.     }  
  113. }  

最终效果图:

原创粉丝点击