Alpha Map Issues...

Blitz3D Forums/Blitz3D Programming/Alpha Map Issues...

Black Hydra(Posted 2005) [#1]
I have an object used in game that I used to use a .png alpha map as a texture.

To reduce filesize I began switching to a 2-map process with dual .jpg's. The first .jpg was the color map, a second .jpg was the alpha layer.

Now when I use the GIMP to transfer the greyscaled alpha map as a layer mask the "dual" images looks identical to the original one.

However when I use the following code to create the dual textures on the object it becomes far more transparent than the original .png version:

Brush = LoadBrush("media/Visuals/Obstacles/WarpGate.jpg", 1)
 PaintEntity Globals\WarpGate, Brush
 EntityTexture Globals\WarpGate, LoadTexture("media/Visuals/Obstacles/WarpGate_alpha.jpg", 2), 0, 1


Now I'm wondering if there is some way Blitz handles this that causes a difference in a .png loaded texture (with existing alpha layer) and loading two textures (one color, other alpha) and applying them to the same mesh.

Just as a side note the color map used in this "dual" texturing process is simply the existing RGB layers of the png map with Alpha at 100%. So it isn't a problem of trying to artificially remove the alpha by having a background color like black or white.

Thanks for reading my inquiry, and I hope that anyone who knows can spare a moment to help me out.

Thanks,
Scott


Ross C(Posted 2005) [#2]
I think the blending process is slightly different (don't hold me to that though). Why don't you load in your two textures, and take the alpha information from the alpha texture and transfer it to the normal texture. Simply a matter of reading the normal textures ARGB, reading the equivilant x,y pos from the alpha texture, and writing the new ARGB texture information back over.


Ross C(Posted 2005) [#3]
There's some code here from my code archive entry to read the ARGB and recombine and write back.

http://www.blitzbasic.com/codearcs/codearcs.php?code=1013

Hope that helps a little. I take it you know .jpgs have visual quality degradation when they are saved? There kinda bad for stuff where all the pixels need to retain there exact values.


Black Hydra(Posted 2005) [#4]
@Ross

Thanks I'll check that out. I use the jpg because the compression rate is much higher. Most of the things I have been using it for (particle images, backdrop images) don't require pixel perfect quality so I'd gladly take a slight hit in quality for a (170kb -> 14kb) change in compression from 1 map to 2.


Black Hydra(Posted 2005) [#5]
Okay I read your code snippet, and although it was for masking I think I can convert it into an alpha map. I just have a question regarding this. In your code you use the variable RGB1 to store (what I'm guessing is a memory position) from ReadPixelFast.

Then you have the r, g, b, and a variables store the RGBA values you want on the pixel, and then you write it again?

Could you explain to me the meaning of the (RGB1 and $FF000000)? I'm assuming it has to do with the memory position (Hex code?)

Sorry, I'm by no means an excellent programmer and I'd like to understand how the code works to prevent a debugging mess when I try to alter it. Thanks again for your help.


Ross C(Posted 2005) [#6]
It's to filter out the A R G B from the readpixel command. The colour information gets returned in a 32 bit number.

			RGB1=ReadPixelFast(loop,loop1,TextureBuffer(texture))
			r=(RGB1 And $FF0000)shr 16;separate out the red
			g=(RGB1 And $FF00) shr 8;green
			b=RGB1 And $FF;and blue parts of the color
			a=(RGB1 And $FF000000)Shr 24


a is the variable that will hold the alpha information. That line of code will extract it from the number the readpixel command returns. I'm not dead sure about it, as i obtained the filtering code from another place, but it's taking certain bits from the number and storing them in a. Basically what you'd want to do, is only extract the alpha from the alpha texture and write it back into the normal texture.

So:

Function transfer_alpha(source,des) ; source is the texture the alpha will get copied from
									; des is the texture the alpha will get written to.

	LockBuffer TextureBuffer(source)
	LockBuffer TextureBuffer(des)

	For loop=0 To TextureWidth(source)-1
		For loop1=0 To TextureHeight(source)-1

			RGBsource=ReadPixelFast(loop,loop1,TextureBuffer(source))
			
			SourceR = (RGBsource And $FF0000)Shr 16;separate out the red
			SourceG = (RGBsource And $FF00) Shr 8;green
			SourceB = RGBsource And $FF;and blue parts of the color
			SourceA = (RGBsource And $FF000000)Shr 24; and seperate out the alpha

			RGBdes = ReadPixelFast(loop,loop1,TextureBuffer(des))

			DesR = (RGBdes And $FF0000)Shr 16;separate out the red
			DesG = (RGBdes And $FF00) Shr 8;green
			DesB = RGBdes And $FF;and blue parts of the color
			DesA = SourceA ; copy the alpha information from the source texture to the desination one.

			
			newrgb= (DesA Shl 24) Or (DesR Shl 16) Or (DesG Shl 8) Or DesB; combine the ARGB back into a number

			WritePixelFast(loop,loop1,newrgb,TextureBuffer(des)); write the info back to the texture
		Next
	Next

	UnlockBuffer TextureBuffer(des)
	UnlockBuffer TextureBuffer(source)


End Function


That function should work for you. Just call it with the source and destination textures. Make sure there the same size though. I didn't take into account they were different :o)


Black Hydra(Posted 2005) [#7]
Thank you!

It took a bit of reworking and messing around with texture flags but I got it working and it looks identical to the png.

Just a side note, if you ever wanted to use that code you would have to replace the line:

SourceA = (RGBSource and $FF000000)Shr 24

with

SourceA = (SourceR+SourceB+SourceG)/3

Seeing as I was using a jpg, the greyscale equivalent was saved as the alpha map so I adjusted seeing as the actual "alpha" value of the alpha map is 100% (.jpg). But that is easy enough to fix.

Also you have to remember to make both textures loaded with the flag 2 for an alpha map for both of them. Not that I really need to explain this to you, but in case anyone stumbles upon this thread that will make it easier to implement.

Thanks again Ross, now back to work!


Ross C(Posted 2005) [#8]
Ah right, sorry, i didn't take into account that ^_^ glad it works for ya!


Shifty Geezer(Posted 2005) [#9]
I had this trouble with png's. When saving an 8 bit+alpha the object overall was too transparent. If I saved the .png as 24 bit + alpha it worked okay. The difference was all of 500-1000 bytes per image so I just save in 24 bit.