Create a texture with alpha?

Blitz3D Forums/Blitz3D Programming/Create a texture with alpha?

Sake906(Posted 2008) [#1]
Pretty lame question, but its frustrating me since I don't know why it is not working.

I am trying to create a texture that contains a character from a specific, previously loaded font. So first I create an image with the right dimensions and color (black background with the character being white, basic alpha knowledge, am I right?)

This image is scaled so it uniformly fits in a 64x64 square, this is done in case the font used for this does not have a power of two size.

What I do next is to copyrect to a newly created texture, having the correct flags. What happens? the texture gets NO info.. it all being either transparent or not having alpha at all. Same happens with masked mode, whats going on here? please someone help me... it works when I don't assign the alpha flag, but that is obviously what I don't want here.

The following function creates the texture and returns its handle so you can then assign it to a texture handle, for use on 3D objects.


Function Create_3Dtext_Char_Texture(leter$,font)
	Local image
	Local pastbuffer=GraphicsBuffer()
	Local texture
	Local cr=ColorRed()
	Local cg=ColorGreen()
	Local cb=ColorBlue()
	
	Local ow#,oh#
	
	leter=Mid(leter,1,1)
	
	
	SetFont font
	
	image=CreateImage(FontHeight(),FontHeight())
	
	SetBuffer ImageBuffer(image)
	
	
	Color 0,0,0
	Rect 0,0,256,256
	
	
	Color 255,255,255
	Text 0,0,leter
	
	
	SetBuffer pastbuffer
	
	
	ow=ImageWidth(image)
	oh=ImageHeight(image)
	
	
	ScaleImage image,(64.0*1.0)/ow,(64.0*1.0)/oh
	
	
	texture=CreateTexture(64,64,16+32+2)
	
	
	CopyRect 0,0,ImageWidth(image),ImageHeight(image),0,0,ImageBuffer(image),TextureBuffer(texture)
	
	
	TextureBlend texture,1
	
	
	
	;_______
	
	FreeImage image
	
	Color cr,cg,cb
	
	Return texture
	
End Function




Sake906(Posted 2008) [#2]
....Anyone, please?


Ross C(Posted 2008) [#3]
Copyrect. unfortunelty, does not copy alpha information.

Check out my sig for my code archive entry. That takes a texture and an RGB colour, and sets all RGB values found in that texture to a specific alpha.


GfK(Posted 2008) [#4]
CopyRect is a 2D command, therefore alpha information is not copied.

You need to copy pixel by pixel if you want to copy alpha.


Ross C(Posted 2008) [#5]
http://www.blitzbasic.com/codearcs/codearcs.php?code=1013

If you look down in the comments section, jfk links to some nice code to blend the alpha value nicely with the surrounding pixels.


Sake906(Posted 2008) [#6]
hmmm you don't get my point though... while writepixel could possibly be a solution, I thought blitz will give alpha levels to any black and white texture when it has the alpha flag but no transparent information at all.

I will try the other thing anyway.. I guess.


Ross C(Posted 2008) [#7]
Alpha isn't dependant on colour. Blitz just by default sets black to highest level to alpha. The video card doesn't care what colour the pixels are. So, you have to set this yourself :o)


Sake906(Posted 2008) [#8]
Thanks Ross... I tried yours and jfk's code, it works!