<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Game Rendering &#187; GLSL</title>
	<atom:link href="http://www.gamerendering.com/tag/glsl/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.gamerendering.com</link>
	<description></description>
	<lastBuildDate>Thu, 21 Jan 2010 01:32:12 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.5</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Radial Blur Filter</title>
		<link>http://www.gamerendering.com/2008/12/20/radial-blur-filter/</link>
		<comments>http://www.gamerendering.com/2008/12/20/radial-blur-filter/#comments</comments>
		<pubDate>Sat, 20 Dec 2008 01:43:55 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Special Effects]]></category>
		<category><![CDATA[Blur]]></category>
		<category><![CDATA[Blur Filter]]></category>
		<category><![CDATA[Effects]]></category>
		<category><![CDATA[Games]]></category>
		<category><![CDATA[GLSL]]></category>
		<category><![CDATA[Radial Blur]]></category>
		<category><![CDATA[Radial Blur Filter]]></category>
		<category><![CDATA[Shader]]></category>

		<guid isPermaLink="false">http://www.gamerendering.com/?p=543</guid>
		<description><![CDATA[Radial blur is an effect that is often used in racing games and FPS-games. In racing games it can give the feel of speed and in fps games it&#8217;s often used when something important has happened, for example the player has been hit by an enemy. Radial blur is one of many blur filters that [...]]]></description>
			<content:encoded><![CDATA[<p>Radial blur is an effect that is often used in racing games and FPS-games. In racing games it can give the feel of speed and in fps games it&#8217;s often used when something important has happened, for example the player has been hit by an enemy. Radial blur is one of many blur filters that can be created in a shader<br />
The image below shows to the left the original image and to the right the radial blurred one.</p>
<p><a href="http://www.gamerendering.com/wp-content/uploads/radialblurinaction.jpg"><img class="size-medium wp-image-545" title="Radial Blur in the right image. The left image is the original." src="http://www.gamerendering.com/wp-content/uploads/radialblurinaction-400x196.jpg" alt="Radial Blur in the right image. The left image is the original." width="400" height="196" /></a></p>
<p>The effects work like the following. First render the scene normally to a texture, then draw a full-screen quad with the radial blur shader.</p>
<p>The radial blur shader blurs the image in the direction towards the center of the screen. When considering uv-mapping, the center of the screen is the coordinates 0.5,0.5 because the uv-mapping starts with 0,0 and ends with 1,1. When drawing a fragment, we know it&#8217;s current uv-mapping coordinates (this can be the black dot in the image below) since it&#8217;s passed down by the vertex shader. We can simply create the direction vector by subtracting the current fragments uv-coordinates from the center of the screen coordinates. This vector will have an distance which can be used when blurring so that the image is more blurred the further it&#8217;s away from the middle (like the image above). Of course you can also remove this distance check and do a constant radial blur over the whole screen.</p>
<p><a href="http://www.gamerendering.com/wp-content/uploads/radial-blur.gif"><img class="size-medium wp-image-544" title="Radial Blur" src="http://www.gamerendering.com/wp-content/uploads/radial-blur.gif" alt="Radial Blur" width="286" height="225" /></a></p>
<p>The GLSL vertex shader:</p>

<div class="wp_codebox"><table width="100%" ><tr id="p5433"><td class="code" id="p543code3"><pre class="c" style="font-family:monospace;">varying vec2  uv<span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// this vertex shader is from AMD RenderMonkey</span>
<span style="color: #993333;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #993333;">void</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
   gl_Position <span style="color: #339933;">=</span> vec4<span style="color: #009900;">&#40;</span> gl_Vertex.<span style="color: #202020;">xy</span><span style="color: #339933;">,</span> <span style="color:#800080;">0.0</span><span style="color: #339933;">,</span> <span style="color:#800080;">1.0</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   gl_Position <span style="color: #339933;">=</span> sign<span style="color: #009900;">&#40;</span> gl_Position <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   
   <span style="color: #666666; font-style: italic;">// Texture coordinate for screen aligned (in correct range):</span>
   uv <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>vec2<span style="color: #009900;">&#40;</span> gl_Position.<span style="color: #202020;">x</span><span style="color: #339933;">,</span> <span style="color: #339933;">-</span> gl_Position.<span style="color: #202020;">y</span> <span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> vec2<span style="color: #009900;">&#40;</span> <span style="color:#800080;">1.0</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #339933;">/</span> vec2<span style="color: #009900;">&#40;</span> <span style="color:#800080;">2.0</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>The GLSL fragment shader:</p>

<div class="wp_codebox"><table width="100%" ><tr id="p5434"><td class="code" id="p543code4"><pre class="c" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// This texture should hold the image to blur.</span>
<span style="color: #666666; font-style: italic;">// This can perhaps be a previous rendered scene.</span>
uniform sampler2D tex<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// texture coordinates passed from the vertex shader</span>
varying vec2 uv<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// some const, tweak for best look</span>
<span style="color: #993333;">const</span> <span style="color: #993333;">float</span> sampleDist <span style="color: #339933;">=</span> <span style="color:#800080;">1.0</span><span style="color: #339933;">;</span>
<span style="color: #993333;">const</span> <span style="color: #993333;">float</span> sampleStrength <span style="color: #339933;">=</span> <span style="color:#800080;">2.2</span><span style="color: #339933;">;</span> 
&nbsp;
<span style="color: #993333;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #993333;">void</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
   <span style="color: #666666; font-style: italic;">// some sample positions</span>
   <span style="color: #993333;">float</span> samples<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">10</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span>
   <span style="color: #993333;">float</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span><span style="color:#800080;">0.08</span><span style="color: #339933;">,-</span><span style="color:#800080;">0.05</span><span style="color: #339933;">,-</span><span style="color:#800080;">0.03</span><span style="color: #339933;">,-</span><span style="color:#800080;">0.02</span><span style="color: #339933;">,-</span><span style="color:#800080;">0.01</span><span style="color: #339933;">,</span><span style="color:#800080;">0.01</span><span style="color: #339933;">,</span><span style="color:#800080;">0.02</span><span style="color: #339933;">,</span><span style="color:#800080;">0.03</span><span style="color: #339933;">,</span><span style="color:#800080;">0.05</span><span style="color: #339933;">,</span><span style="color:#800080;">0.08</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// 0.5,0.5 is the center of the screen</span>
    <span style="color: #666666; font-style: italic;">// so substracting uv from it will result in</span>
    <span style="color: #666666; font-style: italic;">// a vector pointing to the middle of the screen</span>
    vec2 dir <span style="color: #339933;">=</span> <span style="color:#800080;">0.5</span> <span style="color: #339933;">-</span> uv<span style="color: #339933;">;</span> 
&nbsp;
    <span style="color: #666666; font-style: italic;">// calculate the distance to the center of the screen</span>
    <span style="color: #993333;">float</span> dist <span style="color: #339933;">=</span> sqrt<span style="color: #009900;">&#40;</span>dir.<span style="color: #202020;">x</span><span style="color: #339933;">*</span>dir.<span style="color: #202020;">x</span> <span style="color: #339933;">+</span> dir.<span style="color: #202020;">y</span><span style="color: #339933;">*</span>dir.<span style="color: #202020;">y</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
&nbsp;
    <span style="color: #666666; font-style: italic;">// normalize the direction (reuse the distance)</span>
    dir <span style="color: #339933;">=</span> dir<span style="color: #339933;">/</span>dist<span style="color: #339933;">;</span> 
&nbsp;
    <span style="color: #666666; font-style: italic;">// this is the original colour of this fragment</span>
    <span style="color: #666666; font-style: italic;">// using only this would result in a nonblurred version</span>
    vec4 color <span style="color: #339933;">=</span> texture2D<span style="color: #009900;">&#40;</span>tex<span style="color: #339933;">,</span>uv<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
&nbsp;
    vec4 sum <span style="color: #339933;">=</span> color<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// take 10 additional blur samples in the direction towards</span>
    <span style="color: #666666; font-style: italic;">// the center of the screen</span>
    <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> i <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> <span style="color: #0000dd;">10</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      sum <span style="color: #339933;">+=</span> texture2D<span style="color: #009900;">&#40;</span> tex<span style="color: #339933;">,</span> uv <span style="color: #339933;">+</span> dir <span style="color: #339933;">*</span> samples<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">*</span> sampleDist <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// we have taken eleven samples</span>
    sum <span style="color: #339933;">*=</span> <span style="color:#800080;">1.0</span><span style="color: #339933;">/</span><span style="color:#800080;">11.0</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// weighten the blur effect with the distance to the</span>
    <span style="color: #666666; font-style: italic;">// center of the screen ( further out is blurred more)</span>
    <span style="color: #993333;">float</span> t <span style="color: #339933;">=</span> dist <span style="color: #339933;">*</span> sampleStrength<span style="color: #339933;">;</span>
    t <span style="color: #339933;">=</span> clamp<span style="color: #009900;">&#40;</span> t <span style="color: #339933;">,</span><span style="color:#800080;">0.0</span><span style="color: #339933;">,</span><span style="color:#800080;">1.0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//0 &amp;lt;= t &amp;lt;= 1</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">//Blend the original color with the averaged pixels</span>
    gl_FragColor <span style="color: #339933;">=</span> mix<span style="color: #009900;">&#40;</span> color<span style="color: #339933;">,</span> sum<span style="color: #339933;">,</span> t <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> </pre></td></tr></table></div>

<p>This shader is mostly inspired and copied from a CG-shader in Ogre3D.<br />
<a href="http://www.ogre3d.org">www.ogre3D.org</a></p>

<div class="sociable">
<div class="sociable_tagline">
<strong>Please share:</strong>
</div>
<ul>
	<li class="sociablefirst"><a rel="nofollow" id="print" href="javascript:window.location='http%3A%2F%2Fwww.printfriendly.com%2Fprint%3Furl%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F12%252F20%252Fradial-blur-filter%252F%26amp%3Bpartner%3Dsociable';" title="Print this article!"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/printfriendly.png" title="Print this article!" alt="Print this article!" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="digg" href="javascript:window.location='http%3A%2F%2Fdigg.com%2Fsubmit%3Fphase%3D2%26amp%3Burl%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F12%252F20%252Fradial-blur-filter%252F%26amp%3Btitle%3DRadial%2520Blur%2520Filter%26amp%3Bbodytext%3DRadial%2520blur%2520is%2520an%2520effect%2520that%2520is%2520often%2520used%2520in%2520racing%2520games%2520and%2520FPS-games.%2520In%2520racing%2520games%2520it%2520can%2520give%2520the%2520feel%2520of%2520speed%2520and%2520in%2520fps%2520games%2520it%2527s%2520often%2520used%2520when%2520something%2520important%2520has%2520happened%252C%2520for%2520example%2520the%2520player%2520has%2520been%2520hit%2520by%2520an%2520enemy.%2520Radial%2520b';" title="Digg"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="sphinn" href="javascript:window.location='http%3A%2F%2Fsphinn.com%2Findex.php%3Fc%3Dpost%26m%3Dsubmit%26link%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F12%252F20%252Fradial-blur-filter%252F';" title="Sphinn"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/sphinn.png" title="Sphinn" alt="Sphinn" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="del.icio.us" href="javascript:window.location='http%3A%2F%2Fdelicious.com%2Fpost%3Furl%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F12%252F20%252Fradial-blur-filter%252F%26amp%3Btitle%3DRadial%2520Blur%2520Filter%26amp%3Bnotes%3DRadial%2520blur%2520is%2520an%2520effect%2520that%2520is%2520often%2520used%2520in%2520racing%2520games%2520and%2520FPS-games.%2520In%2520racing%2520games%2520it%2520can%2520give%2520the%2520feel%2520of%2520speed%2520and%2520in%2520fps%2520games%2520it%2527s%2520often%2520used%2520when%2520something%2520important%2520has%2520happened%252C%2520for%2520example%2520the%2520player%2520has%2520been%2520hit%2520by%2520an%2520enemy.%2520Radial%2520b';" title="del.icio.us"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="facebook" href="javascript:window.location='http%3A%2F%2Fwww.facebook.com%2Fshare.php%3Fu%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F12%252F20%252Fradial-blur-filter%252F%26amp%3Bt%3DRadial%2520Blur%2520Filter';" title="Facebook"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="mixx" href="javascript:window.location='http%3A%2F%2Fwww.mixx.com%2Fsubmit%3Fpage_url%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F12%252F20%252Fradial-blur-filter%252F%26amp%3Btitle%3DRadial%2520Blur%2520Filter';" title="Mixx"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/mixx.png" title="Mixx" alt="Mixx" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="google" href="javascript:window.location='http%3A%2F%2Fwww.google.com%2Fbookmarks%2Fmark%3Fop%3Dedit%26amp%3Bbkmk%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F12%252F20%252Fradial-blur-filter%252F%26amp%3Btitle%3DRadial%2520Blur%2520Filter%26amp%3Bannotation%3DRadial%2520blur%2520is%2520an%2520effect%2520that%2520is%2520often%2520used%2520in%2520racing%2520games%2520and%2520FPS-games.%2520In%2520racing%2520games%2520it%2520can%2520give%2520the%2520feel%2520of%2520speed%2520and%2520in%2520fps%2520games%2520it%2527s%2520often%2520used%2520when%2520something%2520important%2520has%2520happened%252C%2520for%2520example%2520the%2520player%2520has%2520been%2520hit%2520by%2520an%2520enemy.%2520Radial%2520b';" title="Google Bookmarks"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/googlebookmark.png" title="Google Bookmarks" alt="Google Bookmarks" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="current" href="javascript:window.location='http%3A%2F%2Fcurrent.com%2Fclipper.htm%3Furl%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F12%252F20%252Fradial-blur-filter%252F%26amp%3Btitle%3DRadial%2520Blur%2520Filter';" title="Current"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/current.png" title="Current" alt="Current" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="linkedin" href="javascript:window.location='http%3A%2F%2Fwww.linkedin.com%2FshareArticle%3Fmini%3Dtrue%26amp%3Burl%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F12%252F20%252Fradial-blur-filter%252F%26amp%3Btitle%3DRadial%2520Blur%2520Filter%26amp%3Bsource%3DGame%2BRendering%2B%26amp%3Bsummary%3DRadial%2520blur%2520is%2520an%2520effect%2520that%2520is%2520often%2520used%2520in%2520racing%2520games%2520and%2520FPS-games.%2520In%2520racing%2520games%2520it%2520can%2520give%2520the%2520feel%2520of%2520speed%2520and%2520in%2520fps%2520games%2520it%2527s%2520often%2520used%2520when%2520something%2520important%2520has%2520happened%252C%2520for%2520example%2520the%2520player%2520has%2520been%2520hit%2520by%2520an%2520enemy.%2520Radial%2520b';" title="LinkedIn"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/linkedin.png" title="LinkedIn" alt="LinkedIn" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="live" href="javascript:window.location='https%3A%2F%2Ffavorites.live.com%2Fquickadd.aspx%3Fmarklet%3D1%26amp%3Burl%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F12%252F20%252Fradial-blur-filter%252F%26amp%3Btitle%3DRadial%2520Blur%2520Filter';" title="Live"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/live.png" title="Live" alt="Live" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="myspace" href="javascript:window.location='http%3A%2F%2Fwww.myspace.com%2FModules%2FPostTo%2FPages%2F%3Fu%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F12%252F20%252Fradial-blur-filter%252F%26amp%3Bt%3DRadial%2520Blur%2520Filter';" title="MySpace"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/myspace.png" title="MySpace" alt="MySpace" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="netvibes" href="javascript:window.location='http%3A%2F%2Fwww.netvibes.com%2Fshare%3Ftitle%3DRadial%2520Blur%2520Filter%26amp%3Burl%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F12%252F20%252Fradial-blur-filter%252F';" title="Netvibes"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/netvibes.png" title="Netvibes" alt="Netvibes" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="stumbleupon" href="javascript:window.location='http%3A%2F%2Fwww.stumbleupon.com%2Fsubmit%3Furl%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F12%252F20%252Fradial-blur-filter%252F%26amp%3Btitle%3DRadial%2520Blur%2520Filter';" title="StumbleUpon"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/stumbleupon.png" title="StumbleUpon" alt="StumbleUpon" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="twitter" href="javascript:window.location='http%3A%2F%2Ftwitter.com%2Fhome%3Fstatus%3DRadial%2520Blur%2520Filter%2520-%2520http%253A%252F%252Fwww.gamerendering.com%252F2008%252F12%252F20%252Fradial-blur-filter%252F';" title="Twitter"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/twitter.png" title="Twitter" alt="Twitter" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="reddit" href="javascript:window.location='http%3A%2F%2Freddit.com%2Fsubmit%3Furl%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F12%252F20%252Fradial-blur-filter%252F%26amp%3Btitle%3DRadial%2520Blur%2520Filter';" title="Reddit"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/reddit.png" title="Reddit" alt="Reddit" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="technorati" href="javascript:window.location='http%3A%2F%2Ftechnorati.com%2Ffaves%3Fadd%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F12%252F20%252Fradial-blur-filter%252F';" title="Technorati"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/technorati.png" title="Technorati" alt="Technorati" class="sociable-hovers" /></a></li>
	<li class="sociablelast"><a rel="nofollow" id="yahoo! bookmarks" href="javascript:window.location='http%3A%2F%2Fbookmarks.yahoo.com%2Ftoolbar%2Fsavebm%3Fu%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F12%252F20%252Fradial-blur-filter%252F%26amp%3Bt%3DRadial%2520Blur%2520Filter%26opener%3Dbm%26amp%3Bei%3DUTF-8%26amp%3Bd%3DRadial%2520blur%2520is%2520an%2520effect%2520that%2520is%2520often%2520used%2520in%2520racing%2520games%2520and%2520FPS-games.%2520In%2520racing%2520games%2520it%2520can%2520give%2520the%2520feel%2520of%2520speed%2520and%2520in%2520fps%2520games%2520it%2527s%2520often%2520used%2520when%2520something%2520important%2520has%2520happened%252C%2520for%2520example%2520the%2520player%2520has%2520been%2520hit%2520by%2520an%2520enemy.%2520Radial%2520b';" title="Yahoo! Bookmarks"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/yahoomyweb.png" title="Yahoo! Bookmarks" alt="Yahoo! Bookmarks" class="sociable-hovers" /></a></li>
</ul>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.gamerendering.com/2008/12/20/radial-blur-filter/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Percentage Closer Filtering for Shadow Mapping</title>
		<link>http://www.gamerendering.com/2008/11/15/percentage-closer-filtering-for-shadow-mapping/</link>
		<comments>http://www.gamerendering.com/2008/11/15/percentage-closer-filtering-for-shadow-mapping/#comments</comments>
		<pubDate>Sat, 15 Nov 2008 15:08:46 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Shadow Mapping]]></category>
		<category><![CDATA[Bilinear Interpolation]]></category>
		<category><![CDATA[Fetch4]]></category>
		<category><![CDATA[Filtering]]></category>
		<category><![CDATA[GLSL]]></category>
		<category><![CDATA[Optimization]]></category>
		<category><![CDATA[PCF]]></category>
		<category><![CDATA[Percentage Closer Filtering]]></category>
		<category><![CDATA[Shadows]]></category>

		<guid isPermaLink="false">http://www.gamerendering.com/?p=505</guid>
		<description><![CDATA[This is a technique for making softer shadows when doing shadow mapping. It works by filtering the result of the depth comparison. So when comparing a depth, some depths around should also be compared and the result should be averaged. This will give a softer look on the shadow edges.

It can be implemented as simple as this in [...]]]></description>
			<content:encoded><![CDATA[<p>This is a technique for making softer shadows when doing shadow mapping. It works by filtering the result of the depth comparison. So when comparing a depth, some depths around should also be compared and the result should be averaged. This will give a softer look on the shadow edges.</p>
<div class="mceTemp"><a href="http://www.gamerendering.com/wp-content/uploads/pcf.jpg"><img class="size-medium wp-image-509" title="An example of the the soft shadows when using PCF Shadow Mapping" src="http://www.gamerendering.com/wp-content/uploads/pcf.jpg" alt="An example of the the soft shadows when using PCF Shadow Mapping" width="370" height="252" /></a></div>
<p>It can be implemented as simple as this in a the pixel shader:</p>

<div class="wp_codebox"><table width="100%" ><tr id="p5056"><td class="code" id="p505code6"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">float</span> result<span style="color: #339933;">;</span>
result <span style="color: #339933;">=</span> shadow2DProj<span style="color: #009900;">&#40;</span>shadowMap<span style="color: #339933;">,</span>texCoord<span style="color: #339933;">+</span>offset<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
result <span style="color: #339933;">+=</span> shadow2DProj<span style="color: #009900;">&#40;</span>shadowMap<span style="color: #339933;">,</span>texCoord<span style="color: #339933;">+</span>offset<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
result <span style="color: #339933;">+=</span> shadow2DProj<span style="color: #009900;">&#40;</span>shadowMap<span style="color: #339933;">,</span>texCoord<span style="color: #339933;">+</span>offset<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">2</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
result <span style="color: #339933;">+=</span> shadow2DProj<span style="color: #009900;">&#40;</span>shadowMap<span style="color: #339933;">,</span>texCoord<span style="color: #339933;">+</span>offset<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">3</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
result <span style="color: #339933;">/=</span> <span style="color:#800080;">4.0</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// now result will hold the average shading</span></pre></td></tr></table></div>

<p>The samples are often either taken in a grid-based square around the original sample location or randomly scattered around it.</p>
<p><strong>Optimization:</strong></p>
<p>NVIDIA has built in hardware support for doing bilinear interpolation between four samples.</p>
<p>ATI has fetch4 which will fetch four texture samples at the same time.  </p>
<p>The original paper for PCF:<br />
<a href="http://graphics.pixar.com/library/ShadowMaps/paper.pdf">http://graphics.pixar.com/library/ShadowMaps/paper.pdf</a></p>
<p>A technique that is similar (percentage-closer soft shadows)<br />
<a href="http://developer.download.nvidia.com/shaderlibrary/docs/shadow_PCSS.pdf">http://developer.download.nvidia.com/shaderlibrary/docs/shadow_PCSS.pdf</a></p>
<p>A paper including lot&#8217;s of shadow mapping information, including different ways to do PCF (the image in this article is borrowed from this presentation):<br />
<a href="http://developer.amd.com/media/gpu_assets/Isidoro-ShadowMapping.pdf">http://developer.amd.com/media/gpu_assets/Isidoro-ShadowMapping.pdf</a></p>

<div class="sociable">
<div class="sociable_tagline">
<strong>Please share:</strong>
</div>
<ul>
	<li class="sociablefirst"><a rel="nofollow" id="print" href="javascript:window.location='http%3A%2F%2Fwww.printfriendly.com%2Fprint%3Furl%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F11%252F15%252Fpercentage-closer-filtering-for-shadow-mapping%252F%26amp%3Bpartner%3Dsociable';" title="Print this article!"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/printfriendly.png" title="Print this article!" alt="Print this article!" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="digg" href="javascript:window.location='http%3A%2F%2Fdigg.com%2Fsubmit%3Fphase%3D2%26amp%3Burl%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F11%252F15%252Fpercentage-closer-filtering-for-shadow-mapping%252F%26amp%3Btitle%3DPercentage%2520Closer%2520Filtering%2520for%2520Shadow%2520Mapping%26amp%3Bbodytext%3DThis%2520is%2520a%2520technique%2520for%2520making%2520softer%2520shadows%2520when%2520doing%2520shadow%2520mapping.%2520It%2520works%2520by%2520filtering%2520the%2520result%2520of%2520the%2520depth%2520comparison.%2520So%2520when%2520comparing%2520a%2520depth%252C%2520some%2520depths%2520around%2520should%2520also%2520be%2520compared%2520and%2520the%2520result%2520should%2520be%2520averaged.%2520This%2520will%2520give';" title="Digg"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="sphinn" href="javascript:window.location='http%3A%2F%2Fsphinn.com%2Findex.php%3Fc%3Dpost%26m%3Dsubmit%26link%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F11%252F15%252Fpercentage-closer-filtering-for-shadow-mapping%252F';" title="Sphinn"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/sphinn.png" title="Sphinn" alt="Sphinn" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="del.icio.us" href="javascript:window.location='http%3A%2F%2Fdelicious.com%2Fpost%3Furl%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F11%252F15%252Fpercentage-closer-filtering-for-shadow-mapping%252F%26amp%3Btitle%3DPercentage%2520Closer%2520Filtering%2520for%2520Shadow%2520Mapping%26amp%3Bnotes%3DThis%2520is%2520a%2520technique%2520for%2520making%2520softer%2520shadows%2520when%2520doing%2520shadow%2520mapping.%2520It%2520works%2520by%2520filtering%2520the%2520result%2520of%2520the%2520depth%2520comparison.%2520So%2520when%2520comparing%2520a%2520depth%252C%2520some%2520depths%2520around%2520should%2520also%2520be%2520compared%2520and%2520the%2520result%2520should%2520be%2520averaged.%2520This%2520will%2520give';" title="del.icio.us"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="facebook" href="javascript:window.location='http%3A%2F%2Fwww.facebook.com%2Fshare.php%3Fu%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F11%252F15%252Fpercentage-closer-filtering-for-shadow-mapping%252F%26amp%3Bt%3DPercentage%2520Closer%2520Filtering%2520for%2520Shadow%2520Mapping';" title="Facebook"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="mixx" href="javascript:window.location='http%3A%2F%2Fwww.mixx.com%2Fsubmit%3Fpage_url%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F11%252F15%252Fpercentage-closer-filtering-for-shadow-mapping%252F%26amp%3Btitle%3DPercentage%2520Closer%2520Filtering%2520for%2520Shadow%2520Mapping';" title="Mixx"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/mixx.png" title="Mixx" alt="Mixx" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="google" href="javascript:window.location='http%3A%2F%2Fwww.google.com%2Fbookmarks%2Fmark%3Fop%3Dedit%26amp%3Bbkmk%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F11%252F15%252Fpercentage-closer-filtering-for-shadow-mapping%252F%26amp%3Btitle%3DPercentage%2520Closer%2520Filtering%2520for%2520Shadow%2520Mapping%26amp%3Bannotation%3DThis%2520is%2520a%2520technique%2520for%2520making%2520softer%2520shadows%2520when%2520doing%2520shadow%2520mapping.%2520It%2520works%2520by%2520filtering%2520the%2520result%2520of%2520the%2520depth%2520comparison.%2520So%2520when%2520comparing%2520a%2520depth%252C%2520some%2520depths%2520around%2520should%2520also%2520be%2520compared%2520and%2520the%2520result%2520should%2520be%2520averaged.%2520This%2520will%2520give';" title="Google Bookmarks"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/googlebookmark.png" title="Google Bookmarks" alt="Google Bookmarks" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="current" href="javascript:window.location='http%3A%2F%2Fcurrent.com%2Fclipper.htm%3Furl%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F11%252F15%252Fpercentage-closer-filtering-for-shadow-mapping%252F%26amp%3Btitle%3DPercentage%2520Closer%2520Filtering%2520for%2520Shadow%2520Mapping';" title="Current"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/current.png" title="Current" alt="Current" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="linkedin" href="javascript:window.location='http%3A%2F%2Fwww.linkedin.com%2FshareArticle%3Fmini%3Dtrue%26amp%3Burl%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F11%252F15%252Fpercentage-closer-filtering-for-shadow-mapping%252F%26amp%3Btitle%3DPercentage%2520Closer%2520Filtering%2520for%2520Shadow%2520Mapping%26amp%3Bsource%3DGame%2BRendering%2B%26amp%3Bsummary%3DThis%2520is%2520a%2520technique%2520for%2520making%2520softer%2520shadows%2520when%2520doing%2520shadow%2520mapping.%2520It%2520works%2520by%2520filtering%2520the%2520result%2520of%2520the%2520depth%2520comparison.%2520So%2520when%2520comparing%2520a%2520depth%252C%2520some%2520depths%2520around%2520should%2520also%2520be%2520compared%2520and%2520the%2520result%2520should%2520be%2520averaged.%2520This%2520will%2520give';" title="LinkedIn"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/linkedin.png" title="LinkedIn" alt="LinkedIn" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="live" href="javascript:window.location='https%3A%2F%2Ffavorites.live.com%2Fquickadd.aspx%3Fmarklet%3D1%26amp%3Burl%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F11%252F15%252Fpercentage-closer-filtering-for-shadow-mapping%252F%26amp%3Btitle%3DPercentage%2520Closer%2520Filtering%2520for%2520Shadow%2520Mapping';" title="Live"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/live.png" title="Live" alt="Live" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="myspace" href="javascript:window.location='http%3A%2F%2Fwww.myspace.com%2FModules%2FPostTo%2FPages%2F%3Fu%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F11%252F15%252Fpercentage-closer-filtering-for-shadow-mapping%252F%26amp%3Bt%3DPercentage%2520Closer%2520Filtering%2520for%2520Shadow%2520Mapping';" title="MySpace"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/myspace.png" title="MySpace" alt="MySpace" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="netvibes" href="javascript:window.location='http%3A%2F%2Fwww.netvibes.com%2Fshare%3Ftitle%3DPercentage%2520Closer%2520Filtering%2520for%2520Shadow%2520Mapping%26amp%3Burl%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F11%252F15%252Fpercentage-closer-filtering-for-shadow-mapping%252F';" title="Netvibes"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/netvibes.png" title="Netvibes" alt="Netvibes" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="stumbleupon" href="javascript:window.location='http%3A%2F%2Fwww.stumbleupon.com%2Fsubmit%3Furl%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F11%252F15%252Fpercentage-closer-filtering-for-shadow-mapping%252F%26amp%3Btitle%3DPercentage%2520Closer%2520Filtering%2520for%2520Shadow%2520Mapping';" title="StumbleUpon"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/stumbleupon.png" title="StumbleUpon" alt="StumbleUpon" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="twitter" href="javascript:window.location='http%3A%2F%2Ftwitter.com%2Fhome%3Fstatus%3DPercentage%2520Closer%2520Filtering%2520for%2520Shadow%2520Mapping%2520-%2520http%253A%252F%252Fwww.gamerendering.com%252F2008%252F11%252F15%252Fpercentage-closer-filtering-for-shadow-mapping%252F';" title="Twitter"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/twitter.png" title="Twitter" alt="Twitter" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="reddit" href="javascript:window.location='http%3A%2F%2Freddit.com%2Fsubmit%3Furl%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F11%252F15%252Fpercentage-closer-filtering-for-shadow-mapping%252F%26amp%3Btitle%3DPercentage%2520Closer%2520Filtering%2520for%2520Shadow%2520Mapping';" title="Reddit"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/reddit.png" title="Reddit" alt="Reddit" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="technorati" href="javascript:window.location='http%3A%2F%2Ftechnorati.com%2Ffaves%3Fadd%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F11%252F15%252Fpercentage-closer-filtering-for-shadow-mapping%252F';" title="Technorati"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/technorati.png" title="Technorati" alt="Technorati" class="sociable-hovers" /></a></li>
	<li class="sociablelast"><a rel="nofollow" id="yahoo! bookmarks" href="javascript:window.location='http%3A%2F%2Fbookmarks.yahoo.com%2Ftoolbar%2Fsavebm%3Fu%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F11%252F15%252Fpercentage-closer-filtering-for-shadow-mapping%252F%26amp%3Bt%3DPercentage%2520Closer%2520Filtering%2520for%2520Shadow%2520Mapping%26opener%3Dbm%26amp%3Bei%3DUTF-8%26amp%3Bd%3DThis%2520is%2520a%2520technique%2520for%2520making%2520softer%2520shadows%2520when%2520doing%2520shadow%2520mapping.%2520It%2520works%2520by%2520filtering%2520the%2520result%2520of%2520the%2520depth%2520comparison.%2520So%2520when%2520comparing%2520a%2520depth%252C%2520some%2520depths%2520around%2520should%2520also%2520be%2520compared%2520and%2520the%2520result%2520should%2520be%2520averaged.%2520This%2520will%2520give';" title="Yahoo! Bookmarks"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/yahoomyweb.png" title="Yahoo! Bookmarks" alt="Yahoo! Bookmarks" class="sociable-hovers" /></a></li>
</ul>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.gamerendering.com/2008/11/15/percentage-closer-filtering-for-shadow-mapping/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Basic Shadow Mapping</title>
		<link>http://www.gamerendering.com/2008/10/23/basic-shadow-mapping/</link>
		<comments>http://www.gamerendering.com/2008/10/23/basic-shadow-mapping/#comments</comments>
		<pubDate>Wed, 22 Oct 2008 23:28:12 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Shadow Mapping]]></category>
		<category><![CDATA[Artifacts]]></category>
		<category><![CDATA[Bias]]></category>
		<category><![CDATA[Camera Space]]></category>
		<category><![CDATA[DirectX]]></category>
		<category><![CDATA[Fixed-function]]></category>
		<category><![CDATA[Fragment]]></category>
		<category><![CDATA[GLSL]]></category>
		<category><![CDATA[HLSL]]></category>
		<category><![CDATA[Light Space]]></category>
		<category><![CDATA[OpenGL]]></category>
		<category><![CDATA[Pipeline]]></category>
		<category><![CDATA[Shader]]></category>
		<category><![CDATA[Shaders]]></category>
		<category><![CDATA[Shadows]]></category>

		<guid isPermaLink="false">http://www.gamerendering.com/?p=407</guid>
		<description><![CDATA[Shadow mapping works in that it checks if a point is visible from the light or not. If a point is visible from the light then it&#8217;s obviously not in shadow, otherwise it is. The basic shadow mapping algorithm can be described as short as this:

Render the scene from the lights view and store the depths [...]]]></description>
			<content:encoded><![CDATA[<p>Shadow mapping works in that it checks if a point is visible from the light or not. If a point is visible from the light then it&#8217;s obviously not in shadow, otherwise it is. The basic shadow mapping algorithm can be described as short as this:</p>
<ol>
<li>Render the scene from the lights view and store the depths as shadow map</li>
<li>Render the scene from the camera and compare the depths, if the current fragments depth is greater than the shadow depth then the fragment is in shadow</li>
</ol>
<div class="mceTemp"><a href="http://www.gamerendering.com/wp-content/uploads/shadowmap.jpg"><img class="size-medium wp-image-408" title="Shadow mapping example" src="http://www.gamerendering.com/wp-content/uploads/shadowmap.jpg" alt="Shadow mapping example" width="274" height="163" /></a></div>
<p>It&#8217;s the implementation of it that is hard.</p>
<p>The two big problem areas with shadow mapping:</p>
<ul>
<li>Hard to select an appropriate bias (epsilon)</li>
<li>Hard to get rid of artifacts at shadow edges</li>
</ul>
<p>Projective texturing ( the method used to transform the fragment depth to the light space (where the shadow map is) for comparision)<br />
<a href="http://developer.nvidia.com/object/Projective_Texture_Mapping.html">http://developer.nvidia.com/object/Projective_Texture_Mapping.html</a><br />
<a href="http://en.wikipedia.org/wiki/Projective_texture_mapping">http://en.wikipedia.org/wiki/Projective_texture_mapping</a></p>
<p>OpenGL fixed-function pipeline implementation of shadow mapping:<br />
<a href="http://www.paulsprojects.net/tutorials/smt/smt.html">http://www.paulsprojects.net/tutorials/smt/smt.html</a></p>
<p>A GLSL implementation of shadow mapping (in one of the posts)<br />
<a href="http://www.gamedev.net/community/forums/topic.asp?topic_id=316147">http://www.gamedev.net/community/forums/topic.asp?topic_id=316147</a></p>
<p>Another GLSL shadow mapping shader:<br />
<a href="http://sombermoon.com/shadowmappingdoc.html">http://sombermoon.com/shadowmappingdoc.html</a></p>
<p>DirectX9 shadow mapping example with source<br />
<a href="http://msdn.microsoft.com/en-us/library/bb147372(VS.85).aspx">http://msdn.microsoft.com/en-us/library/bb147372(VS.85).aspx</a></p>
<p>Nvidias implementation of shadow mapping with source for both OpenGL and DirectX.<br />
<a href="http://developer.nvidia.com/object/hwshadowmap_paper.html">http://developer.nvidia.com/object/hwshadowmap_paper.html</a></p>
<p>Shadow mapping in XNA<br />
<a href="http://www.riemers.net/eng/Tutorials/DirectX/Csharp/Series3/Shadow_mapping.php">http://www.riemers.net/eng/Tutorials/DirectX/Csharp/Series3/Shadow_mapping.php</a><br />
<a href="http://msdn.microsoft.com/en-us/library/bb975671.aspx">http://msdn.microsoft.com/en-us/library/bb975671.aspx</a></p>

<div class="sociable">
<div class="sociable_tagline">
<strong>Please share:</strong>
</div>
<ul>
	<li class="sociablefirst"><a rel="nofollow" id="print" href="javascript:window.location='http%3A%2F%2Fwww.printfriendly.com%2Fprint%3Furl%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F10%252F23%252Fbasic-shadow-mapping%252F%26amp%3Bpartner%3Dsociable';" title="Print this article!"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/printfriendly.png" title="Print this article!" alt="Print this article!" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="digg" href="javascript:window.location='http%3A%2F%2Fdigg.com%2Fsubmit%3Fphase%3D2%26amp%3Burl%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F10%252F23%252Fbasic-shadow-mapping%252F%26amp%3Btitle%3DBasic%2520Shadow%2520Mapping%26amp%3Bbodytext%3DShadow%2520mapping%2520works%2520in%2520that%2520it%2520checks%2520if%2520a%2520point%2520is%2520visible%2520from%2520the%2520light%2520or%2520not.%2520If%2520a%2520point%25C2%25A0is%2520visible%2520from%2520the%2520light%2520then%2520it%2527s%2520obviously%2520not%2520in%2520shadow%252C%2520otherwise%2520it%2520is.%2520The%2520basic%2520shadow%2520mapping%2520algorithm%2520can%2520be%2520described%2520as%2520short%2520as%2520this%253A%250D%250A%250D%250A%2509Re';" title="Digg"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="sphinn" href="javascript:window.location='http%3A%2F%2Fsphinn.com%2Findex.php%3Fc%3Dpost%26m%3Dsubmit%26link%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F10%252F23%252Fbasic-shadow-mapping%252F';" title="Sphinn"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/sphinn.png" title="Sphinn" alt="Sphinn" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="del.icio.us" href="javascript:window.location='http%3A%2F%2Fdelicious.com%2Fpost%3Furl%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F10%252F23%252Fbasic-shadow-mapping%252F%26amp%3Btitle%3DBasic%2520Shadow%2520Mapping%26amp%3Bnotes%3DShadow%2520mapping%2520works%2520in%2520that%2520it%2520checks%2520if%2520a%2520point%2520is%2520visible%2520from%2520the%2520light%2520or%2520not.%2520If%2520a%2520point%25C2%25A0is%2520visible%2520from%2520the%2520light%2520then%2520it%2527s%2520obviously%2520not%2520in%2520shadow%252C%2520otherwise%2520it%2520is.%2520The%2520basic%2520shadow%2520mapping%2520algorithm%2520can%2520be%2520described%2520as%2520short%2520as%2520this%253A%250D%250A%250D%250A%2509Re';" title="del.icio.us"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="facebook" href="javascript:window.location='http%3A%2F%2Fwww.facebook.com%2Fshare.php%3Fu%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F10%252F23%252Fbasic-shadow-mapping%252F%26amp%3Bt%3DBasic%2520Shadow%2520Mapping';" title="Facebook"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="mixx" href="javascript:window.location='http%3A%2F%2Fwww.mixx.com%2Fsubmit%3Fpage_url%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F10%252F23%252Fbasic-shadow-mapping%252F%26amp%3Btitle%3DBasic%2520Shadow%2520Mapping';" title="Mixx"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/mixx.png" title="Mixx" alt="Mixx" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="google" href="javascript:window.location='http%3A%2F%2Fwww.google.com%2Fbookmarks%2Fmark%3Fop%3Dedit%26amp%3Bbkmk%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F10%252F23%252Fbasic-shadow-mapping%252F%26amp%3Btitle%3DBasic%2520Shadow%2520Mapping%26amp%3Bannotation%3DShadow%2520mapping%2520works%2520in%2520that%2520it%2520checks%2520if%2520a%2520point%2520is%2520visible%2520from%2520the%2520light%2520or%2520not.%2520If%2520a%2520point%25C2%25A0is%2520visible%2520from%2520the%2520light%2520then%2520it%2527s%2520obviously%2520not%2520in%2520shadow%252C%2520otherwise%2520it%2520is.%2520The%2520basic%2520shadow%2520mapping%2520algorithm%2520can%2520be%2520described%2520as%2520short%2520as%2520this%253A%250D%250A%250D%250A%2509Re';" title="Google Bookmarks"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/googlebookmark.png" title="Google Bookmarks" alt="Google Bookmarks" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="current" href="javascript:window.location='http%3A%2F%2Fcurrent.com%2Fclipper.htm%3Furl%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F10%252F23%252Fbasic-shadow-mapping%252F%26amp%3Btitle%3DBasic%2520Shadow%2520Mapping';" title="Current"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/current.png" title="Current" alt="Current" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="linkedin" href="javascript:window.location='http%3A%2F%2Fwww.linkedin.com%2FshareArticle%3Fmini%3Dtrue%26amp%3Burl%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F10%252F23%252Fbasic-shadow-mapping%252F%26amp%3Btitle%3DBasic%2520Shadow%2520Mapping%26amp%3Bsource%3DGame%2BRendering%2B%26amp%3Bsummary%3DShadow%2520mapping%2520works%2520in%2520that%2520it%2520checks%2520if%2520a%2520point%2520is%2520visible%2520from%2520the%2520light%2520or%2520not.%2520If%2520a%2520point%25C2%25A0is%2520visible%2520from%2520the%2520light%2520then%2520it%2527s%2520obviously%2520not%2520in%2520shadow%252C%2520otherwise%2520it%2520is.%2520The%2520basic%2520shadow%2520mapping%2520algorithm%2520can%2520be%2520described%2520as%2520short%2520as%2520this%253A%250D%250A%250D%250A%2509Re';" title="LinkedIn"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/linkedin.png" title="LinkedIn" alt="LinkedIn" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="live" href="javascript:window.location='https%3A%2F%2Ffavorites.live.com%2Fquickadd.aspx%3Fmarklet%3D1%26amp%3Burl%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F10%252F23%252Fbasic-shadow-mapping%252F%26amp%3Btitle%3DBasic%2520Shadow%2520Mapping';" title="Live"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/live.png" title="Live" alt="Live" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="myspace" href="javascript:window.location='http%3A%2F%2Fwww.myspace.com%2FModules%2FPostTo%2FPages%2F%3Fu%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F10%252F23%252Fbasic-shadow-mapping%252F%26amp%3Bt%3DBasic%2520Shadow%2520Mapping';" title="MySpace"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/myspace.png" title="MySpace" alt="MySpace" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="netvibes" href="javascript:window.location='http%3A%2F%2Fwww.netvibes.com%2Fshare%3Ftitle%3DBasic%2520Shadow%2520Mapping%26amp%3Burl%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F10%252F23%252Fbasic-shadow-mapping%252F';" title="Netvibes"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/netvibes.png" title="Netvibes" alt="Netvibes" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="stumbleupon" href="javascript:window.location='http%3A%2F%2Fwww.stumbleupon.com%2Fsubmit%3Furl%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F10%252F23%252Fbasic-shadow-mapping%252F%26amp%3Btitle%3DBasic%2520Shadow%2520Mapping';" title="StumbleUpon"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/stumbleupon.png" title="StumbleUpon" alt="StumbleUpon" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="twitter" href="javascript:window.location='http%3A%2F%2Ftwitter.com%2Fhome%3Fstatus%3DBasic%2520Shadow%2520Mapping%2520-%2520http%253A%252F%252Fwww.gamerendering.com%252F2008%252F10%252F23%252Fbasic-shadow-mapping%252F';" title="Twitter"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/twitter.png" title="Twitter" alt="Twitter" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="reddit" href="javascript:window.location='http%3A%2F%2Freddit.com%2Fsubmit%3Furl%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F10%252F23%252Fbasic-shadow-mapping%252F%26amp%3Btitle%3DBasic%2520Shadow%2520Mapping';" title="Reddit"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/reddit.png" title="Reddit" alt="Reddit" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="technorati" href="javascript:window.location='http%3A%2F%2Ftechnorati.com%2Ffaves%3Fadd%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F10%252F23%252Fbasic-shadow-mapping%252F';" title="Technorati"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/technorati.png" title="Technorati" alt="Technorati" class="sociable-hovers" /></a></li>
	<li class="sociablelast"><a rel="nofollow" id="yahoo! bookmarks" href="javascript:window.location='http%3A%2F%2Fbookmarks.yahoo.com%2Ftoolbar%2Fsavebm%3Fu%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F10%252F23%252Fbasic-shadow-mapping%252F%26amp%3Bt%3DBasic%2520Shadow%2520Mapping%26opener%3Dbm%26amp%3Bei%3DUTF-8%26amp%3Bd%3DShadow%2520mapping%2520works%2520in%2520that%2520it%2520checks%2520if%2520a%2520point%2520is%2520visible%2520from%2520the%2520light%2520or%2520not.%2520If%2520a%2520point%25C2%25A0is%2520visible%2520from%2520the%2520light%2520then%2520it%2527s%2520obviously%2520not%2520in%2520shadow%252C%2520otherwise%2520it%2520is.%2520The%2520basic%2520shadow%2520mapping%2520algorithm%2520can%2520be%2520described%2520as%2520short%2520as%2520this%253A%250D%250A%250D%250A%2509Re';" title="Yahoo! Bookmarks"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/yahoomyweb.png" title="Yahoo! Bookmarks" alt="Yahoo! Bookmarks" class="sociable-hovers" /></a></li>
</ul>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.gamerendering.com/2008/10/23/basic-shadow-mapping/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Gaussian Blur Filter Shader</title>
		<link>http://www.gamerendering.com/2008/10/11/gaussian-blur-filter-shader/</link>
		<comments>http://www.gamerendering.com/2008/10/11/gaussian-blur-filter-shader/#comments</comments>
		<pubDate>Sat, 11 Oct 2008 19:01:18 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Image Enhancements]]></category>
		<category><![CDATA[Blur]]></category>
		<category><![CDATA[Blur Filter]]></category>
		<category><![CDATA[Bluring Scene]]></category>
		<category><![CDATA[DirectX]]></category>
		<category><![CDATA[Filtering]]></category>
		<category><![CDATA[Fragment Shader]]></category>
		<category><![CDATA[Game]]></category>
		<category><![CDATA[Gaussian Blur]]></category>
		<category><![CDATA[GLSL]]></category>
		<category><![CDATA[Render-to-Texture]]></category>
		<category><![CDATA[Shaders]]></category>
		<category><![CDATA[Textures]]></category>
		<category><![CDATA[Vertex Shader]]></category>

		<guid isPermaLink="false">http://www.gamerendering.com/?p=295</guid>
		<description><![CDATA[There are different ways to perform blur and this is one of the most common way to do it in a shader. It&#8217;s a two step method with first a horizontal blur and then a vertical blur. By splitting the work in two directions (two passes) you can save a lot of computation.
The method can [...]]]></description>
			<content:encoded><![CDATA[<p>There are different ways to perform blur and this is one of the most common way to do it in a shader. It&#8217;s a two step method with first a horizontal blur and then a vertical blur. By splitting the work in two directions (two passes) you can save a lot of computation.</p>
<p>The method can be divided in the following parts:</p>
<ol>
<li>Render the scene you want to blur to a texture (could be downsampled)</li>
<li>Render a screen aligned quad with the horizontal blur shader to a texture</li>
<li>Render a screen aligned quad with the vertical blur shader to the screen or texture depending on what you want to use it for</li>
</ol>
<p>The following image shows how the blur works when splitted up in two directions.</p>
<div class="mceTemp"><a href="http://www.gamerendering.com/wp-content/uploads/image13-bluring.jpg"><img class="size-medium wp-image-298" title="Separable blur filter" src="http://www.gamerendering.com/wp-content/uploads/image13-bluring-390x400.jpg" alt="Separable blur filter" width="390" height="400" /></a></div>
<p>Here&#8217;s the horizontal blur shader.</p>
<p>Vertex Shader (GLSL) . This shader screen align a quad with width 1. Any method to render a screen aligned quad will work. So you&#8217;re free to use other shaders.</p>

<div class="wp_codebox"><table width="100%" ><tr id="p29511"><td class="code" id="p295code11"><pre class="c" style="font-family:monospace;">varying vec2 vTexCoord<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// remember that you should draw a screen aligned quad</span>
<span style="color: #993333;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #993333;">void</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
   gl_Position <span style="color: #339933;">=</span> ftransform<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;;</span>
  
   <span style="color: #666666; font-style: italic;">// Clean up inaccuracies</span>
   vec2 Pos<span style="color: #339933;">;</span>
   Pos <span style="color: #339933;">=</span> sign<span style="color: #009900;">&#40;</span>gl_Vertex.<span style="color: #202020;">xy</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
   gl_Position <span style="color: #339933;">=</span> vec4<span style="color: #009900;">&#40;</span>Pos<span style="color: #339933;">,</span> <span style="color:#800080;">0.0</span><span style="color: #339933;">,</span> <span style="color:#800080;">1.0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #666666; font-style: italic;">// Image-space</span>
   vTexCoord <span style="color: #339933;">=</span> Pos <span style="color: #339933;">*</span> <span style="color:#800080;">0.5</span> <span style="color: #339933;">+</span> <span style="color:#800080;">0.5</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Fragment Shader (GLSL) </p>

<div class="wp_codebox"><table width="100%" ><tr id="p29512"><td class="code" id="p295code12"><pre class="c" style="font-family:monospace;">uniform sampler2D RTScene<span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// the texture with the scene you want to blur</span>
varying vec2 vTexCoord<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #993333;">const</span> <span style="color: #993333;">float</span> blurSize <span style="color: #339933;">=</span> <span style="color:#800080;">1.0</span><span style="color: #339933;">/</span><span style="color:#800080;">512.0</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// I've chosen this size because this will result in that every step will be one pixel wide if the RTScene texture is of size 512x512</span>
&nbsp;
<span style="color: #993333;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #993333;">void</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
   vec4 sum <span style="color: #339933;">=</span> vec4<span style="color: #009900;">&#40;</span><span style="color:#800080;">0.0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
   <span style="color: #666666; font-style: italic;">// blur in y (vertical)</span>
   <span style="color: #666666; font-style: italic;">// take nine samples, with the distance blurSize between them</span>
   sum <span style="color: #339933;">+=</span> texture2D<span style="color: #009900;">&#40;</span>RTScene<span style="color: #339933;">,</span> vec2<span style="color: #009900;">&#40;</span>vTexCoord.<span style="color: #202020;">x</span> <span style="color: #339933;">-</span> <span style="color:#800080;">4.0</span><span style="color: #339933;">*</span>blurSize<span style="color: #339933;">,</span> vTexCoord.<span style="color: #202020;">y</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color:#800080;">0.05</span><span style="color: #339933;">;</span>
   sum <span style="color: #339933;">+=</span> texture2D<span style="color: #009900;">&#40;</span>RTScene<span style="color: #339933;">,</span> vec2<span style="color: #009900;">&#40;</span>vTexCoord.<span style="color: #202020;">x</span> <span style="color: #339933;">-</span> <span style="color:#800080;">3.0</span><span style="color: #339933;">*</span>blurSize<span style="color: #339933;">,</span> vTexCoord.<span style="color: #202020;">y</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color:#800080;">0.09</span><span style="color: #339933;">;</span>
   sum <span style="color: #339933;">+=</span> texture2D<span style="color: #009900;">&#40;</span>RTScene<span style="color: #339933;">,</span> vec2<span style="color: #009900;">&#40;</span>vTexCoord.<span style="color: #202020;">x</span> <span style="color: #339933;">-</span> <span style="color:#800080;">2.0</span><span style="color: #339933;">*</span>blurSize<span style="color: #339933;">,</span> vTexCoord.<span style="color: #202020;">y</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color:#800080;">0.12</span><span style="color: #339933;">;</span>
   sum <span style="color: #339933;">+=</span> texture2D<span style="color: #009900;">&#40;</span>RTScene<span style="color: #339933;">,</span> vec2<span style="color: #009900;">&#40;</span>vTexCoord.<span style="color: #202020;">x</span> <span style="color: #339933;">-</span> blurSize<span style="color: #339933;">,</span> vTexCoord.<span style="color: #202020;">y</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color:#800080;">0.15</span><span style="color: #339933;">;</span>
   sum <span style="color: #339933;">+=</span> texture2D<span style="color: #009900;">&#40;</span>RTScene<span style="color: #339933;">,</span> vec2<span style="color: #009900;">&#40;</span>vTexCoord.<span style="color: #202020;">x</span><span style="color: #339933;">,</span> vTexCoord.<span style="color: #202020;">y</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color:#800080;">0.16</span><span style="color: #339933;">;</span>
   sum <span style="color: #339933;">+=</span> texture2D<span style="color: #009900;">&#40;</span>RTScene<span style="color: #339933;">,</span> vec2<span style="color: #009900;">&#40;</span>vTexCoord.<span style="color: #202020;">x</span> <span style="color: #339933;">+</span> blurSize<span style="color: #339933;">,</span> vTexCoord.<span style="color: #202020;">y</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color:#800080;">0.15</span><span style="color: #339933;">;</span>
   sum <span style="color: #339933;">+=</span> texture2D<span style="color: #009900;">&#40;</span>RTScene<span style="color: #339933;">,</span> vec2<span style="color: #009900;">&#40;</span>vTexCoord.<span style="color: #202020;">x</span> <span style="color: #339933;">+</span> <span style="color:#800080;">2.0</span><span style="color: #339933;">*</span>blurSize<span style="color: #339933;">,</span> vTexCoord.<span style="color: #202020;">y</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color:#800080;">0.12</span><span style="color: #339933;">;</span>
   sum <span style="color: #339933;">+=</span> texture2D<span style="color: #009900;">&#40;</span>RTScene<span style="color: #339933;">,</span> vec2<span style="color: #009900;">&#40;</span>vTexCoord.<span style="color: #202020;">x</span> <span style="color: #339933;">+</span> <span style="color:#800080;">3.0</span><span style="color: #339933;">*</span>blurSize<span style="color: #339933;">,</span> vTexCoord.<span style="color: #202020;">y</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color:#800080;">0.09</span><span style="color: #339933;">;</span>
   sum <span style="color: #339933;">+=</span> texture2D<span style="color: #009900;">&#40;</span>RTScene<span style="color: #339933;">,</span> vec2<span style="color: #009900;">&#40;</span>vTexCoord.<span style="color: #202020;">x</span> <span style="color: #339933;">+</span> <span style="color:#800080;">4.0</span><span style="color: #339933;">*</span>blurSize<span style="color: #339933;">,</span> vTexCoord.<span style="color: #202020;">y</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color:#800080;">0.05</span><span style="color: #339933;">;</span>
&nbsp;
   gl_FragColor <span style="color: #339933;">=</span> sum<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>And here&#8217;s the vertical blur shader.</p>
<p>Vertex Shader (GLSL) (the same as for the blur in horizontal direction)</p>

<div class="wp_codebox"><table width="100%" ><tr id="p29513"><td class="code" id="p295code13"><pre class="c" style="font-family:monospace;">varying vec2 vTexCoord<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// remember that you should draw a screen aligned quad</span>
<span style="color: #993333;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #993333;">void</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
   gl_Position <span style="color: #339933;">=</span> ftransform<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;;</span>
  
   <span style="color: #666666; font-style: italic;">// Clean up inaccuracies</span>
   vec2 Pos<span style="color: #339933;">;</span>
   Pos <span style="color: #339933;">=</span> sign<span style="color: #009900;">&#40;</span>gl_Vertex.<span style="color: #202020;">xy</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
   gl_Position <span style="color: #339933;">=</span> vec4<span style="color: #009900;">&#40;</span>Pos<span style="color: #339933;">,</span> <span style="color:#800080;">0.0</span><span style="color: #339933;">,</span> <span style="color:#800080;">1.0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #666666; font-style: italic;">// Image-space</span>
   vTexCoord <span style="color: #339933;">=</span> Pos <span style="color: #339933;">*</span> <span style="color:#800080;">0.5</span> <span style="color: #339933;">+</span> <span style="color:#800080;">0.5</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Fragment Shader (GLSL) </p>

<div class="wp_codebox"><table width="100%" ><tr id="p29514"><td class="code" id="p295code14"><pre class="c" style="font-family:monospace;">uniform sampler2D RTBlurH<span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// this should hold the texture rendered by the horizontal blur pass</span>
varying vec2 vTexCoord<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #993333;">const</span> <span style="color: #993333;">float</span> blurSize <span style="color: #339933;">=</span> <span style="color:#800080;">1.0</span><span style="color: #339933;">/</span><span style="color:#800080;">512.0</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #993333;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #993333;">void</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
   vec4 sum <span style="color: #339933;">=</span> vec4<span style="color: #009900;">&#40;</span><span style="color:#800080;">0.0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
   <span style="color: #666666; font-style: italic;">// blur in y (vertical)</span>
   <span style="color: #666666; font-style: italic;">// take nine samples, with the distance blurSize between them</span>
   sum <span style="color: #339933;">+=</span> texture2D<span style="color: #009900;">&#40;</span>RTBlurH<span style="color: #339933;">,</span> vec2<span style="color: #009900;">&#40;</span>vTexCoord.<span style="color: #202020;">x</span><span style="color: #339933;">,</span> vTexCoord.<span style="color: #202020;">y</span> <span style="color: #339933;">-</span> <span style="color:#800080;">4.0</span><span style="color: #339933;">*</span>blurSize<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color:#800080;">0.05</span><span style="color: #339933;">;</span>
   sum <span style="color: #339933;">+=</span> texture2D<span style="color: #009900;">&#40;</span>RTBlurH<span style="color: #339933;">,</span> vec2<span style="color: #009900;">&#40;</span>vTexCoord.<span style="color: #202020;">x</span><span style="color: #339933;">,</span> vTexCoord.<span style="color: #202020;">y</span> <span style="color: #339933;">-</span> <span style="color:#800080;">3.0</span><span style="color: #339933;">*</span>blurSize<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color:#800080;">0.09</span><span style="color: #339933;">;</span>
   sum <span style="color: #339933;">+=</span> texture2D<span style="color: #009900;">&#40;</span>RTBlurH<span style="color: #339933;">,</span> vec2<span style="color: #009900;">&#40;</span>vTexCoord.<span style="color: #202020;">x</span><span style="color: #339933;">,</span> vTexCoord.<span style="color: #202020;">y</span> <span style="color: #339933;">-</span> <span style="color:#800080;">2.0</span><span style="color: #339933;">*</span>blurSize<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color:#800080;">0.12</span><span style="color: #339933;">;</span>
   sum <span style="color: #339933;">+=</span> texture2D<span style="color: #009900;">&#40;</span>RTBlurH<span style="color: #339933;">,</span> vec2<span style="color: #009900;">&#40;</span>vTexCoord.<span style="color: #202020;">x</span><span style="color: #339933;">,</span> vTexCoord.<span style="color: #202020;">y</span> <span style="color: #339933;">-</span> blurSize<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color:#800080;">0.15</span><span style="color: #339933;">;</span>
   sum <span style="color: #339933;">+=</span> texture2D<span style="color: #009900;">&#40;</span>RTBlurH<span style="color: #339933;">,</span> vec2<span style="color: #009900;">&#40;</span>vTexCoord.<span style="color: #202020;">x</span><span style="color: #339933;">,</span> vTexCoord.<span style="color: #202020;">y</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color:#800080;">0.16</span><span style="color: #339933;">;</span>
   sum <span style="color: #339933;">+=</span> texture2D<span style="color: #009900;">&#40;</span>RTBlurH<span style="color: #339933;">,</span> vec2<span style="color: #009900;">&#40;</span>vTexCoord.<span style="color: #202020;">x</span><span style="color: #339933;">,</span> vTexCoord.<span style="color: #202020;">y</span> <span style="color: #339933;">+</span> blurSize<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color:#800080;">0.15</span><span style="color: #339933;">;</span>
   sum <span style="color: #339933;">+=</span> texture2D<span style="color: #009900;">&#40;</span>RTBlurH<span style="color: #339933;">,</span> vec2<span style="color: #009900;">&#40;</span>vTexCoord.<span style="color: #202020;">x</span><span style="color: #339933;">,</span> vTexCoord.<span style="color: #202020;">y</span> <span style="color: #339933;">+</span> <span style="color:#800080;">2.0</span><span style="color: #339933;">*</span>blurSize<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color:#800080;">0.12</span><span style="color: #339933;">;</span>
   sum <span style="color: #339933;">+=</span> texture2D<span style="color: #009900;">&#40;</span>RTBlurH<span style="color: #339933;">,</span> vec2<span style="color: #009900;">&#40;</span>vTexCoord.<span style="color: #202020;">x</span><span style="color: #339933;">,</span> vTexCoord.<span style="color: #202020;">y</span> <span style="color: #339933;">+</span> <span style="color:#800080;">3.0</span><span style="color: #339933;">*</span>blurSize<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color:#800080;">0.09</span><span style="color: #339933;">;</span>
   sum <span style="color: #339933;">+=</span> texture2D<span style="color: #009900;">&#40;</span>RTBlurH<span style="color: #339933;">,</span> vec2<span style="color: #009900;">&#40;</span>vTexCoord.<span style="color: #202020;">x</span><span style="color: #339933;">,</span> vTexCoord.<span style="color: #202020;">y</span> <span style="color: #339933;">+</span> <span style="color:#800080;">4.0</span><span style="color: #339933;">*</span>blurSize<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color:#800080;">0.05</span><span style="color: #339933;">;</span>
&nbsp;
   gl_FragColor <span style="color: #339933;">=</span> sum<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>And this is a scene without blur.</p>
<div class="mceTemp"><a href="http://www.gamerendering.com/wp-content/uploads/preblurshader.jpg"><img class="size-medium wp-image-297" title="Scene before bluring" src="http://www.gamerendering.com/wp-content/uploads/preblurshader-395x400.jpg" alt="Scene before bluring" width="357" height="385" /></a></div>
<p>And this is the same scene but with gaussian blur.</p>
<div class="mceTemp"><a href="http://www.gamerendering.com/wp-content/uploads/postblurshader.jpg"><img class="size-medium wp-image-296" title="Blured Scene" src="http://www.gamerendering.com/wp-content/uploads/postblurshader.jpg" alt="Blured Scene" width="356" height="334" /></a></div>
<p>You can tweak the blur radius to change the size of the blur and change the number of samples in each direction.</p>
<p>Cost for separable blur shader : 9+9 = 18 (number of texture samples)<br />
Cost for shader if blured in one pass: 9*9 = 81 (number of texture samples)<br />
So splitting up in two directions saves a lot.</p>
<p>The gaussian weights are calculated accordingly to the gaussian function with standard deviation of 2.7. These calculations were done in the excel document found [2].</p>
<p>Here&#8217;s a description of blur shaders and other image processing shaders in DirectX:<br />
[1] <a href="http://ati.amd.com/developer/shaderx/ShaderX2_AdvancedImageProcessing.pdf">http://ati.amd.com/developer/shaderx/ShaderX2_AdvancedImageProcessing.pdf</a></p>
<p>More info about calculating weights for separable gaussian blur:<br />
[2] <a href="http://theinstructionlimit.com/?p=40">http://theinstructionlimit.com/?p=40</a></p>

<div class="sociable">
<div class="sociable_tagline">
<strong>Please share:</strong>
</div>
<ul>
	<li class="sociablefirst"><a rel="nofollow" id="print" href="javascript:window.location='http%3A%2F%2Fwww.printfriendly.com%2Fprint%3Furl%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F10%252F11%252Fgaussian-blur-filter-shader%252F%26amp%3Bpartner%3Dsociable';" title="Print this article!"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/printfriendly.png" title="Print this article!" alt="Print this article!" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="digg" href="javascript:window.location='http%3A%2F%2Fdigg.com%2Fsubmit%3Fphase%3D2%26amp%3Burl%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F10%252F11%252Fgaussian-blur-filter-shader%252F%26amp%3Btitle%3DGaussian%2520Blur%2520Filter%2520Shader%2520%26amp%3Bbodytext%3DThere%2520are%2520different%2520ways%2520to%2520perform%2520blur%2520and%2520this%2520is%2520one%2520of%2520the%2520most%2520common%2520way%2520to%2520do%2520it%2520in%2520a%2520shader.%2520It%2527s%2520a%2520two%2520step%2520method%2520with%2520first%2520a%2520horizontal%2520blur%2520and%2520then%2520a%2520vertical%2520blur.%2520By%2520splitting%2520the%2520work%2520in%2520two%2520directions%2520%2528two%2520passes%2529%2520you%2520can%2520save%2520a%2520lo';" title="Digg"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="sphinn" href="javascript:window.location='http%3A%2F%2Fsphinn.com%2Findex.php%3Fc%3Dpost%26m%3Dsubmit%26link%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F10%252F11%252Fgaussian-blur-filter-shader%252F';" title="Sphinn"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/sphinn.png" title="Sphinn" alt="Sphinn" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="del.icio.us" href="javascript:window.location='http%3A%2F%2Fdelicious.com%2Fpost%3Furl%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F10%252F11%252Fgaussian-blur-filter-shader%252F%26amp%3Btitle%3DGaussian%2520Blur%2520Filter%2520Shader%2520%26amp%3Bnotes%3DThere%2520are%2520different%2520ways%2520to%2520perform%2520blur%2520and%2520this%2520is%2520one%2520of%2520the%2520most%2520common%2520way%2520to%2520do%2520it%2520in%2520a%2520shader.%2520It%2527s%2520a%2520two%2520step%2520method%2520with%2520first%2520a%2520horizontal%2520blur%2520and%2520then%2520a%2520vertical%2520blur.%2520By%2520splitting%2520the%2520work%2520in%2520two%2520directions%2520%2528two%2520passes%2529%2520you%2520can%2520save%2520a%2520lo';" title="del.icio.us"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="facebook" href="javascript:window.location='http%3A%2F%2Fwww.facebook.com%2Fshare.php%3Fu%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F10%252F11%252Fgaussian-blur-filter-shader%252F%26amp%3Bt%3DGaussian%2520Blur%2520Filter%2520Shader%2520';" title="Facebook"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="mixx" href="javascript:window.location='http%3A%2F%2Fwww.mixx.com%2Fsubmit%3Fpage_url%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F10%252F11%252Fgaussian-blur-filter-shader%252F%26amp%3Btitle%3DGaussian%2520Blur%2520Filter%2520Shader%2520';" title="Mixx"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/mixx.png" title="Mixx" alt="Mixx" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="google" href="javascript:window.location='http%3A%2F%2Fwww.google.com%2Fbookmarks%2Fmark%3Fop%3Dedit%26amp%3Bbkmk%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F10%252F11%252Fgaussian-blur-filter-shader%252F%26amp%3Btitle%3DGaussian%2520Blur%2520Filter%2520Shader%2520%26amp%3Bannotation%3DThere%2520are%2520different%2520ways%2520to%2520perform%2520blur%2520and%2520this%2520is%2520one%2520of%2520the%2520most%2520common%2520way%2520to%2520do%2520it%2520in%2520a%2520shader.%2520It%2527s%2520a%2520two%2520step%2520method%2520with%2520first%2520a%2520horizontal%2520blur%2520and%2520then%2520a%2520vertical%2520blur.%2520By%2520splitting%2520the%2520work%2520in%2520two%2520directions%2520%2528two%2520passes%2529%2520you%2520can%2520save%2520a%2520lo';" title="Google Bookmarks"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/googlebookmark.png" title="Google Bookmarks" alt="Google Bookmarks" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="current" href="javascript:window.location='http%3A%2F%2Fcurrent.com%2Fclipper.htm%3Furl%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F10%252F11%252Fgaussian-blur-filter-shader%252F%26amp%3Btitle%3DGaussian%2520Blur%2520Filter%2520Shader%2520';" title="Current"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/current.png" title="Current" alt="Current" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="linkedin" href="javascript:window.location='http%3A%2F%2Fwww.linkedin.com%2FshareArticle%3Fmini%3Dtrue%26amp%3Burl%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F10%252F11%252Fgaussian-blur-filter-shader%252F%26amp%3Btitle%3DGaussian%2520Blur%2520Filter%2520Shader%2520%26amp%3Bsource%3DGame%2BRendering%2B%26amp%3Bsummary%3DThere%2520are%2520different%2520ways%2520to%2520perform%2520blur%2520and%2520this%2520is%2520one%2520of%2520the%2520most%2520common%2520way%2520to%2520do%2520it%2520in%2520a%2520shader.%2520It%2527s%2520a%2520two%2520step%2520method%2520with%2520first%2520a%2520horizontal%2520blur%2520and%2520then%2520a%2520vertical%2520blur.%2520By%2520splitting%2520the%2520work%2520in%2520two%2520directions%2520%2528two%2520passes%2529%2520you%2520can%2520save%2520a%2520lo';" title="LinkedIn"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/linkedin.png" title="LinkedIn" alt="LinkedIn" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="live" href="javascript:window.location='https%3A%2F%2Ffavorites.live.com%2Fquickadd.aspx%3Fmarklet%3D1%26amp%3Burl%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F10%252F11%252Fgaussian-blur-filter-shader%252F%26amp%3Btitle%3DGaussian%2520Blur%2520Filter%2520Shader%2520';" title="Live"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/live.png" title="Live" alt="Live" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="myspace" href="javascript:window.location='http%3A%2F%2Fwww.myspace.com%2FModules%2FPostTo%2FPages%2F%3Fu%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F10%252F11%252Fgaussian-blur-filter-shader%252F%26amp%3Bt%3DGaussian%2520Blur%2520Filter%2520Shader%2520';" title="MySpace"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/myspace.png" title="MySpace" alt="MySpace" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="netvibes" href="javascript:window.location='http%3A%2F%2Fwww.netvibes.com%2Fshare%3Ftitle%3DGaussian%2520Blur%2520Filter%2520Shader%2520%26amp%3Burl%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F10%252F11%252Fgaussian-blur-filter-shader%252F';" title="Netvibes"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/netvibes.png" title="Netvibes" alt="Netvibes" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="stumbleupon" href="javascript:window.location='http%3A%2F%2Fwww.stumbleupon.com%2Fsubmit%3Furl%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F10%252F11%252Fgaussian-blur-filter-shader%252F%26amp%3Btitle%3DGaussian%2520Blur%2520Filter%2520Shader%2520';" title="StumbleUpon"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/stumbleupon.png" title="StumbleUpon" alt="StumbleUpon" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="twitter" href="javascript:window.location='http%3A%2F%2Ftwitter.com%2Fhome%3Fstatus%3DGaussian%2520Blur%2520Filter%2520Shader%2520%2520-%2520http%253A%252F%252Fwww.gamerendering.com%252F2008%252F10%252F11%252Fgaussian-blur-filter-shader%252F';" title="Twitter"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/twitter.png" title="Twitter" alt="Twitter" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="reddit" href="javascript:window.location='http%3A%2F%2Freddit.com%2Fsubmit%3Furl%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F10%252F11%252Fgaussian-blur-filter-shader%252F%26amp%3Btitle%3DGaussian%2520Blur%2520Filter%2520Shader%2520';" title="Reddit"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/reddit.png" title="Reddit" alt="Reddit" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="technorati" href="javascript:window.location='http%3A%2F%2Ftechnorati.com%2Ffaves%3Fadd%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F10%252F11%252Fgaussian-blur-filter-shader%252F';" title="Technorati"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/technorati.png" title="Technorati" alt="Technorati" class="sociable-hovers" /></a></li>
	<li class="sociablelast"><a rel="nofollow" id="yahoo! bookmarks" href="javascript:window.location='http%3A%2F%2Fbookmarks.yahoo.com%2Ftoolbar%2Fsavebm%3Fu%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F10%252F11%252Fgaussian-blur-filter-shader%252F%26amp%3Bt%3DGaussian%2520Blur%2520Filter%2520Shader%2520%26opener%3Dbm%26amp%3Bei%3DUTF-8%26amp%3Bd%3DThere%2520are%2520different%2520ways%2520to%2520perform%2520blur%2520and%2520this%2520is%2520one%2520of%2520the%2520most%2520common%2520way%2520to%2520do%2520it%2520in%2520a%2520shader.%2520It%2527s%2520a%2520two%2520step%2520method%2520with%2520first%2520a%2520horizontal%2520blur%2520and%2520then%2520a%2520vertical%2520blur.%2520By%2520splitting%2520the%2520work%2520in%2520two%2520directions%2520%2528two%2520passes%2529%2520you%2520can%2520save%2520a%2520lo';" title="Yahoo! Bookmarks"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/yahoomyweb.png" title="Yahoo! Bookmarks" alt="Yahoo! Bookmarks" class="sociable-hovers" /></a></li>
</ul>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.gamerendering.com/2008/10/11/gaussian-blur-filter-shader/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>
