<?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; Keith Judge</title>
	<atom:link href="http://www.altdevblogaday.com/author/keith-judge/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>I&#8217;m Easily Distracted</title>
		<link>http://www.altdevblogaday.com/2011/09/22/im-easily-distracted/</link>
		<comments>http://www.altdevblogaday.com/2011/09/22/im-easily-distracted/#comments</comments>
		<pubDate>Thu, 22 Sep 2011 13:50:57 +0000</pubDate>
		<dc:creator>Keith Judge</dc:creator>
				<category><![CDATA[General Interest]]></category>

		<guid isPermaLink="false">http://altdevblogaday.com/?p=17010</guid>
		<description><![CDATA[<p><a href="http://altdevblogaday.com/wp-content/uploads/2011/09/shiny-thing1.jpg"><img class="alignright size-full wp-image-17012" src="http://altdevblogaday.com/wp-content/uploads/2011/09/shiny-thing1.jpg" alt="" width="500" height="375" /></a>There, I said it. It&#8217;s not the sort of thing any employer will ever want to hear, but it&#8217;s true. Let me qualify that statement &#8211; I think nearly everyone is easily distracted and the difference lies in how people manage distractions.</p>
<p><a href="http://www.altdevblogaday.com/2011/09/22/im-easily-distracted/" class="more-link">Read more on I&#8217;m Easily Distracted&#8230;</a></p>
]]></description>
			<content:encoded><![CDATA[<p><a href="http://altdevblogaday.com/wp-content/uploads/2011/09/shiny-thing1.jpg"><img class="alignright size-full wp-image-17012" src="http://altdevblogaday.com/wp-content/uploads/2011/09/shiny-thing1.jpg" alt="" width="500" height="375" /></a>There, I said it. It&#8217;s not the sort of thing any employer will ever want to hear, but it&#8217;s true. Let me qualify that statement &#8211; I think nearly everyone is easily distracted and the difference lies in how people manage distractions.</p>
<p>All fields of games development (programming, art, writing, design, sound, etc) require concentration and the application of skills learnt over years. There&#8217;s an intangible &#8220;zone&#8221; where you feel fully focussed and do your best work that&#8217;s not easy to get into, and far too easy to fall out of. Some days you can spend hours in the &#8220;zone&#8221;, other days not at all.</p>
<h2>The Internet</h2>
<p>Being honest, this is the big one for me. A friend might send me a link to a webcomic I&#8217;ve not seen before and before I know it, an hour has passed and I&#8217;ve read half the archive, or I&#8217;ll look something up on Wikipedia and end up reading a stream of completely different (though fascinating) articles.</p>
<h3>A Welcome Distraction?</h3>
<p>This isn&#8217;t always a bad thing though. Sometimes when you&#8217;re stuck on a problem, you need a welcome distraction to help your mind see it from a fresh perspective and the internet is more than willing to provide this.</p>
<p>Social networks are an example. I&#8217;ve certainly found twitter to be very useful as you can get advice and tips from other game devs who have tackled similar problems to what you&#8217;re working on, but social networks are still a bad distraction at times (facebook in particular is becoming much more noise than signal these days for me).</p>
<p>Also, now that I&#8217;m an indie developer, the social networks provide vital channels for promoting and building awareness of <a title="Razorblade Games" href="http://razorbladegames.com" target="_blank">the game I&#8217;m building</a>.</p>
<h3>Dealing With The Internet</h3>
<p>Simply pulling the plug doesn&#8217;t help, because the internet is a vast and immensely valuable resource used all the time for our jobs, so it takes a conscious effort to stay productive with an onslaught of information and entertainment at your fingertips. The trick is in limiting the time spent, and it&#8217;s something I&#8217;ve struggled with at times, but there are strategies.</p>
<p>For example, I recently went on holiday for a week and returned to find almost 1,000 unread items in Google Reader &#8211; that&#8217;s in the order of <em>50,000 a year</em>! I immediately decided to remove the highest volume subscriptions (mostly comedy sites such as Memebase), and now the flow is much more manageable.</p>
<p>Closing browser tabs is another good plan and only checking the networks at particular times of day rather than leaving them open all day. The social networks all seem to put an unread message count in the browser tab, which is a siren call for the easily distracted. Close the tabs till later, and the temptation is gone.</p>
<h2>&#8220;AAA&#8221; Office Distractions</h2>
<p>For most of my career, I&#8217;ve worked in open plan offices with many other people. This is the usual layout in most &#8220;AAA&#8221; game studios. There&#8217;s a certain background noise to all this that you learn to tune out &#8211; air conditioning, PC and dev kit cooling fans (who remembers the noise of the early PS3 dev kits?), traffic outside, etc. If this was all there was to it, then avoiding distractions would be easy, but there are other things which need to be managed in this sort of studio.</p>
<h3>Meetings</h3>
<p>You&#8217;re working away, deep in the middle of a problem when &#8220;DING!&#8221;, up pops Outlook&#8217;s meeting reminder on your screen telling you there&#8217;s a meeting in five minutes about a feature the team is due to start in a few weeks&#8217; time. You have to then shunt all the things you were thinking of out of your mind, gather your notes and start thinking about what you need to say in the meeting. When the meeting is over, it&#8217;s going to take a while to get back up to speed on what you were working on.</p>
<p>Unfortunately, the human mind can&#8217;t suspend and resume threads as thoroughly and quickly as a computer can, so this  process of switching thought processes from one subject to another has an overhead. It not only takes time to get from one train of thought to another, but I find it saps mental energy and makes you tired quicker than focussing on one task for an extended period.</p>
<p>There isn&#8217;t really an ideal solution for this. Meetings need to happen to help the people working on a feature or team all understand what each other is doing, and they can&#8217;t realistically be scheduled for times when everyone has just finished a task and has the time.</p>
<p>The only workable strategy is to think hard when you get a meeting request about whether you really need to be there, rejecting those for which you believe you will have little input. You ought to be able to read the minutes of the meeting afterwards if you need to know what happened (assuming anyone is actually taking minutes, which I&#8217;ve found is uncommon in the games industry). Rejecting meeting requests has another benefit, in that meetings with fewer people tend to be more focussed, so are more likely to finish on time and stay on topic (I have no scientific data for that, only my own experience and anecdotal evidence).</p>
<p>If you&#8217;re creating meetings, sometimes the conclusions of a small meeting can be passed to most people in a daily catchup rather than inviting them all. However, it&#8217;s sometimes a good idea to mark the other people as &#8220;optional&#8221; in a meeting as sometimes people don&#8217;t like things being decided in meetings without their input. If they know that the meeting is going to take place and decide not to attend, they can&#8217;t complain about this.</p>
<p>I&#8217;ve also known people and teams that enforce a &#8220;No Meeting Day&#8221;, once a week or fortnight, which lets people concentrate and get work done without this distraction.</p>
<h3>&#8220;Can I ask a quick question?&#8221;</h3>
<p>You&#8217;re deep in concentration when you get a tap on the shoulder. Someone want to ask you a question. You don&#8217;t know the answer, but you know (or think you know) who does, so you point them in the right direction. You turn back to your work, blink a few times and realise you can&#8217;t remember exactly where you were up to, so it costs you a few minutes to get back on track.</p>
<p>This is a tricky one to manage. I like to be open and would much prefer to answer any questions people have for me straight away, then have people sit there stewing because they think I&#8217;d shout at them for distracting me. In theory, the people to go to with questions are the leads, because it&#8217;s part of their job to mentor their team and deal with communication with people in other teams.</p>
<p>However, in reality you will sometimes <em>know</em> that Person A knows the most about a particular feature/level/thing <em>because they built it</em>, so it&#8217;s best to go directly to them rather than disturb their lead, who will then disturb Person A, which would lead to more people distracted and losing time. I guess what I&#8217;m saying here is, when you have a question to ask &#8211; try to make sure you know who to ask directly &#8211; if not, go to the lead. Person A could also help minimise this by creating an intranet wiki page about the feature which answers common questions.</p>
<p>Another way to deal with this distraction is to tell people something like &#8220;This afternoon, I want to really concentrate and get X finished, so can you please ask me no questions unless it&#8217;s an emergency. Thanks!&#8221;, although you probably wouldn&#8217;t want to do that too often as people might then become reluctant to ask you anything.</p>
<h3>Background Chatter</h3>
<p>With a lot of people in an office, there&#8217;s inevitably going to be chatter going on. The usual remedy is to put on a pair of headphones and listen to music in your own little world instead (I prefer non-vocal ambient electronica when working, but the choice of music is up to you &#8211; if death metal works, then so be it). Just make sure the headphones aren&#8217;t leaking sound to other people. This works well in an office, but I&#8217;ll talk a little more about music being a distraction later on in the article!</p>
<h2>Indie Distractions</h2>
<p>You&#8217;d think that without meetings, people asking questions or background chatter it&#8217;d be easy to get more work done. Think of all that time spend in the &#8220;zone&#8221;!</p>
<p>This is partially true &#8211; I&#8217;ve certainly found that I&#8217;m more productive in general working in our spare bedroom on my own than I was in a large office, but indie life comes with its own distractions.</p>
<h3>Making Money</h3>
<p>Like many indie developers, my project is self-funded (but <a title="8-Bit Funding - Razorblade Games" href="http://8bitfunding.com/project_details.php?p_id=204" target="_blank">YOU CAN HELP</a> &#8211; shameless plug!). I say self-funded, what I really mean is wife-funded because the vast majority of the money that&#8217;s paying the household bills comes from her part time work and contracting. I&#8217;ve earned a little bit of money here and there, but she&#8217;s definitely the bread winner at the moment. As we have two pre-school children, this means that whilst she&#8217;s out earning money, I&#8217;m at home looking after the children otherwise that money would be going straight into the coffers of the local nursery.</p>
<p>This week is an extreme example. I had a few hours to work on Monday morning, and today (Thursday), I&#8217;ve found enough time to write this article. Next week I should be able to work a normal 40 hour week, but it varies wildly and it&#8217;s something I&#8217;ve had to get used to. It&#8217;s a problem that will mostly solve itself when both children are old enough to go to school, but for now it&#8217;s a constant frustration.</p>
<h3>Lots Of Different Tasks To Choose From</h3>
<p>When you&#8217;re making a game on your own, you do everything. However, with freedom comes a lack of structure, which means that it&#8217;s easy to put a tricky task off (such as building levels) and do something else (such as optimising shaders) instead. It&#8217;s all progress that adds up to the final game, but really the levels need building before shaving cycles off the lighting code.</p>
<p>I guess the solution is to hire a producer, though I can&#8217;t afford one so instead I&#8217;ll just have to become more disciplined with my scheduling.</p>
<h3>Music</h3>
<p>Earlier, I said music is a good way to avoid the distraction of background chatter in an office. When I started as an indie developer, I continued this habit, but then realised that without any conscious effort, I sometimes wasn&#8217;t putting my headphones on when at my desk. Eventually, I realised that the music itself was a distraction from work, though a lesser one than background chatter. Now I rarely listen to music when working, only occasionally when the children are being particularly noisy downstairs or a neighbour is trimming their hedge or something like that. So little music that I cancelled my Spotify subscription (saving a valuable £60 a year) as the ten free hours a month is more than enough for me.</p>
<h2>Conclusion</h2>
<p>I certainly don&#8217;t have all the answers on avoiding distractions, and I still have to force myself to concentrate on the task in hand, but I hope there are a few useful titbits in this article for people. I&#8217;ve certainly made a more conscious effort to think about this in the last year or so. Thanks for reading &#8211; even if doing so has distracted you from something else!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.altdevblogaday.com/2011/09/22/im-easily-distracted/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Shader Code for Physically Based Lighting</title>
		<link>http://www.altdevblogaday.com/2011/08/23/shader-code-for-physically-based-lighting/</link>
		<comments>http://www.altdevblogaday.com/2011/08/23/shader-code-for-physically-based-lighting/#comments</comments>
		<pubDate>Tue, 23 Aug 2011 12:30:48 +0000</pubDate>
		<dc:creator>Keith Judge</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://altdevblogaday.com/?p=14861</guid>
		<description><![CDATA[<p>SIGGRAPH 2011 took place a couple of weeks ago. Whilst I wasn&#8217;t fortunate enough (or rather, could not afford) to attend, I&#8217;ve been reading up on the games related presentations which have been helpfully archived on the <a title="Advances in Real-Time Rendering in 3D Graphics and Games - SIGGRAPH 2011" href="http://advances.realtimerendering.com/s2011/index.html">Real Time Rendering</a> website. One presentation that caught my eye in particular was <em><a title="Physically-based Lighting in Call of Duty: Black Ops" href="http://advances.realtimerendering.com/s2011/Lazarov-Physically-Based-Lighting-in-Black-Ops%20(Siggraph%202011%20Advances%20in%20Real-Time%20Rendering%20Course).pptx">Physically-based Lighting in Call Of Duty: Black Ops</a></em>. This is a subject I&#8217;ve been interested in for a while after reading Naty Hoffman&#8217;s SIGGRAPH 2010 Course: <em><a title="SIGGRAPH 2010 Course: Physically-Based Shading Models in Film and Game Production" href="http://renderwonk.com/publications/s2010-shading-course/">Physically-Based Shading Models in Film and Game Production</a></em>.</p>
<p><a href="http://www.altdevblogaday.com/2011/08/23/shader-code-for-physically-based-lighting/" class="more-link">Read more on Shader Code for Physically Based Lighting&#8230;</a></p>
]]></description>
			<content:encoded><![CDATA[<p>SIGGRAPH 2011 took place a couple of weeks ago. Whilst I wasn&#8217;t fortunate enough (or rather, could not afford) to attend, I&#8217;ve been reading up on the games related presentations which have been helpfully archived on the <a title="Advances in Real-Time Rendering in 3D Graphics and Games - SIGGRAPH 2011" href="http://advances.realtimerendering.com/s2011/index.html">Real Time Rendering</a> website. One presentation that caught my eye in particular was <em><a title="Physically-based Lighting in Call of Duty: Black Ops" href="http://advances.realtimerendering.com/s2011/Lazarov-Physically-Based-Lighting-in-Black-Ops%20(Siggraph%202011%20Advances%20in%20Real-Time%20Rendering%20Course).pptx">Physically-based Lighting in Call Of Duty: Black Ops</a></em>. This is a subject I&#8217;ve been interested in for a while after reading Naty Hoffman&#8217;s SIGGRAPH 2010 Course: <em><a title="SIGGRAPH 2010 Course: Physically-Based Shading Models in Film and Game Production" href="http://renderwonk.com/publications/s2010-shading-course/">Physically-Based Shading Models in Film and Game Production</a></em>.</p>
<p>The reason I&#8217;m writing this post is that none of the above presentations show any shader code. I hope I&#8217;m not alone in saying that I can understand an algorithm quicker with a small piece of shader code than a mathematical formula &#8211; it&#8217;s just the notation I&#8217;m used to given my day to day job.</p>
<p>I won&#8217;t go into detail with the many reasons why physically based shading is better &#8211; you can read all of that in the links above &#8211; suffice to say it looks better, simplifies the lighting interface for artists, and makes materials that hold up better in different lighting conditions without manual tweaking. It&#8217;s important to note that &#8220;physically based&#8221; does not necessarily mean &#8220;absolutely physically accurate&#8221; &#8211; after all we still need to run within a tight performance budget so all of the calculations below are still approximations, but it&#8217;s a big step forward compared to the old ad-hoc hodge podge of lighting that we&#8217;re used to.</p>
<p>Another benefit of a physically based model is that you can scale the calculations for a performance/quality trade-off as I will show further below.</p>
<h2>Disclaimer</h2>
<p>A prerequisite for all of this is that lighting takes place in linear space and it&#8217;s highly recommended that it is done in HDR.</p>
<p>It is infeasible for me to post an entire shader here as the interface for such code depends entirely on the engine in question &#8211; instead I&#8217;ll just post snippets showing the actual lighting calculations, and assume that the input have already been calculated using a method of your own choosing &#8211; perhaps interpolated from the vertex shader, fetched and decoded from a g-buffer, or some other method.</p>
<p>I&#8217;m not going to take into account any texture colour (albedo) &#8211; you might be doing that in the same shader as your lighting, or later on in a composition pass &#8211; but at whatever stage you do this, it&#8217;s still the classical &#8220;colour = diffuse * albedo + specular;&#8221;.</p>
<p>Also, I&#8217;m not taking into account any light attenuation. It&#8217;s up to you and your game how you do this.</p>
<p>The calculations below refer to a single light. For multiple lights you will need to repeat the calculations for each light and accumulate the results.</p>
<p>Lastly, please let me know if you spot any errors below! Any errors here are my own, not of the presentations linked above!</p>
<h2>Diffuse</h2>
<p>I&#8217;ll start with the easy bit, as the diffuse part of physically based lighting is the same Lambert model we&#8217;re all used to.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="glsl" style="font-family:monospace;">float3 diffuse <span style="color: #000066;">=</span> saturate<span style="color: #000066;">&#40;</span> <span style="color: #993333; font-weight: bold;">dot</span><span style="color: #000066;">&#40;</span> normal<span style="color: #000066;">,</span> light_direction <span style="color: #000066;">&#41;</span> <span style="color: #000066;">&#41;</span> <span style="color: #000066;">*</span> light_colour<span style="color: #000066;">;</span></pre></td></tr></table></div>

<p>From now on, I&#8217;ll refer to the saturated dot product of two vectors in the form &#8220;n_dot_l&#8221; to simplify the code, so the diffuse simply becomes</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="glsl" style="font-family:monospace;">float3 diffuse <span style="color: #000066;">=</span> n_dot_l <span style="color: #000066;">*</span> light_colour<span style="color: #000066;">;</span></pre></td></tr></table></div>

<p>Easy!</p>
<h2>Specular</h2>
<p>Now things start to change from what we&#8217;re used to. We&#8217;re going to use a microfacet BRDF. If you want an explanation of the equation, the presentations linked above do a far better job than I can. In shader code form, we can translate the specular BRDF into the following form</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="glsl" style="font-family:monospace;">float3 specular <span style="color: #000066;">=</span> <span style="color: #000066;">&#40;</span>PI <span style="color: #000066;">/</span> 4<span style="color: #000066;">.</span>0f<span style="color: #000066;">&#41;</span> <span style="color: #000066;">*</span> specular_term <span style="color: #000066;">*</span> cosine_term <span style="color: #000066;">*</span> fresnel_term <span style="color: #000066;">*</span> visibility_term <span style="color: #000066;">*</span> light_colour<span style="color: #000066;">;</span></pre></td></tr></table></div>

<p>I&#8217;ll go through each of the terms in turn and describe them.</p>
<h3>Specular Term</h3>
<p>A good choice for a physically based specular term in a game is Normalised Blinn-Phong. You can read the presentations linked above for the reasons and some images showing that it is a good choice. The &#8220;Normalised&#8221; part is particularly important, as it means that the specular highlight is energy conserving &#8211; as the specular power increases, the highlight should get brighter, as well as smaller. Code that implements this model looks something like this</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="glsl" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">float</span> normalisation_term <span style="color: #000066;">=</span> <span style="color: #000066;">&#40;</span> specular_power <span style="color: #000066;">+</span> 2<span style="color: #000066;">.</span>0f <span style="color: #000066;">&#41;</span> <span style="color: #000066;">/</span> 2<span style="color: #000066;">.</span>0f <span style="color: #000066;">*</span> PI<span style="color: #000066;">;</span>
<span style="color: #000066; font-weight: bold;">float</span> blinn_phong <span style="color: #000066;">=</span> <span style="color: #993333; font-weight: bold;">pow</span><span style="color: #000066;">&#40;</span> n_dot_h<span style="color: #000066;">,</span> specular_power <span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span>    <span style="color: #666666; font-style: italic;">// n_dot_h is the saturated dot product of the normal and half vectors</span>
<span style="color: #000066; font-weight: bold;">float</span> specular_term <span style="color: #000066;">=</span> normalisation_term <span style="color: #000066;">*</span> blinn_phong<span style="color: #000066;">;</span></pre></td></tr></table></div>

<p>Earlier, I showed a (PI / 4.0f) constant multiplied into the BRDF. We can remove this by pre-multiplying it into the normalisation term as follows</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="glsl" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">float</span> normalisation_term <span style="color: #000066;">=</span> <span style="color: #000066;">&#40;</span> specular_power <span style="color: #000066;">+</span> 2<span style="color: #000066;">.</span>0f <span style="color: #000066;">&#41;</span> <span style="color: #000066;">/</span> 8<span style="color: #000066;">.</span>0f<span style="color: #000066;">;</span></pre></td></tr></table></div>

<p>This means that the specular BRDF is simplified to</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="glsl" style="font-family:monospace;">float3 specular <span style="color: #000066;">=</span> specular_term <span style="color: #000066;">*</span> cosine_term <span style="color: #000066;">*</span> fresnel_term <span style="color: #000066;">*</span> visibility_term <span style="color: #000066;">*</span> light_colour<span style="color: #000066;">;</span></pre></td></tr></table></div>

<h3>Cosine Term</h3>
<p>The cosine term exists to prevent specular highlights leaking to the unlit side of the objects, and helpfully also saves a conditional in the shader.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="glsl" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">float</span> cosine_term <span style="color: #000066;">=</span> n_dot_l<span style="color: #000066;">;</span></pre></td></tr></table></div>

<p>This is the same n_dot_l from the diffuse calculation earlier, so it is essentially free.</p>
<h3>Fresnel Term</h3>
<p>The Fresnel term captures the phenomenon from the real world in which a specular highlight is relatively dim when the view and light vectors are both close to the surface normal, but gets brighter as the angle becomes more glancing. To show what I mean, here are two photos of a black piece of cardboard &#8211; excuse the poor quality of my phone&#8217;s camera!</p>
<div id="attachment_14905" class="wp-caption aligncenter" style="width: 310px"><a href="http://altdevblogaday.com/wp-content/uploads/2011/08/Dim-Specular-head-on.jpg"><img class="size-medium wp-image-14905" src="http://altdevblogaday.com/wp-content/uploads/2011/08/Dim-Specular-head-on-300x225.jpg" alt="" width="300" height="225" /></a><p class="wp-caption-text">When looked at head on, the specular highlight is quite dim</p></div>
<div id="attachment_14906" class="wp-caption aligncenter" style="width: 310px"><a href="http://altdevblogaday.com/wp-content/uploads/2011/08/Bright-specular-glancing.jpg"><img class="size-medium wp-image-14906" src="http://altdevblogaday.com/wp-content/uploads/2011/08/Bright-specular-glancing-300x225.jpg" alt="" width="300" height="225" /></a><p class="wp-caption-text">At a glancing angle, the specular highlight is much brighter</p></div>
<p>To do this in shader code, we shall use Schlick&#8217;s approximation of the Fresnel term, modified for microfacet rather than mirror reflection.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="glsl" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">float</span> base <span style="color: #000066;">=</span> 1<span style="color: #000066;">.</span>0f <span style="color: #000066;">-</span> h_dot_l<span style="color: #000066;">;</span>    <span style="color: #666666; font-style: italic;">// Dot product of half vector and light vector. No need to saturate as it can't go above 90 degrees</span>
<span style="color: #000066; font-weight: bold;">float</span> exponential <span style="color: #000066;">=</span> <span style="color: #993333; font-weight: bold;">pow</span><span style="color: #000066;">&#40;</span> base<span style="color: #000066;">,</span> 5<span style="color: #000066;">.</span>0f <span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span>
<span style="color: #000066; font-weight: bold;">float</span> fresnel_term <span style="color: #000066;">=</span> specular_colour <span style="color: #000066;">+</span> <span style="color: #000066;">&#40;</span> 1<span style="color: #000066;">.</span>0f <span style="color: #000066;">-</span> specular_colour <span style="color: #000066;">&#41;</span> <span style="color: #000066;">*</span> exponential<span style="color: #000066;">;</span></pre></td></tr></table></div>

<p>The specular colour is what you see in the situation in the first image, and this will interpolate towards white as the angle becomes more glancing. The surprising thing is that the colour bears very little relation to the diffuse albedo. For most non-metallic materials in the real world, the specular colour will be quite low, around 0.04, for metals this increases to 0.5 or more. You can use a monochrome or RGB specular colour (and by extension a monochrome or RGB Fresnel term), but for most materials, monochrome is fine as the specular power has a much greater impact on the visuals than the specular colour.</p>
<h3>Visibility Term</h3>
<p>This term describes whether the microfacets are being shadowed by the microscopic structure of the surface. It changes the appearance of the specular in a subtle but important way. Mathematically, it is the G (geometry) term from the specular BRDF equation divided by the foreshortening term (n_dot_l * n_dot_v). In the following shader code, I am using the Smith shadowing function, based on the formula in the Call of Duty presentation.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="glsl" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">float</span> alpha <span style="color: #000066;">=</span> 1<span style="color: #000066;">.</span>0f <span style="color: #000066;">/</span> <span style="color: #000066;">&#40;</span> <span style="color: #993333; font-weight: bold;">sqrt</span><span style="color: #000066;">&#40;</span> PI_OVER_FOUR <span style="color: #000066;">*</span> specular_power <span style="color: #000066;">+</span> PI_OVER_TWO <span style="color: #000066;">&#41;</span> <span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span>
<span style="color: #000066; font-weight: bold;">float</span> visibility_term <span style="color: #000066;">=</span> <span style="color: #000066;">&#40;</span> n_dot_l <span style="color: #000066;">*</span> <span style="color: #000066;">&#40;</span> 1<span style="color: #000066;">.</span>0f <span style="color: #000066;">-</span> alpha <span style="color: #000066;">&#41;</span> <span style="color: #000066;">+</span> alpha <span style="color: #000066;">&#41;</span> <span style="color: #000066;">*</span> <span style="color: #000066;">&#40;</span> n_dot_v <span style="color: #000066;">*</span> <span style="color: #000066;">&#40;</span> 1<span style="color: #000066;">.</span>0f <span style="color: #000066;">-</span> alpha <span style="color: #000066;">&#41;</span> <span style="color: #000066;">+</span> alpha <span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span> <span style="color: #666666; font-style: italic;">// Both dot products should be saturated</span>
visibility_term <span style="color: #000066;">=</span> 1<span style="color: #000066;">.</span>0f <span style="color: #000066;">/</span> visibility_term<span style="color: #000066;">;</span></pre></td></tr></table></div>

<p>Now we have all the terms we need, we multiply them all together to get our final specular lighting value.</p>
<h2>Performance/Quality Trade-off</h2>
<p>Put together this shader code is clearly more expensive than the old simple Phong or Blinn-Phong specular, but we don&#8217;t have to do it all on slower hardware. The first thing we can do to make this quicker is to make the visibility term equal to 1. As the visibility term is G (from the BRDF equation) divided by ( n_dot_l * n_dot_v ), we simply make G the same as the denominator, making the entire visibility term equal to 1. This will only be a small drop in quality &#8211; comparison images are available in the Call of Duty presentation.</p>
<p>If this is still too slow for the target hardware, the next casualty is the Fresnel term, which also can be changed to a constant 1. This will mean that the specular highlights will be the same brightness from any angle, but we still get the major benefits of normalised Blinn-Phong specular. Therefore, we can have three variants of our physically based specular calculation, depending on the performance of the hardware.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="glsl" style="font-family:monospace;">float3 slow_hardware_specular <span style="color: #000066;">=</span> specular_term <span style="color: #000066;">*</span> cosine_term <span style="color: #000066;">*</span> light_colour<span style="color: #000066;">;</span>
float3 mid_hardware_specular <span style="color: #000066;">=</span> specular_term <span style="color: #000066;">*</span> cosine_term <span style="color: #000066;">*</span> fresnel_term <span style="color: #000066;">*</span> light_colour<span style="color: #000066;">;</span>
float3 fast_hardware_specular <span style="color: #000066;">=</span> specular_term <span style="color: #000066;">*</span> cosine_term <span style="color: #000066;">*</span> fresnel_term <span style="color: #000066;">*</span> visibility_term <span style="color: #000066;">*</span> light_colour<span style="color: #000066;">;</span></pre></td></tr></table></div>

<h2>Conclusion</h2>
<p>I hope that anyone considering physically based lighting may find this article useful. Any comments and/or corrections are welcome below!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.altdevblogaday.com/2011/08/23/shader-code-for-physically-based-lighting/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Stencil Buffer Optimisation for Deferred Lights</title>
		<link>http://www.altdevblogaday.com/2011/08/08/stencil-buffer-optimisation-for-deferred-lights/</link>
		<comments>http://www.altdevblogaday.com/2011/08/08/stencil-buffer-optimisation-for-deferred-lights/#comments</comments>
		<pubDate>Mon, 08 Aug 2011 12:59:57 +0000</pubDate>
		<dc:creator>Keith Judge</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://altdevblogaday.com/?p=13897</guid>
		<description><![CDATA[<p>This is my ninth post for AltDevBlogADay and it has occurred to me that I haven&#8217;t actually written a single article about a graphics technique, so here&#8217;s my first. I&#8217;m going to describe a common technique which uses the stencil buffer for accelerating the rendering of deferred lights. It is important that deferred lights are rendered as quickly as possible as you may be rendering hundreds of them per frame. I haven&#8217;t found a good tutorial for the technique online, so here&#8217;s my attempt at writing one. First of all, here&#8217;s a screenshot from a sample scene lit by a single deferred spotlight. <a href="http://altdevblogaday.com/wp-content/uploads/2011/08/Before.png"><img class="aligncenter size-large wp-image-13903" src="http://altdevblogaday.com/wp-content/uploads/2011/08/Before-1024x576.png" alt="" width="632" height="355" /></a> Now that looks fine, but let&#8217;s see what the performance is like by capturing a frame in <a title="Intel Graphics Performance Analyser" href="http://software.intel.com/en-us/articles/intel-gpa/">Intel&#8217;s Graphics Performance Analyser (GPA)</a>.</p>
<p><a href="http://www.altdevblogaday.com/2011/08/08/stencil-buffer-optimisation-for-deferred-lights/" class="more-link">Read more on Stencil Buffer Optimisation for Deferred Lights&#8230;</a></p>
]]></description>
			<content:encoded><![CDATA[<p>This is my ninth post for AltDevBlogADay and it has occurred to me that I haven&#8217;t actually written a single article about a graphics technique, so here&#8217;s my first. I&#8217;m going to describe a common technique which uses the stencil buffer for accelerating the rendering of deferred lights. It is important that deferred lights are rendered as quickly as possible as you may be rendering hundreds of them per frame. I haven&#8217;t found a good tutorial for the technique online, so here&#8217;s my attempt at writing one. First of all, here&#8217;s a screenshot from a sample scene lit by a single deferred spotlight. <a href="http://altdevblogaday.com/wp-content/uploads/2011/08/Before.png"><img class="aligncenter size-large wp-image-13903" src="http://altdevblogaday.com/wp-content/uploads/2011/08/Before-1024x576.png" alt="" width="632" height="355" /></a> Now that looks fine, but let&#8217;s see what the performance is like by capturing a frame in <a title="Intel Graphics Performance Analyser" href="http://software.intel.com/en-us/articles/intel-gpa/">Intel&#8217;s Graphics Performance Analyser (GPA)</a>.</p>
<div id="attachment_13904" class="wp-caption aligncenter" style="width: 190px"><a href="http://altdevblogaday.com/wp-content/uploads/2011/08/BeforeTime.png"><img class="size-full wp-image-13904" src="http://altdevblogaday.com/wp-content/uploads/2011/08/BeforeTime.png" alt="" width="180" height="323" /></a><p class="wp-caption-text">Rendering the light takes 297.2 microseconds</p></div>
<p>The draw call for the light takes 0.3 milliseconds to render, which sounds fast, but we can do better. The darker yellow area of the timing bar represents pixel shader time and this is the majority of time taken for this draw call. Therefore we can make this light render faster if we can reduce the number of pixels drawn. The next screenshot shows a wireframe of the light&#8217;s geometry overlaid on the final render. For every pixel within the wireframe (strictly the back faces only), we run the lighting calculation. The pixel shader does a lot, it reconstructs a view space position from the depth stored in the g-buffer, reconstructs the view space normal from two channels (using <a title="Lambert azimuthal equal-area projection" href="http://en.wikipedia.org/wiki/Lambert_azimuthal_equal-area_projection">Lambert azimuthal equal-area projection</a>if you&#8217;re interested) and then calculates diffuse and specular light colour, taking into account distance and spotlight attenuation. As you can see, there are a lot of pixels within the wireframe that are black in the final image. This is because these areas of the light&#8217;s geometry don&#8217;t intersect the world geometry so we&#8217;re wasting work. If the wireframe is hard to see below, click the image to see it full size.</p>
<div id="attachment_13905" class="wp-caption aligncenter" style="width: 642px"><a href="http://altdevblogaday.com/wp-content/uploads/2011/08/LightWithWireframe.png"><img class="size-large wp-image-13905" src="http://altdevblogaday.com/wp-content/uploads/2011/08/LightWithWireframe-1024x576.png" alt="" width="632" height="355" /></a><p class="wp-caption-text">We render more pixels for the light than are actually affected by it.</p></div>
<p>If we can somehow make the GPU only render the pixels that are affected by the light, then we can make this quite a bit quicker. Luckily, there&#8217;s an old technique for calculating the screen space intersection between different volumes &#8211; namely &#8220;stencil shadows&#8221;. What we do is we split the render into two stages. In the first stage, we fill the stencil buffer for areas where the cone intersects the world geometry, and then for the second stage we render the light testing against the stencil buffer. To do this efficiently, we make use of double sided stencil and the zfail stencil technique (also known as Carmack&#8217;s Reverse). The eagle eyed amongst you may have had a warning flag go up in your head regarding patents, though I believe that <a title="Method for rendering shadows using a shadow volume and a stencil buffer " href="http://worldwide.espacenet.com/publicationDetails/biblio?CC=US&amp;NR=6384822&amp;KC=&amp;FT=E&amp;locale=en_EP">the Creative patent</a> only refers to stencil shadow rendering, not use of the stencil buffer for optimising light rendering. Perhaps someone with more legal experience can confirm or refute my thoughts in the comments. We create a depth stencil state with the following parameters. I&#8217;m using DirectX 11 here, but this should translate directly into more or less any modern graphics API.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
</pre></td><td class="code"><pre class="c" style="font-family:monospace;">D3D11_DEPTH_STENCIL_DESC depthstencil_desc<span style="color: #339933;">;</span>
depthstencil_desc.<span style="color: #202020;">DepthEnable</span> <span style="color: #339933;">=</span> TRUE<span style="color: #339933;">;</span>
depthstencil_desc.<span style="color: #202020;">DepthWriteMask</span> <span style="color: #339933;">=</span> D3D11_DEPTH_WRITE_MASK_ZERO<span style="color: #339933;">;</span>
depthstencil_desc.<span style="color: #202020;">DepthFunc</span> <span style="color: #339933;">=</span> D3D11_COMPARISON_LESS<span style="color: #339933;">;</span>
depthstencil_desc.<span style="color: #202020;">StencilEnable</span> <span style="color: #339933;">=</span> TRUE<span style="color: #339933;">;</span>
depthstencil_desc.<span style="color: #202020;">StencilReadMask</span> <span style="color: #339933;">=</span> D3D11_DEFAULT_STENCIL_READ_MASK<span style="color: #339933;">;</span>
depthstencil_desc.<span style="color: #202020;">StencilWriteMask</span> <span style="color: #339933;">=</span> D3D11_DEFAULT_STENCIL_WRITE_MASK<span style="color: #339933;">;</span>
depthstencil_desc.<span style="color: #202020;">FrontFace</span>.<span style="color: #202020;">StencilFunc</span> <span style="color: #339933;">=</span> D3D11_COMPARISON_ALWAYS<span style="color: #339933;">;</span>
depthstencil_desc.<span style="color: #202020;">FrontFace</span>.<span style="color: #202020;">StencilDepthFailOp</span> <span style="color: #339933;">=</span> D3D11_STENCIL_OP_INVERT<span style="color: #339933;">;</span>
depthstencil_desc.<span style="color: #202020;">FrontFace</span>.<span style="color: #202020;">StencilPassOp</span> <span style="color: #339933;">=</span> D3D11_STENCIL_OP_KEEP<span style="color: #339933;">;</span>
depthstencil_desc.<span style="color: #202020;">FrontFace</span>.<span style="color: #202020;">StencilFailOp</span> <span style="color: #339933;">=</span> D3D11_STENCIL_OP_KEEP<span style="color: #339933;">;</span>
depthstencil_desc.<span style="color: #202020;">BackFace</span>.<span style="color: #202020;">StencilFunc</span> <span style="color: #339933;">=</span> D3D11_COMPARISON_ALWAYS<span style="color: #339933;">;</span>
depthstencil_desc.<span style="color: #202020;">BackFace</span>.<span style="color: #202020;">StencilDepthFailOp</span> <span style="color: #339933;">=</span> D3D11_STENCIL_OP_INVERT<span style="color: #339933;">;</span>
depthstencil_desc.<span style="color: #202020;">BackFace</span>.<span style="color: #202020;">StencilPassOp</span> <span style="color: #339933;">=</span> D3D11_STENCIL_OP_KEEP<span style="color: #339933;">;</span>
depthstencil_desc.<span style="color: #202020;">BackFace</span>.<span style="color: #202020;">StencilFailOp</span> <span style="color: #339933;">=</span> D3D11_STENCIL_OP_KEEP<span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>The important bits we&#8217;re doing here are leaving depth test on as normal, setting depth writes off, enabling stencil and setting the stencil depthfail operation for both front and back faces to D3D11_STENCIL_OP_INVERT. If your graphics API does not support an INVERT operator, you can use INCREMENT on the back faces and DECREMENT on the front faces.</p>
<p>Then we render the light geometry with back face culling off and no pixel shader. It is important that the stencil buffer is completely clear before this is done. The stencil buffer is then filled as in the next screenshot.</p>
<div id="attachment_13922" class="wp-caption aligncenter" style="width: 642px"><a href="http://altdevblogaday.com/wp-content/uploads/2011/08/StencilOverlaid.png"><img class="size-large wp-image-13922" src="http://altdevblogaday.com/wp-content/uploads/2011/08/StencilOverlaid-1024x576.png" alt="" width="632" height="355" /></a><p class="wp-caption-text">Stencil buffer contents overlaid on final render. You can see it fits the areas of the image affected by the light more closely than the cone wireframe above.</p></div>
<p>The next stage is to render the cone again with the following depth stencil state.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
</pre></td><td class="code"><pre class="c" style="font-family:monospace;">D3D11_DEPTH_STENCIL_DESC depthstencil_desc<span style="color: #339933;">;</span>
depthstencil_desc.<span style="color: #202020;">DepthEnable</span> <span style="color: #339933;">=</span> FALSE<span style="color: #339933;">;</span>
depthstencil_desc.<span style="color: #202020;">DepthWriteMask</span> <span style="color: #339933;">=</span> D3D11_DEPTH_WRITE_MASK_ZERO<span style="color: #339933;">;</span>
depthstencil_desc.<span style="color: #202020;">DepthFunc</span> <span style="color: #339933;">=</span> D3D11_COMPARISON_LESS<span style="color: #339933;">;</span>
depthstencil_desc.<span style="color: #202020;">StencilEnable</span> <span style="color: #339933;">=</span> TRUE<span style="color: #339933;">;</span>
depthstencil_desc.<span style="color: #202020;">StencilReadMask</span> <span style="color: #339933;">=</span> D3D11_DEFAULT_STENCIL_READ_MASK<span style="color: #339933;">;</span>
depthstencil_desc.<span style="color: #202020;">StencilWriteMask</span> <span style="color: #339933;">=</span> D3D11_DEFAULT_STENCIL_WRITE_MASK<span style="color: #339933;">;</span>
depthstencil_desc.<span style="color: #202020;">FrontFace</span>.<span style="color: #202020;">StencilFunc</span> <span style="color: #339933;">=</span> D3D11_COMPARISON_ALWAYS<span style="color: #339933;">;</span>
depthstencil_desc.<span style="color: #202020;">FrontFace</span>.<span style="color: #202020;">StencilDepthFailOp</span> <span style="color: #339933;">=</span> D3D11_STENCIL_OP_KEEP<span style="color: #339933;">;</span>
depthstencil_desc.<span style="color: #202020;">FrontFace</span>.<span style="color: #202020;">StencilPassOp</span> <span style="color: #339933;">=</span> D3D11_STENCIL_OP_INVERT<span style="color: #339933;">;</span>
depthstencil_desc.<span style="color: #202020;">FrontFace</span>.<span style="color: #202020;">StencilFailOp</span> <span style="color: #339933;">=</span> D3D11_STENCIL_OP_KEEP<span style="color: #339933;">;</span>
depthstencil_desc.<span style="color: #202020;">BackFace</span>.<span style="color: #202020;">StencilFunc</span> <span style="color: #339933;">=</span> D3D11_COMPARISON_NOT_EQUAL<span style="color: #339933;">;</span>
depthstencil_desc.<span style="color: #202020;">BackFace</span>.<span style="color: #202020;">StencilDepthFailOp</span> <span style="color: #339933;">=</span> D3D11_STENCIL_OP_KEEP<span style="color: #339933;">;</span>
depthstencil_desc.<span style="color: #202020;">BackFace</span>.<span style="color: #202020;">StencilPassOp</span> <span style="color: #339933;">=</span> D3D11_STENCIL_OP_ZERO<span style="color: #339933;">;</span>
depthstencil_desc.<span style="color: #202020;">BackFace</span>.<span style="color: #202020;">StencilFailOp</span> <span style="color: #339933;">=</span> D3D11_STENCIL_OP_KEEP<span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>The important bits here are that depth testing is off, and the stencil test for back faces is set to D3D11_COMPARISON_NOT_EQUAL, which will mean only pixels with a non-zero stencil will be shaded. We also set the StencilPassOp for back faces to D3D_STENCIL_OP_ZERO so the stencil buffer is cleared for the next light. The FrontFace settings are ignored as we&#8217;re only going to render back faces in this stage. Then we render the light geometry with the full pixel shader and front faces culled. The visual result is exactly the same, but what of performance? Let&#8217;s see how long those two draw calls take with another GPA grab.</p>
<div id="attachment_13923" class="wp-caption aligncenter" style="width: 174px"><a href="http://altdevblogaday.com/wp-content/uploads/2011/08/AfterTime.png"><img class="size-full wp-image-13923" src="http://altdevblogaday.com/wp-content/uploads/2011/08/AfterTime.png" alt="" width="164" height="176" /></a><p class="wp-caption-text">Total draw time is now 266.3 microseconds</p></div>
<p>This is about 11% quicker for this particular light, with only minor code changes. Savings will vary depending on how the light and geometry intersect and also depends on where the camera is. It should only be marginally slower in the rare case where the light affects every single pixel on the screen.</p>
<p>I hope you find this article useful.</p>
<p>EDIT: Based on a few twitter comments I&#8217;ve further optimised this by modifying the first stage from depth testing with D3D11_COMPARISON_GREATER_EQUAL and inverting stencil on depth pass, to testing depth with D3D11_COMPARISON_LESS and inverting stencil on depth fail instead. This is equivalent, but crucially, it allows hierarchical depth/stencil optimisations that the GPU does to remain in place, further optimising the rendering which now takes less than 0.2ms. Yay!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.altdevblogaday.com/2011/08/08/stencil-buffer-optimisation-for-deferred-lights/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Develop Indie Dev Day 2011</title>
		<link>http://www.altdevblogaday.com/2011/07/26/develop-indie-dev-day-2011/</link>
		<comments>http://www.altdevblogaday.com/2011/07/26/develop-indie-dev-day-2011/#comments</comments>
		<pubDate>Tue, 26 Jul 2011 14:44:23 +0000</pubDate>
		<dc:creator>Keith Judge</dc:creator>
				<category><![CDATA[Bizdev]]></category>

		<guid isPermaLink="false">http://altdevblogaday.com/?p=12504</guid>
		<description><![CDATA[<p style="text-align: center"><a href="http://www.developconference.com/"><img class="aligncenter" src="http://www.developconference.com/g/2011/logos/indiedevlogo2011.jpg" alt="Develop Indie Day" width="600" height="161" /></a></p>
<p>Last Thursday I went to the Develop Indie Dev Day in the English seaside town of Brighton. My reasons for going were threefold. The sessions themselves sounded interesting, I&#8217;d get a chance to chat and socialise with friends my indie lifestyle is preventing me from spending time with, and lastly to try to show my game prototype to a few people.</p>
<p><a href="http://www.altdevblogaday.com/2011/07/26/develop-indie-dev-day-2011/" class="more-link">Read more on Develop Indie Dev Day 2011&#8230;</a></p>
]]></description>
			<content:encoded><![CDATA[<p style="text-align: center"><a href="http://www.developconference.com/"><img class="aligncenter" src="http://www.developconference.com/g/2011/logos/indiedevlogo2011.jpg" alt="Develop Indie Day" width="600" height="161" /></a></p>
<p>Last Thursday I went to the Develop Indie Dev Day in the English seaside town of Brighton. My reasons for going were threefold. The sessions themselves sounded interesting, I&#8217;d get a chance to chat and socialise with friends my indie lifestyle is preventing me from spending time with, and lastly to try to show my game prototype to a few people.</p>
<p>I live in Guildford, which is around 45 miles away from Brighton, but due to a quirk in the layout of the railways in the South-East of England, the train journey would be close to 80 miles. The reason for this is that railways radiate out from London in all directions, but there&#8217;s very few lines going <em>around</em> London so you&#8217;re forced to go into London on one line, and back out on another. For me, this meant I got out of bed at 6am, which is something I&#8217;ve not done for a <em>long</em> time, and I spent two hours trying not to fall asleep on the train.</p>
<h2>The Sessions</h2>
<p>The majority of the day was spent in sessions &#8211; there was a programme specifically targeted towards indie developers, mostly talking about the business side of things rather than the actual game creation process as it&#8217;s assumed we can already do that. This is in contrast to the main Develop sessions that were mostly focussed on the actual production side of games development.</p>
<p>The opening keynote from my old colleague Sean Murray of <a title="Hello Games" href="http://www.hellogames.org/" target="_blank">Hello Games</a> was particularly interesting as it cut through the glamour of indie development and talked about the difficult realities of money, the fact that failed indie studios are never reported in the press &#8211; distorting the chances of success, happiness (or the lack of it), hating your game at various points in development and so on. On his graph, I was still on the first happiness peak after forming a studio. Perhaps the session was a depressing way to begin the day, but it felt like a very honest account and there is still light at the end of the tunnel as his own survey showed that the vast majority of indie developers would rather do nothing else.</p>
<p>Other sessions of particular interest to me were covered the legalities of running a small games studio (IP law, defending against patent trolls, etc), and another on how to raise funding. The funding one in particular was an eye opener as it showed that most investors are not interested too much in the product you&#8217;re creating &#8211; they&#8217;re more interested in you and your business as a going concern and on building a long-term relationship with them, even before any money changes hands. It also showed me that if I&#8217;m to secure funding in this manner, I have a lot of skills to learn outside the field of games development.</p>
<h2>Networking</h2>
<p>Networking is a skill I clearly need to develop (pun intended?). When I&#8217;ve attended conferences like this in the past, I&#8217;ve always been there as part of a large company, so I can spend time between sessions and at the end of the day just catching up with friends and avoiding talking with the scary new people. However, when running a small business you need to promote, this clearly isn&#8217;t the best option as you need to get out there and talk to everyone. I&#8217;m afraid to say I did end up spending too much time &#8220;safely&#8221; with friends and perhaps not enough time talking with potential publishers, investors and other useful people to know.</p>
<p>Perhaps my social awkwardness caused some wasted opportunities, but I did pluck up the courage to talk to some people, especially after a beer or two had been consumed. I swapped lots of business cards and shook hands with a lot of the attendees. I also wore my company logo t-shirt all day with pride, so hopefully more people are aware of my company now &#8211; which may or may not translate into future sales. The Google Analytics for my company website shows there was a definite boost in traffic during the day &#8211; huzzah!</p>
<p>I also got good feedback on my game prototype &#8211; everyone &#8220;got&#8221; the game mechanic straight away which was good, but with a programmer art white-box level I wasn&#8217;t wowing anyone. This showed me I was deluding myself in thinking I could build this game single-handed to a high enough standard. The day made me realise the project needs input from artists &#8211; probably from some cunning level designers too. The trick is going to be achieving this whilst not running out of money. I&#8217;m not about to give up just yet!</p>
<h2>The Aftermath</h2>
<p>The next day I felt utterly drained and depressed. I was not expecting this at all. I don&#8217;t know whether it was just down to tiredness, or the reality of what&#8217;s required to build and sell my game hitting me like a ton of bricks &#8211; &#8220;It&#8217;ll take me forever to build this&#8221;, &#8220;We&#8217;ll run out of money long before it&#8217;s done&#8221;, &#8220;What&#8217;s the point? I should just go and get a job&#8221;. I&#8217;ve not felt that down for a long time. I went to sleep early that evening and didn&#8217;t get out of bed again till lunchtime on Saturday. I didn&#8217;t really feel fully awake for the whole day. This AltDevBlogADay post was due on Sunday, but I just felt like there was no point opening WordPress and starting to type. Only yesterday (Monday), did I start feeling like myself again. I looked after my two children all day and they have a happy tendency to always cheer me up.</p>
<p>Today, I&#8217;m back to working on my game again &#8211; getting a ton of email out of the way, organising a potential concept artist, working out a plan for the next engine feature. From here, I&#8217;m wondering what I was so down about, but I have to say my mood for the last few days has taken me (and my wife) by surprise.</p>
<h2>A (Humorous?) Story About Trains</h2>
<p>I posted on the social networks yesterday that I would include a story about trains in this article. I don&#8217;t like to end on a down note, so here it is.</p>
<p>On the way back from Develop, I had make sure I caught the last train home. It was a three part journey to Guildford. I walked to Brighton station on time and caught the 22:33 to Haywards Heath as planned. The next train was from there to to Clapham Junction, so I boarded the train and got myself comfortable. I might add at this point that I&#8217;d had rather a few beers before beginning this journey. After what seemed like an endless journey, which fely like the train was stopping at every tiny village on the way, we reached Clapham Junction.</p>
<p>Or so I thought&#8230;</p>
<p>I got off the train, found a member of staff and asked them what platform I needed for the Guildford train. He looked at me puzzled &#8211; &#8220;You need to go to Clapham for that mate&#8221;. WTF? I look around and a lot of very clear and well lit signs pronounce that I&#8217;m actually in East Croydon. EAST CROYDON! By this point, the train I had just disembarked had already gone off without me. I won&#8217;t repeat the string of expletives that went through my brain, and probably through my mouth too, but suffice to say, it was time to PANIC!</p>
<p>I sent a message to my wife who told me that there was a later train to Clapham at 11:58, which would get there in time for me to catch the train to Guildford. Panic over I thought and went to the correct platform to wait.</p>
<p>The allotted time arrived. The allotted time passed. No train arrived</p>
<p>Then, an announcement stating that the train was cancelled! Noooooooooooo!!! I waited longer still for an even later train, but by the time I eventually got to Clapham Junction I had missed the last train to Guildford by some margin.</p>
<p>Thankfully, my wife was an absolute star and came to pick me up in the car with the children bundled up in the back, still wearing their pyjamas. Home safely, I collapsed in bed.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.altdevblogaday.com/2011/07/26/develop-indie-dev-day-2011/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Why Do So Many PC Games Still Use DirectX 9?</title>
		<link>http://www.altdevblogaday.com/2011/07/09/why-do-so-many-pc-games-still-use-directx-9/</link>
		<comments>http://www.altdevblogaday.com/2011/07/09/why-do-so-many-pc-games-still-use-directx-9/#comments</comments>
		<pubDate>Sat, 09 Jul 2011 09:48:03 +0000</pubDate>
		<dc:creator>Keith Judge</dc:creator>
				<category><![CDATA[General Interest]]></category>

		<guid isPermaLink="false">http://altdevblogaday.com/?p=10513</guid>
		<description><![CDATA[<h2>A Brief History</h2>
<p>Microsoft released DirectX 9 to the world at the end of 2002 (source &#8211; <a title="DirectX - Wikipedia" href="http://en.wikipedia.org/wiki/Directx_9#Releases" target="_blank">Wikipedia</a>). It had many enhancements over previous versions (most notably fully programmable vertex and pixel shader pipelines), and has proved to be a very popular graphics API with PC game developers.</p>
<p><a href="http://www.altdevblogaday.com/2011/07/09/why-do-so-many-pc-games-still-use-directx-9/" class="more-link">Read more on Why Do So Many PC Games Still Use DirectX 9?&#8230;</a></p>
]]></description>
			<content:encoded><![CDATA[<h2>A Brief History</h2>
<p>Microsoft released DirectX 9 to the world at the end of 2002 (source &#8211; <a title="DirectX - Wikipedia" href="http://en.wikipedia.org/wiki/Directx_9#Releases" target="_blank">Wikipedia</a>). It had many enhancements over previous versions (most notably fully programmable vertex and pixel shader pipelines), and has proved to be a very popular graphics API with PC game developers.</p>
<p>Over the years, DirectX 9 has recieved several minor updates, culminating in DirectX 9.0c in 2004. This eventually became the graphics API for the Xbox 360 with a few enhancements specifically for that hardware, further extending its reach.</p>
<p>Since then, DirectX 10 was released with the launch of Windows Vista in 2006, and DirectX 11 with Windows 7 in 2009.</p>
<h2>PC Games Today</h2>
<p>Most PC games released today still use DirectX 9. For example; <em>Portal 2</em>, <em>Fable III</em> and <em>Crysis 2</em> were all released in 2011 and all use DirectX 9 as their graphics API (though in <em>Crysis 2</em>&#8216;s case there is a DirectX 11 patch available, offering graphical enhancements as seen in <a title="Crysis 2 DX11" href="http://www.youtube.com/watch?v=DdqZT4gv6OU" target="_blank">this pretty video</a>). The only major game I can think of that does not support DirectX 9 at all is <em>Battlefield 3</em> (source -<a title="Battlefield 3 Blog" href="http://bf3blog.com/2010/12/battlfield-3-wont-support-windows-xp/" target="_blank"> Battlefield 3 Blog</a>), though that game has not been released yet. A lot of games from the past few years do support Directx 10 or 11, but almost all of these also support DirectX 9 as well.</p>
<p>This pattern is contrary to the behaviour of game developers for previous versions of DirectX, whereby most would jump onto the new version as soon as practical. So why is this?</p>
<h2>Reasons</h2>
<p>In my opinion, there are several reasons for this. In no particular order&#8230;</p>
<ol>
<li><strong>Windows XP</strong> &#8211; DirectX 10 was the first version of the API to completely break backwards compatibility. It will only run on Windows Vista and Windows 7. This was necessary due to a fundamental change in the driver model for DirectX, but as a result of this, adoption of the new technology was slow. For a game to support only DirectX 10 in 2006 when Vista was launched would have meant losing almost all of the game&#8217;s sales as the vast majority of gamers at the time were still using Windows XP. However, this is changing fast as Valve Software&#8217;s monthly <a title="Steam Hardware Survey" href="http://store.steampowered.com/hwsurvey" target="_blank">Steam Hardware Survey</a> (probably the best publicly available source of information of this type) shows that less than 20% of gamers were still using Windows XP in June 2011 and the figure is dropping. Personally, I think now is the time for games to be written using DirectX 11.</li>
<li><strong>Xbox 360</strong> &#8211; A lot of games are released on multiple platforms. For games that are on both Xbox 360 and PC, it makes sense to use DirectX 9 on both versions to minimise the work required rather than maintain two separate graphics engines. On the other hand, it is tricky to do some things with DirectX 9 on PC that are simple on Xbox 360. For example, on Xbox 360 it is simple to read a depth buffer from a shader, but on PC with DirectX 9, you have to resort to either writing depth to a second R32 format texture (which is slow) or using driver hacks such as the INTZ texture format and then deal with different graphics cards separately. This issue is (of course), solved in DirectX 10/11.</li>
<li><strong>New driver model</strong> - Due to the change in driver model for DirectX 10, all the graphics card manufacturers had to write new drivers to support the new system. This resulted in bugs and performance issues in DirectX 10 games at the time. This is entirely understandable as any new complicated piece of technology is bound to have teething issues (and the issues have since been solved), but it feels as though DirectX 10 gained a (perhaps undeserved?) poor reputation along with Windows Vista. When Windows 7 was released, the new version of DirectX was numbered 11, rather than 10.2 which perhaps shows that Microsoft was aware of the perception and wanted to make a clean break.</li>
<li><strong>DirectX 9 was revolutionary, Direct X 10/11 are not</strong> &#8211; What I mean by this is that DirextX 9 opened up a wealth of new graphical possibilities. The ability to write long HLSL shaders meant that games suddenly had the ability to use more sophisticated lighting models, more varied materials and post-processing &#8211; all with some quite straightforward programming (and a good understanding of maths). There are many DirectX 9 games that simply would not have been visually possible beforehand (ignoring OpenGL for the purposes of this post!). For this reason, DirectX 9 is still visually good enough for modern games. DirectX 10 and 11 have added new features &#8211; geometry shaders, DirectCompute, tessellation, etc, which are all good, but not the technical leap that DirectX 9 allowed. For me, most of the new features of DirectX 10/11 are designed to make things easier for developers or enable performance increases, rather than adding entirely new graphical possibilities. I think the improved graphical features of the recent  DirectX 11 patch for <em>Crysis 2</em> are probably mostly possible in DirectX 9, though would either have been a nightmare to implement, or would have been very slow at runtime.</li>
<li><strong>Perception that a new DirectX means buying a new graphics card</strong> &#8211; This is only partially true &#8211; it was true for DirectX 10 which mandated a minimum feature set, but DirectX 11 has added a new feature to allow older graphics cards to be used. To take advantage of the latest features of DirectX 11 (e.g. hardware tessellation), you need a graphics card that implements those features, but it is entirely possible to run DirectX 11 games on older hardware so long as the developer allows it using the correct feature level. For example, the developer can test for the supported feature level and replace tessellated geometry with normal mapped geometry on hardware where tessellation is not supported. I am developing my own game engine with DirectX 11 using a GeForce 8800 GTX (a DirectX 10 graphics card), and I can even support older DirectX 9 level hardware if I want to (Steam Hardware Survey says YES). The feature level system in DirectX 11 allows me to simulate older hardware and the debug layer will warn me if I try to use a feature that is not available on that level. However, I have not yet tried a real DirectX 9 graphics card with my engine, so I&#8217;m using this feature on faith for now. I&#8217;ll find out for sure when beta testing begins.</li>
</ol>
<h2>Moving Forwards</h2>
<p>Like the<em> Battlefield 3</em> team at DICE, I&#8217;m writing my game engine using DirectX 11, and have no plans to write a DirectX 9 version. I&#8217;m doing this mostly for the ease of use and flexibility of the newer API as I can literally build the game quicker without worrying about different driver hacks for different graphics cards. Also, over 80% of PC gamers can theoretically play my game (according to the Steam Hardware Survey) &#8211; certainly an even greater proportion by the time my game is released. The reasons I&#8217;ve listed above for the extended life of DirectX 9 are mostly historical and I don&#8217;t think they apply for a game engine written today. I read a quote (I forget by who I&#8217;m afraid) from one of the presentations at GDC this year from developers saying that they write their new graphical features in DirectX 11 first, then port across to the Xbox 360 and PS3 as it it easier that way and allows their PC versions to shine (rather than just be a port of the Xbox 360 version).</p>
<p>Let&#8217;s give DirectX 9 a nice retirement and let it live the rest of its time in peace and quiet &#8211; undisturbed by us pesky game developers!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.altdevblogaday.com/2011/07/09/why-do-so-many-pc-games-still-use-directx-9/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Working For Myself &#8211; First Two Months</title>
		<link>http://www.altdevblogaday.com/2011/06/24/working-for-myself-first-two-months/</link>
		<comments>http://www.altdevblogaday.com/2011/06/24/working-for-myself-first-two-months/#comments</comments>
		<pubDate>Fri, 24 Jun 2011 08:08:52 +0000</pubDate>
		<dc:creator>Keith Judge</dc:creator>
		
		<guid isPermaLink="false">http://altdevblogaday.org/?p=9109</guid>
		<description><![CDATA[<p><a href="http://altdevblogaday.com/wp-content/uploads/2011/06/indiegamedev.jpg"><img class="alignright size-full wp-image-9472" src="http://altdevblogaday.com/wp-content/uploads/2011/06/indiegamedev.jpg" alt="" width="407" height="405" /></a>Time is a funny thing. I&#8217;m writing this just after two months since I left my last paid job at <a title="Lionhead Studios" href="http://lionhead.com/" target="_blank">Lionhead Studios</a> &#8211; in some ways it feels like it was a previous life, in others like it was yesterday. I thought I&#8217;d write a post-mortem style post about my experiences so far.</p>
<p><a href="http://www.altdevblogaday.com/2011/06/24/working-for-myself-first-two-months/" class="more-link">Read more on Working For Myself &#8211; First Two Months&#8230;</a></p>
]]></description>
			<content:encoded><![CDATA[<p><a href="http://altdevblogaday.com/wp-content/uploads/2011/06/indiegamedev.jpg"><img class="alignright size-full wp-image-9472" src="http://altdevblogaday.com/wp-content/uploads/2011/06/indiegamedev.jpg" alt="" width="407" height="405" /></a>Time is a funny thing. I&#8217;m writing this just after two months since I left my last paid job at <a title="Lionhead Studios" href="http://lionhead.com/" target="_blank">Lionhead Studios</a> &#8211; in some ways it feels like it was a previous life, in others like it was yesterday. I thought I&#8217;d write a post-mortem style post about my experiences so far.</p>
<h2>What Went Right</h2>
<ul>
<li><strong>Productivity </strong>- When I began, I was excited and determined to write a game on my own. I was also somewhat apprehensive about having the mental discipline to remain productive on my own. A common reaction from people when I told them about my new studio was to ask how I was going to get any work done at home with a family. So far, I&#8217;m happy to report that those fears were unfounded &#8211; or at least so far, my enthusiasm has overridden my natural laziness and the kids have proven less of a distraction than expected. We&#8217;ll see if this extends to shipping a game, though I&#8217;m quite optimistic at present.</li>
<li><strong>Learning </strong>- Even with a decade&#8217;s experience, I would never claim to know everything (or even a substantial portion) of the knowledge required to build a game. In the last two months my learning has accelerated. Subjects such as physically based lighting, better knowledge of rigid body physics, DirectX 11 and a smattering of the Win32 API have since entered my lexicon.</li>
<li><strong>Great Free Software</strong> &#8211; I&#8217;ve been able to build my game at an accelerated rate due to some very handy open source code that is available at no cost. <a title="Bullet Physics" href="http://bulletphysics.org/wordpress/" target="_blank">Bullet Physics</a>, <a title="Open Asset Import Library" href="http://assimp.sourceforge.net/" target="_blank">AssImp</a>, <a title="Blender" href="http://www.blender.org/" target="_blank">Blender</a>, etc have all saved me significant chunks of time. When I finish my game I may be able to donate code or money (if I make any) back to the projects.</li>
<li><strong>DirectX 11</strong> &#8211; It may be seen as a gamble to limit myself to a smaller portion of PC gamers (though not that small according to the monthly Valve Hardware Survey), but supporting only Windows Vista/7 has made development of the game engine a lot simpler. This is because I haven&#8217;t had to rely on any vendor specific DirectX 9 driver hacks (INTZ, RAWZ, instancing, etc) in order to make things work, plus DirectX 11 has a much better debug layer and useful new features. Although I&#8217;m developing my engine with only the GeForce 8800 GTX I have at my disposal, I hope there will only be minor issues in making the game work with any other DirectX 10 or 11 hardware &#8211; and there&#8217;s simply a smaller set of hardware to test which helps with my limited resources. Also, the knowledge gained in using DirectX 11 will be useful if I need to do any contracting to top up the coffers. If nothing else, this technology also gives me something to differentiate my game from most other indie games.</li>
<li><strong>Finding Free Days Out At The Weekend</strong> &#8211; I&#8217;ve been keeping my weekends free so far to spend with the family rather than with my PC. My wife and I became <a title="The National Trust" href="http://www.nationaltrust.org.uk/main/" target="_blank">National Trust</a> members a few months ago, so there&#8217;s plenty of free (except for fuelling the car) days out locally that we can take the family to at the weekends. For those of you not in the UK, the National Trust is a large organisation which looks after lots of old houses, gardens, monuments, castles, palaces, etc around the country, and a large chunk of those are in or near Surrey, where I happen to live.</li>
<li><strong>#guessmycompanyname</strong> &#8211; I ran a little Twitter/Facebook game in May where I invited people to guess the name of my new company, and posted periodic clues. I offered a copy of my first game to the first person to guess correctly. The game lasted a few hours, generating lots of Twitter messages (though not much Facebook activity) before two people correctly guessed &#8220;Razorblade Games&#8221; at the same time &#8211; I&#8217;ll be giving them both the prize when the time comes. At that point I turned on the company website, Twitter and Facebook accounts for business. After an initial flurry of interest things have waned which is no surprise since I&#8217;ve not announced my game yet. I was a little disappointed none of my twitter followers who work in the press reported on the new studio, but that would have been a bonus as that wasn&#8217;t the objective &#8211; I plan to start talking to the press when the game is demo-able and I have screenshots/video to back it up.</li>
<li><strong>More Time With The Family</strong> &#8211; I&#8217;m almost always home for mealtimes, am keeping weekends free and have time to play with the children every day. As a result I feel as though I&#8217;ve grown closer to my family.</li>
<li><strong>I&#8217;m Generally Happier</strong> &#8211; I&#8217;m on the way to achieving a lifetime ambition and this is a great boost for me. I feel less stressed about work, there&#8217;s no office politics or bureaucracy to deal with, I can work more or less when I feel like it (exceptions below!), My wife often comments that I&#8217;m happier now than I&#8217;ve been for years.</li>
</ul>
<h2>What Went Wrong</h2>
<ul>
<li><strong>Very Little Social Life</strong> &#8211; My social life has declined massively since working for myself &#8211; mostly down to the lack of money, but also because there&#8217;s no pub to nip into on the way home from work. I&#8217;ve been to a couple of parties since and had the occasional beer or five with friends, though I can quite easily go an entire week without leaving the house or having a face-to-face conversation with an adult other than my wife. If we can solve the money issue (detailed below), this problem may fix itself.</li>
<li><strong>No Work Colleagues</strong> &#8211; Related to social life, I miss working with my friends and having people to chat about what I&#8217;m actually working on. My wife will listen to me, but she doesn&#8217;t understand the details of normalised specular, or rigid body interpenetration so it&#8217;s harder to have any work conversations. Twitter and MSN are proving to be a substitute of sorts, but it&#8217;s not really a replacement for a good old chinwag. In future I may collaborate with others, but I want to at least get one game done on my own. I&#8217;m also going to the Indie Day of the Develop Conference in Brighton in July (and hopefully to other, future meet-ups), so that will be a great opportunity to chat with other game developers in person.</li>
<li><strong>Sharing the Office With My Wife</strong> &#8211; My wife also does some work from home and we started out time-sharing the office, but that turned out not to be practical. Currently, my wife&#8217;s PC is on the dining table downstairs so we can both work in parallel in the evenings, but this isn&#8217;t a permanent solution and we&#8217;ll have to fork out for a desk for her soon and move some stuff around to fit it in. She&#8217;s a little annoyed I&#8217;ve essentially booted her out of what used to be her office, but she understands the necessity.</li>
<li><strong>Looking After The Kids Is Time Not Making My Game</strong> &#8211; When my wife does her contracting work away from home, I have to look after the children as paid childcare is out of our budget and neither my mum or her parents live nearby. This has occasionally meant 2-3 frustrating days at a time where I&#8217;m unable to work (the kids&#8217; afternoon nap times excluded). There&#8217;s not much we can do about this though, my wife&#8217;s contracting work brings in essential funds, so we&#8217;re just going to have to grin and bear it for the time being.</li>
<li><strong>Game Hint On The Website Is Too Subtle</strong> &#8211; On <a title="Razorblade Games" href="http://razorbladegames.com/" target="_blank">the company website</a> there&#8217;s a little square to the left of the logo that is slightly darker than the background. Click on it, and the website&#8217;s colour scheme will reverse and the text will change slightly. Now there&#8217;s another square slightly darker than the background to the right of the logo, click this and things will go back to how they were. This is actually a hint about the game I&#8217;m building and I was hoping this would generate some chatter and interest, but this seems to have not been the case. I don&#8217;t think anyone has found it without being told there&#8217;s something there to find (and those people found it by looking at the source code, not finding it naturally). However, this sort of ARG style thing is something I&#8217;m interested in playing with further when it comes to promoting my game, so keep your eyes peeled.</li>
</ul>
<h2>What Isn&#8217;t Decided Yet</h2>
<ul>
<li><strong>Money</strong> &#8211; I&#8217;m working entirely self funded (more accurately wife funded), but money is quite tight. As I said above, my social life has suffered as a result of this, but we&#8217;ve been getting by. Unfortunately, there&#8217;s no guarantee my game will make any money, even if it turns out as good as it seems in my head. I&#8217;m probably going to have to do some contracting work before too long to top up the coffers (open to offers, hint hint) or try to obtain some outside financing (again, I&#8217;m open to any offers!).</li>
<li><strong>The Game</strong> &#8211; My game isn&#8217;t finished yet. Although it seems brilliant in my head, I worry about whether it will be any good to the extent that it sometimes keeps me up at night. Perhaps this is the life of a tortured creator, but it hasn&#8217;t put me off &#8211; far from it!</li>
</ul>
<p>Overall I&#8217;d say the good stuff vastly outweighs the bad, and I&#8217;m raring to go for another two months and beyond.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.altdevblogaday.com/2011/06/24/working-for-myself-first-two-months/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>It&#8217;s All Physics</title>
		<link>http://www.altdevblogaday.com/2011/06/09/its-all-physics/</link>
		<comments>http://www.altdevblogaday.com/2011/06/09/its-all-physics/#comments</comments>
		<pubDate>Thu, 09 Jun 2011 13:19:56 +0000</pubDate>
		<dc:creator>Keith Judge</dc:creator>
		
		<guid isPermaLink="false">http://altdevblogaday.org/?p=7596</guid>
		<description><![CDATA[<p><a href="http://altdevblogaday.com/wp-content/uploads/2011/06/albert-einstein.jpg"><img class="alignleft size-full wp-image-7805" src="http://altdevblogaday.com/wp-content/uploads/2011/06/albert-einstein.jpg" alt="Albert Einstein" width="400" height="521" /></a>I&#8217;ve always been fascinated by physics. As a young lad I&#8217;d watch science documentaries rather than the soap operas and comedies everyone else in school seemed to watch. I actually paid attention in science classes (which probably explains why I had no girlfriend at the time, but I digress). I whooped with joy watching the Half Life 2 videos from E3 in 2003 when it was made clear that physics was a big part of the gameplay. If rendering (which is of course a branch of physics) hadn&#8217;t been even more intriguing to me, I might well have become a physics programmer.</p>
<p><a href="http://www.altdevblogaday.com/2011/06/09/its-all-physics/" class="more-link">Read more on It&#8217;s All Physics&#8230;</a></p>
]]></description>
			<content:encoded><![CDATA[<p><a href="http://altdevblogaday.com/wp-content/uploads/2011/06/albert-einstein.jpg"><img class="alignleft size-full wp-image-7805" src="http://altdevblogaday.com/wp-content/uploads/2011/06/albert-einstein.jpg" alt="Albert Einstein" width="400" height="521" /></a>I&#8217;ve always been fascinated by physics. As a young lad I&#8217;d watch science documentaries rather than the soap operas and comedies everyone else in school seemed to watch. I actually paid attention in science classes (which probably explains why I had no girlfriend at the time, but I digress). I whooped with joy watching the Half Life 2 videos from E3 in 2003 when it was made clear that physics was a big part of the gameplay. If rendering (which is of course a branch of physics) hadn&#8217;t been even more intriguing to me, I might well have become a physics programmer.</p>
<p>Fast forward a decade or two &#8211; I&#8217;m writing my own game and naturally physics is a key feature. I know just enough about rigid body physics to know that writing my own physics engine is far beyond my current skill-set so I&#8217;m using a 3rd party physics engine called <a title="Bullet Physics" href="http://bulletphysics.org/wordpress/" target="_blank">Bullet Physics</a>. I won&#8217;t go into the reasons for choosing this particular engine (perhaps in another post), but here I will describe the basic concepts that rigid body physics engines all share. I think it vital to understand the basics of how a physics engine operates, even if you&#8217;re using an existing codebase as this understanding will help you use it most effectively.</p>
<p><strong>Rigid Body Physics</strong></p>
<p>What do I mean by &#8220;rigid body physics&#8221;? Quite simply, it is the simulation of forces such as gravity acting upon solid objects. For example, a crate falling off a ledge, a weight pushing down the end of a see-saw, a ball bouncing on the ground, a pool cue hitting a ball. This is contrary to &#8220;soft body physics&#8221; which simulates objects such as cloth or jelly.</p>
<p>In the real world, physics happens continuously (<a title="Planck Time" href="http://en.wikipedia.org/wiki/Planck_time" target="_blank">or does it</a>?), but in a game we run the simulation in discrete timesteps &#8211; generally 30 or 60 times a second, often even more often than that (perhaps 120 or 180 times a second). The more frequently the simulation runs, the more accurate it tends to be, but of course there is a performance tradeoff for this. That decision is made for each individual game depending on its requirements.</p>
<p><strong>Step By Step</strong></p>
<p>So what does the physics engine do? Quite simply, for each simulation step, it executes the following operations:</p>
<ol>
<li>Apply forces (such as gravity) to the objects and move them according to their momentum.</li>
<li>Detect any collisions between objects.</li>
<li>Resolve the collisions.</li>
</ol>
<p>Of course, there&#8217;s a lot of detail to each of these steps (for example, step two happens either discretely &#8211; checking only each object&#8217;s new position, or continuously by sweeping the object from its previous position to its new position), but I shan&#8217;t go into that level of detail here as it would make for an enormously long post (probably a book) and there are other people better far qualified to write that.</p>
<p><strong>Objects</strong></p>
<p>Whilst debate rages on about whether object oriented programming is a good thing or not, in physics everything is an object. There are three classes of physics object.</p>
<ol>
<li>Static &#8211; these objects do not ever move, no matter how much force is applied to them. You would use static objects for your world geometry &#8211; ground, walls, etc. Whilst technically unrealistic (even a planet can be moved with a big enough force), treating such things as completely static helps to keep the cost of the simulation down.</li>
<li>Dynamic &#8211; these objects are subject to forces and move freely within the static world. They are acted upon by gravity, being hit by other objects, and so on. Examples of dynamic objects include crates, balls, rocks and grenades.</li>
<li>Kinematic &#8211; these objects are explicitly moved by the game though user input, scripting or animation and the dynamic objects respond to collisions with these. You would use kinematic objects to represent the player, melee weapons, etc.</li>
</ol>
<p>Usually, the physics engine will use what are called &#8220;Simulation Islands&#8221; in order to increase performance. What this means is that rather then simulating every dynamic object in each step, only those that are in motion, or have been touched by a kinematic object are simulated. When these dynamic objects are at rest again, they stop being simulated.</p>
<p><strong>Primitives</strong></p>
<p>To get the best performance and accuracy from a physics simulation it is rarely a good idea to use your render meshes as physics primitives. The physics library will provide a set of basic geometric primitives to use instead. You might model a crate using dozens of triangles, but you are best off using a single cube for the physics simulation in order to keep it fast. For accuracy, you would use a sphere to correctly model a pool ball, rather than the mesh of triangles that is being rendered as this mesh won&#8217;t roll correctly. A first person player is best simulated as a cylinder or capsule (a cylinder with rounded ends) rather than an animated character. For a car or a boat, you would use a simple convex hull. Even for world geometry you are probably best off using a simplified version of the render mesh, not only for performance reasons, but also because overly complex physics geometry can cause objects to get stuck or fall out of the world.</p>
<p>In conclusion, I hope this primer is valuable to someone using a physics engine for the first time.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.altdevblogaday.com/2011/06/09/its-all-physics/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Testing, Testing, Testing</title>
		<link>http://www.altdevblogaday.com/2011/05/25/testing-testing-testing/</link>
		<comments>http://www.altdevblogaday.com/2011/05/25/testing-testing-testing/#comments</comments>
		<pubDate>Wed, 25 May 2011 15:16:12 +0000</pubDate>
		<dc:creator>Keith Judge</dc:creator>
		
		<guid isPermaLink="false">http://altdevblogaday.org/?p=6423</guid>
		<description><![CDATA[<div class="wp-caption alignleft" style="width: 610px"><img src="http://upload.wikimedia.org/wikipedia/en/e/ef/Portal_screenshot.jpg" alt="Portal Screenshot" width="600" height="375" /><p class="wp-caption-text">Testing!</p></div>
<p>A challenge that lies ahead for me is testing the game I&#8217;m building. Unlike previous games I&#8217;ve worked on I&#8217;m working on my own with a minimal budget, so I have no QA department at my disposal with an army of young, keen, cunning testers doing their worst to try to break the game for months on end. There are &#8220;for hire&#8221; QA teams, but that is far beyond my budget. Therefore I need to do something else.</p>
<p><a href="http://www.altdevblogaday.com/2011/05/25/testing-testing-testing/" class="more-link">Read more on Testing, Testing, Testing&#8230;</a></p>
]]></description>
			<content:encoded><![CDATA[<div class="wp-caption alignleft" style="width: 610px"><img src="http://upload.wikimedia.org/wikipedia/en/e/ef/Portal_screenshot.jpg" alt="Portal Screenshot" width="600" height="375" /><p class="wp-caption-text">Testing!</p></div>
<p>A challenge that lies ahead for me is testing the game I&#8217;m building. Unlike previous games I&#8217;ve worked on I&#8217;m working on my own with a minimal budget, so I have no QA department at my disposal with an army of young, keen, cunning testers doing their worst to try to break the game for months on end. There are &#8220;for hire&#8221; QA teams, but that is far beyond my budget. Therefore I need to do something else.</p>
<p><strong>Beta Testing</strong></p>
<p>Part of the solution is to give close friends a beta copy of the game and let them play it, noting their feedback which they provide in return for a free copy of the final game. I expect that this type of testing will be most useful for gameplay feedback &#8211; finding out whether the game is actually fun, whether players get lost in my levels and so on. Of course, obvious crash bugs and other functional things will be found, but I wouldn&#8217;t expect any beta testers to play through daily builds of the game, smoke-testing all game assets, getting full coverage on all possible hardware or spending hours trying to find an easy way to reproduce intermittent bugs. For this I need something else.</p>
<p><strong>Automated Testing</strong></p>
<p>It seems that my only realistic option is automated testing. Bear in mind, I&#8217;m not talking about Test Driven Development, Unit Testing, and other source code based testing methodologies &#8211; though of course they do contribute towards the overall testing and QA of the game. I&#8217;m talking about automated testing of builds of the whole game.</p>
<p>In my experience so far, there has been little automated testing of the games I&#8217;ve worked on. Perhaps there would be a hastily written script which loads every level, every character, dynamic object, and so on, checking for crashes before QA get the build, but I&#8217;ve never seen anything very much more sophisticated.</p>
<p>I think the main reason for this lack of automated testing is that in the large companies I&#8217;ve worked for, there&#8217;s always a QA department in the building that can do the testing manually &#8211; from a producer&#8217;s point of view, adding automated testing is extra upfront work that takes time away from making the game. However, I think this sort of testing is worth the effort, even for these large companies. After all, even the most diligent human tester can miss things &#8211; on many an occasion I&#8217;ve received bug reports that turn out to have been unnoticed in the game for months and thought &#8220;How could they have missed that?&#8221;. The simple answer is usually that it wasn&#8217;t on the test plan and was probably found by a lucky accident. This is a problem that also afflicts automated testing &#8211; it can only test what you tell it to test, far more so than human testers. I&#8217;m not sure there is a good answer to this short of a <a title="Culture Mind" href="http://en.wikipedia.org/wiki/Mind_(The_Culture)">Culture Mind</a>.</p>
<p><strong>What I&#8217;d Like To Have</strong></p>
<p>What I would like is a framework to perform the following tests on every build of the game and report results for every test, adding failures into a decent bug database. It would also be good to allow easy promotion of an intended fail (e.g. a change in a graphical effect) to become the new baseline for a pass. This is not an exhaustive list. I&#8217;m sure I could come up with more things to test &#8211; suggestions are welcome in the comments!</p>
<ol>
<li>Smoke-testing. Loading every level, character, model, texture that the game uses to check they all load without crashing.</li>
<li>Render and animate every level, character, model in the game, comparing results with screenshots from previous builds.</li>
<li>Run a recorded playthrough of the entire game. I was thinking something like the old playthrough that went on behind the main menu in <a title="Quake" href="http://en.wikipedia.org/wiki/Quake_(video_game)" target="_blank">Quake</a>. Of course this wouldn&#8217;t suit all game types, though perhaps could be done in a limited fashion for those. This needs to test for crashes, dumping a callstack, game inputs and internal state, and other appropriate data for a programmer to look at.</li>
<li>To find performance hotspots (ideally from the playthrough data) and take PIX/GPA/&lt;insert performance tool&gt; grabs. Comparisons with previous builds are essential here as we need to be able to find when and where performance problems occur.</li>
<li>To soak test every level for a given number of hours.</li>
<li>To test all UI screens, and check that options actually give the correct results from rendering/audio/difficulty/etc (as applicable).</li>
<li>To test rendering at all available resolutions (if appropriate).</li>
<li>The ability to run any subset of these tests on demand (rather than overnight).</li>
<li>The ability to add new tests quickly and easily.</li>
</ol>
<p>I don&#8217;t envisage anything being able to do all of this straight out of the box, but a framework I could gradually add to would be ideal &#8211; even better if I can keep as much of its code separate from the game code as possible to avoid further bugs.</p>
<p>If you, dear reader, know of such a framework that can be set up to do this sort of thing I&#8217;d be eternally grateful if you could let me know in the comments. I&#8217;ve heard of companies having this sort of thing in their tool-sets, but the solutions tend to be proprietary and/or expensive. It&#8217;s most probably infeasible to write something myself that would do all of the above without taking longer to implement than the game itself.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.altdevblogaday.com/2011/05/25/testing-testing-testing/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>So You Want To Be A Graphics Programmer&#8230;</title>
		<link>http://www.altdevblogaday.com/2011/05/10/so-you-want-to-be-a-graphics-programmer/</link>
		<comments>http://www.altdevblogaday.com/2011/05/10/so-you-want-to-be-a-graphics-programmer/#comments</comments>
		<pubDate>Tue, 10 May 2011 13:01:20 +0000</pubDate>
		<dc:creator>Keith Judge</dc:creator>
		
		<guid isPermaLink="false">http://altdevblogaday.org/?p=5207</guid>
		<description><![CDATA[<p><img class="alignleft" src="http://upload.wikimedia.org/wikipedia/commons/2/24/Cornell_box.png" alt="" width="300" height="300" />Recently, a university undergraduate asked me on twitter for advice on becoming a graphics programmer within the games industry. I wrote a fairly detailed email response and thought the information was good enough to make an article for AltDevBlogADay. This is all my personal opinion of course.</p>
<p><a href="http://www.altdevblogaday.com/2011/05/10/so-you-want-to-be-a-graphics-programmer/" class="more-link">Read more on So You Want To Be A Graphics Programmer&#8230;&#8230;</a></p>
]]></description>
			<content:encoded><![CDATA[<p><img class="alignleft" src="http://upload.wikimedia.org/wikipedia/commons/2/24/Cornell_box.png" alt="" width="300" height="300" />Recently, a university undergraduate asked me on twitter for advice on becoming a graphics programmer within the games industry. I wrote a fairly detailed email response and thought the information was good enough to make an article for AltDevBlogADay. This is all my personal opinion of course.</p>
<p>If you&#8217;re at university, you should research whether there&#8217;s a programme to do a summer or year long internship at a games studio. There was nothing like that when I was at the University of Liverpool &#8217;97-&#8217;00 (or I wasn&#8217;t aware of it), but I&#8217;ve seen people come through that kind of programme with much greater practical game development knowledge and it goes a long way towards persuading an employer to take you on. EA, Lionhead and other large companies tend to run this sort of programme so look on their job pages too. Beware that sometimes companies don&#8217;t respond to intern applications for various reasons (team is deep in crunch, budget spent elsewhere, etc) and places are extremely limited.</p>
<p>Your best bet is to make a graphics demo, either on your own or with a small group of people. You learn more by doing than by just reading. Pick a modern graphics technique that interests you and implement it. Even better, do more than one. This is also great training for motivating yourself to get a project finished which is often the hardest part of games development, for all disciplines. Make sure you&#8217;re prepared to talk in detail about the choices you made, performance (in milliseconds, not frames per second!), quality, alternatives and trade offs in a job interview.</p>
<p>When I was in university I did a straight computer science course &#8211; there were barely any games courses available back then, but I still think that employers still value computer science graduates above games graduates as there&#8217;s a perception that you learn a greater range of software engineering skills. This could be a misconception though, as games courses are a lot better than they used to be, but you may have to fight your corner in an interview and prove you know your stuff (and not just the curriculum you were taught).</p>
<p>Computer science courses also tend to be quite maths heavy (I would hope games courses are similar), which is vital for graphics programming. Make sure you understand homogeneous coordinates, matrix maths, dot products, cross products, quaternions, normal vectors, tangent bases, etc and how these things (and countless others) are useful for transforming and lighting geometry. Learn big O notation for algorithmic execution time, understand colour spaces, gamma correction, what high dynamic range means and so on. Learn some basic lighting models - Lambert, Phong, Blinn, etc.</p>
<p><strong>Software</strong></p>
<p>In my experience, Visual Studio is pretty much universal as a code IDE (except for Apple, Linux, Android and Nintendo games), though you can of course use your favourite editor if you really want to, as long as you know Visual Studio. There is a free Express edition available from Microsoft (<a href="http://www.microsoft.com/express/Windows/">http://www.microsoft.com/express/Windows/</a>), so it won&#8217;t cost you any money to learn. The PS3 is a little different as there is a separate hardware specific debugger, but you should be able to learn that on the job.</p>
<p>You should be familiar with a source control system. Perforce (<a href="http://www.perforce.com">www.perforce.com</a>) is a good choice as a lot of game studios use it and it&#8217;s free for single users. Try to learn it on a project with other people as merging, branching and integration are good skills to have. With all source control systems, similar concepts apply so it&#8217;s essential knowledge to have. Shockingly, my university course never mentioned source control and I was naive enough to believe that people just shared code over the network or on floppy disks.</p>
<p>As you&#8217;re unlikely to have access to devkits at home or in university, you&#8217;ll most likely be learning your skills on PC. In what may come as a surprise from someone with a decade&#8217;s game development experience, I don&#8217;t know much OpenGL as there&#8217;s never been a pressing need for me to learn it. Most PC games use DirectX, though if you learn DirectX 11, make sure you also learn DirectX 9 as it&#8217;s still current for Xbox 360 and many PC games still use it to support the dwindling, but still large Windows XP market. DirectX 10 is completely superseded by DirectX 11, so it is not worth learning (you can write DirectX 11 games for DirectX 10 hardware, and even DirectX 9 hardware).</p>
<p>It&#8217;s also definitely worth learning a graphical debugger. PIX for Windows isn&#8217;t as good as the Xbox 360 version, but there are fantastic free alternatives (Intel GPA - <a href="http://software.intel.com/en-us/articles/intel-gpa/" target="_blank">http://software.intel.com/en-us/articles/intel-gpa/</a>, Nvidia Parallel Nsight - <a href="http://developer.nvidia.com/nvidia-parallel-nsight" target="_blank">http://developer.nvidia.com/nvidia-parallel-nsight</a>). These tools are not just for performance tuning on the GPU &#8211; they&#8217;re also for debugging your draw calls, working out why something doesn&#8217;t draw, why it looks wrong, and so on. You can also learn about how a GPU works as you can see all the renderstates, shaders, meshes, textures, etc for any draw call in a frame and really understand what the GPU is actually doing with the data you give it.</p>
<p><strong>Other Duties</strong></p>
<p>As a graphics coder you&#8217;ll probably have to do some tools work too, working with mesh compilers, animation compilers, plugins for Maya/3DS Max or in-house editors for the artists to use. Remember that your job is to provide technology to support the artists in their daily work, so it needs to be presented in a friendly manner. If you give your art team a tool that lets them tweak some coefficients of a fancy rendering algorithm and they have no idea what the numbers mean, they probably won&#8217;t use it. Also, technical artists are your friends &#8211; they&#8217;re the best people to talk about requirements for artists and to work out the best workflow for the content creators.</p>
<p>It&#8217;s also good to learn general performance and optimisation techniques as this often falls to the graphics/engine team to do. You probably won&#8217;t have to write any (or very little) raw assembler, but you ought to be familiar with what the C/C++ compiler is doing to your code, how to spot problems and what to do about them. For example, one of the biggest performance problem will be L2 cache misses (you lose hundreds of cycles per miss on all modern hardware), so learn techniques to reduce them (almost always changing the data, not the code is the fix).</p>
<p><strong>Online Learning Resources</strong></p>
<p>Online resources are a goldmine, and there&#8217;s much better stuff out there than there was when I was at university as a lot of companies publish papers on their techniques which are pretty useful stuff. A few examples&#8230;</p>
<p><a href="http://www.valvesoftware.com/company/publications.html" target="_blank">http://www.valvesoftware.com/company/publications.html</a><br />
<a href="http://publications.dice.se/" target="_blank">http://publications.dice.se/</a><br />
<a href="http://www.crytek.com/cryengine/presentations" target="_blank">http://www.crytek.com/cryengine/presentations</a></p>
<p>Also there are a few good blogs posting regularly about graphics. A few good examples&#8230;</p>
<p><a href="http://aras-p.info/blog/" target="_blank">http://aras-p.info/blog/</a> &#8211; Lost in the Triangles. Aras Pranckevičius&#8217;s blog (a lead programmer for Unity).<br />
<a href="http://www.realtimerendering.com/blog/" target="_blank">http://www.realtimerendering.com/blog/</a> &#8211; Real Time Rendering has good information (also the book is a worthwhile read!)<br />
<a href="http://www.humus.name/" target="_blank">http://www.humus.name/</a> &#8211; Another good graphics programming blog.</p>
<p>Make sure you read the relevant presentations from GDC (very useful) and SIGGRAPH (slightly less useful as a lot of it is for non-realtime graphics, but useful as a crystal ball for future techniques).</p>
<p>My last handy tip is that if you live near a big dev studio, find out which pub they go drinking at after work and join in on a Friday night. You&#8217;ll learn a lot just chatting with developers. You can also join twitter and talk to many games developers there who are willing to share their experience.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.altdevblogaday.com/2011/05/10/so-you-want-to-be-a-graphics-programmer/feed/</wfw:commentRss>
		<slash:comments>25</slash:comments>
		</item>
		<item>
		<title>Input for Modern PC Games</title>
		<link>http://www.altdevblogaday.com/2011/04/25/input-for-modern-pc-games/</link>
		<comments>http://www.altdevblogaday.com/2011/04/25/input-for-modern-pc-games/#comments</comments>
		<pubDate>Mon, 25 Apr 2011 02:54:25 +0000</pubDate>
		<dc:creator>Keith Judge</dc:creator>
		
		<guid isPermaLink="false">http://altdevblogaday.org/?p=4026</guid>
		<description><![CDATA[<p><a href="http://altdevblogaday.com/wp-content/uploads/2011/04/Keyboard-Cropped.png"><img class="size-full wp-image-4574 alignleft" src="http://altdevblogaday.com/wp-content/uploads/2011/04/Keyboard-Cropped.png" alt="Keyboard Question Mark" width="206" height="130" /></a>As I said at the end of my last post, I was going to write an article listing all the little handy utilities/settings that make my life easier as a programmer, but in a change in schedule I&#8217;ve decided to postpone that and write about something I&#8217;ve been coding this week instead whilst it&#8217;s still fresh in my mind.</p>
<p><a href="http://www.altdevblogaday.com/2011/04/25/input-for-modern-pc-games/" class="more-link">Read more on Input for Modern PC Games&#8230;</a></p>
]]></description>
			<content:encoded><![CDATA[<p><a href="http://altdevblogaday.com/wp-content/uploads/2011/04/Keyboard-Cropped.png"><img class="size-full wp-image-4574 alignleft" src="http://altdevblogaday.com/wp-content/uploads/2011/04/Keyboard-Cropped.png" alt="Keyboard Question Mark" width="206" height="130" /></a>As I said at the end of my last post, I was going to write an article listing all the little handy utilities/settings that make my life easier as a programmer, but in a change in schedule I&#8217;ve decided to postpone that and write about something I&#8217;ve been coding this week instead whilst it&#8217;s still fresh in my mind.</p>
<p>As I&#8217;m writing my own engine, one of the first things that needs to be done is input processing. In the latest DirectX SDK, there are two options listed for input, namely DirectInput8 and XInput. DirectInput8 is long in the tooth, being over a decade old and has localisation issues as I&#8217;ll discuss below. XInput is newer and is a cleaner API, but seems to exist entirely for using Xbox 360 controllers (presumably as an aid to porting) and supports nothing else. Neither seemed to be an ideal solution to me.</p>
<p><strong>Twitter to the Rescue</strong></p>
<p>I asked about this on Twitter and got a few replies suggesting I use the Raw Input API which I wasn&#8217;t previously aware of. <a title="richard_a_sim" href="http://twitter.com/#!/richard_a_sim" target="_blank">Richard Sim</a> pointed me to <a title="Taking Advantage of High-Definition Mouse Input" href="http://msdn.microsoft.com/en-us/library/ee418864.aspx" target="_blank">this MSDN article</a> comparing the APIs (in this case the example is reading mouse input). As you can see, the code for using Raw Input is a lot cleaner and simpler than DirectInput8. The article also says that DirectInput8 is built on top of the Raw Input API and uses a separate thread to capture the WM_INPUT messages, adding overhead in the process. It felt to me that using Raw Input directly was the better solution.</p>
<p><strong>Using Raw Input</strong></p>
<p>The MSDN documentation for the Raw Input API is missing a few things which I&#8217;ve had to dig around and find out for myself, so I&#8217;ll explain what I&#8217;ve found out here. The basic process is that in your initialisation code you register the devices you&#8217;d like input from using RegisterRawInputDevices(), and then you receive WM_INPUT messages from the device in the Windows message loop from which you extract the actual keys/mouse input/etc. If you want to register for input from the keyboard, you would use the following code.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="c" style="font-family:monospace;">RAWINPUTDEVICE keyboard_device<span style="color: #339933;">;</span>
keyboard_device.<span style="color: #202020;">usUsagePage</span> <span style="color: #339933;">=</span> <span style="color: #208080;">0x01</span><span style="color: #339933;">;</span>
keyboard_device.<span style="color: #202020;">usUsage</span> <span style="color: #339933;">=</span> <span style="color: #208080;">0x06</span><span style="color: #339933;">;</span>
keyboard_device.<span style="color: #202020;">hwndTarget</span> <span style="color: #339933;">=</span> hWnd<span style="color: #339933;">;</span>
keyboard_device.<span style="color: #202020;">dwFlags</span> <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
&nbsp;
BOOL ret <span style="color: #339933;">=</span> RegisterRawInputDevices<span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span>keyboard_device<span style="color: #339933;">,</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">,</span> <span style="color: #993333;">sizeof</span><span style="color: #009900;">&#40;</span>RAWINPUTDEVICE<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>The question is &#8211; where on Earth do those UsagePage and Usage numbers come from? The MSDN documentation doesn&#8217;t explain, but after a bit of digging I found out that they&#8217;re part of the <a title="USB HID Usage Tables" href="http://www.usb.org/developers/devclass_docs/Hut1_12.pdf" target="_blank">USB HID standard (pdf)</a>. On page 26 is table 1 for generic desktop devices, and the keyboard is device number 6, hence the numbers passed to the code above. For the mouse you would use page 1, usage 2. In fact, the Raw Input API lets you register multiple device at once as shown here.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="c" style="font-family:monospace;">RAWINPUTDEVICE keyboard_and_mouse_devices<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">2</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
keyboard_and_mouse_devices<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #009900;">&#93;</span>.<span style="color: #202020;">usUsagePage</span> <span style="color: #339933;">=</span> <span style="color: #208080;">0x01</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Generic desktop page</span>
keyboard_and_mouse_devices<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #009900;">&#93;</span>.<span style="color: #202020;">usUsage</span> <span style="color: #339933;">=</span> <span style="color: #208080;">0x06</span><span style="color: #339933;">;</span>     <span style="color: #666666; font-style: italic;">// Keyboard</span>
keyboard_and_mouse_devices<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #009900;">&#93;</span>.<span style="color: #202020;">hwndTarget</span> <span style="color: #339933;">=</span> hWnd<span style="color: #339933;">;</span>
keyboard_and_mouse_devices<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #009900;">&#93;</span>.<span style="color: #202020;">dwFlags</span> <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
keyboard_and_mouse_devices<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#93;</span>.<span style="color: #202020;">usUsagePage</span> <span style="color: #339933;">=</span> <span style="color: #208080;">0x01</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Generic desktop page</span>
keyboard_and_mouse_devices<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#93;</span>.<span style="color: #202020;">usUsage</span> <span style="color: #339933;">=</span> <span style="color: #208080;">0x02</span><span style="color: #339933;">;</span>     <span style="color: #666666; font-style: italic;">// Mouse</span>
keyboard_and_mouse_devices<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#93;</span>.<span style="color: #202020;">hwndTarget</span> <span style="color: #339933;">=</span> hWnd<span style="color: #339933;">;</span>
keyboard_and_mouse_devices<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#93;</span>.<span style="color: #202020;">dwFlags</span> <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
&nbsp;
BOOL ret <span style="color: #339933;">=</span> RegisterRawInputDevices<span style="color: #009900;">&#40;</span>keyboard_and_mouse_devices<span style="color: #339933;">,</span> <span style="color: #0000dd;">2</span><span style="color: #339933;">,</span> <span style="color: #993333;">sizeof</span><span style="color: #009900;">&#40;</span>RAWINPUTDEVICE<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>I have no idea why the API insists you pass the size of the RAWINPUTDEVICE struct as the final parameter, though it seems to be a common trait for Win32 API functions.</p>
<p>I think the MSDN docs explain actually getting the data well enough, so I won&#8217;t repeat that here.</p>
<p>Once your application is registered to recieve WM_INPUT messages, it will also receive WM_INPUT_DEVICE_CHANGE messages whenever a device is added/removed from the system so you can print up &#8220;Controller Removed&#8221; messages, or switch to an alternate controller as your game requires.</p>
<p><strong>Wrapping Up</strong></p>
<p>I mentioned above that DirectInput8 has localisation issues. What I mean by this is that often in games you need to show the name of the key on screen in control select screens or tutorials. To do this you would use <a title="GetKeyNameText Function" href="http://msdn.microsoft.com/en-us/library/ms646300(VS.85).aspx" target="_blank">GetKeyNameText()</a> which will return a string from a scan code. Unfortunately, DirectInput8 uses its own DIK_ enums for the keys, which don&#8217;t exactly map onto scan codes. On previous games I&#8217;ve worked on, we&#8217;ve ended up with a large remapping table, with a few exceptions for various locales. The Raw Input API gives you the scan code directly as well as the virtual VK_ enum, so in theory this problem disappears (I still need to confirm this).</p>
]]></content:encoded>
			<wfw:commentRss>http://www.altdevblogaday.com/2011/04/25/input-for-modern-pc-games/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>Workstation Set Up for Game Developers</title>
		<link>http://www.altdevblogaday.com/2011/04/10/workstation-setup-for-gamedevs/</link>
		<comments>http://www.altdevblogaday.com/2011/04/10/workstation-setup-for-gamedevs/#comments</comments>
		<pubDate>Sun, 10 Apr 2011 14:17:29 +0000</pubDate>
		<dc:creator>Keith Judge</dc:creator>
		
		<guid isPermaLink="false">http://altdevblogaday.org/?p=3307</guid>
		<description><![CDATA[<p>Greetings all! For my first post I thought I&#8217;d start with something I&#8217;ve been thinking about lately. As I&#8217;m preparing to leave &#8220;AAA&#8221; games development and become a fledgling indie, I need to set up my home office for maximum productivity and comfort. I don&#8217;t have a facilities department to handle all of this for me, so here&#8217;s my tips for comfort based on my experiences. I&#8217;ll leave out the actual PC/Mac hardware itself and focus more on the ergonomic aspect of things. This article isn&#8217;t meant to be authoritative (how could it be without reams of ergonomic data?) so I&#8217;ve avoided recommending specific products, but the general point is that you should try out a few things until you find what works best for you.</p>
<p><a href="http://www.altdevblogaday.com/2011/04/10/workstation-setup-for-gamedevs/" class="more-link">Read more on Workstation Set Up for Game Developers&#8230;</a></p>
]]></description>
			<content:encoded><![CDATA[<p>Greetings all! For my first post I thought I&#8217;d start with something I&#8217;ve been thinking about lately. As I&#8217;m preparing to leave &#8220;AAA&#8221; games development and become a fledgling indie, I need to set up my home office for maximum productivity and comfort. I don&#8217;t have a facilities department to handle all of this for me, so here&#8217;s my tips for comfort based on my experiences. I&#8217;ll leave out the actual PC/Mac hardware itself and focus more on the ergonomic aspect of things. This article isn&#8217;t meant to be authoritative (how could it be without reams of ergonomic data?) so I&#8217;ve avoided recommending specific products, but the general point is that you should try out a few things until you find what works best for you.</p>
<p><em>Chair &#8211; </em>First and foremost, you need a good chair. I can&#8217;t stress this enough after having lower back pain from working on a bad office chair a few years ago. If you can afford it (and have space in your home office), get an Aeron or whatever your favourite type of chair is. A really good chair may cost a lot at first, but you&#8217;ll be sitting, slouching and wheeling around on it for a long time so it&#8217;s a worthwhile investment. It&#8217;s <em>not </em>something to skimp on. Make sure you take the type of chair for a test run (test sit?) if possible. A few seconds sat on one in a shop really isn&#8217;t enough. If you know someone who has the chair you&#8217;re thinking of buying, ask if you can spend an hour or so working on it to see how it works for you (although you may irritate them if you change the settings too much!). Last of all, make sure you&#8217;re really happy with your choice before handing over your money.</p>
<p>I&#8217;m typing this article sat on a solid wooden <a title="Ikea Ivar" href="http://www.ikea.com/gb/en/catalog/products/68156009" target="_blank">Ikea Ivar</a> borrowed from the dining room which is <em>far</em> from ideal, so I need to sort out a proper chair sooner rather than later!</p>
<p><em>Desk</em> &#8211; A desk is easier to sort out. It just needs to be the right height (or adjustable) and big enough to put everything you want on it. Also it needs no obstructions underneath for your knees to accidentally bash against. I&#8217;m using an <a title="Ikea Galant" href="http://www.ikea.com/gb/en/catalog/products/S79836974" target="_blank">Ikea Galant</a> which is a nice corner desk that fits nicely in the room I&#8217;m using, has adjustable height legs, is pretty strong (it is no problem for me to sit on it) and it is easily big enough for all the desktop equipment I need. Make sure you have measurements of the room you&#8217;re going to use before buying so you can eliminate anything that won&#8217;t fit quickly.</p>
<p><em>Monitor(s) and Light</em> &#8211; A monitor should be big, bright, comfortable to read, well calibrated and more than one if possible. Make sure the height is set up so you don&#8217;t strain your neck as you look at it. I&#8217;m counting light as part of this category as I think it is intrinsically linked to how well your monitor works for you. I like there to be a good level of ambient light in the room to avoid eye-strain from the monitors, but make sure the light is out of your field of vision or it will irritate you in your peripheral zone. Other people I&#8217;ve worked with (mostly artists and older coders), seem to have a preference for working in the dark &#8211; though I find that odd as it hurts my eyes after a while. I can (sort of) understand that way of working in the bad old days of curved, highly reflective CRT monitors as darkness would minimise reflections, but these days I think that is minimal. However, it is of course up to you how you light your room &#8211; whatever works best for you is most important.</p>
<p><em>Keyboard</em> &#8211; You&#8217;re going to need something you can type on for hours without causing pain to your wrists. Ideally you want your hands and forearms fairly flat on the desk and not bent upwards so your wrists aren&#8217;t strained. Ergonomic, straight &#8211; it&#8217;s up to you, but find something that works for you and doesn&#8217;t cause pain after extended use. Also, you need to find a keyboard that has a key feel you like (how much travel, how &#8220;clicky&#8221; it is), sounds right (not too loud, not too quiet). Some keyboards also have extra buttons mimicking browser navigation, application shortcuts, volume control, etc. How useful these features are is debatable (I quite like having volume/mute buttons), but once again what works for you is best.</p>
<p><em>Mouse</em> &#8211; Likewise, find something that fits your hand well, has all the buttons you need (mine is right hand shaped with two handy thumb buttons) and slides about the desk smoothly. Optical mice are all very good these days in terms of ability to read surfaces accurately so unless your desk is made of glass, you shouldn&#8217;t need a mouse mat. Alternatively, you might be someone who prefers a pad, trackball or other pointing device &#8211; I only have limited experience with these so comments below welcome! I&#8217;ve recently bought a Wacom Bamboo pad and am getting used to using a pen as a pointing device (as well as for drawing lines and curves).</p>
<p><em>Headphones</em> &#8211; As I&#8217;ll be working in a house with two young children around, I&#8217;ll need a way to block out the distraction of their noise whilst they are playing, so a good, comfortable set of headphones is important to me. Find some that don&#8217;t hurt your ears and have good sound quality (I find cheap tinny sound tends to irritate after a while). Noise cancelling is an option, though on the ones I&#8217;ve tried, sound quality seems flatter (perhaps that&#8217;s just me though).</p>
<p><em>Space</em> &#8211; An odd thing to include, but I find that when I&#8217;m stuck on a problem, I like to pace back on forth whilst my brain is working on a solution. It&#8217;s something I&#8217;ve deliberately refrained from at big companies as being watched by an office full of people stops my thought processes, but I do it at home (to the chagrin of my wife). Having an area you can do this without driving the people who live with you insane is probably a good idea!</p>
<p>I&#8217;d love comments on this article from people with suggestions of your own. For my next article, I plan to look at the software side of things &#8211; those little utilities that make my life as a programmer easier and more productive.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.altdevblogaday.com/2011/04/10/workstation-setup-for-gamedevs/feed/</wfw:commentRss>
		<slash:comments>35</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic page generated in 1.196 seconds. -->
<!-- Cached page generated by WP-Super-Cache on 2012-05-17 04:02:31 -->
<!-- Compression = gzip -->
