萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網路程式設計 >> Android 開發

Android OpenGL ES 立方體貼圖旋轉:glDrawElements 多紋理綁定與頂點索引繪製教學

目標

為立方體 6 個面分別貼上不同圖片,並實現三軸自動旋轉動畫。

核心流程

  1. 啟用 2D 紋理:glEnable(GL_TEXTURE_2D)
  2. 生成紋理 ID:glGenTextures(6, intBuffer) (需 6 個 ID)
  3. 綁定並上傳圖片:glBindTexture + GLUtils.texImage2D
  4. 設定濾波:GL_LINEAR 放大/縮小
  5. 繪製迴圈:每幀 glRotate → 逐面 glBindTexture → glDrawElements

關鍵程式碼片段 (GLRender)

public class GLRender implements GLSurfaceView.Renderer {
    private IntBuffer texIds = IntBuffer.allocate(6);
    private IntBuffer vertices, texCoords, indices;
    private float xrot, yrot, zrot;

    @Override public void onSurfaceCreated(GL10 gl, EGLConfig config) {
        gl.glEnable(GL10.GL_TEXTURE_2D);
        gl.glGenTextures(6, texIds); // 生成 6 個紋理 ID
        gl.glClearColor(0, 0, 0, 1);
        gl.glEnable(GL10.GL_DEPTH_TEST);
        gl.glShadeModel(GL10.GL_SMOOTH);

        // 載入 6 張圖片到 GLImage 快取
        GLImage.load(context.getResources());

        // 定義立方體 8 頂點 (x,y,z * 65536 定點數)
        int one = 0x10000;
        vertices = IntBuffer.wrap(new int[]{
            -one,-one, one,  one,-one, one,  one, one, one,  -one, one, one, // 前面
            -one,-one,-one,  -one, one,-one,  one, one,-one,  one,-one,-one  // 後面
        });
        // 紋理座標 (u,v * 65536)
        texCoords = IntBuffer.wrap(new int[]{
            0,0,  one,0,  one,one,  0,one,  // 面 0
            0,0,  one,0,  one,one,  0,one,  // 面 1
            // ... 共 6 面 24 個座標
        });
        // 索引:每面 4 頂點組成 TRIANGLE_STRIP
        indices = ByteBuffer.wrap(new byte[]{
            0,1,2,3,   // 面 0
            4,5,6,7,   // 面 1
            8,9,10,11, // 面 2
            12,13,14,15, // 面 3
            16,17,18,19, // 面 4
            20,21,22,23  // 面 5
        }).asUnsignedByteBuffer();
    }

    @Override public void onDrawFrame(GL10 gl) {
        gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
        gl.glLoadIdentity();
        gl.glTranslatef(0, 0, -5.0f);
        gl.glRotatef(xrot, 1, 0, 0);
        gl.glRotatef(yrot, 0, 1, 0);
        gl.glRotatef(zrot, 0, 0, 1);

        gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
        gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
        gl.glVertexPointer(3, GL10.GL_FIXED, 0, vertices);
        gl.glTexCoordPointer(2, GL10.GL_FIXED, 0, texCoords);

        for (int face = 0; face < 6; face++) {
            gl.glBindTexture(GL10.GL_TEXTURE_2D, texIds.get(face));
            GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, GLImage.getBitmap(face), 0);
            gl.glDrawElements(GL10.GL_TRIANGLE_STRIP, 4, GL10.GL_UNSIGNED_BYTE,
                    (ByteBuffer) indices.position(face * 4));
        }

        gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
        gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);

        xrot += 0.5f; yrot += 0.3f; zrot += 0.2f; // 自動旋轉
    }
    // onSurfaceChanged 略
}

GLImage 圖片快取

class GLImage {
    static Bitmap[] bitmaps = new Bitmap[6];
    static void load(Resources res) {
        bitmaps[0] = BitmapFactory.decodeResource(res, R.drawable.img1);
        // ... img2 ~ img6
    }
    static Bitmap getBitmap(int face) { return bitmaps[face]; }
}

版面 (main.xml)

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <Button android:id="@+id/button1" android:text="演示開始"
        android:layout_width="145dp" android:layout_height="wrap_content"/>
</LinearLayout>

Activity 啟動

public class MainActivity extends Activity {
    GLSurfaceView glView;
    @Override protected void onCreate(Bundle s) {
        super.onCreate(s); setContentView(R.layout.main);
        GLImage.load(getResources());
        glView = new GLSurfaceView(this);
        glView.setRenderer(new GLRender());
        findViewById(R.id.button1).setOnClickListener(v -> setContentView(glView));
    }
}

總結

使用 glDrawElements 配合索引緩衝區可高效繪製立方體 6 面,每面綁定不同紋理 ID 並上傳對應 Bitmap,結合旋轉矩陣即可實現 3D 貼圖動畫。現代專案建議改用 OpenGL ES 2.0+ Shader 管線。

copyright © 萬盛學電腦網 all rights reserved