SetBlendRef()

BlitzMax Forums/BlitzMax Module Tweaks/SetBlendRef()

TomToad(Posted 2013) [#1]
When using SetBlend MASKBLEND, pixels will be drawn when alpha is 128 or greater. I made some modifications to the Max2D mods so you can change the reference alpha value so pixels can be drawn at any alpha value you choose.

First in brl.mod\max2d.mod\max2d.bmx line 500, add this below the SetBlend function
Rem
bbdoc: Sets the reference alpha for the MASKBLEND mode
about:
When using the MASKBLEND mode with the SetBlend command, you can change the reference alpha
used to determine when pixels are to be drawn.  Use values 0-255, default is 128
End Rem
Function SetBlendRef( Ref )
	_max2dDriver.SetBlendRef Ref
End Function


in brl.mod\d3d9max2d.mod\d3d9max2d.bmx line 391, right below the SetBlend Method
	Method SetBlendRef( Ref )
		Ref = Max(Min(Ref,255),0)
		_d3dDev.SetRenderState D3DRS_ALPHAREF,Ref
	End Method


in brl.mod\d3d7max2d.mod\d3d7max2d.bmx line 237
	Method SetBlendRef( Ref )
		Ref=Max(Min(Ref,255),0)
		device.SetRenderState D3DRS_ALPHAREF,Ref
	End Method


in brl.mod\glmax2d.mod\glmax2d.bmx line 378
	Method SetBlendRef( Ref )
		Ref=Max(Min(Ref,255),0)
		glAlphaFunc GL_GEqual,Ref/255.0
	End Method


And lastly in brl.mod\max2d.mod\driver.bmx line 34
	Method SetBlendRef( Ref ) Abstract


now recompile modules and rebuild docs. When you use the SetBlend MASKBLEND function, you can use SetBlendRef with values 0-255 to change at what level the pixels are drawn. Here's a sample program.
SuperStrict
SetGraphicsDriver D3D9Max2DDriver()

Local Filename:String = RequestFile("File")
Graphics 800,600,32


Local Image:TImage = LoadImage(ResizePixmap(LoadPixmap(filename),800,600))

Local pixmap:TPixmap = LockImage(Image)
For Local x:Int = 0 To 799
	For Local y:Int = 0 To 599
		'WritePixel(Pixmap,x,y,(ReadPixel(pixmap,x,y)&$FFFFFF)|(Rand(0,255)Shl 24))
		WritePixel(Pixmap,x,y,(ReadPixel(pixmap,x,y)& $FFFFFF)|Int(Sqr((x-400)^2+(y-300)^2)*.51)Shl 24)
	Next
Next

SetBlend MASKBLEND
Local AlphaRef:Int = 255
SetBlendRef(255)

Local Timer:Int = MilliSecs()+10
While Not KeyHit(KEY_ESCAPE) And Not AppTerminate()
	Cls
	DrawImage Image,0,0
	Flip
	
	If AlphaRef And MilliSecs() >= Timer
		AlphaRef :- 1
		If AlphaRef < 0 Then AlphaRef = 0
		SetBlendRef(AlphaRef)
		Timer :+ 10
	End If
Wend



Kryzon(Posted 2013) [#2]
Cool tweak, thanks for sharing Tom.