Masked Texture

Blitz3D Forums/Blitz3D Programming/Masked Texture

Moore(Posted 2011) [#1]
Ive tried creating a masked texture. It seems to work fine when I load a texture but when I create one on the fly it doesnt work... The black still appears. With the code below the goal is to just see a red circle with out the back around it.

Graphics3D 800, 600
camera = CreateCamera()
AmbientLight 255,255,255

txt = CreateTexture(100,100,8) ; flag for color, alpha and mask
img = CreateImage(100, 100)
SetBuffer ImageBuffer(img)
Color 255,0,0
Oval 0,0,100,100

CopyRect 0,0,100,100,1,1, ImageBuffer(img), TextureBuffer(txt)

sprite = CreateSprite(camera)
EntityTexture sprite, txt
PositionEntity sprite,1,1,3

cube = CreateCube()
ScaleEntity cube, 5, 5, 5
PositionEntity cube,1,1,10

PointEntity camera, sprite

While Not KeyHit(1)
	SetBuffer BackBuffer()
	RenderWorld()
	Flip
	If KeyDown(200) Then TurnEntity camera,5,0,0
	If KeyDown(208) Then TurnEntity camera,-5,0,0
	If KeyDown(203) Then TurnEntity camera,0,5,0
	If KeyDown(205) Then TurnEntity camera,0,-5,0
Wend

End



Rroff(Posted 2011) [#2]
Try with just flags 4 or flags 1+4 for createtexture (for alpha to work you'd have to write directly to the alpha channel, masking is different).

EDIT: Thats not working either


As an aside you really want to be using powers of 2 i.e. 64, 128, 256, etc. when working with textures for dimensions as no all videocards can handle non powers of 2 correctly.

EDIT2: Even more curiously if I set the mask flags on create texture say 1+4 and change the coordinate set around in copyrect it shows what should be drawn behind the masked bit, but offset by that amount... weird.

Last edited 2011


Warner(Posted 2011) [#3]
That is a known problem. There are multiple threads on this subject.
Flag 2 and 4 don't work with CreateTexture.
You could try two things:
1. Manually add alpha to texture's pixels

or
2. Use LoadTexture, since LoadTexture's flag 2 and 4 do work



Kryzon(Posted 2011) [#4]
I'm thinking it's something the Blitz3D texture loader does (judging from the way Max2D handles masked images). When the masked texture is loaded, all black pixels are repainted with alpha < 0.5 or any other value that will make them fail an Alpha Test (which is what Max2D uses for masked images).

Creating on-the-fly is most likely not running through this masking algorithm.


Moore(Posted 2011) [#5]
MARK please include this bug in the next fix.

For now it seems LoadSprite("temp_texture.bmp", 1+4, camera) works fine too.

Thanks guys!


BIG BUG(Posted 2011) [#6]
This is no bug, its just how it works...


Matty(Posted 2011) [#7]
Actually it does work. you have to set the alpha bits in the texture manually though.

Ie createtexture(width,height,1+2) ;alpha or createtexture(width,height,1+4) ;masked

then go through the texture pixel by pixel and change the Alpha value of each ARGB element to 0, or the alpha level you want.

I do this all the time so I know it works.

Also - there is a way of doing it much faster which is to create a template texture..basically an alpha texture or masked texture with the entire texture's alpha value in the RGB values being zero, then use copyrect with this texture and you can then create textures which are fully transparent correctly much faster.


Moore(Posted 2011) [#8]
"This is no bug, "

When the only work around is not documented, nor apparent and it slows things down I would say thats a bug.

When you create a texture that is masked it should automatically be completely masked and not require more lines of code to get it there. That would be the intuitive thing.

Moreover there should be an integrated means to manipulate alpha just as you control color... hmmm perhaps thats a fix... rewrite Color to be

Color(r, g, b, a# = 1.0) insted of Color(r,g,b)

Besides all that thank you everyone, its working now and better yet I know why it wasnt.


Moore(Posted 2011) [#9]
Here you go. Pixel perfect sprites with masking on load or create.



Last edited 2011 Forgot to set frame in mask texture

Last edited 2011 Fixed bug that would not allow for uneven texture (miss named variable)

Last edited 2011


Warner(Posted 2011) [#10]
Nice! May be an idea to post this in the code archives. Makes it easier to find.


Ross C(Posted 2011) [#11]
I would disagree it's a bug. When you load a texture, it's not exactly quick. The time taken to load the texture, probably includes the time to set the alpha information as well. It would be nice to have this though. Especially on copyrect, I would love it to copy alpha information :)


Moore(Posted 2011) [#12]
Wow... so the text function does not work on textures. Read a few posts on it.

SO... if you want to use text then... create a image, draw and write to your hearts content on it, use copyrect to transfer it to the texture and mask_texture to fix the alpha. Uhg!


Matty(Posted 2011) [#13]
Copyrect *does* copy alpha information - I use it all the time - I create a base texture with all argb values as 0 then copyrect this to any new alphaed/masked textures I create and it does indeed copy the alpha values. Note this is from a texture to another texture, not from or to the backbuffer..


Matty(Posted 2011) [#14]
Example:

Graphics3D 512,512,0,2
camera=CreateCamera()
CameraClsColor camera,32,32,32
alphatexture=CreateTexture(128,128,1+2)
SetBuffer TextureBuffer(alphatexture)
LockBuffer
For y=0 To TextureHeight(alphatexture)-1
	For x=0 To TextureWidth(alphatexture)-1
		WritePixelFast x,y,0
	Next
Next
UnlockBuffer
SetBuffer BackBuffer()

cube=CreateCube()
EntityFX cube,1+16
cubetex=CreateTexture(128,128,1+2)
EntityTexture cube,cubetex
MoveEntity camera,0,0,-4


Repeat

	TurnEntity cube,1,2,3
	RenderWorld
	;press space to copyrect the alphatexture to the cube texture..and we'll draw somethings to it too!
	If KeyHit(57) Then 
		CopyRect 0,0,128,128,0,0,TextureBuffer(alphatexture),TextureBuffer(cubetex)
		SetBuffer TextureBuffer(cubetex)
		Color 127+Rand(1,4)*32,127+Rand(1,4)*32,127+Rand(1,4)*32
		Text 64,64,"Hello",1,1
		SetBuffer BackBuffer()
	EndIf 
	Flip

Until KeyDown(1)




Ross C(Posted 2011) [#15]
Yeah, sorry I should have been more specific. It doesn't copy alpha information from the backbuffer. Say you draw a cube on the cameraclscolor of white, it will copy the white. Just would be handy to not do that :) Apologies for the confusion :)