<?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>Encelo's Blog &#187; GLSL</title>
	<atom:link href="http://encelo.netsons.org/blog/tag/glsl/feed/" rel="self" type="application/rss+xml" />
	<link>http://encelo.netsons.org/blog</link>
	<description>When I grow up I want to be a game developer</description>
	<lastBuildDate>Mon, 02 Nov 2009 23:49:21 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Gimme some (real-time) water!</title>
		<link>http://encelo.netsons.org/blog/2009/11/02/gimme-some-real-time-water/</link>
		<comments>http://encelo.netsons.org/blog/2009/11/02/gimme-some-real-time-water/#comments</comments>
		<pubDate>Mon, 02 Nov 2009 22:20:44 +0000</pubDate>
		<dc:creator>encelo</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Real-time Graphics]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[Fresnel]]></category>
		<category><![CDATA[GLSL]]></category>
		<category><![CDATA[OpenGL]]></category>
		<category><![CDATA[reflection]]></category>
		<category><![CDATA[refraction]]></category>
		<category><![CDATA[shaders]]></category>

		<guid isPermaLink="false">http://encelo.netsons.org/blog/?p=411</guid>
		<description><![CDATA[Computer generated water has always interested me, since the days of POVRay on Amiga I was trying to simulate it in some way.
Some days ago, while studying another technique, I put everything aside because in that particular moment I felt the urge to implement a water shader.  

I began looking for existing implementations and [...]]]></description>
			<content:encoded><![CDATA[<p>Computer generated water has always interested me, since the days of POVRay on Amiga I was trying to simulate it in some way.<br />
Some days ago, while studying another technique, I put everything aside because in that particular moment I felt the urge to implement a water shader. <img src='http://encelo.netsons.org/blog/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> </p>
<p><img src="http://encelo.netsons.org/blog/wp-content/uploads/2009/11/water_crop.png" alt="water_crop" title="water_crop" width="400" height="175" class="aligncenter size-full wp-image-415" /></p>
<p>I began looking for existing implementations and I found <a href="http://www.riemers.net/eng/Tutorials/XNA/Csharp/Series4/The_water_technique.php">Reimer&#8217;s XNA tutorial</a>, a simple approach which I think could be optimized, but that already provides a nice looking water.</p>
<p>The technique is composed of four passes:</p>
<ul>
<li>Rendering the reflection map</li>
<li>Rendering the refraction map</li>
<li>Rendering the scene</li>
<li>Rendering the water plane linearly interpolating the two maps with a Fresnel term</li>
</ul>
<p>One of the drawbacks is represented by the fact that the whole scene is rendered three times during the first three passes, I&#8217;m sure that this procedure could be optimized, but I was lazy enough to cease any further test. <img src='http://encelo.netsons.org/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /><br />
Moreover having every pass clearly separated helps with debugging and makes the application capable of displaying them one at a time.</p>
<p>The scene is rendered with parallax mapping enabled, and that is more evident than ever thanks to the new bricks textures, but with an altered shader that also performs user plane clipping, decisive for the first two passes.<br />
Talking about the Fresnel term I have implemented a naive (nothing more than a <tt>dot(V, N)</tt>) and a better approximation based on the Nvidia&#8217;s  <a href="http://developer.nvidia.com/object/fresnel_wp.html">Fresnel reflection</a> paper.<br />
In the source you will find both but only the first one is actually used, it works better in the scene used in this demo.<br />
You can easily see that waves are fake, the water is composed of just two triangles, the ripple animation is generated by the fragment shader altering texture coordinates based on a normal map and using a time variable.</p>
<p>Of course you can have a look at videos on YouTube (<a href="http://www.youtube.com/watch?v=oBZhS2btj2U<br />
">GLSL_water</a>, <a href=http://www.youtube.com/watch?v=nmc8WQhgkNM">GLSL_water_HD</a>) or Vimeo (<a href="http://vimeo.com/7400766<br />
">GLSL_water</a>, <a href="http://vimeo.com/7401000 ">GLSL_water_HD</a>) and download the <a href="http://encelo.netsons.org/_download/glsl_water.tar.gz">sources</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://encelo.netsons.org/blog/2009/11/02/gimme-some-real-time-water/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>High Dynamic Range galore</title>
		<link>http://encelo.netsons.org/blog/2009/10/14/high-dynamic-range-galore/</link>
		<comments>http://encelo.netsons.org/blog/2009/10/14/high-dynamic-range-galore/#comments</comments>
		<pubDate>Wed, 14 Oct 2009 19:42:50 +0000</pubDate>
		<dc:creator>encelo</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Real-time Graphics]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[GLSL]]></category>
		<category><![CDATA[HDR]]></category>
		<category><![CDATA[High Dynamic Range]]></category>
		<category><![CDATA[OpenGL]]></category>
		<category><![CDATA[post-processing]]></category>
		<category><![CDATA[shaders]]></category>
		<category><![CDATA[snippets]]></category>

		<guid isPermaLink="false">http://encelo.netsons.org/blog/?p=378</guid>
		<description><![CDATA[In January, during my internship activity, I was researching in the field of HDR imaging, today I had the time, at last, to polish a bit and release the two demos I made at the time.
They both load an RGBE image (the two you see here are courtesy of the Paul Devebec&#8217;s Light Probe Image [...]]]></description>
			<content:encoded><![CDATA[<p>In January, during my internship activity, I was researching in the field of HDR imaging, today I had the time, at last, to polish a bit and release the two demos I made at the time.</p>
<p>They both load an <a href="http://en.wikipedia.org/wiki/RGBE_image_format">RGBE</a> image (the two you see here are courtesy of the Paul Devebec&#8217;s <a href="http://www.debevec.org/probes/">Light Probe Image Gallery</a>) through the <a href="http://www.graphics.cornell.edu/online/formats/rgbe/">library</a> of Bruce Walter.</p>
<div id="attachment_382" class="wp-caption aligncenter" style="width: 310px"><a href="http://encelo.netsons.org/blog/wp-content/uploads/2009/10/hdr_strip1.png"><img src="http://encelo.netsons.org/blog/wp-content/uploads/2009/10/hdr_strip1-300x100.png" alt="Light probe at different exposure levels (hdr_load1)" title="hdr_strip1" width="300" height="100" class="size-medium wp-image-382" /></a><p class="wp-caption-text">Light probe at different exposure levels (hdr_load1)</p></div>
<p>The first demo implements the technique described in the article <a href="http://www.gamedev.net/columns/hardcore/hdrrendering/">High Dynamic Range Rendering</a> published on GameDev.net and is based on five passes and four FBOs:</p>
<ol>
<li>Rendering of the floating-point texture in an FBO</li>
<li>Down-sampling in a 1/4 FBO and high-pass filter</li>
<li>Gaussian filter along the X axis</li>
<li>Gaussian filter along the Y axis</li>
<li>Tone-mapping and composition</li>
</ol>
<p>The algorithm is very simple, it first renders the original scene then it extracts bright parts at the second pass, which merely discards fragments which are below a specified threshold:</p>
<pre class="brush: cpp;">
// excrpt from hipass.frag
if (colorMap.r &gt; 1.0 || colorMap.g &gt; 1.0 || colorMap.b &gt; 1.0)
	gl_FragColor = colorMap;
else
	gl_FragColor = vec4(0.0);
</pre>
<p>While the third and fourth passes blurs the bright mask, the last one mixes it with the first FBO and sets exposure and gamma to achieve a bloom effect.</p>
<pre class="brush: cpp;">
// excerpt from tonemap.frag
gl_FragColor = colorMap + Factor * (bloomMap - colorMap);
gl_FragColor *= Exposure;
gl_FragColor = pow(gl_FragColor, vec4(Gamma));
</pre>
<div id="attachment_382" class="wp-caption aligncenter" style="width: 310px"><a href="http://encelo.netsons.org/blog/wp-content/uploads/2009/10/hdr_strip2.png"><img src="http://encelo.netsons.org/blog/wp-content/uploads/2009/10/hdr_strip2-300x100.png" alt="Light probe at different exposure levels (hdr_load2)" title="hdr_strip2" width="300" height="100" class="size-medium wp-image-382" /></a><p class="wp-caption-text">Light probe at different exposure levels (hdr_load2)</p></div>
<p>The second demo implements the technique described in the article <a href="http://www.ziggyware.com/readarticle.php?article_id=226">High Dynamic Range Rendering in XNA</a> published on Ziggyware and is based on seven passed and more than five FBOs:</p>
<ol>
<li>Rendering of the floating-point texture in an FBO</li>
<li>Calculating maximum and mean luminance for the entire scene</li>
<li>Bright-pass filter</li>
<li>Gaussian filter along the X axis</li>
<li>Gaussian filter along the Y axis</li>
<li>Tone-mapping</li>
<li>Bloom layer addition</li>
</ol>
<p>This approach is far more complex than the previous one and is based on converting the scene to its <a href="http://en.wikipedia.org/wiki/Luma_%28video%29">luminance</a> (defined as <em>Y = 0.299*R + 0.587*G + 0.114*B</em>) version, the mean and maximum value can be calculated using a particular downsampling shader and working in more passes, at each one rendering on an FBO with a smaller resolution than the previous until the last pass, when you render the luminance of the entire scene on a 1&#215;1 FBO.</p>
<p>As usual you can have a look at YouTube (<a href="http://www.youtube.com/watch?v=D93glosIbbo">GLSL_hdrload1</a>, <a href="http://www.youtube.com/watch?v=BRdkseJs7t0">GLSL_hdrload2</a>) or Vimeo (<a href="http://vimeo.com/7067100">GLSL_hdrload1</a>, <a href="http://vimeo.com/7067374">GLSL_hdrload2</a>) videos and download the <a href="http://encelo.netsons.org/_download/glsl_hdrload.tar.gz">sources</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://encelo.netsons.org/blog/2009/10/14/high-dynamic-range-galore/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Yet another toon shader</title>
		<link>http://encelo.netsons.org/blog/2009/04/26/yet-another-toon-shader/</link>
		<comments>http://encelo.netsons.org/blog/2009/04/26/yet-another-toon-shader/#comments</comments>
		<pubDate>Sun, 26 Apr 2009 20:33:42 +0000</pubDate>
		<dc:creator>encelo</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Real-time Graphics]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[GLSL]]></category>
		<category><![CDATA[OpenGL]]></category>
		<category><![CDATA[post-processing]]></category>
		<category><![CDATA[shaders]]></category>

		<guid isPermaLink="false">http://encelo.netsons.org/blog/?p=241</guid>
		<description><![CDATA[Maybe is true, as I wrote in the README file, that I coded this demo because I felt like the only one who hasn&#8217;t yet implemented a toon shader. 
Actually this is not the only reason, I came with the inspiration when I was presenting the first part of my updated Modern GPUs slides at [...]]]></description>
			<content:encoded><![CDATA[<p>Maybe is true, as I wrote in the README file, that I coded this demo because I felt like the only one who hasn&#8217;t yet implemented a toon shader. <img src='http://encelo.netsons.org/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /><br />
Actually this is not the only reason, I came with the inspiration when I was presenting the first part of my updated <a href="http://encelo.netsons.org/_download/le_moderne_gpu.pdf">Modern GPUs</a> slides at the university, this time the event was organized by some students and advertised with leaflets. <img src='http://encelo.netsons.org/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /><br />
So, for the second part that will be held next Wednesday, I&#8217;m planning to integrate the explanations about the internals of this demo.</p>
<div id="attachment_251" class="wp-caption aligncenter" style="width: 490px"><img src="http://encelo.netsons.org/blog/wp-content/uploads/2009/04/toon_strip.png" alt="From untextured to textured with outlines" title="toon_strip" width="480" height="120" class="size-full wp-image-251" /><p class="wp-caption-text">From untextured to textured with outlines</p></div>
<p>It was easy and fast to have a basic toon shader working, thanks to the Lighthouse 3D <a href="http://encelo.netsons.org/_download/gl3_parallax_dof.tar.gz">tutorial</a>.<br />
This version uses a cascade of <em>if-then-else</em> instead of a more usual 1D texture lookup but, judging from the tests I have run, it&#8217;s not a performance issue, at least on GeForce 8 and newer cards.</p>
<p>For the edge detecting I wanted to exploit the fragment shader capabilities, working in screen space with the <a href="http://en.wikipedia.org/wiki/Sobel_operator">sobel operator</a> and thus being independent from geometric complexity. </p>
<p>The only problem was about *what* to filter.</p>
<ol>
<li>The first test was straight, I filtered the rendered image, a grey version of the textured and lit MrFixit head, but the results were poor: edge detecting outlined toon lighting shades too.</li>
<li>In the second one I decided to filter the depth buffer, I could get rid of colour to grey conversion but, again, the results were not satisfactory: there were no outlines in the model, just a contour all around.<br />
Maybe it could have been corrected with a per-model clip planes tuning, but I gave up.</li>
<li>With the third test I filtered out the unilluminated color texture and the results were better. Unfortunately it relied on the presence of a texture and outlined too much details.</li>
<li>I think the fourth approach, as seen in this demo, is the best one.<br />
I used MRTs to save the eye-space normal buffer during the toon shader pass, then I filtered a grey version of it, outlining the contour plus some other geometric details.
</li>
</ol>
<p>A small note: saving an already grey converted buffer in the toon shader pass speeds up the demo a bit, but storing the normal in a single 8 bits component of the texture causes a loss of precision that leads to some visible artefacts.<br />
Using a floating point texture helps with the precision issue but makes the demo too slow.<br />
Maybe I should try using a single component texture or some kind of RGBA packing algorithm&#8230;</p>
<p>As usual you can have a look at <a href="http://www.youtube.com/watch?v=3k3o-W1UIKk">YouTube</a> or <a href="http://vimeo.com/4340872">Vimeo</a> videos and download the <a href="http://encelo.netsons.org/_download/glsl_toon.tar.gz">sources</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://encelo.netsons.org/blog/2009/04/26/yet-another-toon-shader/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Blurring the parallax</title>
		<link>http://encelo.netsons.org/blog/2008/11/10/blurring-the-parallax/</link>
		<comments>http://encelo.netsons.org/blog/2008/11/10/blurring-the-parallax/#comments</comments>
		<pubDate>Mon, 10 Nov 2008 22:26:14 +0000</pubDate>
		<dc:creator>encelo</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Real-time Graphics]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[GLSL]]></category>
		<category><![CDATA[OpenGL]]></category>
		<category><![CDATA[post-processing]]></category>
		<category><![CDATA[shaders]]></category>

		<guid isPermaLink="false">http://encelo.netsons.org/blog/?p=99</guid>
		<description><![CDATA[Today I have published the first demo making use of my new C++ class library, I designed it to be very easily ported to a strict GL3 profile or to ES 2.0.
As a matter of fact, it doesn&#8217;t make use of fixed pipeline or deprecated functions at all:

No immediate mode, only VBOs
No use of OpenGL [...]]]></description>
			<content:encoded><![CDATA[<p>Today I have published the first demo making use of my new C++ class library, I designed it to be very easily ported to a strict GL3 profile or to ES 2.0.</p>
<div id="attachment_101" class="wp-caption alignnone" style="width: 310px"><a href="http://encelo.netsons.org/blog/wp-content/uploads/2008/11/plain_to_dof_strip.jpg"><img src="http://encelo.netsons.org/blog/wp-content/uploads/2008/11/plain_to_dof_strip-300x75.jpg" alt="From plain rendering to depth of field" title="From plain rendering to depth of field" width="300" height="75" class="size-medium wp-image-101" /></a><p class="wp-caption-text">From plain rendering to depth of field</p></div>
<p>As a matter of fact, it doesn&#8217;t make use of fixed pipeline or deprecated functions at all:</p>
<ul>
<li>No immediate mode, only VBOs</li>
<li>No use of OpenGL matrix stacks, I have my classes handling transformations and passing matrices to shaders directly</li>
<li>No OpenGL lighting, only per-fragment one</li>
<li>No quads or polygons, just triangles</li>
</ul>
<div id="attachment_116" class="wp-caption alignnone" style="width: 310px"><a href="http://encelo.netsons.org/blog/wp-content/uploads/2008/11/normal_vs_parallax.jpg"><img src="http://encelo.netsons.org/blog/wp-content/uploads/2008/11/normal_vs_parallax-300x75.jpg" alt="Normal versus parallax mapping" title="Normal vs parallax" width="300" height="75" class="size-medium wp-image-116" /></a><p class="wp-caption-text">Normal versus parallax mapping</p></div>
<p>I couldn&#8217;t release something only to show changes &#8220;under the hood&#8221;, I had to make something cool, so I decided to mix together parallax mapping (that, as you can see in the screenshot, is a lot more pronounced now) and depth of field, with the little addition of Stanford PLY mesh loading. <img src='http://encelo.netsons.org/blog/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> </p>
<p>Mr.Fixit model and maps (the character players protray in <a href="http://www.sauerbraten.org">Sauerbraten</a>) are courtesy of John Siar, thank you John. <img src='http://encelo.netsons.org/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>As usual, you can have a look to Vimeo videos (<a href="http://vimeo.com/2208683">640&#215;480</a>, <a href="http://vimeo.com/2208706">1280&#215;720</a>) and download the <a href="/_download/gl3_parallax_dof.tar.gz">sources</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://encelo.netsons.org/blog/2008/11/10/blurring-the-parallax/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A long presentation&#8230;</title>
		<link>http://encelo.netsons.org/blog/2008/06/10/a-long-presentation/</link>
		<comments>http://encelo.netsons.org/blog/2008/06/10/a-long-presentation/#comments</comments>
		<pubDate>Tue, 10 Jun 2008 13:39:45 +0000</pubDate>
		<dc:creator>encelo</dc:creator>
				<category><![CDATA[Rants]]></category>
		<category><![CDATA[GLSL]]></category>
		<category><![CDATA[GPU]]></category>
		<category><![CDATA[shaders]]></category>

		<guid isPermaLink="false">http://encelo.netsons.org/blog/?p=64</guid>
		<description><![CDATA[The professor of the computer graphics course at my university was continuosly annoyed by my protests and comments during her lectures..

I&#8217;m sorry but I just couldn&#8217;t stand some claims like: &#8220;Phong shading is never used in interactive applications because of it being computationally too heavy&#8221;&#8230; 
Fortunately she gave me the opportunity to give everyone a [...]]]></description>
			<content:encoded><![CDATA[<p>The professor of the computer graphics course at my university was continuosly annoyed by my protests and comments during her lectures..</p>
<p><img src="/blog/wp-content/uploads/2008/07/latex_logo.png" title="LaTeX_logo" width="300" height="129" /></p>
<p>I&#8217;m sorry but I just couldn&#8217;t stand some claims like: &#8220;Phong shading is never used in interactive applications because of it being computationally too heavy&#8221;&#8230; <img src='http://encelo.netsons.org/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /><br />
Fortunately she gave me the opportunity to give everyone a small technological update. <img src='http://encelo.netsons.org/blog/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /><br />
After about a month, my presentation was born.<br />
Made entirely with <a href="http://latex-beamer.sourceforge.net/">LaTeX Beamer</a>, <a href="http://www.vim.org/">VIM</a>, <a href="http://www.gnome.org/projects/dia/">Dia</a> and <a href="http://www.gimp.org/">GIMP</a>, it deals about what modern GPU are capable of, showing some GPGPU applications, along with traditional ones (videogames <img src='http://encelo.netsons.org/blog/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' />  ), and some shader examples together with commented code.</p>
<p>I discussed it yesterday in a couple of hours, I was all shook up at first but then I advanced smooth and plain. <img src='http://encelo.netsons.org/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /><br />
It is in Italian, of course, but I published it anyway: <a href="/_download/le_moderne_gpu.pdf">Le Moderne GPU</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://encelo.netsons.org/blog/2008/06/10/a-long-presentation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Let there be light!</title>
		<link>http://encelo.netsons.org/blog/2008/04/28/let-there-be-light/</link>
		<comments>http://encelo.netsons.org/blog/2008/04/28/let-there-be-light/#comments</comments>
		<pubDate>Mon, 28 Apr 2008 20:24:12 +0000</pubDate>
		<dc:creator>encelo</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Real-time Graphics]]></category>
		<category><![CDATA[deferred]]></category>
		<category><![CDATA[GLSL]]></category>
		<category><![CDATA[OpenGL]]></category>
		<category><![CDATA[PyOpenGL]]></category>
		<category><![CDATA[shaders]]></category>

		<guid isPermaLink="false">http://encelo.netsons.org/blog/?p=62</guid>
		<description><![CDATA[I started exploring deferred shading rendering to display multiple light sources and ended writing a demo featuring eight different lighting techniques and a PyOpenGL class library.  

The whole story is more than a month old, just after releasing the first depth of field demo I began studying deferred shading, but I extended my purpose [...]]]></description>
			<content:encoded><![CDATA[<p>I started exploring deferred shading rendering to display multiple light sources and ended writing a demo featuring eight different lighting techniques and a PyOpenGL class library. <img src='http://encelo.netsons.org/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p><img src="/blog/wp-content/uploads/2008/04/glsl_multilight.png" alt="glsl_multilight" /></p>
<p>The whole story is more than a month old, just after releasing the first depth of field demo I began studying deferred shading, but I extended my purpose to include other lighting methods, like single and multi-pass fixed-pipeline lighting, per-vertex and per-pixel single and multi-pass shader lighting and, of course, deferred one.</p>
<p>While writing the C code, I thought it was going to be fun to also port it to Python, this way I could have also have a look to the &#8220;new&#8221; (ArchLinux adopted it quite late <img src='http://encelo.netsons.org/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  ) ctypes PyOpenGL, aka PyOpenGL 3.</p>
<p>Unfortunately, many little but annoying issues delayed me until today:</p>
<ul>
<li>not setting explicitely <tt>glDepthFunc(GL_LEQUAL)</tt> (or, alternatively, not clearing the depth buffer at each pass) for multi-pass scene rendering made every pass to be discarded excepting the first one.</li>
<li>trying to make a buggy Python <tt>glDrawBuffers()</tt> wrapper work.<br />
Actually I had no luck with this and give up on MRTs support in PyOpenGL.</li>
<li>trying to figure out why VBOs didn&#8217;t work on PyOpenGL, I give up on this too. <img src='http://encelo.netsons.org/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </li>
<li>using a uniform variable to index the <tt>gl_LightSource</tt> structure array, which prevented the shader from running on Shader Model 3.0 cards</li>
<li>exploring all the possibilities that could ever lead to &#8220;the brick room is very dark in fixed-pipeline mode&#8221; issue, only to discover today that this was a mere scaled normals problem.<br />
It was easily solved enabling <tt>GL_RESCALE_NORMAL</tt></li>
</ul>
<p>At last I made it, I have made a multi light demo that includes deferred lighting (although very rough and not optimized at all) and shows coherent lighting in all rendering modes.<br />
The PyOpenGL class library almost works, no MRTs and VBOs, but it is functional enough to sport a complete DoF2  and multilight (without deferred mode, which relies on MRTs, of course) demo conversions.</p>
<p>It&#8217;s not a news anymore that you can view it in action on my <a href="http://www.youtube.com/encelo">YouTube Channel</a>, or in a high definition <a href="http://www.vimeo.com/952114">720p version</a> hosted on my <a href="http://www.vimeo.com/encelo">Vimeo page</a>.</p>
<p>All&#8217;s well that ends well. <img src='http://encelo.netsons.org/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://encelo.netsons.org/blog/2008/04/28/let-there-be-light/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Depth of field reloaded</title>
		<link>http://encelo.netsons.org/blog/2008/04/15/depth-of-field-reloaded/</link>
		<comments>http://encelo.netsons.org/blog/2008/04/15/depth-of-field-reloaded/#comments</comments>
		<pubDate>Tue, 15 Apr 2008 16:28:09 +0000</pubDate>
		<dc:creator>encelo</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Real-time Graphics]]></category>
		<category><![CDATA[GLSL]]></category>
		<category><![CDATA[OpenGL]]></category>
		<category><![CDATA[post-processing]]></category>
		<category><![CDATA[shaders]]></category>
		<category><![CDATA[snippets]]></category>

		<guid isPermaLink="false">http://encelo.netsons.org/blog/?p=60</guid>
		<description><![CDATA[Lately I&#8217;ve been really disappointed by the poor performances of my first depth of field implementation, thus I decided to do something about it&#8230;

The most natural step to do was to give a look to the second Direct3D example from the same paper I used for the first one, as I was sure it would [...]]]></description>
			<content:encoded><![CDATA[<p>Lately I&#8217;ve been really disappointed by the poor performances of my <a href="/blog/2008/03/23/i-love-depth-of-field/">first depth of field implementation</a>, thus I decided to do something about it&#8230;</p>
<p><img src="/blog/wp-content/uploads/2008/04/glsl_dof2.png" alt="glsl_dof2" /></p>
<p>The most natural step to do was to give a look to the second Direct3D example from the <a href="http://ati.amd.com/developer/shaderx/ShaderX2_Real-TimeDepthOfFieldSimulation.pdf">same paper</a> I used for the first one, as I was sure it would have led to more satisfactory results.<br />
I spent the two last nights converting, correcting and fine tuning it, but I was rewarded by the fact that I was right: even if it is a five passes algorithm which is using four different <a href="http://en.wikipedia.org/wiki/Framebuffer_Object">Frame Buffer Objects</a>, it is about 2.5 times faster than my previous implementation!</p>
<p>I think the speed boost depends on the two following:</p>
<ol>
<li>image blurring is achieved by a gaussian filter which is calculated separating the X from the Y axis, it is an approximation of a standard 2D kernel but it also means that the convolution matrix calculation complexity decreases from a quadratic to a linear factor.</li>
<li>this filter operates only on a downsampled (1/4th of the screen resolution actually) FBO</li>
</ol>
<p>Another nice note about this new implementation is that there are only two focal parameters, <em>focus depth</em> and <em>focus range</em>, which really help to setup a correct scene.</p>
<p>Now let&#8217;s review the five passes in detail:</p>
<ol>
<li>Render the scene normally while calculating a blur amount per-vertex, then store the interpolated value per-pixel inside the alpha component of the fragment.<br />
The calculation at the vertex shader is just:</p>
<pre class="brush: cpp;">
Blur = clamp(abs(-PosWV.z - focalDistance) / focalRange, 0.0, 1.0);
</pre>
</li>
<li>Downsample the scene rendered at the previous pass storing it in a smaller FBO</li>
<li>Apply the gaussian filter along the X axis on the downsampled scene and store it in a new FBO</li>
<li>Apply the gaussian filter along the Y axis on the already X blurred scene and store it in a new FBO</li>
<li>Calculate a linear interpolation between the first full resolution FBO and the XY downsampled blurred one<br />
This is performed in the fragment shader as:</p>
<pre class="brush: cpp;">
gl_FragColor = Fullres + Fullres.a * (Blurred - Fullres);
</pre>
</li>
</ol>
<p>Again, you can view it in action on my <a href="http://www.youtube.com/encelo">YouTube Channel</a>, or in a high definition <a href="http://www.vimeo.com/899935">720p version</a> hosted on my <a href="http://www.vimeo.com/encelo">Vimeo page</a>. <img src='http://encelo.netsons.org/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://encelo.netsons.org/blog/2008/04/15/depth-of-field-reloaded/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>I love depth of field</title>
		<link>http://encelo.netsons.org/blog/2008/03/23/i-love-depth-of-field/</link>
		<comments>http://encelo.netsons.org/blog/2008/03/23/i-love-depth-of-field/#comments</comments>
		<pubDate>Sun, 23 Mar 2008 19:33:48 +0000</pubDate>
		<dc:creator>encelo</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Real-time Graphics]]></category>
		<category><![CDATA[GLSL]]></category>
		<category><![CDATA[OpenGL]]></category>
		<category><![CDATA[post-processing]]></category>
		<category><![CDATA[shaders]]></category>

		<guid isPermaLink="false">http://encelo.netsons.org/blog/2008/03/23/i-love-depth-of-field/</guid>
		<description><![CDATA[I consider depth of field as one of the most beautiful post-processing effects of the &#8220;next-gen&#8221; games.
It was natural for me to choose it as the first shader demo to implement after months of inactivity, as a matter of fact GLSL_impgro was really just a testbed for post-processing basic techniques, like Frame Buffer Objects.

I have [...]]]></description>
			<content:encoded><![CDATA[<p>I consider <a href="http://en.wikipedia.org/wiki/Depth_of_field">depth of field</a> as one of the most beautiful post-processing effects of the &#8220;next-gen&#8221; games.<br />
It was natural for me to choose it as the first shader demo to implement after months of inactivity, as a matter of fact GLSL_impgro was really just a testbed for post-processing basic techniques, like <a href="http://en.wikipedia.org/wiki/Framebuffer_Object">Frame Buffer Objects</a>.</p>
<p><img src='/blog/wp-content/uploads/2008/03/glsl_dof.png' alt='GLSL_DoF' /></p>
<p>I have studied the theory from an ATI paper included in the ShaderX2 book, titled <a href="http://ati.amd.com/developer/shaderx/ShaderX2_Real-TimeDepthOfFieldSimulation.pdf">Real-Time Depth of Field Simulation</a>, I have choosen the first of the two different implementation and converted it from Direct3D and HLSL to OpenGL and GLSL.</p>
<p>Of course, being a post-processing effect, the rendering is actually divided in two pass:</p>
<ol>
<li>Rendering the scene storing the depth of every vertex and calculating the amount of blur per fragment</li>
<li>Applying the blur per fragment based on the value from the previous step</li>
</ol>
<p>The second pass fragment shader, the one which is really applying the blur effect, is slow even on my 8600GT, because it performs several calculations for every one of the twelve fragments that are contributing to the blur of the center one.</p>
<p>Another interesting aspect is that, in order to calculate a correct approximation of the circular blur needed for <a href="http://en.wikipedia.org/wiki/Circle_of_confusion">circles of confusion</a> simulation, these twelve pixel are sampled around the center based on a poissonian disc distribution, thus creating much less artifacts than a small convolution matrix scaled too much in order to sample from far away the center.</p>
<p>Just like the previous demo you can view it in action on my <a href="http://www.youtube.com/encelo">YouTube Channel</a>, but I really suggest you to give a look to the high definition <a href="http://www.vimeo.com/798278">720p version</a> instead, hosted together with the other ones on my <a href="http://www.vimeo.com/encelo">Vimeo page</a>. <img src='http://encelo.netsons.org/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://encelo.netsons.org/blog/2008/03/23/i-love-depth-of-field/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>glUniform1f() is working!</title>
		<link>http://encelo.netsons.org/blog/2008/03/17/gluniform1f-is-working/</link>
		<comments>http://encelo.netsons.org/blog/2008/03/17/gluniform1f-is-working/#comments</comments>
		<pubDate>Mon, 17 Mar 2008 21:02:36 +0000</pubDate>
		<dc:creator>encelo</dc:creator>
				<category><![CDATA[Rants]]></category>
		<category><![CDATA[GLSL]]></category>
		<category><![CDATA[OpenGL]]></category>
		<category><![CDATA[shaders]]></category>

		<guid isPermaLink="false">http://encelo.netsons.org/blog/2008/03/17/gluniform1f-is-working/</guid>
		<description><![CDATA[I faced this problem for the first time a year ago, while working for my parallax mapping demo, and I met it again these days, in which I&#8217;m busy to fine tune my depth of field demo to permit keyboard driven parameters tweaking.

The issue I&#8217;m talking about is quite seriuos, on my machine it is [...]]]></description>
			<content:encoded><![CDATA[<p>I faced this problem for the first time a year ago, while working for my <a href="/blog/2007/05/01/parallax-mapping-for-the-masses/">parallax mapping</a> demo, and I met it again these days, in which I&#8217;m busy to fine tune my <em>depth of field</em> demo to permit keyboard driven parameters tweaking.</p>
<p><img src='http://encelo.netsons.org/blog/wp-content/uploads/2008/03/bug.jpg' alt='Bug' /></p>
<p>The issue I&#8217;m talking about is quite seriuos, on my machine it is impossible to pass a float uniform variable to a shader, and I&#8217;m not the only one reporting it:</p>
<ul>
<li><a href="http://www.gamedev.net/community/forums/topic.asp?topic_id=470542">glUniform*f seems to&#8230; not work.</a></li>
<li><a href="http://www.nvnews.net/vbulletin/showthread.php?t=95980">Problem with glUniform1f in 100.14.11 on 8800GTS</a></li>
<li><a href="http://www.groupsrv.com/computers/about318237.html">GLSL Vertex Sh. uniform aren&#8217;t set (OGL 2.0)</a></li>
</ul>
<p>The first link is a forum thread from GameDev written by a girl whose applications suffer from this annoying issue, he has written a <em>proof of concept</em> which works perfectly on my box, i.e. float uniforms are NOT passed. <img src='http://encelo.netsons.org/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /><br />
But it has been the third one which made me think about how to fix the problem: it has been reported that, after calling <tt>glewInit()</tt>, <tt>glUniform*f()</tt> functions work again.</p>
<p>The first thing I did, of course, was to download and investigate inside <a href="http://glew.sourceforge.net/">GLEW</a> sources to see what was happening inside that magic function. What it does, actually, is redefining all the GL function pointers calling <tt>glXGetProcAddress()</tt> for everyone of them, I thought it would have been a good thing to try to replicate this behaviour in my programs, and I was right! <img src='http://encelo.netsons.org/blog/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> </p>
<p>This is what I added to my sources for the incriminated function to work:</p>
<pre class="brush: cpp;">
PFNGLUNIFORM1FPROC glUniform1f = NULL;
glUniform1f = (PFNGLUNIFORM1FPROC)glXGetProcAddress((const GLubyte*)&quot;glUniform1f&quot;);
</pre>
<p>This also seems to explain why my Python shader demo didn&#8217;t suffer from all of this, I think that PyOpenGL initializes itself retrieving the addresses for all the GL functions it needs.</p>
<p><strong>IMPORTANT UPDATE</strong><br />
<a href="http://www.m3xbox.com">M3xican</a>, the <em>shader master</em> came with THE solution, just add <tt>-DGL_GLEXT_PROTOTYPES</tt> to CFLAGS.<br />
Hail to the master! <img src='http://encelo.netsons.org/blog/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://encelo.netsons.org/blog/2008/03/17/gluniform1f-is-working/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Image post-processing with shaders</title>
		<link>http://encelo.netsons.org/blog/2008/03/13/image-post-processing-with-shaders/</link>
		<comments>http://encelo.netsons.org/blog/2008/03/13/image-post-processing-with-shaders/#comments</comments>
		<pubDate>Thu, 13 Mar 2008 01:00:04 +0000</pubDate>
		<dc:creator>encelo</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Real-time Graphics]]></category>
		<category><![CDATA[GLSL]]></category>
		<category><![CDATA[OpenGL]]></category>
		<category><![CDATA[post-processing]]></category>
		<category><![CDATA[shaders]]></category>
		<category><![CDATA[snippets]]></category>

		<guid isPermaLink="false">http://encelo.netsons.org/blog/2008/03/13/image-post-processing-with-shaders/</guid>
		<description><![CDATA[I&#8217;m back to work after many months, university exams take really a lot of time&#8230;
For I am a bit rusty on GLSL programming, but willing to learn new things anyway, I have decided to begin with a simple yet interesting topic, image processing.

The whole thing, actually, needs two rendering passes and relies heavily on Frame [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m back to work after many months, university exams take really a lot of time&#8230;<br />
For I am a bit rusty on GLSL programming, but willing to learn new things anyway, I have decided to begin with a simple yet interesting topic, image processing.</p>
<p><img src='/blog/wp-content/uploads/2008/03/glsl_imgpro.png' alt='GLSL_imgpro' /></p>
<p>The whole thing, actually, needs two rendering passes and relies heavily on <a href=""http://oss.sgi.com/projects/ogl-sample/registry/EXT/framebuffer_object.txt>Frame Buffer Objects</a> because:</p>
<ol>
<li>You render the scene to an off-screen texture.</li>
<li>You render a quad covering the entire screen and binded to the previously written texture.</li>
<li>You make a shader process the fragments resulted from rendering this textured quad, i.e. post-processing the original scene.</li>
</ol>
<p>In this program post-processing is demanded to convolution matrices calculated with these kernels:</p>
<pre class="brush: cpp;">
GLfloat kernels[7][9] = {
    { 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f}, /* Identity */
    { 0.0f,-1.0f, 0.0f,-1.0f, 5.0f,-1.0f, 0.0f,-1.0f, 0.0f}, /* Sharpen */
    { 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f}, /* Blur */
    { 1.0f, 2.0f, 1.0f, 2.0f, 4.0f, 2.0f, 1.0f, 2.0f, 1.0f}, /* Gaussian blur */
    { 0.0f, 0.0f, 0.0f,-1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f}, /* Edge enhance */
    { 1.0f, 1.0f, 1.0f, 1.0f, 8.0f, 1.0f, 1.0f, 1.0f, 1.0f}, /* Edge detect */
    { 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f,-1.0f}  /* Emboss */
};
</pre>
<p>The final fragment color is calculated by a simple shader which, at the core, just performs the following:</p>
<pre class="brush: cpp;">
for(i = -1; i &lt;= 1; i++)
    for(j = -1; j &lt;= 1; j++) {
        coord = gl_TexCoord[0].st + vec2(float(i) * (1.0/float(Width)) * float(Dist), float(j) * (1.0/float(Height)) * float(Dist));
        sum += Kernel[i+1][j+1] * texture2D(Tex0, coord.xy);
        contrib += Kernel[i+1][j+1];
    }

    gl_FragColor = sum/contrib;
</pre>
<p>When the user chooses a filter, the application updates the kernel currently in use with a call to:</p>
<pre class="brush: cpp;">
loc = glGetUniformLocation(sh.p2, &quot;Dist&quot;);
glUniform1i(loc, dist);
loc = glGetUniformLocation(sh.p2, &quot;Kernel&quot;);
glUniformMatrix3fv(loc, 1, GL_FALSE, &amp;kernels[curker]);
</pre>
<p>Dist is a user defined parameter (you can change it using arrows) that defines the distance in pixels from the center to the contributing sample.</p>
<p>Since a month I have created a <a href="http://www.youtube.com/encelo">YouTube Channel</a>, now you can have an idea of how this demo works without downloading and compiling <a href="/_download/glsl_imgpro.tar.gz">the source code</a>: have a look at <a href="http://www.youtube.com/watch?v=lmTS9qSwCLg">this link</a>! <img src='http://encelo.netsons.org/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://encelo.netsons.org/blog/2008/03/13/image-post-processing-with-shaders/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
