Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Having a transparent background using opengles

I'm developing a videogame using Opengl-es for android. I'm having an issue with the background of the images I use, since it draws the color of the background.

Here my renderer:

@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {

    //Initialize GL:                
    gl.glDisable(GL10.GL_DITHER);
    gl.glEnable(GL10.GL_TEXTURE_2D);            //Enable Texture Mapping ( NEW )
    gl.glShadeModel(GL10.GL_SMOOTH);            //Enable Smooth Shading
    gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f);    //Black Background
    gl.glClearDepthf(1.0f);                     //Depth Buffer Setup
    gl.glEnable(GL10.GL_DEPTH_TEST);            //Enables Depth Testing
    gl.glEnable(GL10.GL_CULL_FACE);
    gl.glDepthFunc(GL10.GL_LEQUAL);             //The Type Of Depth Testing To Do
    gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST); //Really Nice Perspective Calculations //<---- SI LAG, probar GL_FASTEST

    LoadTextures(gl);
}

Here the draw method:

@Override
public void onDrawFrame(GL10 gl) {

    gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
    gl.glEnable(GL10.GL_BLEND);
    gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
    gl.glLoadIdentity();

    for(Entity e : entities)
    {
        if(e.IsActive())
        {
            gl.glPushMatrix();
                e.Draw(gl);
            gl.glPopMatrix();
        }
    }

    gl.glDisable(GL10.GL_BLEND);        
    isDrawing = false;
}

And the draw method of each entity:

gl.glColor4f(red, green, blue, alpha);

    // bind the previously generated texture
    gl.glBindTexture(GL10.GL_TEXTURE_2D, textureId);

    gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
    gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);

    // Point to our vertex buffer
    gl.glVertexPointer(2, GL10.GL_FLOAT, 0, screenBuffer); //2 dimensiones
    gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer);

    // Draw the vertices as triangle strip
    gl.glDrawArrays(GL10.GL_TRIANGLE_FAN, 0, 4); //12 /3 -> 12 es el tamano del vector de vertices del cuadrado

    //Disable the client state before leaving
    gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
    gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);

I also have this in my GLSurfaceView:

setEGLConfigChooser(8,8,8,8,16,0);
    renderer = new GRenderer(c);
    setRenderer(renderer);        
    setRenderMode(RENDERMODE_WHEN_DIRTY);
    getHolder().setFormat(PixelFormat.TRANSLUCENT);
    setZOrderOnTop(true);

But it doesn't work. Which color should I use to achieve a translucent background? Any error in the code? Thanks

like image 877
Frion3L Avatar asked Dec 29 '25 22:12

Frion3L


1 Answers

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.

like image 113
GraphicsMuncher Avatar answered Jan 01 '26 12:01

GraphicsMuncher



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!