<?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>TepNetwork</title>
	<atom:link href="http://www.3dgametechnology.com/wp/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.3dgametechnology.com/wp</link>
	<description>Dev Experience. Shared.</description>
	<lastBuildDate>Thu, 08 Sep 2011 16:56:19 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.4</generator>
		<item>
		<title>DDS and DXT format</title>
		<link>http://www.3dgametechnology.com/wp/dds-and-dxt-format/</link>
		<comments>http://www.3dgametechnology.com/wp/dds-and-dxt-format/#comments</comments>
		<pubDate>Wed, 07 Sep 2011 15:47:22 +0000</pubDate>
		<dc:creator>tep</dc:creator>
				<category><![CDATA[Tools]]></category>

		<guid isPermaLink="false">http://www.3dgametechnology.com/wp/?p=271</guid>
		<description><![CDATA[If you are working on a game chances are you will work with DDS (DirectDrawSurface) files. DDS file is a standard format by Microsoft to store image/textures. It can store single image, mip maps, cube maps, etc2 and can have standard DXT compression technique. If you want to understand various DXT variants (DXT1, DXT1a, DXT3, [...]]]></description>
			<content:encoded><![CDATA[<p>If you are working on a game chances are you will work with DDS (DirectDrawSurface) files. DDS file is a standard format by Microsoft to store image/textures. It can store single image, mip maps, cube maps, etc2 and can have standard DXT compression technique.</p>
<p>If you want to understand various DXT variants (DXT1, DXT1a, DXT3, DXT5), you can check out:</p>
<ul>
<li><a href="http://www.fsdeveloper.com/wiki/index.php?title=DXT_compression_explained">http://www.fsdeveloper.com/wiki/index.php?title=DXT_compression_explained</a></li>
</ul>
<p>The following library/tools are proven to be useful:</p>
<ul>
<li>NVIDIA Texture Tools for Photoshop <a href="http://developer.nvidia.com/nvidia-texture-tools-adobe-photoshop">http://developer.nvidia.com/nvidia-texture-tools-adobe-photoshop</a></li>
<li>NVIDIA Texture Tools Source Code <a href="http://code.google.com/p/nvidia-texture-tools/">http://code.google.com/p/nvidia-texture-tools/</a></li>
<li>libsquish (open source DXT compression library) <a href="http://code.google.com/p/libsquish/">http://code.google.com/p/libsquish/</a></li>
</ul>
<p>If you ever need to write your own source code, the following links can be your <span style="text-decoration: underline;"><em>starting point</em></span> (don&#8217;t just copy and paste, some of them have bugs):</p>
<ul>
<li>DDS image format loader <a href="http://www.mindcontrol.org/~hplus/graphics/dds-info/">http://www.mindcontrol.org/~hplus/graphics/dds-info/</a></li>
<li>S3TC DXT1 / DXT5 Texture Decompression Routines <a href="http://www.glassechidna.com.au/2009/devblogs/s3tc-dxt1dxt5-texture-decompression/">http://www.glassechidna.com.au/2009/devblogs/s3tc-dxt1dxt5-texture-decompression/</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.3dgametechnology.com/wp/dds-and-dxt-format/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Reconstructing Position From Depth</title>
		<link>http://www.3dgametechnology.com/wp/reconstructing-position-from-depth/</link>
		<comments>http://www.3dgametechnology.com/wp/reconstructing-position-from-depth/#comments</comments>
		<pubDate>Wed, 23 Feb 2011 05:32:40 +0000</pubDate>
		<dc:creator>tep</dc:creator>
				<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[game development]]></category>
		<category><![CDATA[math]]></category>

		<guid isPermaLink="false">http://www.3dgametechnology.com/wp/?p=251</guid>
		<description><![CDATA[Matt posted an excellent article about reconstructing position from depth. Check out his article here: http://mynameismjp.wordpress.com/2009/03/10/reconstructing-position-from-depth/ He has the following function to reconstruct View Space Position from Post Clip Space Position: // Function for converting depth to view-space position // in deferred pixel shader pass. &#160;vTexCoord is a texture // coordinate for a full-screen quad, [...]]]></description>
			<content:encoded><![CDATA[<p>Matt posted an excellent article about reconstructing position from depth. Check out his article here: <a href="http://mynameismjp.wordpress.com/2009/03/10/reconstructing-position-from-depth/">http://mynameismjp.wordpress.com/2009/03/10/reconstructing-position-from-depth/</a></p>
<p>He has the following function to reconstruct <em>View Space Position</em> from <em>Post Clip Space Position</em>:</p>
<div class="codecolorer-container cpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666;">// Function for converting depth to view-space position</span><br />
<span style="color: #666666;">// in deferred pixel shader pass. &nbsp;vTexCoord is a texture</span><br />
<span style="color: #666666;">// coordinate for a full-screen quad, such that x=0 is the</span><br />
<span style="color: #666666;">// left of the screen, and y=0 is the top of the screen.</span><br />
float3 VSPositionFromDepth<span style="color: #008000;">&#40;</span>float2 vTexCoord<span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #666666;">// Get the depth value for this pixel</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">float</span> z <span style="color: #000080;">=</span> tex2D<span style="color: #008000;">&#40;</span>DepthSampler, vTexCoord<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <br />
<br />
&nbsp; &nbsp; <span style="color: #666666;">// Get x/w and y/w from the viewport position</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">float</span> x <span style="color: #000080;">=</span> vTexCoord.<span style="color: #007788;">x</span> <span style="color: #000040;">*</span> <span style="color: #0000dd;">2</span> <span style="color: #000040;">-</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">float</span> y <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span><span style="color: #0000dd;">1</span> <span style="color: #000040;">-</span> vTexCoord.<span style="color: #007788;">y</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">*</span> <span style="color: #0000dd;">2</span> <span style="color: #000040;">-</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; float4 vProjectedPos <span style="color: #000080;">=</span> float4<span style="color: #008000;">&#40;</span>x, y, z, <span style="color:#800080;">1.0f</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #666666;">// Transform by the inverse projection matrix</span><br />
&nbsp; &nbsp; float4 vPositionVS <span style="color: #000080;">=</span> mul<span style="color: #008000;">&#40;</span>vProjectedPos, g_matInvProjection<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> &nbsp;<br />
<br />
&nbsp; &nbsp; <span style="color: #666666;">// Divide by w to get the view-space position</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">return</span> vPositionVS.<span style="color: #007788;">xyz</span> <span style="color: #000040;">/</span> vPositionVS.<span style="color: #007788;">w</span><span style="color: #008080;">;</span> &nbsp;<br />
<span style="color: #008000;">&#125;</span></div></div>
<p>The basic idea is that if we have a matrix that transfoms from View Space to Clip Space, we just take the inverse of that matrix and multiply it with the clip space position we should be able to get back the view space position. But my biggest question was why did we need to divide-by-w (in view space) at the end?</p>
<p>I thought the order is like this:</p>
<div class="codecolorer-container cpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">PosInClipSpace <span style="color: #000080;">=</span> PosInViewSpace <span style="color: #000040;">*</span> ProjMtx<span style="color: #008080;">;</span><br />
PosInPostClipSpace <span style="color: #000080;">=</span> PosInClipSpace <span style="color: #000040;">/</span> PosInClipSpace.<span style="color: #007788;">w</span><span style="color: #008080;">;</span></div></div>
<p>so, if we want to get back from PosInPostClipSpace to PosInViewSpace, we need to do this:</p>
<div class="codecolorer-container cpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">PosInClipSpace <span style="color: #000080;">=</span> PosInPostClipSpace <span style="color: #000040;">*</span> PosInClipSpace.<span style="color: #007788;">w</span><span style="color: #008080;">;</span><br />
PosInViewSpace <span style="color: #000080;">=</span> PosInClipSpace <span style="color: #000040;">*</span> ProjMtx_INVERSE<span style="color: #008080;">;</span></div></div>
<p>so, somehow we need to save the w in order to get back the position in View Space. Out of curiosity, I did the Inverse of Projection Matrix MANUALLY and work out the following:</p>
<div class="codecolorer-container cpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">PosInViewSpace0 <span style="color: #000080;">=</span> PosInPostClipSpace <span style="color: #000040;">*</span> ProjMtx_INVERSE<span style="color: #008080;">;</span><br />
PosInViewSpace <span style="color: #000080;">=</span> PosInViewSpace0 <span style="color: #000040;">/</span> PosInViewSpace0.<span style="color: #007788;">w</span><span style="color: #008080;">;</span></div></div>
<p>It turns out the MATH WORKS OUT! I can&#8217;t see why this is happening logically, but mathematically it&#8217;s correct! It&#8217;s amazing, I learned a new thing today!</p>
<p>My next question is, if I want to bring it to World Space instead of View Space, do I need 2 matrix multiplication steps such as the following:</p>
<div class="codecolorer-container cpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">PosInViewSpace0 <span style="color: #000080;">=</span> PosInPostClipSpace <span style="color: #000040;">*</span> ProjMtx_INVERSE<span style="color: #008080;">;</span> &nbsp; &nbsp;<span style="color: #666666;">// First Mtx Mult</span><br />
PosInViewSpace <span style="color: #000080;">=</span> PosInViewSpace0 <span style="color: #000040;">/</span> PosInViewSpace0.<span style="color: #007788;">w</span><span style="color: #008080;">;</span><br />
PosInWorldSpace<span style="color: #000080;">=</span> PosInViewSpace <span style="color: #000040;">*</span> ViewMtx_INVERSE<span style="color: #008080;">;</span> &nbsp; &nbsp;<span style="color: #666666;">// Second Mtx Mult</span></div></div>
<p>It turns out, I don&#8217;t need that. I can just multiply with ViewProjMtx_INVERSE and divide-by-w at the end, i.e.:</p>
<div class="codecolorer-container cpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">PosInWorldSpace0 <span style="color: #000080;">=</span> PosInPostClipSpace <span style="color: #000040;">*</span> ViewProjMtx_INVERSE<span style="color: #008080;">;</span><br />
PosInWorldSpace <span style="color: #000080;">=</span> PosInWorldSpace0 <span style="color: #000040;">/</span> PosInWorldSpace0.<span style="color: #007788;">w</span><span style="color: #008080;">;</span></div></div>
<p>This works because if you pay attention to ViewMtx_INVERSE, the fourth column is actually (0, 0, 0, 1) which basically doesn&#8217;t modify the w. That&#8217;s why you can do divide-by-w at the end and still get the correct result!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.3dgametechnology.com/wp/reconstructing-position-from-depth/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>C# (Empty) Method Stripping</title>
		<link>http://www.3dgametechnology.com/wp/c-empty-method-stripping/</link>
		<comments>http://www.3dgametechnology.com/wp/c-empty-method-stripping/#comments</comments>
		<pubDate>Tue, 18 Jan 2011 03:52:51 +0000</pubDate>
		<dc:creator>tep</dc:creator>
				<category><![CDATA[Optimization]]></category>
		<category><![CDATA[c++]]></category>
		<category><![CDATA[game development]]></category>

		<guid isPermaLink="false">http://www.3dgametechnology.com/wp/?p=240</guid>
		<description><![CDATA[When we develop games (or any apps really), we typically have at least two configurations: DEBUG and RELEASE. Furthermore, we usually add a bunch of debug code that should only be visible in DEBUG mode, but not in RELEASE (FINAL) mode. I don&#8217;t know about you, but I am spoiled by C++ thinking that when [...]]]></description>
			<content:encoded><![CDATA[<p>When we develop games (or any apps really), we typically have at least two configurations: DEBUG and RELEASE. Furthermore, we usually add a bunch of debug code that should only be visible in DEBUG mode, but not in RELEASE (FINAL) mode. </p>
<p>I don&#8217;t know about you, but I am spoiled by C++ thinking that when we switch on the optimization, the compiler should strip empty method. However, having been in the game industry for a while, I have the mentality that we can&#8217;t really assume anything until we confirm it. So, I made a test creating an empty function and function with conditional attribute.</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System</span><span style="color: #008000;">;</span><br />
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.Diagnostics</span><span style="color: #008000;">;</span><br />
<br />
<span style="color: #0600FF; font-weight: bold;">namespace</span> OptTest<br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">class</span> Debug<br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> EmptyFunction<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> <span style="color: #008000;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#91;</span>Conditional<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;DEBUG&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> DebugFunction<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Debug Here&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">class</span> Program<br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> Main<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> args<span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Debug<span style="color: #008000;">.</span><span style="color: #0000FF;">EmptyFunction</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Debug<span style="color: #008000;">.</span><span style="color: #0000FF;">DebugFunction</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
<span style="color: #008000;">&#125;</span></div></div>
<p>This is how the disassembly look like in DEBUG mode:</p>
<div class="codecolorer-container asm default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="asm codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; &nbsp; &nbsp; static void Main<span style="color: #009900; font-weight: bold;">&#40;</span>string<span style="color: #009900; font-weight: bold;">&#91;</span><span style="color: #009900; font-weight: bold;">&#93;</span> args<span style="color: #009900; font-weight: bold;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900; font-weight: bold;">&#123;</span><br />
<span style="color: #adadad; font-style: italic;">00000000</span> &nbsp;<span style="color: #00007f; font-weight: bold;">mov</span> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">qword</span> <span style="color: #000000; font-weight: bold;">ptr</span> <span style="color: #009900; font-weight: bold;">&#91;</span>rsp<span style="color: #339933;">+</span><span style="color: #0000ff;">8</span><span style="color: #009900; font-weight: bold;">&#93;</span><span style="color: #339933;">,</span>rcx <br />
<span style="color: #adadad; font-style: italic;">00000005</span> &nbsp;<span style="color: #00007f; font-weight: bold;">sub</span> &nbsp; &nbsp; &nbsp; &nbsp; rsp<span style="color: #339933;">,</span><span style="color: #0000ff;">28h</span> <br />
<span style="color: #adadad; font-style: italic;">00000009</span> &nbsp;<span style="color: #00007f; font-weight: bold;">nop</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
<span style="color: #adadad; font-style: italic;">0000000a</span> &nbsp;<span style="color: #00007f; font-weight: bold;">mov</span> &nbsp; &nbsp; &nbsp; &nbsp; rax<span style="color: #339933;">,</span><span style="color: #0000ff;">7FF001B1DF8h</span> <br />
<span style="color: #adadad; font-style: italic;">00000014</span> &nbsp;<span style="color: #00007f; font-weight: bold;">mov</span> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #00007f;">eax</span><span style="color: #339933;">,</span><span style="color: #000000; font-weight: bold;">dword</span> <span style="color: #000000; font-weight: bold;">ptr</span> <span style="color: #009900; font-weight: bold;">&#91;</span>rax<span style="color: #009900; font-weight: bold;">&#93;</span> <br />
<span style="color: #adadad; font-style: italic;">00000016</span> &nbsp;<span style="color: #00007f; font-weight: bold;">test</span> &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #00007f;">eax</span><span style="color: #339933;">,</span><span style="color: #00007f;">eax</span> <br />
<span style="color: #adadad; font-style: italic;">00000018</span> &nbsp;<span style="color: #00007f; font-weight: bold;">je</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0000ff;">000000000000001F</span> <br />
<span style="color: #adadad; font-style: italic;">0000001a</span> &nbsp;<span style="color: #00007f; font-weight: bold;">call</span> &nbsp; &nbsp; &nbsp; &nbsp;FFFFFFFFF9B384F0 <br />
<span style="color: #adadad; font-style: italic;">0000001f</span> &nbsp;<span style="color: #00007f; font-weight: bold;">nop</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Debug<span style="color: #339933;">.</span>EmptyFunction<span style="color: #009900; font-weight: bold;">&#40;</span><span style="color: #009900; font-weight: bold;">&#41;</span><span style="color: #666666; font-style: italic;">;</span><br />
<span style="color: #adadad; font-style: italic;">00000020</span> &nbsp;<span style="color: #00007f; font-weight: bold;">call</span> &nbsp; &nbsp; &nbsp; &nbsp;FFFFFFFFFFEC9A70 <br />
<span style="color: #adadad; font-style: italic;">00000025</span> &nbsp;<span style="color: #00007f; font-weight: bold;">nop</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Debug<span style="color: #339933;">.</span>DebugFunction<span style="color: #009900; font-weight: bold;">&#40;</span><span style="color: #009900; font-weight: bold;">&#41;</span><span style="color: #666666; font-style: italic;">;</span><br />
<span style="color: #adadad; font-style: italic;">00000026</span> &nbsp;<span style="color: #00007f; font-weight: bold;">call</span> &nbsp; &nbsp; &nbsp; &nbsp;FFFFFFFFFFEC9A78 <br />
<span style="color: #adadad; font-style: italic;">0000002b</span> &nbsp;<span style="color: #00007f; font-weight: bold;">nop</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900; font-weight: bold;">&#125;</span></div></div>
<p>Ok, everything works as expected in DEBUG mode, i.e. empty function and function with DEBUG conditional is not stripped. Let&#8217;s take a look the RELEASE mode assembly:</p>
<div class="codecolorer-container asm default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="asm codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; &nbsp; &nbsp; static void Main<span style="color: #009900; font-weight: bold;">&#40;</span>string<span style="color: #009900; font-weight: bold;">&#91;</span><span style="color: #009900; font-weight: bold;">&#93;</span> args<span style="color: #009900; font-weight: bold;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900; font-weight: bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Debug<span style="color: #339933;">.</span>EmptyFunction<span style="color: #009900; font-weight: bold;">&#40;</span><span style="color: #009900; font-weight: bold;">&#41;</span><span style="color: #666666; font-style: italic;">;</span><br />
<span style="color: #adadad; font-style: italic;">00000000</span> &nbsp;<span style="color: #00007f; font-weight: bold;">mov</span> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">qword</span> <span style="color: #000000; font-weight: bold;">ptr</span> <span style="color: #009900; font-weight: bold;">&#91;</span>rsp<span style="color: #339933;">+</span><span style="color: #0000ff;">8</span><span style="color: #009900; font-weight: bold;">&#93;</span><span style="color: #339933;">,</span>rcx <br />
<span style="color: #adadad; font-style: italic;">00000005</span> &nbsp;<span style="color: #00007f; font-weight: bold;">sub</span> &nbsp; &nbsp; &nbsp; &nbsp; rsp<span style="color: #339933;">,</span><span style="color: #0000ff;">28h</span> <br />
<span style="color: #adadad; font-style: italic;">00000009</span> &nbsp;<span style="color: #00007f; font-weight: bold;">nop</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
<span style="color: #adadad; font-style: italic;">0000000a</span> &nbsp;<span style="color: #00007f; font-weight: bold;">mov</span> &nbsp; &nbsp; &nbsp; &nbsp; rax<span style="color: #339933;">,</span><span style="color: #0000ff;">7FF001C1DF8h</span> <br />
<span style="color: #adadad; font-style: italic;">00000014</span> &nbsp;<span style="color: #00007f; font-weight: bold;">mov</span> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #00007f;">eax</span><span style="color: #339933;">,</span><span style="color: #000000; font-weight: bold;">dword</span> <span style="color: #000000; font-weight: bold;">ptr</span> <span style="color: #009900; font-weight: bold;">&#91;</span>rax<span style="color: #009900; font-weight: bold;">&#93;</span> <br />
<span style="color: #adadad; font-style: italic;">00000016</span> &nbsp;<span style="color: #00007f; font-weight: bold;">test</span> &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #00007f;">eax</span><span style="color: #339933;">,</span><span style="color: #00007f;">eax</span> <br />
<span style="color: #adadad; font-style: italic;">00000018</span> &nbsp;<span style="color: #00007f; font-weight: bold;">je</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0000ff;">000000000000001F</span> <br />
<span style="color: #adadad; font-style: italic;">0000001a</span> &nbsp;<span style="color: #00007f; font-weight: bold;">call</span> &nbsp; &nbsp; &nbsp; &nbsp;FFFFFFFFF9B28280 <br />
<span style="color: #adadad; font-style: italic;">0000001f</span> &nbsp;<span style="color: #00007f; font-weight: bold;">call</span> &nbsp; &nbsp; &nbsp; &nbsp;FFFFFFFFFFEC9800 &nbsp; <span style="color: #339933;">//</span> Why is <span style="color: #000000; font-weight: bold;">this</span> still here???<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Debug<span style="color: #339933;">.</span>DebugFunction<span style="color: #009900; font-weight: bold;">&#40;</span><span style="color: #009900; font-weight: bold;">&#41;</span><span style="color: #666666; font-style: italic;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900; font-weight: bold;">&#125;</span></div></div>
<p>To my surprise, C# compiler DOES NOT strip empty method!! Luckily, function with DEBUG conditional is stripped as expected. After further investigation, I found out that there are actually 2 compilers at work here: C# and JIT (Just-In-Time) compiler. C# compiler turns C# code to IL at compile time and JIT takes the IL and generates the native machine code at runtime. In addition, if you start your application from Visual Studio with the debugger attached (F5) then all the JIT optimizations will be disabled even if optimization is enabled.</p>
<p>In <a href="http://msdn.microsoft.com/en-us/library/ms973858.aspx#highperfmanagedapps_topic10">Writing High-Performance Managed Applications : A Primer</a>, it explains how we can view the optimized JIT version of the code. After stepping through the code, it&#8217;s relieving to know that actually JIT strips out the empty method.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.3dgametechnology.com/wp/c-empty-method-stripping/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>C# Integer to String Builder</title>
		<link>http://www.3dgametechnology.com/wp/c-integer-to-string-builder/</link>
		<comments>http://www.3dgametechnology.com/wp/c-integer-to-string-builder/#comments</comments>
		<pubDate>Mon, 17 Jan 2011 19:43:39 +0000</pubDate>
		<dc:creator>tep</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[c++]]></category>
		<category><![CDATA[game development]]></category>
		<category><![CDATA[xna]]></category>

		<guid isPermaLink="false">http://www.3dgametechnology.com/wp/?p=232</guid>
		<description><![CDATA[As many of you know, StringBuilder.Append(int) method creates a garbage. This is bad for XNA games that do this conversion every frame. In this article, I provide one implementation to convert int to string without creating garbage. I tried to be as efficient as possible; if you find better way to do this, please let [...]]]></description>
			<content:encoded><![CDATA[<p>As many of you know, StringBuilder.Append(int) method creates a garbage. This is bad for XNA games that do this conversion every frame. In this article, I provide one implementation to convert int to string without creating garbage. I tried to be as efficient as possible; if you find better way to do this, please let me know.</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:500px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">class</span> StringBuilderExtension<br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">char</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> charToInt <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> <span style="color: #6666cc; font-weight: bold;">char</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">&#123;</span> <span style="color: #666666;">'0'</span>, <span style="color: #666666;">'1'</span>, <span style="color: #666666;">'2'</span>, <span style="color: #666666;">'3'</span>, <span style="color: #666666;">'4'</span>, <span style="color: #666666;">'5'</span>, <span style="color: #666666;">'6'</span>, <span style="color: #666666;">'7'</span>, <span style="color: #666666;">'8'</span>, <span style="color: #666666;">'9'</span> <span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> Swap<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span> StringBuilder sb, <span style="color: #6666cc; font-weight: bold;">int</span> startIndex, <span style="color: #6666cc; font-weight: bold;">int</span> endIndex<span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// Swap the integers</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Debug<span style="color: #008000;">.</span><span style="color: #0000FF;">Assert</span><span style="color: #008000;">&#40;</span>endIndex <span style="color: #008000;">&gt;=</span> startIndex<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #6666cc; font-weight: bold;">int</span> count <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span>endIndex <span style="color: #008000;">-</span> startIndex <span style="color: #008000;">+</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">/</span> <span style="color: #FF0000;">2</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> i <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> i <span style="color: #008000;">&lt;</span> count<span style="color: #008000;">;</span> <span style="color: #008000;">++</span>i<span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #6666cc; font-weight: bold;">char</span> temp <span style="color: #008000;">=</span> sb<span style="color: #008000;">&#91;</span>startIndex <span style="color: #008000;">+</span> i<span style="color: #008000;">&#93;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sb<span style="color: #008000;">&#91;</span>startIndex <span style="color: #008000;">+</span> i<span style="color: #008000;">&#93;</span> <span style="color: #008000;">=</span> sb<span style="color: #008000;">&#91;</span>endIndex <span style="color: #008000;">-</span> i<span style="color: #008000;">&#93;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sb<span style="color: #008000;">&#91;</span>endIndex <span style="color: #008000;">-</span> i<span style="color: #008000;">&#93;</span> <span style="color: #008000;">=</span> temp<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> AppendNumber<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span> StringBuilder sb, <span style="color: #6666cc; font-weight: bold;">int</span> number<span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// Save the current length as starting index</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #6666cc; font-weight: bold;">int</span> startIndex <span style="color: #008000;">=</span> sb<span style="color: #008000;">.</span><span style="color: #0000FF;">Length</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// Handle negative</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #6666cc; font-weight: bold;">bool</span> isNegative<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #6666cc; font-weight: bold;">uint</span> unumber<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>number <span style="color: #008000;">&lt;</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; unumber <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">uint</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>number <span style="color: #008000;">==</span> <span style="color: #6666cc; font-weight: bold;">int</span><span style="color: #008000;">.</span><span style="color: #0000FF;">MinValue</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">?</span> number <span style="color: #008000;">:</span> <span style="color: #008000;">-</span>number<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; isNegative <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">else</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; unumber <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">uint</span><span style="color: #008000;">&#41;</span>number<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; isNegative <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// Convert </span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">do</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sb<span style="color: #008000;">.</span><span style="color: #0000FF;">Append</span><span style="color: #008000;">&#40;</span>charToInt<span style="color: #008000;">&#91;</span>unumber <span style="color: #008000;">%</span> <span style="color: #FF0000;">10</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; unumber <span style="color: #008000;">/=</span> <span style="color: #FF0000;">10</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span> <span style="color: #0600FF; font-weight: bold;">while</span> <span style="color: #008000;">&#40;</span>unumber <span style="color: #008000;">!=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>isNegative<span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sb<span style="color: #008000;">.</span><span style="color: #0000FF;">Append</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">'-'</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; sb<span style="color: #008000;">.</span><span style="color: #0000FF;">Swap</span><span style="color: #008000;">&#40;</span>startIndex, sb<span style="color: #008000;">.</span><span style="color: #0000FF;">Length</span> <span style="color: #008000;">-</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> AppendNumber<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span> StringBuilder sb, <span style="color: #6666cc; font-weight: bold;">uint</span> unumber<span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// Save the current length as starting index</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #6666cc; font-weight: bold;">int</span> startIndex <span style="color: #008000;">=</span> sb<span style="color: #008000;">.</span><span style="color: #0000FF;">Length</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// Convert </span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">do</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sb<span style="color: #008000;">.</span><span style="color: #0000FF;">Append</span><span style="color: #008000;">&#40;</span>charToInt<span style="color: #008000;">&#91;</span>unumber <span style="color: #008000;">%</span> <span style="color: #FF0000;">10</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; unumber <span style="color: #008000;">/=</span> <span style="color: #FF0000;">10</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span> <span style="color: #0600FF; font-weight: bold;">while</span> <span style="color: #008000;">&#40;</span>unumber <span style="color: #008000;">!=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; sb<span style="color: #008000;">.</span><span style="color: #0000FF;">Swap</span><span style="color: #008000;">&#40;</span>startIndex, sb<span style="color: #008000;">.</span><span style="color: #0000FF;">Length</span> <span style="color: #008000;">-</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
<span style="color: #008000;">&#125;</span></div></div>
]]></content:encoded>
			<wfw:commentRss>http://www.3dgametechnology.com/wp/c-integer-to-string-builder/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Packing Data Format</title>
		<link>http://www.3dgametechnology.com/wp/packing-data-format/</link>
		<comments>http://www.3dgametechnology.com/wp/packing-data-format/#comments</comments>
		<pubDate>Mon, 17 Jan 2011 19:36:36 +0000</pubDate>
		<dc:creator>tep</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[game development]]></category>
		<category><![CDATA[graphics]]></category>
		<category><![CDATA[shader]]></category>

		<guid isPermaLink="false">http://www.3dgametechnology.com/wp/?p=228</guid>
		<description><![CDATA[During shader development, packing/unpacking from one format to another format is very common. Sometimes, the purpose is to support older graphics card and occasionally, it is more efficient to do so. Below are growing list of packing/unpacking methods in shader. Packing Float to RGBA32 float4 packFloatToRGBA32&#40;float f&#41; &#123; &#160; &#160; const float4 pack = float4&#40;1, [...]]]></description>
			<content:encoded><![CDATA[<p>During shader development, packing/unpacking from one format to another format is very common. Sometimes, the purpose is to support older graphics card and occasionally, it is more efficient to do so. Below are growing list of packing/unpacking methods in shader.</p>
<h4>Packing Float to RGBA32</h4>
<div class="codecolorer-container cpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">float4 packFloatToRGBA32<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">float</span> f<span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">const</span> float4 pack <span style="color: #000080;">=</span> float4<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">1</span>, <span style="color: #0000dd;">256</span>, <span style="color: #0000dd;">65536</span>, <span style="color: #0000dd;">16777216</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">return</span> f <span style="color: #000040;">*</span> pack<span style="color: #008080;">;</span><br />
<span style="color: #008000;">&#125;</span></div></div>
<h4>Unpacking RGBA32 to Float</h4>
<div class="codecolorer-container cpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0000ff;">float</span> unpackRGBA32ToFloat<span style="color: #008000;">&#40;</span>float4 rgba<span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">const</span> float4 unpack <span style="color: #000080;">=</span> float4<span style="color: #008000;">&#40;</span><span style="color:#800080;">1.0f</span>, <span style="color:#800080;">1.0f</span><span style="color: #000040;">/</span><span style="color: #0000dd;">256</span>, <span style="color:#800080;">1.0f</span><span style="color: #000040;">/</span><span style="color: #0000dd;">65536</span>, <span style="color:#800080;">1.0f</span><span style="color: #000040;">/</span><span style="color: #0000dd;">16777216</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">return</span> dot<span style="color: #008000;">&#40;</span>rgba, unpack<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<span style="color: #008000;">&#125;</span></div></div>
]]></content:encoded>
			<wfw:commentRss>http://www.3dgametechnology.com/wp/packing-data-format/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>World, View and Projection Matrix Internals</title>
		<link>http://www.3dgametechnology.com/wp/world-view-and-projection-matrix-internals/</link>
		<comments>http://www.3dgametechnology.com/wp/world-view-and-projection-matrix-internals/#comments</comments>
		<pubDate>Mon, 17 Jan 2011 19:32:10 +0000</pubDate>
		<dc:creator>tep</dc:creator>
				<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[game development]]></category>
		<category><![CDATA[graphics]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[xna]]></category>

		<guid isPermaLink="false">http://www.3dgametechnology.com/wp/?p=215</guid>
		<description><![CDATA[This convention below is applicable to Direct3D and XNA matrices World Matrix Given a position and basis vectors right, up and look of an object, a world matrix can be formed by the following arrangement: View Matrix Given a position and basis vectors right, up and look of a viewer, a view matrix can be [...]]]></description>
			<content:encoded><![CDATA[<p>This convention below is applicable to Direct3D and XNA matrices</p>
<h4>World Matrix</h4>
<p>Given a <em>position </em>and basis vectors <em>right</em>, <em>up</em> and <em>look</em> of an <em>object</em>, a world matrix can be formed by the following arrangement:</p>
<p><a href="http://www.3dgametechnology.com/wp/wp-content/uploads/2011/01/world_matrix.png"><img class="alignnone size-medium wp-image-220" title="world_matrix" src="http://www.3dgametechnology.com/wp/wp-content/uploads/2011/01/world_matrix-300x116.png" alt="" width="300" height="116" /></a></p>
<h4>View Matrix</h4>
<p>Given a <em>position</em> and basis vectors <em>right</em>, <em>up</em> and <em>look</em> of a viewer, a view matrix can be formed by the following arrangement:</p>
<p><a href="http://www.3dgametechnology.com/wp/wp-content/uploads/2011/01/view_matrix.png"><img class="alignnone size-full wp-image-221" title="view_matrix" src="http://www.3dgametechnology.com/wp/wp-content/uploads/2011/01/view_matrix.png" alt="" width="430" height="103" /></a></p>
<h4>Projection Matrix</h4>
<p>Given field of view <em>FOV</em>, <em>aspect ratio</em>, near clip plane <em>Zn</em> and far clip plane <em>Zf</em>, a perspective projection matrix can be formed by the following arrangment:</p>
<p><a href="http://www.3dgametechnology.com/wp/wp-content/uploads/2011/01/projection_matrix.png"><img class="alignnone size-medium wp-image-222" title="projection_matrix" src="http://www.3dgametechnology.com/wp/wp-content/uploads/2011/01/projection_matrix-300x219.png" alt="" width="300" height="219" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.3dgametechnology.com/wp/world-view-and-projection-matrix-internals/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Median Filter Exploration</title>
		<link>http://www.3dgametechnology.com/wp/median-filter-exploration/</link>
		<comments>http://www.3dgametechnology.com/wp/median-filter-exploration/#comments</comments>
		<pubDate>Mon, 17 Jan 2011 07:08:23 +0000</pubDate>
		<dc:creator>tep</dc:creator>
				<category><![CDATA[Technology]]></category>
		<category><![CDATA[game development]]></category>
		<category><![CDATA[graphics]]></category>
		<category><![CDATA[math]]></category>

		<guid isPermaLink="false">http://www.3dgametechnology.com/wp/?p=195</guid>
		<description><![CDATA[Background One day, I was browsing through some concept arts and stopped at one stunning image. Basically, there&#8217;s &#8220;depth of field&#8221; on that image; however, it&#8217;s not the normal depth of field. Instead of blurred objects at far distance, you have &#8220;median filtered look&#8221; of objects at far distance. It led me to think: &#8220;hey, [...]]]></description>
			<content:encoded><![CDATA[<h4><strong>Background</strong></h4>
<p>One day, I was browsing through some concept arts and stopped at one stunning image. Basically, there&#8217;s &#8220;depth of field&#8221; on that image; however, it&#8217;s not the normal depth of field. Instead of blurred objects at far distance, you have &#8220;median filtered look&#8221; of objects at far distance. It led me to think: &#8220;hey, what about using median filter instead of gaussian filter?&#8221;</p>
<p>Implementing a trivial median filter has a big disadvantage that is it costs a lot more than gaussian filter. You need to sort the samples you take and look for the middle (median) value of that samples. Let say you take N samples, the fastest sorting algorithm is N log N (Of course, in this case I don&#8217;t include bucket/radix sort). Remember, we need to do this in GPU, so a complex algorithm like quick sort/merge sort/heap sort is out of question. You probably ended up with N*N sorting algorithm.</p>
<h4><strong>Pseudo Median Filter</strong></h4>
<p>As always, I googled to find an answer. I found this &#8220;Pseudo Median Filter&#8221; that&#8217;s faster than Median Filter and tries to mimic the original median filter effect. I found this from a forum in <a href="http://www.razyboard.com/system/morethread-cartoon-glsl-shader-pete_bernert-266904-2751605-0.html">http://www.razyboard.com/system/morethread-cartoon-glsl-shader-pete_bernert-266904-2751605-0.html</a>.</p>
<p>Basically, the algorithm is like this:</p>
<table>
<tbody>
<tr>
<td>c00</td>
<td>c01</td>
<td>c02</td>
</tr>
<tr>
<td>c10</td>
<td>c11</td>
<td>c12</td>
</tr>
<tr>
<td>c20</td>
<td>c21</td>
<td>c22</td>
</tr>
</tbody>
</table>
<p>Assuming the current pixel is c11, take the c11,c10,c12,c01,c21 colors and then re-calculate c11:</p>
<div class="codecolorer-container cpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">float3 mx <span style="color: #000080;">=</span> max<span style="color: #008000;">&#40;</span> max<span style="color: #008000;">&#40;</span>c10,c12<span style="color: #008000;">&#41;</span>, max<span style="color: #008000;">&#40;</span>c01,c21<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
float3 mn <span style="color: #000080;">=</span> min<span style="color: #008000;">&#40;</span> min<span style="color: #008000;">&#40;</span>c10,c12<span style="color: #008000;">&#41;</span>, min<span style="color: #008000;">&#40;</span>c01,c21<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
c11 <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span>c11<span style="color: #000040;">+</span>c10<span style="color: #000040;">+</span>c12<span style="color: #000040;">+</span>c01<span style="color: #000040;">+</span>c21<span style="color: #000040;">-</span>mx<span style="color: #000040;">-</span>mn<span style="color: #008000;">&#41;</span><span style="color: #000040;">/</span><span style="color:#800080;">3.0</span><span style="color: #008080;">;</span></div></div>
<p>That yields the following result:</p>
<table width="100%">
<tbody>
<tr>
<th>Pseudo Median Filter &#8211; Before</th>
<th>Pseudo Median Filter &#8211; After</th>
</tr>
<tr>
<td><img src="http://wiki.3dgametechnology.com/images/wiki/medianfiltertest/medianfiltertest_pseudo_before.png" alt="Psuedo Median Filter - Before" width="225" height="225" /></td>
<td><img src="http://wiki.3dgametechnology.com/images/wiki/medianfiltertest/medianfiltertest_pseudo_after.png" alt="Psuedo Median Filter - After" width="225" height="225" /></td>
</tr>
</tbody>
</table>
<p>To me, it doesn&#8217;t look good enough. First, it looks more like a blur than a median filter. What I&#8217;m looking for is the &#8220;abstraction&#8221; of image when I apply median filter. This algorithm is no good; let&#8217;s move on.</p>
<h4><strong>Fast, Small Radius GPU Median Filter</strong></h4>
<p>My next research is to implement the &#8220;real&#8221; median filter. However, I&#8217;m not going to code the median filter; I&#8217;ll just google it. I found this page: <a href="http://graphics.cs.williams.edu/papers/MedianShaderX6/">http://graphics.cs.williams.edu/papers/MedianShaderX6/</a>. I took their 5&#215;5 median filter code, and here&#8217;s the result and comparison with gaussian blur.</p>
<table>
<tbody>
<tr>
<th>Original</th>
<th>What I Want (Photoshop Median w/ Radius = 4)</th>
</tr>
<tr>
<td><img src="http://wiki.3dgametechnology.com/images/wiki/medianfiltertest/medianfiltertest_original.png" alt="Original" width="225" height="225" /></td>
<td><img src="http://wiki.3dgametechnology.com/images/wiki/medianfiltertest/medianfiltertest_whatiwant.png" alt="What I Want" width="225" height="225"" /></td>
</tr>
<tr>
<th>Median Filter</th>
<th>Gaussian Blur</th>
</tr>
<tr>
<td><img src="http://wiki.3dgametechnology.com/images/wiki/medianfiltertest/medianfiltertest_median.png" alt="Median Filter" width="225" height="225"" /></td>
<td><img src="http://wiki.3dgametechnology.com/images/wiki/medianfiltertest/medianfiltertest_blur.png" alt="Gaussian Blur" width="225" height="225" /></td>
</tr>
</tbody>
</table>
<p>And Yes! This is close to what I want. There are two defects:</p>
<ol type="1">
<li>It&#8217;s quite slow. I apply the filter 5 times.</li>
<li>I probably need bigger kernel size than just 5&#215;5 in order to get more abstraction</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://www.3dgametechnology.com/wp/median-filter-exploration/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Converting Quaternion to Euler Angle</title>
		<link>http://www.3dgametechnology.com/wp/converting-quaternion-to-euler-angle/</link>
		<comments>http://www.3dgametechnology.com/wp/converting-quaternion-to-euler-angle/#comments</comments>
		<pubDate>Mon, 17 Jan 2011 06:54:47 +0000</pubDate>
		<dc:creator>tep</dc:creator>
				<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[c++]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[xna]]></category>

		<guid isPermaLink="false">http://www.3dgametechnology.com/wp/?p=167</guid>
		<description><![CDATA[Overview Conceptually, there are many ways to represent rotation in 3D game programming, the most common being Quaternion, Euler Angle, Matrix and Axis Angle. A lot of resources can be found on the web on each representation. However, there are not so many &#8220;concrete&#8221; implementation on how to convert from quaternion to euler angle. This article will focus solely [...]]]></description>
			<content:encoded><![CDATA[<h4><strong>Overview</strong></h4>
<p>Conceptually, there are many ways to represent rotation in 3D game programming, the most common being <em>Quaternion</em>, <em>Euler Angle</em>, <em>Matrix</em> and <em>Axis Angle</em>. A lot of resources can be found on the web on each representation.</p>
<p>However, there are not so many &#8220;concrete&#8221; implementation on how to convert from quaternion to euler angle. This article will focus solely on this matter. It will discuss on what you need to know and give a working example code in XNA.</p>
<h4><strong>Convention</strong></h4>
<p>One problem in Euler angle is there are possibly more than one way to represent a rotation. Euler angle can be defined as three numbers (ex, ey, ez) whereby each number represent a rotation relative to its axis.</p>
<p>Problem arises when we need to construct a rotation matrix based on the Euler angle. We can construct a rotation matrix of an axis, i.e.:</p>
<p><img src="http://wiki.3dgametechnology.com/images/wiki/quattoeuler/xna_rotx_matrix.png" alt="XNA RotationX Matrix" width="218" height="87" /> <img src="http://wiki.3dgametechnology.com/images/wiki/quattoeuler/xna_roty_matrix.png" alt="XNA RotationY Matrix" width="218" height="87" /> <img src="http://wiki.3dgametechnology.com/images/wiki/quattoeuler/xna_rotz_matrix.png" alt="XNA RotationZ Matrix" width="218" height="87" /></p>
<h4><strong>Derivation</strong></h4>
<p>To create a single rotation matrix, we need to know the order of multiplication. In XNA, the order is Z-X-Y, so the rotation matrix will be:</p>
<p><img src="http://wiki.3dgametechnology.com/images/wiki/quattoeuler/xna_combinedrot_matrix_1.png" alt="image" width="149" height="26" /><br />
<img src="http://wiki.3dgametechnology.com/images/wiki/quattoeuler/xna_combinedrot_matrix_2.png" alt="image" width="100%" /><br />
<img src="http://wiki.3dgametechnology.com/images/wiki/quattoeuler/xna_combinedrot_matrix_3.png" alt="image" width="100%" /><br />
<img src="http://wiki.3dgametechnology.com/images/wiki/quattoeuler/xna_combinedrot_matrix_4.png" alt="image" width="100%" /></p>
<p>Your engine may adopt a different convention; thus it will lead to a different result.</p>
<p>From, the result above, we can get ex:</p>
<p><img src="http://wiki.3dgametechnology.com/images/wiki/quattoeuler/eulerx_from_matrix.png" alt="image" width="131" height="53" /></p>
<p>also ey:</p>
<p><img src="http://wiki.3dgametechnology.com/images/wiki/quattoeuler/eulery_from_matrix.png" alt="image" width="163" height="162" /></p>
<p>and finally ez:</p>
<p><img src="http://wiki.3dgametechnology.com/images/wiki/quattoeuler/eulerz_from_matrix.png" alt="image" /></p>
<p>Using .NET reflection, we can know how XNA derive matrix from quaternion Q=(qx,qy,qz,qw) and thus we have all the information we need:</p>
<p><img src="http://wiki.3dgametechnology.com/images/wiki/quattoeuler/matrix12_from_quat.png" alt="image" width="160" height="32" /><br />
<img src="http://wiki.3dgametechnology.com/images/wiki/quattoeuler/matrix22_from_quat.png" alt="image" width="160" height="31" /><br />
<img src="http://wiki.3dgametechnology.com/images/wiki/quattoeuler/matrix31_from_quat.png" alt="image" width="160" height="32" /><br />
<img src="http://wiki.3dgametechnology.com/images/wiki/quattoeuler/matrix32_from_quat.png" alt="image" width="160" height="31" /><br />
<img src="http://wiki.3dgametechnology.com/images/wiki/quattoeuler/matrix33_from_quat.png" alt="image" width="160" height="31" /></p>
<p>and finally, we can get the euler angle ex, ey, ez:</p>
<p><img src="http://wiki.3dgametechnology.com/images/wiki/quattoeuler/eulerx_from_quat.png" alt="image" width="303" height="33" /><br />
<img src="http://wiki.3dgametechnology.com/images/wiki/quattoeuler/eulery_from_quat.png" alt="image" width="313" height="67" /><br />
<img src="http://wiki.3dgametechnology.com/images/wiki/quattoeuler/eulerz_from_quat.png" alt="image" width="313" height="63" /></p>
<h4><strong>Special Case</strong></h4>
<p>There is a problem when ex=90 or ex=-90degree. Since cos(ex) will be 0, so M12 = M22 = M31 = M33 = 0 and yields ey and ez undefined. To handle this, let see how XNA converts Euler Angle to Quaternion:</p>
<p><img src="http://wiki.3dgametechnology.com/images/wiki/quattoeuler/quat_from_euler.png" alt="image" width="430" height="226" /></p>
<p>When ex=90:</p>
<p><img src="http://wiki.3dgametechnology.com/images/wiki/quattoeuler/quat_from_euler_ex90.png" alt="image" width="518" height="125" /></p>
<p>it yields</p>
<p><img src="http://wiki.3dgametechnology.com/images/wiki/quattoeuler/eyez_from_euler_ex90.png" alt="image" width="191" height="230" /></p>
<p>If we choose ez=0, then we get ey = 2 * atan2(qy, qw)</p>
<p>Similarly, when ex=-90:</p>
<p><img src="http://wiki.3dgametechnology.com/images/wiki/quattoeuler/quat_from_euler_ex-90.png" alt="image" width="512" height="122" /></p>
<p>it yields</p>
<p><img src="http://wiki.3dgametechnology.com/images/wiki/quattoeuler/eyez_from_euler_ex-90.png" alt="image" width="185" height="231" /></p>
<p>If we choose ez=0, then we get ey = 2 * atan2(qy, qw).</p>
<h4><strong>Implementation So Far</strong></h4>
<p>Based on the theory above, we can write a function to convert from Quaternion to Euler Angle. This method guarantees if you transform point P to P&#8217; by using quaternion Q, you can also apply rotation using euler angle E to transform P to P&#8217;.</p>
<p>(NOTE: This method does not guarantee that <a title="Create this page" href="http://wiki.3dgametechnology.com/EulerToQuaternion/edit">EulerToQuaternion</a>(E) = Q).</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> Vector3 QuaternionToEuler2<span style="color: #008000;">&#40;</span>Quaternion q<span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; Vector3 euler<span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #6666cc; font-weight: bold;">float</span> sqx <span style="color: #008000;">=</span> q<span style="color: #008000;">.</span><span style="color: #0000FF;">X</span> <span style="color: #008000;">*</span> q<span style="color: #008000;">.</span><span style="color: #0000FF;">X</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; <span style="color: #6666cc; font-weight: bold;">float</span> sqy <span style="color: #008000;">=</span> q<span style="color: #008000;">.</span><span style="color: #0000FF;">Y</span> <span style="color: #008000;">*</span> q<span style="color: #008000;">.</span><span style="color: #0000FF;">Y</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; <span style="color: #6666cc; font-weight: bold;">float</span> sqz <span style="color: #008000;">=</span> q<span style="color: #008000;">.</span><span style="color: #0000FF;">Z</span> <span style="color: #008000;">*</span> q<span style="color: #008000;">.</span><span style="color: #0000FF;">Z</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; <span style="color: #6666cc; font-weight: bold;">float</span> sqw <span style="color: #008000;">=</span> q<span style="color: #008000;">.</span><span style="color: #0000FF;">W</span> <span style="color: #008000;">*</span> q<span style="color: #008000;">.</span><span style="color: #0000FF;">W</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #6666cc; font-weight: bold;">float</span> unit <span style="color: #008000;">=</span> sqx <span style="color: #008000;">+</span> sqy <span style="color: #008000;">+</span> sqz <span style="color: #008000;">+</span> sqw<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; <span style="color: #6666cc; font-weight: bold;">float</span> test <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span>q<span style="color: #008000;">.</span><span style="color: #0000FF;">X</span> <span style="color: #008000;">*</span> q<span style="color: #008000;">.</span><span style="color: #0000FF;">W</span> <span style="color: #008000;">-</span> q<span style="color: #008000;">.</span><span style="color: #0000FF;">Y</span> <span style="color: #008000;">*</span> q<span style="color: #008000;">.</span><span style="color: #0000FF;">Z</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// Handle singularity</span><br />
&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>test <span style="color: #008000;">&gt;</span> 0<span style="color: #008000;">.</span>4999999f <span style="color: #008000;">*</span> unit<span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; euler<span style="color: #008000;">.</span><span style="color: #0000FF;">X</span> <span style="color: #008000;">=</span> MathHelper<span style="color: #008000;">.</span><span style="color: #0000FF;">PiOver2</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; euler<span style="color: #008000;">.</span><span style="color: #0000FF;">Y</span> <span style="color: #008000;">=</span> 2<span style="color: #008000;">.</span>0f <span style="color: #008000;">*</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">float</span><span style="color: #008000;">&#41;</span><span style="color: #000000;">System</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Math</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Atan2</span><span style="color: #008000;">&#40;</span>q<span style="color: #008000;">.</span><span style="color: #0000FF;">Y</span>, q<span style="color: #008000;">.</span><span style="color: #0000FF;">W</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; euler<span style="color: #008000;">.</span><span style="color: #0000FF;">Z</span> <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">else</span> <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>test <span style="color: #008000;">&lt;</span> <span style="color: #008000;">-</span>0<span style="color: #008000;">.</span>4999999f <span style="color: #008000;">*</span> unit<span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; euler<span style="color: #008000;">.</span><span style="color: #0000FF;">X</span> <span style="color: #008000;">=</span> <span style="color: #008000;">-</span>MathHelper<span style="color: #008000;">.</span><span style="color: #0000FF;">PiOver2</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; euler<span style="color: #008000;">.</span><span style="color: #0000FF;">Y</span> <span style="color: #008000;">=</span> 2<span style="color: #008000;">.</span>0f <span style="color: #008000;">*</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">float</span><span style="color: #008000;">&#41;</span><span style="color: #000000;">System</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Math</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Atan2</span><span style="color: #008000;">&#40;</span>q<span style="color: #008000;">.</span><span style="color: #0000FF;">Y</span>, q<span style="color: #008000;">.</span><span style="color: #0000FF;">W</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; euler<span style="color: #008000;">.</span><span style="color: #0000FF;">Z</span> <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">else</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #6666cc; font-weight: bold;">float</span> ey_Y <span style="color: #008000;">=</span> <span style="color: #FF0000;">2</span> <span style="color: #008000;">*</span> <span style="color: #008000;">&#40;</span>q<span style="color: #008000;">.</span><span style="color: #0000FF;">X</span> <span style="color: #008000;">*</span> q<span style="color: #008000;">.</span><span style="color: #0000FF;">Z</span> <span style="color: #008000;">+</span> q<span style="color: #008000;">.</span><span style="color: #0000FF;">Y</span> <span style="color: #008000;">*</span> q<span style="color: #008000;">.</span><span style="color: #0000FF;">W</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #6666cc; font-weight: bold;">float</span> ey_X <span style="color: #008000;">=</span> <span style="color: #FF0000;">1</span> <span style="color: #008000;">-</span> <span style="color: #FF0000;">2</span> <span style="color: #008000;">*</span> <span style="color: #008000;">&#40;</span>sqy <span style="color: #008000;">+</span> sqx<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #6666cc; font-weight: bold;">float</span> ez_Y <span style="color: #008000;">=</span> <span style="color: #FF0000;">2</span> <span style="color: #008000;">*</span> <span style="color: #008000;">&#40;</span>q<span style="color: #008000;">.</span><span style="color: #0000FF;">X</span> <span style="color: #008000;">*</span> q<span style="color: #008000;">.</span><span style="color: #0000FF;">Y</span> <span style="color: #008000;">+</span> q<span style="color: #008000;">.</span><span style="color: #0000FF;">Z</span> <span style="color: #008000;">*</span> q<span style="color: #008000;">.</span><span style="color: #0000FF;">W</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #6666cc; font-weight: bold;">float</span> ez_X <span style="color: #008000;">=</span> <span style="color: #FF0000;">1</span> <span style="color: #008000;">-</span> <span style="color: #FF0000;">2</span> <span style="color: #008000;">*</span> <span style="color: #008000;">&#40;</span>sqx <span style="color: #008000;">+</span> sqz<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; euler<span style="color: #008000;">.</span><span style="color: #0000FF;">X</span> <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">float</span><span style="color: #008000;">&#41;</span><span style="color: #000000;">System</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Math</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Asin</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">2</span> <span style="color: #008000;">*</span> test<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; euler<span style="color: #008000;">.</span><span style="color: #0000FF;">Y</span> <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">float</span><span style="color: #008000;">&#41;</span><span style="color: #000000;">System</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Math</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Atan2</span><span style="color: #008000;">&#40;</span>ey_Y, ey_X<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; euler<span style="color: #008000;">.</span><span style="color: #0000FF;">Z</span> <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">float</span><span style="color: #008000;">&#41;</span><span style="color: #000000;">System</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Math</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Atan2</span><span style="color: #008000;">&#40;</span>ez_Y, ez_X<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// Convert to degrees</span><br />
&nbsp; &nbsp; euler<span style="color: #008000;">.</span><span style="color: #0000FF;">X</span> <span style="color: #008000;">=</span> MathHelper<span style="color: #008000;">.</span><span style="color: #0000FF;">ToDegrees</span><span style="color: #008000;">&#40;</span>euler<span style="color: #008000;">.</span><span style="color: #0000FF;">X</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; euler<span style="color: #008000;">.</span><span style="color: #0000FF;">Y</span> <span style="color: #008000;">=</span> MathHelper<span style="color: #008000;">.</span><span style="color: #0000FF;">ToDegrees</span><span style="color: #008000;">&#40;</span>euler<span style="color: #008000;">.</span><span style="color: #0000FF;">Y</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; euler<span style="color: #008000;">.</span><span style="color: #0000FF;">Z</span> <span style="color: #008000;">=</span> MathHelper<span style="color: #008000;">.</span><span style="color: #0000FF;">ToDegrees</span><span style="color: #008000;">&#40;</span>euler<span style="color: #008000;">.</span><span style="color: #0000FF;">Z</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">return</span> euler<span style="color: #008000;">;</span><br />
<span style="color: #008000;">&#125;</span></div></div>
<h4><strong>Antipodal Quaternion</strong></h4>
<p>As I have mentioned above, the Euler angle E obtained by the method above does not guarantee that converting it back to quaternion will yield Q. Using the method above, it may yield its <em>antipodal</em>, i.e. -Q. It is important to note that Antipodal quaternions, Q and −Q, represent the same rotation.</p>
<p>Thus we need some way to ensure property <a title="Create this page" href="http://wiki.3dgametechnology.com/EulerToQuaternion/edit">EulerToQuaternion</a>( <a title="Create this page" href="http://wiki.3dgametechnology.com/QuaternionToEuler/edit">QuaternionToEuler</a>(Q) ) = Q. Unfortunately, I don&#8217;t know a good way to do this. I found a workaround by trying to add/subtract 360 from the Euler angle. If you find a better way, I would be happy to update this posting.</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:500px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> Vector3 QuaternionToEuler<span style="color: #008000;">&#40;</span>Quaternion q<span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; Vector3 euler<span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #6666cc; font-weight: bold;">float</span> sqx <span style="color: #008000;">=</span> q<span style="color: #008000;">.</span><span style="color: #0000FF;">X</span> <span style="color: #008000;">*</span> q<span style="color: #008000;">.</span><span style="color: #0000FF;">X</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; <span style="color: #6666cc; font-weight: bold;">float</span> sqy <span style="color: #008000;">=</span> q<span style="color: #008000;">.</span><span style="color: #0000FF;">Y</span> <span style="color: #008000;">*</span> q<span style="color: #008000;">.</span><span style="color: #0000FF;">Y</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; <span style="color: #6666cc; font-weight: bold;">float</span> sqz <span style="color: #008000;">=</span> q<span style="color: #008000;">.</span><span style="color: #0000FF;">Z</span> <span style="color: #008000;">*</span> q<span style="color: #008000;">.</span><span style="color: #0000FF;">Z</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; <span style="color: #6666cc; font-weight: bold;">float</span> sqw <span style="color: #008000;">=</span> q<span style="color: #008000;">.</span><span style="color: #0000FF;">W</span> <span style="color: #008000;">*</span> q<span style="color: #008000;">.</span><span style="color: #0000FF;">W</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #6666cc; font-weight: bold;">float</span> unit <span style="color: #008000;">=</span> sqx <span style="color: #008000;">+</span> sqy <span style="color: #008000;">+</span> sqz <span style="color: #008000;">+</span> sqw<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; <span style="color: #6666cc; font-weight: bold;">float</span> test <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span>q<span style="color: #008000;">.</span><span style="color: #0000FF;">X</span> <span style="color: #008000;">*</span> q<span style="color: #008000;">.</span><span style="color: #0000FF;">W</span> <span style="color: #008000;">-</span> q<span style="color: #008000;">.</span><span style="color: #0000FF;">Y</span> <span style="color: #008000;">*</span> q<span style="color: #008000;">.</span><span style="color: #0000FF;">Z</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// Handle singularity</span><br />
&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>test <span style="color: #008000;">&gt;</span> 0<span style="color: #008000;">.</span>4999999f <span style="color: #008000;">*</span> unit<span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; euler<span style="color: #008000;">.</span><span style="color: #0000FF;">X</span> <span style="color: #008000;">=</span> MathHelper<span style="color: #008000;">.</span><span style="color: #0000FF;">PiOver2</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; euler<span style="color: #008000;">.</span><span style="color: #0000FF;">Y</span> <span style="color: #008000;">=</span> 2<span style="color: #008000;">.</span>0f <span style="color: #008000;">*</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">float</span><span style="color: #008000;">&#41;</span><span style="color: #000000;">System</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Math</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Atan2</span><span style="color: #008000;">&#40;</span>q<span style="color: #008000;">.</span><span style="color: #0000FF;">Y</span>, q<span style="color: #008000;">.</span><span style="color: #0000FF;">W</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; euler<span style="color: #008000;">.</span><span style="color: #0000FF;">Z</span> <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">else</span> <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>test <span style="color: #008000;">&lt;</span> <span style="color: #008000;">-</span>0<span style="color: #008000;">.</span>4999999f <span style="color: #008000;">*</span> unit<span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; euler<span style="color: #008000;">.</span><span style="color: #0000FF;">X</span> <span style="color: #008000;">=</span> <span style="color: #008000;">-</span>MathHelper<span style="color: #008000;">.</span><span style="color: #0000FF;">PiOver2</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; euler<span style="color: #008000;">.</span><span style="color: #0000FF;">Y</span> <span style="color: #008000;">=</span> 2<span style="color: #008000;">.</span>0f <span style="color: #008000;">*</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">float</span><span style="color: #008000;">&#41;</span><span style="color: #000000;">System</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Math</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Atan2</span><span style="color: #008000;">&#40;</span>q<span style="color: #008000;">.</span><span style="color: #0000FF;">Y</span>, q<span style="color: #008000;">.</span><span style="color: #0000FF;">W</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; euler<span style="color: #008000;">.</span><span style="color: #0000FF;">Z</span> <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">else</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #6666cc; font-weight: bold;">float</span> ey_Y <span style="color: #008000;">=</span> <span style="color: #FF0000;">2</span> <span style="color: #008000;">*</span> <span style="color: #008000;">&#40;</span>q<span style="color: #008000;">.</span><span style="color: #0000FF;">X</span> <span style="color: #008000;">*</span> q<span style="color: #008000;">.</span><span style="color: #0000FF;">Z</span> <span style="color: #008000;">+</span> q<span style="color: #008000;">.</span><span style="color: #0000FF;">Y</span> <span style="color: #008000;">*</span> q<span style="color: #008000;">.</span><span style="color: #0000FF;">W</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #6666cc; font-weight: bold;">float</span> ey_X <span style="color: #008000;">=</span> <span style="color: #FF0000;">1</span> <span style="color: #008000;">-</span> <span style="color: #FF0000;">2</span> <span style="color: #008000;">*</span> <span style="color: #008000;">&#40;</span>sqy <span style="color: #008000;">+</span> sqx<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #6666cc; font-weight: bold;">float</span> ez_Y <span style="color: #008000;">=</span> <span style="color: #FF0000;">2</span> <span style="color: #008000;">*</span> <span style="color: #008000;">&#40;</span>q<span style="color: #008000;">.</span><span style="color: #0000FF;">X</span> <span style="color: #008000;">*</span> q<span style="color: #008000;">.</span><span style="color: #0000FF;">Y</span> <span style="color: #008000;">+</span> q<span style="color: #008000;">.</span><span style="color: #0000FF;">Z</span> <span style="color: #008000;">*</span> q<span style="color: #008000;">.</span><span style="color: #0000FF;">W</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #6666cc; font-weight: bold;">float</span> ez_X <span style="color: #008000;">=</span> <span style="color: #FF0000;">1</span> <span style="color: #008000;">-</span> <span style="color: #FF0000;">2</span> <span style="color: #008000;">*</span> <span style="color: #008000;">&#40;</span>sqx <span style="color: #008000;">+</span> sqz<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; euler<span style="color: #008000;">.</span><span style="color: #0000FF;">X</span> <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">float</span><span style="color: #008000;">&#41;</span><span style="color: #000000;">System</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Math</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Asin</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">2</span> <span style="color: #008000;">*</span> test<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; euler<span style="color: #008000;">.</span><span style="color: #0000FF;">Y</span> <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">float</span><span style="color: #008000;">&#41;</span><span style="color: #000000;">System</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Math</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Atan2</span><span style="color: #008000;">&#40;</span>ey_Y, ey_X<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; euler<span style="color: #008000;">.</span><span style="color: #0000FF;">Z</span> <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">float</span><span style="color: #008000;">&#41;</span><span style="color: #000000;">System</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Math</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Atan2</span><span style="color: #008000;">&#40;</span>ez_Y, ez_X<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
<br />
&nbsp; &nbsp; Quaternion qNeg <span style="color: #008000;">=</span> <span style="color: #008000;">-</span>q<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; Quaternion qTest<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; Quaternion<span style="color: #008000;">.</span><span style="color: #0000FF;">CreateFromYawPitchRoll</span><span style="color: #008000;">&#40;</span>euler<span style="color: #008000;">.</span><span style="color: #0000FF;">Y</span>, euler<span style="color: #008000;">.</span><span style="color: #0000FF;">X</span>, euler<span style="color: #008000;">.</span><span style="color: #0000FF;">Z</span>, <span style="color: #0600FF; font-weight: bold;">out</span> qTest<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>ExMath<span style="color: #008000;">.</span><span style="color: #0000FF;">Equals</span><span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">ref</span> qNeg, <span style="color: #0600FF; font-weight: bold;">ref</span> qTest<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>euler<span style="color: #008000;">.</span><span style="color: #0000FF;">X</span> <span style="color: #008000;">&lt;</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; euler<span style="color: #008000;">.</span><span style="color: #0000FF;">X</span> <span style="color: #008000;">+=</span> TwoPi<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">else</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; euler<span style="color: #008000;">.</span><span style="color: #0000FF;">X</span> <span style="color: #008000;">-=</span> TwoPi<span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; Quaternion<span style="color: #008000;">.</span><span style="color: #0000FF;">CreateFromYawPitchRoll</span><span style="color: #008000;">&#40;</span>euler<span style="color: #008000;">.</span><span style="color: #0000FF;">Y</span>, euler<span style="color: #008000;">.</span><span style="color: #0000FF;">X</span>, euler<span style="color: #008000;">.</span><span style="color: #0000FF;">Z</span>, <span style="color: #0600FF; font-weight: bold;">out</span> qTest<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>ExMath<span style="color: #008000;">.</span><span style="color: #0000FF;">Equals</span><span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">ref</span> qNeg, <span style="color: #0600FF; font-weight: bold;">ref</span> qTest<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>euler<span style="color: #008000;">.</span><span style="color: #0000FF;">Y</span> <span style="color: #008000;">&lt;</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; euler<span style="color: #008000;">.</span><span style="color: #0000FF;">Y</span> <span style="color: #008000;">+=</span> TwoPi<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">else</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; euler<span style="color: #008000;">.</span><span style="color: #0000FF;">Y</span> <span style="color: #008000;">-=</span> TwoPi<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// Convert to degrees</span><br />
&nbsp; &nbsp; euler<span style="color: #008000;">.</span><span style="color: #0000FF;">X</span> <span style="color: #008000;">=</span> MathHelper<span style="color: #008000;">.</span><span style="color: #0000FF;">ToDegrees</span><span style="color: #008000;">&#40;</span>euler<span style="color: #008000;">.</span><span style="color: #0000FF;">X</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; euler<span style="color: #008000;">.</span><span style="color: #0000FF;">Y</span> <span style="color: #008000;">=</span> MathHelper<span style="color: #008000;">.</span><span style="color: #0000FF;">ToDegrees</span><span style="color: #008000;">&#40;</span>euler<span style="color: #008000;">.</span><span style="color: #0000FF;">Y</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; euler<span style="color: #008000;">.</span><span style="color: #0000FF;">Z</span> <span style="color: #008000;">=</span> MathHelper<span style="color: #008000;">.</span><span style="color: #0000FF;">ToDegrees</span><span style="color: #008000;">&#40;</span>euler<span style="color: #008000;">.</span><span style="color: #0000FF;">Z</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">return</span> euler<span style="color: #008000;">;</span><br />
<span style="color: #008000;">&#125;</span></div></div>
<h4><strong>Final Note</strong></h4>
<p>As a final note, the code above uses <a title="Create this page" href="http://wiki.3dgametechnology.com/ExMath/edit">ExMath</a>.Equals(ref Quaternion a, ref Quaternion b) which compares quaternion components with some epsilon:</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">bool</span> Equals<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">ref</span> Quaternion a, <span style="color: #0600FF; font-weight: bold;">ref</span> Quaternion b<span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #000000;">System</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Math</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Abs</span><span style="color: #008000;">&#40;</span>a<span style="color: #008000;">.</span><span style="color: #0000FF;">X</span> <span style="color: #008000;">-</span> b<span style="color: #008000;">.</span><span style="color: #0000FF;">X</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&lt;</span> ExMath<span style="color: #008000;">.</span><span style="color: #0000FF;">Epsilon</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&amp;&amp;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#40;</span><span style="color: #000000;">System</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Math</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Abs</span><span style="color: #008000;">&#40;</span>a<span style="color: #008000;">.</span><span style="color: #0000FF;">Y</span> <span style="color: #008000;">-</span> b<span style="color: #008000;">.</span><span style="color: #0000FF;">Y</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&lt;</span> ExMath<span style="color: #008000;">.</span><span style="color: #0000FF;">Epsilon</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&amp;&amp;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#40;</span><span style="color: #000000;">System</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Math</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Abs</span><span style="color: #008000;">&#40;</span>a<span style="color: #008000;">.</span><span style="color: #0000FF;">Z</span> <span style="color: #008000;">-</span> b<span style="color: #008000;">.</span><span style="color: #0000FF;">Z</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&lt;</span> ExMath<span style="color: #008000;">.</span><span style="color: #0000FF;">Epsilon</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&amp;&amp;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#40;</span><span style="color: #000000;">System</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Math</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Abs</span><span style="color: #008000;">&#40;</span>a<span style="color: #008000;">.</span><span style="color: #0000FF;">W</span> <span style="color: #008000;">-</span> b<span style="color: #008000;">.</span><span style="color: #0000FF;">W</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&lt;</span> ExMath<span style="color: #008000;">.</span><span style="color: #0000FF;">Epsilon</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<span style="color: #008000;">&#125;</span></div></div>
]]></content:encoded>
			<wfw:commentRss>http://www.3dgametechnology.com/wp/converting-quaternion-to-euler-angle/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>The Gaussian Equation</title>
		<link>http://www.3dgametechnology.com/wp/the-gaussian-equation/</link>
		<comments>http://www.3dgametechnology.com/wp/the-gaussian-equation/#comments</comments>
		<pubDate>Sun, 16 Jan 2011 04:15:10 +0000</pubDate>
		<dc:creator>tep</dc:creator>
				<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[math]]></category>

		<guid isPermaLink="false">http://www.3dgametechnology.com/wp/?p=157</guid>
		<description><![CDATA[The Equation I believe you all have seen the famous 1D gaussian equation: Although for me it&#8217;s still a little bit cryptic. Basically, the function accepts 2 parameters: t is your data input sigma is the user controllable parameter To understand this, pick a number for sigma and plot the graph, whereby t is the [...]]]></description>
			<content:encoded><![CDATA[<h4>The Equation</h4>
<p>I believe you all have seen the famous 1D gaussian equation:</p>
<p><img src="http://wiki.3dgametechnology.com/images/wiki/gaussianeq/gaussianeq_eq.png" alt="1D Gaussian Equation" /></p>
<p>Although for me it&#8217;s still a little bit cryptic. Basically, the function accepts 2 parameters:</p>
<ol type="1">
<li>t is your data input</li>
<li>sigma is the user controllable parameter</li>
</ol>
<p>To understand this, pick a number for sigma and plot the graph, whereby t is the x-axis, and the function result is the y-axis.</p>
<p><img src="http://wiki.3dgametechnology.com/images/wiki/gaussianeq/gaussianeq_graph.png" alt="1D Gaussian Equation Graph" width="100%" /></p>
]]></content:encoded>
			<wfw:commentRss>http://www.3dgametechnology.com/wp/the-gaussian-equation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Hello TepNetwork!</title>
		<link>http://www.3dgametechnology.com/wp/hello-tepnetwork/</link>
		<comments>http://www.3dgametechnology.com/wp/hello-tepnetwork/#comments</comments>
		<pubDate>Sat, 15 Jan 2011 08:26:50 +0000</pubDate>
		<dc:creator>tep</dc:creator>
				<category><![CDATA[TepNetwork]]></category>

		<guid isPermaLink="false">http://www.3dgametechnology.com/wp/?p=55</guid>
		<description><![CDATA[Happy new year 2011! I know it&#8217;s a little bit too late but it&#8217;s better late than never right? In this new year, I am unifying all (most) of my sites into TepNetwork. I am unifying all these websites: http://www.3dgametechnology.com http://wiki.3dgametechnology.com http://gsteph.blogspot.com Although, I like to keep my &#8220;portfolio website&#8221; separate. So, I left http://stephanus.3dgametechnology.com [...]]]></description>
			<content:encoded><![CDATA[<p>Happy new year 2011! I know it&#8217;s a little bit too late but it&#8217;s better late than never right?</p>
<p>In this new year, I am unifying all (most) of my sites into TepNetwork. I am unifying all these websites:</p>
<ol>
<li>http://www.3dgametechnology.com</li>
<li>http://wiki.3dgametechnology.com</li>
<li>http://gsteph.blogspot.com</li>
</ol>
<p>Although, I like to keep my &#8220;portfolio website&#8221; separate. So, I left <a href="http://stephanus.3dgametechnology.com">http://stephanus.3dgametechnology.com</a> as is. I will not be updating those website anymore. Unfortunately, I might lost some content from http://www.3dgametechnology.com because of some stupid mistake I made.</p>
<p>Anyway, this will be my main website, I hope you enjoy this site!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.3dgametechnology.com/wp/hello-tepnetwork/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

