Skip to content Skip to sidebar Skip to footer

Is It Possible To Render An Android View To An Opengl Fbo Or Texture?

Is it possible to render a View (say, a WebView) to an FBO so it can be used as a texture in an OpenGL composition?

Solution 1:

I brought together a complete demo project which renders a view to GL textures in real time in an efficient way which can be found in this repo. It shows how to render WebView to GL texture in real time as an example.

Also a brief code for this can look like the following (taken from the demo project from the repo above):

publicclassGLWebViewextendsWebView {

    private ViewToGLRenderer mViewToGLRenderer;
    ...
    // drawing magic@Overridepublicvoiddraw( Canvas canvas ) {
        //returns canvas attached to gl texture to draw onCanvasglAttachedCanvas= mViewToGLRenderer.onDrawViewBegin();
        if(glAttachedCanvas != null) {
            //translate canvas to reflect view scrollingfloatxScale= glAttachedCanvas.getWidth() / (float)canvas.getWidth();
            glAttachedCanvas.scale(xScale, xScale);
            glAttachedCanvas.translate(-getScrollX(), -getScrollY());
            //draw the view to provided canvassuper.draw(glAttachedCanvas);
        }
        // notify the canvas is updated
        mViewToGLRenderer.onDrawViewEnd();
    }

    ...
}


publicclassViewToGLRendererimplementsGLSurfaceView.Renderer{

    private SurfaceTexture mSurfaceTexture;
    private Surface mSurface;

    privateint mGlSurfaceTexture;
    private Canvas mSurfaceCanvas;

    ...

    @OverridepublicvoidonDrawFrame(GL10 gl){
        synchronized (this){
            // update texture
            mSurfaceTexture.updateTexImage();
        }
   }

    @OverridepublicvoidonSurfaceChanged(GL10 gl, int width, int height){
        releaseSurface();
        mGlSurfaceTexture = createTexture();
        if (mGlSurfaceTexture > 0){
            //attach the texture to a surface.//It's a clue class for rendering an android view to gl level
            mSurfaceTexture = newSurfaceTexture(mGlSurfaceTexture);
            mSurfaceTexture.setDefaultBufferSize(mTextureWidth, mTextureHeight);
            mSurface = newSurface(mSurfaceTexture);
        }

    }

    public Canvas onDrawViewBegin(){
        mSurfaceCanvas = null;
        if (mSurface != null) {
            try {
                mSurfaceCanvas = mSurface.lockCanvas(null);
            }catch (Exception e){
                Log.e(TAG, "error while rendering view to gl: " + e);
            }
        }
        return mSurfaceCanvas;
    }

    publicvoidonDrawViewEnd(){
        if(mSurfaceCanvas != null) {
            mSurface.unlockCanvasAndPost(mSurfaceCanvas);
        }
        mSurfaceCanvas = null;
    }
}

The demo output screenshot:

Solution 2:

Yes is it certainly possible, I have written up a how-to here; http://www.felixjones.co.uk/neo%20website/Android_View/

However for static elements that won't change, the bitmap option may be better.

Solution 3:

At least someone managed to render text this way:

Rendering Text in OpenGL on Android

It describes the method I used for rendering high-quality dynamic text efficiently using OpenGL ES 1.0, with TrueType/OpenType font files.

[...]

The whole process is actually quite easy. We generate the bitmap (as a texture), calculate and store the size of each character, as well as it's location on the texture (UV coordinates). There are some other finer details, but we'll get to that.

OpenGL ES 2.0 Version: https://github.com/d3kod/Texample2

Post a Comment for "Is It Possible To Render An Android View To An Opengl Fbo Or Texture?"