Sushi271
Sushi271

Reputation: 554

Fading-out texture looks bad over the same solid texture when rendered

I'm writing a mobile game for android, using OpenGL ES 2.0. Currently I'm trying to make a scroll with spells, that should in-game look like this:

Scroll design

It would be an element of GUI, where player could select spell which he currently wants to cast. As you can see there can be more spells, than scroll's width allows, so player should be able to scroll them left-right to show more. These near the edge should fade out.

My game has sort of two "layers": GUI and the game itself. I render both in single GUISurfaceView: the game-world in perspective projection and GUI in orthogonal, in front of the game-world. In fact every GUI element has the same mesh: flat plane made of two triangular faces. When I need rectangle I just modify scale matrix.

I figured I can achieve the effect above by having 3 layers of GUI:

  1. foreground (it's fading with alpha):

Left edge of the foreground................................................................................................. Right edge of the foreground

  1. spells below foreground:

Fireball Fireball Fireball Fireball Fireball Fireball Fireball Fireball

  1. background below spells:

Left edge of the background Middle background Right edge of the background

As you can see, edges of the background and egdes of the foreground are exactly the same, except the alpha channel. So they should align perfectly and allow for such fade-out effect. And they are - at least in Paint.NET (I uploaded .pdn file with all 3 layers of scroll here). But not in rendered OpenGL. Instead, it looks like this (without any spell-icons):

Rendered scroll

As you can see, there are darker areas where foregrounds are gradienting with alpha. As if foreground and background weren't aligned perfectly. I absolutely can't understand why.

Vertex shader:

    uniform mat4 uMVPMatrix;
    uniform mat4 uMVMatrix;
    uniform mat4 uNormalMatrix;

    attribute vec4 aPosition;
    attribute vec3 aNormal;
    attribute vec4 aColor;
    attribute vec2 aTexCoord;

    varying vec4 vColor;
    varying vec3 vNormal;
    varying vec3 vPosition;
    varying vec2 vTexCoord;

    void main() 
    {
        vColor = aColor;
        vNormal = aNormal;
        vTexCoord = aTexCoord;

        vPosition = vec3(uMVMatrix * aPosition);
        gl_Position = uMVPMatrix * aPosition;
    }

Fragment shader:

    precision mediump float;

    uniform sampler2D uTexture;
    uniform bool uHasTexture;
    uniform vec4 uDiffuse;

    varying vec4 vColor;
    varying vec3 vNormal;
    varying vec3 vPosition;
    varying vec2 vTexCoord;

    void main()
    {
        vec4 texColor;
        if (uHasTexture)
            texColor = texture2D(uTexture, vTexCoord);
        else texColor = vec4(0, 0, 0, 0);

        vec4 color = vColor * texColor;
        gl_FragColor = vec4(color.rgb * uDiffuse.rgb, texColor.a * uDiffuse.a);
    }

Do you have any ideas why does this happen? Maybe for some reason it just can't be done the way I figured? If so, could you suggest any other way to do this?

Upvotes: 1

Views: 192

Answers (1)

ovikoomikko
ovikoomikko

Reputation: 577

Probably your texture does not have the same colors as the texture beneath. I.e. the color bits are also fading, not only the alpha part.

Then you should use GLES20.glBlendFunc(GLES20.GL_ONE,GLES20.GL_ONE_MINUS_SRC_ALPHA); as your blending function.

Upvotes: 2

Related Questions