在OpenGL ES 2.0中的显示功能中生成纹理

时间:2012-12-18 08:53:05

标签: android opengl-es opengl-es-2.0

我可以在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用于使用纹理渲染屏幕。我注意到这段代码已经执行了。但是屏幕上没有显示任何结果。

1 个答案:

答案 0 :(得分: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;
}