呃,先說幾話廢話,關於顯示列表的實現方法,NEHE是將一個正方體分解爲無頂盒與盒頂兩個部分,我想了一下,其實更簡單的實現方式:不用分解那麼麻煩,直接先繪一個正方體出來,再用雙重循環出15個正方體就行了,可能他這樣實現有他的道理,大家也可去NEHE網上參考他的。
(1)Activity類:
package sim.feel;
import android.app.Activity;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
public class DefaultList extends Activity {
public MyRenderer myRenderer;
public GLSurfaceView glSurfaceView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LoadImage.load(getResources());
myRenderer = new MyRenderer(this);
glSurfaceView = new GLSurfaceView(this);
glSurfaceView.setRenderer(myRenderer);
setContentView(glSurfaceView);
}
}
// 載入圖片
class LoadImage {
public static Bitmap bitmap;
public static void load(Resources res) {
bitmap = BitmapFactory.decodeResource(res, R.drawable.cube);
}
}
(2)MyRenderer類:
package sim.feel;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.IntBuffer;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import android.content.Context;
import android.graphics.Bitmap;
import android.opengl.GLSurfaceView.Renderer;
import android.opengl.GLUtils;
public class MyRenderer implements Renderer {
// Context
public Context context;
// 頂點紋理相關
private int one = 0x10000;
// Bitmap對象
private Bitmap bitmap;
// 循環相關
private int xloop, yloop;
// 紋理相關
private int[] textureids;
// 頂點Buffer
private IntBuffer vertexBuffer;
// 紋理Buffer
private IntBuffer texBuffer;
// 旋轉方向
private float xrot, yrot;
// 顏色數組
private float[][] boxcol = {
{ 1.0f, 0.0f, 0.0f },
{ 1.0f, 0.5f, 0.0f },
{ 1.0f, 1.0f, 0.0f },
{ 0.0f, 1.0f, 0.0f },
{ 0.0f, 1.0f, 1.0f }
};
// 正方體頂點
private int[] vertices = {
-one, -one, one,
one, -one, one,
-one, one, one,
one, one, one,
one, -one, one,
one, -one, -one,
one, one, one,
one,one, -one,
one, -one, -one,
-one, -one, -one,
one, one, -one,
-one,one, -one,
-one, -one, -one,
-one, -one, one,
-one, one, -one,
-one, one, one,
-one, one, -one,
one, one, -one,
-one, one, one,
one, one, one,
-one, -one, -one,
-one, -one, one,
one, -one, -one,
one, -one, one
};
// 紋理點
private int[] texCoords = {
0, 0,
one, 0,
0, one,
one, one
};
public MyRenderer(Context context) {
this.context = context;
// 初始化
textureids = new int[1];
// 實例化bitmap
bitmap = LoadImage.bitmap;
ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4);
vbb.order(ByteOrder.nativeOrder());
vertexBuffer = vbb.asIntBuffer();
vertexBuffer.put(vertices);
vertexBuffer.position(0);
ByteBuffer tbb = ByteBuffer.allocateDirect(texCoords.length * 4 * 6);
tbb.order(ByteOrder.nativeOrder());
texBuffer = tbb.asIntBuffer();
// 爲每一個面貼上紋理
for (int i = 0; i < 6; i++) {
texBuffer.put(texCoords);
}
texBuffer.position(0);
}
@Override
public void onDrawFrame(GL10 gl) {
// 清除深度和顏色緩存
gl.glClear(GL10.GL_DEPTH_BUFFER_BIT | GL10.GL_COLOR_BUFFER_BIT);
for (yloop = 0; yloop < 6; yloop++) {
for (xloop = 0; xloop < yloop; xloop++) {
// 重置模型觀察矩陣
gl.glLoadIdentity();
// 設置正方體的位置
gl.glTranslatef(1.4f + ((float) (xloop) * 2.8f)
- ((float) (yloop) * 1.4f),
((6.0f - (float) (yloop)) * 2.4f) - 7.0f, -20.f);
// 旋轉
gl.glRotatef(45.0f - (2.0f * yloop) + xrot, 1.0f, 0.0f, 0.0f);
gl.glRotatef(45.0f + yrot, 0.0f, 1.0f, 0.0f);
gl.glColor4f(boxcol[yloop - 1][0], boxcol[yloop - 1][1],
boxcol[yloop - 1][2], 1.0f);
// 開啓頂點及紋理設置
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
gl.glVertexPointer(3, GL10.GL_FIXED, 0, vertexBuffer);
gl.glTexCoordPointer(2, GL10.GL_FIXED, 0, texBuffer); // Define
// 繪製正方體
for (int i = 0; i < 6; i++) {
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, i * 4, 4);
}
gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
}
}
}
@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
// 視角
gl.glViewport(0, 0, width, height);
float ratio = (float) width / height;
// 觀察模式
gl.glMatrixMode(GL10.GL_PROJECTION);
// 重置觀察佈局
gl.glLoadIdentity();
gl.glFrustumf(-ratio, ratio, -1, 1, 1, 30);
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
}
@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
// 告訴系統對透視進行修正
gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);
// 黑色背景
gl.glClearColor(0, 0, 0, 0);
// 啓用陰影平滑
gl.glShadeModel(GL10.GL_SMOOTH);
// 清除深度緩存
gl.glClearDepthf(one);
// 啓用深度測試
gl.glEnable(GL10.GL_DEPTH_TEST);
// 所做深度測試的類型
gl.glDepthFunc(GL10.GL_LEQUAL);
gl.glEnable(GL10.GL_TEXTURE_2D);
// 創建紋理
gl.glGenTextures(1, textureids, 0);
// 綁定要使用的紋理
gl.glBindTexture(GL10.GL_TEXTURE_2D, textureids[0]);
// 生成紋理
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
// 線性濾波
gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER,
GL10.GL_LINEAR);
gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER,
GL10.GL_LINEAR);
// 啓用默認0號燈
gl.glEnable(GL10.GL_LIGHT0);
// 使用燈光
gl.glEnable(GL10.GL_LIGHTING);
// 使用顏色材質,即改變紋理顏色,若不使用這句,設置顏色將不起作用
gl.glEnable(GL10.GL_COLOR_MATERIAL);
}
}
效果圖: