Android OpenGL ES Transparent Background
Just some simple changes that I did to get this to work.
On my GLSurfaceView.Renderer
:
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
gl.glDisable(GL10.GL_DITHER);
gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT,
GL10.GL_FASTEST);
gl.glClearColor(0,0,0,0);
gl.glEnable(GL10.GL_CULL_FACE);
gl.glShadeModel(GL10.GL_SMOOTH);
gl.glEnable(GL10.GL_DEPTH_TEST);
}
On my GLSurfaceView
:
setEGLConfigChooser(8, 8, 8, 8, 16, 0);
getHolder().setFormat(PixelFormat.TRANSLUCENT);
OpenGL ES 2.0 render to texture with transparent background
The posted code looks perfectly fine. You will need to make sure that the texture you use for the color render target (renderTex[0]
) has an alpha component.
Note that the number of texture formats in ES 2.0 that are guaranteed to be color-renderable is very limited. The only two with an alpha component (see table 4.5. in the spec) are GL_RGBA4
and GL_RGB5_A1
. Most notably, this does not include GL_RGBA
with 8 bits per component.
So for defining a color-renderable texture with alpha component that is guaranteed to work across all ES 2.0 implementations, you will have to use GL_RGBA
for the internal format, and GL_UNSIGNED_SHORT_4_4_4_4
or GL_UNSIGNED_SHORT_5_5_5_1
for the type argument of glTexImage2D()
.
Most common devices (at least all the ones I have seen) do support the OES_rgb8_rgba8 extension, which adds support for rendering to 8 bit component textures. But if you want to be completely portable, you should check for the presence of this extension before using render targets with those formats.
Having a transparent background using opengles
For achieving alpha, your code seems okay in terms of OpenGL calls.
However, for images:
1) Your images need to have an alpha channel baked into them. RGB won't cut it - you need RGBA. This can easily be fixed in something like Photoshop, Gimp, or other image editor.
2) If you want translucency, you might be better off setting the alpha channel to some intermediate value and have your RGB set to whatever color you want to be the "translucent" blending color. Or you can create a custom blending function in your fragment shader to do something special with the alpha channel.
How to make libGDX background transparent in Android?
I finally made libGDX background transparent by the following code:
// add this to your AndroidApplication
AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
config.a = 8; // default is 0
View view = initializeForView(new YourApplicationAdapter(), config);
if (view instanceof SurfaceView) {
SurfaceView sv = (SurfaceView) view;
sv.setZOrderOnTop(true);
sv.getHolder().setFormat(PixelFormat.TRANSLUCENT);
}
setContentView(view);
// add this to your ApplicationAdapter
public void render() {
// other codes
ScreenUtils.clear(0, 0, 0, 0);
// other codes
}
And I find a good way to discuss what you want about libGDX by Discord.
Android OpenGL Transparent Texture Draws Black
After much troubleshooting, I was able to solve the problem on both devices (and presumably all devices) by adding texture wrapping as so:
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S, GL10.GL_CLAMP_TO_EDGE);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T, GL10.GL_CLAMP_TO_EDGE);
I'm not sure why this was necessary for two devices but not the other two, though.
Related Topics
Android Viewpager Get the Current View
Could Not Access the Package Manager. Is the System Running While Installing Android Application
Android:How to Update the Selector(Statelistdrawable) Programmatically
Measuring a View Before Rendering It
How to Simulate Accelerometer in Android Emulator
Webview Textarea Doesn't Pop Up the Keyboard
Disable Soft-Keyboard from Edittext But Still Allow Copy/Paste
Android Sdk Cannot Be Found by Flutter
How to Open the Options Menu Programmatically
How to Handle the Firebase Notification When App Is in Foreground
Android Kitkat 4.4 Hangouts Cannot Handle Sending Sms Intent
How to Change Color of Listview Items on Focus and on Click
Applying Effects on Video Being Played
Android Exporting to CSV and Sending as Email Attachment
Receiver Not Registered Exception Error
Android Studio: Application Installation Failed