3

I am working on a game project using LibGDX. Now I'm facing a problem I can't understand; let me explain what I've got before coming to the actual problem:

  1. Static background C (tiled)
  2. Moving background B (tiled)
  3. Moving background A (tiled)
  4. Characters, entities
  5. Walls (tiled)
  6. HUD

The render loop goes as follows:

  1. Render 2 to 5 in previous list to a transparent FBO
  2. Render the lightmap to another FBO (using box2dLights)
  3. Blend both products together using a custom shader
  4. Draw the static background
  5. Draw the blended texture from step 3
  6. Draw the HUD

The problem I'm facing is that I obtain a white sprite when rendering background B (not a white block; it is more like a rgba(1, 1, 1, x). Then the background B is blended correctly with the lightmap (render step 3), but I cannot get its real color, just white:

The background B should appear dark blue and with a texture; it appears as a white sprite (blended with the lightmap)

The background B should appear dark blue and with a texture; it appears as a white sprite (blended with the lightmap). Behind background A, at the left of the picture (and also where the "SHADOW" text is), you can still see background B in a purplish tone, result of the blending with the light map, but please note it has no texture and it seems to me like if the texture is just plain white + alpha.

The render code:

    @Override
    public void render(float delta) {
        Gdx.gl.glClearColor(0f, 0f, 0f, 1f);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        /* 1. Update Physics and lights */
        if (worldFixedStep(delta)) {
            rayHandler.update();
        }
        updateCameras();
        cameraMatrixCopy.set(camera.combined);
        rayHandler.setCombinedMatrix(cameraMatrixCopy.scale(Globals.BOX_TO_WORLD, Globals.BOX_TO_WORLD, 1.0f), camera.position.x,
                camera.position.y, camera.viewportWidth * camera.zoom * Globals.BOX_TO_WORLD,
                camera.viewportHeight * camera.zoom * Globals.BOX_TO_WORLD);

        rayHandler.render();
        final Texture lightMap = rayHandler.getLightMapTexture();

        fbo.begin();
        {
            Gdx.gl.glClearColor(0f, 0f, 0f, 0f);
            Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

            /* 2. Draw the backgrounds */
            batch.enableBlending();
            batch.setBlendFunction(GL20.GL_ONE, GL20.GL_ONE_MINUS_SRC_ALPHA);

            batch.setProjectionMatrix(bgCamera2.combined); //Background B
            batch.begin();
            bTileMapRenderer.setView(bgCamera2);
            bTileMapRenderer.renderTileLayer(tilesBg2Layer);
            batch.end();

            batch.setProjectionMatrix(bgCamera1.combined); //Background A
            batch.begin();
            bTileMapRenderer.setView(bgCamera1);
            bTileMapRenderer.renderTileLayer(tilesBg1Layer);
            batch.end();

            batch.setBlendFunction(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA); //FIXME this is where to touch to avoid gray alpha'ed borders.
            batch.setProjectionMatrix(camera.combined);
            batch.begin();

            (...) //Draw everything else that needs to be blended with the light map

            batch.end();
        }
        fbo.end();

        /* 3. Render the static background (background C) */
        batch.setProjectionMatrix(bgCameraStatic.combined);
        batch.disableBlending();
        batch.begin();
        bTileMapRenderer.setView(bgCameraStatic);
        bTileMapRenderer.renderTileLayer(tilesBg3Layer);
        batch.end();

        /* 4. Blend the frame buffer's texture with the light map in a fancy way */
        Gdx.gl20.glActiveTexture(GL20.GL_TEXTURE0);
        fboRegion.getTexture().bind();

        Gdx.gl20.glActiveTexture(GL20.GL_TEXTURE1);
        lightMap.bind();

        Gdx.gl20.glEnable(Gdx.gl20.GL_BLEND);
        Gdx.gl20.glBlendFunc(Gdx.gl20.GL_SRC_ALPHA, Gdx.gl20.GL_ONE_MINUS_SRC_ALPHA);
        lightShader.begin();
        lightShader.setUniformf("ambient_color", bgColor[0], bgColor[1], bgColor[2]);
        lightShader.setUniformi("u_texture0", 0);
        lightShader.setUniformi("u_texture1", 1);
        fullScreenQuad.render(lightShader, GL20.GL_TRIANGLE_FAN, 0, 4);
        lightShader.end();

        Gdx.gl20.glDisable(Gdx.gl20.GL_BLEND);

        hud.draw();
    }

I just can't understand why this background is drawn white but still with its alpha data. The image is a premultiplied alpha texture, but again I cannot see why could this affect the color rendering.

Any help would be much appreciated.

Cheers

Azurlake
  • 612
  • 1
  • 6
  • 29

0 Answers0