Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Professional Discussion / Alphamap on images

Author
Message
Hayer
20
Years of Service
User Offline
Joined: 4th Nov 2005
Location: Norway
Posted: 10th Apr 2011 18:06
Hi

Im trying to achive this effect:


The problem is that I don't really know how to do it in DBPro.. so far this is the only code I got:



I'm lost on what to do next, halp?!

Keep it simple.
Questions? Mail me
KISTech
18
Years of Service
User Offline
Joined: 8th Feb 2008
Location: Aloha, Oregon
Posted: 10th Apr 2011 19:27
You have to apply the alpha map to the image that is supposed to have transparency in it. I would use something memblocks or the 2D Image Kit to do this. You could also use Blend Mapping, or a Shader.

Basically all you have to do is copy the alpha map pixels to the alpha channel of the image. Then pasting it with the transparency mode 1 will work.

Link102
21
Years of Service
User Offline
Joined: 1st Dec 2004
Location: On your head, weeeeee!
Posted: 10th Apr 2011 19:29
It depends on what you're planning on doing with the image.
If you want to use it as a (ground) texture then you should use a shader.

Hayer
20
Years of Service
User Offline
Joined: 4th Nov 2005
Location: Norway
Posted: 10th Apr 2011 20:16
Link102 @ Well, atm I'm trying to use it in my 2d level editor so I can paint textures on to blocks. It is non-tiled, and a "shape" is defined as a block.

Atm the block can have 1 texture, but I want to be able to "paint" a texture on to the other. Im only prototyping in DBPro, the real code is in c++

Keep it simple.
Questions? Mail me
Link102
21
Years of Service
User Offline
Joined: 1st Dec 2004
Location: On your head, weeeeee!
Posted: 11th Apr 2011 14:19
In this case I believe memblocks would be the most direct approach.
You can do this in a couple of ways:
- You can multi texture the image with alpha (tex1.jpg + alphamap.bmp) on top of the other image (tex2.jpg) on the 3d model.
- You can blend both images yourself in the memblock. this requires the most math, but it's the fastest at runtime, because you only have 1 texture to apply on the 3d model.
- You can blend both images yourself in photoshop, but I figure that's not what you're going for

Don't know what you mean with 'a "shape" is defined as a block'. What shape?

WLGfx
18
Years of Service
User Offline
Joined: 1st Nov 2007
Location: NW United Kingdom
Posted: 11th Apr 2011 15:08
I recently came across that in the GDK when I found the command dbSetBitmapFormat()... Setting the format to 21 makes the screen and all bitmaps to ARGB format instead of RGB by default. That way loading an image from a PNG file with an alpha channel makes pasting images and texturing objects (after setting the transparency) look nicer than just using the colorkey.

Warning! May contain Nuts!
TheComet
18
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 11th Apr 2011 15:09 Edited at: 11th Apr 2011 15:17
This is from the top of my head, so it may not work, but I hope it helps Probably a little inefficient, but it's a start.



Note that both images have to be 32-bit images, and the alphatex has to be the greyscale version of the alpha channel, or this won't work.

This is how it works. Each pixel in an image containes 4 bytes of data (32 bits). red, green, blue, and alpha. This can be displayed as a hexadecimal value:

FF FF 80 00

The first FF is the alpha channel, so FF would be the maximum (no transparency). The next FF is for red (full red), 80 for green (half green), and 00 for blue (no blue). So that pixel would be orange. What my code does is it takes the color data from the texture, takes the alpha channel from the alpha map, and writes it together:

tex1 : FF A2 67 C9 <-- texture
alpha: FF B1 B1 B1 <-- grey scale alpha

final image : B1 A2 67 C9

TheComet

Grog Grueslayer
Valued Member
21
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 11th Apr 2011 21:21
Quote: "I recently came across that in the GDK when I found the command dbSetBitmapFormat()... Setting the format to 21 makes the screen and all bitmaps to ARGB format instead of RGB by default. "


You don't need that command if he's just loading a .png. That command is used when you want to grab the alpha channel when using the GET IMAGE command.

Sven B
21
Years of Service
User Offline
Joined: 5th Jan 2005
Location: Belgium
Posted: 15th Apr 2011 19:42
About the "math" behind blending two colors:

For pasting on an opaque image, the formula is quite simple:
T = channel of the bottom image (red, green or blue)
B = channel of the top image (red, green or blue)
a = alpha value (0.0 - 1.0)

C = B * (1.0 - a) + T * a
(C = the new value of the channel)

If a (the alpha value) is not in the range (a = 0.0 - 1.0) but extracted from a channel (so A = 0 - 255), you can use the following slightly 'off' but very fast formula:
C = (B * (255 - A) + T * A) >> 8
Where >> is the bitwise shift operator (DBP can do this).

For example:
rgb(0, 80, 100) blended with rgb(100, 20, 200) using an alpha of 64 (extracted from the top image for example) gives:
For red:
(0 * (255 - 64) + 100 * 64) >> 8 = 25
For green:
(80 * (255 - 64) + 20 * 64) >> 8 = 64
For blue:
(100 * (255 - 64) + 200 * 64) >> 8 = 124
For alpha:
Pasting on an opaque image will result in an opaque color (so 255 always)

The resulting color is: rgb(25, 64, 124)

Good luck!
Sven B

Login to post a reply

Server time is: 2026-07-18 06:41:16
Your offset time is: 2026-07-18 06:41:16