简体   繁体   English

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

[英]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? 我可以在OpenGL ES 2.0的GLSurfaceRenderer onDrawFrame中生成纹理吗? 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. 代码中的pmF类用于使用纹理渲染屏幕。 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. 是的,可以在onDrawFrame()重新加载纹理,我可以在我的OpenGL代码中使用此方法重新加载纹理。 Here is excerpt from my onDrawFrame() code: 这是我的onDrawFrame()代码的摘录:

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: 方法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;
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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