2 SetAlphas makes final image full alpha

Started by Sinjin, September 16, 2023, 18:38:21

Previous topic - Next topic

Sinjin

Lets say I have 2 images, any kind, now I try to draw them like this:
setalpha 0.2
drawimage pic1,0,0
setalpha 1-0.2
drawimage pic2,20,0
But then the background is shining through. What alpha value do I set for the 2nd image so the background is not shining through anymore?
I also tried it like this but its still not working as I wanted to:
setalpha al
drawimage pic1,0,0
setalpha 1-(1-al)*al
drawimage pic2,20,0
wait...the 2nd code is complete gibberish haha

Midimaster

Of course you set it back with

SetAlpha 1
SetAlpha uses absolut values:

Alpha goes from 0.00 (=unvisible) over 0.50 (=half transparent) upto 1.00 (=solid). A new SetAlpha() never depends on a prior SetAlpha()

...back from Egypt

Sinjin

#2
I think my title is wrong, I meant to be fully opaque with 2 images which blend over.
here is another example:
drawimage background,0,0
alp:+0.1
local al#=alp mod 1
setalpha al
drawimage pic1,0,0
setalpha 1-al
drawimage pic2,20,0
'sure i do this:
setalpha 1
I dont want the background image shining through.
The idea is for animations which are slower than 1 or the framerate. I like to blend them over like for the torches in my game to blend over more smoothly, but as it is the background is visible in between.

Midimaster

#3
This can only be done with some DrawSubImageRect(). You have set different alpha depending on the overlapping parts of the both images.



Older image:
The part, that not overlaps need to be draw with fading alpha. The part that overlaps need to be drawn with alpha=1

Newer image:
You can draw it with increasing alpha


OverBlend.gif

Graphics 800,600
SetBlend alphablend
Global fade:Float

SetColor 255,0,0
DrawRect 0,0,200,200
Local older:TImage = CreateImage(200,200,1,DYNAMICIMAGE)
GrabImage(older,0,0)

SetColor 0,255,0
DrawRect 0,0,200,200
Global newer:TImage = CreateImage(200,200,1,DYNAMICIMAGE)
GrabImage(newer,0,0)

Cls
SetColor 255,255,0
For Local i:Int=0 To 800 Step 30
DrawRect 0,i,800,1
DrawRect i,0,1,600
Next
Global back:TImage = CreateImage(800,600,1,DYNAMICIMAGE)
GrabImage(back,0,0)

SetColor 255,255,255
Repeat
Cls
SetAlpha 1
DrawImage Back,0,0

SetAlpha 1.0-fade
DrawImageRect    older, 100, 100,100,200
DrawSubImageRect older, 200,100,100,100 , 100,0,100,100
SetAlpha 1
DrawSubImageRect older, 200, 200,100,100 , 100,100,100,100
SetAlpha fade
DrawImage newer,200,200
fade:+0.01
Flip
Until AppTerminate()


...back from Egypt

Sinjin

#4
I see, so I guess it wouldnt really work if the overlapping part has transparent pixels in it, because it would stay at SetAlpha 1 and you would see whats happening. Hmm, if you would do it with 3 pictures then, might work like the 1st image stays at alpha 1, then do the next blending for the next 2 images. I mean in reality all parts are overlapping and at some point one of the older pictures has to fade out. But thanks midi.
Just implemented it with 3 pictures and it works fine, it doesnt make sence for every animation but fires look much better. I just did something like this:
    local fr#=frame-int(frame)
    setalpha 1-fr
    drawimage img,x,y,frame
    setalpha 1
    drawimage img,x,y,(frame+1) mod frames
    setalpha fr
    drawimage img,x,y,(frame+2) mod frames
    setalpha 1

Midimaster

@Sinjin  I have another very smooth idea for you:

paint 5 pictures:
1st image: 2 pixel left of main image with decreasing alpha 0.5 -> 0.0
2nd image: 1 pixel left of main image with decreasing alpha 1.0 ->-0.0
3rd image: exact position main image with alpha 1
4th image: 1 pixel right of main image with increasing alpha 0.5 -> 1.0 
5th image: 2 pixel right of main image with increasing alpha 0.0 -> 0.5


See the difference: (video does not really show how smooth the movement in the app is)
OverBlend2.gif
 
Graphics 800,600
SetBlend ALPHABLEND
Global X:Int=200, timer:Int
Global Alpha:Float
SetColor 255,255,0
Repeat
 Cls
 If timer<MilliSecs()
 timer =MilliSecs() +500
 X=X+1
 EndIf
 alpha=(timer -MilliSecs())/1000.0
 Print alpha

 SetAlpha alpha
 DrawRect X-2, 300,1,100
 SetAlpha 0.5+alpha
 DrawRect X-1, 300,1,100

 SetAlpha 1.0-alpha
 DrawRect X+100, 300,1,100
 SetAlpha 0.5-alpha
 DrawRect X+101, 300,1,100

 SetAlpha 1
 DrawRect X, 100,100,100
 DrawRect X, 300,100,100

DrawText "without side pictures:", 100,80
DrawText "  with 4side pictures:", 100,280
 Flip 0
Until AppTerminate()
...back from Egypt

Derron

If you want to adjust the "alpha" of "all tiles" and individual alpha of single tiles too ... this does not work with normal max2d (in vanilla).

You need to use render images / render 2 texture.

First you combine your images on a new texture ("drawing on them") with the alpha you want.
Finally you render this "all in one" thing on your normal backbuffer with the alpha you want.

for a (sluggish) visual example check this out:
https://github.com/GWRon/brl.mod-NG/issues/2

With "NG" you get access to Render2Texture with the default installation. In vanilla/1.50/legacy it is up to you to dig out some render2texture code (which will be either Ogl _or_ DX).


bye
Ron

Sinjin

#7
I noticed it too when an image is moving, the edges can be quite iffy even with filteredimage flag activated, but I already have a simple workaround for that by adding 1 pixel to each side with full transparency, then it moves smoothly for every image. Its not that smooth but I dont have my systems graphics quite a bit down so its not running with all the filterings on my computer. Also, I didnt test it but I would imagine your idea would be quite blurry with real pictures instead of having one with one color and all opaque. It wasnt really about movements just the blending. Thanks ^^

Sinjin

#8
I wonder does it work for you midi like put a border with transparent pixels around your image? Does it move smoother?
And @Derron I know about FBOs I guess that would do the same thing, like change the alpha for all tiles, so far I didnt fiddle around with that, but you render everything to an image.
Btw mky.mojo2 works for me, I just tried to "harvest" some of the NG-code to my older Bmax 1.50 since the NG-IDE wont work on my Win7, there I saw like "rendertoimage.bmx", works fine, but so far I didnt need FBOs, not sure if thats FBOs...but thanks as well. If I ever buy a new computer I would have win 10 or 11 or whatever, so far I didnt like any OS-change, I have so many programs, all older but they serve me well and when I changed from Win98 to XP (which was the best in my eyes) and from XP to Win7, you lose some of those programs. :D
Plus, at some point I had a new graphics card and more RAM, I never called Microsoft to validate my serial number, means I kinda cracked it looool again, as long as my OS serves me well, I dont see the need to change that.