Cube.java
上传用户:saijinfu
上传日期:2018-08-05
资源大小:1961k
文件大小:5k
源码类别:

android开发

开发平台:

Java

  1. /*
  2.  * Copyright (C) 2007 The Android Open Source Project
  3.  *
  4.  * Licensed under the Apache License, Version 2.0 (the "License");
  5.  * you may not use this file except in compliance with the License.
  6.  * You may obtain a copy of the License at
  7.  *
  8.  *      http://www.apache.org/licenses/LICENSE-2.0
  9.  *
  10.  * Unless required by applicable law or agreed to in writing, software
  11.  * distributed under the License is distributed on an "AS IS" BASIS,
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  * See the License for the specific language governing permissions and
  14.  * limitations under the License.
  15.  */
  16. package com.ex;
  17. import java.nio.ByteBuffer;
  18. import java.nio.ByteOrder;
  19. import java.nio.FloatBuffer;
  20. import java.nio.IntBuffer;
  21. import java.nio.ShortBuffer;
  22. import javax.microedition.khronos.opengles.GL10;
  23. import android.util.Log;
  24.    
  25. class Cube
  26. {
  27.     public Cube()
  28.     {
  29.         int one = 0x10000;
  30.         int vertices[] = {
  31.         
  32.                 // FRONT
  33.                 -one, -one, one, one, -one, one,
  34.                 -one, one, one, one, one, one,
  35.                 // BACK
  36.                 -one, -one, -one, -one, one, -one,
  37.                 one, -one, -one, one, one, -one,
  38.                 // LEFT
  39.                 -one, -one, one, -one, one, one,
  40.                 -one, -one, -one, -one, one, -one,
  41.                 // RIGHT
  42.                 one, -one, -one, one, one, -one,
  43.                 one, -one, one, one, one, one,
  44.                 // TOP
  45.                 -one, one, one, one, one, one,
  46.                 -one, one, -one, one, one, -one,
  47.                 // BOTTOM
  48.                 -one, -one, one, -one, -one, -one,
  49.                 one, -one, one, one, -one, -one,
  50.         };
  51.         
  52.      float spriteTexcoords[]  = {
  53.     
  54.                 // FRONT
  55.                 0, 1, 1, 1, 0, 0, 1, 0,
  56.                 // BACK
  57.                 1, 1, 1, 0, 0, 1, 0, 0,
  58.                 // LEFT
  59.                 1, 1, 1, 0, 0, 1, 0, 0,
  60.                 // RIGHT
  61.                 1, 1, 1, 0, 0, 1, 0, 0,
  62.                 // TOP
  63.                 1, 0, 0, 0, 1, 1, 0, 1,
  64.                 // BOTTOM
  65.                 0, 0, 0, 1, 1, 0, 1, 1, 
  66.  
  67.      };
  68.         ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length*4);
  69.         vbb.order(ByteOrder.nativeOrder());
  70.         mVertexBuffer = vbb.asIntBuffer();
  71.         mVertexBuffer.put(vertices);
  72.         mVertexBuffer.position(0);
  73.         
  74.         ByteBuffer tbb = ByteBuffer.allocateDirect(spriteTexcoords.length*4);
  75.         tbb.order(ByteOrder.nativeOrder());
  76.         mTexBuffer = tbb.asFloatBuffer();
  77.         mTexBuffer.position(0);
  78.        
  79.         
  80.         for(int i=0; i<48; i++){
  81.          mTexBuffer.put(spriteTexcoords[i]);
  82.         }
  83.         
  84.         mTexBuffer.position(0);
  85.         
  86.         
  87.     }
  88.     
  89.     public void draw(GL10 gl){    
  90.      //Main.debug("draw");
  91.     
  92.      gl.glFrontFace(GL10.GL_CCW);
  93.     
  94.      //启用深度测试    3D 图形一般都启用
  95.      gl.glEnable(GL10.GL_DEPTH_TEST);
  96.     
  97.         //启用顶点坐标数组
  98.         gl.glEnableClientState(gl.GL_VERTEX_ARRAY);              
  99.      //预缓冲 顶点坐标数组
  100.         gl.glVertexPointer(3, gl.GL_FIXED, 0, mVertexBuffer);
  101.         //左 右  下 上 近 远
  102.         //gl.glOrthof(-1.0f, 1.0f, -1.5f, -1.5f, -1.0f, 1.0f);
  103.         
  104.         
  105.         //缩放
  106.         gl.glScalef(0.65f, 0.65f, 0.65f);
  107.         
  108.        
  109.         //启用纹理
  110.         gl.glEnable(GL10.GL_TEXTURE_2D);              
  111.         
  112.         
  113.         gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, mTexBuffer);
  114.         
  115.         
  116.         gl.glBindTexture(GL10.GL_TEXTURE_2D, Cube.mTextureID_front);
  117.         gl.glDrawArrays(gl.GL_TRIANGLE_STRIP, 0, 4);  
  118.         
  119.         gl.glBindTexture(GL10.GL_TEXTURE_2D, Cube.mTextureID__back);
  120.         gl.glDrawArrays(gl.GL_TRIANGLE_STRIP, 4, 4);
  121.         //gl.glColor4f(0.0f, 1.0f, 0.0f, 1.0f);
  122.         gl.glBindTexture(GL10.GL_TEXTURE_2D, Cube.mTextureID_left);
  123.         gl.glDrawArrays(gl.GL_TRIANGLE_STRIP, 8, 4);
  124.         gl.glBindTexture(GL10.GL_TEXTURE_2D, Cube.mTextureID_right);
  125.         gl.glDrawArrays(gl.GL_TRIANGLE_STRIP, 12, 4);
  126.         
  127.         //gl.glColor4f(0.0f, 0.0f, 1.0f, 1.0f);
  128.         gl.glBindTexture(GL10.GL_TEXTURE_2D, Cube.mTextureID_top);
  129.         gl.glDrawArrays(gl.GL_TRIANGLE_STRIP, 16, 4);
  130.         gl.glBindTexture(GL10.GL_TEXTURE_2D, Cube.mTextureID_bottom);
  131.         gl.glDrawArrays(gl.GL_TRIANGLE_STRIP, 20, 4);
  132.         
  133.         
  134.     }
  135.    
  136.     private IntBuffer   mVertexBuffer;
  137.     private FloatBuffer mTexBuffer;
  138.     public static int mTextureID_front;
  139.     public static int mTextureID__back;
  140.     public static int mTextureID_left;
  141.     public static int mTextureID_right;
  142.     public static int mTextureID_top;
  143.     public static int mTextureID_bottom;
  144.     
  145.     
  146.     
  147. }