WorldViz User Forum  

Go Back   WorldViz User Forum > Vizard

Reply
 
Thread Tools Rate Thread Display Modes
  #1  
Old 04-16-2010, 01:34 PM
whj whj is offline
Member
 
Join Date: Apr 2008
Posts: 60
Shader for blurring

Hi,

I had this sample code for blurring. But I messed it up and don't know where is the problem. It should can enable fullscreen blurring, and larger kernel create more blur. Can somebody help me to fix it and explain it? Thanks a lot.

Code:
import viz

viz.go()

viz.add('tankmaze.wrl')

size=512
kernel=3

d = viz.Data()
d.scale = 1.0
d.created = False


d.created = True
d.passes = []

#Uniforms for shaders
srcImageUniform = viz.addUniformInt('srcImage',0)
blurScaleUniform = viz.addUniformFloat('blurScale',1.0)

blur_source = """
uniform sampler2D srcImage;
uniform float blurScale;
void main(void)
{
	vec4 color = vec4(0,0,0,0);
	%code%
	color.a = 1.0;
	gl_FragColor = color;
}"""
		
#Calculate weight and offsets for blur code
weights = []
offsets = []
mid = float(kernel - 1)
kernel *= 2
for i in xrange(0,kernel,2):
	offsets.append( ((i-mid) / 2.0) * (1.0 / size) )
	x = (i - mid) / mid
	weights.append( 0.05 + ((-(x*x) + 1.0) / 4.0) )
		
#Normalize weights
total = sum(weights)
frac = 1.0 / total
weights = [ frac * x for x in weights]

#Create blur code for shaders
horz_blur_code = []
vert_blur_code = []
for w,o in zip(weights,offsets):
	horz_blur_code.append('color += ( texture2D( srcImage, gl_TexCoord[0].xy + vec2( %f*blurScale, 0.0 ) ) * %f );'%(o,w))
	vert_blur_code.append('color += ( texture2D( srcImage, gl_TexCoord[0].xy + vec2( 0.0, %f*blurScale ) ) * %f );'%(o,w))
			
horzBlurShader = viz.addShader(frag=blur_source.replace('%code%','\n'.join( horz_blur_code ) ))
vertBlurShader = viz.addShader(frag=blur_source.replace('%code%','\n'.join( vert_blur_code ) ))


for eye in [viz.RENDER_LEFT,viz.RENDER_RIGHT]:
			
	#Render textures for ping-ponging
	tex1 = viz.addRenderTexture()
	tex2 = viz.addRenderTexture()
			
	"""
	Pass 1 renders the normal scene to a texture
	"""
	pass1 = viz.addRenderNode(size=[size,size])
	pass1.attachTexture(tex1)
	pass1.setOrder(viz.PRE_RENDER,-5)
	pass1.disable(eye)
			
	"""
	Pass 2 renders the texture from pass 1 onto a quad.
	The quad has a shader attached which blurs the texture horizontally.
	"""
	pass2 = viz.addRenderNode()
	pass2.setHUD(0,200,0,200,renderQuad=True)
	pass2.setSize(size,size)
	pass2.setBuffer(viz.RENDER_FBO)
	pass2.setOrder(viz.PRE_RENDER,-4)
	pass2.attachTexture(tex2)
	pass2.texture(tex1)
	pass2.apply(horzBlurShader)
	pass2.apply(srcImageUniform)
	pass2.apply(blurScaleUniform)
	pass2.disable(eye)
			
	"""
	Pass 3 renders the texture from pass 2 onto a quad.
	The quad has a shader attached which blurs the texture vertically.
	"""
	pass3 = viz.addRenderNode()
	pass3.setHUD(0,200,0,200,renderQuad=True)
	pass3.setSize(size,size)
	pass3.setBuffer(viz.RENDER_FBO)
	pass3.setOrder(viz.PRE_RENDER,-3)
	pass3.attachTexture(tex1)
	pass3.texture(tex2)
	pass3.apply(vertBlurShader)
	pass3.apply(srcImageUniform)
	pass3.apply(blurScaleUniform)
	pass3.disable(eye)
			
	"""
	Pass 4 renders the final blurred texture to the screen
	"""
	pass4 = viz.addRenderNode()
	pass4.setHUD(0,200,0,200,renderQuad=True)
	pass4.setOrder(viz.POST_RENDER,-1)
	pass4.texture(tex1)
	pass4.disable(eye)
			
	#Save render nodes and blur uniform
	d.passes += [pass1,pass2,pass3,pass4]

d.blurUniform = blurScaleUniform
d.blurUniform.set(d.scale)


[ p.visible(True) for p in d.passes ]
viz.MainScene.visible(0,viz.WORLD)
Reply With Quote
  #2  
Old 04-19-2010, 11:28 AM
whj whj is offline
Member
 
Join Date: Apr 2008
Posts: 60
Hi,

I just realized that the code works well on the other computer in our lab. I guess that depends on graphic card.

So my next question is, how can I apply the blurring to part of my view? Like in a circle area in the middle?

THANKS A LOT!
Reply With Quote
Reply

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
begginer tutorial shader? Darkmax Vizard 1 04-15-2010 04:26 PM
Multiple Textures for Diffuse and Specularity Shader Issue shivanangel Vizard 1 05-11-2009 10:44 AM
Specular Shader Issue shivanangel Vizard 2 05-06-2009 11:08 AM
Vertex shader performance problem Joran Vizard 2 11-17-2008 01:29 AM
Fur Shader djdesmangles Vizard 1 05-29-2008 09:37 PM


All times are GMT -7. The time now is 03:54 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2021, vBulletin Solutions, Inc.
Copyright 2002-2018 WorldViz LLC