Skip to content
This repository has been archived by the owner on Jan 1, 2020. It is now read-only.

double check that alpha works correctly #12

Closed
gre opened this issue Sep 22, 2015 · 6 comments
Closed

double check that alpha works correctly #12

gre opened this issue Sep 22, 2015 · 6 comments

Comments

@gre
Copy link
Owner

gre commented Sep 22, 2015

I think something is weird with alpha in gl-react-native.

We need to make a test proof example that alpha works correctly (draw a linear black opacity=100% to black opacity=0% over an image, same with white. compare with the same effect done with gimp also do the same on gl-react side).

@gre
Copy link
Owner Author

gre commented Sep 24, 2015

(use case: http://i.imgur.com/mp79p5T.png over http://i.imgur.com/S22HNaU.png )

screen shot 2015-09-24 at 15 31 11

- **the 2 top results are right**: transparent `

` (so native UIImage) over the rainbow _(both tested using an UIImage (left) & a GLKView (right))_ - **the 2 bottom results are wrong**: transparent GLKView over the rainbow.

the alpha seems to combine in a non linear way using a GLKView on top of anything.

currently using this formula:

  glEnable(GL_BLEND);
  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

is there something wrong here?

(code https://github.com/ProjectSeptemberInc/gl-react-native/blob/424c7cf25c03a20e5b0471b5a0fc36b202811cb5/Examples/Tests/index.ios.js#L89-L115 )

@gre
Copy link
Owner Author

gre commented Sep 24, 2015

This bug is not present in gl-react. WebGL does not have the same issue. No I was wrong, in more advanced use-case it happens too

screen shot 2015-09-24 at 15 49 16

@gre
Copy link
Owner Author

gre commented Sep 24, 2015

I think this fix the issue: I was wrong too...

blending should not be enabled during fbo drawings

    glDisable(GL_BLEND);
    recDraw(_renderData);
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

my guess is otherwise blending will accumulates during last bindFramebuffer ( null )

@gre gre closed this as completed in 8267b0c Sep 24, 2015
@gre gre reopened this Sep 24, 2015
@gre
Copy link
Owner Author

gre commented Sep 24, 2015

      glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
      glClearColor(0.0, 0.0, 0.0, 0.0);
      glClear(GL_COLOR_BUFFER_BIT);

      glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
      glDrawArrays(GL_TRIANGLES, 0, 6);

( http://stackoverflow.com/questions/2171085/opengl-blending-with-previous-contents-of-framebuffer/18497511#18497511 )

this is a complex problem. not sure about the final solution yet. For now it works but all shaders MUST be premultiplied alpha...

@gre
Copy link
Owner Author

gre commented Sep 25, 2015

there is now a premultipliedAlpha prop that allows to specify if the classical non-premultiplied alpha is preferred.
That way we can make this part conditional:

      if (renderData.premultipliedAlpha)
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
      else
        glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);

now i'm not super happy with this and especially in the gl-react WebGL implementation, it seems to not work in Safari..

Still looking for a better solution

@gre
Copy link
Owner Author

gre commented Sep 25, 2015

basically it seems the problem fundamentally exists because textures loaded are automatically preloaded.

because kCGImageAlphaPremultipliedLast is used here https://github.com/ProjectSeptemberInc/gl-react-native/blob/master/RNGL/GLTexture.m#L37 .
However, I can't find a way to disable this in iOS.. see also: https://www.safaribooksonline.com/library/view/iphone-3d-programming/9781449388133/ch06s02.html

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

1 participant