You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在ARCore中使用OpenGL为单个3D对象加载多张2D纹理

Loading Multiple Textures for a Single 3D Object (Java/OpenGL ES 2.0)

Core Idea to Build On

Your single-texture code already covers the essential OpenGL texture workflow—we just need to scale this to handle multiple textures by assigning each a unique ID, using separate texture units, and repeating the setup steps for every texture asset.

Full Implementation Code

First, set up your texture ID array and list of texture asset names:

// Array to store unique IDs for each texture
private int[] mTextures;
// Replace with your actual texture asset filenames (e.g., diffuse, specular, normal maps)
private String[] textureAssetNames = {"diffuse_tex.png", "specular_tex.png", "normal_tex.png"};

Then create a method to load all textures in one go:

public void loadMultipleTextures(Context context) {
    // Initialize the texture ID array with the number of textures we need
    mTextures = new int[textureAssetNames.length];
    
    // Generate all texture IDs upfront
    GLES20.glGenTextures(mTextures.length, mTextures, 0);
    
    for (int i = 0; i < textureAssetNames.length; i++) {
        try {
            // Load the bitmap from the app's assets folder
            Bitmap textureBitmap = BitmapFactory.decodeStream(
                context.getAssets().open(textureAssetNames[i])
            );
            
            // Activate the corresponding texture unit (GL_TEXTURE0, GL_TEXTURE1, etc.)
            GLES20.glActiveTexture(GLES20.GL_TEXTURE0 + i);
            
            // Bind the current texture ID to the active unit
            GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mTextures[i]);
            
            // Apply texture filtering parameters (matches your original single-texture setup)
            GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR_MIPMAP_LINEAR);
            GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
            
            // Load the bitmap data into the bound texture
            GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, textureBitmap, 0);
            
            // Generate mipmaps for smoother rendering at different distances
            GLES20.glGenerateMipmap(GLES20.GL_TEXTURE_2D);
            
            // Free up memory by recycling the bitmap (no longer needed after loading)
            textureBitmap.recycle();
            
            // Unbind the texture to avoid accidental modifications in subsequent code
            GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, 0);
            
        } catch (IOException e) {
            e.printStackTrace();
            // Add custom error handling here (e.g., load a fallback default texture)
        }
    }
}

Using the Textures in Rendering

When drawing your 3D object, you need to bind each texture and pass its unit index to your shader:

// In your object's draw method
for (int i = 0; i < mTextures.length; i++) {
    // Activate the correct texture unit
    GLES20.glActiveTexture(GLES20.GL_TEXTURE0 + i);
    // Bind the texture to the active unit
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mTextures[i]);
    // Tell the shader which unit to use for this texture (match your shader's uniform names)
    int uniformLoc = GLES20.glGetUniformLocation(mShaderProgram, "uTexture" + i);
    GLES20.glUniform1i(uniformLoc, i);
}

// Proceed with drawing your object (glDrawArrays or glDrawElements)

Quick Notes

  • Ensure your shader has matching uniform samplers (e.g., uniform sampler2D uTexture0; uniform sampler2D uTexture1;).
  • Always recycle bitmaps after loading to prevent memory leaks in Android.
  • You can expand error handling to gracefully handle missing texture assets (e.g., fall back to a solid color texture).

内容的提问来源于stack exchange,提问作者Raj

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 07:41:52