CreateTexture mask

Blitz3D Forums/Blitz3D Programming/CreateTexture mask

I have created a texture with CreateTexture(256,256,4) and draw dots on it then apply to a mesh. The masking doesn't seem to work, i.e I can see the black background.

Any thoughts?

are you clearing the texture 0,0,0? Example:
tex=CreateTexture(256,256,4)
setbuffer texturebuffer(tex)
clscolor 0,0,0
cls ; ***edit***
...put dot routine here
setbuffer backbuffer()


Yeah...

ClsColor 0,0,0
Cls

LOL...reply while I was editing. Other than that, the only thing that I can think of is a palette issue.

Actually, isn't masking done in 32 bit? Is your display and program set to 32 bit?

I have ever try anything to do the mask work correctly with createxture, but ... never arrived.

The only solution i have found is :

Size=256
Tex=CreateTexture(Size,Size)
CurBuffer = Graphicsbuffer()
setbuffer texturebuffer(tex)
[...]
setbuffer CurBuffer
saveBuffer (texturebuffer,"TempTexture.bmp")
freetexture tex
tex=loadtexture("TempTexture.bmp",5)
deletefile ("TempTexture.bmp")



I got it to work with loading a texture too...the closest I could get it to work with creating a masked texture was to
clscolor 255,255,255


Methinks this is a bug!

Yeah, I've been experimenting with the masking and can't seem to get it to work properly.

This has been an issue in BB3D for as long as I can remember.

IIRC, if you draw dots onto your texture, save it, then reload it, it *should* work.

I realise this probably doesn't solve your problem, though.

Gfk,

well it does but hardly an elegant solution. Shouldn't BRL fix this?

Yep, this issue has been around forever.

You can get around it by using this code before drawing to the texture:
tex = CreateTexture(256,256,5)	
LockBuffer TextureBuffer(tex)
For y = 0 To 255
  For x = 0 To 255
    WritePixelFast x,y,0,TextureBuffer(tex)
  Next
Next
UnlockBuffer TextureBuffer(tex)


Or, you could do as GfK said, for example:
Graphics3D 800,600,32

SetBuffer BackBuffer()

Global x#,y#,z#
Global speed#=0.1
Global angle#
Global player=CreatePivot()
	PositionEntity player,0,1,0

Global camera=CreateCamera(player)
CameraRange camera,1,100
PositionEntity camera,0,1,0

light=CreateLight()
PositionEntity light,0,50,20
RotateEntity light,45,45,0

plane=CreatePlane()
tex=CreateTexture(32,32)
SetBuffer TextureBuffer(tex)
ClsColor 55,100,255
Cls
Color 0,0,240
Rect 1,1,31,31
Color 230,55,55
Rect 5,5,23,23,1
EntityTexture plane,tex
SetBuffer BackBuffer()

goal=CreateSphere(32)
ScaleEntity goal,5,5,5
EntityRadius goal,5
EntityPickMode goal,1
PositionEntity goal,0,5,55
EntityColor goal,0,255,0
EntityShininess goal,1
tex=CreateTexture(256,256,4)
SetBuffer TextureBuffer(tex)
ClsColor 255,100,255
Cls
Color 0,0,0
For xx=0 To 255 Step 8
	For yy=0 To 255 Step 8
		Oval xx,yy,3,3,1
	Next
Next
SaveBuffer(TextureBuffer(tex),"test.bmp")
tex=LoadTexture("test.bmp",4)
EntityTexture goal,tex
SetBuffer BackBuffer()

While Not KeyHit(1)
If KeyDown(200) Then z#=z#+speed#
If KeyDown(208) Then z#=z#-speed#
If KeyDown(203) Then x#=x#-speed#
If KeyDown(205) Then x#=x#+speed#

TurnEntity goal,0,1,0

x#=x#*.97
z#=z#*.97

MoveEntity player,x#,y#,z#
mxs#=-MouseXSpeed()*.05
mys#=MouseYSpeed()*.05
TurnEntity camera,mys#,0,0
TurnEntity player,0,mxs#,0
RotateEntity player,EntityPitch#(player),EntityYaw#(player),0
If EntityPitch(camera)>55 Then RotateEntity camera,55,0,0
If EntityPitch(camera)<-55 Then RotateEntity camera,-55,0,0
MoveMouse GraphicsWidth()/2,GraphicsHeight()/2


UpdateWorld
RenderWorld
Flip
Delay 10
Wend
End


if you are using drawing commands on a texture buffer, alpha channel information will be lost. It is however a good idea to touch up the alpha channel information manually to prevent black edges due to texture filtering:

if a texel is black (assuming this should be transparent!) then
-Read the rgb of all surrounding texels that are not transparent as well.
-Calcualte the average RGB of them, then set the transparent center texel this way:
alpha=0
argb= (average_rgb and $FFFFFF) or (alpha shl 24)
endif

this way the edges of the masked parts will fade to a color that makes more sense than black.

EDIT: I just posted an example in the archives:
http://www.blitzbasic.com/codearcs/codearcs.php?code=1806