QB64.org Forum

Active Forums => QB64 Discussion => Topic started by: MasterGy on May 10, 2021, 09:36:40 am

Title: question: light transmission with any image
Post by: MasterGy on May 10, 2021, 09:36:40 am
Hello ! i just can't figure out how to do it. i want to be able to adjust the light transmission of any image. For example, 100, only the image should be visible. If you are 50, you will see half of the image and half of the background. If 0, the image disappears completely. What do I need to do with the "pic" picture for this to work?

Code: QB64: [Select]
  1. mon = _NEWIMAGE(800, 800, 32)
  2. SCREEN mon: CLS
  3.  
  4. 'what do i do with this image to adjust its light transmission?
  5. pic = _LOADIMAGE("d:\picture.jpg", 32)
  6.  
  7.  
  8. 'changing color background
  9. FOR t = 0 TO 2999
  10.     COLOR _RGB32(256 * RND(1), 256 * RND(1), 256 * RND(1)), 0
  11.     LOCATE 1 + 40 * RND(1), 1 + 99 * RND(1)
  12.     PRINT "Ű"
  13.  
  14. _DEST mon
  15.  
  16.  
  17.  
Title: Re: question: light transmission with any image
Post by: justsomeguy on May 10, 2021, 11:04:19 am
I'm not sure what the final result your looking for but I modified your code on my best guess.
Code: QB64: [Select]
  1. mon = _NEWIMAGE(800, 800, 32)
  2. SCREEN mon: CLS
  3.  
  4. 'what do i do with this image to adjust its light transmission?
  5. pic = _LOADIMAGE("d:\picture.jpg", 32)
  6.  
  7. newimg = _NEWIMAGE(_WIDTH(pic), _HEIGHT(pic), 32)
  8.  
  9. '   setTransparency ( Image that you loaded, New Image that you created , Transparency vale (0-255) )
  10. CALL setTransparency(pic, newimg, 200)
  11. '   Don't forget to free images after you are done.
  12.  
  13.  
  14. 'changing color background
  15. FOR t = 0 TO 2999
  16.     COLOR _RGB32(256 * RND(1), 256 * RND(1), 256 * RND(1)), 0
  17.     LOCATE 1 + 40 * RND(1), 1 + 99 * RND(1)
  18.     PRINT "Ű"
  19.  
  20.  
  21.  
  22. _DEST mon
  23. _SOURCE newimg
  24.  
  25.  
  26. SUB setTransparency (img AS LONG, newimg AS LONG, tr AS INTEGER)
  27.     _SOURCE img ' Your loaded image handle
  28.     _DEST newimg 'The New Image Handle
  29.     DIM AS INTEGER x, y
  30.     DIM AS LONG c
  31.     FOR x = 0 TO _WIDTH(img)
  32.         FOR y = 0 TO _HEIGHT(img)
  33.             c = POINT(x, y)
  34.             PSET (x, y), _RGBA32(_RED(c), _GREEN(c), _BLUE(c), tr)
  35.         NEXT
  36.     NEXT
  37.     _SOURCE 0
  38.     _DEST 0
  39.  
  40.  

For some reason I could not modify the loaded images transparency, I could only create a new one and modify that one.
Hope this helps!
Title: Re: question: light transmission with any image
Post by: Petr on May 10, 2021, 11:53:11 am
Hi. Try my method using _SETALPHA.

I assume that SETALPHA sets the transparency of the image ONLY once (for example, let's say you set the transparency for color _RGB32 (255,255,255) 50 percent in one step, you get a new color, then (and that's just my assumption) in the next step already fifty percent transparency will be calculated from the image in which the fifty percent transparency was set in the previous step) and that is why, for this reason I use COPYIMAGE in my program, I will set a new transparency once, display it and immediately discard a copy of the original image. So I use COPYIMAGE to adjust the transparency relative to the original image. The construction of this program is only for this purpose, so that you can use the + and - keys to continuously change the size of the alpha channel of the pic image.

Also keep in mind that _NEWIMAGE creates an image with a transparent background, the CLS command fills the entire image with the color 0,0,0,255 so then the transparency of the background is lost and this is why when alpha = 0 the background image is overwritten with black.

Code: QB64: [Select]
  1. mon = _NEWIMAGE(800, 800, 32)
  2. SCREEN mon: CLS
  3.  
  4. 'what do i do with this image to adjust its light transmission?
  5. DIM Alpha AS LONG, W AS INTEGER, H AS INTEGER, Back AS LONG, Pic AS LONG
  6. Pic = _LOADIMAGE("picture.jpg", 32)
  7. W = _WIDTH(Pic)
  8. H = _HEIGHT(Pic)
  9. Back = _NEWIMAGE(W, H, 32)
  10. _DEST Back
  11. 'changing color background - draw to 'back' screen
  12. FOR t = 0 TO W * H / 128
  13.     COLOR _RGB32(256 * RND(1), 256 * RND(1), 256 * RND(1))
  14.     LOCATE 1 + 40 * RND(1), 1 + 99 * RND(1)
  15.     PRINT "Ű"
  16. _DEST mon
  17. Alpha = 127
  18.     i$ = INKEY$
  19.     SELECT CASE i$
  20.         CASE "+": Alpha = Alpha + 1
  21.         CASE "-": Alpha = Alpha - 1
  22.     END SELECT
  23.     IF Alpha > 255 THEN Alpha = 255
  24.     IF Alpha < 0 THEN Alpha = 0
  25.     PicNew = _COPYIMAGE(Pic, 32)
  26.     _SETALPHA Alpha, _RGB32(0, 0, 0) TO _RGB32(255, 255, 255), PicNew 'set pic ALPHA
  27.     CLS
  28.     _PUTIMAGE , Back, mon ' place still full colored background image to screen
  29.     _PUTIMAGE , PicNew, mon ' place pic image with changed alpha for all colors in pic image
  30.     _FREEIMAGE PicNew
  31.     LOCATE 1
  32.     PRINT Alpha
  33.     _DISPLAY
  34.     _LIMIT 20
  35. LOOP UNTIL i$ = CHR$(27)
  36.  
Title: Re: question: light transmission with any image
Post by: MasterGy on May 10, 2021, 12:24:49 pm
Thank you Justsomeguy and Petr ! Both are good and work in the example.
I was completely confused. I want the "terror in the maze" game that the dragon fires fire to be a little transparent. I have both tried a solution and although it works in my example program, it does not. The game has a _CLEARCOLOR and then turns it into a hardware image. With your solution, I am not claiming transparency, but taking it away from color. Galleon’s glass method isn’t good here either, because I don’t want to turn an empty something into a sour cream, but I want to make an existing colored dot semi-transparent.

  [ This attachment cannot be displayed inline in 'Print Page' view ]  
Title: Re: question: light transmission with any image
Post by: MasterGy on May 10, 2021, 12:26:28 pm
In the evening I will write a game-compatible code for the problem. I wonder what needs to change in your ideas so far.
Title: Re: question: light transmission with any image
Post by: MasterGy on May 10, 2021, 12:36:30 pm
if someone is trying to rewrite directly in the game, (I don't expect anyone to do this kindness), set cheatkey = 1 at the beginning and two dragons can be activated by pressing the "u" key.
Title: Re: question: light transmission with any image
Post by: johnno56 on May 10, 2021, 04:57:44 pm
Umm... I know my eyes are old, but even I cannot see the link to "picture.jpg", or can any image suffice?
Title: Re: question: light transmission with any image
Post by: MasterGy on May 10, 2021, 05:52:08 pm
I show the problem. The program draws a fire with the background hidden. The fire, or "pic," is a piercing image. How can I adjust your transparency?

Code: QB64: [Select]
  1.  
  2. 'what do i do with this image to adjust its light transmission?
  3. temp = _LOADIMAGE("0000.bmp", 32)
  4. _DEST temp: _SOURCE temp
  5. DIM temp_color AS _INTEGER64
  6. temp_color = POINT(1, 1)
  7. _CLEARCOLOR temp_color
  8. pic = _COPYIMAGE(temp, 32)
  9.  
  10.  
  11.  
  12.  
  13. 'show neverend------------------------------------------------------
  14. mon = _NEWIMAGE(800, 800, 32)
  15. SCREEN mon: CLS
  16.  
  17.     'changing color background
  18.     FOR t = 0 TO 2999
  19.         COLOR _RGB32(256 * RND(1), 256 * RND(1), 256 * RND(1)), 0
  20.         LOCATE 1 + 40 * RND(1), 1 + 99 * RND(1)
  21.         PRINT "Ű"
  22.     NEXT t
  23.  
  24.     'draw fire
  25.     marg = 50
  26.  
  27.     'display loacation
  28.     dx0 = marg: dy0 = marg
  29.     dx1 = _WIDTH(mon) - marg: dy1 = marg
  30.     dx2 = marg: dy2 = _HEIGHT(mon) - marg
  31.     dx3 = dx1: dy3 = dy2
  32.  
  33.     'picture location
  34.     sx0 = 0: sy0 = 0
  35.     sx1 = _WIDTH(pic) - 1: sy1 = 0
  36.     sx2 = 0: sy2 = _HEIGHT(pic) - 1
  37.     sx3 = sx1: sy3 = sy2
  38.  
  39.     _MAPTRIANGLE (sx0, sy0)-(sx1, sy1)-(sx2, sy2), pic TO(dx0, dy0)-(dx1, dy1)-(dx2, dy2)
  40.     _MAPTRIANGLE (sx3, sy3)-(sx1, sy1)-(sx2, sy2), pic TO(dx3, dy3)-(dx1, dy1)-(dx2, dy2)
  41.     _DISPLAY

  [ This attachment cannot be displayed inline in 'Print Page' view ]  
Title: Re: question: light transmission with any image
Post by: bplus on May 10, 2021, 07:37:42 pm
justsomeguy's procedure looked pretty good just from reading it.

Load it into a very very transparent image handle and _PutImage it all over area you want on fire with different widths and heights, the overlapping fires will begin to look real? just thinking out loud here...

If you need code for live fire let me know. (see Battleship Splash screen)
Title: Re: question: light transmission with any image
Post by: justsomeguy on May 10, 2021, 07:41:45 pm
@bplus Thanks, but @Petr 's method might be faster and more efficient. Mine was a case of me struggling with a problem, finding something that worked, and never circling back to it to make it better.
Title: Re: question: light transmission with any image
Post by: bplus on May 10, 2021, 07:47:26 pm
@bplus Thanks, but @Petr 's method might be faster and more efficient. Mine was a case of me struggling with a problem, finding something that worked, and never circling back to it to make it better.

Yeah Petr's looked pretty complex (so I'd really have to test and play with) but he is fine coder and comes up with great effects, I still remember Christmas! :) His and Dav's so different.

I know yours is not a fast thing but once one very transparent image is loaded in handle, _PutImage can do allot of fast random layering. Ha! maybe it only works in my head, really great there!
Title: Re: question: light transmission with any image
Post by: MasterGy on May 11, 2021, 02:00:38 am
Thanks Bplus! I really like Battleship Fire! Very lifelike! I analyzed the code, although I don't fully understand how it works, but I find the solution interesting! The trouble is, I noticed that that fire wasn’t transparent either. So the original problem is not solved with this. For many things, it would be good for me to have a hardware image that has become BMP or JPG that I can make transparent because I might need it in other cases as well.
Title: Re: question: light transmission with any image
Post by: johnno56 on May 11, 2021, 03:35:08 am
Quick question. The print statement, on line #22, seems to be a special "U" character. Can I assume that this is a "Windows" character?

I will try to attach a screen image. All the 'background' characters are continuously alternating colours....

  [ This attachment cannot be displayed inline in 'Print Page' view ]  
Title: Re: question: light transmission with any image
Post by: MasterGy on May 11, 2021, 04:00:35 am
yes, in windows it's a "full" character. I did the alternating color to make the changing color background perceptible. My question would be how to make the fire semi-transparent
Title: Re: question: light transmission with any image
Post by: justsomeguy on May 11, 2021, 07:50:30 am
Perhaps not the best way or fastest way, but here goes... I think @Petr 's method could also work but It may require layering several images.

The artifacts around the flame bitmap are due to those not quite being the full transparency color.

Code: QB64: [Select]
  1. 'what do i do with this image to adjust its light transmission?
  2. temp = _LOADIMAGE("0000.bmp", 32)
  3. _DEST temp: _SOURCE temp
  4. DIM temp_color AS _INTEGER64
  5. temp_color = POINT(1, 1)
  6. '_CLEARCOLOR temp_color
  7. 'pic = _COPYIMAGE(temp, 32)
  8.  
  9. pic = _NEWIMAGE(_WIDTH(temp), _HEIGHT(temp), 32)
  10. CALL setTransparency(temp, pic, 200, temp_color)
  11.  
  12.  
  13.  
  14. 'show neverend------------------------------------------------------
  15. mon = _NEWIMAGE(800, 800, 32)
  16. SCREEN mon: CLS
  17.  
  18.     'changing color background
  19.     FOR t = 0 TO 2999
  20.         COLOR _RGB32(256 * RND(1), 256 * RND(1), 256 * RND(1)), 0
  21.         LOCATE 1 + 40 * RND(1), 1 + 99 * RND(1)
  22.         PRINT "Ű"
  23.     NEXT t
  24.  
  25.     'draw fire
  26.     marg = 50
  27.  
  28.     'display loacation
  29.     dx0 = marg: dy0 = marg
  30.     dx1 = _WIDTH(mon) - marg: dy1 = marg
  31.     dx2 = marg: dy2 = _HEIGHT(mon) - marg
  32.     dx3 = dx1: dy3 = dy2
  33.  
  34.     'picture location
  35.     sx0 = 0: sy0 = 0
  36.     sx1 = _WIDTH(pic) - 1: sy1 = 0
  37.     sx2 = 0: sy2 = _HEIGHT(pic) - 1
  38.     sx3 = sx1: sy3 = sy2
  39.  
  40.     _MAPTRIANGLE (sx0, sy0)-(sx1, sy1)-(sx2, sy2), pic TO(dx0, dy0)-(dx1, dy1)-(dx2, dy2)
  41.     _MAPTRIANGLE (sx3, sy3)-(sx1, sy1)-(sx2, sy2), pic TO(dx3, dy3)-(dx1, dy1)-(dx2, dy2)
  42.     _DISPLAY
  43.  
  44. SUB setTransparency (img AS LONG, newimg AS LONG, tr AS INTEGER, backcolor AS LONG)
  45.     _SOURCE img ' Your loaded image handle
  46.     _DEST newimg 'The New Image Handle
  47.     DIM AS INTEGER x, y
  48.     DIM AS LONG c
  49.     FOR x = 0 TO _WIDTH(img)
  50.         FOR y = 0 TO _HEIGHT(img)
  51.             c = POINT(x, y)
  52.             IF c = backcolor THEN
  53.                 PSET (x, y), _RGBA32(_RED(c), _GREEN(c), _BLUE(c), 0)
  54.             ELSE
  55.                 PSET (x, y), _RGBA32(_RED(c), _GREEN(c), _BLUE(c), tr)
  56.             END IF
  57.         NEXT
  58.     NEXT
  59.     _SOURCE 0
  60.     _DEST 0
  61.  
  62.  
Title: Re: question: light transmission with any image
Post by: MasterGy on May 11, 2021, 10:24:48 am
I finally found the error. or no error, just not working properly. Thank you for your help! All your solutions are super and workable! In the form in which it appears in the attached code. De! I can’t draw on the track so easily. I finally understood why it doesn’t let light through, instead it gets closer to black. Here may be the z-buffer problem. I will draw the fire, but if I draw behind it later, its color will no longer be visible. So I messed up there that I put it in the wrong drawing order. However, I can't hit the right order. Either it is not visible through a glass window or it is visible even when it should not. I think I'll ask Galleon about this. Thank you for your help !
Title: Re: question: light transmission with any image
Post by: MasterGy on May 11, 2021, 10:28:42 am
I formulate the problem: on the track, the walls of the blocks have a glass effect. To look all right, you need to draw in the right order. That's fine, it works. But what happens if we put a light-transmitting texture inside the maze? _dontblend? _depthbuffer? Or do I have to put the fireballs in order? Unfortunately, I don't really understand this thing yet.
Title: Re: question: light transmission with any image
Post by: Petr on May 11, 2021, 11:20:14 am
Quote
I show the problem. The program draws a fire with the background hidden. The fire, or "pic," is a piercing image. How can I adjust your transparency?

just add this before DO (to row 16 after SCREEN statement):

Code: QB64: [Select]
  1. _SETALPHA 127, _RGB32(0, 0, 0) TO _RGB32(255, 255, 255), pic
  2.  

then is this result:

  [ This attachment cannot be displayed inline in 'Print Page' view ]  
Title: Re: question: light transmission with any image
Post by: Petr on May 11, 2021, 11:41:03 am
Of course, you can also set only part of the image as transparent. For only one part of the flame transparent, try setting the values to
 
_SETALPHA 127, _RGB32 (100, 100, 100) TO _RGB32 (255, 255, 0), pic

or is possible to set different alpha levels to different colors on image :)
Title: Re: question: light transmission with any image
Post by: MasterGy on May 11, 2021, 12:32:04 pm
thanks Petr! It has already succeeded. I just can’t integrate into the game because the drawing sequences are complicated.
there are plenty of things I don't understand in the world :) _SETALPHA. I studied, but I don't understand how you can use "A TO B" to determine which colors I mean. :) Then here’s _DEPTHBUFFER ... it’s a very funny thing. I experiment with this because either the fire is full or just dark or transparent, but it is visible from everywhere, even through the wall. So it's not good at all. What else do I not understand? :) _DONTBLEND / _BLEND I want to understand these once :)
Unfortunately, I can't find a good description anywhere.
Title: Re: question: light transmission with any image
Post by: SMcNeill on May 11, 2021, 01:17:37 pm
Blend and dontblend, I can tell you about easily.

Take a screen with any red/green/blue color you want:  _RGB(255, 0, 0) for all red, for example.

Take a second screen with any non-solid alpha color: _RGBA(0, 0, 255, 128) for half-transparent blue, for example.

If _BLEND is on, and you _PUTIMAGE the 2nd image onto the 1st one, it’ll blend the colors together and you’ll end up with a pale purple screen.

If _DONTBLEND is active, and you _PUTIMAGE the 2nd image onto the first one, it’ll completely overwrite the first one and duplicate the 2nd. 

_BLEND on mixes colors together.

_BLEND off replaces one color with the other.  (_DONTBLEND)
Title: Re: question: light transmission with any image
Post by: MasterGy on May 11, 2021, 02:19:54 pm
thanks SMcNeill! i think i understand (not sure) .then i will try. Just _PUTIMAGE? Does it work with simple PRINT, _MAPTRIANGLE, LINE, CIRCLE? I'll try.
Title: Re: question: light transmission with any image
Post by: SMcNeill on May 11, 2021, 03:49:06 pm
thanks SMcNeill! i think i understand (not sure) .then i will try. Just _PUTIMAGE? Does it work with simple PRINT, _MAPTRIANGLE, LINE, CIRCLE? I'll try.

It should work with all those, as long as you’re putting one color over another.

Think of _BLEND as taking two buckets of paint, dumping them together into a third bucket to mix them together.  Half Yellow + Half Blue makes Green.

With _DONTBLEND, you’re just pouring paint from one bucket into the other.  If it starts blue, it ends up blue.
Title: Re: question: light transmission with any image
Post by: johnno56 on May 11, 2021, 04:37:19 pm
I am a Linux user and do not know what the Windows character "U" is supposed to do, but for me, my machine interprets the "U" as a large plus sign and a checkered pattern.  Can someone post what it is meant to look like?  Not complaining. Just curious.
Title: Re: question: light transmission with any image
Post by: justsomeguy on May 11, 2021, 04:44:34 pm
@johnno56 I believe it is a uni-code character. Different language keyboards have these.
Title: Re: question: light transmission with any image
Post by: johnno56 on May 11, 2021, 04:48:05 pm
My version of QB64 (1.5) receives a "copy and paste" of the listing as shown in the screen capture. I am not complaining. Just curious and looking for a fix....

  [ This attachment cannot be displayed inline in 'Print Page' view ]  
Title: Re: question: light transmission with any image
Post by: MasterGy on May 11, 2021, 04:50:44 pm
command me! :)

  [ This attachment cannot be displayed inline in 'Print Page' view ]  
Title: Re: question: light transmission with any image
Post by: johnno56 on May 11, 2021, 06:15:05 pm
MasterGy,

Removed the Plus from the print string and added Petr's alpha line and runs just fine...

  [ This attachment cannot be displayed inline in 'Print Page' view ]  

Thank you.