Can I create an alpha channelled texture in code?

Blitz3D Forums/Blitz3D Programming/Can I create an alpha channelled texture in code?

John Pickford(Posted 2005) [#1]
I want to create alpha channeled sprite portraits for my game characters. The idea is to render them to the back buffer and create an alpha channeled sprite from that render.

Is there any way to create a texture with an alpha channel or do I need to load in a dummy png to start with?

Also, how to I access (write to) the alpha channel?


TeraBit(Posted 2005) [#2]
Tex = CreateTexture(64,46,1+2)

Will create an alpha texture. You then need to set the alpha/color manually with Read/Write Pixels. Copyrecting from the backbuffer will not give you alpha, since the backbuffer has none.

You can use this to help.
Function GetRGBA(red,green,blue,alpha)
	Return blue Or (green Shl 8) Or (red Shl 16) Or (alpha Shl 24)
End Function



fredborg(Posted 2005) [#3]
Yes, you can.

If you want to use a rendered image to create it from it's a good idea if you render twice. Once in normal color, and once where the model is completely white (on a black background) to get an alpha channel from.

You cannot copy it directly over to the texture, so you have to use readpixel/writepixel to generate the alpha channel.

The steps are these:
1: CreateTexture of proper size and with the Alpha flag set
2: Render normal model
3: Render completely white model on black background
4: For every pixel in the texture
5: rgb = ReadPixel from normal render
6: a = ReadPixel from b/w render
7: argb= blue Or (green Shl 8) Or (red Shl 16) Or (alpha Shl 24)
8: WritePixel x,y,argb,to the texture
9: Repeat from 5 until done


John Pickford(Posted 2005) [#4]
Cheers guys. I think that's everything I need.

I'm planning on shrinking the image down from something like 256x256 -> 64x64 so I can create a nice alpha channel rather than just on\off.

I'll post the results later.


John Pickford(Posted 2005) [#5]
Right I've got the shrinking the rendered image to create colour map + alpha channel working.



The top two quadrants are renders the bottom two are the generated colour and alphamaps (scaled up).

Now I need to squirt the data into a sprite to see if it works properly.


big10p(Posted 2005) [#6]
That looks neat. I did wonder what the point of having an on/off alpha channel was when you can just use plain masking, but now I see that scaling the images creates a nice antialiased alpha. :)

I guess you scale the colour map image with TFormFilter off, and scale the alpha map image with it on?

P.S. Cool character!


John Pickford(Posted 2005) [#7]
The reason I need to do this is there are up to 8 units involved in a given battle; the units can be one of 5 'ranks' and they can be on foot and in various vehicles. Currently I'm using just a face portrait but by doing this I can create images which convey the character, their 'side' (which army), their rank and their vehicle. A single image conveys all that lot better than displaying text and the large number of permutations mean I can't have a library of portraits.






fredborg(Posted 2005) [#8]
Hehe, funny army. But they aren't naked? ;)


John Pickford(Posted 2005) [#9]
That's as near as you're getting in a PG game.

Perhaps someone will do a nude mod.


John Pickford(Posted 2005) [#10]
It works! These are the end results rendered as sprites. They are currently just slapped on top of everything but they look good. Nicely anti-aliased etc.





big10p(Posted 2005) [#11]
Really nice looking! ;)