Code archives/3D Graphics - Misc/LoadMaskedTexture

This code has been declared by its author to be Public Domain code.

Download source code

LoadMaskedTexture by fredborg2006
Avoid those nasty black edges on masked textures, and simply load a texture with an alpha channel instead. This function will load and convert it to a masked texture.

You can use the threshold value to set which pixels are rendered as solid and which are rendered as transparent. Higher threshold = more transparent pixels.

It even generates a debug texture if the texture cannot be loaded. Neat!
Function LoadMaskedTexture(file$,flags=0,threshold=200)

	If flags And 2 Then flags = flags Xor 2
	If flags And 4 Then flags = flags Xor 4

	tex = LoadTexture(file$,flags Or 2)
	
	If tex
		buff	= TextureBuffer(tex)
		width	= TextureWidth(tex) - 1
		height	= TextureHeight(tex) - 1

		masktex = CreateTexture(width+1,height+1,flags Or 4)

		tbuff	= TextureBuffer(masktex)

		amul#	= 255.0/(255.0-threshold)

		LockBuffer buff
		LockBuffer tbuff
		For x = 0 To width
			For y = 0 To height
				argb = ReadPixelFast(x,y,buff)
				
				a = (argb Shr 24) And %11111111
				If a<threshold
					a = 0
				Else
					a = (a-threshold)*amul
				EndIf
				
				r = (argb Shr 16) And %11111111 
				g = (argb Shr 8) And %11111111 
				b = argb And %11111111 
				
				argb = b Or (g Shl 8) Or (r Shl 16) Or (a Shl 24)
				WritePixelFast x,y,argb,tbuff
			Next
		Next
		UnlockBuffer buff
		UnlockBuffer tbuff
		
		FreeTexture tex
	Else
		masktex = CreateTexture(64,64,flags)
		buff	= TextureBuffer(masktex)
		width	= TextureWidth(masktex) - 1
		height	= TextureHeight(masktex) - 1
		For x = 0 To width
			xb = (x/8) Mod 2
			For y = 0 To height
				yb = (y/8) Mod 2
				c = -Sgn(xb+yb)
				WritePixel x,y,c,buff
			Next
		Next
	End If
	
	Return masktex

End Function

Comments

Naughty Alien2006
, so it means there is no problem with Z order?..I'll give it a try..looks good..


Mustang2006
There are many versions of alpha - "alpha test" is to my knowledge "masked alpha" [1-bit] and is on/off so there shouldn't be any z-issues like with 8-bit alpha.


Alpha (value, component, channel)
The "fourth" colour of a pixel. The alpha value describes how transparent the pixel is - many textures often have a 1-bit alpha channel, as this is all that is needed for a "see-through" hole. Materials such as glass or water require a higher level of accuracy (i.e. more bits) to blend the colours of the pixels behind the object correctly.

Alpha Blending
See Alpha. This is the process where the pixels of a transparent object are blended with those behind it, in such a way that the final scene allows the viewer to look through the object (e.g. a glass window). Alpha blending is typically done last in the rendering pipeline, as it requires all the objects in the scene to be drawn with the further object done first.

Alpha Testing
This is a process that can be used in the rendering pipeline to decide whether a pixel is declared visible, and therefore written to the frame buffer (or any other target surface).

Alpha textures
These are texture maps that contain transparent or semi-transparent pixels; therefore, the surface format must contain an alpha channel. A popular use for them in games is for mapping leaves or blades of grass; unless the texture is of a high resolution though, the visual effect decreases rapidly in quality as the camera approaches the map.




Chroma2006
Thanks Fred!


virtualjesus2012
Thousand Thanks Fredborg,
I was looking for this code, it will be very useful to me.

Greetings from Colombia
Thanks again Fredborg


Code Archives Forum