1

我可以在 OpenGL ES 2.0的函数onDrawFrame中生成纹理吗?GLSurfaceRenderer例如,我使用的代码如下所示。

GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, MyTexture);

int[] mNewTexture = new int[weight * height * 4];
for(int ii = 0; ii < weight * height; ii = ii + 4){
    mNewTexture[ii]   = 127;
    mNewTexture[ii+1] = 127;
    mNewTexture[ii+2] = 127;
    mNewTexture[ii+3] = 127;
}

IntBuffer texBuffer = IntBuffer.wrap(mNewTexture);
GLES20.glTexImage2D(GL10.GL_TEXTURE_2D, 0, GL10.GL_RGBA, weight, height, 0, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, texBuffer);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, 0);
GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, 0);

pmF.drawSelf(MyTexture);

并且pmF代码中的类用于使用纹理来渲染屏幕。我注意到这段代码被执行了。但是屏幕上没有显示结果。

4

1 回答 1

1

是的,可以onDrawFrame()在 OpenGL 代码中重新加载纹理,我在此方法中重新加载纹理。这是我的代码的摘录onDrawFrame()

public void onDrawFrame(GL10 glUnused) {
    if (bReloadWood) {
        unloadTexture(mTableTextureID);
        mTableTextureID = loadETC1Texture("textures/" + mWoodTexture);
        bReloadWood = false;
    } 
    //...
}

方法unloadTexture()loadETC1Texture()做通常的 OpenGL 东西来卸载和加载纹理到 GPU:

protected void unloadTexture(int id) {
    int[] ids = { id };
    GLES20.glDeleteTextures(1, ids, 0);
} 

protected int loadETC1Texture(String filename) {
    int[] textures = new int[1];
    GLES20.glGenTextures(1, textures, 0);

    int textureID = textures[0];
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureID);


    GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST);
    GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);

    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_REPEAT);
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_REPEAT);

    InputStream is = null;
            // excluded code for loading InputStream for brevity

    try {
        ETC1Util.loadTexture(GLES10.GL_TEXTURE_2D, 0, 0, GLES10.GL_RGB, GLES10.GL_UNSIGNED_SHORT_5_6_5, is);
    } catch (IOException e) {
        Log.w(TAG, "Could not load texture: " + e);
    } finally {
        try {
            is.close();
        } catch (IOException e) {
            // ignore exception thrown from close.
        }
    }

    return textureID;
} 
于 2012-12-18T09:52:01.660 回答