Opengl es:通过Frame Buffer渲染到纹理只渲染一种颜色

时间:2012-02-08 15:35:28

标签: java android opengl-es opengl-es-2.0 render-to-texture

我试图在我的安卓游戏中实现运动模糊效果。

经过大量研究后,我发现最好的方法是使用帧缓冲对象将前一帧保存为纹理,并将其渲染到当前帧的顶部。所以看到一些关于如何做类似事情的好教程我最终得到了这个代码,它基本上在纹理上渲染我的场景,然后将纹理绘制到默认的帧缓冲区。

但是纹理只有一种颜色,就像我有一个绿色的explotion纹理只有绿色 我认为这是渲染缓冲区或纹理参数

的问题

这是我的代码

int[] fb, depthRb, renderTex; 
int texW = 1024; 
int texH = 1024; 
IntBuffer texBuffer;
int[] buf = new int[texW * texH];
GL11ExtensionPack gl11ep ;
void setup(GL10 gl)
{
    fb = new int[1];
    depthRb = new int[1];
    renderTex = new int[1];
    // generate
    ((GL11ExtensionPack)gl).glGenFramebuffersOES(1, fb, 0);
    ((GL11ExtensionPack)gl).glGenRenderbuffersOES(1, depthRb, 0); // the depth buffer

    gl.glEnable(GL10.GL_TEXTURE_2D);
    gl.glGenTextures(1, renderTex, 0);// generate texture
    gl.glBindTexture(GL10.GL_TEXTURE_2D, renderTex[0]);

    gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST);
    gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
    gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S, GL10.GL_CLAMP_TO_EDGE);
    gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T, GL10.GL_CLAMP_TO_EDGE);

    texBuffer = ByteBuffer.allocateDirect(buf.length*4).order(ByteOrder.nativeOrder()).asIntBuffer();

     gl.glTexEnvf(GL10.GL_TEXTURE_ENV, GL10.GL_TEXTURE_ENV_MODE,GL10.GL_MODULATE);

    gl.glTexImage2D(GL10.GL_TEXTURE_2D, 0, GL10.GL_RGBA, texW, texH, 0, GL10.GL_RGBA, GL10.GL_UNSIGNED_SHORT_4_4_4_4, texBuffer);

    // create render buffer and bind 16-bit depth buffer
    ((GL11ExtensionPack)gl).glBindRenderbufferOES(GL11ExtensionPack.GL_RENDERBUFFER_OES, depthRb[0]);
    ((GL11ExtensionPack)gl).glRenderbufferStorageOES(GL11ExtensionPack.GL_RENDERBUFFER_OES, GL11ExtensionPack.GL_DEPTH_COMPONENT16, texW, texH);

    gl.glDisable(GL10.GL_TEXTURE_2D);

}

boolean RenderStart(GL10 gl)
{
    // Bind the framebuffer
    ((GL11ExtensionPack)gl).glBindFramebufferOES(GL11ExtensionPack.GL_FRAMEBUFFER_OES, fb[0]);

    // specify texture as color attachment
    ((GL11ExtensionPack)gl).glFramebufferTexture2DOES(GL11ExtensionPack.GL_FRAMEBUFFER_OES, GL11ExtensionPack.GL_COLOR_ATTACHMENT0_OES, GL10.GL_TEXTURE_2D, renderTex[0], 0);

    // attach render buffer as depth buffer
    ((GL11ExtensionPack)gl).glFramebufferRenderbufferOES(GL11ExtensionPack.GL_FRAMEBUFFER_OES, GL11ExtensionPack.GL_DEPTH_ATTACHMENT_OES, GL11ExtensionPack.GL_RENDERBUFFER_OES, depthRb[0]);

    int error = gl.glGetError();
    if (error != GL10.GL_NO_ERROR) {
        Log.w("err", "Background Load GLError: " + error+"      ");
    }
    int status = ((GL11ExtensionPack)gl).glCheckFramebufferStatusOES(GL11ExtensionPack.GL_FRAMEBUFFER_OES);
    if (status != GL11ExtensionPack.GL_FRAMEBUFFER_COMPLETE_OES)
    {
        Tools.con("ret");
        return true;
    }
    gl.glClear( GL10.GL_DEPTH_BUFFER_BIT | GL10.GL_COLOR_BUFFER_BIT);
    return true;
}

void RenderEnd(GL10 gl)
{
    ((GL11ExtensionPack)gl).glBindFramebufferOES(GL11ExtensionPack.GL_FRAMEBUFFER_OES, 0);

    gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);

    gl.glEnable(GL10.GL_TEXTURE_2D);
    gl.glBindTexture(GL10.GL_TEXTURE_2D, renderTex[0]);
    gl.glColor4f(1,1,1,1);
    ((GL11Ext) gl).glDrawTexfOES(0, 0, -2,1024,1024);
    gl.glDisable(GL10.GL_TEXTURE_2D);

}


public void onDrawFrame(GL10 gl)
    {

    this.RenderStart(gl);
    render(gl);//render scene
    this.RenderEnd(gl);
    }

public void onSurfaceCreated(GL10 gl, EGLConfig config)
    {
       ...
       setup(gl);
    }

1 个答案:

答案 0 :(得分:0)

尝试替换

gl.glTexEnvf(GL10.GL_TEXTURE_ENV, GL10.GL_TEXTURE_ENV_MODE,GL10.GL_MODULATE); 

gl.glTexEnvf(GL10.GL_TEXTURE_ENV, GL10.GL_TEXTURE_ENV_MODE,GL10.GL_REPLACE);