呃,先说几话废话,关于显示列表的实现方法,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);
}
}
效果图: