android OpenGL ES实现渲染到透明的纹理 render to transparent texture

PC上OpenGL渲染到纹理,很容易得到透明背景,但是在android上OpenGL ES渲染出来是黑色背景,对于这个问题,想了两个解决办法。

1> 让android的OpenGL ES环境支持RGBA,默认android系统给出的渲染环境是RGB565的,这是出于性能原因。如果framebuffer不支持alpha通道,renderToTexture当然得不到透明纹理。

        在androidmanifest.xml文件里改用OpenGL ES2.0,并为GLSurefaceView.Render改用RGBA8888配置,仍然得不到透明图像。尝试失败。

2> 第二种憋足的办法,读取render2texture的pixels,然后手动修改其黑色背景为透明。渲染后使用glReadPixels读取,但是这种办法效率低下,每一帧都这样干是不可能的。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
实现模糊效果,可以使用双重渲染技术,即将OpenGL渲染到SurfaceTexture,再将SurfaceTexture渲染到屏幕上。 下面是一个简单的示例代码: 1. 首先,在XML布局文件中添加一个TextureView: ``` <TextureView android:id="@+id/textureView" android:layout_width="match_parent" android:layout_height="match_parent" /> ``` 2. 在Activity中获取TextureView并设置监听器: ``` private TextureView textureView; private SurfaceTexture surfaceTexture; private int textureId; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textureView = findViewById(R.id.textureView); textureView.setSurfaceTextureListener(surfaceTextureListener); } private TextureView.SurfaceTextureListener surfaceTextureListener = new TextureView.SurfaceTextureListener() { @Override public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) { surfaceTexture = surface; textureId = createTexture(); startRender(); } @Override public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) { } @Override public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) { stopRender(); surfaceTexture = null; return true; } @Override public void onSurfaceTextureUpdated(SurfaceTexture surface) { } }; ``` 3. 创建OpenGL纹理: ``` private int createTexture() { int[] textures = new int[1]; GLES20.glGenTextures(1, textures, 0); int textureId = textures[0]; GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureId); GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR); GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR); GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE); GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE); return textureId; } ``` 4. 开始渲染: ``` private RenderThread renderThread; private void startRender() { if (renderThread == null) { renderThread = new RenderThread(); renderThread.start(); } } private void stopRender() { if (renderThread != null) { renderThread.stopRender(); renderThread = null; } } private class RenderThread extends Thread { private volatile boolean isRender = true; @Override public void run() { super.run(); EglCore eglCore = new EglCore(); WindowSurface windowSurface = new WindowSurface(eglCore, surfaceTexture); GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f); while (isRender) { GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT); // 在这里进行OpenGL渲染操作 windowSurface.swapBuffers(); } windowSurface.release(); eglCore.release(); } public void stopRender() { isRender = false; } } ``` 5. 最后,在onDrawFrame方法中进行双重渲染: ``` @Override public void onDrawFrame(GL10 gl) { GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f); GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT); // 在这里进行OpenGL渲染操作 SurfaceTexture surfaceTexture = textureView.getSurfaceTexture(); surfaceTexture.updateTexImage(); // 将SurfaceTexture渲染到屏幕上 GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, 0); GLES20.glViewport(0, 0, getWidth(), getHeight()); GLES20.glUseProgram(programId); int uTextureSamplerLocation = GLES20.glGetUniformLocation(programId, "uTextureSampler"); GLES20.glActiveTexture(GLES20.GL_TEXTURE0); GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, textureId); GLES20.glUniform1i(uTextureSamplerLocation, 0); GLES20.glEnableVertexAttribArray(aPositionLocation); GLES20.glVertexAttribPointer(aPositionLocation, 2, GLES20.GL_FLOAT, false, 0, vertexBuffer); GLES20.glEnableVertexAttribArray(aTextureCoordLocation); GLES20.glVertexAttribPointer(aTextureCoordLocation, 2, GLES20.GL_FLOAT, false, 0, textureBuffer); GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4); GLES20.glDisableVertexAttribArray(aPositionLocation); GLES20.glDisableVertexAttribArray(aTextureCoordLocation); } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值