Can't get sprite texture to mask...

Blitz3D Forums/Blitz3D Programming/Can't get sprite texture to mask...

big10p(Posted 2003) [#1]
Hi

I'm obviously doing - or not doing - something silly but I can't get a simple texture, applied to a sprite, mask so that the black areas are transparent. I'm drawing the texture myself in-code and using CreatTexture() with flag 4 (mask) set but it doesn't make any difference.

On a similar note, how do I turn of mip-mapping on a texture? I've tried ClearTextureFilters but, again, no difference.

Clue me up, someone, please!

Cheers

big10p


DJWoodgate(Posted 2003) [#2]
The cheap and cheerful solution is just to draw to an image in the normal way, save it and load it as a masked texture. The more involved method is to use readpixel and writepixel so you can in effect do what the loadtexture routine does when it processes a masked texture. Set the high alpha byte to 0 for those pixels you want masked or 255 otherwise and write this alpha and color information to your texture. That leaves your mip-mapping problem. Cleartexturefilters should work and as for created textures I would have thought the problem would more likely be that there is no mip-mapping, not the other way around as I think you have to explicitly apply the mipmapping flag on created texures. Having said that trying to combine masking with mipmapping could be problematic. So the question has to be what leads you to think it does not work.


big10p(Posted 2003) [#3]
So, I have to set the alpha byte explicitly on created textures. This surprises me especially when CreateTexture() allows me to specify the mask flag and then just ignores it.

As for the mip-mapping thing: say I create a simple texture which is all black with a solid, white circle in the centre. When I use this to texture a sprite, the circle's edges appear smoothed/anti-aliased. Maybe I'm wrong but I assumed it was the mip-mapping causing this. It certainly doesn't change even if I use ClearTextureFilters.

Thanks for replying!

Cheers

big10p


DJWoodgate(Posted 2003) [#4]
That sounds like bi-linear filtering to me. No way to turn that off at the moment. It has been requested though, so you never know what future updates might allow.

In the meantime try making the texture bigger, or... Theres a neat thing you can do if you are masking textures and bearing in mind you can have the maskcolor any color you like if you are using the read/write pixel method. Taking your white circle as an example set the background color to 254,254,254 say, then mask out this nearly white part. Now the bilinear filtering will be much less obvious on the white circle that remains. In general use mask colors that are close to but not the same as the colors that will be left after masking if you want this sort of effect.

This gives the general idea. I am being a bit naughty here by drawing directly to the texture. It usually works ok though, but the recommended method of writing to textures is by sole use of readpixel/writepixel.

texture=CreateTexture(128,128,4)
SetBuffer TextureBuffer(texture)
maskred=254 : maskgreen=0 : maskblue=0
maskrgb=maskred Shl 16 Or maskgreen Shl 8 Or maskblue
Color maskred,maskgreen,maskblue
Rect 0,0,128,128
Color 255,0,0
For i = 0 To 128 Step 16
	Rect i,0,1,128 	Rect 0,i,128,1
Next

For i=0 To 127
	For j=0 To 127
		argb=ReadPixel(i,j,TextureBuffer(texture))
		rgb= argb And $FFFFFF
		If rgb=maskrgb Then alpha=0 Else alpha=255
		WritePixel i,j,alpha Shl 24+rgb,TextureBuffer(texture)
	Next
Next



big10p(Posted 2003) [#5]
Yes, I was obviously getting mixed up between bi-linear filtering and mip-mapping.

That's a good tip for reducing the effects of bi-linear filtering, thanks. I managed to get masking to work by simply writing $000000 to every pixel in the newly created texture and then drawing the image on top of that.

BTW, I wasn't aware it was 'naughty' to draw directly to a texture. Why do you say that? I'm sure it's mentioned somewhere in the manual as well as being used in several demos I've come across.

Thanks again!

Cheers,

big10p


DJWoodgate(Posted 2003) [#6]
Yes, I know it works generally, and it seems to be card dependent, some cards like my old ATI have problems drawing to a texture while rendering is going on, it sometimes causes the texture to become corrupted. That could be a driver issue as much as anything else. Doing it just after the texture is created works ok though and using read/write pixel always seems to work. So you will probably be ok, just suck it and see.