Bloom filter and entityalpha ?!

Blitz3D Forums/Blitz3D Programming/Bloom filter and entityalpha ?!

FlagDKT(Posted 2008) [#1]
Graphics3D 800,600,32,0

Const BLUR_CAM_X# = 65536.0
Const BLUR_CAM_Y# = 65536.0
Const BLUR_CAM_Z# = 0.0
Global BLUR_CAM% = 0
Global GLsprite%
Global GLtexsize% = 256
Global GLcam%
Global GLtex%
Global GLdummytex%
Dim BLURMESH%(4*4)

Function BlurTexture(texture, Blur_Quality, Blur_Radius#, blurpasses%)
	If Blur_Quality > 0
		If BLUR_CAM=0
			BLUR_CAM = CreateCamera()
			CameraRange BLUR_CAM, 0.1, 100
			CameraZoom BLUR_CAM, 16.0
			RotateEntity BLUR_CAM, 90, 0, 0, True
			CameraViewport BLUR_CAM, 0, 0, GLtexsize, GLtexsize
			CameraClsColor BLUR_CAM, 0,0,0
			CameraClsMode  BLUR_CAM,True, True
			PositionEntity BLUR_CAM, BLUR_CAM_X#, BLUR_CAM_Y#, BLUR_CAM_Z#
		Else
			ShowEntity BLUR_CAM
		End If
		For Loop = 0 To (Blur_Quality*4)-1
			
			If BLURMESH(loop) = 0
				BLURMESH(Loop) = CreateSprite()
				EntityBlend BLURMESH(Loop),3;3
				
				EntityTexture BLURMESH(loop),GLdummytex,0,0
				EntityTexture BLURMESH(Loop),texture,0,1
				EntityTexture BLURMESH(loop),texture,0,2
				
				EntityFX BLURMESH(Loop), 1+8
				ScaleSprite BLURMESH(Loop), 2, 2
			End If
			
			ShowEntity BLURMESH(loop)
		Next
		ScaleTexture    texture, 0.5, 0.5
		PositionTexture texture, 0.5, 0.5
		BlurRadius# = Blur_Radius# * (1.0 / 256.0)
		BlurAngleStep# = 360.0 / Float(Blur_Quality*4)
		For Loop = 0 To (Blur_Quality*4)-1
			
			BlurAngle# = BlurAngleStep# * Float(Loop) + 180.0*(Loop Mod 2)
			Xoff# = BlurRadius# * Cos(BlurAngle#)
			Yoff# = BlurRadius# * Sin(BlurAngle#)
			PositionEntity BLURMESH(Loop), BLUR_CAM_X# + Xoff#-0.006, BLUR_CAM_Y# - 16.0, BLUR_CAM_Z# + Yoff#+0.008 , True
		Next
		For i=1 To blurpasses
			RenderWorld
			CopyRect 0, 0, TextureWidth(texture), TextureHeight(texture), 0, 0, BackBuffer(), TextureBuffer(texture)
		Next
		For Loop = 0 To (Blur_Quality*4)-1
			HideEntity BLURMESH(loop)
		Next
		HideEntity BLUR_CAM
	EndIf
End Function

Function InitGlow(cam)
	GLcam = cam
	If GLtex = 0
		ClearTextureFilters()
		GLtex = CreateTexture(GLtexsize,GLtexsize,1+16+32+256)
		TextureFilter("",1+8)
	End If
	GLsprite = CreateSprite()
	EntityTexture GLsprite,GLtex
	PositionEntity GLsprite,0,0,100
	EntityOrder GLsprite,-9999
	EntityBlend GLsprite,3
	ScaleSprite GLsprite,100,100
	EntityParent GLsprite,cam
	EntityFX GLsprite,1
	If GLdummytex=0
		ClearTextureFilters()
		GLdummytex = CreateTexture(32,32,1)
		TextureFilter("",1+8)
		TextureBlend GLdummytex,2
		SetGlowIntensity()
	End If
End Function

Function SetGlowIntensity(d#=0.2)
	SetBuffer TextureBuffer(GLdummytex)
	ClsColor 255*d,255*d,255*d
	Cls
	SetBuffer BackBuffer()
End Function

Function RenderGlow(bleed%=30,tween%=0,blurpasses%=1)
	TextureBlend GLtex,5
	EntityColor GLsprite,bleed,bleed,bleed
	CameraViewport GLcam,0,0,GLtexsize,GLtexsize
	RenderWorld tween
	CopyRect 0,0,GLtexsize,GLtexsize,0,0,BackBuffer(),TextureBuffer(GLtex)
	CameraViewport GLcam,0,0,GraphicsWidth(),GraphicsHeight()
	EntityColor GLsprite,255,255,255
	BlurTexture(GLtex,1,4,blurpasses)
	ScaleTexture GLtex, 1, 1
	PositionTexture GLtex,0,0
End Function

Function FreeGlow()
	If GLsprite<>0
		FreeEntity GLsprite : GLsprite=0
		FreeTexture GLtex : GLtex=0
		FreeTexture GLdummytex : GLdummytex=0
		FreeEntity BLUR_CAM : BLUR_CAM=0
		For i=1 To 4*4
			If BLURMESH(i)<>0 Then FreeEntity BLURMESH(i) : BLURMESH(i)=0
		Next
	End If
End Function

cam = CreateCamera()
light = CreateLight()
mesh = CreateCube()
PositionEntity mesh,0,0,3
EntityColor mesh,255,0,0
ScaleEntity mesh,.5,.5,.5
EntityAlpha mesh,0 ;<------------------ !!!!!!!!
InitGlow(cam)

Repeat
	TurnEntity mesh,0,1,1
	SetGlowIntensity(0.03)
	RenderGlow(30)
	RenderWorld
	Flip True
Until KeyDown(1)=1
FreeGlow()
End


Did'nt remember where I took this bloomFilter from but it works almost perfect, BUT there's a little problem.
As you may see in this example, the Cube mesh has entityAlpha 0 , so how could bloom filter render it??!?
AND..I use a different cameraZoom. How to align blur Texture correctly??

I'd really need this filter though ;(


MikhailV(Posted 2008) [#2]
It's very slow... May be FastExtension library?


FlagDKT(Posted 2008) [#3]
Some questions :

1)How much faster is your glow effect?
2)Don't you think that using an external dll may lead to compatibility problem?
3)Why don't you create a dll for the slowest thing to do on blitz3d...the Shadows? :)


MikhailV(Posted 2008) [#4]
1 - You can test it, download lib from my site.

2 - Compatibility problem? Many users tested library more than one year and have not found problems.

3. Shadows is a hard task for Blitz3D. Stencil shadows are already realised in other libraries, but I think that they cannot be applied in projects (very slow on DirectX7). There was now a possibility to create fast projective shadows (pseudo PSSM with small limitations) - this in progress...

Simply try it, I do not force you to use library in the projects.


FlagDKT(Posted 2008) [#5]
Your libs are really cool.
I think i'll buy them soon.
Good work man !! :)