简体   繁体   中英

Generating Texture in the Display Function in OpenGL ES 2.0

Is it possible for me to generate a texture in the function onDrawFrame of GLSurfaceRenderer in OpenGL ES 2.0? For example, the code I used is shown below.

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);

And the class pmF in the code is used to render the screen using the texture. I noticed that this piece of code is executed. But no result is displayed on the screen.

Yes, it is possible to reload textures in onDrawFrame() , I reload textures in this method in my OpenGL code. Here is excerpt from my onDrawFrame() code:

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

Methods unloadTexture() and loadETC1Texture() do usual OpenGL stuff to unload and load textures to 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;
} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM