简体   繁体   English

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

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

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

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 .我汇集了一个完整的演示项目,它以一种有效的方式实时渲染 GL 纹理的视图,可以在这个 repo 中找到。 It shows how to render WebView to GL texture in real time as an example.它以如何将 WebView 实时渲染为 GL 纹理为例。

Also a brief code for this can look like the following (taken from the demo project from the repo above):还有一个简短的代码如下(取自上述 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;
    }
}

The demo output screenshot:演示输出截图:

Yes is it certainly possible, I have written up a how-to here;是的,这当然可能,我在这里写了一个操作方法; http://www.felixjones.co.uk/neo%20website/Android_View/ http://www.felixjones.co.uk/neo%20website/Android_View/

However for static elements that won't change, the bitmap option may be better.但是对于不会改变的静态元素,位图选项可能更好。

At least someone managed to render text this way:至少有人设法以这种方式呈现文本:

Rendering Text in OpenGL on Android 在 Android 上的 OpenGL 中渲染文本

It describes the method I used for rendering high-quality dynamic text efficiently using OpenGL ES 1.0, with TrueType/OpenType font files.它描述了我使用 OpenGL ES 1.0 和 TrueType/OpenType 字体文件高效渲染高质量动态文本的方法。

[...] [...]

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).我们生成位图(作为纹理),计算并存储每个字符的大小,以及它在纹理上的位置(UV 坐标)。 There are some other finer details, but we'll get to that.还有一些其他更精细的细节,但我们会谈到这一点。

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

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

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