Android高级进阶七 Android OpenGL开发四棱锥和立方体

最新版本:Android高级进阶七 Android OpenGL开发四棱锥和立方体

         前面我们貌似接触到的都是“平面上的3D”,可是貌似终究是貌似,这一节我们开始接触真正的3D,使用OpenGL画四棱锥和立方体,并使用前面我们已经学过的方法对他们进行着色处理,着色方法见:Android高级进阶五 Android OpenGL给多边形着色

        效果图如下:

        那我们接着看看这是如何实现的,我们在开发2D三角形和四边形的时候,使用了画点方法画出了三角形和四边形,不过只画出了一面,现在我们只要再画出其他的三面和五面就可以完成3D效果,着色和之前介绍的方法一样,可以直接看代码:

  1. package org.ourunix.android.opengltest;  
  2.   
  3. import java.nio.ByteBuffer;  
  4. import java.nio.ByteOrder;  
  5. import java.nio.IntBuffer;  
  6.   
  7. import javax.microedition.khronos.egl.EGLConfig;  
  8. import javax.microedition.khronos.opengles.GL10;  
  9.   
  10. import android.app.Activity;  
  11. import android.opengl.GLSurfaceView;  
  12. import android.opengl.GLSurfaceView.Renderer;  
  13. import android.os.Bundle;  
  14.   
  15. public class OpenGLTestActivity extends Activity {  
  16.     /** Called when the activity is first created. */  
  17.     @Override  
  18.     public void onCreate(Bundle savedInstanceState) {  
  19.         super.onCreate(savedInstanceState);  
  20.         GLSurfaceView glView = new GLSurfaceView(this);  
  21.         glView.setRenderer(new GLRender());  
  22.         setContentView(glView);  
  23.     }  
  24.       
  25.     public class GLRender implements Renderer{    
  26.         private int one = 0x10000;    
  27.         private int[] trigger = {0, one, 0,  
  28.                                 -one, -one, one,  
  29.                                 one, -one, one,  
  30.                                   
  31.                                 0, one, 0,  
  32.                                 one, -one, one,  
  33.                                 one, -one, -one,  
  34.                                   
  35.                                 0, one, 0,  
  36.                                 one, -one, -one,  
  37.                                 -one, -one, -one,  
  38.                                   
  39.                                 0, one, 0,  
  40.                                 -one, -one, -one,  
  41.                                 -one, -one, one};    
  42.         private int[] quarter = {one,one,-one,  
  43.                                 -one,one,-one,  
  44.                                 one,one,one,  
  45.                                 -one,one,one,  
  46.                                   
  47.                                 one,-one,one,  
  48.                                 -one,-one,one,  
  49.                                 one,-one,-one,  
  50.                                 -one,-one,-one,  
  51.                                   
  52.                                 one,one,one,  
  53.                                 -one,one,one,  
  54.                                 one,-one,one,  
  55.                                 -one,-one,one,  
  56.                                   
  57.                                 one,-one,-one,  
  58.                                 -one,-one,-one,  
  59.                                 one,one,-one,  
  60.                                 -one,one,-one,  
  61.                                   
  62.                                 -one,one,one,  
  63.                                 -one,one,-one,  
  64.                                 -one,-one,one,  
  65.                                 -one,-one,-one,  
  66.                                   
  67.                                 one, one, -one,  
  68.                                 one, one, one,  
  69.                                 one, -one, -one,  
  70.                                 one, -one, one,};    
  71.         private int[] color = {  one,0,0,one,  
  72.                                  0,one,0,one,  
  73.                                  0,0,one,one,     
  74.                                    
  75.                                  one,0,0,one,  
  76.                                  0,one,0,one,  
  77.                                  0,0,one,one,  
  78.                                    
  79.                                  one,0,0,one,  
  80.                                  0,one,0,one,  
  81.                                  0,0,one,one,  
  82.                                    
  83.                                  one,0,0,one,  
  84.                                  0,one,0,one,  
  85.                                  0,0,one,one, };  
  86.         private int[] colorForQua = {0,one,0,one,  
  87.                                  0,one,0,one,  
  88.                                  0,one,0,one,  
  89.                                  0,one,0,one,  
  90.                                    
  91.                                  one, one/20, one,  
  92.                                  one, one/20, one,  
  93.                                  one, one/20, one,  
  94.                                  one, one/20, one,  
  95.                                    
  96.                                  one,0,0,one,  
  97.                                  one,0,0,one,  
  98.                                  one,0,0,one,  
  99.                                  one,0,0,one,  
  100.                                    
  101.                                  one,one,0,one,  
  102.                                  one,one,0,one,  
  103.                                  one,one,0,one,  
  104.                                  one,one,0,one,  
  105.                                    
  106.                                  0,0,one,one,  
  107.                                  0,0,one,one,  
  108.                                  0,0,one,one,  
  109.                                  0,0,one,one,  
  110.                                    
  111.                                  one,0,one,one,  
  112.                                  one,0,one,one,  
  113.                                  one,0,one,one,  
  114.                                  one,0,one,one,};  
  115.         //准备四棱锥各顶点    
  116.         private IntBuffer triggerBuffer = BufferUtil.iBuffer(trigger);    
  117.         //准备正方体顶点    
  118.         private IntBuffer quarterBuffer = BufferUtil.iBuffer(quarter);    
  119.         //准备四棱锥颜色  
  120.         private IntBuffer colorBuffer = BufferUtil.iBuffer(color);  
  121.         //准备正方体颜色  
  122.         private IntBuffer colorBufferForQua = BufferUtil.iBuffer(colorForQua);  
  123.           
  124.      
  125.         //咳,咳现在开始画图了    
  126.         @Override    
  127.         public void onDrawFrame(GL10 gl) {    
  128.             // TODO Auto-generated method stub    
  129.             //清楚屏幕和深度缓存    
  130.             gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);    
  131.             gl.glLoadIdentity();    
  132.             //现将屏幕向左移动,用来画三角锥  
  133.             gl.glTranslatef(-1.5f, 0.0f, -6.0f);   
  134.             //旋转四棱锥  
  135.             gl.glRotatef(rotateTri, 0.0f, 1.0f, 0.0f);  
  136.             //允许设置顶点    
  137.             gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);   
  138.             //开启颜色渲染功能  
  139.             gl.glEnableClientState(GL10.GL_COLOR_ARRAY);  
  140.             //设置四棱锥各顶点    
  141.             gl.glVertexPointer(3, GL10.GL_FIXED, 0, triggerBuffer);   
  142.             //给四棱锥的每一顶点着色  
  143.             gl.glColorPointer(4, GL10.GL_FIXED, 0, colorBuffer);  
  144.             //绘制四棱锥    
  145.             for (int i = 0; i < 4; i++){  
  146.                 gl.glDrawArrays(GL10.GL_TRIANGLES, i * 33);   
  147.             }  
  148.             //绘制四棱锥结束  
  149.             gl.glFinish();  
  150.               
  151.             //重置    
  152.             gl.glLoadIdentity();    
  153.             //现将屏幕向左移动,用来画正方体    
  154.             gl.glTranslatef(1.5f, 0.0f, -6.0f);   
  155.             gl.glRotatef(rotateQua, 1.0f, 0.0f, 0.0f);  
  156.             //设置正方体    
  157.             gl.glVertexPointer(3, GL10.GL_FIXED, 0, quarterBuffer);   
  158.             //各顶点着色  
  159.             gl.glColorPointer(4, GL10.GL_FIXED, 0, colorBufferForQua);  
  160.             //绘制正方体    
  161.             for (int i = 0; i < 6; i++){  
  162.                 gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, i * 44);    
  163.             }  
  164.             gl.glColor4f(0.5f, 0.5f, 1.0f, 1.0f);  
  165.             //绘制正方体结束  
  166.             gl.glFinish();  
  167.               
  168.             //关闭颜色渲染功能  
  169.             gl.glDisableClientState(GL10.GL_COLOR_ARRAY);  
  170.             gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);    
  171.         }    
  172.     
  173.         //当窗口改变时,调用,至少在创建窗口时调用一次,这边设置下场景大小    
  174.         @Override    
  175.         public void onSurfaceChanged(GL10 gl, int width, int height) {    
  176.             // TODO Auto-generated method stub    
  177.             //设置OpenGL场景大小    
  178.             float ratio = (float) width / height;    
  179.             gl.glViewport(00, width, height);    
  180.             gl.glMatrixMode(GL10.GL_PROJECTION);//设置为投影矩阵模式    
  181.             gl.glLoadIdentity();//重置    
  182.             gl.glFrustumf(-ratio, ratio, -11110);//设置视角    
  183.             gl.glMatrixMode(GL10.GL_MODELVIEW);    
  184.             gl.glLoadIdentity();    
  185.         }    
  186.     
  187.         //当窗口被创建时我们可以做些初始化工作    
  188.         @Override    
  189.         public void onSurfaceCreated(GL10 gl, EGLConfig config) {    
  190.             // TODO Auto-generated method stub    
  191.             //告诉系统对透视进行修正    
  192.             gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);    
  193.             //设置清除屏幕时所用的颜色,参数依次为红、绿、蓝、Alpha值    
  194.             gl.glClearColorx(0000);    
  195.             //启用阴影平滑    
  196.             gl.glShadeModel(GL10.GL_SMOOTH);    
  197.             //以下是关于深度缓存的设置,非常重要    
  198.             gl.glClearDepthf(1.0f);//设置深度缓存    
  199.             gl.glEnable(GL10.GL_DEPTH_TEST);//启用深度测试    
  200.             gl.glDepthFunc(GL10.GL_LEQUAL);//所做深度测试的类型    
  201.         }    
  202.             
  203.     }    
  204.         
  205.     public static class BufferUtil {    
  206.         public static IntBuffer intBuffer;    
  207.     
  208.         public static IntBuffer iBuffer(int[] a) {    
  209.             // 先初始化buffer,数组的长度*4,因为一个float占4个字节    
  210.             ByteBuffer mbb = ByteBuffer.allocateDirect(a.length * 4);    
  211.             // 数组排列用nativeOrder    
  212.             mbb.order(ByteOrder.nativeOrder());    
  213.             intBuffer = mbb.asIntBuffer();    
  214.             intBuffer.put(a);    
  215.             intBuffer.position(0);    
  216.             return intBuffer;    
  217.         }    
  218.     }    
  219. }

        可以看到代码比较简单,注释都已经写到了代码中,看官看之即可。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值