1 package org.example.opengl;
2 -
3 - import java.nio.ByteBuffer;
4 - import java.nio.ByteOrder;
5 5 import java.nio.IntBuffer;
6 -
7 - import javax.microedition.khronos.opengles.GL10;
8 -
9 - import android.content.Context;
10 10 import android.graphics.Bitmap;
11 - import android.graphics.BitmapFactory;
12 - import android.opengl.GLUtils;
13 -
14 - class GLCube {
15 private final IntBuffer mVertexBuffer;
16 - public GLCube() {
17 - int one = 65536;
18 - int half = one / 2;
19 - int vertices[] = {
20 // 前
21 - -half, -half, half, half, -half, half,
22 - -half, half, half, half, half, half,
23 - // 后
24 - -half, -half, -half, -half, half, -half,
25 half, -half, -half, half, half, -half,
26 - // 左
27 - -half, -half, half, -half, half, half,
28 - -half, -half, -half, -half, half, -half,
29 - // 右
30 half, -half, -half, half, half, -half,
31 - half, -half, half, half, half, half,
32 - // 顶
33 - -half, half, half, half, half, half,
34 - -half, half, -half, half, half, -half,
35 // 底
36 - -half, -half, half, -half, -half, -half,
37 - half, -half, half, half, -half, -half, };
38 -
45 ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4);
46 - vbb.order(ByteOrder.nativeOrder());
47 - mVertexBuffer = vbb.asIntBuffer();
48 - mVertexBuffer.put(vertices);
49 - mVertexBuffer.position(0);
50 5 }- public void draw(GL10 gl) {
51 - gl.glVertexPointer(3, GL10.GL_FIXED, 0, mVertexBuffer);
52 -
53 gl.glColor4f(1, 1, 1, 1);
54 - gl.glNormal3f(0, 0, 1);
55 - gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);
56 - gl.glNormal3f(0, 0, -1);
57 - gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 4, 4);
58
59 - gl.glColor4f(1, 1, 1, 1);
60 - gl.glNormal3f(-1, 0, 0);
61 - gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 8, 4);
62 - gl.glNormal3f(1, 0, 0);
63 gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 12, 4);
64 -
65 - gl.glColor4f(1, 1, 1, 1);
66 - gl.glNormal3f(0, 1, 0);
67 - gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 16, 4);
68 gl.glNormal3f(0, -1, 0);
69 - gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 20, 4);
70 - }
71 - }