13

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

4

3 回答 3

17

我汇集了一个完整的演示项目,它以一种有效的方式实时渲染 GL 纹理的视图,可以在这个 repo中找到。以如何将 WebView 实时渲染为 GL 纹理为例。

对此的简短代码也可能如下所示(取自上面 repo 的演示项目):

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;
    }
}

演示输出截图:

于 2015-03-15T21:54:44.610 回答
14

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

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

于 2014-06-26T18:43:11.027 回答
0

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

在 Android 上的 OpenGL 中渲染文本

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

[...]

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

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

于 2013-10-15T07:47:30.873 回答