<?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; C++</title>
	<atom:link href="http://encelo.netsons.org/blog/tag/c/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>A flexible PLY loader for Evolution War r71</title>
		<link>http://encelo.netsons.org/blog/2009/09/16/a-flexible-ply-loader-for-evolution-war-r71/</link>
		<comments>http://encelo.netsons.org/blog/2009/09/16/a-flexible-ply-loader-for-evolution-war-r71/#comments</comments>
		<pubDate>Wed, 16 Sep 2009 01:28:33 +0000</pubDate>
		<dc:creator>encelo</dc:creator>
				<category><![CDATA[Evolution War]]></category>
		<category><![CDATA[GameDev]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[snippets]]></category>

		<guid isPermaLink="false">http://encelo.netsons.org/blog/?p=290</guid>
		<description><![CDATA[I&#8217;ve cited Evolution War for the first time on this blog in my previous post, today I want to celebrate my return to SVN committing after a very long time.  

Revision 71 adds the support for a real Stanford PLY parser and loader, while the one I coded for my graphic class library is [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve cited <a href="http://www.evolution-war.com">Evolution War</a> for the first time on this blog in my <a href="/blog/2009/08/28/summer-summary/">previous post</a>, today I want to celebrate my return to SVN committing after a very long time. <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/09/ply_export.png" alt="PLY Export" title="PLY Export" width="400" height="175" class="aligncenter size-full wp-image-295" /></p>
<p>Revision 71 adds the support for a real <a href="http://en.wikipedia.org/wiki/PLY_%28file_format%29">Stanford PLY</a> parser and loader, while the one I coded for my graphic class library is very primitive, expecting a hard-coded order for data, this one shouldn&#8217;t have any kind of problem with every well-formed PLY file.</p>
<p>For example, while the hard-coded loader can only accept a file like this:</p>
<pre class="brush: plain;">
ply
format ascii 1.0
comment Created by Blender3D 249 - www.blender.org
element vertex 4
property float x
property float y
property float z
property float nx
property float ny
property float nz
element face 3
property list uchar uint vertex_indices
end_header
1.000000 2.000000 3.000000 -4.000000 -5.000000 6.000000
-1.000000 -2.000000 -3.000000 4.000000 5.000000 6.000000
1.000000 2.000000 3.000000 -4.000000 -5.000000 6.000000
-1.000000 -2.000000 -3.000000 4.000000 5.000000 6.000000
3 0 1 2
3 1 3 2
3 4 2 1
</pre>
<p>the parser loader can load even something like this:</p>
<pre class="brush: plain;">
ply
format ascii 1.0
comment Created and shuffled by hand
element face 3
property list uchar uint vertex_indices
element skipme 3
property float skipfirst
property float skipsecond
element vertex 4
property float z
property float nz
property float y
property float ny
property float nx
property float x
end_header
3 0 1 2
3 1 3 2
3 4 2 1
0.0 0.0
0.0 0.0
0.0 0.0
3.000000 -6.000000 2.000000 -5.000000 -4.000000 1.000000
-3.000000 6.000000 -2.000000 5.000000 4.000000 -1.000000
3.000000 -6.000000 2.000000 -5.000000 -4.000000 1.000000
-3.000000 6.000000 -2.000000 5.000000 4.000000 -1.000000
</pre>
<p>But one of its most important feature resides in the ability to correctly load <em>binary</em> PLY files! <img src='http://encelo.netsons.org/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Related to it there&#8217;s a bug I would like to share with you together with the fix:</p>
<pre class="brush: cpp;">
istream&amp; istream::read (char* s, streamsize n);
[...]
ifstream ifs;
unsigned int *uIndices;
[...]
ifs.read((char *) uIndices+(j*3), sizeof(unsigned int) * 3);
</pre>
<p>The <tt>read()</tt> method only accepts <tt>char</tt> pointers, so <tt>uIndices</tt> is casted, but the precedence goes to casting and not to native <tt>unsigned int</tt> pointer arithmetics, leading to catastrophic effects! <img src='http://encelo.netsons.org/blog/wp-includes/images/smilies/icon_surprised.gif' alt=':o' class='wp-smiley' /> </p>
<p>The fix was as simple as the bug was subtle:</p>
<pre class="brush: diff;">
-ifs.read((char *) uIndices+(j*3), sizeof(unsigned int) * 3);
+ifs.read((char *) (uIndices+(j*3)), sizeof(unsigned int) * 3)
</pre>
]]></content:encoded>
			<wfw:commentRss>http://encelo.netsons.org/blog/2009/09/16/a-flexible-ply-loader-for-evolution-war-r71/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>Back to work, Mars r622</title>
		<link>http://encelo.netsons.org/blog/2007/02/28/back-to-work-mars-r622/</link>
		<comments>http://encelo.netsons.org/blog/2007/02/28/back-to-work-mars-r622/#comments</comments>
		<pubDate>Wed, 28 Feb 2007 17:50:16 +0000</pubDate>
		<dc:creator>encelo</dc:creator>
				<category><![CDATA[GameDev]]></category>
		<category><![CDATA[Mars: Land of No Mercy]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[snippets]]></category>

		<guid isPermaLink="false">http://encelo.netsons.org/blog/?p=16</guid>
		<description><![CDATA[Yesterday my exams session finally ended, I&#8217;m really satisfied about the results achieved, but during the studying period I was eager to get back to coding&#8230;
Today I fixed bug #0000008, a fastidious one which caused program termination if the user tried to take a screenshot after having deleted the hidden directory inside his home where [...]]]></description>
			<content:encoded><![CDATA[<p>Yesterday my exams session finally ended, I&#8217;m really satisfied about the results achieved, but during the studying period I was eager to get back to coding&#8230;</p>
<p>Today I fixed bug #0000008, a fastidious one which caused program termination if the user tried to take a screenshot after having deleted the hidden directory inside his home where settings and images are saved by default. <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/2007/03/mars_011_2.jpg' alt='Mars 0.1.1 2nd' /></p>
<p>The first thing I thought was that I was missing a fopen() return code check, but, fortunately for my reputation, it wasn&#8217;t the case. <img src='http://encelo.netsons.org/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<pre class="brush: cpp;">
// Opening output file
if((fp = fopen(filename, &quot;wb&quot;)) == NULL)
{
  throw Exception(&quot;Screen&quot;, &quot;fopen error&quot;);
  return -1;
}
</pre>
<p>The problem, as the shell output was suggesting, was related to the exception system: the fopen() exception was never caught.</p>
<p>Just changing this:</p>
<pre class="brush: cpp;">
case SDLK_F4:
  screen-&gt;TakeScreenshot();
  break;
</pre>
<p>into this:</p>
<pre class="brush: cpp;">
case SDLK_F4:
  try
  {
    screen-&gt;TakeScreenshot();
  }
  catch(Exception e)
  {
    e.PrintError();
  }
  break;
</pre>
<p>fixed everything.</p>
<p>Have a look at <a href="http://svn.sourceforge.net/viewvc/mars?view=rev&#038;revision=622">r622 log</a> and at <a href="http://svn.sourceforge.net/viewvc/mars/mars/trunk/src/mars.cpp?r1=622&#038;r2=621&#038;pathrev=622">mars.cpp changes</a> and remember to catch all the exception you may throw. <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/2007/02/28/back-to-work-mars-r622/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Mars r594 and the vflip hack</title>
		<link>http://encelo.netsons.org/blog/2006/12/30/mars-r594-and-the-vflip-hack/</link>
		<comments>http://encelo.netsons.org/blog/2006/12/30/mars-r594-and-the-vflip-hack/#comments</comments>
		<pubDate>Sat, 30 Dec 2006 02:41:41 +0000</pubDate>
		<dc:creator>encelo</dc:creator>
				<category><![CDATA[GameDev]]></category>
		<category><![CDATA[Mars: Land of No Mercy]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[OpenGL]]></category>
		<category><![CDATA[snippets]]></category>

		<guid isPermaLink="false">http://encelo.netsons.org/blog/?p=6</guid>
		<description><![CDATA[For my first entry on this blog let me tell you a tale, it&#8217;s about OpenGL framebuffer and vertical flipping&#8230;

Once upon a time a little fool called Encelo used to perform, in a little testing program, a vertical flip of the entire OpenGL framebuffer this way:

if(_flags &#38; SDL_OPENGL)
{

  GLvoid * pixels;

  pixels = [...]]]></description>
			<content:encoded><![CDATA[<p>For my first entry on this blog let me tell you a tale, it&#8217;s about OpenGL framebuffer and vertical flipping&#8230;</p>
<p><img src="/blog/wp-content/uploads/2007/03/mars_011_1.jpg" alt="Mars 0.1.1 1st" /></p>
<p>Once upon a time a little fool called Encelo used to perform, in a <a href="http://autistici.org/encelo/download/savesurf.tar.gz">little testing program</a>, a vertical flip of the entire OpenGL framebuffer this way:</p>
<pre class="brush: cpp;">
if(_flags &amp; SDL_OPENGL)
{

  GLvoid * pixels;

  pixels = (GLvoid *) malloc(_width * _height * 4);
  glPushAttrib(GL_COLOR_BUFFER_BIT | GL_CURRENT_BIT | GL_PIXEL_MODE_BIT);
  glReadBuffer(GL_FRONT);
  glReadPixels(0, 0, _width, _height, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
  glDrawBuffer(GL_BACK);
  glRasterPos2f(-1.0f, 1.0f);
  glPixelZoom(1.0f, -1.0f);
  glDrawPixels(_width, _height, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
  glReadBuffer(GL_BACK);
  glReadPixels(0, 0, _width, _height, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
  glPopAttrib();
  output_surf = SDL_CreateRGBSurfaceFrom(pixels, _width, _height, 32, _surface-&gt;pitch, rmask, gmask, bmask, amask);
}
</pre>
<p>It wasn&#8217;t really bad, it performed some interesting tricks with buffers, and, as a matter of fact, Encelo was really proud of this implementation. <img src='http://encelo.netsons.org/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /><br />
But&#8230; it didn&#8217;t work on Mars. Yes, no matter how much Encelo tested, changed, and tested again, it simply didn&#8217;t work on anything else than the original testing program.<br />
A decision had to be taken soon, to persevere or not  to persevere? That was the question.</p>
<p>Encelo chose not to persevere and to try a completely different approach&#8230; memcpy() flipping! <img src='http://encelo.netsons.org/blog/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /><br />
Yeah, something as simple, elegant, fast and smart as this:</p>
<pre class="brush: cpp;">
if(_flags &amp; SDL_OPENGL)
{
  int row, stride;
  GLubyte * swapline;
  GLubyte * pixels;

  stride = _width * 4; // length of a line in bytes
  pixels = (GLubyte *) malloc(stride * _height);
  swapline = (GLubyte *) malloc(stride);

  glReadPixels(0, 0, _width, _height, GL_RGBA, GL_UNSIGNED_BYTE, pixels);

  // vertical flip
  for(row = 0; row &lt; _height/2; row++)
  {
    memcpy(swapline, pixels + row * stride, stride);
    memcpy(pixels + row * stride, pixels + (_height - row - 1) * stride, stride);
    memcpy(pixels + (_height - row -1) * stride, swapline, stride);
  }

  output_surf = SDL_CreateRGBSurfaceFrom(pixels, _width, _height, 32, _surface-&gt;pitch, rmask, gmask, bmask, amask);
}
</pre>
<p>This story is true, and happened exactly a month ago, on the 30 of November 2006.<br />
As a proof have a look at <a href="http://svn.sourceforge.net/viewvc/mars?view=rev&amp;revision=594">r594 log</a> and at <a href="http://svn.sourceforge.net/viewvc/mars/mars/trunk/src/graphic/Screen.cpp?r1=594&amp;r2=592&amp;pathrev=594">Screen.cpp changes</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/2006/12/30/mars-r594-and-the-vflip-hack/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
