如何在Flutter for Android中渲染OpenGL? [英] How to render OpenGL in Flutter for Android?

查看:1307
本文介绍了如何在Flutter for Android中渲染OpenGL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

if (call.method.equals("createVideoRenderer")) {
            TextureRegistry.SurfaceTextureEntry entry = textures.createSurfaceTexture();
            SurfaceTexture surfaceTexture = entry.surfaceTexture();
            this.surfaceTexture = surfaceTexture;

这是我的java方法调用,它创建我的SurfaceTexture并将其保存到我的类实例中.我一直在阅读有关SurfaceTexture的类,这是我的理解方式:

This is my java method call that creates my SurfaceTexture and saves it into my class instance. I've been reading about SurfaceTexture's class and this is how I understand it:

我需要调用surfaceTexture.setOnFrameAvailableListener(SurfaceTexture.OnFrameAvailableListener listener)来传递定义功能onFrameAvailable(SurfaceTexture surfaceTexture)SurfaceTexture.OnFrameAvailableListener实例.如果我理解正确,该函数将被updateTexImage()调用.所以我认为onFrameAvailable应该可以捕获图像并进行渲染?如果是这样,如何在其中调用OpenGL函数? OpenGL上下文在哪里?

I need to call surfaceTexture.setOnFrameAvailableListener(SurfaceTexture.OnFrameAvailableListener listener) to pass an instance of SurfaceTexture.OnFrameAvailableListener that defines the function onFrameAvailable(SurfaceTexture surfaceTexture). If I understood correctly, this function is called by updateTexImage(). So I think onFrameAvailable is supposed to grab the image and render it? If so, how can I call OpenGL functions inside it? Where's the OpenGL context?

我对如何渲染它完全迷失了.我发现的所有Android OpenGL示例都使用曲面视图或类似的视图.我需要知道如何创建一个纯opengl上下文.

I'm totally lost on how to render it. All Android OpenGL examples that I find use surface views or something like that. I need to know how to create a pure opengl context.

我还认为,当调用onFrameAvailable时,surfaceTexture的纹理将绑定到OpenGL上下文,因此我不需要创建着色器,调整纹理参数等.我只需要对其渲染,也许调用.

I also think that when onFrameAvailable is called, the surfaceTexture's texture is bound to the OpenGL context, so I don't need to create shaders, tune texture parameters, etc. I just need to render to it, perhaps calling glTexImage2D.

那么如何使用Flutter给我的SurfaceTexture用glTexImage2D渲染像红色正方形一样简单的东西?

So how to render something as simple as a red square with glTexImage2D using this SurfaceTexture that Flutter gives me?

更新:

我发现了这一点: https://github.com/mogol/opengl_texture_widget_example

我成功地通过使用

渲染了颜色

and I successfully rendered a color by using

GLES20.glClearColor(0f, green, 0f, 1f);

GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);

但是,我想使用glTeximage2D.我尝试只添加

However, I want to use glTeximage2D. I tried simply adding

GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D, 0, GLES20.GL_RGBA, 100, 100,
                0, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, buffer);

但这没用

更新:

我做了一些研究,发现(我认为)android使用gl外部纹理,所以我尝试了:

I did some research and discovered (I think) that android uses gl external textures, so I tried:

    buffer = ByteBuffer.allocate(bufferSize);
    for (int i = 0; i < bufferSize; i = i+=4) {
        buffer.put((byte)255);
    }
    for (int i = 1; i < bufferSize-1; i = i+=4) {
        buffer.put((byte)0);
    }
    for (int i = 2; i < bufferSize-2; i = i+=4) {
        buffer.put((byte)0);
    }
    for (int i = 3; i < bufferSize-3; i = i+4) {
        buffer.put((byte)255);
    }
   buffer.rewind();
    GLES20.glTexParameteri(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE);
    GLES20.glTexParameteri(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE);
    GLES20.glTexParameteri(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR);
    GLES20.glTexParameteri(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
    GLES20.glTexImage2D(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, 0, GLES11Ext.GL_RGB8_OES, 100, 100, 0, GLES11Ext.GL_RGB8_OES, GLES20.GL_UNSIGNED_BYTE, buffer);

我应该看到红色图像,但是什么也看不到.

I should see a red image, but I see nothing.

我也尝试在glTexImage2D中使用GLES20.RGBA8,但是没有用.我也尝试用GLES11Ext.glEGLImageTargetTexture2DOES( GL_TEXTURE_EXTERNAL_OES, buffer )代替glTexImage2D,它也没有用.

I also tries using GLES20.RGBA8 in glTexImage2D but it didn't work. I also tried GLES11Ext.glEGLImageTargetTexture2DOES( GL_TEXTURE_EXTERNAL_OES, buffer ) in place of glTexImage2D and it also didn't work.

推荐答案

纹理在Android模拟器和设备上效果很好,但仅在iOS设备上有效,而在iOS Simulator上效果不佳.

Texture works well on Android Emulator and devices, but only on iOS device, not iOS Simulator.

嘿,我发现了一些对您有用的链接,我认为这可以解决您的问题

hey I found some useful links for you i think this solve your issue

检出:带有纹理小部件的OpenGL

这篇关于如何在Flutter for Android中渲染OpenGL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆