繁体   English   中英

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

[英]Is it possible to render an Android View to an OpenGL FBO or texture?

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

我汇集了一个完整的演示项目,它以一种有效的方式实时渲染 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;
    }
}

演示输出截图:

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

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

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

在 Android 上的 OpenGL 中渲染文本

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

[...]

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM