是否可以将Android视图渲染为OpenGL FBO或纹理?

时间:2012-09-19 16:56:49

标签: android opengl-es

是否可以将视图(例如,WebView)呈现给FBO,以便它可以用作OpenGL合成中的纹理?

3 个答案:

答案 0 :(得分:11)

是的,这当然是可能的,我在这里写了一个方法; http://www.felixjones.co.uk/neo%20website/Android_View/

但是对于不会改变的静态元素,位图选项可能更好。

答案 1 :(得分:9)

可以在this repo中找到完整的演示项目,该项目以高效的方式实时呈现GL纹理。它显示了如何实时渲染WebView到GL纹理。

此处的简短代码如下所示(取自上面回购的演示项目):

public class GLWebView extends WebView {

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

    ...
}


public class ViewToGLRenderer implements GLSurfaceView.Renderer{

    private SurfaceTexture mSurfaceTexture;
    private Surface mSurface;

    private int mGlSurfaceTexture;
    private Canvas mSurfaceCanvas;

    ...

    @Override
    public void onDrawFrame(GL10 gl){
        synchronized (this){
            // update texture
            mSurfaceTexture.updateTexImage();
        }
   }

    @Override
    public void onSurfaceChanged(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 = new SurfaceTexture(mGlSurfaceTexture);
            mSurfaceTexture.setDefaultBufferSize(mTextureWidth, mTextureHeight);
            mSurface = new Surface(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;
    }

    public void onDrawViewEnd(){
        if(mSurfaceCanvas != null) {
            mSurface.unlockCanvasAndPost(mSurfaceCanvas);
        }
        mSurfaceCanvas = null;
    }
}

演示输出截图:

答案 2 :(得分:0)

至少有人设法以这种方式呈现文字:

Rendering Text in OpenGL on Android

  

它描述了我使用OpenGL ES 1.0和TrueType / OpenType字体文件高效渲染高质量动态文本高效的方法。

[...]

  

整个过程实际上非常简单。我们生成位图(作为纹理),计算并存储每个字符的大小,以及它在纹理上的位置(UV坐标)。还有一些其他更精细的细节,但我们会做到这一点。

OpenGL ES 2.0版本:https://github.com/d3kod/Texample2