在过去的几天里,我正在研究在屏幕外的一些图像上应用 OpenGL 着色器并将结果作为文件保存在 sdcard 中的最佳方法是什么。所有这些都应该在后台,用户不应该注意到它。
我遇到了两种可能的解决方案:EGL(没有文档,所有示例都不能编译)RenderBuffer,在 OpenGl 中创建一个 FBO,将 renderBuffer 分配给 FBO 并显式地绘制到其中,问题是我总是得到“glError 1280" 在标记的行中,而且我很困惑是否可以在没有 glSurfaceView 的情况下完成所有这些工作(正如我所说,我希望它在后台)。
我在 Android SDK 中的 HelloEfect 暗室示例中遇到并做了以下更改:
private void loadFrameBuffers(int tex) throws Exception {
// creating FBO object
int[] frame = new int[1];
GLES20.glGenFramebuffers(1, frame, 0);
int fboId = frame[0];
TextureRenderer.checkGlError("glGenFramebuffers");
GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, fboId);
TextureRenderer.checkGlError("glBindFramebuffer");
// // attaches texture to framebuffer - We need to attached RenderBuffer
// image and not texture image!!!
GLES20.glFramebufferTexture2D(GLES20.GL_FRAMEBUFFER,
GLES20.GL_COLOR_ATTACHMENT0, GLES20.GL_TEXTURE_2D, tex, 0);
TextureRenderer.checkGlError("glFramebufferTexture2D");
// creating RenderBuffer object
int[] renderbuffers = new int[1];
GLES20.glGenRenderbuffers(1, renderbuffers, 0);
TextureRenderer.checkGlError("glGenRenderbuffers");
GLES20.glBindRenderbuffer(GLES20.GL_RENDERBUFFER, renderbuffers[0]);
TextureRenderer.checkGlError("glBindRenderbuffer");
int width = Math.min(GLES20.GL_MAX_RENDERBUFFER_SIZE, mImageWidth);
int height = Math.min(GLES20.GL_MAX_RENDERBUFFER_SIZE, mImageHeight);
GLES20.glRenderbufferStorage(GLES20.GL_RENDERBUFFER,
GLES20.GL_RGBA , width, height);
TextureRenderer.checkGlError("glRenderbufferStorage"); // !!!!! throws exception
GLES20.glFramebufferRenderbuffer(GLES20.GL_FRAMEBUFFER,
GLES20.GL_COLOR_ATTACHMENT0, GLES20.GL_RENDERBUFFER,
renderbuffers[0]);
TextureRenderer.checkGlError("glFramebufferRenderbuffer");
int status = GLES20.glCheckFramebufferStatus(GLES20.GL_FRAMEBUFFER);
if (status != GLES20.GL_FRAMEBUFFER_COMPLETE)
throw new Exception("error in creating fbo, error code is: "
+ status);
}
private void loadTextures() {
// Generate textures
GLES20.glGenTextures(2, mTextures, 0);
// Load input bitmap
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
R.drawable.puppy);
mImageWidth = bitmap.getWidth();
mImageHeight = bitmap.getHeight();
mTexRenderer.updateTextureSize(mImageWidth, mImageHeight);
// Upload to texture
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mTextures[0]);
GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);
// Set texture parameters
GLToolbox.initTexParams();
}
@Override
public void onDrawFrame(GL10 gl) {
if (!mInitialized) {
// Only need to do this once
mEffectContext = EffectContext.createWithCurrentGlContext();
mTexRenderer.init();
loadTextures();
try {
loadFrameBuffers(mTextures[0]);
} catch (Exception e) {
e.printStackTrace();
}
mInitialized = true;
}
if (mCurrentEffect != R.id.none) {
// if an effect is chosen initialize it and apply it to the texture
initEffect();
applyEffect();
}
renderResult();
}
行:GLES20.glRenderbufferStorage(GLES20.GL_RENDERBUFFER,GLES20.GL_DEPTH_RANGE,宽度,高度);TextureRenderer.checkGlError("glRenderbufferStorage");
抛出“hlError 1280”异常,我找不到问题所在。任何帮助将不胜感激!