Part of the Khronos Group

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 1 of 1

Thread: Point sprite alpha blending issue (Android / OpenGL ES 2.0)

  1. #1
    Junior Member Newbie
    Join Date
    Jul 2017
    London, United Kingdom

    Point sprite alpha blending issue (Android / OpenGL ES 2.0)

    Hey guys! I've recently started looking into OpenGL ES for Android and am working on a drawing app. I've implemented some basics such as point sprites, path smoothing and FBO for double buffering. At the moment I am playing around with the glBlendFunc, more specifically when I put two textures close to each other with the same color/alpha values, the alpha gets added so it appears darker at the intersection of the sprites. This is a problem because the stroke opacity is not preserved if a lot of points are close together, as the color tends to more opaque rather than staying with the same opacity. Is there a way to make the textures have the same color on the intersection, i.e. have the same alpha value for the intersecting pixels, but keep the alpha values for the rest of the pixels?

    Here's how I've done the relevant parts of the app:
    for drawing the list of point sprites I use blending like this:

    Code :

    the app uses an FBO with a texture, where it renders each brush stroke first and then this texture is rendered to the main screen. The blending func there is:

    Code :

    OpenGL ES 2.0 doesn't support alpha masking;
    there is no DEPTH_TEST function used anywhere in the app;
    the textures for the point sprites are PNGs with transparent backgrounds;
    the app supports texture masking which means one texture is used for the shape and one texture is used for the content;
    my fragment shader looks like this:

    Code :
    precision mediump float;
    uniform sampler2D uShapeTexture;
    uniform sampler2D uFillTexture;
    uniform float vFillScale;
    varying vec4 vColor;
    varying float vShapeRotation;
    varying float vFillRotation;
    varying vec4 vFillPosition;
    vec2 calculateRotation(float rotationValue) {
        float mid = 0.5;
        return vec2(cos(rotationValue) * (gl_PointCoord.x - mid) + sin(rotationValue) * (gl_PointCoord.y - mid) + mid,
                    cos(rotationValue) * (gl_PointCoord.y - mid) - sin(rotationValue) * (gl_PointCoord.x - mid) + mid);
    void main() {
        // Calculations.
        vec2 rotatedShape = calculateRotation(vShapeRotation);
        vec2 rotatedFill = calculateRotation(vFillRotation);
        vec2 scaleVector = vec2(vFillScale, vFillScale);
        vec2 positionVector = vec2(vFillPosition[0], vFillPosition[1]);
        // Obtain colors.
        vec4 colorShape = texture2D(uShapeTexture, rotatedShape);
        vec4 colorFill = texture2D(uFillTexture, (rotatedFill * scaleVector) + positionVector);
        gl_FragColor = colorShape * colorFill * vColor;

    my vertext shader is this:

    Code :
    attribute vec4 aPosition;
    attribute vec4 aColor;
    attribute vec4 aJitter;
    attribute float aShapeRotation;
    attribute float aFillRotation;
    attribute vec4 aFillPosition;
    attribute float aPointSize;
    varying vec4 vColor;
    varying float vShapeRotation;
    varying float vFillRotation;
    varying vec4 vFillPosition;
    uniform mat4 uMVPMatrix;
    void main() {
        // Sey position and size.
        gl_Position = uMVPMatrix * (aPosition + aJitter);
        gl_PointSize = aPointSize;
        // Pass values to fragment shader.
        vColor = aColor;
        vShapeRotation = aShapeRotation;
        vFillRotation = aFillRotation;
        vFillPosition = aFillPosition;

    I've tried playing around with the glBlendFunc parameters but I can't find the right combination to draw what I want. I've attached some images showing what I would like to achieve and what I have at the moment. A
    Attached Thumbnails Attached Thumbnails Click image for larger version. 

Name:	intersection.jpg 
Views:	19 
Size:	7.0 KB 
ID:	2422  

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts