我正在尝试在我的安卓游戏中实现运动模糊效果。
经过大量研究后,我发现最好的方法是使用帧缓冲对象将前一帧保存为纹理,并将其渲染到当前帧的顶部。所以看到一些关于如何做类似事情的好tutorials我最终得到了这个代码,它基本上在纹理上渲染我的场景,然后将纹理绘制到默认的帧缓冲区。
它只在gl.glGetError()中绘制了一个带有1286(无效帧缓冲操作)错误的白色纹理**
已解决:问题似乎是两个纹理维度的无力量,如Jean所说
int[] fb, depthRb, renderTex;
int texW = 480 * 2;
int texH = 800 * 2;
IntBuffer texBuffer;
int[] buf = new int[texW * texH];
GL11ExtensionPack gl11ep ;
void setup(GL10 gl)
{
gl11ep=(GL11ExtensionPack)gl;
fb = new int[1];
depthRb = new int[1];
renderTex = new int[1];
gl11ep.glGenFramebuffersOES(1, fb, 0);
gl11ep.glGenRenderbuffersOES(1, depthRb, 0); // the depth buffer
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_REPEAT);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T, GL10.GL_REPEAT);
texBuffer = ByteBuffer.allocateDirect(buf.length*4).order(ByteOrder.nativeOrder()).asIntBuffer();
gl.glTexImage2D(GL10.GL_TEXTURE_2D, 0, GL10.GL_RGB, texW, texH, 0, GL10.GL_RGB, GL10.GL_UNSIGNED_SHORT_5_6_5, texBuffer);
gl11ep.glBindRenderbufferOES(GL11ExtensionPack.GL_RENDERBUFFER_OES, depthRb[0]);
gl11ep.glRenderbufferStorageOES(GL11ExtensionPack.GL_RENDERBUFFER_OES, GL11ExtensionPack.GL_DEPTH_COMPONENT16, texW, texH);
}
boolean RenderStart(GL10 gl)
{
// Bind the framebuffer
gl11ep.glBindFramebufferOES(GL11ExtensionPack.GL_FRAMEBUFFER_OES, fb[0]);
// specify texture as color attachment
gl11ep.glFramebufferTexture2DOES(GL11ExtensionPack.GL_FRAMEBUFFER_OES, GL11ExtensionPack.GL_COLOR_ATTACHMENT0_OES, GL10.GL_TEXTURE_2D, renderTex[0], 0);
// attach render buffer as depth buffer
gl11ep.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) {
Tools.con("Background Load GLError: " + error) ;//here the 1286 error
}
int status = gl11ep.glCheckFramebufferStatusOES(GL11ExtensionPack.GL_FRAMEBUFFER_OES);
if (status != GL11ExtensionPack.GL_FRAMEBUFFER_COMPLETE_OES)//here always geting true
{
return false;
}
return true;
}
void RenderEnd(GL10 gl)
{
gl11ep.glBindFramebufferOES(GL11ExtensionPack.GL_FRAMEBUFFER_OES, 0);
gl.glClearColor(0f, 0f, 0f, 1.0f);
gl.glClear( gl.GL_DEPTH_BUFFER_BIT | gl.GL_COLOR_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, 0,800, 480);
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);
}
答案 0 :(得分:2)
可能你应该让你的纹理具有2的高度和宽度(即512x512,甚至更小,因为它仅用于模糊效果)。几乎所有具有硬件加速功能的设备都需要它。快速测试方法是将其运行到模拟器中(只有软件才能运行,如果它支持代码中所需的所有其他功能)