在屏幕外应用着色器的最佳方法

时间:2013-11-06 14:58:22

标签: android opengl-es-2.0 egl

我正在调查过去几天在屏幕上对一些图像应用OpenGl着色器的最佳方法是什么,并将结果保存为SD卡中的文件。所有这一切都应该在后台,用户不应该注意到它。

我遇到了两种可能的解决方案: 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”异常,我找不到问题所在。任何帮助都会非常有用!

0 个答案:

没有答案