是否有可能将Android视图呈现给OpenGL FBO或纹理?

是否有可能呈现一个视图(比如说一个WebView)给一个FBO,以便它可以用作OpenGL构图中的纹理?

Solutions Collecting From Web of "是否有可能将Android视图呈现给OpenGL FBO或纹理?"

是的,这是肯定可能的,我已经写了一个如何在这里; http://www.felixjones.co.uk/neo%20website/Android_View/

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

完整的演示项目,以高效的方式实时呈现GL纹理的视图可以在这个回购中find。 它演示了如何实时渲染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; } } 

演示输出截图:

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

在Android上的OpenGL中渲染文本

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

[…]

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

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