<?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>#AltDevBlogADay &#187; Nick Darnell</title>
	<atom:link href="http://www.altdevblogaday.com/author/nick-darnell/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.altdevblogaday.com</link>
	<description>Each day a little more #gamedev love</description>
	<lastBuildDate>Wed, 08 May 2013 03:07:50 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<item>
		<title>Oxel: A Tool for Occluder Generation</title>
		<link>http://www.altdevblogaday.com/2012/04/06/oxel-a-tool-for-occluder-generation/</link>
		<comments>http://www.altdevblogaday.com/2012/04/06/oxel-a-tool-for-occluder-generation/#comments</comments>
		<pubDate>Fri, 06 Apr 2012 23:30:27 +0000</pubDate>
		<dc:creator>Nick Darnell</dc:creator>
				<category><![CDATA[Computer Graphics]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tools]]></category>
		<category><![CDATA[graphics]]></category>
		<category><![CDATA[Hierarchical Z-Buffer]]></category>
		<category><![CDATA[Occlusion Volumes]]></category>
		<category><![CDATA[Rendering]]></category>
		<category><![CDATA[tool]]></category>

		<guid isPermaLink="false">http://altdevblogaday.com/?p=25415</guid>
		<description><![CDATA[<p>Its been awhile since I’ve done an update to my research into occluder generation.  If you need a refresher, take a look at:</p>
<ul>
<li><a href="http://www.nickdarnell.com/2011/06/hierarchical-z-buffer-occlusion-culling-generating-occlusion-volumes/" target="_blank">Hierarchical Z-Buffer Occlusion Culling – Generating Occlusion Volumes</a></li>
</ul>
<p><a href="http://www.altdevblogaday.com/2012/04/06/oxel-a-tool-for-occluder-generation/" class="more-link">Read more on Oxel: A Tool for Occluder Generation&#8230;</a></p>
]]></description>
				<content:encoded><![CDATA[<p>Its been awhile since I’ve done an update to my research into occluder generation.  If you need a refresher, take a look at:</p>
<ul>
<li><a href="http://www.nickdarnell.com/2011/06/hierarchical-z-buffer-occlusion-culling-generating-occlusion-volumes/" target="_blank">Hierarchical Z-Buffer Occlusion Culling – Generating Occlusion Volumes</a></li>
<li><a href="http://www.nickdarnell.com/2011/09/robust-inside-and-outside-solid-voxelization/" target="_blank">Robust Inside and Outside Solid Voxelization</a></li>
</ul>
<p>Lets start with the newest and most important information&#8230;</p>
<h3>Update 4/13/2012</h3>
<p>The project is now hosted on BitBucket, <a href="http://bitbucket.org/NickDarnell/oxel/overview" target="_blank">http://bitbucket.org/NickDarnell/oxel/overview</a> feel free to contribute or just follow along :)</p>
<h3>It’s a Tool Now!</h3>
<p><img style="margin: 5px;padding-left: 0px;padding-right: 0px;padding-top: 0px;border-width: 0px" src="http://altdevblogaday.com/wp-content/uploads/2012/04/oxel_v1.png" alt="oxel_v1" width="590" height="592" border="0" /></p>
<h4>Download</h4>
<p><a href="http://bitbucket.org/NickDarnell/oxel/downloads" target="_blank">Oxel 1.0.0 &#8211; Win32.zip</a></p>
<h4>Requirements</h4>
<ul>
<li><a href="http://www.microsoft.com/download/en/details.aspx?id=17718" target="_blank">.Net 4.0</a></li>
<li><a href="http://www.microsoft.com/download/en/details.aspx?id=5555" target="_blank">VS2010 C++ Runtime</a></li>
</ul>
<h4>Description</h4>
<p>Oxel is a tool for generating occluders – primarily for use with the Hierarchical Z-Buffer method of occlusion culling.  Open an .obj file then go to Build &gt; Voxelize to generate the proxy.  Try it out, let me know what you think.</p>
<p>There are some further improvements that have been made over the method that is laid out in the original <a href="http://www.nickdarnell.com/2011/06/hierarchical-z-buffer-occlusion-culling-generating-occlusion-volumes/" target="_blank">Generating Occlusion Volumes</a> post,</p>
<ul>
<li>Retriangulation</li>
<li>Evaluating Occlusion-ness</li>
<li>Filtering Polygons</li>
</ul>
<h3>Retriangulation</h3>
<p>I went back to the CSG article <a href="http://sandervanrossen.blogspot.com/">Sander van Rossen</a> and Matthew Baranowski wrote.  I noticed near the end that they recommended retriangulating the final CSG mesh because their library doesn’t handle it.</p>
<p>The easiest method I found to do the retriangulation was to just use David Eberly’s Wild Magic math library which can do it, <a href="http://www.geometrictools.com/SampleMathematics/Triangulation/Triangulation.html" target="_blank">see here</a>.  Before you perform the retriangulation you need to collect all the external/internal edge loops on each surface and then merge collinear edges before performing the retriangulation.</p>
<p>It’s was definitely worth doing, performing the retriangulation saved about 30% of the triangles.</p>
<h3>Evaluating Occlusion-ness</h3>
<p>When you’re generating occluder geometry you need to ensure that the volumes you’re adding are useful enough to pay the additional polygon tax they will incur.</p>
<p>Oxel achieves this by measuring the number of pixels written to the color buffer by rendering the original visual mesh into the stencil buffer, and then using the stencil buffer to clip a full screen quad while a “samples passed” hardware query is performed to record the number of pixels written.  This is performed from about 64 different camera angles – then when all the samples are summed together this defines the <strong>ground truth</strong> occlusion of the mesh.</p>
<p>Knowing that information we can evaluate every new box we plan to add to the final occlusion proxy geometry and ask – Does this increase the coverage enough to make it worth it?.  The default threshold is set to 3%.  If a new volume does not at least cover 3% of new silhouette pixels, we don’t include it in the final occluder mesh.</p>
<h3>Filtering Polygons</h3>
<p>Not every game needs the occluders to be visible from all angles.  For example most racing games will never have the cars jumping so high they see on top of a building.  In those circumstances you may not want to have the occluders bothering to have polygons on the top.</p>
<table width="596" border="0" cellspacing="0" cellpadding="2">
<tbody>
<tr>
<td valign="top" width="298"><a href="http://altdevblogaday.com/wp-content/uploads/2012/04/with.png"><img style="margin: 1px;padding-left: 0px;padding-right: 0px;padding-top: 0px;border-width: 0px" src="http://altdevblogaday.com/wp-content/uploads/2012/04/with_thumb.png" alt="with" width="275" height="168" border="0" /></a></td>
<td valign="top" width="296"><a href="http://altdevblogaday.com/wp-content/uploads/2012/04/without.png"><img style="margin: 1px;padding-left: 0px;padding-right: 0px;padding-top: 0px;border-width: 0px" src="http://altdevblogaday.com/wp-content/uploads/2012/04/without_thumb.png" alt="without" width="275" height="168" border="0" /></a></td>
</tr>
</tbody>
</table>
<p>So the tool offers the ability to remove Top and Bottom polygons from the final occluder mesh after everything has finished being processed.</p>
<p>If you decide to filter out all top polygons they’re all removed.  For bottom surfaces this has to be handled slightly differently, see below -</p>
<p><a href="http://altdevblogaday.com/wp-content/uploads/2012/04/bottom_removed.png"><img style="margin: 5px;padding-left: 0px;padding-right: 0px;padding-top: 0px;border-width: 0px" src="http://altdevblogaday.com/wp-content/uploads/2012/04/bottom_removed_thumb.png" alt="bottom_removed" width="580" height="331" border="0" /></a></p>
<p>Imagine a bridge structure or a large over hang on a building.  You wouldn’t want the bottom portion of the occluder to be removed from the overhang since it would allow seeing into and through the occluder &#8211; because presumably you wouldn’t have double sided rendering enabled.  So to distinguish between bottom surfaces not meant to ever be seen by the player, and bottom surfaces that may be important to higher up potions of the structure, only bottom surfaces within 1 voxel’s height from the base of the mesh are removed.  (See picture above)</p>
<h3>Work Continues</h3>
<p>I’ve got some more ideas I need to test out but I wanted to go ahead and cut a version of where I’m at and let others take a look and give me some feedback.</p>
<p>One area I need to investigate next is a better way to generate the boxes.  Currently they are just expanded in all directions equally, but that’s not ideal.  I’m thinking about trying a parallel brute force method that would test all possible boxes at a specific origin point to find the best shaped box to generate from that point.</p>
<p><a href="http://www.nickdarnell.com/2012/04/oxel-a-tool-for-occluder-generation/" target="_blank">Oxel: A Tool for Occluder Generation @ nickdarnell.com</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.altdevblogaday.com/2012/04/06/oxel-a-tool-for-occluder-generation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Global Game Jam 2012 Wrap-Up</title>
		<link>http://www.altdevblogaday.com/2012/02/06/global-game-jam-2012-wrap-up/</link>
		<comments>http://www.altdevblogaday.com/2012/02/06/global-game-jam-2012-wrap-up/#comments</comments>
		<pubDate>Mon, 06 Feb 2012 23:54:16 +0000</pubDate>
		<dc:creator>Nick Darnell</dc:creator>
				<category><![CDATA[Game design]]></category>
		<category><![CDATA[General Interest]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[2012]]></category>
		<category><![CDATA[Game Jam]]></category>
		<category><![CDATA[Global Game Jam]]></category>

		<guid isPermaLink="false">http://altdevblogaday.com/?p=23951</guid>
		<description><![CDATA[<p>The 2012 Global Game Jam is over and it was another year of awesome games in the <a href="http://www.trianglegamejam.com/global-game-jam-2012/" target="_blank">Raleigh-Durham &#8211; North Carolina</a> area.  This week I wanted to put the spotlight on all the games lovingly crafted by our crack squad of jammers – especially mine, Low Power! :D</p>
<p><a href="http://www.altdevblogaday.com/2012/02/06/global-game-jam-2012-wrap-up/" class="more-link">Read more on Global Game Jam 2012 Wrap-Up&#8230;</a></p>
]]></description>
				<content:encoded><![CDATA[<p>The 2012 Global Game Jam is over and it was another year of awesome games in the <a href="http://www.trianglegamejam.com/global-game-jam-2012/" target="_blank">Raleigh-Durham &#8211; North Carolina</a> area.  This week I wanted to put the spotlight on all the games lovingly crafted by our crack squad of jammers – especially mine, Low Power! :D</p>
<h1>The Theme</h1>
<p><a href="http://altdevblogaday.com/wp-content/uploads/2012/02/theme.png"><img style="margin: 5px;padding-left: 0px;padding-right: 0px;padding-top: 0px;border-width: 0px" src="http://altdevblogaday.com/wp-content/uploads/2012/02/theme_thumb.png" alt="theme" width="240" height="239" border="0" /></a></p>
<p>The Ouroboros – but no explanation was given.  We just showed the jammers the image and it was up to them to interprete it.</p>
<h1><a href="http://www.trianglegamejam.com/global-game-jam-2012/lotr-body-snatching/" target="_blank">LOTR Body Snatching</a></h1>
<p>Rock, Paper, Scissors with body snatching!<br />
Avoid or possess enemies to stay alive and win.</p>

<object width="853" height="480">
<param name="allowfullscreen" value="true" />
<param name="allowscriptaccess" value="always" />
<param name="movie" value="http://www.youtube.com/v/_UTZHepSNO8&autoplay=0&loop=0&rel=0" />
<param name="wmode" value="transparent">
<embed src="http://www.youtube.com/v/_UTZHepSNO8&autoplay=0&loop=0&rel=0" type="application/x-shockwave-flash" wmode="transparent" allowfullscreen="true" allowscriptaccess="always" width="853" height="480">
</embed>
</object>


<h1><a href="http://www.trianglegamejam.com/global-game-jam-2012/low-power/" target="_blank">Low Power</a></h1>
<p>In order to survive Low Power you must consume yourself (like the Ouroboros) to survive.  You control a robot whose battery is constantly draining but there are valuable energy cores scattered throughout the level that will sustain your life.  There are all kinds of dangerous environmental hazards that unless you enable your sensors/abilities (lights, shield, ground sensor, microphone) you’ll never survive.  However enabling the different systems will drain your battery even faster!</p>

<object width="853" height="480">
<param name="allowfullscreen" value="true" />
<param name="allowscriptaccess" value="always" />
<param name="movie" value="http://www.youtube.com/v/PBw0g5rVPvQ&autoplay=0&loop=0&rel=0" />
<param name="wmode" value="transparent">
<embed src="http://www.youtube.com/v/PBw0g5rVPvQ&autoplay=0&loop=0&rel=0" type="application/x-shockwave-flash" wmode="transparent" allowfullscreen="true" allowscriptaccess="always" width="853" height="480">
</embed>
</object>


<h1><a href="http://www.trianglegamejam.com/global-game-jam-2012/parasite/" target="_blank">Parasite</a></h1>
<p>You are a Parasite that leeches off battle ships. Enemies will constantly come in formations to destroy the ship you are occupying. Using the possessed ship, you must defeat your enemies! However, as you leech your ship it slowly dies, you will constantly need to take control of and leech of another ship. With out a ship for protection you are helpless!</p>

<object width="853" height="480">
<param name="allowfullscreen" value="true" />
<param name="allowscriptaccess" value="always" />
<param name="movie" value="http://www.youtube.com/v/VD2FlSpbu-A&autoplay=0&loop=0&rel=0" />
<param name="wmode" value="transparent">
<embed src="http://www.youtube.com/v/VD2FlSpbu-A&autoplay=0&loop=0&rel=0" type="application/x-shockwave-flash" wmode="transparent" allowfullscreen="true" allowscriptaccess="always" width="853" height="480">
</embed>
</object>


<h1><a href="http://www.trianglegamejam.com/global-game-jam-2012/planes-on-a-snake/" target="_blank">Planes on a Snake</a></h1>
<p>A rift in spacetime has resulted in a large number of World War II era planes getting stuck on the world snake.  Join the frequent fliers club of Ouroboros Airlines, racking up points while taking advantage of the torus nature of your new environment.</p>

<object width="853" height="480">
<param name="allowfullscreen" value="true" />
<param name="allowscriptaccess" value="always" />
<param name="movie" value="http://www.youtube.com/v/UHdp0cqIcMY&autoplay=0&loop=0&rel=0" />
<param name="wmode" value="transparent">
<embed src="http://www.youtube.com/v/UHdp0cqIcMY&autoplay=0&loop=0&rel=0" type="application/x-shockwave-flash" wmode="transparent" allowfullscreen="true" allowscriptaccess="always" width="853" height="480">
</embed>
</object>


<h1><a href="http://www.trianglegamejam.com/global-game-jam-2012/roller-snake/" target="_blank">Roller Snake: The Quest for Chili Dogs</a></h1>
<p>Control “Hardy the Hoop Snake, Jr., III, IV, and V” in his quest to eat as many chili dogs as possible while winding through ‘Catastrophe Canyon’</p>

<object width="853" height="480">
<param name="allowfullscreen" value="true" />
<param name="allowscriptaccess" value="always" />
<param name="movie" value="http://www.youtube.com/v/WofSQtdaPm4&autoplay=0&loop=0&rel=0" />
<param name="wmode" value="transparent">
<embed src="http://www.youtube.com/v/WofSQtdaPm4&autoplay=0&loop=0&rel=0" type="application/x-shockwave-flash" wmode="transparent" allowfullscreen="true" allowscriptaccess="always" width="853" height="480">
</embed>
</object>


<h1><a href="http://www.trianglegamejam.com/global-game-jam-2012/snake-run/" target="_blank">Snake Run</a></h1>
<p>You are the snake’s guardian, you must ensure that balance is maintained.  Let enough creatures be eaten by the snake to ensure he doesn’t starve, but not enough that he frenzies and destroys everything.  All the while outrunning him!</p>

<object width="853" height="480">
<param name="allowfullscreen" value="true" />
<param name="allowscriptaccess" value="always" />
<param name="movie" value="http://www.youtube.com/v/WN8FaRJ00Zg&autoplay=0&loop=0&rel=0" />
<param name="wmode" value="transparent">
<embed src="http://www.youtube.com/v/WN8FaRJ00Zg&autoplay=0&loop=0&rel=0" type="application/x-shockwave-flash" wmode="transparent" allowfullscreen="true" allowscriptaccess="always" width="853" height="480">
</embed>
</object>


<p><a href="http://www.nickdarnell.com/2012/02/global-game-jam-2012-wrap-up/" target="_blank">Global Game Jam 2012 Wrap-Up @ nickdarnell.com</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.altdevblogaday.com/2012/02/06/global-game-jam-2012-wrap-up/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Creating Kinect Controls for Angry Birds</title>
		<link>http://www.altdevblogaday.com/2011/12/08/creating-kinect-controls-for-angry-birds/</link>
		<comments>http://www.altdevblogaday.com/2011/12/08/creating-kinect-controls-for-angry-birds/#comments</comments>
		<pubDate>Thu, 08 Dec 2011 16:00:38 +0000</pubDate>
		<dc:creator>Nick Darnell</dc:creator>
				<category><![CDATA[Game design]]></category>
		<category><![CDATA[General Interest]]></category>
		<category><![CDATA[Angry Birds]]></category>
		<category><![CDATA[Kinect]]></category>
		<category><![CDATA[Motion Gaming]]></category>
		<category><![CDATA[NUI]]></category>

		<guid isPermaLink="false">http://altdevblogaday.com/?p=21302</guid>
		<description><![CDATA[<p>Before I begin let me give you a little background.  About a month ago we decided to try and create NUI (Natural User Interface) or Kinect controls for Angry Birds.  Partially as a learning experience but also as proof of concept that games like Angry Birds (ex. applications that lend themselves to touchscreen devices) can work in a gestural / depth camera environment better than has been demonstrated.  In the end we tried many different input methods that would be possible with a Kinect and I wanted to catalog that experience.</p>
<p><a href="http://www.altdevblogaday.com/2011/12/08/creating-kinect-controls-for-angry-birds/" class="more-link">Read more on Creating Kinect Controls for Angry Birds&#8230;</a></p>
]]></description>
				<content:encoded><![CDATA[<p>Before I begin let me give you a little background.  About a month ago we decided to try and create NUI (Natural User Interface) or Kinect controls for Angry Birds.  Partially as a learning experience but also as proof of concept that games like Angry Birds (ex. applications that lend themselves to touchscreen devices) can work in a gestural / depth camera environment better than has been demonstrated.  In the end we tried many different input methods that would be possible with a Kinect and I wanted to catalog that experience.</p>
<p>I&#8217;ll start with the worst input paradigm we tried go from there.</p>
<h3>#5 The Faux Mouse</h3>
<p><strong>The Idea<br />
</strong>First we just tried mapping the hands to the screen as if they were each a mouse or finger.  Each would control a hand like cursor and move it around the screen.  Clicks could either be performed by grasping the hands or pushing towards the screen.  The game would be played normally &#8211; except you&#8217;d click on everything using your hand.</p>
<p><img class="alignnone" src="http://media.tested.com/uploads/0/5/6432-minorityreport_600_super.jpg" alt="" width="480" height="240" /></p>
<p><strong>The Reality<br />
</strong>This input method was by far the worst.  Your hands are simply not mice, they get tired much faster and are not as dexterous.  Even with heavy filtering and snap-able buttons, the interaction is just too nuanced and motor skill intensive an operation to click on a button or object on the screen using your hands as mice.  The time it took us to play a level vs. someone playing it on the iPhone was an order of magnitude or longer a task.</p>
<p>When pushing towards the screen a lot of care had to be taken to deal with user drift.  When a user pushes towards the screen, they may in fact be pushing towards many possible locations.  They may drift towards the TV, or the camera if that&#8217;s their focus.  They may also consciously attempt to maintain a straight hand as they push forward.  No matter the case &#8211; a user will drift even off their intended target.</p>
<p>When we tried grasp detection using computer vision &#8211; again we saw drift.  When a user opens and closes their hand the volume of the hand is changed from the point of view of the camera.  The result ends up displacing the center of mass of the hand causing it to shift downward when the hand is closed.  There are solutions to this problem such as eliminating the fingers from the hand volume calculation but this is a difficult problem given the quality of the data.</p>
<p>In the end the drift issues prevented us from playing as well as we could with the real mouse or on the iPhone and since Angry Birds is both a game of quick victory and defeat as well as a game of precision.  Having both large problems with precision and game play progression speed &#8211; we ditched this idea.</p>
<h3>#4 Voice</h3>
<p><strong>The Idea<br />
</strong>This one is exactly what you might expect &#8211; firing the bird using your voice.  Either by saying &#8220;Fire!&#8221; or maybe&#8230; &#8220;Ca Cah!&#8221;.</p>
<p><img class="alignnone size-full wp-image-1908" src="http://www.nickdarnell.com/wp-content/uploads/2011/12/fire_anime.png" alt="" width="440" height="224" /></p>
<p><strong>The Reality<br />
</strong>We really were not sure how this method would perform.  The voice portion was layered onto the mouse control system.  Essentially you would click on the slingshot &#8211; move your hand to aim and exclaim viciously to let loose the birds of war.  The hope was that it would alleviate the drift problems, which it did.  Without having to push your hand forward to fire the drift was eliminated from firing.</p>
<p>However there were other problems, there was delay in the speech recognition and often outright failure if you didn&#8217;t say the words just right.  We tried several tricks, like using multiple words to identify &#8220;Fire&#8221;.  One good way to generate that list is to take the top 10 words it mistakened someone saying fire for and add those to the dictionary as triggers for firing the bird.</p>
<h3>Moving Away From Faux Mouse</h3>
<p>After the failures with both #4 and #5 we went back to the drawing board.  We needed to get away from the mouse or touch device centered thinking.  The worlds are simply too different to treat it the same.  So we prototyped 3 other solutions that ended up all being better than the mouse style interface.</p>
<p>The ideas all stem from an understanding that when you go to port an application from a touch enabled world you need to think about 2 things primarily,</p>
<ul>
<li><strong>Context</strong></li>
<li><strong>Automation</strong></li>
</ul>
<p><strong>Context</strong> &#8211; How can you reduce and scope the options to the user so that a broad array of options can be presented to the user &#8211; but with only a few usable at any given time with a small vocabulary of motions.</p>
<p>An example from Angry Birds is all the options the user can perform in the game:</p>
<ul>
<li>Fire Bird</li>
<li>Activate Bird Special Attack</li>
<li>Restart</li>
<li>Pan</li>
<li>Zoom</li>
<li>Return to the Menu</li>
</ul>
<p>We needed to find a way to contextualize these options when moving away from a mouse driven style interface.</p>
<p><strong>Automation</strong> &#8211; Find the items in the application that everyone does without thinking about it and automate them.  If they aren&#8217;t relevant to game play find a way to make them irrelevant in a NUI application.</p>
<p>An example from Angry Birds is activating the slingshot.  You probably don&#8217;t think about it when playing the game but to fire a bird you must first place your finger on the slingshot before drawing back the bird to fire it.  While this is unbelievably trivial to the point of not thinking about it on an iPhone, it&#8217;s a huge pain in an environment where you have to get a virtual hand cursor over it, even more so if you then need to push forward to activate it.</p>
<p>So we needed to find a way to automate clicking the slingshot.  That way instead of clicking the slingshot explicitly it would be implicitly activated by performing some gesture to begin the act of firing a bird, that would be disconnected from the onscreen location of the hand relative to the slingshot on the screen.</p>
<h3>#3 Arclight</h3>
<p><strong>The Idea<br />
</strong>You would draw back the slingshot by bringing your hands together.  Then bring your hands apart and rotate them around your center of mass to change the firing angle on the slingshot.  Then once you&#8217;ve settled on the firing angle, bring your hands together fast to trigger the fire.</p>
<p><img class="alignnone size-full wp-image-1897" src="http://www.nickdarnell.com/wp-content/uploads/2011/12/arc_light1.png" alt="" width="550" height="266" /></p>
<p><strong>The Reality<br />
</strong>The problem with this kind of activation of the slingshot was the drift when the hands come together.  This can be partially accounted for but it&#8217;s heuristically based and can be erroneous.  Additionally activating the bird&#8217;s special power was difficult.  You would have to choose a different kind of interaction to activate the special power which would complicate the process.</p>
<h3>#2 Stretch and Snap</h3>
<p><strong>The Idea</strong><br />
This idea grew out of the Arclight firing system.  To attempt to solve the problem of drift, have the slingshot fire as soon as the arms reach a certain distance apart.</p>
<p><img class="alignnone size-full wp-image-1898" style="margin: 5px" src="http://www.nickdarnell.com/wp-content/uploads/2011/12/stretch_snap.png" alt="" width="376" height="266" /></p>
<p><strong>The Reality<br />
</strong>With this firing system you still have the problem of determining how to activate the bird special power.  You also introduce a new problem &#8211; all birds are fired at maximum drawback.  You also need to make sure to provide the user with feedback so that they know how close the user is to the *snap*, some kind of progress firing bar.</p>
<h3>#1 Axis Separated</h3>
<p><strong>The Idea</strong><br />
For this idea I separated the functions of the hands into distinct responsibilities.  Your left hand activates the slingshot by pushing forward (doesn&#8217;t matter where).  After a threshold is crossed the slingshot is activated, from then on an angle is calculated between the shoulder location and the left hand&#8217;s location relative to it, to produce the slingshot firing angle.  To fire the bird the right hand is pushed forward and pulled back, this sends the bird flying.  To activate the bird&#8217;s special power you again push the right hand forward.</p>
<p><img class="alignnone size-full wp-image-1888" src="http://www.nickdarnell.com/wp-content/uploads/2011/12/domain_separated.png" alt="" width="265" height="275" /></p>
<p><strong>The Reality<br />
</strong>This method ended up working perfectly.  It doesn&#8217;t result in any drift when firing is activated.  It is also easy to perform because all the motions can be performed with your arms down by your sides, reducing exhaustion in long game play sessions.</p>
<h3>Lessons Learned</h3>
<p>You hear it all the time but it is critically important to prototype ideas when it comes to creating Kinect controls.  They simply don&#8217;t work as well as you would like in reality as they do in your head.  Here&#8217;s a demonstration video of the end result,</p>
<span class='embed-youtube' style='text-align:center; display: block;'><iframe class='youtube-player' type='text/html' width='640' height='390' src='http://www.youtube.com/embed/TCRxmWxPO5o?version=3&#038;rel=1&#038;fs=1&#038;showsearch=0&#038;showinfo=1&#038;iv_load_policy=1&#038;wmode=transparent' frameborder='0'></iframe></span>
<p><a href="http://www.nickdarnell.com/2011/12/creating-kinect-controls-for-angry-birds/" target="_blank">Creating Kinect Controls for Angry Birds @ nickdarnell.com</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.altdevblogaday.com/2011/12/08/creating-kinect-controls-for-angry-birds/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Kinect, Anthropometry and You</title>
		<link>http://www.altdevblogaday.com/2011/11/08/kinect-anthropometry-and-you/</link>
		<comments>http://www.altdevblogaday.com/2011/11/08/kinect-anthropometry-and-you/#comments</comments>
		<pubDate>Tue, 08 Nov 2011 07:48:06 +0000</pubDate>
		<dc:creator>Nick Darnell</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Anthropometry]]></category>
		<category><![CDATA[Kinect]]></category>
		<category><![CDATA[Motion Gaming]]></category>

		<guid isPermaLink="false">http://altdevblogaday.com/?p=19941</guid>
		<description><![CDATA[<blockquote><p><strong>Anthropometry</strong> (Greek <em>anthropos</em> (άνθρωπος &#8211; &#8220;man&#8221;) and <em>metron</em> (μέτρον &#8211; &#8220;measure&#8221;) therefore &#8220;measurement of man&#8221;) refers to the measurement of the human individual.</p></blockquote>
<p>Ask any Kinect developer what the hardest problem is developing a game or application that uses the Kinect – or any other depth camera.  The answer you’ll get most often will be creating something that works well for 95% of target users.</p>
<p><a href="http://www.altdevblogaday.com/2011/11/08/kinect-anthropometry-and-you/" class="more-link">Read more on Kinect, Anthropometry and You&#8230;</a></p>
]]></description>
				<content:encoded><![CDATA[<blockquote><p><strong>Anthropometry</strong> (Greek <em>anthropos</em> (άνθρωπος &#8211; &#8220;man&#8221;) and <em>metron</em> (μέτρον &#8211; &#8220;measure&#8221;) therefore &#8220;measurement of man&#8221;) refers to the measurement of the human individual.</p></blockquote>
<p>Ask any Kinect developer what the hardest problem is developing a game or application that uses the Kinect – or any other depth camera.  The answer you’ll get most often will be creating something that works well for 95% of target users.</p>
<p>This is something you have to consider for all your gestures, poses, and UI interaction.</p>
<ul>
<li>Is this gesture too difficult for your average user?</li>
<li>Does this pose require too much flexibility?</li>
<li>Is this UI interaction comfortable and easy to perform?</li>
</ul>
<p>One area that can benefit from anthropometry data is UI interaction.  When you think about UI interaction with Kinect you’ve got to picture it as a real world space (box, sphere, cylinder, other) located somewhere around the body that you’re mapping the hand position in that space to the 2D or 3D UI coordinate plane.</p>
<p>When determining the size and location of this real world space and how it maps user hand locations onto the UI coordinate system the largest question you need to consider is:</p>
<h4>Where will they be most comfortable?</h4>
<p>Generally speaking you want a space that minimizes upper arm movement – as that is much more strenuous compared to forearm movements.</p>
<p>However, since the 3d position we’re mapping into our 2d/3d UI coordinate system varies based upon user skeleton size we can’t choose a single set of real world dimensions that will work for everyone.  We’ll have to make educated guesses about the size and location of our real world UI coordinate frame based on size and location of the users bones.</p>
<h4>So how does anthropometry fit in?</h4>
<p>Because the skeleton you get from Kinect – and other SDKs can be unreliable in certain poses you often find yourself heavily filtering any kind of data you’re tracking about the user.  Especially things like the user’s arm length – <strong>which can vary dramatically over a session</strong>.</p>
<p>So one thing I prefer to do is use anthropometry tables to ensure a more consistent size and location and doesn’t fluctuate as much as the user’s skeleton.  Using anthropometry tables we can estimate the users arm length or hand size based on other bones in their body, bones that are more stable in your skeleton SDK of choice (Kinect, OpenNI, Iisu, Omek…etc).</p>
<p>You can also use anthropometry tables to estimate the size of body parts that the skeleton SDK you’re using doesn’t provide – such as the size of the users hand.</p>
<h4>But where do you find that kind of anthropometry data?</h4>
<p>Luckily such a resource has already been painstakingly cataloged for us by the FAA – <a href="http://files.nickdarnell.com/hfe_design_guide.pdf">The Human Factors Design Guide</a>.  The HFDG was put together so that planes could be constructed so that almost anyone would fit and be able to operate anything from their seat.</p>
<p>The anthropometry data that’s valuable to us starts in chapter 14, page 791.  For example, these lovely tables from page 818 show the functional reach and the extended functional reach of men and women broken down by population percentiles.</p>
<p><a href="http://altdevblogaday.com/wp-content/uploads/2011/11/hfdg_reach.png"><img class="aligncenter" style="margin: 5px auto;padding-left: 0px;padding-right: 0px;float: none;padding-top: 0px;border-width: 0px" src="http://altdevblogaday.com/wp-content/uploads/2011/11/hfdg_reach_thumb.png" alt="hfdg_reach" width="450" height="330" border="0" /></a></p>
<p><a href="http://www.nickdarnell.com/2011/11/kinect-anthropometry-and-you/" target="_blank">Kinect, Anthropometry and You @ nickdarnell.com</a></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.altdevblogaday.com/2011/11/08/kinect-anthropometry-and-you/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Making Your Files Merge Friendly</title>
		<link>http://www.altdevblogaday.com/2011/10/24/making-your-files-merge-friendly/</link>
		<comments>http://www.altdevblogaday.com/2011/10/24/making-your-files-merge-friendly/#comments</comments>
		<pubDate>Mon, 24 Oct 2011 14:05:26 +0000</pubDate>
		<dc:creator>Nick Darnell</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tools]]></category>
		<category><![CDATA[Merging]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[Revision Control]]></category>
		<category><![CDATA[tools]]></category>

		<guid isPermaLink="false">http://altdevblogaday.com/?p=18943</guid>
		<description><![CDATA[<p>Merging is a fact of life for most of us.  Eventually two users touch the same files and a conflict must be resolved.  For programmers merging is a daily activity, but what about the content creators?</p>
<p><a href="http://www.altdevblogaday.com/2011/10/24/making-your-files-merge-friendly/" class="more-link">Read more on Making Your Files Merge Friendly&#8230;</a></p>
]]></description>
				<content:encoded><![CDATA[<p>Merging is a fact of life for most of us.  Eventually two users touch the same files and a conflict must be resolved.  For programmers merging is a daily activity, but what about the content creators?</p>
<p>If two artists touch the same level, have your tools programmers made enough of an effort to make merging possible and if possible as painless as can be?</p>
<p>Here’s a handy checklist,</p>
<h3>1 &#8211; Use XML/JSON (or some other text based file format)</h3>
<p>It’s slower, it’s bigger but it’s going to make merging possible.  If your level files are binary blobs merging without a custom tool just isn’t going to be possible.  Using XML or JSON are the simplest text based alternatives because there are already many libraries for reading and writing them.</p>
<h3>2 &#8211; For XML – Give Each Attribute a Line</h3>
<p>If you’re using XML, you should make sure to write out the files such that every element and every attribute receives its own line.  If you don’t do this it will make conflicts a lot more likely if two users touch the same object.</p>
<p>If you’re using JSON, having each attribute on its own line is the norm unless the library is attempting to keep the text compact.</p>
<p>Here’s a quick example, note that after separating the attributes the merge tool handles the conflict correctly while it fails to do so when they are on the same line.</p>
<p align="center"><a href="http://altdevblogaday.com/wp-content/uploads/2011/10/nonewline.png"><img style="margin: 5px;padding-left: 0px;padding-right: 0px;padding-top: 0px;border-width: 0px" src="http://altdevblogaday.com/wp-content/uploads/2011/10/nonewline_thumb.png" alt="nonewline" width="500" height="190" border="0" /></a><br />
Figure 1 &#8211; Attributes all on the same line</p>
<p align="center"><a href="http://altdevblogaday.com/wp-content/uploads/2011/10/newline.png"><img style="margin: 5px;padding-left: 0px;padding-right: 0px;padding-top: 0px;border-width: 0px" src="http://altdevblogaday.com/wp-content/uploads/2011/10/newline_thumb.png" alt="newline" width="500" height="248" border="0" /></a><br />
Figure 2 &#8211; Attributes on different lines</p>
<p>If you’re using .Net this can be achieved very easily by changing the default XmlWriterSettings with the NewLineOnAttribute property set to true.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">var</span> settings <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> XmlWriterSettings<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    NewLineOnAttributes <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">true</span>
<span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008000;">&#40;</span>XmlWriter writer <span style="color: #008000;">=</span> XmlWriter<span style="color: #008000;">.</span><span style="color: #0000FF;">Create</span><span style="color: #008000;">&#40;</span>filestream, settings<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #008080; font-style: italic;">// Write out document...</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<h3>3 &#8211; Maintain Order</h3>
<p>Always write out the order of the data the same.  This one is pretty easy, the only real pitfall is to make sure your data structure and undo/redo system are working correctly.  For example, if you happen to store your objects in a list and someone removes and object at the front, but when it’s undone it’s inserted at the end of the list (Even if the UI doesn’t reflect this) this could affect your output order.</p>
<h3>4 &#8211; Don’t Add New Objects At The End</h3>
<p>When you go to save the entities don’t write them out in the order they were created.  This will definitely result in a merge conflict.  Because it ensures that if two users edit the file and both add an entity they’ll both show up in the same location and confuse the merge tool.</p>
<p>One thing you could do is to sort them based on a GUID that is stored as part of the objects data.  Sorting based on GUID ensures a lower probability of collisions occurring when two users both add objects.</p>
<p>Alternatively a sorting based on a string containing the machine name of the original creator of the object is another idea.  It would ensure every user touching the file would be inserting to their own section of the file instead of everyone inserting to the tail.</p>
<p><a href="http://www.nickdarnell.com/2011/10/making-your-files-merge-friendly/" target="_blank">Making Your Files Merge Friendly @ nickdarnell.com</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.altdevblogaday.com/2011/10/24/making-your-files-merge-friendly/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Robust Inside and Outside Solid Voxelization</title>
		<link>http://www.altdevblogaday.com/2011/09/24/robust-inside-and-outside-solid-voxelization/</link>
		<comments>http://www.altdevblogaday.com/2011/09/24/robust-inside-and-outside-solid-voxelization/#comments</comments>
		<pubDate>Sat, 24 Sep 2011 18:41:20 +0000</pubDate>
		<dc:creator>Nick Darnell</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tools]]></category>
		<category><![CDATA[Visual Arts]]></category>
		<category><![CDATA[Hierarchical Z-Buffer]]></category>
		<category><![CDATA[Occlusion Culling]]></category>
		<category><![CDATA[opengl]]></category>
		<category><![CDATA[Rendering]]></category>
		<category><![CDATA[Solid Voxelization]]></category>
		<category><![CDATA[Voxel]]></category>
		<category><![CDATA[Voxelization]]></category>

		<guid isPermaLink="false">http://altdevblogaday.com/?p=17300</guid>
		<description><![CDATA[<p><a href="http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.14.6993" target="_blank"><img style="margin: 5px;padding-left: 0px;padding-right: 0px;float: right;padding-top: 0px;border-width: 0px" src="http://altdevblogaday.com/wp-content/uploads/2011/09/complete_polygonal_scene_voxelization.png" alt="complete_polygonal_scene_voxelization" width="170" height="240" align="right" border="0" /></a></p>
<p>While wrapping up my post for generating simplified occluders for <a href="http://altdevblogaday.com/2011/06/26/hierarchical-z-buffer-occlusion-culling-generating-occlusion-volumes/" target="_blank">Hierarchical Z-Buffer Occlusion Culling</a>, I was pointed to a paper called <a href="http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.14.6993" target="_blank">Complete Polygonal Scene Voxelization</a>.  Afterwards I found time to read it thoroughly and implement it as a replacement for my existing ray casting based solid voxelization method.</p>
<p><a href="http://www.altdevblogaday.com/2011/09/24/robust-inside-and-outside-solid-voxelization/" class="more-link">Read more on Robust Inside and Outside Solid Voxelization&#8230;</a></p>
]]></description>
				<content:encoded><![CDATA[<p><a href="http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.14.6993" target="_blank"><img style="margin: 5px;padding-left: 0px;padding-right: 0px;float: right;padding-top: 0px;border-width: 0px" src="http://altdevblogaday.com/wp-content/uploads/2011/09/complete_polygonal_scene_voxelization.png" alt="complete_polygonal_scene_voxelization" width="170" height="240" align="right" border="0" /></a></p>
<p>While wrapping up my post for generating simplified occluders for <a href="http://altdevblogaday.com/2011/06/26/hierarchical-z-buffer-occlusion-culling-generating-occlusion-volumes/" target="_blank">Hierarchical Z-Buffer Occlusion Culling</a>, I was pointed to a paper called <a href="http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.14.6993" target="_blank">Complete Polygonal Scene Voxelization</a>.  Afterwards I found time to read it thoroughly and implement it as a replacement for my existing ray casting based solid voxelization method.</p>
<p>The problem with the solid voxelization technique I was using previously was that it used ray casting; making it impossible to perform solid voxelization unless the mesh is watertight in addition to having no anomalies like intersecting geometry.</p>
<p>However, that restriction makes it an unrealistic solution in the real world because game art typically has holes in the locations players never see; such as the bottom cap on a building, which is rarely modeled.</p>
<h3>The New Solution</h3>
<p>The <a href="http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.14.6993" target="_blank">Complete Polygonal Scene Voxelization</a> paper’s solution to voxelizing a scene is pretty clever; It applies a heuristic model to the problem of determining the inside/outside status of each voxel or octree cell.  Allowing it to overcome the problem of holes and intersecting geometry making it suitable as a real world solution.</p>
<h3>How It Works</h3>
<p><a href="http://altdevblogaday.com/wp-content/uploads/2011/09/bunny_octree_3.png" target="_blank"><img style="margin: 5px;padding-left: 0px;padding-right: 0px;float: right;padding-top: 0px;border-width: 0px" src="http://altdevblogaday.com/wp-content/uploads/2011/09/bunny_octree_3_thumb.png" alt="bunny_octree_3" width="200" height="187" align="right" border="0" /></a></p>
<p>You can download the paper and read it for yourself, but let me go ahead and summarize the algorithm for brevity&#8217;s sake so that the rest of the article makes sense.</p>
<p>The algorithm takes place in 3 stages:</p>
<ol>
<li>Create Octree</li>
<li>Find Seed Cell</li>
<li>Propagate Seed Cell</li>
</ol>
<h5>Create Octree</h5>
<p>First you create an octree around the mesh that continues to subdivide each cell until either the cell no longer intersects with any triangle or a maximum depth is reached.  A typical maximum octree depth that will work for most meshes is 5.  If the mesh has some exceptionally thin walls that you want the cells to be small enough to fill you may need to go as high as 7 or 8.</p>
<p>I was having some problems with the GPU AABB/Triangle overlap test I used for voxelization in the <a href="http://altdevblogaday.com/2011/06/26/hierarchical-z-buffer-occlusion-culling-generating-occlusion-volumes/" target="_blank">Hierarchical Z-Buffer Occlusion Culling</a> article and so I ended up porting the <a href="http://fileadmin.cs.lth.se/cs/Personal/Tomas_Akenine-Moller/code/" target="_blank">Möller implementation of AABB/Triangle overlap test</a> to C# and just used it instead.</p>
<p>Also if you ever need to lookup how an intersection is performed I highly recommend the <a href="http://www.realtimerendering.com/intersections.html" target="_blank">gigantic matrix of intersections over at realtimerendering.com</a>.  It was a handy resource since I don’t keep the algorithm for AABB/Triangle overlap stored in my brain.</p>
<p>After you’ve created the octree we need to process each cell that wasn’t intersecting with a triangle to determine if it is inside or outside.</p>
<h5>Find Seed Cell</h5>
<p>Before we can determine if a cell is inside or outside the mesh we need to find a seed cell.  The seed cell is sort of the ground truth example cell that we use to propagate its status to the other cells that it can see.  The seed cell’s status is determined by rendering a cube map centered inside the cell with the near plane placed at the cell edge.</p>
<p>When rendering each side of the cube map, you render the scene such that all front facing polygons are blue and all back facing polygons are red.  You then read back each cube map surface from the GPU and determine the percentage of red and blue pixels seen at each face.</p>
<p>If at least 4 sides of the cube map contain red pixels, the cell is determined to be inside the mesh.</p>
<p>The paper says that NO red pixels can be seen for a seed cell to be determined to be outside however I found this problematic since occasionally a red pixel can be seen just through tiny rendering artifacts.</p>
<p>So I feel a better solution is one like the following:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="csharp" style="font-family:monospace;">MIN_INSIDE_FACES <span style="color: #008000;">=</span> <span style="color: #FF0000;">4</span><span style="color: #008000;">;</span>
MIN_INSIDE_PERCENTAGE <span style="color: #008000;">=</span> 0<span style="color: #008000;">.</span>03f<span style="color: #008000;">;</span>
&nbsp;
<span style="color: #6666cc; font-weight: bold;">int</span> cubemap_sides_seeing_inside <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> i <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> i <span style="color: #008000;">&lt;</span> <span style="color: #FF0000;">6</span><span style="color: #008000;">;</span> i<span style="color: #008000;">++</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    RenderCubeMapSide<span style="color: #008000;">&#40;</span>i<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #6666cc; font-weight: bold;">float</span> backfacePercentage <span style="color: #008000;">=</span> CalculateBackfacePercentage<span style="color: #008000;">&#40;</span>i<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>backfacePercentage <span style="color: #008000;">&gt;</span> MIN_INSIDE_PERCENTAGE<span style="color: #008000;">&#41;</span>
        cubemap_sides_seeing_inside<span style="color: #008000;">++;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>cubemap_sides_seeing_inside <span style="color: #008000;">&gt;=</span> MIN_INSIDE_FACES <span style="color: #008000;">||</span> cubemap_sides_seeing_inside <span style="color: #008000;">==</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>cubemap_sides_seeing_inside <span style="color: #008000;">&gt;=</span> MIN_INSIDE_FACES<span style="color: #008000;">&#41;</span>
        cell<span style="color: #008000;">.</span><span style="color: #0000FF;">Status</span> <span style="color: #008000;">=</span> CellStatus<span style="color: #008000;">.</span><span style="color: #0000FF;">Inside</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">else</span> <span style="color: #008080; font-style: italic;">// cubemap_sides_seeing_inside == 0</span>
        cell<span style="color: #008000;">.</span><span style="color: #0000FF;">Status</span> <span style="color: #008000;">=</span> CellStatus<span style="color: #008000;">.</span><span style="color: #0000FF;">Outside</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">// Propagate cell status...</span>
<span style="color: #008000;">&#125;</span>
<span style="color: #0600FF; font-weight: bold;">else</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #008080; font-style: italic;">// Unable to solve status exactly.</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>Where you don’t count a face as inside unless at least 3% of the total red and blue pixels are red.  The percentage is just something I picked out of thin air, it feels like a number small enough to be easily overcome by any truly inside cube face, but high enough to allow me to ignore tiny artifacts.</p>
<h5>Propagate Seed Cell</h5>
<p>The last step is to propagate the seed cell’s status to other cells.  After classifying a seed cell you need to test every unknown cell against each the depth map and frustum of each cube map surface.</p>
<p>You’re performing a test to see if any of the 8 corners of the octree cell when projected into the camera space of each cube face are closer than the depth value at that pixel.  If it is, then then the entire octree cell likely visible.</p>
<p>If the cell is visible from the seed cell, then in all likelihood the cell has the same status as the seed cell.  However because having holes means 2 seed cells (one inside and one outside) can potentially see the same cell you want several seed cells to confirm the status of a cell before committing to it.</p>
<p>So once you’ve determined a cell is visible from the seed cell you’ll increment a counter on the cell for that status.  Once one of the statuses reaches a threshold, like for example 16 you’ll change the status of the cell from Unknown to whatever status counter overcame the threshold and no longer process that cell.</p>
<p>It should be noted that only seed cells propagate their status.  Cells that you propagate to do not propagate their own status.</p>
<h5>Repeat</h5>
<p>After you’ve found a seed cell and propagated its status you’ll continue to repeat finding a seed cell and propagating the seed cell until all cells have a status of inside, outside or intersecting.  You can rarely end up with some cells whose status simply can’t be determined so make sure your code can handle that scenario and not loop forever.</p>
<h3>Improvements</h3>
<p>While implementing the paper I made some additional improvements to the proposed solution.  I sped up the process by taking advantage of hardware improvements to render the scene using a single pass.  I also improved the conservativeness of the algorithm in situations where you&#8217;re using square voxels.  When a mesh is wider than it is tall in those sitations there will be padding below the mesh; if the bottom of the mesh is uncapped it can lead to inside cells &#8216;leaking&#8217; their status outside.</p>
<h5>Single Pass Rendering</h5>
<p>The paper was published back in 2002 and due to limitations at the time the simplest method of rendering front faces one color and back faces another was to render the scene twice and flip the winding order and the color of the triangles being rendered.  However this method is slower than just using a simple pixel shader to change the color of front/back faces.</p>
<p>In OpenGL you can use <a href="http://www.opengl.org/sdk/docs/manglsl/xhtml/gl_FrontFacing.xml" target="_blank">gl_FrontFacing</a> and in DirectX 11 you can use <a href="http://msdn.microsoft.com/en-us/library/bb509647%28v=vs.85%29.aspx" target="_blank">SV_IsFrontFace</a>.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="glsl" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #000066;">&#40;</span><span style="color: #000066;">&#41;</span>
<span style="color: #000066;">&#123;</span>
     <span style="color: #551111;">gl_FragColor</span> <span style="color: #000066;">=</span> <span style="color: #551111;">gl_FrontFacing</span> <span style="color: #000066;">?</span> <span style="color: #000066; font-weight: bold;">vec4</span><span style="color: #000066;">&#40;</span><span style="color: #0000ff;">0</span><span style="color: #000066;">,</span><span style="color: #0000ff;">0</span><span style="color: #000066;">,</span><span style="color: #0000ff;">1</span><span style="color: #000066;">,</span><span style="color: #0000ff;">1</span><span style="color: #000066;">&#41;</span> <span style="color: #000066;">:</span> <span style="color: #000066; font-weight: bold;">vec4</span><span style="color: #000066;">&#40;</span><span style="color: #0000ff;">1</span><span style="color: #000066;">,</span><span style="color: #0000ff;">0</span><span style="color: #000066;">,</span><span style="color: #0000ff;">0</span><span style="color: #000066;">,</span><span style="color: #0000ff;">1</span><span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span>
<span style="color: #000066;">&#125;</span></pre></td></tr></table></div>

<h5>Intersect Mesh Bounds and Clip To Bounds</h5>
<p>One problem I found is that when a mesh (like a building) is uncapped at its base but is wider than it is tall there will be several cells below the base of the mesh.  Cells that will have the status of inside spread to them, even though a human could easily see those cells are outside.</p>
<p>So one improvement I ended up adding is that when testing for triangle intersection, you should also test against intersection against the mesh bounds.  Additionally you immediately mark a cell as Outside if is outside the mesh bounds, since it simply is not possible for that cell to be inside the mesh, but don’t treat that cell like it’s a seed cell; just mark it as outside and move on.</p>
<p>I needed some real game art to properly test the solution so I exported a roof structure from the Necropolis map from <a href="http://www.udk.com/" target="_blank">UDK</a>’s UTGame sample.  Here you can really see the difference it makes to clip to the bounds of the mesh.  Note how many additional voxel/octree cells (purple lines) are determined to be ‘inside’ because of how many backfaces (red triangles) they can see.</p>
<p align="center"><a href="http://altdevblogaday.com/wp-content/uploads/2011/09/udk_necropolis_roof_not_fixed.png" target="_blank"><img style="margin: 5px;padding-left: 0px;padding-right: 0px;padding-top: 0px;border-width: 0px" src="http://altdevblogaday.com/wp-content/uploads/2011/09/udk_necropolis_roof_not_fixed_thumb.png" alt="udk_necropolis_roof_not_fixed" width="600" height="318" border="0" /></a><br />
Figure 1 – Roof Inner Voxelization Not Bounded (Before)</p>
<p align="center"><a href="http://altdevblogaday.com/wp-content/uploads/2011/09/udk_necropolis_roof_fixed.png" target="_blank"><img style="margin: 5px;padding-left: 0px;padding-right: 0px;padding-top: 0px;border-width: 0px" src="http://altdevblogaday.com/wp-content/uploads/2011/09/udk_necropolis_roof_fixed_thumb.png" alt="udk_necropolis_roof_fixed" width="600" height="318" border="0" /></a><br />
Figure 2 – Roof Inner Voxelization Bounded (After)</p>
<h3 align="left">Future Improvements</h3>
<p>When the cube map for each seed cell is processed it’s read back from the GPU and each pixel is checked on the CPU.  This is wildly inefficient when all we care about is the percentage of red to blue pixels on each face.</p>
<p>So that processing can be moved to OpenCL to improve the performance significantly.  I would prefer to have all cells be seed cells since that makes it easier to define the rules of what cells are inside vs. outside.  Allowing the cells to propagate has a higher potential to cause problems I suspect on meshes with very nasty artifacts.  Giving each cell the ability to individually determine their status will be more stable and more predictable.</p>
<p>Currently my cube map rendering is performed in 6 passes.  Shifting over to a single pass method using the geometry shader will likely add additional speed improvements, but I don’t know for sure.</p>
<p>If I move enough of the processing to the GPU it may allow me to make more cells seed cells (perhaps all?) and still maintain an acceptable performance footprint for tool time usage.  For offline usage though this method is already very acceptable (a few seconds for an average mesh and a maximum depth of 5) even with all the CPU read-backs I’m performing.</p>
<h3>Sample Code</h3>
<p>I’m still working on an improved version of the Generating Occluders for Hierarchical Z-Buffer Occlusion Culling sample.  So the code is a bit tied up a the moment.  However the next post I do on generating the occluders will contain it, which should be soon.</p>
<p><a href="http://www.nickdarnell.com/?p=1678" target="_blank">Robust Inside and Outside Solid Voxelization @ nickdarnell.com</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.altdevblogaday.com/2011/09/24/robust-inside-and-outside-solid-voxelization/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Trip Report: Gamefest 2011 – Seattle</title>
		<link>http://www.altdevblogaday.com/2011/09/09/trip-report-gamefest-2011-%e2%80%93-seattle/</link>
		<comments>http://www.altdevblogaday.com/2011/09/09/trip-report-gamefest-2011-%e2%80%93-seattle/#comments</comments>
		<pubDate>Fri, 09 Sep 2011 19:00:57 +0000</pubDate>
		<dc:creator>Nick Darnell</dc:creator>
				<category><![CDATA[Education]]></category>
		<category><![CDATA[General Interest]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[conference]]></category>
		<category><![CDATA[DirectX 11]]></category>
		<category><![CDATA[Gamefest]]></category>
		<category><![CDATA[graphics]]></category>
		<category><![CDATA[Kinect]]></category>

		<guid isPermaLink="false">http://altdevblogaday.com/?p=16175</guid>
		<description><![CDATA[<p>I managed make it out to Seattle this year for Gamefest and figured I&#8217;d share my thoughts on some of the different presentations I saw. They are not available yet, but it looks like Microsoft is going to be posting the slides/audio for the different presentations <a href="http://www.microsoftgamefest.com/seattle_conferencedetails.htm" target="_blank">here</a> soon.</p>
<p><a href="http://www.altdevblogaday.com/2011/09/09/trip-report-gamefest-2011-%e2%80%93-seattle/" class="more-link">Read more on Trip Report: Gamefest 2011 – Seattle&#8230;</a></p>
]]></description>
				<content:encoded><![CDATA[<p>I managed make it out to Seattle this year for Gamefest and figured I&#8217;d share my thoughts on some of the different presentations I saw. They are not available yet, but it looks like Microsoft is going to be posting the slides/audio for the different presentations <a href="http://www.microsoftgamefest.com/seattle_conferencedetails.htm" target="_blank">here</a> soon.</p>
<h3>Tiled Resources for Xbox 360 and Direct3D 11 &#8211; Matt Lee</h3>
<p>This talk was about mega-texturing in DirectX 11/Xbox 360. Matt Lee was showing a new DirectX SDK sample that&#8217;s coming in the next SDK release giving a reference implementation of a mega-texturing run-time. I&#8217;ve only skimmed mega-texturing papers so I got a lot out of this talk since he walked through all the steps in the run-time.</p>
<p>The sample shows off how you begin by creating different tiles for different resource formats. Each pool is dedicated to a different texture format. The tiles in the pool are all the same size; However the tiles may vary in size depending upon the texture format to maximize cache efficiency. When you render the scene you have a shader that can write out texture look-up failures. When the UV coordinates and mip level are not found to be resident in memory a failure is added to this list. After the shader completes you read back the failures and proceed to load the tiles that will fit in your established pools.</p>
<p>Unlike most texture streaming systems you&#8217;re not loading an entire mip level or the entire mip chain of the texture. You&#8217;re only ever loading into the tiles a sub-region of a texture (like a 64&#215;64 pixel region), which overcomes one common texture streaming problem, texture memory fragmentation. Because the tile pools you create are never deallocated you don&#8217;t have to worry about fragmenting your texture memory because of different sized textures being streamed in and out.</p>
<p>Now the sample is not without its short comings, but that is mostly due to hardware limitations. Ideally the virtual texture system would be transparent, you wouldn&#8217;t need to write a shader that recorded look-up failures. The GPU and DirectX would simply report when a failure occurred and allow you to handle it. Maybe some day&#8230;</p>
<h3>Gesture Detection Using Machine Learning &#8211; Claude Marais</h3>
<p>If you have ever been interested in machine learning this is a worthwhile presentation to check out when the slides are posted. Claude Marais talked about a case study they performed to try and use machine learning to detect a Punch and a Kick. For their experiment they used <a href="http://en.wikipedia.org/wiki/AdaBoost" target="_blank">Adaboost</a> which is a machine learning technique that combines thousands of weak classifiers that &#8216;boost&#8217; each other and provide you with a high degree of accuracy in the results.</p>
<p>The classifiers are all extremely simple things, for example you may have a classifier like:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>elbow_joint_angle <span style="color: #000080;">&gt;</span> ANGLE<span style="color: #008000;">&#41;</span>
    <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
<span style="color: #0000ff;">return</span> <span style="color: #000040;">-</span><span style="color: #0000dd;">1</span><span style="color: #008080;">;</span></pre></td></tr></table></div>

<p>Then simply create a macro and have 180 variants of this classifier one for each ANGLE. If you can imagine all the different things you could measure about the skeleton, creating simple variants of the kernels for each of the possible test cases will explode the number of weak classifiers you have; Claude had around 21,000 weak classifiers for his system.</p>
<p>The training phase looks at labeled data sets to know what examples of punches look like (positive examples) and what -not- punches look like (negative examples). It uses the +1/-1 scores each weak classifier provides to determine the weights to apply to each classifier. After it has determined the best weak classifiers to detect a punch and not detect a negative example as a punch on accident you can use the classifiers at run-time with the weights applied to detect a punch.</p>
<p>The results were undeniable; they had a demo setup the the expo area that was really good at detecting a punch and kick.</p>
<p><del>The only real drawback to this solution is the data collection; they needed something on the order of 70,000 examples of punches and 7x that in negative not a punch examples before the training produced accuracies over 90-95% from the chart they had; if my memory is correct.</del></p>
<p><del></del><del></del>In training the system they had 70,000 frames worth of recorded training data.  The actual number of recorded punches used to train the system was 25 different people doing 10 punches, so around 250 punch examples.  Then they had about 7x that number in negative training examples, which might be things like waves, or other actions that SVM can use to differentiate between random movement and an intentional punch. (Thanks to Claude for clarifying this)</p>
<h3>Kinect and Kids: Pitfalls and Pleasantries &#8211; Deborah Hendersen</h3>
<p>If you had asked me to make a Kinect game for kids (ages 3-6) before seeing this presentation I likely would&#8217;ve designed something with a dumb-me as the target audience. What I quickly realized is how wrong I would&#8217;ve been to make that assumption. At that stage of development kids are not capable of interacting with games I&#8217;m used to playing.</p>
<p>Something as simple as a menu of options is an impossibility since they are illiterate. How many games have you seen that you could play without knowing how to read?</p>
<p>When interacting with an onscreen character, the kids ignore social norms of waiting for the person to finish talking. They may just jump the gun if they already know what is expected and get frustrated if they can&#8217;t do it when they want to.</p>
<p>Kids are distracted very easily and will make their own games out of game behavior. Deborah mentioned one story where a kid stopped playing the game because he realized he could get the game to react to leaving the play area and Kinect could no longer detect him the game would do something. So he made up his own game of jumping in and out of the play area to activate this condition; utterly boring for adults, completely entertaining for this kid.</p>
<p>You almost have to design the game like passive experience like a children&#8217;s TV show. Where on TV because there is no feedback, the TV show host asks the kid, &#8220;Can you find _______?&#8221; and the kid at home says something, and expecting this the show simply pauses while he waits for the response. The game has to function in essentially the same way, regardless of the kid participating in the expected fashion the game has to move forward. If it functions like a state machine that requires proper actions to move forward the kid may become bored and simply want to move on. If the game refuses to let them move on, they&#8217;ll just walk away.</p>
<p>I really enjoyed this presentation because it was very clear how difficult the problem space is and it was interesting to hear how they tried to solve each one.</p>
<h3>Kinect Hands: Finger Tracking and Voxel UI &#8211; Abdulwajid Mohamed and Tony Ambrus</h3>
<p>This presentation was broken into two completely different parts, the first part was on finger tracking with Kinect. This is one area I&#8217;ve been playing around in for awhile so it was interesting to see someone else&#8217;s attempt to solve the problem. Because the Kinect is a structured light depth camera you don&#8217;t necessarily have depth at each pixel like you would on a time of flight depth camera. Structured light cameras build a topology of depth using the light pattern it projects into the scene, viewed from a different angle it can discern depth, but a single dot does not give you depth. It connects groups of them when determining the depth of a surface. This means that even though your hand can be seen by Kinect, the further you back away from the sensor, the more like a mitten your hand becomes. The gaps between your fingers disappear until they are just clumps on your wrist.</p>
<p>Because of this limitation you can&#8217;t go past 10 feet, there simply isn&#8217;t enough data. Ideally the user is at 6 feet or closer, past 6 feet the accuracy begins to break down.</p>
<p>The way Microsoft tackled the problem was to first capture lots of hand examples and then to train an SVM (Support Vector Machine) against a curvature analysis of the hands. So once you know all the pixels that make up a persons hand you find the points on the hand that result in the largest changes in curvature. On an open hand these curves are your fingers and if you&#8217;re close enough to the camera that it can see the gaps between fingers it&#8217;s a very large change in curvature. A closed hand has more or less a uniform curvature change viewed from any angle. By training the SVM against a set of closed hand curvature examples vs. open hand curvature examples they were able to get pretty accurate results at about the 6-8 foot range for an adult, 5-7 feet for kids.</p>
<p>Because the detector is instantaneous i.e. it can tell you in a single frame is the hand open or closed, you need some something to counteract a single/couple misinterpreted frame. So they trained an HMM (Hidden Markov Model) on examples of a flaky transition where the system is quickly switching between 2 states because the hand is at an odd orientation confusing the SVM; I thought it was an interesting solution to the problem. I&#8217;ve only ever tried something simple like requiring 3 contiguous frames of agreement to have a state change.</p>
<p>The second half of the presentation was on a 3D (not stereoscopic) UI for Kinect. One of the problems with navigating a &#8216;push to click&#8217; interface is that it&#8217;s hard to correct for user drift. When a user pushes forward they may do several things,</p>
<ul>
<li>Push toward the TV</li>
<li>Push toward the sensor</li>
<li>Push forward (wherever forward happens to be at that moment in time)</li>
</ul>
<p>Depending upon what you&#8217;re expecting them to do there&#8217;s going to be drift away from the thing on the screen they are trying to click. To attempt to correct this Abdulwajid presented a UI where the hands are visualized as voxelized clumps of boxes in a 3D environment with 3D buttons that could be mashed. Seeing the hand in the same space as the button appeared to make it much easier to perform the click.</p>
<p>One thing I noticed that was not called out was his use of 2 directional shadow casting lights. By having 2 directional lights facing each other both casting shadows, the resulting effect is a focal point. As the hand gets closer to a surface the eye perceives the two shadows heading towards each other and can see the point where they will meet. I thought that was and additional powerful indicator of where your hand was moving in the space and made it much easier to correct drift.</p>
<p><a href="http://www.nickdarnell.com/2011/09/trip-report-gamefest-2011-seattle/" target="_blank">Trip Report: Gamefest 2011 – Seattle @ nickdarnell.com</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.altdevblogaday.com/2011/09/09/trip-report-gamefest-2011-%e2%80%93-seattle/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SWIG: Casting Revisited</title>
		<link>http://www.altdevblogaday.com/2011/08/10/swig-casting-revisited/</link>
		<comments>http://www.altdevblogaday.com/2011/08/10/swig-casting-revisited/#comments</comments>
		<pubDate>Wed, 10 Aug 2011 12:26:25 +0000</pubDate>
		<dc:creator>Nick Darnell</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[SWIG]]></category>

		<guid isPermaLink="false">http://altdevblogaday.com/?p=14114</guid>
		<description><![CDATA[<p>A while back I wrote <a href="http://altdevblogaday.com/2011/05/27/swig-and-a-miss/" target="_blank">SWIG and a Miss</a>, which is a post about several of the problems I’ve encountered with SWIG.  At that time I didn’t have a solution for dealing with down casting – the process of casting from a base class to a more derived class.</p>
<p><a href="http://www.altdevblogaday.com/2011/08/10/swig-casting-revisited/" class="more-link">Read more on SWIG: Casting Revisited&#8230;</a></p>
]]></description>
				<content:encoded><![CDATA[<p>A while back I wrote <a href="http://altdevblogaday.com/2011/05/27/swig-and-a-miss/" target="_blank">SWIG and a Miss</a>, which is a post about several of the problems I’ve encountered with SWIG.  At that time I didn’t have a solution for dealing with down casting – the process of casting from a base class to a more derived class.</p>
<p>I ended up coming up with a solution that I thought would be good to do a post on, since there doesn’t seem to be much out there on SWIG and these types of problems.</p>
<h3>Some Background</h3>
<p>For those unfamiliar with why it’s difficult, let me explain.  When your API returns a native pointer to be wrapped, of lets say class Foo, and your function returns Foo* but the instance it returns is actually the more derived version Bar.  Sadly SWIG has no way of knowing this, so the object it creates in C# would be a C# Foo class.  Therefore, if you tried to cast the C# Foo to the more derived C# Bar you’d be unable to because as far as .Net is concerned the instance of Foo is just a Foo and nothing more.</p>
<p>Alright now that you understand the problem, lets talk about how we can solve it.</p>
<h3>How We Can Solve It</h3>
<p>The solution to the problem can be solved in essentially 3 ways,</p>
<h4>1 // Walk Away</h4>
<p>Simply don&#8217;t do anything that would require it.  Don&#8217;t expose classes that users will ever need to downcast to for any reason.  If this option is available to you, take it.  This however means that your base class needs to support all the functionality a user will ever need, something not always possible or desirable.  But if you can, do it, because once you get to script land, down casting becomes a expensive process.</p>
<h4>2 // Manual Cast Wrapper</h4>
<p>Write or generate C++ functions for every downcast that you would like to perform and SWIG those functions.  This would allow you to take object Foo as a parameter, returns Bar, does the dynamic cast in C++ and returns the pointer to Bar.</p>
<p>This option is manpower intensive and is truly brute forcing a solution.  It also causes potential holes where nasty aliasing occurs and can become a real problem if the source object is ever deleted.  Imagine an API with a factory function that returns object Foo, but Foo is actually Bar due to the way the factory works.  You need to access a Bar specific only function on the object so you downcast Foo to Bar.</p>
<p>Now in C++ these objects are the same object, but SWIG doesn&#8217;t know this, so two completely different C# objects are created one wrapping the native pointer to Foo, and one wrapping the native pointer to Bar.  If the SWIG layer was properly authored, the factory function notes that the object returned was created with new memory and thus SWIG is responsible for deleting the object.</p>
<p>So we call our Bar specific function and we decide to hang onto Bar and we let Foo on the stack pass out of scope, because why hang onto it, it&#8217;s the same object as Bar…right?  But Foo was responsible for managing the memory for the common native object they both point to.  So the underlying native object is destroyed when the garbage collector runs next after Foo is no longer referenced and now our Bar object points to totally bogus memory.</p>
<h4>3 // Custom Solution</h4>
<p>The solution I ended up coming up with requires you to have a custom RTTI system that allows you to check if an object is a specific type by name.</p>
<p>The first step is to extend the C# wrapper for your base class with RTTI information.  If you don’t have a base RTTI object this will be a bit harder.  The reason we have a new m_castedSource member variable is to solve the aliasing problem.  After the cast we can store the source here so that we never have to worry about the garbage collector accidentally cleaning up memory we’re still using.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">%</span>typemap<span style="color: #008000;">&#40;</span>cscode<span style="color: #008000;">&#41;</span> YourBaseRTTIObject
<span style="color: #008000;">%</span><span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">internal</span> YourBaseRTTIObject m_castedSource<span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">bool</span> IsKindOf<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #008080; font-style: italic;">// Use your C++ RTTI system to test if this wrapped C# object's</span>
        <span style="color: #008080; font-style: italic;">// native object is a 'typeof(T).Name'</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">%</span><span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>The next step is to create a function that can create the C# method needed to factory the C# object. We could use reflection, but that method is slow and we can do a far better job by emitting IL at runtime to specifically initialize one type and just cache those created methods.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">static</span> Dictionary<span style="color: #008000;">&lt;</span>Type, Func<span style="color: #008000;">&lt;</span>IntPtr, <span style="color: #6666cc; font-weight: bold;">bool</span>, <span style="color: #6666cc; font-weight: bold;">object</span><span style="color: #008000;">&gt;&gt;</span> constructorCache <span style="color: #008000;">=</span>
    <span style="color: #008000;">new</span> Dictionary<span style="color: #008000;">&lt;</span>Type, Func<span style="color: #008000;">&lt;</span>IntPtr, <span style="color: #6666cc; font-weight: bold;">bool</span>, <span style="color: #6666cc; font-weight: bold;">object</span><span style="color: #008000;">&gt;&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">static</span> Func<span style="color: #008000;">&lt;</span>IntPtr, <span style="color: #6666cc; font-weight: bold;">bool</span>, <span style="color: #6666cc; font-weight: bold;">object</span><span style="color: #008000;">&gt;</span> CreateConstructorDelegate<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #008080; font-style: italic;">// We need to first grab the reflection information for the more derrived type we're casting to.</span>
    <span style="color: #008080; font-style: italic;">// SWIG has protected constructors we'll be able to call that take 2 parameters, the native pointer</span>
    <span style="color: #008080; font-style: italic;">// to the type and whether or not SWIG owns the memory</span>
    ConstructorInfo ctor <span style="color: #008000;">=</span> <span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span>T<span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">GetConstructor</span><span style="color: #008000;">&#40;</span>
        BindingFlags<span style="color: #008000;">.</span><span style="color: #0000FF;">Instance</span> <span style="color: #008000;">|</span> BindingFlags<span style="color: #008000;">.</span><span style="color: #0000FF;">NonPublic</span>, <span style="color: #0600FF; font-weight: bold;">null</span>,
        CallingConventions<span style="color: #008000;">.</span><span style="color: #0000FF;">HasThis</span>, <span style="color: #008000;">new</span> Type<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">&#123;</span> <span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span>IntPtr<span style="color: #008000;">&#41;</span>, <span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">bool</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#125;</span>, <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">// Lets emit some IL that will allow us to construct our type much</span>
    <span style="color: #008080; font-style: italic;">// faster than using reflection.</span>
    DynamicMethod dm <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> DynamicMethod<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Create&quot;</span>, <span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span><span style="color: #008000;">&#41;</span>,
        <span style="color: #008000;">new</span> Type<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">&#123;</span> <span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span>IntPtr<span style="color: #008000;">&#41;</span>, <span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">bool</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#125;</span>, <span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span>T<span style="color: #008000;">&#41;</span>, <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    ILGenerator il <span style="color: #008000;">=</span> dm<span style="color: #008000;">.</span><span style="color: #0000FF;">GetILGenerator</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    il<span style="color: #008000;">.</span><span style="color: #0000FF;">Emit</span><span style="color: #008000;">&#40;</span>OpCodes<span style="color: #008000;">.</span><span style="color: #0000FF;">Ldarg_0</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    il<span style="color: #008000;">.</span><span style="color: #0000FF;">Emit</span><span style="color: #008000;">&#40;</span>OpCodes<span style="color: #008000;">.</span><span style="color: #0000FF;">Ldarg_1</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    il<span style="color: #008000;">.</span><span style="color: #0000FF;">Emit</span><span style="color: #008000;">&#40;</span>OpCodes<span style="color: #008000;">.</span><span style="color: #0000FF;">Newobj</span>, ctor<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    il<span style="color: #008000;">.</span><span style="color: #0000FF;">Emit</span><span style="color: #008000;">&#40;</span>OpCodes<span style="color: #008000;">.</span><span style="color: #0000FF;">Ret</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">// Create the delegate for the dynamic method, which will further improve the calling speed.</span>
    <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #008000;">&#40;</span>Func<span style="color: #008000;">&lt;</span>IntPtr, <span style="color: #6666cc; font-weight: bold;">bool</span>, <span style="color: #6666cc; font-weight: bold;">object</span><span style="color: #008000;">&gt;</span><span style="color: #008000;">&#41;</span>dm<span style="color: #008000;">.</span><span style="color: #0000FF;">CreateDelegate</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span>Func<span style="color: #008000;">&lt;</span>IntPtr, <span style="color: #6666cc; font-weight: bold;">bool</span>, <span style="color: #6666cc; font-weight: bold;">object</span><span style="color: #008000;">&gt;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>After that we need to add a function that can perform the down cast. This is just a matter of checking if the object is actually the desired casting type. If it is we get or create the factory delegate for the type. We construct the new wrapper object and hand it the pointer for the type. (NOTE: This is only viable if the new type isn&#8217;t offset in the vtable, as would be the case with multiple inheritance).</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> T CastTo<span style="color: #008000;">&lt;</span>t<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span>YourBaseRTTIObject self<span style="color: #008000;">&#41;</span> <span style="color: #0600FF; font-weight: bold;">where</span> T <span style="color: #008000;">:</span> YourBaseRTTIObject
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>self <span style="color: #008000;">==</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span>
        <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">// Check if the object is actually the type we're trying to cast to.</span>
    <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>self<span style="color: #008000;">.</span><span style="color: #0000FF;">IsKindOf</span><span style="color: #008000;">&lt;</span>t<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        Type type <span style="color: #008000;">=</span> <span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span>T<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">// Check if we've already cached the factory function.</span>
        Func<span style="color: #008000;">&lt;</span>IntPtr, <span style="color: #6666cc; font-weight: bold;">bool</span>, <span style="color: #6666cc; font-weight: bold;">object</span><span style="color: #008000;">&gt;</span> factory<span style="color: #008000;">;</span>
        <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">!</span>constructorCache<span style="color: #008000;">.</span><span style="color: #0000FF;">TryGetValue</span><span style="color: #008000;">&#40;</span>type, <span style="color: #0600FF; font-weight: bold;">out</span> factory<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            factory <span style="color: #008000;">=</span> CreateConstructorDelegate<span style="color: #008000;">&lt;</span>t<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            constructorCache<span style="color: #008000;">.</span><span style="color: #0600FF; font-weight: bold;">Add</span><span style="color: #008000;">&#40;</span>type, factory<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">// Call the factory function and set the casted source to the</span>
        <span style="color: #008080; font-style: italic;">// current object.</span>
        T castedObject <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span>T<span style="color: #008000;">&#41;</span>factory<span style="color: #008000;">&#40;</span>YourBaseRTTIObject<span style="color: #008000;">.</span><span style="color: #0000FF;">getCPtr</span><span style="color: #008000;">&#40;</span>self<span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Handle</span>, <span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        castedObject<span style="color: #008000;">.</span><span style="color: #0000FF;">m_castedSource</span> <span style="color: #008000;">=</span> self<span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">return</span> castedObject<span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
    <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p><a href="http://www.nickdarnell.com/?p=1631" target="_blank">SWIG: Casting Revisited @ nickdarnell.com</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.altdevblogaday.com/2011/08/10/swig-casting-revisited/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Project Photofly Experiment</title>
		<link>http://www.altdevblogaday.com/2011/07/26/project-photofly-experiment/</link>
		<comments>http://www.altdevblogaday.com/2011/07/26/project-photofly-experiment/#comments</comments>
		<pubDate>Tue, 26 Jul 2011 06:34:47 +0000</pubDate>
		<dc:creator>Nick Darnell</dc:creator>
				<category><![CDATA[General Interest]]></category>
		<category><![CDATA[Tools]]></category>
		<category><![CDATA[Kinect]]></category>
		<category><![CDATA[Project Photofly]]></category>

		<guid isPermaLink="false">http://altdevblogaday.com/?p=12486</guid>
		<description><![CDATA[<p>Last week we were sitting around the office wondering if it would be possible to place ourselves in a game world with <a href="http://labs.autodesk.com/utilities/photo_scene_editor/overview/" target="_blank">Autodesk’s Project Photofly</a>.  How cool would that be?  We thought we might be able to scan one of us in a T-pose and then use <a href="http://www.mixamo.com/c/auto-rigger" target="_blank">Mixamo’s Auto-Rigging</a> tool to create a rigged avatar.  Then we could be running around a level in front of our Kinect as ourselves.</p>
<p><a href="http://www.altdevblogaday.com/2011/07/26/project-photofly-experiment/" class="more-link">Read more on Project Photofly Experiment&#8230;</a></p>
]]></description>
				<content:encoded><![CDATA[<p>Last week we were sitting around the office wondering if it would be possible to place ourselves in a game world with <a href="http://labs.autodesk.com/utilities/photo_scene_editor/overview/" target="_blank">Autodesk’s Project Photofly</a>.  How cool would that be?  We thought we might be able to scan one of us in a T-pose and then use <a href="http://www.mixamo.com/c/auto-rigger" target="_blank">Mixamo’s Auto-Rigging</a> tool to create a rigged avatar.  Then we could be running around a level in front of our Kinect as ourselves.</p>
<p>Sadly it never went past stage one.  It’s harder than you might think to hold a T-pose for 3 minutes while someone circles you twice snapping pictures at 10 degree intervals.</p>
<p>I don’t have any pictures of the results; I came out looking like the elephant man.  We’ll probably try again at some point, but in the meantime I made another scan of a pair of static objects that was turning out pretty good until I got to the back of the monkey.</p>
<span class='embed-youtube' style='text-align:center; display: block;'><iframe class='youtube-player' type='text/html' width='640' height='390' src='http://www.youtube.com/embed/UEs1cjlD4W4?version=3&#038;rel=1&#038;fs=1&#038;showsearch=0&#038;showinfo=1&#038;iv_load_policy=1&#038;wmode=transparent' frameborder='0'></iframe></span>
<p>All in all Autodesk’s Photofly software is pretty cool.  It’s still lacking in the area of iteration and debugging.  You can try manually tagging photo matchup points between images to give it a better idea on how the images fit together but it takes awhile for the data to be processed in the cloud.  It’s also unclear where some data comes from, or why portions of the background become part of the foreground mesh.  If it had better feedback for how that data became part of the mesh cleaning up the results would be a lot easier.</p>
<p>Also, if you own a camera with a sports video mode that captures at 60 FPS you can just slowly circle the subject and then dump all the frames using <a href="http://paul.glagla.free.fr/imagegrab_en.htm" target="_blank">ImageGrab</a>.  Which is way easier than snapping individual pictures.</p>
<p>I wonder if I could generate 3d art for a game jam&#8230;</p>
<p><a href="http://www.nickdarnell.com/2011/07/project-photofly-experiment/" target="_blank">Project Photofly Experiment @ nickdarnell.com</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.altdevblogaday.com/2011/07/26/project-photofly-experiment/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How I Organize Research</title>
		<link>http://www.altdevblogaday.com/2011/07/11/how-i-organize-research/</link>
		<comments>http://www.altdevblogaday.com/2011/07/11/how-i-organize-research/#comments</comments>
		<pubDate>Mon, 11 Jul 2011 06:05:00 +0000</pubDate>
		<dc:creator>Nick Darnell</dc:creator>
				<category><![CDATA[General Interest]]></category>
		<category><![CDATA[Media Wiki]]></category>
		<category><![CDATA[Organization]]></category>
		<category><![CDATA[Research]]></category>
		<category><![CDATA[Whitepapers]]></category>

		<guid isPermaLink="false">http://altdevblogaday.com/?p=11063</guid>
		<description><![CDATA[<p>About a year ago I got tired of trying to keep the mounting supply of PDFs and PowerPoint slides I’ve collected over the years organized.  They weren’t searchable, I had to organize them in broad categories in folders, I couldn’t share them between work and home, they took up a ton of space on my computer and I had to make sure I kept them backed up somewhere.</p>
<p><a href="http://www.altdevblogaday.com/2011/07/11/how-i-organize-research/" class="more-link">Read more on How I Organize Research&#8230;</a></p>
]]></description>
				<content:encoded><![CDATA[<p>About a year ago I got tired of trying to keep the mounting supply of PDFs and PowerPoint slides I’ve collected over the years organized.  They weren’t searchable, I had to organize them in broad categories in folders, I couldn’t share them between work and home, they took up a ton of space on my computer and I had to make sure I kept them backed up somewhere.</p>
<p>I couldn’t find a service online that did everything I wanted.  The only one I found with any promise was Google Docs, but they have all kinds of size restrictions and the PDFs uploaded aren&#8217;t searchable.</p>
<p>So I ended up cobbling together a solution that works for me and wanted to share the solution with everyone else.</p>
<h3>The Cobbling</h3>
<p>I started with a <a href="http://www.mediawiki.org/wiki/MediaWiki" target="_blank">Media Wiki</a> based web application because I want to be able to access this library anywhere.  I already had a bunch of domains lying around I could use and was already paying for hosting.  Media Wiki also has a <a href="http://www.mediawiki.org/wiki/Extension_Matrix/AllExtensions" target="_blank">huge number of extensions</a> people have written for it that were invaluable in accomplishing my end goal.</p>
<p><a href="http://altdevblogaday.com/wp-content/uploads/2011/07/wiki_main.png" target="_blank"><img style="margin: 5px;padding-left: 0px;padding-right: 0px;padding-top: 0px;border-width: 0px" src="http://altdevblogaday.com/wp-content/uploads/2011/07/wiki_main_thumb.png" alt="wiki_main" width="400" height="277" border="0" /></a></p>
<p>The main page is a combination of two extensions,</p>
<ul>
<li><a href="http://www.mediawiki.org/wiki/Extension:WikiCategoryTagCloud" target="_blank">WikiCategoryTagCloud</a> – Lets me display all my categories as a cloud on the main page, letting me quickly find something from a particular event.</li>
<li><a href="http://www.mediawiki.org/wiki/Extension:DynamicPageList_%28Wikimedia%29" target="_blank">DynamicPageList</a> – Displays the giant list of files I’ve collected.  They are organized by when they were uploaded, so finding something I uploaded recently is a breeze.</li>
</ul>
<p>I also made some modifications to the default limits on my wiki’s upload so that I could upload the fairly large PDF files and PowerPoint slides.</p>
<p>One crucial goal I had was to make the text in PDFs and PowerPoints searchable.  For that I used the <a href="http://www.mediawiki.org/wiki/Extension:FileIndexer" target="_blank">FileIndexer</a> extension.</p>
<p><a href="http://altdevblogaday.com/wp-content/uploads/2011/07/wiki_search.png" target="_blank"><img style="margin: 5px;padding-left: 0px;padding-right: 0px;padding-top: 0px;border-width: 0px" src="http://altdevblogaday.com/wp-content/uploads/2011/07/wiki_search_thumb.png" alt="wiki_search" width="400" height="318" border="0" /></a></p>
<p>I could have stopped there but ended up finding a extension that would render the contents of the PDF directly to the browser window if for some reason I didn’t have a PDF reader or was on a slow connection and wanted to look at a single portion of a large PDF.</p>
<p>For that I was able to use the <a href="http://www.mediawiki.org/wiki/Extension:PdfHandler" target="_blank">PdfHandler</a> extension, which has a dependency on the <a href="http://www.mediawiki.org/wiki/Extension:WebStore" target="_blank">WebStore</a> extension.</p>
<p><a href="http://altdevblogaday.com/wp-content/uploads/2011/07/wiki_pdf.png" target="_blank"><img style="margin: 5px;padding-left: 0px;padding-right: 0px;padding-top: 0px;border-width: 0px" src="http://altdevblogaday.com/wp-content/uploads/2011/07/wiki_pdf_thumb.png" alt="wiki_pdf" width="400" height="310" border="0" /></a></p>
<h3>One Year Later</h3>
<p>A year after cobbling the pieces together I’m still pretty happy with the solution.  It hasn’t failed on me yet and I have constant access to my library no matter where I am.  I wish I could package up the relevant pieces so others could just setup their own whitepaper repository, but I haven’t checked the redistribution rights of all the relevant bits.</p>
<p><a href="http://www.nickdarnell.com/2011/07/how-i-organize-research/" target="_blank">How I Organize Research @ nickdarnell.com</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.altdevblogaday.com/2011/07/11/how-i-organize-research/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Hierarchical Z-Buffer Occlusion Culling &#8211; Generating Occlusion Volumes</title>
		<link>http://www.altdevblogaday.com/2011/06/26/hierarchical-z-buffer-occlusion-culling-generating-occlusion-volumes/</link>
		<comments>http://www.altdevblogaday.com/2011/06/26/hierarchical-z-buffer-occlusion-culling-generating-occlusion-volumes/#comments</comments>
		<pubDate>Sun, 26 Jun 2011 15:47:19 +0000</pubDate>
		<dc:creator>Nick Darnell</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Automation]]></category>
		<category><![CDATA[Hierarchical Z-Buffer]]></category>
		<category><![CDATA[Occlusion Culling]]></category>
		<category><![CDATA[Occlusion Volumes]]></category>
		<category><![CDATA[OpenCL]]></category>

		<guid isPermaLink="false">http://altdevblogaday.com/?p=9762</guid>
		<description><![CDATA[<p><img style="border-right-width: 0px;margin: 5px;padding-left: 0px;padding-right: 0px;float: right;border-top-width: 0px;border-bottom-width: 0px;border-left-width: 0px;padding-top: 0px" border="0" alt="volume_cat" align="right" src="http://altdevblogaday.com/wp-content/uploads/2011/06/volume_cat.png" width="239" height="240" /></p>
<p>Almost a year ago I wrote a couple of posts on Hierarchical Z-Buffer Occlusion Culling (HZB-OC).</p>
<ul>
<li><a title="http://www.nickdarnell.com/?p=942" href="http://www.nickdarnell.com/?p=942" target="_blank">Hierarchical Z-Buffer Occlusion Culling</a> </li>
<li><a href="http://www.nickdarnell.com/?p=1036" target="_blank">Hierarchical Z-Buffer Occlusion Culling – Shadows</a> </li>
</ul>
<p>One very glaring issue I left largely untouched was the workflow issue.&#160; Artists currently have to author all or a large percentage of the volumes by hand in the implementations I’m aware exist.&#160; Ideally these simplified occlusion volumes would be generated by a tool in the art pipeline instead.</p>
<p><a href="http://www.altdevblogaday.com/2011/06/26/hierarchical-z-buffer-occlusion-culling-generating-occlusion-volumes/" class="more-link">Read more on Hierarchical Z-Buffer Occlusion Culling &#8211; Generating Occlusion Volumes&#8230;</a></p>
]]></description>
				<content:encoded><![CDATA[<p><img style="border-right-width: 0px;margin: 5px;padding-left: 0px;padding-right: 0px;float: right;border-top-width: 0px;border-bottom-width: 0px;border-left-width: 0px;padding-top: 0px" border="0" alt="volume_cat" align="right" src="http://altdevblogaday.com/wp-content/uploads/2011/06/volume_cat.png" width="239" height="240" /></p>
<p>Almost a year ago I wrote a couple of posts on Hierarchical Z-Buffer Occlusion Culling (HZB-OC).</p>
<ul>
<li><a title="http://www.nickdarnell.com/?p=942" href="http://www.nickdarnell.com/?p=942" target="_blank">Hierarchical Z-Buffer Occlusion Culling</a> </li>
<li><a href="http://www.nickdarnell.com/?p=1036" target="_blank">Hierarchical Z-Buffer Occlusion Culling – Shadows</a> </li>
</ul>
<p>One very glaring issue I left largely untouched was the workflow issue.&#160; Artists currently have to author all or a large percentage of the volumes by hand in the implementations I’m aware exist.&#160; Ideally these simplified occlusion volumes would be generated by a tool in the art pipeline instead.</p>
<p>A few weeks ago I started looking for a new side project and decided to try and flesh-out a very rough idea I had for automatically generating occlusion volumes.&#160; This is still a work in progress but I wanted to share my current progress because I think it has some value even at this early stage.</p>
<h3>The Problem</h3>
<p>An ideal occlusion volume has some important features,</p>
<ol>
<li>Conservativeness – Doesn’t extend beyond the surface of the mesh </li>
<li>Simplicity – The occlusion volume is made of very few triangles or is fast to render </li>
<li>Volume Conservation – Closely matches the original mesh’s volume </li>
<li>Dynamic – Some games have large movable occluders or destroyable walls </li>
</ol>
<p>Normal methods of simplifying a mesh such as typical triangle simplification causes problems in both the area of conservativeness and volume conservation.&#160; In some cases you can use the physics collision mesh if available.&#160; One thing to be aware of is that when the physics volume is larger than the visual mesh it can cause false occlusions leading to things popping into view.&#160; Also not every mesh needs a physics volume nor are physics volumes always meshes.&#160; <a href="http://www.secondintention.com/news/gdc-slides-available-to-download" target="_blank">Will Vale’s talk from GDC 2011</a> covered using physics collision meshes pretty thoroughly for HZB-OC, you should check it out. </p>
<h3>The Technique</h3>
<p>Let me start by summarizing the technique I’ve been developing for generating the occlusion volumes before I go in-depth into each step in the process.</p>
<ol>
<li>Find all the voxels completely inside a mesh </li>
<li>Find the voxel at the densest point in the volume </li>
<li>Expand a box from this point until all sides collide with the mesh surface or another box </li>
<li>Repeat 2-3 until you’ve consumed X% of the total volume </li>
<li>Filter small or useless boxes (Unimplemented) </li>
<li>Use a Constructive Solid Geometry (CSG) algorithm to merge the boxes you create </li>
</ol>
<h3>1. Voxelization</h3>
<p><a href="http://altdevblogaday.com/wp-content/uploads/2011/06/surface_solid.png"><img style="border-right-width: 0px;margin: 5px;padding-left: 0px;padding-right: 0px;float: right;border-top-width: 0px;border-bottom-width: 0px;border-left-width: 0px;padding-top: 0px" border="0" alt="surface_solid" align="right" src="http://altdevblogaday.com/wp-content/uploads/2011/06/surface_solid_thumb.png" width="240" height="137" /></a>First you have to determine all the voxels completely inside the mesh.&#160; That way we can have complete confidence that anything we generate contained inside these voxels will be conservative.&#160; It also gives us a very easy way of quantifying the total volume and the volume remaining.</p>
<p>The voxelization algorithms I ended up using comes from this paper <a title="http://www.mpi-inf.mpg.de/~mschwarz/papers/vox-siga10.pdf" href="http://www.mpi-inf.mpg.de/~mschwarz/papers/vox-siga10.pdf" target="_blank">Fast Parallel Surface and Solid Voxelization on GPUs</a> [Schwarz 2010].&#160; I used the 3.1 section for surface voxelization and the 4.1 section for solid voxelization.&#160; Both are required because a voxel is considered part of the solid volume if the center of the voxel is inside the mesh.&#160; We want the entire voxel to be inside the mesh though.&#160; So you have to remove the excess voxels from the solid set using the surface set.</p>
<p>My first implementation was in C++ and entirely on the CPU which took about 20 seconds to run on a mesh with ~10,000 triangles and a 50<sup>3</sup> voxel volume.&#160; In the version I’m presenting here I moved to C#, when I did that time went up to about 60 seconds.&#160; So I ended up porting a similar version of the CPU implementation to the GPU using OpenCL (<a href="http://sourceforge.net/projects/cloo/" target="_blank">Cloo</a>), on the same data it ran in about 6 seconds on my Nvidia 540M, with no attempt to optimize.</p>
<p>One unfortunate limitation of Schwarz’s solid voxelization algorithm it that it requires watertight meshes.&#160; The reason for this is you’re shooting a ray down a column of voxels until you intersect with a triangle.&#160; You’re then xor’ing the bit for each voxel signaling that this column of voxel’s is inside or outside.&#160; Voxels that are inside a mesh will be Xor’ed an odd number of times, where as outside voxels will be xored an even number of times.&#160; So if there is a hole in the mesh, you’d leave a incomplete trail of xor’s in that column, leaving every voxel from the last triangle to the bounding box marked as ‘inside’.</p>
<p>Two papers I’ve run across that might contain solutions to this problem are <a title="http://www.cadanda.com/CAD_5_6__889-899.pdf" href="http://www.cadanda.com/CAD_5_6__889-899.pdf" target="_blank">An Automatic Hole-Filling Algorithm for Polygon Meshes</a> and <a href="http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.14.6993" target="_blank">Complete Polygonal Scene Voxelization</a>.</p>
<h3>2. Find The Highest Density Voxel</h3>
<p>In this step you’ll find the voxel that is furthest away from any external area, and excluding any voxel that has already been enclosed by a box in step 3.&#160; Because the number of empty voxels likely far exceeds the number of voxels touching an empty voxel, you’ll probably want to test against that list instead of determining the distance from the closest empty voxel.</p>
<p>For my project this was implemented on the CPU, but this is probably something you could do faster on the GPU if this ever became too slow for your needs.</p>
<h3>3. Box Expansion</h3>
<p>Once you’ve found the densest voxel you’re going to create a 1<sup>3</sup> voxel sized box at that location.&#160; You’ll then proceed to iteratively expand each side of the box in voxel space until you can’t expand any side of the box without entering an empty voxel or another box that has already been placed.</p>
<p>As you verify each expansion of the box you’ll mark the enclosed voxels in such a way so that the next time you choose the densest voxel you can exclude any already enclosed in a box.</p>
<p>Currently the expansion order is uniform, however, I suspect a better approach would be some small amount of prediction allowing me to grow one side more than another to maximize the potential volume of each box.&#160; </p>
<p>The best implementation of this AABB expansion methodology would be to use an optimization solver, but that could potentially take quite awhile to run.</p>
<p>I’m still looking for alternatives to the AABB box expansion method of generating the simplified geometry. The axis aligned boxes aren’t the best for models at non-right angles.&#160; Perhaps a better approach could be divined with OBBs but that’s just conjecture.</p>
<p>I briefly considered the Iso-Surface method Xi Wang mentioned in his <a href="http://www.gdcvault.com/play/1014344/Automated-Level-of-Detail-Generation" target="_blank">Automated Level of Detail Generation for HALO: REACH</a> talk at GDC 2011 but that generates a lot of triangles and a volume larger than the voxel volume you start with. So that just gets us right back to the problem we started with.</p>
<h3>4. Repeat 2-3</h3>
<p>I ended up having two different stopping conditions.&#160; If an absolute percentage of the remaining volume is met I stop adding new boxes.&#160; Alternatively if the last box created consumed too small a percentage of the total volume that also stops the box expansion process.</p>
<p>Some other things I didn’t implement but that could be tried are, setting a maximum number of boxes, or a box too small (in voxel space, instead of percentage) as a cutoff point.</p>
<h3>5. Filter Boxes</h3>
<p>This portion of the technique I haven’t tried but makes a lot of sense depending on your stopping condition.&#160; If several small boxes are generated before one of the cutoff conditions is met during step 4, you’ll probably want to ignore them before moving to step 6 to reduce your occluder triangle count.</p>
<h3>6. Merge Boxes With Constructive Solid Geometry</h3>
<p>Drawing individual boxes even as a single draw call would cause lots of overdraw.&#160; Instead we should take our collection of boxes and combine them into a single mesh before we finish.&#160; To do this we’re going to use Constructive Solid Geometry (CSG).</p>
<p>Soon after I realized I was going to need CSG (or something akin to it) to solve this problem I was informed of a new book, <a title="http://gamedevelopmenttools.com/" href="http://gamedevelopmenttools.com" target="_blank">Game Development Tools</a>.&#160; In that book there is an implementation for Real-Time Constructive Solid Geometry written by <a href="http://sandervanrossen.blogspot.com" target="_blank">Sander van Rossen</a> and Matthew Baranowski.&#160; The implementation is pretty slick and I was able to get it up and running pretty quickly.</p>
<h3>The Results: Time-Lapse</h3>
<p>The box expansion runs in a fraction of a second, but I slowed it down and broke it up over several frames so that you could see how the occlusion shape evolves.</p>
<span class='embed-youtube' style='text-align:center; display: block;'><iframe class='youtube-player' type='text/html' width='640' height='390' src='http://www.youtube.com/embed/R9IFcPtxonc?version=3&#038;rel=1&#038;fs=1&#038;showsearch=0&#038;showinfo=1&#038;iv_load_policy=1&#038;wmode=transparent' frameborder='0'></iframe></span>
<span class='embed-youtube' style='text-align:center; display: block;'><iframe class='youtube-player' type='text/html' width='640' height='390' src='http://www.youtube.com/embed/dFeKgHK_Ckc?version=3&#038;rel=1&#038;fs=1&#038;showsearch=0&#038;showinfo=1&#038;iv_load_policy=1&#038;wmode=transparent' frameborder='0'></iframe></span>
<h3>The Results: Different Models</h3>
<p>To get an idea of the results on different models I’ve provided some screenshots.&#160; The settings I used for these was to cutoff after 90% of the volume had been consumed by boxes.</p>
<table border="0" cellspacing="0" cellpadding="2" width="576">
<tbody>
<tr>
<td width="300" align="center"><a href="http://altdevblogaday.com/wp-content/uploads/2011/06/occluder_results_1.png" target="_blank"><img style="border-right-width: 0px;margin: 5px 0px;padding-left: 0px;padding-right: 0px;border-top-width: 0px;border-bottom-width: 0px;border-left-width: 0px;padding-top: 0px" border="0" alt="occluder_results_1" src="http://altdevblogaday.com/wp-content/uploads/2011/06/occluder_results_1_thumb.png" width="240" height="180" /></a> </td>
<td valign="top" width="274"><a href="http://altdevblogaday.com/wp-content/uploads/2011/06/occluder_results_3.png" target="_blank"><img style="border-right-width: 0px;margin: 5px;padding-left: 0px;padding-right: 0px;border-top-width: 0px;border-bottom-width: 0px;border-left-width: 0px;padding-top: 0px" border="0" alt="occluder_results_3" src="http://altdevblogaday.com/wp-content/uploads/2011/06/occluder_results_3_thumb.png" width="240" height="179" /></a></td>
</tr>
<tr>
<td width="311" align="center"><a href="http://altdevblogaday.com/wp-content/uploads/2011/06/occluder_results_2.png" target="_blank"><img style="border-right-width: 0px;margin: 5px;padding-left: 0px;padding-right: 0px;border-top-width: 0px;border-bottom-width: 0px;border-left-width: 0px;padding-top: 0px" border="0" alt="occluder_results_2" src="http://altdevblogaday.com/wp-content/uploads/2011/06/occluder_results_2_thumb.png" width="240" height="180" /></a></td>
<td valign="top" width="285"><a href="http://altdevblogaday.com/wp-content/uploads/2011/06/occluder_results_4.png"><img style="border-right-width: 0px;margin: 5px;padding-left: 0px;padding-right: 0px;border-top-width: 0px;border-bottom-width: 0px;border-left-width: 0px;padding-top: 0px" border="0" alt="occluder_results_4" src="http://altdevblogaday.com/wp-content/uploads/2011/06/occluder_results_4_thumb.png" width="240" height="190" /></a></td>
</tr>
</tbody>
</table>
<h3>Notes</h3>
<p>In situations where the technique doesn’t work very well allow an artist to specify a custom mesh.&#160; In some cases an option to use the same mesh for both the visual and the occluder would probably be a good idea (like for planes).</p>
<p>To reduce draw calls you will likely want to generate the occluders during export time.&#160; Then during the cook stage in your level editor, merge some number of the occluders together to reduce draw calls.</p>
<h3>Download</h3>
<p>
<table border="0" cellspacing="0" cellpadding="2" width="579">
<tbody>
<tr>
<td valign="top" width="87"><img style="border-right-width: 0px;margin: 5px;padding-left: 0px;padding-right: 0px;border-top-width: 0px;border-bottom-width: 0px;border-left-width: 0px;padding-top: 0px" border="0" alt="works_on_my_machine" src="http://altdevblogaday.com/wp-content/uploads/2011/06/works_on_my_machine.png" width="80" height="77" /></td>
<td valign="top" width="490"><a href="http://files.nickdarnell.com/NickDarnell-VOG-Source.zip">             <br />Voxelization Occlusion Generator.zip</a>             <br />License: MIT</td>
</tr>
</tbody>
</table>
<h3>Thanks</h3>
<p>Thanks to <a href="http://michaelnoland.com" target="_blank">Michael Noland</a> and <a href="https://shaunkime.wordpress.com/" target="_blank">Shaun Kime</a> for letting me bounce ideas off them.&#160; Also thanks to <a href="http://selfshadow.com/" target="_blank">Stephen Hill</a> for telling me about Rossen / Baranowski’s Constructive Solid Geometry implementation and for giving me feedback on this post.</p>
<p><a href="http://www.nickdarnell.com/2011/06/hierarchical-z-buffer-occlusion-culling-generating-occlusion-volumes/" target="_blank">Hierarchical Z-Buffer Occlusion Culling &#8211; Generating Occlusion Volumes</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.altdevblogaday.com/2011/06/26/hierarchical-z-buffer-occlusion-culling-generating-occlusion-volumes/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Extend Visual Studio!</title>
		<link>http://www.altdevblogaday.com/2011/06/11/extend_visual_studio/</link>
		<comments>http://www.altdevblogaday.com/2011/06/11/extend_visual_studio/#comments</comments>
		<pubDate>Sat, 11 Jun 2011 09:32:12 +0000</pubDate>
		<dc:creator>Nick Darnell</dc:creator>
		
		<guid isPermaLink="false">http://altdevblogaday.org/?p=7912</guid>
		<description><![CDATA[<h3>The Pitch</h3>
<p>I don’t want to show you how to add a command button or toolbar to Visual Studio, there are plenty of examples out there for that. I want to provide you with an example showing how writing a Visual Studio extension could benefit your studio’s workflow.</p>
<p><a href="http://www.altdevblogaday.com/2011/06/11/extend_visual_studio/" class="more-link">Read more on Extend Visual Studio!&#8230;</a></p>
]]></description>
				<content:encoded><![CDATA[<h3>The Pitch</h3>
<p>I don’t want to show you how to add a command button or toolbar to Visual Studio, there are plenty of examples out there for that. I want to provide you with an example showing how writing a Visual Studio extension could benefit your studio’s workflow.</p>
<p><a href="http://altdevblogaday.com/author/andy-firth/" target="_blank">Andy Firth</a> wrote an article on <a href="http://altdevblogaday.com/" target="_blank">#AltDevBlogADay</a> a few days ago on <a href="http://altdevblogaday.com/2011/06/08/watch-window-part1/" target="_blank">Extending the Watch Window in Visual Studio via autoexp.dat</a>. It’s a nice introduction article to modifying the autoexp.dat file to auto-expand and format variables in the watch window.</p>
<p>Now, if I take a bunch of time designing a nice formatter to speed up my debugging sessions <em>how do I share it across the company?</em></p>
<p>Well, the only way is for you to manually merge other peoples additions to the AutoExp.dat file into your own. Which inevitably leads to varying levels of adoption of the formatters and debugging times suffer as a result.</p>
<p><strong>Lets fix this!</strong></p>
<p>We’re going to write an extension to patch the AutoExp.dat file with one collocated with the solution file.</p>
<p>&#160;</p>
<h3>Getting Started</h3>
<p><a href="http://altdevblogaday.com/wp-content/uploads/2011/06/new_vs_package.png"><img style="border-right-width: 0px;margin: 5px;padding-left: 0px;padding-right: 0px;float: right;border-top-width: 0px;border-bottom-width: 0px;border-left-width: 0px;padding-top: 0px" border="0" alt="new_vs_package" align="right" src="http://altdevblogaday.com/wp-content/uploads/2011/06/new_vs_package_thumb.png" width="240" height="140" /></a>After downloading the <a href="http://www.microsoft.com/downloads/en/details.aspx?FamilyID=21307c23-f0ff-4ef2-a0a4-dca54ddb1e21" target="_blank">Visual Studio 2010 SP1 SDK</a>&#160; you’ll create a new solution using the Visual Studio Package template.&#160; You could have also chosen the Add-in template.&#160; Though the packages are basically just as easy to install in Visual Studio 2010 and they offer much more control should you end up needing it.</p>
<p>The code you’ll start out with will be a bit more verbose than this, but here’s the important bit to get started playing with different capabilities.&#160; Visual Studio is based on a loose coupling service architecture, so to accomplish anything useful you’ll need to find the the service that handles it.&#160; Luckily Microsoft has documented the <a href="http://msdn.microsoft.com/en-us/library/bb165789.aspx" target="_blank">List of Services</a>.&#160; If I had to pick the “main” service it would be the DTE service.&#160; It manages the majority of the functionally you’ll be interested in playing with.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">sealed</span> <span style="color: #6666cc; font-weight: bold;">class</span> MyPackage <span style="color: #008000;">:</span> Package
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> DTE m_dte<span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">protected</span> <span style="color: #0600FF; font-weight: bold;">override</span> <span style="color: #6666cc; font-weight: bold;">void</span> Initialize<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #008080; font-style: italic;">// Your package is also a service container</span>
        IServiceContainer serviceContainer <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">this</span> <span style="color: #0600FF; font-weight: bold;">as</span> IServiceContainer<span style="color: #008000;">;</span>
        <span style="color: #008080; font-style: italic;">// The DTE object contains most of the goodies you'll want to play with</span>
        m_dte <span style="color: #008000;">=</span> serviceContainer<span style="color: #008000;">.</span><span style="color: #0000FF;">GetService</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span>SDTE<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span> <span style="color: #0600FF; font-weight: bold;">as</span> DTE<span style="color: #008000;">;</span>
        <span style="color: #008080; font-style: italic;">// I could also add a service of my own if I wanted</span>
        <span style="color: #008080; font-style: italic;">// serviceContainer.AddService(typeof(MyService), new MyService(), true);</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>&#160;</p>
<h3>The AutoExp.dat Patcher</h3>
<p>Because we’ve decided to look for an AutoExp.dat patcher file beside the solution, lets hook some events so that we know when the solution is opened and closed.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">// Must hold a reference to the solution events object or the events wont </span>
<span style="color: #008080; font-style: italic;">// fire, garbage collection related</span>
m_solutionEvents <span style="color: #008000;">=</span> m_dte<span style="color: #008000;">.</span><span style="color: #0000FF;">Events</span><span style="color: #008000;">.</span><span style="color: #0000FF;">SolutionEvents</span><span style="color: #008000;">;</span>
m_solutionEvents<span style="color: #008000;">.</span><span style="color: #0000FF;">Opened</span> <span style="color: #008000;">+=</span> SolutionOpened<span style="color: #008000;">;</span>
m_solutionEvents<span style="color: #008000;">.</span><span style="color: #0000FF;">AfterClosing</span> <span style="color: #008000;">+=</span> SolutionAfterClosing<span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">// Most of the events you'll find useful are catagoried under </span>
<span style="color: #008080; font-style: italic;">// DTE.Events...</span></pre></td></tr></table></div>

<p>Now when the solution is opened we’re going to scan for the patcher file in the same directory as the solution file and we’re going to use it to modify the AutoExp.dat file.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #6666cc; font-weight: bold;">void</span> SolutionOpened<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #6666cc; font-weight: bold;">string</span> slnFile <span style="color: #008000;">=</span> m_dte<span style="color: #008000;">.</span><span style="color: #0000FF;">Solution</span><span style="color: #008000;">.</span><span style="color: #0000FF;">FullName</span><span style="color: #008000;">;</span>
    <span style="color: #6666cc; font-weight: bold;">string</span> solutionDirectory <span style="color: #008000;">=</span> Path<span style="color: #008000;">.</span><span style="color: #0000FF;">GetDirectoryName</span><span style="color: #008000;">&#40;</span>slnFile<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #6666cc; font-weight: bold;">string</span> pathfileName <span style="color: #008000;">=</span> Path<span style="color: #008000;">.</span><span style="color: #0000FF;">GetFileNameWithoutExtension</span><span style="color: #008000;">&#40;</span>slnFile<span style="color: #008000;">&#41;</span> <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;.autoexp&quot;</span><span style="color: #008000;">;</span>
    <span style="color: #6666cc; font-weight: bold;">string</span> patchFile <span style="color: #008000;">=</span> Path<span style="color: #008000;">.</span><span style="color: #0000FF;">Combine</span><span style="color: #008000;">&#40;</span>solutionDirectory, pathfileName<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>File<span style="color: #008000;">.</span><span style="color: #0000FF;">Exists</span><span style="color: #008000;">&#40;</span>patchFile<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #6666cc; font-weight: bold;">string</span> autoExpPatch <span style="color: #008000;">=</span> File<span style="color: #008000;">.</span><span style="color: #0000FF;">ReadAllText</span><span style="color: #008000;">&#40;</span>patchFile<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #6666cc; font-weight: bold;">string</span> autoExpPath <span style="color: #008000;">=</span> Environment<span style="color: #008000;">.</span><span style="color: #0000FF;">ExpandEnvironmentVariables</span><span style="color: #008000;">&#40;</span>
            <span style="color: #666666;">&quot;%VS100COMNTOOLS%<span style="color: #008080; font-weight: bold;">\\</span>..<span style="color: #008080; font-weight: bold;">\\</span>Packages<span style="color: #008080; font-weight: bold;">\\</span>Debugger<span style="color: #008080; font-weight: bold;">\\</span>autoexp.dat&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #6666cc; font-weight: bold;">string</span> blockBegin <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;; BEGIN AEP [AutoExpand] (&quot;</span> <span style="color: #008000;">+</span> pathfileName <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;)&quot;</span><span style="color: #008000;">;</span>
        <span style="color: #6666cc; font-weight: bold;">string</span> blockEnd <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;; END AEP [AutoExpand] (&quot;</span> <span style="color: #008000;">+</span> pathfileName <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;)&quot;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">// What follows is a bunch string parsing code that's not relevant to the article.</span>
        <span style="color: #008080; font-style: italic;">// (1) We remove the previous auto expansion block we added, if we added one </span>
        <span style="color: #008080; font-style: italic;">//     in another session.</span>
        <span style="color: #008080; font-style: italic;">// (2) We insert the new auto expansion patched section from our </span>
        <span style="color: #008080; font-style: italic;">//     SOLUTION_FILE_NAME.autoexp file.</span>
        <span style="color: #008080; font-style: italic;">// (3) Save the new AutoExp.dat file.</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>Because our extension isn’t triggered by a command and doesn’t have a UI we need to make sure Visual Studio loads our extension from the start.&#160; You accomplish this by adding the ProvideAutoLoad attribute to the Package class, with the right GUID identifying the time to load the extension.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">// --- Microsoft.VisualStudio.VSConstants.UICONTEXT_NoSolution</span>
<span style="color: #008000;">&#91;</span>ProvideAutoLoad<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;ADFC4E64-0397-11D1-9F4E-00A0C911004F&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span>
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">sealed</span> <span style="color: #6666cc; font-weight: bold;">class</span> AutoExpPatchPackage <span style="color: #008000;">:</span> Package
<span style="color: #008000;">&#123;</span>
   <span style="color: #008080; font-style: italic;">//...</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>&#160;</p>
<h3>Results</h3>
<p>In our test solution I&#8217;m going to start without any patch file and this is what I see when hovering over the test variable.</p>
<p><img style="border-right-width: 0px;margin: 5px;padding-left: 0px;padding-right: 0px;border-top-width: 0px;border-bottom-width: 0px;border-left-width: 0px;padding-top: 0px" border="0" alt="before_autoexppatch" src="http://altdevblogaday.com/wp-content/uploads/2011/06/before_autoexppatch.png" width="464" height="107" /></p>
<p>After we add the patch file though and reload the solution we see something completely different.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="text" style="font-family:monospace;">[AutoExpand]
&nbsp;
TestStruct =x=&lt;x,g&gt;, y=&lt;y,g&gt;, z=&lt;z,g&gt;</pre></td></tr></table></div>

<p><img style="border-right-width: 0px;margin: 5px;padding-left: 0px;padding-right: 0px;border-top-width: 0px;border-bottom-width: 0px;border-left-width: 0px;padding-top: 0px" border="0" alt="after_autoexppatch" src="http://altdevblogaday.com/wp-content/uploads/2011/06/after_autoexppatch.png" width="464" height="107" /></p>
<p>Voilà we’ve created a simple extension to Visual Studio that could actually improve workflow.&#160; We can now check-in our patcher file into source control, allowing us to share our rules with all of our fellow employees.</p>
<p>The extension I wrote only handles the [AutoExpand] section of the AutoExp.dat file.&#160; With a little bit of work though you could easily handle the other sections.</p>
<p>You could also add some other improvements such as removing the patched portions after the solution is closed to make sure there are no conflicts between versions, if you work on multiple versions of the codebase.</p>
<p>Another improvement would be to automatically re-patch the AutoExp.dat file if you detect a change to the patcher file while the solution is open.</p>
<p>&#160;</p>
<h3>Download</h3>
<p>You can download the source code for the <a href="http://files.nickdarnell.com/autoexppatcher.zip" target="_blank">AutoExp.dat Patcher here</a>. </p>
<p>&#160;</p>
<h3>Resources</h3>
<ul>
<li><a href="http://www.microsoft.com/downloads/en/details.aspx?FamilyID=21307c23-f0ff-4ef2-a0a4-dca54ddb1e21" target="_blank">Visual Studio 2010 SP1 SDK</a> </li>
<li><a href="http://social.msdn.microsoft.com/Forums/en/vsx/threads/" target="_blank">Visual Studio Extensibility Forum</a> </li>
<li><a href="http://www.mztools.com/resources_vsnet_addins.aspx" target="_blank">MZ-Tools: VSX Articles / Resources</a> </li>
<li><a href="http://msdn.microsoft.com/en-us/library/bb166441.aspx" target="_blank">Visual Studio MSDN</a>
<ul>
<li><a href="http://code.msdn.microsoft.com/site/search?f[0].Type=Topic&amp;f[0].Value=VSX&amp;f[1].Type=User&amp;f[1].Value=Microsoft%20-%20Visual%20Studio%20Platform%20Team&amp;f[1].Text=Microsoft%20-%20Visual%20Studio%20Platform%20Team" target="_blank">Samples</a> </li>
<li><a href="http://msdn.microsoft.com/en-us/library/bb165789.aspx" target="_blank">List of Services</a> </li>
</ul>
</li>
</ul>
<p><a href="http://www.nickdarnell.com/?p=1436">Extend Visual Studio! @ nickdarnell.com</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.altdevblogaday.com/2011/06/11/extend_visual_studio/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>SWIG and a Miss</title>
		<link>http://www.altdevblogaday.com/2011/05/27/swig-and-a-miss/</link>
		<comments>http://www.altdevblogaday.com/2011/05/27/swig-and-a-miss/#comments</comments>
		<pubDate>Fri, 27 May 2011 04:00:59 +0000</pubDate>
		<dc:creator>Nick Darnell</dc:creator>
		
		<guid isPermaLink="false">http://altdevblogaday.org/?p=6798</guid>
		<description><![CDATA[<p>There are so many pitfalls you&#8217;ll encounter after using SWIG for any extended period of time or with a large enough codebase.&#160; I thought I would go over some of the more notable ones I&#8217;ve encountered creating a C# wrapper that had me sighing and doubling my caffeine intake.&#160; That said, I do like SWIG.&#160; If you color within the lines it works quite well and has in the end saved me more time than it has cost me.</p>
<p><a href="http://www.altdevblogaday.com/2011/05/27/swig-and-a-miss/" class="more-link">Read more on SWIG and a Miss&#8230;</a></p>
]]></description>
				<content:encoded><![CDATA[<p>There are so many pitfalls you&#8217;ll encounter after using SWIG for any extended period of time or with a large enough codebase.&#160; I thought I would go over some of the more notable ones I&#8217;ve encountered creating a C# wrapper that had me sighing and doubling my caffeine intake.&#160; That said, I do like SWIG.&#160; If you color within the lines it works quite well and has in the end saved me more time than it has cost me.</p>
<p>I&#8217;m still debating if it&#8217;s worth investing time into my own cleaner/purpose built wrapper generator.&#160; I&#8217;d like to hear some thoughts from others who have had to deal with this decision and what you chose.&#160; If you went with writing your own, did you use any helpful libraries (like for parsing C++)<strong></strong>?&#160; Also, feel free to add to the this list in the comments if you&#8217;ve run into another notable problem you ran into with SWIG.</p>
<h3>1 ) Consistent Generator Configuration</h3>
<p>Using different build configurations for code generation is a bad idea.&#160; Even though your generated code will compile using different build configurations, the #defines you set on the swig command line (-DMY_DEFINE) should be consistent in all build configurations.&#160; Varying the generator configuration can lead to things like stale files (unless you rebuild every time), as well as an inconsistent wrapper interface.</p>
<h3>2 ) Automatic Macro Expansion Is Evil</h3>
<p>If you have a macro that does different things based upon Debug or Release SWIG will pick the one matching the -D command line define and expand the code to match that.&#160; It won&#8217;t just insert your macro as you would if you were writing the code naturally.&#160; The best way I&#8217;ve found to prevent this from happening is just block off the macros it&#8217;s trying to expand.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#if !defined(SWIG)</span>
    <span style="color: #339900;">#define MYNEW ...</span>
    <span style="color: #339900;">#define MYDELETE ...</span>
<span style="color: #339900;">#endif</span></pre></td></tr></table></div>

<h3>3 ) Const Correctness</h3>
<p>C# does not have a way of mimicking C++&#8217;s const&#8217;ness.&#160; So objects returned in C++ from a function as const Type&amp; are treated as a regular pointer.&#160; Allowing your wrapper user to try and change the underlying data in the object even though it&#8217;s suppose to be const.&#160; To avoid this, you should probably remove the function entirely.&#160; Alternatively you can treat it as a new object and generate a new C++ object on the heap and copy it there so that any edits made to it wont be reflected in the actual object.&#160; But that has many negative sides also, like not being able to see changes in the object the reference was for.&#160; You also create a memory leak since SWIG has no idea it has created a new object, so you need to find a way to tell swig you created new memory.&#160; To create a wrapper class for it you can use the %typemap(out) macro like this,</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #000040;">%</span>typemap<span style="color: #008000;">&#40;</span>out<span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span> Vector3<span style="color: #000040;">&amp;</span> <span style="color: #000040;">%</span><span style="color: #008000;">&#123;</span> $result <span style="color: #000080;">=</span> <span style="color: #0000dd;">new</span> Vector3<span style="color: #008000;">&#40;</span><span style="color: #000040;">*</span>$<span style="color:#800080;">1</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #000040;">%</span><span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<h3>4 ) Return Reference</h3>
<p>By default SWIG treats TYPE &amp; as a pointer.&#160; Which is in some sense good and in others horrible.&#160; Like it makes perfect sense for a List class to have a function called Get(int index) that returns the TYPE&amp; to the object in the list.&#160; In a case like that, treating it as a pointer is fantastic.&#160; However, suppose you have the following C++ class,</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">class</span> Matrix
<span style="color: #008000;">&#123;</span>
    Matrix<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    Matrix<span style="color: #000040;">&amp;</span> Identity<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>Then in C# you do the following,</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="cpp" style="font-family:monospace;">Matrix4 wrappedMatrix <span style="color: #000080;">=</span> <span style="color: #0000dd;">new</span> Matrix4<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #0000ff;">return</span> wrappedMatrix.<span style="color: #007788;">Identity</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></td></tr></table></div>

<p>Creating the Matrix4 from C# sets the flag that SWIG needs to delete the underlying native Matrix4 instance when this object goes out of scope.</p>
<p>Next we call Identity() which using the default C# generated code will return a pointer to the same native Matrix4 instance, but a new managed C# wrapper class will be used to wrap it.&#160; This new C# class will have the flag set to NOT clean up the memory since it&#8217;s just holding onto a pointer and wasn&#8217;t responsible for creating it.</p>
<p>Finally we return the C# object created using the Identity() method, which is a different instance than the one wrappedMatrix points to, even though they both point to the same native class.&#160; Then we return from the scope we are in, wrappedMatrix is garbage collected, the Identity() spawned matrix continues to live on in managed code, but is now pointing at a completely bogus location in memory since wrappedMatrix was responsible for deleting the native object and did so.</p>
<p>So how do you solve this one?&#160; The easiest way is the same as #3, safe bet is just to not have those functions wrapped, instead provide functions that return void.&#160; Ideally, SWIG would provide some way for me to markup the class and say,</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="cpp" style="font-family:monospace;">SWIG_THIS Matrix<span style="color: #000040;">&amp;</span> Identity<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #666666;">// SWIG_THIS doesn't actually exist.</span></pre></td></tr></table></div>

<p>SWIG_THIS would indicate that I&#8217;m just returning the same object, so instead of creating a new wrapper C# class when you return from the wrapped Identity() function, just return &quot;this&quot;.</p>
<h3>5 ) Customizable Allocation</h3>
<p>There&#8217;s no standardized way of changing the allocation mechanism SWIG uses.&#160; It will create/destroy arrays using stock malloc and free.&#160; It will create/destroy everything else using new/delete.&#160; The way around this is to modify the SWIG script files it uses to generate the default code. (swig\Lib\*.swg,*.i).&#160; In particular, carrays.i for changing array allocation.&#160; For object creation, you&#8217;ll have to modify swigs scripts for your destination language of choice.&#160; For C# you can find them here (swig\Lib\csharp\csharp.swg).</p>
<h3>6 ) Alignment Fail</h3>
<p>SWIG and aligned data types don&#8217;t get along together.&#160; Presumably you have your own allocator and have either A) Have overridden global new/delete, B) Created a common base class with new/delete functions, or C) Created a macro to use on every class you want to override new/delete on.&#160; If you have then in the case of object creation, you&#8217;re safe.&#160; Though you may want to replace SWIGs use of new/delete anyway with your new/delete macros as I am sure you&#8217;d like to make sure you track exactly where those allocations came from.&#160; If you have any arrays SWIG creates, you&#8217;ll definitely need to replace their usage of malloc and free with one going through your allocator so that you can handle alignment.</p>
<h3>7 ) Nested Types</h3>
<p>SWIG doesn&#8217;t handle nested data types.&#160; I haven&#8217;t found a way around it, you just have to pull them out of the parent class if you want them wrapped.</p>
<h3>8 ) Where Are The Out/Ref Parameter Flags</h3>
<p>SWIG doesn&#8217;t map method parameters that are passed by (non-const) reference as &quot;out&quot; or &quot;ref&quot; parameters in C#.&#160; The way around this is to do the following,</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #000040;">%</span>include <span style="color: #FF0000;">&quot;typemaps.i&quot;</span>
&nbsp;
<span style="color: #000040;">%</span>define <span style="color: #000040;">%</span>TypeOutParam<span style="color: #008000;">&#40;</span>TYPE<span style="color: #008000;">&#41;</span>
    <span style="color: #000040;">%</span>apply TYPE<span style="color: #000040;">&amp;</span> OUTPUT <span style="color: #008000;">&#123;</span> TYPE<span style="color: #000040;">&amp;</span> <span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
<span style="color: #000040;">%</span>enddef
&nbsp;
<span style="color: #000040;">%</span>TypeOutParam<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">bool</span><span style="color: #008000;">&#41;</span>
<span style="color: #000040;">%</span>TypeOutParam<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span><span style="color: #008000;">&#41;</span>
<span style="color: #000040;">%</span>TypeOutParam<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">float</span><span style="color: #008000;">&#41;</span>
...</pre></td></tr></table></div>

<p>That will make all instances of those types being used in the context void MyMethod(int&amp; x, int&amp; y) would be mapped to void MyMethod(out int x, out int y) in C#.&#160; Alternatively if I had done,</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #000040;">%</span>include <span style="color: #FF0000;">&quot;typemaps.i&quot;</span>
&nbsp;
<span style="color: #000040;">%</span>define <span style="color: #000040;">%</span>TypeRefParam<span style="color: #008000;">&#40;</span>TYPE<span style="color: #008000;">&#41;</span>
    <span style="color: #000040;">%</span>apply TYPE<span style="color: #000040;">&amp;</span> INOUT <span style="color: #008000;">&#123;</span> TYPE<span style="color: #000040;">&amp;</span> <span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
<span style="color: #000040;">%</span>enddef
&nbsp;
<span style="color: #000040;">%</span>TypeRefParam<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">bool</span><span style="color: #008000;">&#41;</span>
<span style="color: #000040;">%</span>TypeRefParam<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span><span style="color: #008000;">&#41;</span>
<span style="color: #000040;">%</span>TypeRefParam<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">float</span><span style="color: #008000;">&#41;</span>
...</pre></td></tr></table></div>

<p>The method void MyMethod(int&amp; x, int&amp; y) would be mapped to void MyMethod(ref int x, ref int y) in C#.</p>
<h3>9 ) IntPtr == void* </h3>
<p>For some unknown reason SWIG does not map void* to anything useful in C#.&#160; So I came up with the following to map it to the IntPtr struct in C#.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #000040;">%</span>typemap<span style="color: #008000;">&#40;</span>ctype<span style="color: #008000;">&#41;</span>  <span style="color: #0000ff;">void</span> <span style="color: #000040;">*</span> <span style="color: #FF0000;">&quot;void *&quot;</span>
<span style="color: #000040;">%</span>typemap<span style="color: #008000;">&#40;</span>imtype<span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">void</span> <span style="color: #000040;">*</span> <span style="color: #FF0000;">&quot;IntPtr&quot;</span>
<span style="color: #000040;">%</span>typemap<span style="color: #008000;">&#40;</span>cstype<span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">void</span> <span style="color: #000040;">*</span> <span style="color: #FF0000;">&quot;IntPtr&quot;</span>
<span style="color: #000040;">%</span>typemap<span style="color: #008000;">&#40;</span>csin<span style="color: #008000;">&#41;</span>   <span style="color: #0000ff;">void</span> <span style="color: #000040;">*</span> <span style="color: #FF0000;">&quot;$csinput&quot;</span>
<span style="color: #000040;">%</span>typemap<span style="color: #008000;">&#40;</span>in<span style="color: #008000;">&#41;</span>     <span style="color: #0000ff;">void</span> <span style="color: #000040;">*</span> <span style="color: #000040;">%</span><span style="color: #008000;">&#123;</span> $<span style="color:#800080;">1</span> <span style="color: #000080;">=</span> $input<span style="color: #008080;">;</span> <span style="color: #000040;">%</span><span style="color: #008000;">&#125;</span>
<span style="color: #000040;">%</span>typemap<span style="color: #008000;">&#40;</span>out<span style="color: #008000;">&#41;</span>    <span style="color: #0000ff;">void</span> <span style="color: #000040;">*</span> <span style="color: #000040;">%</span><span style="color: #008000;">&#123;</span> $result <span style="color: #000080;">=</span> $<span style="color:#800080;">1</span><span style="color: #008080;">;</span> <span style="color: #000040;">%</span><span style="color: #008000;">&#125;</span>
<span style="color: #000040;">%</span>typemap<span style="color: #008000;">&#40;</span>csout<span style="color: #008000;">&#41;</span>  <span style="color: #0000ff;">void</span> <span style="color: #000040;">*</span> <span style="color: #008000;">&#123;</span> <span style="color: #0000ff;">return</span> $imcall<span style="color: #008080;">;</span> <span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<h3>10 ) string[] –&gt; char**</h3>
<p>For C# string[] is not mapped to char**.&#160; So here is how you map it,</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #000040;">%</span>typemap<span style="color: #008000;">&#40;</span>ctype<span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">char</span><span style="color: #000040;">**</span> <span style="color: #FF0000;">&quot;char**&quot;</span>
<span style="color: #000040;">%</span>typemap<span style="color: #008000;">&#40;</span>imtype<span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">char</span><span style="color: #000040;">**</span> <span style="color: #FF0000;">&quot;string[]&quot;</span>
<span style="color: #000040;">%</span>typemap<span style="color: #008000;">&#40;</span>cstype<span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">char</span><span style="color: #000040;">**</span> <span style="color: #FF0000;">&quot;string[]&quot;</span>
&nbsp;
<span style="color: #000040;">%</span>typemap<span style="color: #008000;">&#40;</span>csin<span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">char</span><span style="color: #000040;">**</span> <span style="color: #FF0000;">&quot;$csinput&quot;</span>
<span style="color: #000040;">%</span>typemap<span style="color: #008000;">&#40;</span>csout, excode<span style="color: #000080;">=</span>SWIGEXCODE<span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">char</span><span style="color: #000040;">**</span>, <span style="color: #0000ff;">const</span> <span style="color: #0000ff;">char</span><span style="color: #000040;">**</span><span style="color: #000040;">&amp;</span> <span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">int</span> ret <span style="color: #000080;">=</span> $imcall<span style="color: #008080;">;</span>$excode
    <span style="color: #0000ff;">return</span> ret<span style="color: #008080;">;</span>
  <span style="color: #008000;">&#125;</span>
<span style="color: #000040;">%</span>typemap<span style="color: #008000;">&#40;</span>csvarin, excode<span style="color: #000080;">=</span>SWIGEXCODE2<span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">char</span><span style="color: #000040;">**</span> <span style="color: #000040;">%</span><span style="color: #008000;">&#123;</span>
    set <span style="color: #008000;">&#123;</span>
      $imcall<span style="color: #008080;">;</span>$excode
    <span style="color: #008000;">&#125;</span> <span style="color: #000040;">%</span><span style="color: #008000;">&#125;</span>
<span style="color: #000040;">%</span>typemap<span style="color: #008000;">&#40;</span>csvarout, excode<span style="color: #000080;">=</span>SWIGEXCODE2<span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">char</span><span style="color: #000040;">**</span> <span style="color: #000040;">%</span><span style="color: #008000;">&#123;</span>
    get <span style="color: #008000;">&#123;</span>
      <span style="color: #0000ff;">int</span> ret <span style="color: #000080;">=</span> $imcall<span style="color: #008080;">;</span>$excode
      <span style="color: #0000ff;">return</span> ret<span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span> <span style="color: #000040;">%</span><span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #000040;">%</span>typemap<span style="color: #008000;">&#40;</span>in<span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">char</span><span style="color: #000040;">**</span> <span style="color: #000040;">%</span><span style="color: #008000;">&#123;</span> $<span style="color:#800080;">1</span> <span style="color: #000080;">=</span> $input<span style="color: #008080;">;</span> <span style="color: #000040;">%</span><span style="color: #008000;">&#125;</span>
<span style="color: #000040;">%</span>typemap<span style="color: #008000;">&#40;</span>out<span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">char</span><span style="color: #000040;">**</span> <span style="color: #000040;">%</span><span style="color: #008000;">&#123;</span> $result <span style="color: #000080;">=</span> $<span style="color:#800080;">1</span><span style="color: #008080;">;</span> <span style="color: #000040;">%</span><span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<h3>11 ) Operators</h3>
<p>Operators are not wrapped by default.&#160; There is probably a way to map them to operators in C#, but I went with another route.  You can rename them so that they are, for example</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #000040;">%</span><span style="color: #0000dd;">rename</span><span style="color: #008000;">&#40;</span>Add<span style="color: #008000;">&#41;</span> Vector3<span style="color: #008080;">::</span><span style="color: #007788;">operator</span> <span style="color: #000040;">+</span><span style="color: #008080;">;</span></pre></td></tr></table></div>

<h3>12 ) Memory Lifecycle Management</h3>
<p>SWIG has this internal flag in generated classes that tells it if it needs to clean up the memory for the native object when the instance is garbage collected by .Net.&#160; If you have pesky life-cycle management requirements in your system you&#8217;ll need to be able to control this flag on the fly.&#160; The way I&#8217;ve found to do this is with the %typemap macro,</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #000040;">%</span>typemap<span style="color: #008000;">&#40;</span>cscode<span style="color: #008000;">&#41;</span> YOUR_CLASS
<span style="color: #000040;">%</span><span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">bool</span> IsLifecycleManaged
    <span style="color: #008000;">&#123;</span>
        get <span style="color: #008000;">&#123;</span> <span style="color: #0000ff;">return</span> swigCMemOwn<span style="color: #008080;">;</span> <span style="color: #008000;">&#125;</span>
        set <span style="color: #008000;">&#123;</span> swigCMemOwn <span style="color: #000080;">=</span> value<span style="color: #008080;">;</span> <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #000040;">%</span><span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>Now you can control on a per instance basis whether or not .Net garbage collecting the object will clean it up.</p>
<h3>13 ) Callbacks</h3>
<p>If you need to do callbacks use the &quot;%feature(&quot;director&quot;)&quot;.&#160; The documentation explains it more in depth, but it wasn&#8217;t clear to me at first how to do it until I learned about directors.</p>
<h3>14 ) Extension Macro</h3>
<p>You can extend/add functionality to the C++ class prior to the wrapper code getting generated using the &quot;%extend&quot; macro.&#160; The extension code is added as a C function though, so you might wonder, how do I access the class that this function is supposed to be part of.&#160; The code SWIG generates refers to the &#8216;this&#8217; pointer as the variable &#8216;self&#8217; so you can access anything public on the class by just doing</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="cpp" style="font-family:monospace;">self<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>...</pre></td></tr></table></div>

<h3>15 ) Unfriendly Template Class Names</h3>
<p>SWIG generates unfriendly names for templated classes.&#160; The best way around this is to use the %template macro.&#160; It works like this,</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #000040;">%</span><span style="color: #0000ff;">template</span><span style="color: #008000;">&#40;</span>ListFloat<span style="color: #008000;">&#41;</span> List<span style="color: #000080;">&lt;</span><span style="color: #0000ff;">float</span><span style="color: #000080;">&gt;</span><span style="color: #008080;">;</span></pre></td></tr></table></div>

<h3>16 ) Tracking New Memory</h3>
<p>If a function creates a new object and returns it, you need to tell SWIG this, otherwise you&#8217;ll have a memory leak.&#160; By default SWIG does not destroy any object it wraps unless it created a temporary object in the heap for a value type on the stack.&#160; The way you inform swig a method returns a new object is by doing,</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #000040;">%</span>newobject MyFactory<span style="color: #008080;">::</span><span style="color: #007788;">Create</span><span style="color: #008080;">;</span></pre></td></tr></table></div>

<h3>17 ) Casting</h3>
<p>As it stands I don&#8217;t have a good solution to this one.  I thought I should mention it though, because it has been a real thorn in my side.  If you have a method in C++ that you wrap and you return a base class like BaseObject*.  SWIG will wrap that inside a BaseObject C# class.  Now all the other SWIG wrapped classes that derrived from BaseObject will exist and will similarly inherit from a C# BaseObject.</p>
<p>However, attempting to cast your new C# BaseObject into anything other than its parent classes will result in an exception.  Because SWIG has no way of knowing the true type of the BaseObject* it returned from the function.  So the C# object it creates is going to be BaseObject and not the actual C# wrapper class for the native types true type.</p>
<p><a href="http://www.nickdarnell.com/?p=1348">SWIG and a Miss @ nickdarnell.com</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.altdevblogaday.com/2011/05/27/swig-and-a-miss/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Do the Truffle Shuffle to Start</title>
		<link>http://www.altdevblogaday.com/2011/05/12/do-the-truffle-shuffle-to-start/</link>
		<comments>http://www.altdevblogaday.com/2011/05/12/do-the-truffle-shuffle-to-start/#comments</comments>
		<pubDate>Thu, 12 May 2011 18:16:43 +0000</pubDate>
		<dc:creator>Nick Darnell</dc:creator>
		
		<guid isPermaLink="false">http://altdevblogaday.org/?p=5706</guid>
		<description><![CDATA[<h2>Preface</h2>
<p><a href="http://www.nickdarnell.com/wp-content/uploads/2011/05/chunk.png"></a><a href="http://altdevblogaday.com/wp-content/uploads/2011/05/chunk.png"></a><a href="http://altdevblogaday.com/wp-content/uploads/2011/05/chunk.png"><img class="alignright size-medium wp-image-5709" src="http://altdevblogaday.com/wp-content/uploads/2011/05/chunk-300x206.png" alt="" width="300" height="206" /></a>The first time I stepped in front of a depth camera was almost a year ago now.  We had a reference version of a PrimeSense camera that is heavily related to the final hardware that went into Kinect.  The first thing I got to do was make a stick figure guy move around on the screen.  It was very captivating to see him match my movements, even with the occasional arm through my chest Kali-Ma style.</p>
<p><a href="http://www.altdevblogaday.com/2011/05/12/do-the-truffle-shuffle-to-start/" class="more-link">Read more on Do the Truffle Shuffle to Start&#8230;</a></p>
]]></description>
				<content:encoded><![CDATA[<h2>Preface</h2>
<p><a href="http://www.nickdarnell.com/wp-content/uploads/2011/05/chunk.png"></a><a href="http://altdevblogaday.com/wp-content/uploads/2011/05/chunk.png"></a><a href="http://altdevblogaday.com/wp-content/uploads/2011/05/chunk.png"><img class="alignright size-medium wp-image-5709" src="http://altdevblogaday.com/wp-content/uploads/2011/05/chunk-300x206.png" alt="" width="300" height="206" /></a>The first time I stepped in front of a depth camera was almost a year ago now.  We had a reference version of a PrimeSense camera that is heavily related to the final hardware that went into Kinect.  The first thing I got to do was make a stick figure guy move around on the screen.  It was very captivating to see him match my movements, even with the occasional arm through my chest Kali-Ma style.</p>
<p>Those first days were filled with lots of experimentation because everything was new to us in this world of full body motion gaming.  Which reminds me…if you ever want to see a cool effect, go grab a large mirror and hold it in front of a depth camera at an angle; now you’re really playing with portals!</p>
<h2>Introduction</h2>
<p>With all the time I’ve spent around these cameras I wanted to capture some thoughts on some problems developing games and software driven by full body motion input.</p>
<h2>Unnatural User Input</h2>
<p>Lately I’ve come to find the statement “Natural User Input” a bit of a misnomer.  There are still many technological and human hurdles that have to be overcome with time and good ideas before the interaction is truly natural.  The problem with natural is that it’s different for everyone, which generally forces you to make it unnatural for some group of people.  Also with the limitations of the current technology you will often find yourself making unnatural concessions to make something work.</p>
<p>A great example of this is getting detected by the camera, often referred to in the office as “Doing the Truffle Shuffle”.  Some skeleton SDKs require a pose or gesture to be detected as an active user.  For example, OpenNI has the “Psi” pose.  Some ask you to wave your hand.  Some just work, like Kinect but even so many games have logic layered on top about when a user can join that is highly varied and currently unnatural because there isn’t one consistent way yet.</p>
<p>Another good example of this is turning.  If I asked you to turn, how would you do it?</p>
<p style="padding-left: 30px">Q: Would you naturally turn, away from the TV?<br />
A: No, then you couldn’t see the TV.</p>
<p style="padding-left: 30px">Q: So would you turn your whole body and continue to face the TV, or just your shoulders?<br />
A: If you turn your body naturally you’ll occlude half of your body, making it harder to detect other actions simultaneously (Walk + Turn).  Also many skeleton SDKs have varying levels of success tracking shoulder angle and occlusion of the shoulder usually causes them to move around.</p>
<p style="padding-left: 30px">Q: What about if we let the hands determine turning, moving them left to move left, right to move right?<br />
A: Good in some contexts, like skiing and horseback riding.  It’s very unnatural when walking around. It also prevents you from using the hands to do other things at the same time.  It’s also very hard to hold for long periods of time if you have to keep them there.</p>
<p style="padding-left: 30px">Q: How about leaning left to turn left, leaning right to turn right?<br />
A: It’s great from a technological standpoint. It won’t ever occlude any part of the body.  Very easy to do for all users.  Very easy to hold for long durations.  Can be combined with many other actions.  However, it’s completely unnatural.</p>
<p>The best advice I can give here is to get people to test out your ideas.  I can’t tell you how many times I’ve thought to have solved a problem only to see a tester or coworker break it almost immediately.  If you can help it, find new people to try out the game.  We refer to them as untrained users around the office.  For these systems you’ll find that over time the system trains you back.  You learn just the right movements without thinking about it, which will lead to a false sense of improvement in your gesture detection code.</p>
<p>I haven’t seen it happen yet, but I suspect many motion games in the future will actually ship with multiple ways of handling the same input and users will select the one they prefer.  In the same way we have inverted controls and different control schemes.</p>
<h2>Noise</h2>
<p>The cameras are not perfect and they’re mapping a physical space to some finite number of pixels.  Surfaces that poorly reflect infrared, other infrared sources (like the sun), and even the manner in which the cameras define a contiguous surface can cause variations from frame to frame leading to lots of jaggy shifting edges on objects.  This jitteriness influences the volume of an object and thus the calculated positions of bones in a skeleton are shifting too.</p>
<p>So you’ve got to find a way to smooth out the data without adding lag to the propagation of player movements onto the character. The best way we’ve found is with a predictive filter.  They average in old frames with the current frames data, but are simultaneously predicting N (usually 1) frames forward in time.  The only drawback is they end up over and undershooting the actual curve of motion because it’s predicting the motion is going to continue in the same direction.  Luckily this largely goes unnoticed by users.</p>
<h2>Generally Avoid</h2>
<p>The amount you should avoid each of these varies across cameras and skeleton SDKs, but generally speaking this is my own list of things you should try to avoid.</p>
<ul>
<li>Small Motions – Detecting them is very difficult, they are very easy to confuse with noise.</li>
<li>Holding hard poses – It’s hard to hold your arms out for extended periods of time.</li>
<li>Motions near the body – Occlusion problems, bone loss.</li>
<li>Fast motions – Most of the consumer grade depth cameras right now are running at 30 FPS.  It’s very easy to move faster than the segmentation / skeleton prediction code is willing to bet you’ve moved and will happily ignore your motion.</li>
<li>Extreme poses – Poses most people would have trouble making.  Not just because people have trouble making them, but because most of the skeleton SDKs are not trained for unusual body positions.</li>
<li>Sitting – It’s is generally not handled well across skeleton SDKs.  The overall skeleton becomes a lot less trustworthy.</li>
</ul>
<h2>That&#8217;s Normal Right?</h2>
<p>All the skeleton SDKs I’ve used so far don’t generally return you anything other than the rawest of the raw bone positions.  Which is generally a good thing; you wouldn’t want them to hide the raw data from you.  However, this will tends to result in moments when your hand will penetrate your chest, your knee will flip backwards and you’ll have your leg behind your back.</p>
<p>So it becomes important to try and avoid these events by using joint constraints.  Even though the skeleton SDKs usually have bone confidence numbers, they’re not comparing confidence based upon how a normal human can move.  It’s based on can they clearly see something they think is a body part.  If so they will report things like, 100% confident your leg has driven itself up into your chest.</p>
<h2>Time</h2>
<p>Timing is very difficult.  The user has to predict how long he is going to take to move, while at the same time accounting for how long the avatar will take to move, plus how long the gesture detection will take to detect his action.  Making it very hard for him to predict when he has to jump or duck or move to the side.</p>
<p>In these situations feedback that he has done the right thing, as well as how long he has left to do the right thing can be important.  One handy trick when compressing timespans to play back animation is Bullet-Time.  Imagine a player running and jumping hurdles.   There’s this unknown zone that once entered there will not be enough time to playback the animation to jump the hurdle without it looking sped up bizarrely fast.  However with bullet time, if you detect the gesture just in time, you can slow down time long enough to play back the animation and also indicate to the user, “Hey, you almost missed that one”.  Bullet-Time is also handy for just giving the user more time to make a split second decision, and then as soon as they’ve made it, speed back up.</p>
<h2>Just a Little Bit Closer&#8230;</h2>
<p>Depth perception is another frustrating problem.  Users have really poor perception about how far away objects are from their avatar that they can interact with.  Luckily there are many ways around this problem.</p>
<ul>
<li>Depth Cues – shadows do a great job of helping to show distance as you get closer to an object</li>
<li>UI Visual Cues – Visual feedback that you can now interact with the object is important.  If I’m playing a volleyball game, changing the halo around the ball from red to green to indicate I can now jump and hit it can be valuable feedback, because it’s hard estimating how high my character can jump, or when they can jump.</li>
<li>Camera angle is everything.  Having the right angle to the object can make it much easier to tell depth.</li>
<li>Audio Cues – I don’t see these get used very often, but sound is a great way to indicate action is required, or success or failure on the user’s part.</li>
</ul>
<p><a href="http://www.nickdarnell.com/?p=1337" target="_blank">Do the Truffle Shuffle to Start @ nickdarnell.com</a> / <a href="http://activate3d.com/?p=518" target="_blank">activate3d.com</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.altdevblogaday.com/2011/05/12/do-the-truffle-shuffle-to-start/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Chaining Compute Shaders</title>
		<link>http://www.altdevblogaday.com/2011/04/27/chaining-compute-shaders/</link>
		<comments>http://www.altdevblogaday.com/2011/04/27/chaining-compute-shaders/#comments</comments>
		<pubDate>Wed, 27 Apr 2011 14:15:54 +0000</pubDate>
		<dc:creator>Nick Darnell</dc:creator>
		
		<guid isPermaLink="false">http://altdevblogaday.org/?p=4864</guid>
		<description><![CDATA[<p><a href="http://altdevblogaday.com/wp-content/uploads/2011/04/Bitchin-fast-3D-2000.jpg"><img style="margin: 2px 0px 5px 10px;padding-left: 0px;padding-right: 0px;float: right;padding-top: 0px;border-width: 0px" src="http://altdevblogaday.com/wp-content/uploads/2011/04/Bitchin-fast-3D-2000_thumb.jpg" border="0" alt="Bitchin-fast-3D-2000" width="230" height="302" align="right" /></a>My new side-side project with DirectX 11 is something that has been bothering me for awhile.  Chaining compute shaders with variable length output.  Chaining compute shaders that output the same amount of work is trivial, in fact, you’d likely just merge the code into one uber compute shader.</p>
<p><a href="http://www.altdevblogaday.com/2011/04/27/chaining-compute-shaders/" class="more-link">Read more on Chaining Compute Shaders&#8230;</a></p>
]]></description>
				<content:encoded><![CDATA[<p><a href="http://altdevblogaday.com/wp-content/uploads/2011/04/Bitchin-fast-3D-2000.jpg"><img style="margin: 2px 0px 5px 10px;padding-left: 0px;padding-right: 0px;float: right;padding-top: 0px;border-width: 0px" src="http://altdevblogaday.com/wp-content/uploads/2011/04/Bitchin-fast-3D-2000_thumb.jpg" border="0" alt="Bitchin-fast-3D-2000" width="230" height="302" align="right" /></a>My new side-side project with DirectX 11 is something that has been bothering me for awhile.  Chaining compute shaders with variable length output.  Chaining compute shaders that output the same amount of work is trivial, in fact, you’d likely just merge the code into one uber compute shader.</p>
<p>One thing shader authors routinely take advantage of is the fact that they can do some simple work to filter out data points in the vertex shader by placing them outside the frustum.  Giving them a cheap and effective way to remove all that data that would otherwise go to the pixel shader or show up as a branch if this were all happening in a single shader.  By being able to have multiple stages the shader cores are all able to stay busy instead of several of them idling from a discard branch.</p>
<p>Which brings me to the main question of this blog post,</p>
<h3>How can I chain compute shaders to consume variable amounts of data to duplicate this same staging capability?</h3>
<p>So here is our scenario, you have a FilterComputeShader (FilterCS) and a HardWorkComputeShader (HardWorkCS).  The FilterCS will use the <a href="http://msdn.microsoft.com/en-us/library/ff471448%28VS.85%29.aspx" target="_blank">AppendStructuredBuffer</a> to output some amount of data less than or equal to some incoming amount of data.  Now, you place one or more branch statements in your FilterCS that way by the time you get to the HardWorkCS you’ve got all your shader cores being utilized and no branching there (hopefully)</p>
<p>After appending some amount of data onto this buffer, how will we know how many threads or thread groups to spawn after the FilterCS runs?</p>
<p><a href="http://msdn.microsoft.com/en-us/library/ff476393%28v=vs.85%29.aspx" target="_blank">ID3D11DeviceContext::CopyStructureCount</a></p>
<p>You can use it to copy the count from the buffer into some other buffer on the GPU.  Now you could then read back that value from the GPU and use it in another call to Dispatch, but forcing a sync point between the CPU and GPU isn’t a good idea.  Which is why there is this.</p>
<p><a href="http://msdn.microsoft.com/en-us/library/ff476406%28v=VS.85%29.aspx" target="_blank">ID3D11DeviceContext::DispatchIndirect</a></p>
<p>You can pass that same buffer you just copied that size of the append buffer into as input into this function to subsequently spawn some number of <strong><span style="text-decoration: underline">thread groups</span></strong>.</p>
<p>For this to work, your compute shaders would all need to be defined with [numthreads(1, 1, 1)].  That way the number of threads matched the number of thread groups.</p>
<p>However, part of utilizing our shader cores effectively will be making sure we aren’t defining our compute shaders with [numthreads(1, 1, 1)].  Ideally the number of threads matches or is some multiple of the size of the GPU’s wavefront.  If you don’t do this you’ll be vastly underutilizing the GPU.</p>
<p>Crap…</p>
<p>So how do we get around this without adding a sync point between the CPU and the GPU?</p>
<h3>Another compute shader! :D</h3>

<div class="wp_syntax"><table><tr><td class="code"><pre class="hlsl" style="font-family:monospace;">RWBuffer dispatchBuff : register(u0);
&nbsp;
[numthreads(1, 1, 1)]
void UpdateIndirectDispatchBuffer()
{
    dispatchBuff[0] = (uint)ceil(dispatchBuff[0] / NumThreadGroupSize);
}</pre></td></tr></table></div>

<p>I know I said [numthreads(1, 1, 1)] was bad, but here it makes sense, we&#8217;re only going to spawn a single thread group and thread to do make this quick fixup to the buffer we’re going to use for DispatchIndrect so that instead of telling it the number of threads to dispatch, we tell it the number of thread groups to dispatch, based on the number of threads in our thread group for the x thread group dimension.</p>
<p>So now the code flow looks like this,</p>
<ul>
<li>Dispatch(FilterCS);</li>
<li>CopyStructureCount():</li>
<li>Dispatch(UpdateIndirectDispatchBuffer);</li>
<li>DispatchIndirect(HardWorkCS);</li>
</ul>
<p>I haven’t finished the side project this is part of so I don’t know if the overhead of using the AppendStructuredBuffer + 2 additional Dispatch calls is actually better than doing it all in a single compute shader with a branch.  My current assumption is that this method will be far superior in cases where you’re filtering.</p>
<p>If the branch were simply doing hard work X or hard work Y, there’s obviously no win.  But given the choice hard work X or no work.  Your best bet is to be able to handle each stage in a separate Dispatch, or so I think.  The performance results of the side project this is part of will probably make for a good post.</p>
<p><a href="http://www.nickdarnell.com/?p=1331" target="_blank">Chaining Compute Shaders @ nickdarnell.com</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.altdevblogaday.com/2011/04/27/chaining-compute-shaders/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Subsurface Scattering Volumes</title>
		<link>http://www.altdevblogaday.com/2011/04/12/subsurface-scattering-volumes/</link>
		<comments>http://www.altdevblogaday.com/2011/04/12/subsurface-scattering-volumes/#comments</comments>
		<pubDate>Tue, 12 Apr 2011 04:36:27 +0000</pubDate>
		<dc:creator>Nick Darnell</dc:creator>
		
		<guid isPermaLink="false">http://altdevblogaday.org/?p=3769</guid>
		<description><![CDATA[<p>I didn’t get to see many sessions at GDC this year.  One I really wanted to see but didn’t make it to was the talk on “<a href="http://zigguratvertigo.com/2011/03/07/gdc-2011-approximating-translucency-for-a-fast-cheap-and-convincing-subsurface-scattering-look/" target="_blank">Approximating Translucency for a Fast, Cheap and Convincing Subsurface Scattering Look</a>” given by Colin Barré-Brisebois.  However, I read over the slides online as soon as they were posted.</p>
<p><a href="http://www.altdevblogaday.com/2011/04/12/subsurface-scattering-volumes/" class="more-link">Read more on Subsurface Scattering Volumes&#8230;</a></p>
]]></description>
				<content:encoded><![CDATA[<p>I didn’t get to see many sessions at GDC this year.  One I really wanted to see but didn’t make it to was the talk on “<a href="http://zigguratvertigo.com/2011/03/07/gdc-2011-approximating-translucency-for-a-fast-cheap-and-convincing-subsurface-scattering-look/" target="_blank">Approximating Translucency for a Fast, Cheap and Convincing Subsurface Scattering Look</a>” given by Colin Barré-Brisebois.  However, I read over the slides online as soon as they were posted.</p>
<p>The valuable nugget from the talk is the idea that you can approximate object density by inverting the mesh normals and have your DCC tool generate a map of ambient occlusion.  Which is a pretty good approximation of internal surfaces proximity to each other.  Which gives you a map of thickness you can use in SSS calculations.</p>
<p>My graphics side project of late has been experimenting with Morgan McGuire’s <a href="http://graphics.cs.williams.edu/papers/AOVHPG10/" target="_blank">Ambient Occlusion Volume</a> paper.  Which generates AO by creating prisms around each polygon to accumulate AO where they overlap against an opposing surface.</p>
<p>So of course while reading the Approximating Translucency paper I thought I could take Ambient Occlusion Volumes, flip the normals the prisms are generated on so that I could generate AO for the purpose of calculating thickness for subsurface scattering.  <em>voilà </em>Subsurface Scattering Volumes.</p>
<p>So the awesome bit here is because the thickness is calculated based on AO generated from the geometry directly.  You could use this method if you wanted an approximation of subsurface scattering to work on something that was destroyable or morphable.  Now, I doubt the applicability of this method in any immediate sense, but it’s still a neat effect.</p>
<p>Enough words, bring on the moving pictures!</p>
<p style="text-align: left"><!--YouTube Error: bad URL entered--></p>
<p style="text-align: left"><!--YouTube Error: bad URL entered--></p>
<p style="text-align: center"><a href="http://altdevblogaday.com/wp-content/uploads/2011/04/SSSV_5.jpg"><img class="aligncenter" style="margin-top: 2px;margin-bottom: 5px;padding-left: 0px;padding-right: 0px;padding-top: 0px;border: 0pt none" src="http://altdevblogaday.com/wp-content/uploads/2011/04/SSSV_5_thumb.jpg" border="0" alt="SSSV_5" width="600" height="465" /></a></p>
<p>NOTE: This is cross-posted at my personal blog, <a href="http://www.nickdarnell.com/?p=1316" target="_blank">nickdarnell.com</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.altdevblogaday.com/2011/04/12/subsurface-scattering-volumes/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Teaching the Toaster to Feel Love [Part 2]</title>
		<link>http://www.altdevblogaday.com/2011/03/28/teaching-the-toaster-to-feel-love-part-2/</link>
		<comments>http://www.altdevblogaday.com/2011/03/28/teaching-the-toaster-to-feel-love-part-2/#comments</comments>
		<pubDate>Mon, 28 Mar 2011 07:24:34 +0000</pubDate>
		<dc:creator>Nick Darnell</dc:creator>
		
		<guid isPermaLink="false">http://altdevblogaday.org/?p=2611</guid>
		<description><![CDATA[<p><img style="padding-left: 0px;padding-right: 0px;float: left;padding-top: 0px;border-width: 0px" src="http://altdevblogaday.com/wp-content/uploads/2011/03/toaster1.jpg" border="0" alt="toaster1" width="400" height="176" align="left" /></p>
<p>Welcome to part two of my series of posts on teaching a toaster to feel love, or introduction to machine learning for the rest of us.</p>
<p>If you missed part one, you can read it <a href="http://altdevblogaday.com/2011/03/13/teaching-the-toaster-to-feel-love-part-1/" target="_blank">here</a>.</p>
<p><a href="http://www.altdevblogaday.com/2011/03/28/teaching-the-toaster-to-feel-love-part-2/" class="more-link">Read more on Teaching the Toaster to Feel Love [Part 2]&#8230;</a></p>
]]></description>
				<content:encoded><![CDATA[<p><img style="padding-left: 0px;padding-right: 0px;float: left;padding-top: 0px;border-width: 0px" src="http://altdevblogaday.com/wp-content/uploads/2011/03/toaster1.jpg" border="0" alt="toaster1" width="400" height="176" align="left" /></p>
<p>Welcome to part two of my series of posts on teaching a toaster to feel love, or introduction to machine learning for the rest of us.</p>
<p>If you missed part one, you can read it <a href="http://altdevblogaday.com/2011/03/13/teaching-the-toaster-to-feel-love-part-1/" target="_blank">here</a>.</p>
<p>Last time I thought in part two I would be able to start posting some code.  However after writing a good chunk of the code I wanted to include in part two, I realized it was way too much to cover in one post.  So I’m going use this post to introduce some function calls and classes, but focus mostly on the process.</p>
<h2>Download</h2>
<p>A fairly popular SVM library is LibSVM, which you can download on this page <a href="http://www.csie.ntu.edu.tw/~cjlin/libsvm/" target="_blank">here</a>, or directly <a href="http://www.csie.ntu.edu.tw/~cjlin/cgi-bin/libsvm.cgi?+http://www.csie.ntu.edu.tw/~cjlin/libsvm+zip">here</a>.  There are several variations of that library in many languages.  It’s what I will be referencing in this and in coming posts.</p>
<h2>Features</h2>
<p>The first and most important step when using an SVM is to figure out what constitutes a good feature vector.  If you remember from part one, a feature is just a floating point value with importance to the programmer but is just another number in a dimension for the SVM.  The feature vector is the collection of these features, constituting a single example.</p>
<p>The features that your feature vector is composed of is the real secret sauce, everything else in the process is more or less mechanical.  Sadly it also means I can’t tell you what to put into your feature vector because everyone’s problem needs a different vector.  However, there are lots of tactics that apply in all cases that you should remember.</p>
<h6>Normalization</h6>
<p>You need to remember to normalize your feature data.  I don’t mean just normalizing the feature vector like you would a float4. I mean, each feature should be normalized either from 0..1 or –1..1 (not both).  For two important reasons, numerical stability and weighting.</p>
<p>Numerical Stability &#8211; We don’t want to get into situations where we might be hitting up against the 32bit limit with very large numbers during the testing process.</p>
<p>Weighting &#8211; We also don’t want one dimension to greatly out weigh any other dimension accidentally.  Because an SVM is attempting to find the hyperplane with the greatest margin that divides the data, having one dimension in the feature vector run from 0..1000 while everything else runs from 0..1, would mean that almost all your data would be classified based on that one feature because it might find a hyperplane that gave it a margin of 500 in that one dimension while every other feature’s margin added together doesn’t even approach 500.  So that feature more than any of the others would be what determined classification.</p>
<h6>Numericalization</h6>
<p>Not all kinds of data that you may want to include in a feature vector exists naturally as a number.  Sometimes you need to transform a category into a feature.  Your first thought might be to divide the category number by the number of categories and <em>voilà</em>, a floating point number.  However, this is not the best approach.  The better approach is to represent each category like you would if you wanted to represent them as bit fields, and dedicate one dimension for each category.  So if there were 3 possible categories, you’d represent the first category with 3 features, 0, 0, 1, the second category as 0, 1, 0, and the third as 1, 0, 0.</p>
<h6>Rephrasing</h6>
<p>It’s important to remember that a machine doesn’t have insight.  If I gave you position and time data, you could tell me other information like velocity and acceleration.  However an SVM doesn’t know what the data is, so it can’t derive any sort of insightful observations about it.  So it’s important to remember to rephrase the same data in different ways that tell the machine new things about the same data.</p>
<h6>Ordering</h6>
<p>The order of the features does not influence the process, however you need to keep your ordering consistent.</p>
<h6>Size</h6>
<p>Your feature vectors should all have the same number of features.</p>
<h2>Training</h2>
<p>The second step in the SVM process is training the SVM model using the feature vectors you’ve built containing all the useful features you think will help distinguish one thing from another thing.</p>
<p><img style="padding-left: 0px;padding-right: 0px;float: none;margin-left: auto;margin-right: auto;padding-top: 0px;border-width: 0px" src="http://altdevblogaday.com/wp-content/uploads/2011/03/spceaser.jpg" border="0" alt="spceaser" width="460" height="356" /></p>
<p>To train an SVM you need 2 sets of data; the training set and the validation set.  While you can use just a training dataset, it’s not advisable as it can lead to a problem known as ‘over fitting’.  Which is just a fancy way of saying, you’ve created an SVM that ONLY recognizes the training data and doesn’t handle variation very well.</p>
<p>The most common and easiest training methodology to use for beginners with an SVM is known as a grid search.  The process is pretty simple, to determine the best support vectors we’ll iterate over a series of parameter magic number combinations (Cost (C) and Gamma) until we find a pair that seeds the process of dividing of our data the best.  The kernel type we’ll be using to divide our data will be the Radial Basis Function (RBF) kernel.  You could devise any kind of kernel you wanted to divide the data, however there’s a bunch of existing kernel functions people tend to re-use because they work in a wide number of situations.</p>
<p>The Cost (C) parameter represents the penalty for miss-classification in the training process.  The higher the cost, the more rigid/strict the trained model will be for the training data.  Meaning that while you may see fewer miss classified items (for data that resembles closely the training set), the model will be much more likely ‘over fit’ the problem.</p>
<p>Gamma is a parameter that’s used directly in the RBF kernel function.</p>
<p>The first step in the training process is to create an svm_problem object.  It holds all the training data in the form of svm_nodes.  Then we define our svm_parameter, which represents the kernel type and all the other parameters used in the training and creation process of our svm_model.</p>
<p>To determine the best parameters in the grid search we’ll use the svm_cross_validation method that’s built into SVMLib.  Which allows us to take the ground truth training data and see how well it would be classified given the resulting model from the selected Gamma and C parameters.</p>
<p>Now, you don’t have to vary JUST the C and Gamma parameters you could try different kernel functions.  But the more things you vary in the grid search the longer the training process is going to take.  Which, depending on the size of your dataset may be hours.  It all depends on how many different combinations of parameters you are willing to try.</p>
<p>Side Note: Should you ever run into a training time problem, I highly recommend looking into ways to running the training process in distributed environment.  The training process for an SVM is VERY amenable to being parallelized.  There are also some GPU (CUDA) implementations of the SVM that I’ve read about but not yet tried.  You can always use one SVM implementation for finding the best parameters and another for runtime usage.</p>
<p>Once you’ve determined the best C and Gamma based on the miss-classifications from your svm_cross_validation grid search process.  You’ll just need to call svm_train to generate the final model that you can save out and re-use at runtime to classify datasets that you want the SVM to identify for you.</p>
<p>Once you have your model you’ll want to run your validation dataset against it to see how well it runs on data that wasn’t used to train it.  If you find that the model performs very poorly you’ve over-fit to the training data.  So you may want to save the N best C and Gamma parameters you found and actually generate N final svm_models and pick the one that performs the best on the validation dataset.</p>
<p>At the very least, having a validation dataset allows you to make sure there’s no regressions in the unit tests of your code.</p>
<h2>Next Time</h2>
<p>Next time I should be able to walk you through the code easier and just reference concepts introduced in this post.</p>
<p>Cross-posted to my <a href="http://www.nickdarnell.com/?p=1298" target="_blank">personal blog</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.altdevblogaday.com/2011/03/28/teaching-the-toaster-to-feel-love-part-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Teaching the Toaster to Feel Love [Part 1]</title>
		<link>http://www.altdevblogaday.com/2011/03/13/teaching-the-toaster-to-feel-love-part-1/</link>
		<comments>http://www.altdevblogaday.com/2011/03/13/teaching-the-toaster-to-feel-love-part-1/#comments</comments>
		<pubDate>Sun, 13 Mar 2011 20:01:57 +0000</pubDate>
		<dc:creator>Nick Darnell</dc:creator>
		
		<guid isPermaLink="false">http://altdevblogaday.org/?p=1935</guid>
		<description><![CDATA[<p><img style="padding-left: 0px;padding-right: 0px;float: left;padding-top: 0px;border-width: 0px" src="http://altdevblogaday.com/wp-content/uploads/2011/03/Farnsworth.png" border="0" alt="Farnsworth" width="140" height="156" align="left" />Every so often you find yourself confronted with a problem that you as a human can answer, but to express it algorithmically in code would be very difficult.  In these times people often turn to machine learning to solve their problem.  However, machine learning is a enormous field and it’s difficult finding where to even begin.</p>
<p><a href="http://www.altdevblogaday.com/2011/03/13/teaching-the-toaster-to-feel-love-part-1/" class="more-link">Read more on Teaching the Toaster to Feel Love [Part 1]&#8230;</a></p>
]]></description>
				<content:encoded><![CDATA[<p><img style="padding-left: 0px;padding-right: 0px;float: left;padding-top: 0px;border-width: 0px" src="http://altdevblogaday.com/wp-content/uploads/2011/03/Farnsworth.png" border="0" alt="Farnsworth" width="140" height="156" align="left" />Every so often you find yourself confronted with a problem that you as a human can answer, but to express it algorithmically in code would be very difficult.  In these times people often turn to machine learning to solve their problem.  However, machine learning is a enormous field and it’s difficult finding where to even begin.</p>
<p>As new devices are developed to interface with humans for games and other applications; accelerometers, gyroscopes, depth cameras with skeletal tracking and who knows what next.  Interpreting the input reliably has become a much more difficult task.</p>
<p>So I wanted to write a series of posts which hopefully will benefit anyone wanting to learn how to use one variety of machine learning; Support Vector Machines (SVM).</p>
<h2>Introduction</h2>
<p>An SVM makes a binary decision about a feature vector deciding if it is closer to the positive class or the negative class.  During the training process for an SVM it’s given many feature vectors that exist as examples of what kinds of data appears in the positive vs. negative classifications.</p>
<p>Here is a breakdown of a class,</p>
<p>Class<br />
{<br />
Feature Vector 1 { Feature 1, Feature 2, … },<br />
Feature Vector 2 { Feature 1, Feature 2, … },<br />
…<br />
}</p>
<p>So what is a feature?  A feature is simply a floating point number that has some special meaning as far as the programmer is concerned.  To the SVM however, it’s just a number in a dimension.  So as you can see the inputs for a class are nothing more than a list of vectors filled with floats.</p>
<p>To determine the best best hyperplanes (support vectors) that best divide the data you have to train the SVM with example feature vectors for each class.  The trick is finding the support vectors that maximize the margin (distance) on either side of the hyperplane.  The easier it is to split the data, the less likely you are to see incorrectly classified data when you put the system to use.</p>
<table border="0" cellspacing="0" cellpadding="0" width="400">
<tbody>
<tr>
<td width="400" valign="top"><img style="padding-left: 0px;padding-right: 0px;padding-top: 0px;border-width: 0px" src="http://altdevblogaday.com/wp-content/uploads/2011/03/Svm_max_sep_hyperplane_with_margin.gif" border="0" alt="Svm_max_sep_hyperplane_with_margin" width="223" height="240" /></td>
</tr>
</tbody>
</table>
<p>So let’s talk examples, if I were to say the following feature vector { 0.25 } was the only feature vector in the positive class, and feature vector { 0.75 } was the only feature vector in the negative class, the ideal support vector would be { 0.50 } because it best divides the data, giving the greatest margin between both classification feature vectors.  Which is easy to see when you have only 1 feature and 1 feature vector in either class.  The problem becomes infinitely more difficult when you have classes of 10,000 feature vectors with 600 features in them each being used for training data.</p>
<p>There are several kernel types used to find the best hyperplane through the features. Linear, Radial Basis Function (RBF), Sigmoid, and Polynomial.  From the papers I’ve read, RBF seems to be quite popular.</p>
<h2>Next Time</h2>
<p>For the first post I just wanted to introduce the basic vocabulary.  Next time I’ll get into actually using them in practice.</p>
<p>Cross-posted from my <a href="http://www.nickdarnell.com/?p=1272" target="_blank">personal blog</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.altdevblogaday.com/2011/03/13/teaching-the-toaster-to-feel-love-part-1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Redneck Cloud Computing</title>
		<link>http://www.altdevblogaday.com/2011/02/26/redneck-cloud-computing/</link>
		<comments>http://www.altdevblogaday.com/2011/02/26/redneck-cloud-computing/#comments</comments>
		<pubDate>Sat, 26 Feb 2011 20:59:31 +0000</pubDate>
		<dc:creator>Nick Darnell</dc:creator>
		
		<guid isPermaLink="false">http://altdevblogaday.org/?p=1275</guid>
		<description><![CDATA[<p>Every now and then I wonder what decisions might be different if I had access to a cloud of machines dedicated to baking game assets or solving other highly parallelizable tasks. So I started looking into what options were available to someone wanting to distribute a ton of work over many machines. I found lots of options but all of them suffered from one or more of these problems,</p>
<p><a href="http://www.altdevblogaday.com/2011/02/26/redneck-cloud-computing/" class="more-link">Read more on Redneck Cloud Computing&#8230;</a></p>
]]></description>
				<content:encoded><![CDATA[<p>Every now and then I wonder what decisions might be different if I had access to a cloud of machines dedicated to baking game assets or solving other highly parallelizable tasks. So I started looking into what options were available to someone wanting to distribute a ton of work over many machines. I found lots of options but all of them suffered from one or more of these problems,</p>
<ul>
<li>Specialized language</li>
<li>Specialized framework (tied to language)</li>
<li>Specialized operating system</li>
<li>New versions would require manual deployment</li>
<li>Non-commercial license</li>
</ul>
<p>I wanted a solution that didn’t require a specialized framework or language because chances are I would find something I wanted to distribute that I didn’t want to completely rewrite. No specialized OS, I want to be able to slave any unused windows machine in the office (dedicated farms are really expensive). Also if I need to perform new operations or fix a bug, I don’t want to reinstall / deploy new versions to everyone’s machine.</p>
<p>So I decided to roll my own solution and share the design. I’ll share the finished version of the software once I’ve completed it.</p>
<p>Let’s start with a usecase. I have an executable and a pile of data on disk that I would like to distribute over a ton of machines. I was able to quickly modify the existing program to have a command line option that allowed it to process a range of the data on disk instead of all of it.  How do I distribute this work over multiple machines so that I process all the data on disk?</p>
<p>Each time we execute the program with some amount of data to be processed, let’s call that a task. Each task will be processed by some machine in the cloud. To submit the tasks, we will need some common / cross language mechanism of communication. I chose named pipes just because they are easy to use on windows.</p>
<p>To submit tasks to the named pipe, we could either write a simple reusable program that reads task descriptions from a file and submits them to the named pipe, or we could wrap the named pipe communication in a C++ library so that C++ and C# (P-Invoke) could both reuse the logic inside of many various tools (including the generic reusable program that reads task descriptions from files or std::in).</p>
<p>Each machine has a single server running on it. I chose to write the server in C#, but it’s a blackbox as far as your tasks are concerned so you could use something different if you wanted.</p>
<p>Once the server receives the task description via the named pipe it looks at the list of servers it knows about.  The serves are detected using a simple UDP broadcast.</p>
<p>Using .Net remoting I then connect to each one of these machines to see what it can offer me.</p>
<p>Now each of these machines could have who knows what installed on them, and you’re about to transfer and run an exe that may have requirements, like .Net 4.0. So each task needs to contain a list of requirements. For now, I’ve got them scoped to 3 things, defined environment variables, registry key/values and .Net version. You could probably drop the .Net version if you just always keep your server written using the latest version so it’s a prerequisite for any machine on your network.</p>
<p>Now I can try and reserve a slot on the machine, if I fail to reserve a slot because of a race condition I move onto the next server.</p>
<p>Having reserved a slot (some machines that are idle or dedicated may have multiple slots), I need to transfer the executable and all the data files listed inside the task description that are claimed to be needed by the task.</p>
<p>I tell the remote server to then begin running the task in a new thread and I continue looking for empty slots in the cloud to submit tasks to.</p>
<p>After I discover that a task has finished, everything that is different in the remote folder where the task was dumped and subsequently executed is then transferred back to the local machine that submitted the task.</p>
<p>The named pipe that was used to submit one or more tasks remains open the whole time and is notified after each task is finished and the data successfully transferred back to the host machine.</p>
<p>So that’s the design in a nutshell. It’s not a solution designed to solve every distributed computing problem, but I like that it solves a very common pattern of problems I see fairly often in a non-intrusive manner.</p>
<p>I haven’t nailed down yet the best way to prevent the system from being abused by a malicious user.  However, I suspect having a SQL server with the list of MD5 hashes of the executables that are approved for deployment is one idea I’ve been toying with.</p>
<p>Here’s a simple diagram to help explain the task submission process. Because who doesn’t love diagrams.</p>
<p><a href="http://altdevblogaday.com/wp-content/uploads/2011/02/image.png"><img style="padding-left: 0px;padding-right: 0px;float: none;margin-left: auto;margin-right: auto;padding-top: 0px;border: 0px" src="http://altdevblogaday.com/wp-content/uploads/2011/02/image_thumb.png" border="0" alt="image" width="482" height="362" /></a></p>
<p>Also posted on my <a href="http://www.nickdarnell.com/?p=1269" target="_blank">personal blog</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.altdevblogaday.com/2011/02/26/redneck-cloud-computing/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>Documentation [Easy Mode]</title>
		<link>http://www.altdevblogaday.com/2011/02/11/documentation-easy-mode/</link>
		<comments>http://www.altdevblogaday.com/2011/02/11/documentation-easy-mode/#comments</comments>
		<pubDate>Fri, 11 Feb 2011 17:00:29 +0000</pubDate>
		<dc:creator>Nick Darnell</dc:creator>
		
		<guid isPermaLink="false">http://altdevblogaday.org/2011/02/11/documentation-easy-mode/</guid>
		<description><![CDATA[<p>(Also posted on <a href="http://www.nickdarnell.com" target="_blank">http://www.nickdarnell.com</a>)</p>
<p>I’ve talked about <a href="http://www.atomineerutils.com/" target="_blank">AtomineerUtils</a> before on my blog very briefly several months back when I first  discovered it.  I’ve now been using it pretty much straight since then  and I can’t live without it at this point.</p>
<p><a href="http://www.altdevblogaday.com/2011/02/11/documentation-easy-mode/" class="more-link">Read more on Documentation [Easy Mode]&#8230;</a></p>
]]></description>
				<content:encoded><![CDATA[<p>(Also posted on <a href="http://www.nickdarnell.com" target="_blank">http://www.nickdarnell.com</a>)</p>
<p>I’ve talked about <a href="http://www.atomineerutils.com/" target="_blank">AtomineerUtils</a> before on my blog very briefly several months back when I first  discovered it.  I’ve now been using it pretty much straight since then  and I can’t live without it at this point.</p>
<p>So what does it do?</p>
<p>It takes an educated guess at what your method does based on the  method name, return type and parameters and inserts a pretty good stub  of documentation to get you started.</p>
<p>Think about it… if you write Doxygen style comments in your code, how  much time do you lose to just formatting and tagging each parameter and  then repeatedly copying and pasting the same description over and over  for parameters that are basically self explanatory?</p>
<p>The current version lets your organization share the same preference  files so you can all share the same rules to assist the tool generate  the first stab at documenting your code.  The files that allow you to  control the documentation generation are VERY customizable, my favorite  feature is word expansion.  Which allows it to look for patterns in  variables or method names like “MS” and expand them to “milliseconds” in  the documentation.</p>
<p>I whipped together some examples to show you what it produces out of the box…</p>
<p><strong>Before:</strong></p>
<div>
<div class="syntaxhighlighter  cpp">
<table border="0">
<tbody>
<tr>
<td class="gutter"></td>
<td class="code">
<div class="container">
<div class="line number1 index0 alt2"><code class="cpp color1 bold">int</code> <code class="cpp plain">GetTimeInMS();</code></div>
<div class="line number3 index2 alt2"><code class="cpp keyword bold">void</code> <code class="cpp plain">SendPlayerDeathMsg(Player player,</code></div>
<div class="line number4 index3 alt1"><code class="cpp spaces"> </code><code class="cpp keyword bold">const</code> <code class="cpp color1 bold">char</code><code class="cpp plain">* howPlayerDied,</code></div>
<div class="line number5 index4 alt2"><code class="cpp spaces"> </code><code class="cpp keyword bold">const</code> <code class="cpp color1 bold">char</code><code class="cpp plain">* wherePlayerDied);</code></div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<p><strong>After:</strong></p>
<div>
<div class="syntaxhighlighter  cpp">
<table border="0">
<tbody>
<tr>
<td class="gutter"></td>
<td class="code">
<div class="container">
<div class="line number1 index0 alt2"><code class="cpp comments">/**</code></div>
<div class="line number2 index1 alt1"><code class="cpp spaces"> </code><code class="cpp comments">* @brief   Gets the time in milliseconds.</code></div>
<div class="line number3 index2 alt2"><code class="cpp spaces"> </code><code class="cpp comments">*</code></div>
<div class="line number4 index3 alt1"><code class="cpp spaces"> </code><code class="cpp comments">* @return  The time in milliseconds.</code></div>
<div class="line number5 index4 alt2"><code class="cpp comments">**/</code></div>
<div class="line number6 index5 alt1"><code class="cpp color1 bold">int</code> <code class="cpp plain">GetTimeInMS();</code></div>
<div class="line number8 index7 alt1"><code class="cpp comments">/**</code></div>
<div class="line number9 index8 alt2"><code class="cpp spaces"> </code><code class="cpp comments">* @brief   Sends a player death message.</code></div>
<div class="line number10 index9 alt1"><code class="cpp spaces"> </code><code class="cpp comments">*</code></div>
<div class="line number11 index10 alt2"><code class="cpp spaces"> </code><code class="cpp comments">* @param   player          The player.</code></div>
<div class="line number12 index11 alt1"><code class="cpp spaces"> </code><code class="cpp comments">* @param   howPlayerDied   Describes how player died.</code></div>
<div class="line number13 index12 alt2"><code class="cpp spaces"> </code><code class="cpp comments">* @param   wherePlayerDied The where player died.</code></div>
<div class="line number14 index13 alt1"><code class="cpp comments">**/</code></div>
<div class="line number15 index14 alt2"><code class="cpp keyword bold">void</code> <code class="cpp plain">SendPlayerDeathMsg(Player player,</code></div>
<div class="line number16 index15 alt1"><code class="cpp spaces"> </code><code class="cpp keyword bold">const</code> <code class="cpp color1 bold">char</code><code class="cpp plain">* howPlayerDied,</code></div>
<div class="line number17 index16 alt2"><code class="cpp spaces"> </code><code class="cpp keyword bold">const</code> <code class="cpp color1 bold">char</code><code class="cpp plain">* wherePlayerDied);</code></div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<p>It has a tool menu button you can use for generating the  documentation, but I prefer to bind it to a key, makes it a lot easier  to use.  Here are the steps if you don’t know how.</p>
<table border="0" width="400">
<tbody>
<tr>
<td width="400" valign="top">Go to Tools &gt; Customize</td>
</tr>
<tr>
<td width="400" valign="top"><a href="http://www.nickdarnell.com/wp-content/uploads/2011/02/Customize.png"><img style="background-image: none; border: 0px none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px;" title="Customize" src="http://img1.nickdarnell.com/http://www.nickdarnell.com/wp-content/uploads/2011/02/Customize_thumb.png&amp;t=da2a0e7225ccfd8693bb1ca873129bad" border="0" alt="Customize" width="500" height="388" /></a></td>
</tr>
<tr>
<td width="400" valign="top">Click on Keyboard… in the bottom left and  you can setup a key binding to the AddDocComment action, personally I  use Ctrl+D.  All you need to do then to generate the doc block is have  your cursor just above or on the method or class you want documented and  just hit Ctrl+D.</td>
</tr>
<tr>
<td width="400" valign="top"><a href="http://www.nickdarnell.com/wp-content/uploads/2011/02/Keyboard.png"><img style="background-image: none; border: 0px none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px;" title="Keyboard" src="http://img1.nickdarnell.com/http://www.nickdarnell.com/wp-content/uploads/2011/02/Keyboard_thumb.png&amp;t=da2a0e7225ccfd8693bb1ca873129bad" border="0" alt="Keyboard" width="500" height="290" /></a></td>
</tr>
</tbody>
</table>
<p>Remember though, this is not a replacement for writing actual  documentation.  Don’t let yourself get into the habit of hitting Ctrl+D  and moving on.  AtomineerUtils will get you started, but you still need  to verify and improve its results.  If you find yourself correcting the  same stuff over and over though, try creating a rule for it.</p>
<p>Happy  documenting.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.altdevblogaday.com/2011/02/11/documentation-easy-mode/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>The Art of the 48 Hour Game</title>
		<link>http://www.altdevblogaday.com/2011/01/27/the-art-of-the-48-hour-game/</link>
		<comments>http://www.altdevblogaday.com/2011/01/27/the-art-of-the-48-hour-game/#comments</comments>
		<pubDate>Thu, 27 Jan 2011 08:00:00 +0000</pubDate>
		<dc:creator>Nick Darnell</dc:creator>
		
		<guid isPermaLink="false">http://altdevblogaday.org/2011/01/27/the-art-of-the-48-hour-game/</guid>
		<description><![CDATA[<p>The idea isn&#8217;t new, a few people get together one weekend with a  common goal; make a game in 48 hours. I have game jams on the mind  because the <a href="http://www.globalgamejam.org/" target="_blank">Global Game Jam</a> is this weekend. So I thought I might do a post on hosting and participating in a successful Jam.</p>
<p><a href="http://www.altdevblogaday.com/2011/01/27/the-art-of-the-48-hour-game/" class="more-link">Read more on The Art of the 48 Hour Game&#8230;</a></p>
]]></description>
				<content:encoded><![CDATA[<p>The idea isn&rsquo;t new, a few people get together one weekend with a  common goal; make a game in 48 hours. I have game jams on the mind  because the <a href="http://www.globalgamejam.org/" target="_blank">Global Game Jam</a> is this weekend. So I thought I might do a post on hosting and participating in a successful Jam.</p>
<p>I&rsquo;ve been party to five game jams over the last few years and I&rsquo;ve  made some observations of what works really well and what doesn&rsquo;t.</p>
<h3>1) The Organizer</h3>
<p>Every Jam needs an organizer, the guy who aligns everyone and makes  sure there&rsquo;s a Jam Site and that that everything is ready to go long  before anyone else shows up. There&rsquo;s a fair amount of work that goes  into a Jam, especially if you require internet access. A lot of  businesses that might like or be open to hosting it tend to nix it  because of the work, risk or liability issues. Depending on the size of  the Jam, the organizer also needs to recruit helpers. People that have  experience with Jams and can help people out that are new to the  experience and possibly to the technology primarily being used in the  Jam. Also, don&rsquo;t forget to setup a source control server for your  participants. Otherwise how will they collaborate?</p>
<h3>2) Pick a Framework</h3>
<p>We tend to use XNA for our Game Jams. While I&rsquo;m sure you could use  UDK or Unity for a Game Jam, those frameworks have a much higher  barrier to entry. If you are unfamiliar with the technology, you&rsquo;ll  spend a lot of time chasing down tutorials and you just don&rsquo;t have that  kind of time in a jam. Doing an XNA game in a weekend for someone who  has never touched XNA is far more achievable. But you should ask your  participants, heck, I almost want to host a Jam focused around Little  Big Planet 2.</p>
<h3>3) The Schedule</h3>
<p>Typically a Jam runs Friday to Sunday. Starting on Friday around 5-6  o&rsquo;clock and ending at approximately the same time on Sunday to do  presentations. We&rsquo;ve found that the first night is usually not the time  to start burning the candle at both ends; most people are already tired  from work. You should recommend to the jammers to get into groups, talk  about the game, go get some food and then do a little work on it. Go  home and get plenty of rest and come in Saturday bright and early and  ready to write a ton of code for 12+ hours.</p>
<h3>4) Simple and Creative Theme</h3>
<p>Your theme should be creative and simple. Something that allows for a  lot of creativity but doesn&rsquo;t completely open the players up to making  whatever game they want. The global game jam themes tend to actually be  my least favorite. The group organizing it tends to pick themes that are kind of vague, and actually a little hard to turn into concrete ideas for  games. Last year&rsquo;s theme was a little better than their first, &ldquo;Deception &ndash; and you had to  incorporate a net, a set or a pet&rdquo;. The year before that it was &ldquo;As long  as we have each other, we&rsquo;ll never run out of problems&rdquo;. Personally, I  find these kinds of themes a little annoying because they tend to turn  into a challenge of &ldquo;how do I morph my game idea into this vague theme&rdquo;,  which I don&rsquo;t think is what you want.</p>
<p>You should really try and give your jammers something concrete to  build from. For example, the local jam we do in Raleigh, NC has had  themes like &ldquo;Triangles&rdquo; and &ldquo;Blocks&rdquo;. My personal favorite was Madlibs.  Everyone comes to the jam with 5 nouns, 5 adjectives and 5 verbs. We  then entered them into a simple website which randomized everyone&rsquo;s  words into game titles in the form of &ldquo;Adjective Noun Verb&rdquo;. So you  might see game titles like &ldquo;Meaty Nun Flyer&rdquo;, &ldquo;Musical Dragon Twirler&rdquo;  and &ldquo;Pixelated Martini Roller&rdquo; just to name a very small number of the  ones generated. I like this method a lot because a title like &ldquo;Meaty Nun  Flyer&rdquo; is evocative, immediately conjuring up possible game mechanics  and a mental image. Something I find very important when I&rsquo;m trying to  crank out a game in 48 hours.</p>
<h3>5) The Grouping</h3>
<p>After the theme has been announced you should give everyone 15  minutes or so to come up with an elevator pitch for their game. If you  are doing something like madlibs you skip this step. Then have each  person with an idea stand up and pitch it. Have someone write down the  title and a 3 word description. After everyone finishes, now comes the  least fun part, the puppy killing phase. You need to reduce the number  of game ideas to the ones people actually want to work on.</p>
<p>We tend to do this by first getting a general consensus of what  people would even be interested in working on. So just go down each game  title, remind them what it was and then have them raise their hand if  they would want to work on it. People can vote multiple times in this  phase. You should cut anything from the running that doesn&rsquo;t at least  have 3-4 people even interested in it (numbers vary based on the size of  the jam).</p>
<p>After you&rsquo;ve narrowed the list you should have the final grouping  phase. Start by going down the titles left and asking people to vote  once. Ask them to pick the title they want to work on, even if no one  else wanted to work on it. People can skip this phase if they are  unsure. This will give you the list of games that are going to be made.  From here, anyone left undecided can just join the group they are most  interested in.</p>
<h3>6) Forget</h3>
<p>Forget everything you ever learned about software engineering. It&rsquo;s  actually nice to remove yourself from the mindset of someone writing  production code 50+ hours a week. Copy paste code, make everything  public. I mean you&rsquo;ve got 48 hours; it&rsquo;s really not the time to worry  about overhead, performance or maintenance.&nbsp; Always take the quick and  dirty path to getting the game done, you simply don&rsquo;t have time to  constantly refactor systems during this process.</p>
<h3>7) Gameplay First</h3>
<p>Get the mechanics and controls of the game working before anything  else. You need to try and aim for getting the game done by Sunday at  noon and leaving the next 4-5 hours to do the random bits like improve  graphics (maybe add a neat shader effect) and add a title screen, all of  the polish items basically. It also means you&rsquo;ll be having fun on the  last day instead of stressing over your game only being half done.</p>
<h3>8) The End</h3>
<p>Just some final thoughts I have on the subject. If you&rsquo;re a student  you should come to game jams. Industry folks do participate and it&rsquo;s a  good way to make contacts.&nbsp; If not, at the very least you&rsquo;ll have  something cool to show a potential employer. If you&rsquo;re already in the  game industry, you should participate in game jams. The experience can  be very invigorating to return to a simpler time when games or projects  don&rsquo;t stretch on for several months or years. It&rsquo;s nice to be able to  sit back and feel like you&rsquo;ve accomplished something that is a whole  instead of merely a part in such a short time.</p>
<p><a href="http://www.nickdarnell.com/?p=1198" target="_blank">Cross posted from my personal blog.</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.altdevblogaday.com/2011/01/27/the-art-of-the-48-hour-game/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic page generated in 1.324 seconds. -->
<!-- Cached page generated by WP-Super-Cache on 2013-05-16 00:21:48 -->
<!-- Compression = gzip -->