<?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>What Ev &#187; graphics programming</title>
	<atom:link href="http://what-ev.net/tag/graphics-programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://what-ev.net</link>
	<description>Chris Cascioli - Graphics Programming &#124; Game Design</description>
	<lastBuildDate>Mon, 14 Jun 2010 04:37:23 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Dynamic Billboarded Integer Rendering</title>
		<link>http://what-ev.net/2010/06/13/dynamic-billboarded-integer-rendering/</link>
		<comments>http://what-ev.net/2010/06/13/dynamic-billboarded-integer-rendering/#comments</comments>
		<pubDate>Sun, 13 Jun 2010 21:06:48 +0000</pubDate>
		<dc:creator>Chris</dc:creator>
				<category><![CDATA[My Graphics Programming]]></category>
		<category><![CDATA[My Stuff]]></category>
		<category><![CDATA[Aliens]]></category>
		<category><![CDATA[graphics programming]]></category>

		<guid isPermaLink="false">http://what-ev.net/?p=115</guid>
		<description><![CDATA[
A few months ago I was working on Aliens and implementing a new scoring system.  I ran into an issue: I needed a way to dynamically render a number (basically a point value) on a billboarded quad.  It had to be dynamic because the value of each item would change based on a [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://what-ev.net/wordpress/wp-content/uploads/2010/06/scores.png" target="_blank"><img class="size-medium wp-image-119 alignnone" title="Aliens Dynamic Score 2" src="http://what-ev.net/wordpress/wp-content/uploads/2010/06/scores-300x145.png" alt="Aliens Dynamic Score" width="300" height="145" /></a></p>
<p>A few months ago I was working on Aliens and implementing a new scoring system.  I ran into an issue: I needed a way to dynamically render a number (basically a point value) on a billboarded quad.  It had to be dynamic because the value of each item would change based on a multiplier, which could also change at any time.  I could have done it as a UI element instead of actual geometry, but that didn&#8217;t feel right for the game.  I was committed to having the scores be billboarded quads in the scene.</p>
<p><img class="size-medium wp-image-118   alignright" title="Aliens Dynamic Score" src="http://what-ev.net/wordpress/wp-content/uploads/2010/06/score-261x300.png" alt="Aliens Dynamic Score" width="190" height="218" /></p>
<p>My first thought was to simply render a number to a texture and apply that texture to the quads.  This would work great if I needed few unique numbers.  I could render the current numbers to a few textures, or create a large texture with multiple numbers and map the numbers to different quads.  I quickly realized that this wouldn&#8217;t work for Aliens; if different items were worth different amounts, and your multiplier was frequently changing, the quantity of different numbers that were required would be changing as well.  Since the quads fade in and out, the player could destroy an object worth 500 points, get a multiplier, and then destroy the same type of object which is now worth 1000 points.  Both quads would be on the screen at the same time.  It could get hairy fast.</p>
<p>I needed the whole process to be dynamic, so that I could feed an integer to the shader and have it render.  I wrestled with it for a few days until I came up with the solution I ended up using: If I could get both the number of digits in a number, and any one digit based on the position inside the quad, I could render that number.  The results were exactly what I wanted: Dynamic numbers on geometry in the scene, in a font that matched the rest of the game.</p>
<p>So how does it work?  I&#8217;ve got some shader code below that shows the process.  Several pieces of data are passed in: the number itself, the number of digits, the age of the quad, a scale and a color.  The number, scale and color are self-explanatory.  The number of digits is used in several places.  It could have been calculated in the shader, but it seemed better to calculate it once when the quad was created.  The age of the quad is used to fade it in and out, as well as make it float upwards.  The numbers are pulled from a greyscale texture:</p>
<p><img class="size-full wp-image-116  alignnone" title="Digit Texture" src="http://what-ev.net/wordpress/wp-content/uploads/2010/06/digits.png" alt="Digit Texture" width="512" height="64" /></p>
<p>Below is my vertex shader code.  The first block of code does the billboarding and scaling.  The overall scale and Y position are based on age.  This allows the quad to grow and move upwards over time.  The width is based on the number of digits that we need to render; the quad auto-adjusts to fit the number.  The BigUV output parameter scales the overall UV by the number of digits, which is used in the pixel shader to determine which digit a particular pixel represents.  There are constants mixed in which adjust the height and alpha (to achieve the exact look I wanted).</p>
<div class="codecolorer-container glsl default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br /></div></td><td><div class="glsl codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666; font-style: italic;">// Calculate the position</span><br />
scale <span style="color: #000066;">*=</span> 1<span style="color: #000066;">.</span>0f <span style="color: #000066;">+</span> age<span style="color: #000066;">;</span><br />
<span style="color: #333399; font-weight: bold;">output</span><span style="color: #000066;">.</span><span style="color: #006600;">Position</span> <span style="color: #000066;">=</span> mul<span style="color: #000066;">&#40;</span>float4<span style="color: #000066;">&#40;</span><span style="color: #0000ff;">0</span><span style="color: #000066;">,</span> <span style="color: #0000ff;">0</span><span style="color: #000066;">,</span> <span style="color: #0000ff;">0</span><span style="color: #000066;">,</span> <span style="color: #0000ff;">1</span><span style="color: #000066;">&#41;</span><span style="color: #000066;">,</span> mul<span style="color: #000066;">&#40;</span>World<span style="color: #000066;">,</span> View<span style="color: #000066;">&#41;</span><span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span><br />
<span style="color: #333399; font-weight: bold;">output</span><span style="color: #000066;">.</span><span style="color: #006600;">Position</span><span style="color: #000066;">.</span><span style="color: #006600;">x</span> <span style="color: #000066;">+=</span> <span style="color: #333399; font-weight: bold;">input</span><span style="color: #000066;">.</span><span style="color: #006600;">Position</span><span style="color: #000066;">.</span><span style="color: #006600;">x</span> <span style="color: #000066;">*</span> scale <span style="color: #000066;">*</span> numDigits<span style="color: #000066;">;</span><br />
<span style="color: #333399; font-weight: bold;">output</span><span style="color: #000066;">.</span><span style="color: #006600;">Position</span><span style="color: #000066;">.</span><span style="color: #006600;">y</span> <span style="color: #000066;">+=</span> <span style="color: #333399; font-weight: bold;">input</span><span style="color: #000066;">.</span><span style="color: #006600;">Position</span><span style="color: #000066;">.</span><span style="color: #006600;">y</span> <span style="color: #000066;">*</span> scale <span style="color: #000066;">*</span> 1<span style="color: #000066;">.</span>5f <span style="color: #000066;">+</span> <span style="color: #000066;">&#40;</span>15<span style="color: #000066;">.</span>0f <span style="color: #000066;">*</span> age<span style="color: #000066;">&#41;</span> <span style="color: #000066;">+</span> 5<span style="color: #000066;">.</span>0f<span style="color: #000066;">;</span><br />
<span style="color: #333399; font-weight: bold;">output</span><span style="color: #000066;">.</span><span style="color: #006600;">Position</span> <span style="color: #000066;">=</span> mul<span style="color: #000066;">&#40;</span><span style="color: #333399; font-weight: bold;">output</span><span style="color: #000066;">.</span><span style="color: #006600;">Position</span><span style="color: #000066;">,</span> Projection<span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span><br />
<br />
<span style="color: #666666; font-style: italic;">// Pass the UVs through</span><br />
<span style="color: #333399; font-weight: bold;">output</span><span style="color: #000066;">.</span><span style="color: #006600;">UV</span> <span style="color: #000066;">=</span> <span style="color: #333399; font-weight: bold;">input</span><span style="color: #000066;">.</span><span style="color: #006600;">UV</span><span style="color: #000066;">;</span><br />
<span style="color: #333399; font-weight: bold;">output</span><span style="color: #000066;">.</span><span style="color: #006600;">BigUV</span> <span style="color: #000066;">=</span> <span style="color: #333399; font-weight: bold;">input</span><span style="color: #000066;">.</span><span style="color: #006600;">UV</span><span style="color: #000066;">.</span><span style="color: #006600;">x</span> <span style="color: #000066;">*</span> numDigits<span style="color: #000066;">;</span><br />
<br />
<span style="color: #666666; font-style: italic;">// Adjust the alpha based on age</span><br />
<span style="color: #333399; font-weight: bold;">output</span><span style="color: #000066;">.</span><span style="color: #006600;">Alpha</span> <span style="color: #000066;">=</span> saturate<span style="color: #000066;">&#40;</span>5<span style="color: #000066;">.</span>0f <span style="color: #000066;">-</span> <span style="color: #993333; font-weight: bold;">abs</span><span style="color: #000066;">&#40;</span>age <span style="color: #000066;">*</span> 10<span style="color: #000066;">.</span>0f <span style="color: #000066;">-</span> 5<span style="color: #000066;">.</span>0f<span style="color: #000066;">&#41;</span><span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span></div></td></tr></tbody></table></div>
<p>The pixel shader code below determines which place the current pixel represents and which digit from the digit texture it needs to sample.</p>
<div class="codecolorer-container glsl default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br /></div></td><td><div class="glsl codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666; font-style: italic;">// Get the current digit</span><br />
<span style="color: #000066; font-weight: bold;">float</span> place <span style="color: #000066;">=</span> numDigits <span style="color: #000066;">-</span> <span style="color: #993333; font-weight: bold;">floor</span><span style="color: #000066;">&#40;</span><span style="color: #333399; font-weight: bold;">input</span><span style="color: #000066;">.</span><span style="color: #006600;">BigUV</span><span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span><br />
<span style="color: #000066; font-weight: bold;">float</span> digit <span style="color: #000066;">=</span> <span style="color: #993333; font-weight: bold;">floor</span><span style="color: #000066;">&#40;</span><span style="color: #000066;">&#40;</span>number <span style="color: #000066;">%</span> <span style="color: #993333; font-weight: bold;">pow</span><span style="color: #000066;">&#40;</span><span style="color: #0000ff;">10</span><span style="color: #000066;">,</span> place<span style="color: #000066;">&#41;</span><span style="color: #000066;">&#41;</span> <span style="color: #000066;">/</span> <span style="color: #993333; font-weight: bold;">pow</span><span style="color: #000066;">&#40;</span><span style="color: #0000ff;">10</span><span style="color: #000066;">,</span> place <span style="color: #000066;">-</span> <span style="color: #0000ff;">1</span><span style="color: #000066;">&#41;</span><span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span><br />
<br />
<span style="color: #666666; font-style: italic;">// Calculate the correct U coord</span><br />
<span style="color: #000066; font-weight: bold;">float</span> u <span style="color: #000066;">=</span> <span style="color: #333399; font-weight: bold;">input</span><span style="color: #000066;">.</span><span style="color: #006600;">BigUV</span><span style="color: #000066;">.</span><span style="color: #006600;">x</span> <span style="color: #000066;">-</span> <span style="color: #993333; font-weight: bold;">floor</span><span style="color: #000066;">&#40;</span><span style="color: #333399; font-weight: bold;">input</span><span style="color: #000066;">.</span><span style="color: #006600;">BigUV</span><span style="color: #000066;">.</span><span style="color: #006600;">x</span><span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span><br />
u <span style="color: #000066;">*=</span> 0<span style="color: #000066;">.</span>1f<span style="color: #000066;">;</span><br />
u <span style="color: #000066;">+=</span> digit <span style="color: #000066;">*</span> 0<span style="color: #000066;">.</span>1f<span style="color: #000066;">;</span><br />
<span style="color: #333399; font-weight: bold;">input</span><span style="color: #000066;">.</span><span style="color: #006600;">UV</span><span style="color: #000066;">.</span><span style="color: #006600;">x</span> <span style="color: #000066;">=</span> u<span style="color: #000066;">;</span><br />
<br />
<span style="color: #666666; font-style: italic;">// Return the correct color</span><br />
color<span style="color: #000066;">.</span><span style="color: #006600;">a</span> <span style="color: #000066;">=</span> <span style="color: #333399; font-weight: bold;">input</span><span style="color: #000066;">.</span><span style="color: #006600;">Alpha</span><span style="color: #000066;">;</span><br />
<span style="color: #000000; font-weight: bold;">return</span> tex2D<span style="color: #000066;">&#40;</span>digitSampler<span style="color: #000066;">,</span> <span style="color: #333399; font-weight: bold;">input</span><span style="color: #000066;">.</span><span style="color: #006600;">UV</span><span style="color: #000066;">&#41;</span> <span style="color: #000066;">*</span> color<span style="color: #000066;">;</span></div></td></tr></tbody></table></div>
<p><a href="http://what-ev.net/wordpress/wp-content/uploads/2010/06/DynamicBillboardInteger.jpg" target="_blank"><img class="alignright size-thumbnail wp-image-117" title="Dynamic Billboarded Integer Rendering" src="http://what-ev.net/wordpress/wp-content/uploads/2010/06/DynamicBillboardInteger-80x150.jpg" alt="Dynamic Billboarded Integer Rendering" width="80" height="150" /></a></p>
<p>That&#8217;s about it!  I haven&#8217;t really done any optimization, and my math may be a little verbose, but it works pretty well.  I&#8217;ve been meaning to make a post about this for a while, but I haven&#8217;t been working on Aliens since development started on Blocks.  Once Blocks wraps up, I&#8217;ll probably revisit this (and start refactoring most of Aliens).  I made a simple <a href="http://what-ev.net/wordpress/wp-content/uploads/2010/06/DynamicBillboardInteger.jpg" target="_blank">info-graphic</a> on the overall process, which may or may not help visualize what&#8217;s going on.</p>
<p>-Chris</p>
]]></content:encoded>
			<wfw:commentRss>http://what-ev.net/2010/06/13/dynamic-billboarded-integer-rendering/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>XNA Fur Rendering</title>
		<link>http://what-ev.net/2009/11/22/xna-fur-rendering/</link>
		<comments>http://what-ev.net/2009/11/22/xna-fur-rendering/#comments</comments>
		<pubDate>Mon, 23 Nov 2009 02:24:54 +0000</pubDate>
		<dc:creator>Chris</dc:creator>
				<category><![CDATA[My Graphics Programming]]></category>
		<category><![CDATA[fur]]></category>
		<category><![CDATA[graphics programming]]></category>
		<category><![CDATA[tech demo]]></category>
		<category><![CDATA[video]]></category>
		<category><![CDATA[XNA]]></category>

		<guid isPermaLink="false">http://what-ev.net/?p=109</guid>
		<description><![CDATA[I was pretty sick last weekend, and sticking to my odd &#8220;get sick, code something&#8221; habit, I decided to finally try to make a fur rendering demo.  I read a few articles about it years ago, so I knew the general concept behind it: render lots of &#8220;shells&#8221; around the model with an appropriate [...]]]></description>
			<content:encoded><![CDATA[<p>I was pretty sick last weekend, and sticking to my odd &#8220;get sick, code something&#8221; habit, I decided to finally try to make a fur rendering demo.  I read a few articles about it years ago, so I knew the general concept behind it: render lots of &#8220;shells&#8221; around the model with an appropriate fur texture.  The fur texture is basically a transparent texture with dots on it, and as the shells get layered on top of each other, the dots are essentially extruded into individual furs (without having to render each fur individually).</p>
<p><object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/o36G4AiapiE&#038;hl=en_US&#038;fs=1&#038;"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/o36G4AiapiE&#038;hl=en_US&#038;fs=1&#038;" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed></object></p>
<p>I&#8217;ve seen fur effects used in a few games, notably <a href="http://cubemedia.ign.com/media/previews/image/starfoxa/fur1_640w.jpg">Star Fox Adventures</a> for the GameCube, and the more recent <a href="http://image.com.com/gamespot/images/2007/303/915692_20071030_screen109.jpg">Super Mario Galaxy</a> for the Wii.  I have no idea if they are using the same technique, but the Super Mario Galaxy queen bee looks similar.</p>
<p>What really got me thinking about finally starting this demo was an article I found on <a href="http://www.reddit.com">reddit</a>.  The article is from <a href="http://www.xbdev.net">xbdev.net</a>, and is (appropriately) called <a href="http://www.xbdev.net/directx3dx/specialX/Fur/index.php">Generating Fur in DirectX or OpenGL Easily</a>.  While it&#8217;s not the most well-written article I&#8217;ve ever read (especially the comments in the provided code), it has some nice diagrams and screenshots of the technique in progress.  If you&#8217;re interested in learning more about fur rendering, I&#8217;d suggest starting there.</p>
<p>After reading that article, I decided to go for it.  I got the basics up and running pretty quickly.  I added some camera controls and the ability to change a few things on the fly (like the fur length), mostly for testing purposes.  One of the first things you notice with basic fur rendering is that everything is the same color, so you only notice the &#8220;fur&#8221; aspect of it around the edges.  I added some basic N dot L lighting and I modified the fur by the model&#8217;s texture so it wasn&#8217;t all the exactly the same.  This helped a bit, but the hairs were still bleeding into each other and not looking very fur-like.  Clearly some sort of individual fur shading was needed.</p>
<p>I went back to the fur article linked above to check out their shadowing technique.  To be honest, I&#8217;m not 100% sure I even implemented it the way they explained it (which I would have to describe as &#8220;poorly&#8221;).  The gist of it is to re-render each shell, offsetting the texture a bit and making it look like a shadow.  I didn&#8217;t like it from the start.  Double the number of shells that need to be rendered?  No thanks.  I didn&#8217;t need to render an actual shadow of each fur.  I just wanted the fur to get darker the &#8220;deeper&#8221; it was, so that&#8217;s what I did.  The shells closer to the model are rendered a bit darker, and they get brighter as they get further away.  The last piece of this puzzle was to make sure the model itself was rendered slightly darker, giving the illusion that the fur is shading the scalp.</p>
<p>One thing I did implement from the article was the concept of gravity affecting the fur.  Each shell is offset a tiny bit in the negative Y direction (based on a power function) to give the impression that the fur is bending due to gravity.  It&#8217;s a very nice effect, and it was easy to implement.  I decided to take it a step further and make the fur react as you drag the model around.  When you drag it, the fur gets stretched in the opposite direction and slowly interpolates back to its normal &#8220;bent&#8221; position.  It&#8217;s fake physics, but it looks neat.</p>
<p>I&#8217;ve got some plans to continue this the next time I&#8217;m bored.  A few features off the top of my head would be: multiple models &#038; swapping at run time, more controls for different settings (gravity, fur density, etc) and adding a fur length map, so certain sections of the model can have different lengths of fur (or be bald).</p>
<p>-Chris</p>
]]></content:encoded>
			<wfw:commentRss>http://what-ev.net/2009/11/22/xna-fur-rendering/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
