1. Hey! Guest! The 34th GMC Jam will take place between August 22nd, 12:00 UTC (Thursday noon) and August 26th, 12:00 UTC (Monday noon). Why not join in! Click here to find out more!
    Dismiss Notice

Keeping alpha when drawing to a surface

Discussion in 'Programming' started by TheSnidr, Oct 30, 2017.

  1. TheSnidr

    TheSnidr Heavy metal viking dentist GMC Elder

    Joined:
    Jun 21, 2016
    Posts:
    462
    I know I've done this before, but I can't figure out how to draw a sprite to a surface while maintaining its alpha value. I have to do this because converting from sprite to buffer involves drawing the sprite to the surface.
    I use the alpha channel as a specular map, and needless to say, it needs to stay the same value whenever I convert to a buffer and back.

    Drawing transparent sprites to a surface using the default drawing options messes up the alpha completely. So... how do I draw to a surface without blending the alphas?
     
  2. Simon Gust

    Simon Gust Member

    Joined:
    Nov 15, 2016
    Posts:
    3,149
  3. DukeSoft

    DukeSoft Member

    Joined:
    Jun 20, 2016
    Posts:
    769
    There is a function to disable drawing to the alpha channel all together - draw_set_color_write_enable(red, green, blue, alpha);

    I know it works in 1.4, but I'm not sure that the GMS2 equivilant is - maybe try making a small project with just that line, and importing it in GMS2. (I wish there was a compatibility script overview somewhere :p )

    EDIT: Nevermind, didn't read well.

    I think I do this as well and did it using draw_set_blend_mode(). I'll have to check in my project, will return to you later.
     
    TheSnidr likes this.
  4. DukeSoft

    DukeSoft Member

    Joined:
    Jun 20, 2016
    Posts:
    769
    Victorstk and CMAllen like this.
  5. flyingsaucerinvasion

    flyingsaucerinvasion Member

    Joined:
    Jun 20, 2016
    Posts:
    2,088
    what if you use

    draw_set_blend_mode_ext( bm_one, bm_zero );

    what I'd really like to find is a blending mode that works the same as stacking multiple layers using normal blending mode in photoshop.
     
  6. CMAllen

    CMAllen Member

    Joined:
    Mar 2, 2017
    Posts:
    855
    Agreed. Pretty much everyone would like that. But, then, many of us would also like an easy way to access both the source and destination pixel data in a shader. From what I've read on the matter, doing that is at odds with some restrictions/limitations on shaders themselves -- reading from and writing to the same pixel can cause errors and produces unreliable results. Still, it must be possible to define other blending modes.
     
  7. flyingsaucerinvasion

    flyingsaucerinvasion Member

    Joined:
    Jun 20, 2016
    Posts:
    2,088
    Oh by the way I should elaborate on the comment i made about photoshop's normal blending mode to include the fact that I want it to work even if the opacity of the surface starts at zero, (i.e. building up opacity by adding layers). I struggled for days to sort that out, eventually I gave up. Sometime I'll need to come back to that problem, at which point I hope I've found a solution.
     
  8. CMAllen

    CMAllen Member

    Joined:
    Mar 2, 2017
    Posts:
    855
    It hasn't been mentioned here (and I don't know how applicable it is to what you're trying to accomplish) but there's also gpu_set_blendendable(). This disables draw blending entirely.
     
  9. flyingsaucerinvasion

    flyingsaucerinvasion Member

    Joined:
    Jun 20, 2016
    Posts:
    2,088
    well not exactly, I want alpha to blend, I just want to be able to control the way in which it happens. If only we could set the constants for blend_mode on a per channel basis! Maybe in 10 years, that'll be how all gpus work.
     
  10. Tsa05

    Tsa05 Member

    Joined:
    Jun 21, 2016
    Posts:
    548
  11. Victorstk

    Victorstk Member

    Joined:
    Jul 5, 2018
    Posts:
    21
    This fixed my issue

    i had my applications surface being drawn on a separate surface and the background sprite was transparent and it was having issues, but this solved my problems instantly :p:p:p:p
     
  12. Joe Ellis

    Joe Ellis Member

    Joined:
    Aug 30, 2016
    Posts:
    891
    Woah Snidy this is crazy seeing you ask for help,
    I think I've got the answer, btw I didn't read any of the previous comments because I feel like they're "beneath me"

    Create a surface and clear it with 0 color and 0 alpha,
    then enable additive blend mode, then draw the sprite, it should add everything the sprite has, onto the completely 0, 0 surface,

    Btw if I'm wrong, I was only joking about the beneath thing
     
    TheSnidr likes this.
  13. TheSnidr

    TheSnidr Heavy metal viking dentist GMC Elder

    Joined:
    Jun 21, 2016
    Posts:
    462
    Lol, necrobumpers! xD
    @flyingsaucerinvasion 's solution ended up working perfectly, I don't remember why I didn't reply to this topic sooner.
     
  14. Joe Ellis

    Joe Ellis Member

    Joined:
    Aug 30, 2016
    Posts:
    891
    Oh that was Victostk's fault, I thought this was like now, btw is my answer wrong? I'd like to know if it is, I mean I could test it myself, but I feel like it's "beneath me"
     
  15. TheSnidr

    TheSnidr Heavy metal viking dentist GMC Elder

    Joined:
    Jun 21, 2016
    Posts:
    462
    bm_add uses bm_src_alpha for the source and bm_one for the target. The former multiplies the colour with the alpha value, so if the texel is white but has an alpha of .5, the final pixel will end up gray. So I don't think it would work with bm_add.
     
  16. Joe Ellis

    Joe Ellis Member

    Joined:
    Aug 30, 2016
    Posts:
    891
    Ah that makes sense, but my logic IS correct, I basically want a blend mode that just simply adds each rgba component with no bias from the alpha, like just adding data, add those to a 0, 0 surface and it should equal the image you've pasted
    That's probably an annoying thing that shaders do where they always use the alpha even if you don't include it in the vec, (vec3 rgb, vec4. rgba)
    But would disabling alphablend fix this? sorry I'm asking questions just in my train of thought, I can obviously find out for myself,
     
  17. TheSnidr

    TheSnidr Heavy metal viking dentist GMC Elder

    Joined:
    Jun 21, 2016
    Posts:
    462
    Setting a blend mode of (bm_one, bm_one) adds all values together without putting alpha into the mix
     
    Joe Ellis likes this.
  18. Joe Ellis

    Joe Ellis Member

    Joined:
    Aug 30, 2016
    Posts:
    891
    @TheSnidr Cool! that's just what I wanted :)

    And that would be a correct answer to your op, (Late I know, but, glad I could help man)
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice