<?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; Stuart Riffle</title>
	<atom:link href="http://www.altdevblogaday.com/author/stuart-riffle/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.altdevblogaday.com</link>
	<description>Each day a little more #gamedev love</description>
	<lastBuildDate>Thu, 17 May 2012 03:06:10 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Get your hands dirty</title>
		<link>http://www.altdevblogaday.com/2011/08/01/get-your-hands-dirty/</link>
		<comments>http://www.altdevblogaday.com/2011/08/01/get-your-hands-dirty/#comments</comments>
		<pubDate>Mon, 01 Aug 2011 06:45:47 +0000</pubDate>
		<dc:creator>Stuart Riffle</dc:creator>
				<category><![CDATA[#gamedev]]></category>
		<category><![CDATA[Game design]]></category>
		<category><![CDATA[General Interest]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[design]]></category>
		<category><![CDATA[game design]]></category>
		<category><![CDATA[riffle]]></category>
		<category><![CDATA[stuart riffle]]></category>

		<guid isPermaLink="false">http://altdevblogaday.com/?p=13035</guid>
		<description><![CDATA[<p>Imagine I&#8217;m opening a restaurant.</p>
<p>I lease a prime location, buy high quality equipment, and bring aboard some experienced (and expensive) chefs. But instead of letting my chefs build out the menu, I give that responsibility to a small team of Meal Designers.</p>
<p><a href="http://www.altdevblogaday.com/2011/08/01/get-your-hands-dirty/" class="more-link">Read more on Get your hands dirty&#8230;</a></p>
]]></description>
			<content:encoded><![CDATA[<p>Imagine I&#8217;m opening a restaurant.</p>
<p>I lease a prime location, buy high quality equipment, and bring aboard some experienced (and expensive) chefs. But instead of letting my chefs build out the menu, I give that responsibility to a small team of Meal Designers.</p>
<p>These people are passionate about food; they love to eat it, and to talk about it. They are familiar with current food trends, and regularly try new dishes from nice restaurants around the city. In fact, they&#8217;re a lot like the chefs. The difference is that they don&#8217;t actually make food, and plenty of them don&#8217;t think it&#8217;s important to know how.</p>
<p>This causes a number of problems. For a start, they have a hard time communicating to the chefs how the elements of their meals should taste, smell, and feel. They also have a hard time articulating what they like or need changed. Dishes are worked out by a long process of iteration, which is made more difficult because cooking involves making tradeoffs, and changing the salt level (for example) might affect the texture. </p>
<p>They also don&#8217;t have a complete understanding of what sort of things are hard to make, and which are easy. Or which things can be cranked out in big batches by a line cook, and which much be done to order by a more skilled worker. Or which techniques are &#8220;forgiving&#8221; in a hectic kitchen, and which require strict attention to come out properly (reducing overall kitchen bandwidth). Or what can be done readily with existing kitchen equipment, and what would require costly upgrades to do efficiently. Or the alternatives that might be available for a dish, to get a similar result with lower food cost, less labor, fewer expensive specialists, shorter cooking time, or less risk. You get the idea.</p>
<p>By the time opening day rolls around, the meal designers will generally be pretty satisfied with what they&#8217;ve got, though the menu will be shorter than they had hoped. The kitchen will have been trashed a bit by the elaborate cooking procedures that evolved in response to design changes as the dishes were tuned. Everyone will be a bit burned out. And most people will wish they could go back and start over, given all that they had learned, and do things a bit differently.</p>
<p>That&#8217;s crazy, right? Well, that&#8217;s how a lot of big budget games are made. In fact, as projects get larger, the hierarchy that emerges makes it more likely to be true. And in some places, the line between designer and manager is a blurry one, which puts the designers in charge of running the kitchen too.</p>
<p>Now, before my game designer friends get all huffed up, I&#8217;m not trying to pick on you, and I have to point out that this goes both ways. A chef can learn every technique under the sun, but still not have the taste or imagination to create new food. Plenty of developers spend all their time repairing small pieces of a big machine, but have no idea how to make &#8220;fun&#8221; happen. How empty would it be to spend your career building guitars, but never learn to play music? Code doesn&#8217;t make video games, any more than wood can make songs. Those things are just tools&#8230; the good stuff only happens when they move.</p>
<p>However, I do think that if you want to design games, you have a responsibility to understand how they work, and be at least able to cobble together prototypes independently. This is your medium, after all. You don&#8217;t have to be an elite hacker. The important thing is to have some clay under your nails. Designing systems is hard, and getting good at it requires a lot of practice and failure. You need to be able to do that for yourself, or you&#8217;re going to waste a lot of time. Trust me, you can do it.</p>
<p>Good games are art machines. They are sensitive living systems. They are huge, twisted robots with shiny faces, made out of clicks and taps and a little bit of luck. If we want to make great ones, we all need to embrace both the craft and the art in what we do, and whether you&#8217;re a developer or designer, there is plenty of both required. </p>
<p>Anyway, that&#8217;s what I think.</p>
<p>[If you'd like to follow my own development work, go to <a title="pureenergygames.com" href="http://pureenergygames.com" target="_blank">pureenergygames.com</a> and subscribe to the RSS feed!]</p>
]]></content:encoded>
			<wfw:commentRss>http://www.altdevblogaday.com/2011/08/01/get-your-hands-dirty/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>High voltage retro: CRT emulation for pixel art</title>
		<link>http://www.altdevblogaday.com/2011/07/18/high-voltage-retro-crt-emulation-for-pixel-art/</link>
		<comments>http://www.altdevblogaday.com/2011/07/18/high-voltage-retro-crt-emulation-for-pixel-art/#comments</comments>
		<pubDate>Mon, 18 Jul 2011 04:09:57 +0000</pubDate>
		<dc:creator>Stuart Riffle</dc:creator>
				<category><![CDATA[#gamedev]]></category>
		<category><![CDATA[General Interest]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Visual Arts]]></category>
		<category><![CDATA[artifact]]></category>
		<category><![CDATA[crt]]></category>
		<category><![CDATA[emulation]]></category>
		<category><![CDATA[ntsc]]></category>
		<category><![CDATA[riffle]]></category>
		<category><![CDATA[stuart riffle]]></category>

		<guid isPermaLink="false">http://altdevblogaday.com/?p=11664</guid>
		<description><![CDATA[<p><em>[This article can also be read on my <a href="http://pureenergygames.com/blog/34-development/60-crt-emulation-for-pixel-art" title="CRT emulation for pixel art" target="_blank">development blog</a>].<br />
</em></p>
<p>A good way to get an authentic look for retro-pixel art is to simulate the distortion caused by encoding the image into an NTSC signal, decoding it again (as a TV would), and projecting it onto a virtual CRT. This gives you natural-looking artifacts, like fringing and color bleeding.</p>
<p><a href="http://www.altdevblogaday.com/2011/07/18/high-voltage-retro-crt-emulation-for-pixel-art/" class="more-link">Read more on High voltage retro: CRT emulation for pixel art&#8230;</a></p>
]]></description>
			<content:encoded><![CDATA[<p><em>[This article can also be read on my <a href="http://pureenergygames.com/blog/34-development/60-crt-emulation-for-pixel-art" title="CRT emulation for pixel art" target="_blank">development blog</a>].<br />
</em></p>
<p>A good way to get an authentic look for retro-pixel art is to simulate the distortion caused by encoding the image into an NTSC signal, decoding it again (as a TV would), and projecting it onto a virtual CRT. This gives you natural-looking artifacts, like fringing and color bleeding.</p>
<p>It also makes copyrighted hedgehogs look even more dashing.</p>
<p><a href="http://altdevblogaday.com/wp-content/uploads/2011/07/TubeExampleSonic.bmp"><img class="alignnone size-full wp-image-11665" src="http://altdevblogaday.com/wp-content/uploads/2011/07/TubeExampleSonic.bmp" alt="Tube simulator example" /></a></p>
<p>Console emulators do this sometimes, and if you&#8217;re old enough to have actually played games on a CRT TV, it really helps with the sense of immersion. This post gives a quick overview of the process, in case you&#8217;d like to try it for yourself. All of these steps are texture operations performed by pixel shaders.</p>
<ol>
<li>We start by encoding the low resolution input image as an NTSC signal. Each input line is converted into voltage over time, in the same format an NTSC signal would be sent across a wire (except for the sync and color pulse stuff).</li>
<li>A &#8220;cable reflection&#8221; shader smears the signal out a little to the right. I&#8217;m not sure how much it looks like cable reflection, but it does kind of evoke the streaking artifacts you see on some old TVs.</li>
<li>The luma is split out of the signal, and then used in the NTSC decoding process. This is also where the standard OSD parameters (brightness, contrast, sharpness, etc) are applied. Now our image is RGB again.</li>
<li>The image is projected onto a curved tube. This step also takes care of tracing the scan lines and applying the phosphor pattern.</li>
<li>The phosphors from the previous frame are decayed, and the new values are accumulated. This allows for ghosting of moving images.</li>
<li>A standard post-processing stack is applied (bloom, glare, and tone mapping). This give users a taste of the eye-burning glow produced by a real CRT. (Do you remember when staying up late to play games caused physical pain? Kids these days are soft.)</li>
</ol>
<p>Naturally, there are a few problems.</p>
<h4>Moire</h4>
<p><em>AUGH, TEH MOIRE.</em></p>
<p><a href="http://altdevblogaday.com/wp-content/uploads/2011/07/TubeAughMoire.bmp"><img class="alignnone size-full wp-image-11667" src="http://altdevblogaday.com/wp-content/uploads/2011/07/TubeAughMoire.bmp" alt="Moire artifacts" /></a></p>
<p>The combination of the scanlines and the phosphor texture make a little bit of moire pretty much unavoidable. Tuning the brightness, contrast, bloom, scaling, or NTSC parameters can produce moire, or move it from one place to another. The good news is that it looks far worse in screen shots than it does on a running game.</p>
<p>(I cranked it up for the screen shot above&#8230; it&#8217;s not that ghastly under normal circumstances).</p>
<h4>Crosstalk</h4>
<p>Crosstalk between chroma and luma is an key part of the effect, so it&#8217;s a feature, not a bug. The problem is getting it to look bad in a &#8220;good&#8221; way.</p>
<p>A band-stop FIR filter can be used to chop out the chroma signal, but it&#8217;s tough to find the right balance between soft (filter too wide) and stripy (filter too narrow).</p>
<p><a href="http://altdevblogaday.com/wp-content/uploads/2011/07/TubeExampleFilter.bmp"><img class="alignnone size-full wp-image-11668" src="http://altdevblogaday.com/wp-content/uploads/2011/07/TubeExampleFilter.bmp" alt="Tuning crosstalk" /></a></p>
<h4>Phosphor resolution</h4>
<p>My original goal was to have RGB phosphors visible in the image when you examined it up close. That&#8217;s really hard, because if you make the phosphors small enough to look realistic, the RGB pattern blurs out and all you can see is a sort of vertical striping. If you don&#8217;t mipmap or supersample the phosphors, you again get more moire.</p>
<p><a href="http://altdevblogaday.com/wp-content/uploads/2011/07/TubePhosphor.bmp"><img class="alignnone size-full wp-image-11669" src="http://altdevblogaday.com/wp-content/uploads/2011/07/TubePhosphor.bmp" alt="Phosphor patterns at different scales" /></a></p>
<p>These examples have the phosphor texture enlarged and strengthened to exaggerate the problem. I wasn&#8217;t able to get individual phosphors to look good at 720, and they are only barely tolerable at 1080.</p>
<h4>Balance</h4>
<p>Cranking all these techniques to the max gives you a delightfully bad video signal, which is also no fun to look at for more than 10 seconds. There is also a fair bit of interplay between the parameters: adjusting the scanline gap changes the brightness, and so on. Tuning all this can be a touchy process.</p>
<p>Below are the parameters that control the effect. As you can see, there are a lot of ways I can screw things up.</p>
<p><a href="http://altdevblogaday.com/wp-content/uploads/2011/07/TubeTuningSmall.bmp"><img class="alignnone size-full wp-image-11670" src="http://altdevblogaday.com/wp-content/uploads/2011/07/TubeTuningSmall.bmp" alt="Effect tuning parameters" /></a></p>
<h4>Up next</h4>
<p>In future posts I&#8217;ll get into the technical details and show you what the shaders look like. If you&#8217;re interested, go to <a title="pureenergygames.com" href="http://pureenergygames.com" target="_blank">pureenergygames.com</a> and subscribe to the RSS feed!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.altdevblogaday.com/2011/07/18/high-voltage-retro-crt-emulation-for-pixel-art/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Understanding the Fourier transform</title>
		<link>http://www.altdevblogaday.com/2011/05/17/understanding-the-fourier-transform/</link>
		<comments>http://www.altdevblogaday.com/2011/05/17/understanding-the-fourier-transform/#comments</comments>
		<pubDate>Tue, 17 May 2011 04:00:32 +0000</pubDate>
		<dc:creator>Stuart Riffle</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://altdevblogaday.org/?p=5769</guid>
		<description><![CDATA[<p>Yes, I realize that after reading the title of this post, 99% of potential readers just kept scrolling. So to the few of you who clicked on it, welcome! Don&#8217;t worry, this won&#8217;t take long.</p>
<p><a href="http://www.altdevblogaday.com/2011/05/17/understanding-the-fourier-transform/" class="more-link">Read more on Understanding the Fourier transform&#8230;</a></p>
]]></description>
			<content:encoded><![CDATA[<p>Yes, I realize that after reading the title of this post, 99% of potential readers just kept scrolling. So to the few of you who clicked on it, welcome! Don&#8217;t worry, this won&#8217;t take long.</p>
<p>A very long time ago, I was curious how to detect the strength of the bass and treble in music, in order to synchronize some graphical effects. I had no idea how to do such a thing, so I tried to figure it out, but I didn&#8217;t get very far. Eventually I learned that I needed something called a <a title="Fourier transform" href="http://en.wikipedia.org/wiki/Fourier_transform" target="_blank">Fourier transform</a>, so I took a trip to the library and looked it up (which is what we had to do back in those days).</p>
<p>What I found was the Discrete Fourier Transform (DFT), which looks like this:</p>
<p><a href="http://altdevblogaday.com/wp-content/uploads/2011/05/AlienDFT.png"><img class="alignnone size-full wp-image-5771" src="http://altdevblogaday.com/wp-content/uploads/2011/05/AlienDFT.png" alt="The Discrete Fourier Transform" width="172" height="75" /></a></p>
<p>This formula, as anyone can see, makes no sense at all. I decided that Fourier must have been speaking to aliens, because if you gave me all the time and paper in the world, I would not have been able to come up with that.</p>
<p>Eventually, I was able to visualize how it works, which was a bit of a lightbulb for me. That&#8217;s what I want to write about today: an intuitive way to picture the Fourier transform. This may be obvious to you, but it wasn&#8217;t to me, so if you work with audio or rendering, I hope there&#8217;s something here you find useful.</p>
<p>Disclaimer: my math skills are pitch-patch at best, and this is just intended to be an informal article, so please don&#8217;t expect a rigorous treatment. However, I will do my best not to flat-out <em>lie</em> about anything, and I&#8217;m sure people will set me straight if I get something wrong.</p>
<p>A quick bit of background &#8211; what does the Fourier transform <em>do</em>? It translates between two different ways to represent a signal:</p>
<ul>
<li>The <strong>time domain</strong> representation (a series of evenly spaced samples over time)</li>
<li>The <strong>frequency domain</strong> representation (the strength and phase of waves, at different frequencies, that can be used to reconstruct the signal)</li>
</ul>
<p><a href="http://altdevblogaday.com/wp-content/uploads/2011/05/FourierExample.png"><img src="http://altdevblogaday.com/wp-content/uploads/2011/05/FourierExample.png" alt="The difference between the time and frequency domain representations" width="100%" /></a></p>
<p>The picture on the left shows 3 cycles of a sine wave, and the picture on the right shows the Fourier transform of those samples. The output bars show energy at 3 cycles (and, confusingly enough, <em>negative</em> 3 cycles&#8230; more on that below).</p>
<p>The  inputs and outputs are actually complex numbers, so to feed a real signal (like  some music) into the Fourier transform,  we just set all the  imaginary components to zero. And to check the  strength of the  frequency information, we just look at the  magnitude of the  outputs, and ignore the phase. But let&#8217;s never mind all that for now.</p>
<p>What are we trying to accomplish? We&#8217;ve got a sampled signal, and we want to extract frequency information from it. The Fourier transform works on a periodic, or looping signal. This seems like a problem, since we don&#8217;t actually have any signals like that. In practice, you just take a small slice of a longer signal, fade both ends to zero so that they can be joined (which is a whole topic unto itself), and pretend it&#8217;s a loop.</p>
<p>Let&#8217;s make things simple and say that our loop repeats once per second.</p>
<p><a href="http://altdevblogaday.com/wp-content/uploads/2011/05/Signal.png"><img class="alignnone size-full wp-image-5773" src="http://altdevblogaday.com/wp-content/uploads/2011/05/Signal.png" alt="A periodic signal" width="300" height="216" /></a></p>
<p>Picture it as a bead, sliding up and down along a thin rod, tracing out the signal. So as this bead is bobbing up and down, look what happens if we spin the rod at a rate of, say, 10 revolutions per second:</p>
<p><a href="http://altdevblogaday.com/wp-content/uploads/2011/05/Spin10.png"><img class="alignnone size-full wp-image-5777" src="http://altdevblogaday.com/wp-content/uploads/2011/05/Spin10.png" alt="Signal spinning at 10 revs/sec, producing a scribble" width="286" height="267" /></a></p>
<p>We get a scribble, as you&#8217;d expect. And it is roughly centered on the origin.</p>
<p>Now, let&#8217;s assume we know there&#8217;s some energy in the signal at 3 Hz, and we want to measure it. What that means is that on top of whatever else is causing the signal to wobble around, we&#8217;ve added a wave that oscillates 3 times per second. It has a high point every 1/3 of a second, and corresponding low points in between, also spaced 1/3 of a second apart. You can probably see now how we might be able to detect it&#8230; let&#8217;s try spinning our signal at a matching rate of 3 revolutions per second.</p>
<p><a href="http://altdevblogaday.com/wp-content/uploads/2011/05/Spin3.png"><img class="alignnone size-full wp-image-5774" src="http://altdevblogaday.com/wp-content/uploads/2011/05/Spin3.png" alt="Signal spinning at 3 revs/sec, producing an off-center scribble" width="227" height="215" /></a></p>
<p>Since the signal completes a rotation every 1/3 of a second, all the high points in our 3 Hz wave line up at the same part of the rotation, and this pulls the whole scribble off-center. How can we quantify that? The easiest way would be to record a bunch of points as we rotate, and average them to find their midpoint:</p>
<p><a href="http://altdevblogaday.com/wp-content/uploads/2011/05/Spin3Center.png"><img class="alignnone size-full wp-image-5775" src="http://altdevblogaday.com/wp-content/uploads/2011/05/Spin3Center.png" alt="Signal spinning at 3 revs/sec, producing an off-center scribble, with center marked" width="236" height="214" /></a></p>
<p>It makes sense that the distance of this midpoint from the origin is proportional to the strength of the signal, because as the high points in our signal get higher, they will move the scribble farther away. But what if the signal contains no energy at 3 Hz? Let&#8217;s remove the 3 Hz wave and see:</p>
<p><a href="http://altdevblogaday.com/wp-content/uploads/2011/05/Spin3CenterNoEnergy.png"><img class="alignnone size-full wp-image-5776" src="http://altdevblogaday.com/wp-content/uploads/2011/05/Spin3CenterNoEnergy.png" alt="Signal with no 3 Hz energy spinning at 3 revs/sec, producing a centered scribble" width="237" height="235" /></a></p>
<p>Now there is nothing to pull the scribble off center, and all of the other oscillations tend to (approximately) balance each other out.</p>
<p>This looks promising as a way to detect energy at a given frequency. Time to translate it into math! For a looping signal of N samples:</p>
<p><a href="http://altdevblogaday.com/wp-content/uploads/2011/05/DerivedDFT.png"><img class="alignnone size-full wp-image-5772" src="http://altdevblogaday.com/wp-content/uploads/2011/05/DerivedDFT.png" alt="Description of the method, with corresponding parts of the DFT highlighted" width="377" height="166" /></a></p>
<p>(Raising e to an imaginary power produces rotation around a unit circle in the complex plane, according to Euler&#8217;s formula. How? Magic, as far as I can tell. But apparently it&#8217;s true).</p>
<p>So this equation is a little different from what we started with. I&#8217;ve added a normalization factor of 1/N, and changed the sign of the exponent. I also rearranged the terms slightly for clarity. This form is normally called the <em>inverse</em> DFT, which is confusing, but apparently the difference between the DFT and IDFT is a matter of convention, and can depend on the application. So, let&#8217;s call that close enough.</p>
<p><a href="http://altdevblogaday.com/wp-content/uploads/2011/05/AchievementUnlocked.png"><img class="alignnone size-full wp-image-5770" src="http://altdevblogaday.com/wp-content/uploads/2011/05/AchievementUnlocked.png" alt="Achievement Unlocked: Figured out Discrete Fourier Transform" width="429" height="62" /></a></p>
<p>Anyway, once you can &#8220;see&#8221; what&#8217;s going on in your head, a lot of the quirks of working with the DFT become much less mysterious. If you&#8217;ve had to work with DFT output before, you may have wondered:</p>
<ul>
<li><em>Why does the first element in the result (k=0) contain the DC offset?</em> Because in that case, our samples don&#8217;t spin at all, so all we&#8217;re doing is averaging them.</li>
<li><em>Why doesn&#8217;t the DC offset affect the frequency information?</em> Because adding a constant value to all the samples just makes the whole scribble bigger, which doesn&#8217;t affect the midpoint.</li>
<li><em>Why does the second half of the output array contain a mirror image of the first half?</em> It&#8217;s just our old friend aliasing. When calculating the last element (k=N-1), we&#8217;re rotating by (N-1)/N at each step, which is almost all of the way around. This is the same as taking small steps (1/N) in the wrong direction. That&#8217;s why the result at (k=N-1) has the same magnitude as (k=1). It&#8217;s equivalent to processing a negative frequency of (k=-1).</li>
<li><em>Why does a sine wave with amplitude 1.0 come out of the DFT as 0.5?</em> When we spin the sine wave, we get a circle of diameter 1.0, but it&#8217;s midpoint is only half that distance away from the origin.</li>
<li><em>Where is the other half of the energy then?</em> It&#8217;s hiding in the negative frequency part!</li>
</ul>
<p>Hopefully this was more helpful than confusing.</p>
<p>And if you&#8217;d like to get updates on my game development work, come subscribe to my RSS feed at <a title="pureenergygames.com" href="http://pureenergygames.com" target="_blank">pureenergygames.com</a>!</p>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.altdevblogaday.com/2011/05/17/understanding-the-fourier-transform/feed/</wfw:commentRss>
		<slash:comments>96</slash:comments>
		</item>
	</channel>
</rss>

