<?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>AnKi 3D Engine Dev Blog</title>
	<atom:link href="http://anki3d.org/feed/" rel="self" type="application/rss+xml" />
	<link>http://anki3d.org</link>
	<description>The home of AnKi 3D Engine developers</description>
	<lastBuildDate>Tue, 10 Apr 2012 09:16:08 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>C++11: Variadic templates. Part I</title>
		<link>http://anki3d.org/cxx11-variadic-templates-part1/</link>
		<comments>http://anki3d.org/cxx11-variadic-templates-part1/#comments</comments>
		<pubDate>Fri, 16 Dec 2011 16:59:30 +0000</pubDate>
		<dc:creator>Panos Christopoulos Charitos</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[C++11]]></category>
		<category><![CDATA[variadic]]></category>

		<guid isPermaLink="false">http://anki3d.org/?p=211</guid>
		<description><![CDATA[UPDATE: Adding a new concept The new C++ standard, namely C++11, is here at last; offering many additions to the language&#8217;s core as well as in the companion library, the STL. Without doubt it will change the way we think &#8230;<p class="read-more"><a href="http://anki3d.org/cxx11-variadic-templates-part1/">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<p><strong>UPDATE: Adding a new concept</strong></p>
<p>The new C++ standard, namely C++11, is here at last; offering many additions to the language&#8217;s core as well as in the companion library, the STL. Without doubt it will change the way we think and work but nobody can predict if it is for better or worst. The experimentation period is nearly over, only a handful of features missing from GCC and clang and the C++ engineers will have to learn and master the new tricks in both fronts (core and STL). For those familiar with boost library the second front should be an easy transaction to the new STL, for the first though we need tutorials and lots of them. This little article is one tutorial that extends the already published ones.</p>
<p>One of the new cool features is the variadic templates, simply put, templates with variable number of template parameters. To put it into context this is a variadic template:</p>
<pre class="brush: cpp; title: Code; notranslate">template&lt;typename... Types&gt;
struct Foo{};</pre>
<p><span id="more-211"></span><br />
In this article we will not present the basic usage of variadic templates. You can find great publications online (the wikipedia&#8217;s to name one) with an introduction and first steps, its not my intention to repeat the same in here. The present article&#8217;s purpose is to expand the wikipedia&#8217;s one by presenting a few cool things that now are doable. So, before proceeding please take a look at this first <a title="en.wikipedia.org/wiki/Variadic_template" href="http://en.wikipedia.org/wiki/Variadic_template" target="_blank" onclick="pageTracker._trackPageview('/outgoing/en.wikipedia.org/wiki/Variadic_template?referer=');">http://en.wikipedia.org/wiki/Variadic_template</a>.</p>
<h1>Concept 1: Creating a visitor class</h1>
<p>I&#8217;m not sure how the C++ scientists name that technique but because its pretty useful for all the examples I will call it &#8220;three way&#8221; technique. This technique has the word &#8220;three&#8221; because it utilizes three things. A forward declaration, a declaration and a partial template specialization. It&#8217;s purpose is to use the variadic parameters to do repetitive things.</p>
<p>This example explains an easy way to implement the visitor pattern. To construct a visitor we use the &#8220;three way&#8221;:</p>
<pre class="brush: cpp; title: Code; notranslate">// Forward declaration (1st part)
template&lt;typename... Types&gt;
struct ConstVisitor;

// Declaration (2nd part)
template&lt;typename First, typename... Types&gt;
struct ConstVisitor&lt;First, Types...&gt;: ConstVisitor&lt;Types...&gt;
{
	using ConstVisitor&lt;Types...&gt;::visit;
	virtual void visit(const First&amp;) = 0;
};

// Specialized for one (3rd part)
template&lt;typename First&gt;
struct ConstVisitor&lt;First&gt;
{
	virtual void visit(const First&amp;) = 0;
};</pre>
<p>The declaration of ConstVisitor is recursive (second part) as the class inherits itself. This recursiveness needs to be stopped so we need a specialization for the single template parameter (third part). The forward declaration is there to satisfy the compiler. The above code defines the pure virtual visitor() methods for a number of types. Now lets see the class Base and its derived MyDouble and MyInt that utilize the visitor pattern.</p>
<pre class="brush: cpp; title: Code; notranslate">// Forwards
struct MyDouble;
struct MyInt;

/// Base class
struct Base
{
	typedef ConstVisitor&lt;MyDouble, MyInt&gt; MyConstVisitor;

	virtual void accept(MyConstVisitor&amp;) = 0;
};

/// Double
struct MyDouble: Base
{
	double x;

	void accept(Base::MyConstVisitor&amp; v)
	{
		v.visit(*this);
	}
};

/// Int
struct MyInt: Base
{
	int x;

	void accept(Base::MyConstVisitor&amp; v)
	{
		v.visit(*this);
	}
};

/// A visitor that does something
struct PrintVisitor: Base::MyConstVisitor
{
	void visit(const MyDouble&amp; x)
	{
		std::cout &lt;&lt; &quot;I am a MyDouble: &quot; &lt;&lt; x.x &lt;&lt; std::endl;
	}

	void visit(const MyInt&amp; x)
	{
		std::cout &lt;&lt; &quot;I am a MyInt: &quot; &lt;&lt; x.x &lt;&lt; std::endl;
	}
};</pre>
<p>The Base::MyConstVisitor is abstract and it contains two pure virtual methods, one for const MyDouble&#038; and the second for const MyInt&#038;. We used the Base::MyConstVisitor to create the PrintVisitor, a visitor that actually does something. The main may look like this:</p>
<pre class="brush: cpp; title: Code; notranslate">int main(int, char**)
{
	PrintVisitor vis;
	MyDouble md;
	md.x = 123.456;
	MyInt mi;
	mi.x = 987;

	Base* b = &amp;md;
	b-&gt;accept(vis);
	b = &amp;mi;
	b-&gt;accept(vis);
}</pre>
<p>And the output:</p>
<pre class="brush: plain; title: Code; notranslate">I am a MyDouble: 123.456
I am a MyInt: 987</pre>
<p>The &#8220;variandic template way&#8221; to define a visitor does not do anything super smart or super useful. Its only a good way to save us from typing and nothing more.</p>
<h1>Concept 2: Getting the nth type of the variadic template class</h1>
<p>Having a variadic template class we want to access the type of the nth template parameter. Once again using the &#8220;tree way&#8221; technique this is doable.</p>
<pre class="brush: cpp; title: Code; notranslate">template&lt;typename... Types&gt;
struct Foo
{
	// Forward declaration
	template&lt;int id, typename... Types_&gt;
	struct X;

	// Declaration
	template&lt;int id, typename First, typename... Types_&gt;
	struct X&lt;id, First, Types_...&gt;: X&lt;id - 1, Types_...&gt;
	{
	};

	// Specialized
	template&lt;typename First, typename... Types_&gt;
	struct X&lt;0, First, Types_...&gt;
	{
		typedef First DataType;
	};

	// Preferable (template aliases) way but we have to wait for GCC 4.7
	// template&lt;int id&gt;
	// using DataType = X&lt;id, Types...&gt;::DataType;

	template&lt;int id&gt;
	struct Y
	{
		typedef typename X&lt;id, Types...&gt;::DataType DataType;
	};
};

int main(int, char**)
{
	typedef Foo&lt;double, int, std::string&gt; MyFoo;
	MyFoo::Y&lt;0&gt;::DataType a(1.23);
	MyFoo::Y&lt;1&gt;::DataType b(123);
	MyFoo::Y&lt;2&gt;::DataType c(&quot;Hello&quot;);

	std::cout &lt;&lt; a &lt;&lt; &quot; &quot; &lt;&lt; b &lt;&lt; &quot; &quot; &lt;&lt; c &lt;&lt; std::endl;
}</pre>
<p>The Foo::X uses the &#8220;three way&#8221; to define a type, it takes as template argument an integer indicating the position of the template parameter. As you can see this id is getting decreased starting from N and it stops in the specialized Foo::X where the type is defined. In the main() the Foo::Y::DataType defines the correct type, 0 for the first (double), 1 for the second (int) and 2 for the third (std::string).</p>
<p>The output of main is:</p>
<pre class="brush: plain; title: Code; notranslate">1.23 123 Hello</pre>
<h1>Concept 3: Getting the position of type in variadic template</h1>
<p>Rationale: Imagine we have a variadic template class <i>Foo</i> that accepts a number of types and the typedef <i>MyFoo</i> that accepts tree common types. For example:</p>
<pre class="brush: cpp; title: Code; notranslate">template&lt;typename... Types&gt;
struct Foo {};

typedef Foo&lt;int, float, std::string&gt; MyFoo;</pre>
<p>What we want is to get the type ID of one of the types at compile time. For example the:</p>
<pre class="brush: cpp; title: Code; notranslate">std::cout &lt;&lt; MyFoo::getTypeId&lt;int&gt;() &lt;&lt; &quot; &quot; &lt;&lt;
	MyFoo::getTypeId&lt;float&gt;() &lt;&lt; &quot; &quot; &lt;&lt;
	MyFoo::getTypeId&lt;std::string&gt;() &lt;&lt;
	std::endl;</pre>
<p>We want to output this:</p>
<pre class="brush: plain; title: Code; notranslate">0 1 2</pre>
<p>To do that we will use the &#8220;three way&#8221; once more like this:</p>
<pre class="brush: cpp; title: Code; notranslate">// Forward
template&lt;typename Type, typename... Types&gt;
struct GetVisitableId;

// Declaration
template&lt;typename Type, typename First, typename... Types&gt;
struct GetVisitableId&lt;Type, First, Types...&gt;: GetVisitableId&lt;Type, Types...&gt;
{};

// Specialized
template&lt;typename Type, typename... Types&gt;
struct GetVisitableId&lt;Type, Type, Types...&gt;
{
	static const int ID = sizeof...(Types);
};</pre>
<p>This smart structure given a Type and a list of types defines a const integer indicating the Type&#8217;s position from the back of the list. The integer is named ID.</p>
<p>The last step is to add the getTypeId() in the Foo:</p>
<pre class="brush: cpp; title: Code; notranslate">template&lt;typename... Types&gt;
struct Foo
{
	/// Using the GetVisitableId get the id of the @a T
	template&lt;typename T&gt;
	static constexpr int getTypeId()
	{
		return sizeof...(Types) - GetVisitableId&lt;T, Types...&gt;::ID - 1;
	}
};</pre>
<p>Once again with the recursive &#8220;tree way&#8221; technique we are able to do magic.</p>
<h1>Bottom line</h1>
<p>After reading this you will be ready for the second part where it implements a variant-like structure. A concept that is complex and not that easy to understand. All the above concepts are needed to create a very fast and abstract class that behaves in almost the same way as the boost::variant.</p>
]]></content:encoded>
			<wfw:commentRss>http://anki3d.org/cxx11-variadic-templates-part1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>C++ Class: Accessing contents but not the container</title>
		<link>http://anki3d.org/cpp-class-accessin-contents-container/</link>
		<comments>http://anki3d.org/cpp-class-accessin-contents-container/#comments</comments>
		<pubDate>Fri, 16 Sep 2011 11:02:52 +0000</pubDate>
		<dc:creator>Panos Christopoulos Charitos</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[boost]]></category>
		<category><![CDATA[C++]]></category>

		<guid isPermaLink="false">http://anki3d.org/?p=195</guid>
		<description><![CDATA[When constructing a C++ class a good practice is to never have any member variables in public scope and this includes containers. In this little article we will discuss how to have a C++ container as a private member and &#8230;<p class="read-more"><a href="http://anki3d.org/cpp-class-accessin-contents-container/">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<p>When constructing a C++ class a good practice is to never have any member variables in public scope and this includes containers. In this little article we will discuss how to have a C++ container as a private member and be able to expose its contents without exposing the container itself.</p>
<p><span id="more-195"></span><br />
To put it more simply imagine that we have a container that it lies in the private scope of some class (aka a private member). We want to expose the container&#8217;s data in a read/write manner but we dont want to allow access to the container itself. For the user of this class, we grant him access to the nth element of this container but we don&#8217;t allow him to push_back or from the container.</p>
<pre class="brush: cpp; title: Code; notranslate">class Foo
{
	public:
		typedef std::vector Container;

		Container&amp; getStrings()
		{
			return strs;
		}

		const Container&amp; getStrings() const
		{
			return strs;
		}

	private:
		Container strs;
};

...
Foo foo;
...
foo.getStrings()[0] = &quot;Hello&quot;; // OK!
foo.getStrings().push_back(&quot;World&quot;); // Not Acceptable</pre>
<p>The example above is a bad example of how to expose a container. It doesn&#8217;t feet our needs simply because we give access to the container itself.</p>
<p>After describing what we want to do and how <strong>not</strong> to do it lets see the correct way. Once again we will use the boost library and more especially the iterator_range module.</p>
<pre class="brush: cpp; title: Code; notranslate">#include &lt;boost/range/iterator_range.hpp&gt;

class Foo
{
	public:
		typedef std::vector Container;
		typedef boost::iterator_range&lt;Container::iterator&gt; MutableRange;
		typedef boost::iterator_range&lt;Container::const_iterator&gt; ConstRange;

		MutableRange getStrings()
		{
			return MutableRange(strs.begin(), strs.end());
		}

		ConstRange getStrings() const
		{
			return ConstRange(strs.begin(), strs.end());
		}

	private:
		Container strs;
};

...
Foo foo;
...
// Iterate, OK!
BOOST_FOREACH(std::string&amp; one, foo.getStrings())
{
	std::cout &lt;&lt; one &lt;&lt; std::endl;
	one += &quot;...&quot;;
}

foo.getStrings()[0] = &quot;Hello&quot;; // OK!
foo.getStrings().push_back(&quot;World&quot;); // OK! Error compiler</pre>
<p>In the above example we managed to do what we wanted. We can access the contents of Foo::strs vector, we can alter them, we can iterate and more importantly we cannot alter the container at all.</p>
<p>At this point you may ask about the overhead of range. Apparently its very lightweight and equal to two iterator copies.</p>
<p>Special thanks to K.K. for creating the original proof of concept.</p>
]]></content:encoded>
			<wfw:commentRss>http://anki3d.org/cpp-class-accessin-contents-container/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Multithreading: Parallel uniform jobs</title>
		<link>http://anki3d.org/multithreading-parallel-uniform-jobs/</link>
		<comments>http://anki3d.org/multithreading-parallel-uniform-jobs/#comments</comments>
		<pubDate>Thu, 11 Aug 2011 09:03:40 +0000</pubDate>
		<dc:creator>Panos Christopoulos Charitos</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[AnKi]]></category>
		<category><![CDATA[boost]]></category>
		<category><![CDATA[C++]]></category>

		<guid isPermaLink="false">http://anki3d.org/?p=164</guid>
		<description><![CDATA[Multithreading is a concept that exist for many decades in computer science, nevertheless, only in recent years it become a trend in game development with the arrival of multi-core CPUs in our home PCs. In this small article we will &#8230;<p class="read-more"><a href="http://anki3d.org/multithreading-parallel-uniform-jobs/">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<p>Multithreading is a concept that exist for many decades in computer science, nevertheless, only in recent years it become a trend in game development with the arrival of multi-core CPUs in our home PCs. In this small article we will discuss how AnKi utilizes the power of multiple processors. Also the source code of an example can be found at the end of the article.</p>
<p>The concept of multiple threads offers many advantages in applications that execute many uniform jobs at the same time, for example a webserver needs to serve multiple hosts at the same time and without a host waiting for a previous request to finish. A game application though used to have a very standard and linear flow. For example, we first update the AI, then the physics, then we update the world, we do visibility determination and lastly we render, then we repeat the same again. Sometimes its difficult to execute some of these distinctive steps in parallel because the data of the previous step will be used by the next. I bet high class development studios have found ways to blend these steps but in AnKi we use a more simple approach. We use multiple threads to run uniform jobs in parallel.</p>
<p>One good example to illustrate how AnKi uses the power of multiple threads is the visibility determination algorithm. One step of visibility determination is the test for every renderable scene node against the camera&#8217;s view frustum. If we have N nodes to test and M threads we can roughly assign for testing the first N/M nodes to the first thread, the next N/M nodes to the next thread etc.</p>
<p><span id="more-164"></span><br />
AnKi features a job manager that executes N jobs in parallel in N different threads. One thing to note is that the threads are being created only once (upon the manager&#8217;s initialization) and not for every parallel run. This gives an obvious performance advantage (over OpenMP for example) because creating and destroying threads is time consuming and not efficient at all. The job manager works from the main thread like this:</p>
<pre class="brush: plain; title: Code; notranslate">Assign job to first thread
Assign job to second thread
...
Assign job to N thread
Wait for all threads to finish</pre>
<p>The implementation of the job manager is pretty simple. Underneath it utilizes the boost threading library (as always) and it uses:</p>
<ul>
<li>N condition variables</li>
<li>N mutexes</li>
<li>N threads</li>
<li>One barrier</li>
</ul>
<p>If you are not familiar with these concepts please refer to a good textbook about parallel programming.</p>
<p>The condition variables are being used for thread waiting. When the thread is not assigned to do a job it waits using a condition variable. Every condition variable comes with a mutex and that&#8217;s the reason we need N mutexes. The reason of using N threads is pretty obvious but the usage of the barrier is not. The barrier is being used for thread synchronization. What we synchronize is the N threads along with the main thread.</p>
<p>According to the boost documentation:</p>
<blockquote><p>When a barrier is created, it is initialized with a thread count N. The first N-1 calls to wait() will all cause their threads to be blocked. The Nth call to wait() will allow all of the waiting threads, including the Nth thread, to be placed in a ready state. The Nth call will also &#8220;reset&#8221; the barrier such that, if an additional N+1th call is made to wait(), it will be as though this were the first call to wait(); in other words, the N+1th to 2N-1th calls to wait() will cause their threads to be blocked, and the 2Nth call to wait() will allow all of the waiting threads, including the 2Nth thread, to be placed in a ready state and reset the barrier. This functionality allows the same set of N threads to re-use a barrier object to synchronize their execution at multiple points during their execution.</p></blockquote>
<p>We actually initialize the barrier with N+1, the number of the N threads plus the main thread.</p>
<p>When we say that we assign a job to the job manager what we actually mean is that we pass to the thread a pointer to a function to execute along with a set of arguments. So lets see how we use the job manager in AnKi&#8217;s view frustum test:</p>
<pre class="brush: cpp; title: Code; notranslate">VisJobParameters jobParameters;
jobParameters.cam = &amp;cam;
jobParameters.skipShadowless = skipShadowless;
jobParameters.visibilityInfo = &amp;storage;
jobParameters.scene = &amp;scene;
jobParameters.msRenderableNodesMtx = &amp;msRenderableNodesMtx;
jobParameters.bsRenderableNodesMtx = &amp;bsRenderableNodesMtx;

for(uint i = 0;
	i &lt; parallel::ManagerSingleton::getInstance().getThreadsNum();
	i++)
{
	parallel::ManagerSingleton::getInstance().assignNewJob(i,
		getRenderableNodesJobCallback, jobParameters);
}

parallel::ManagerSingleton::getInstance().waitForAllJobsToFinish();</pre>
<p>The code for waitForAllJobsToFinish() is the simple:</p>
<pre class="brush: cpp; title: Code; notranslate">barrier-&gt;wait();</pre>
<h1>Getting the source</h1>
<p>You can download the job manager and an example bellow. The build instructions and usage are inside the readme file.</p>
<p><a href="http://anki3d.org/wp-content/uploads/2011/08/parallel.zip">Download the example archive</a></p>
]]></content:encoded>
			<wfw:commentRss>http://anki3d.org/multithreading-parallel-uniform-jobs/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>STL auto_ptr: A bad idea</title>
		<link>http://anki3d.org/stl-auto_ptr-a-bad-idea/</link>
		<comments>http://anki3d.org/stl-auto_ptr-a-bad-idea/#comments</comments>
		<pubDate>Tue, 14 Jun 2011 10:33:47 +0000</pubDate>
		<dc:creator>Panos Christopoulos Charitos</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[boost]]></category>
		<category><![CDATA[C++]]></category>

		<guid isPermaLink="false">http://anki3d.org/?p=146</guid>
		<description><![CDATA[C++ Standard Template Library (STL) offers a smart pointer called auto_ptr. auto_ptr is a template class that acts as a wrapper for a single dynamically allocated class instance. Its purpose and its usefulness is to deallocate the memory when this &#8230;<p class="read-more"><a href="http://anki3d.org/stl-auto_ptr-a-bad-idea/">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<p>C++ Standard Template Library (STL) offers a smart pointer called auto_ptr. auto_ptr is a template class that acts as a wrapper for a single dynamically allocated class instance. Its purpose and its usefulness is to deallocate the memory when this smart pointer gets out of scope. Its a way to do automatic garbage collection in C++ by saving us from the extra code writing. One extra reason to use auto_ptr is that it makes the code less error prone simply because it doesn&#8217;t rely on the programmer to do the memory dealocation.</p>
<p>In this short article we will discuss a case where auto_ptr fails to do what its supposed to do&#8230;.</p>
<p><span id="more-146"></span></p>
<p>How the auto_ptr is being used:</p>
<pre class="brush: cpp; title: Code; notranslate">// Begin of block
{
	MyClass* newInstance = new MyClass;
	auto_ptr ptr(newInstance);
}
// End of block</pre>
<p>At the end of the block the auto_ptr calls its destructor and checks if it holds a non null pointer. In our case its not null so it deallocates the newInstance.</p>
<p>Now that we roughly know what auto_ptr let us see why its a bad idea to use it at least in GCC. For our test case we have four files:</p>
<p>&nbsp;</p>
<p><strong>ClassA.h</strong></p>
<pre class="brush: cpp; title: Code; notranslate">#ifndef CLASS_A_H
#define CLASS_A_H

#include &lt;memory&gt;

class ClassB; // Forward declaration of ClassB

class ClassA
{
	public:
		ClassA();
		// Implicit destructor:
		//~ClassA() {}

	private:
		std::auto_ptr&lt;ClassB&gt; bPtr;
};

#endif</pre>
<p>In the above declaration we dont include the ClassB.h at all, the compiler wont complain at all because the forward declaration is sufficient for pointers and bPtr is essentially a pointer.</p>
<p>&nbsp;</p>
<p><strong>ClassA.cpp</strong></p>
<pre class="brush: cpp; title: Code; notranslate">#include &quot;ClassA.h&quot;
#include &quot;ClassB.h&quot;

ClassA::ClassA():
	bPtr(new ClassB)
{}</pre>
<p>&nbsp;</p>
<p><strong>ClassB.h</strong></p>
<pre class="brush: cpp; title: Code; notranslate">#ifndef CLASS_B_H
#define CLASS_B_H

#include &lt;iostream&gt;

class ClassB
{
	public:
		ClassB() {std::cout &lt;&lt; &quot;ClassB()&quot; &lt;&lt; std::endl;}
		~ClassB() {std::cout &lt;&lt; &quot;~ClassB()&quot; &lt;&lt; std::endl;}

	private:
		int x;
};

#endif</pre>
<p>&nbsp;</p>
<p><strong>Main.cpp</strong></p>
<pre class="brush: cpp; title: Code; notranslate">#include &quot;ClassA.h&quot;
//#include &quot;ClassB.h&quot;

int main(int, char**)
{
	{
		ClassA a;
	}

	return 0;
}</pre>
<p>The output of the above test case is:</p>
<pre class="brush: plain; title: Code; notranslate">ClassB</pre>
<p>If we uncomment the second line (#include &#8220;ClassB.h&#8221;) the output will become:</p>
<pre class="brush: plain; title: Code; notranslate">ClassB
~ClassB</pre>
<p>In the first case the destructor of ClassB was not called at all! The &#8220;a&#8221; instance is deleted but the ClassA::bPtr destructor is never called. Is this an error or what? Actually in Main.cpp there is no mention of the ClassB destructor at all so a simple dealocation happens without calling the destructor. As you can see this is a VERY VERY bad situation where auto_ptr fails to do what its supposed to do. GCC could and should post a warning but even with -Wall and -Wextra enabled it failed to do so.</p>
<p>Boost library once again shows its brilliance and avoids these kind of logical errors with compile time assertions. In our test case we can use a boost::scoped_ptr to ensure that the ~ClassB destructor will be called. A simple replace of auto_ptr will not do the trick as we have to add a destructor in ClassA.cpp or remove the ClassB forward declaration from ClassA.h.</p>
]]></content:encoded>
			<wfw:commentRss>http://anki3d.org/stl-auto_ptr-a-bad-idea/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Screen Space Ambient Occlusion</title>
		<link>http://anki3d.org/screen-space-ambient-occlusion/</link>
		<comments>http://anki3d.org/screen-space-ambient-occlusion/#comments</comments>
		<pubDate>Mon, 18 Apr 2011 21:13:08 +0000</pubDate>
		<dc:creator>Panos Christopoulos Charitos</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[AnKi]]></category>
		<category><![CDATA[GLSL]]></category>
		<category><![CDATA[OpenGL]]></category>
		<category><![CDATA[SSAO]]></category>

		<guid isPermaLink="false">http://anki3d.org/?p=118</guid>
		<description><![CDATA[&#8220;Ambient occlusion is a shading method used in 3D computer graphics which helps add realism to local reflection models by taking into account attenuation of light due to occlusion&#8221; [Wikipedia.org] The proper way to do ambient occlusion is very expensive &#8230;<p class="read-more"><a href="http://anki3d.org/screen-space-ambient-occlusion/">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<p>&#8220;Ambient occlusion is a shading method used in 3D computer graphics which helps add realism to local reflection models by taking into account attenuation of light due to occlusion&#8221; [Wikipedia.org]</p>
<p>The proper way to do ambient occlusion is very expensive for today&#8217;s hardware and especially without the use of a ray-tracing renderer. For that reason a few new techniques developed that tried to produce the same result using simpler and faster algorithms. One of these approaches is the Screen Space Ambient Occlusion (aka SSAO) that makes the calculations of ambient occlusion in 2D space just like a normal 2D filter. This article will not go deep into what SSAO is, there are many great readings around the web that cover the theory behind SSAO, in this article we will jump into the implementation that AnKi uses.</p>
<p>There are quite a few techniques to produce SSAO, a top level way to group them is from the resources they need to produce the SSAO:</p>
<ul>
<li>Some use just the depth buffer (Amnesia, Aliens VS Predator)</li>
<li>others use the depth buffer and the normal buffer (Crysis, <a href="http://www.gamerendering.com/2009/01/14/ssao/" target="_blank" onclick="pageTracker._trackPageview('/outgoing/www.gamerendering.com/2009/01/14/ssao/?referer=');">gamerendering article</a>)</li>
<li>and others use the pixel position and the normal buffer (<a href="http://www.gamedev.net/page/resources/_/reference/programming/140/lighting-and-shading/a-simple-and-practical-approach-to-ssao-r2753" target="_blank" onclick="pageTracker._trackPageview('/outgoing/www.gamedev.net/page/resources/_/reference/programming/140/lighting-and-shading/a-simple-and-practical-approach-to-ssao-r2753?referer=');">gamedev article</a>)</li>
</ul>
<p>AnKi used to implement the second technique for quite some time. The results were good but apparently not that good, so for the past couple of weeks I&#8217;ve tried to implement the third technique by reading <a href="http://www.gamedev.net/page/resources/_/reference/programming/140/lighting-and-shading/a-simple-and-practical-approach-to-ssao-r2753" target="_blank" onclick="pageTracker._trackPageview('/outgoing/www.gamedev.net/page/resources/_/reference/programming/140/lighting-and-shading/a-simple-and-practical-approach-to-ssao-r2753?referer=');">this</a> great article from gamedev. The present article extends the gamedev one by adding a few optimization tips and by presenting the code in GLSL.</p>
<p>Bellow you can see the old and the new SSAO, rendered at the half of the original rendering size with two blurring passes.</p>
<div id="attachment_135" class="wp-caption alignnone" style="width: 310px"><a href="http://anki3d.org/wp-content/uploads/2011/04/ssao-old.jpg"><img class="size-medium wp-image-135" title="ssao-old" src="http://anki3d.org/wp-content/uploads/2011/04/ssao-old-300x168.jpg" alt="" width="300" height="168" /></a><p class="wp-caption-text">Old implementation of SSAO</p></div>
<div id="attachment_136" class="wp-caption alignnone" style="width: 310px"><a href="http://anki3d.org/wp-content/uploads/2011/04/ssao-new.jpg"><img class="size-medium wp-image-136" title="ssao-new" src="http://anki3d.org/wp-content/uploads/2011/04/ssao-new-300x168.jpg" alt="" width="300" height="168" /></a><p class="wp-caption-text">New implementation of SSAO</p></div>
<div id="attachment_139" class="wp-caption alignnone" style="width: 310px"><a href="http://anki3d.org/wp-content/uploads/2011/04/screenshot.jpg"><img class="size-medium wp-image-139" title="anki-full" src="http://anki3d.org/wp-content/uploads/2011/04/screenshot-300x168.jpg" alt="" width="300" height="168" /></a><p class="wp-caption-text">The whole scene with the new SSAO</p></div>
<p><span id="more-118"></span></p>
<p>In order to produce the SSAO factor we need practically tree variables for every fragment, the first is the position of the fragment in view or world space (view space in our case), the normal of that fragment and a random vector that we obtain using a noise texture. The fact that AnKi uses a deferred shading renderer gives us the normals of the scene in a texture. The gamedev article suggests that we need to have the view space positions stored in a texture but in AnKi we don&#8217;t do that for any reason. Its very expensive to store the position in a texture and for that reason we use a few techniques to obtain the position from the depth buffer.</p>
<p>To obtain the fragment position in view space using the fragments depth we do:</p>
<pre class="brush: cpp; title: Code; notranslate">vec3 getPosition(in vec2 uv)
{
	float depth = texture2D(depthTexture, uv).r;

	vec3 fragPosVspace;
	fragPosVspace.z = -planes.y / (planes.x + depth);

	fragPosVspace.xy = (((uv) * 2.0) - 1.0) * limitsOfNearPlane;

	float sc = -fragPosVspace.z / zNear;
	fragPosVspace.xy *= sc;

	return fragPosVspace;
}</pre>
<p>The variables:</p>
<p><strong>uv</strong>: The texture coordinates to read from</p>
<p><strong>depthTexture</strong>: The texture that stores the depth</p>
<p><strong>zNear</strong>: Camera&#8217;s near plane</p>
<p><strong>zFar</strong>: Camera&#8217;s far plane</p>
<p><strong>planes</strong>: This is an optimization that we use to calculate the fragment&#8217;s z. The original and unoptimized code that gives the z is given by:</p>
<pre class="brush: cpp; title: Code; notranslate">fragPosVspace.z = -zNear / (zFar - (depth * (zFar - zNear))) * zFar;</pre>
<p>if we do a few calculations we can isolate two expressions that can be pre-calculated in the CPU and then passed as uniforms in the shader. The planes 2D vector is (C++):</p>
<pre class="brush: cpp; title: Code; notranslate">planes.x() = zFar / (zNear - zFar);
planes.y() = (zFar * zNear) / (zNear -zFar);</pre>
<p><strong>limitsOfNearPlane</strong>: This is a 2D vector that keeps the limits of the right and top eye vector (C++):</p>
<pre class="brush: cpp; title: Code; notranslate">limitsOfNearPlane.y() = zNear * tan(0.5 * fovY);
limitsOfNearPlane.x() = limitsOfNearPlane.y() * (fovX / fovY);</pre>
<p>fovX is the horizontal angle (imagine the cam positioned in the default OGL pos). Note that fovX &gt; fovY in the classic resolutions where the width &gt; height</p>
<p>As may have already noticed by now the getPosition is used to calculate the position using a perspective camera.</p>
<p>The SSAO vertex shader:</p>
<pre class="brush: cpp; title: Code; notranslate">layout(location = 0) in vec2 position;

out vec2 vTexCoords;

layout(location = 0) in vec2 position;

out vec2 vTexCoords;

void main()
{
	vec2 vertPos = position;
	vTexCoords = vertPos;
	vec2 vertPosNdc = vertPos * 2.0 - 1.0;
	gl_Position = vec4(vertPosNdc, 0.0, 1.0);
}</pre>
<p>The <strong>position</strong> is the vertex coordinates of the quad, the coordinates are: {1.0, 1.0}, {0.0, 1.0}, {0.0, 0.0}, {1.0, 0.0}. The values are pretty easy to digest and they help to calculate the texture coordinates. With this way we don&#8217;t pass a separate vertex attribute for the texture coordinates.</p>
<p>Fragment shader:</p>
<pre class="brush: cpp; title: Code; notranslate">/// @name Uniforms
/// @{
uniform vec2 planes; ///&lt; for the calculation of frag pos in view space
uniform vec2 limitsOfNearPlane; ///&lt; for the calculation of frag pos in view space
uniform float zNear; ///&lt; for the calculation of frag pos in view space
uniform sampler2D msDepthFai; ///&lt; for the calculation of frag pos in view space

uniform sampler2D noiseMap; /// Used in getRandom
uniform float noiseMapSize = 100.0; /// Used in getRandom
uniform vec2 screenSize; /// Used in getRandom

uniform sampler2D msNormalFai; /// Used in getNormal
/// @}

/// @name Varyings
/// @{
in vec2 vTexCoords;
/// @}

/// @name Output
/// @{
layout(location = 0) out float fColor;
/// @}

/// @name Consts
/// @{
uniform float SAMPLE_RAD = 0.1;  /// Used in main
uniform float SCALE = 1.0; /// Used in doAmbientOcclusion
uniform float INTENSITY = 3.0; /// Used in doAmbientOcclusion
uniform float BIAS = 0.00; /// Used in doAmbientOcclusion
/// @}

/// globals: msNormalFai
vec3 getNormal(in vec2 uv)
{
	return unpackNormal(texture2D(msNormalFai, uv).rg);
}

/// globals: noiseMap, screenSize, noiseMapSize
vec2 getRandom(in vec2 uv)
{
	return normalize(texture2D(noiseMap, screenSize * uv / noiseMapSize).xy * 2.0 - 1.0);
}

/// Get frag position in view space
/// globals: msDepthFai, planes, zNear, limitsOfNearPlane
vec3 getPosition(in vec2 uv)
{
	float depth = texture2D(msDepthFai, uv).r;

	vec3 fragPosVspace;
	fragPosVspace.z = -planes.y / (planes.x + depth);

	fragPosVspace.xy = (((uv) * 2.0) - 1.0) * limitsOfNearPlane;

	float sc = -fragPosVspace.z / zNear;
	fragPosVspace.xy *= sc;

	return fragPosVspace;
}

/// Calculate the ambient occlusion factor
float doAmbientOcclusion(in vec2 tcoord, in vec2 uv, in vec3 original, in vec3 cnorm)
{
	vec3 newp = getPosition(tcoord + uv);
	vec3 diff = newp - original;
	vec3 v = normalize(diff);
	float d = length(diff) /* * SCALE*/;

	float ret = max(0.0, dot(cnorm, v) /* - BIAS*/) * (INTENSITY / (1.0 + d));
	return ret;
}

void main(void)
{
	const vec2 KERNEL[16] = vec2[](vec2(0.53812504, 0.18565957), vec2(0.13790712, 0.24864247), vec2(0.33715037, 0.56794053), vec2(-0.6999805, -0.04511441), vec2(0.06896307, -0.15983082), vec2(0.056099437, 0.006954967), vec2(-0.014653638, 0.14027752), vec2(0.010019933, -0.1924225), vec2(-0.35775623, -0.5301969), vec2(-0.3169221, 0.106360726), vec2(0.010350345, -0.58698344), vec2(-0.08972908, -0.49408212), vec2(0.7119986, -0.0154690035), vec2(-0.053382345, 0.059675813), vec2(0.035267662, -0.063188605), vec2(-0.47761092, 0.2847911));

	vec3 p = getPosition(vTexCoords);
	vec3 n = getNormal(vTexCoords);
	vec2 rand = getRandom(vTexCoords);

	fColor = 0.0;

	const int ITERATIONS = 16;
	for(int j = 0; j &lt; ITERATIONS; ++j)
	{
		vec2 coord = reflect(KERNEL[j], rand) * SAMPLE_RAD;
		fColor += doAmbientOcclusion(vTexCoords, coord, p, n);
	}

	fColor = 1.0 - fColor / ITERATIONS;
}</pre>
<ul>
<li>The <strong>noiseMapSize</strong> is the size of the noise map. The width and height of the noise map is the same</li>
<li>The <strong>screenSize</strong> is the width and height of the window. This value is not that relevant and it doesn&#8217;t affect the result that much</li>
<li>The <strong>KERNEL</strong> is a number of 2D coordinates that form a circle around (0, 0) and we use them for sampling </li>
<li>The <strong>unpackNormal</strong> is a function that unpacks the normal from two 16bit floats, dont be alerted by this function its internal</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://anki3d.org/screen-space-ambient-occlusion/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The perfect logging system for C++ apps</title>
		<link>http://anki3d.org/the-perfect-logging-system-for-cpp-apps/</link>
		<comments>http://anki3d.org/the-perfect-logging-system-for-cpp-apps/#comments</comments>
		<pubDate>Mon, 03 Jan 2011 17:42:32 +0000</pubDate>
		<dc:creator>Panos Christopoulos Charitos</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[boost]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[logging]]></category>

		<guid isPermaLink="false">http://anki3d.org/?p=94</guid>
		<description><![CDATA[Ok, the title sounds a little bit pompous and promises allot, many fellow programmers will disagree with the described methodology and others may find it useful. After many changes and rewrites of the AnKi logging system we&#8217;ve gained some experience &#8230;<p class="read-more"><a href="http://anki3d.org/the-perfect-logging-system-for-cpp-apps/">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<p>Ok, the title sounds a little bit pompous and promises allot, many fellow programmers will disagree with the described methodology and others may find it useful. After many changes and rewrites of the AnKi logging system we&#8217;ve gained some experience of how a proper logger should be designed and work. The purpose of this article is to pass this knowledge to anyone who is interested.</p>
<p><span id="more-94"></span></p>
<p>First of all lets see what logging is. Logger is a subsystem responsible of keeping a record of the various engine events, to inform about problems and eventually help us (the programmers) diagnose them.</p>
<p>A simple logging method is to use the printf function for writing in the standard output:</p>
<pre class="brush: cpp; title: Code; notranslate">
// Report an error
printf(&quot;Error: %s\n&quot;, &quot;Something went wrong&quot;);
// ...or inform the user of a certain event:
printf(&quot;Renderer initialized successfully!\n&quot;);
</pre>
<p>The simple printf is more than inadequate. A proper logger should have the following attributes:</p>
<ul>
<li>Data abstraction</li>
<li>Minimal dependences</li>
<li>Only one instance &amp; self sustained</li>
<li>Multiple outputs <strong>(VERY VERY INTERESTING)</strong></li>
<li>Error tolerant</li>
</ul>
<h3>Data abstraction</h3>
<p>The logger should be able to take as input various data types and be able to process them without much trouble from our side. The std::cout is a perfect example. cout can output C strings, STL strings, floats, ints and other standard data types. So a its a good idea for a logger to have the same functionality as the STL ostream.</p>
<pre class="brush: cpp; title: Code; notranslate">
// Bad and slow (thanks to boost::lexical_cast)
Logger::getInstance().write(&quot;The current line is: &quot; + boost::lexical_cast&lt;std::string&gt;(__LINE__));
// Good
Logger::getInstance() &lt;&lt; &quot;The current line is: &quot; &lt;&lt; __LINE__ &lt;&lt; endl;
</pre>
<p>Also note that the ostream&#8217;s way is much faster because we can use a preallocated buffer instead of invoking malloc to concatenate the STL strings all the time.</p>
<h3>﻿﻿Minimal dependences</h3>
<p>The logger is an essential subsystem that practically every other subsystem uses. It&#8217;s a very bad idea for the logger to depend on other engine subsystems like the renderer or some global variables. The cross dependencies across subsystems should be avoided when possible and especially for logging.</p>
<h3>Only one instance &amp; self sustained</h3>
<p>In an application only one logger should exist. To ensure the uniqueness we can use the <a href="http://en.wikipedia.org/wiki/Singleton_pattern" target="_blank" onclick="pageTracker._trackPageview('/outgoing/en.wikipedia.org/wiki/Singleton_pattern?referer=');">singleton pattern</a> or create a global instance. As we all know globals should be avoided when possible and for that reason the AnKi logger is a singleton class. Also the logger cannot be part of any other subsystem because this makes the unit testing difficult.</p>
<p>In the previous version the logger was member of the global application class and it was initialized by the application. For testing, if a subsystem wanted to use only the logger it had to initialize the whole application. Thats a bad practice for our simple unit tests because it renders the code pretty untestable. For unit testing the preferable practice is to have many independent subsystems with very little dependences with each other.</p>
<h3>Multiple outputs</h3>
<p>In a game engine the logs should be outputted in various locations. The output can be the standard output (or standard error), the in-game console (eg quake console), a pop-up message box, in a log file(s) or in all of these at the same time. As we already mentioned before the logger should not have any dependencies so a question is how to write to the in-game console, for example, without invoking the renderer? The answer is to use the <a href="http://en.wikipedia.org/wiki/Observer_pattern" target="_blank" onclick="pageTracker._trackPageview('/outgoing/en.wikipedia.org/wiki/Observer_pattern?referer=');">observer pattern</a>. For those who have worked with a signal/slot framework (eg Qt) this introduction is pointless but for those who haven&#8217;t lets explain how AnKi uses it.</p>
<p>The logger has a signal data member, if another subsystem wants to know the logger&#8217;s output it can simply &#8220;connect&#8221; to this signal. When the logger is ready output a message (eg when it flushes) it emits the signal with the message as a parameter. The subsystems that are &#8220;connected&#8221; to this signal will get the message and handle it according to their needs.</p>
<pre class="brush: cpp; title: Code; notranslate">
// Connect the signal with a method...
Logger::getInstance().getSignal().connect(boost::bind(&amp;App::handleMessageHanlderMsgs, this, _1, _2, _3, _4));
// ... and the method just outputs to the stdout
void App::handleMessageHanlderMsgs(const char* file, int line, const char* func, const std::string&amp; msg)
{
	std::cout &lt;&lt; &quot;(&quot; &lt;&lt; file &lt;&lt; &quot;:&quot; &lt;&lt; line &lt;&lt; &quot; &quot;&lt;&lt; func &lt;&lt; &quot;) &quot; &lt;&lt; msg &lt;&lt; std::endl;
}
</pre>
<h3>Error tolerant</h3>
<p>The logger provides the means for error reporting but what happens if the logger itself needs to report an internal error? Its very stupid for the logger to use itself to for reporting, the reason is pretty obvious I guess. The only solution is to be fault tolerant or use assertions. For example, in AnKi when we have a buffer overflow we flush the already filled buffer and continue from the top. In another case when the logger&#8217;s input is very big and exceeds the logger&#8217;s internal buffer (more than 512 bytes!!??) we simply ignore the message.</p>
<h3>Conclusion &amp; references</h3>
<p>The logger in AnKi is very clean and abstract. One of the disadvantages it that the signals and slots add a certain overhead but the messages in the main loop should be kept to minimum either way.</p>
<p><a href="http://code.google.com/p/anki-3d-engine/source/browse/trunk/src/Core/Logger.h" onclick="pageTracker._trackPageview('/outgoing/code.google.com/p/anki-3d-engine/source/browse/trunk/src/Core/Logger.h?referer=');">AnKi logger header file</a></p>
<p><a href="http://code.google.com/p/anki-3d-engine/source/browse/trunk/src/Core/Logger.cpp" onclick="pageTracker._trackPageview('/outgoing/code.google.com/p/anki-3d-engine/source/browse/trunk/src/Core/Logger.cpp?referer=');">AnKi logger source file</a></p>
<p><a href="http://www.boost.org/doc/libs/1_45_0/doc/html/signals2.html" target="_blank" onclick="pageTracker._trackPageview('/outgoing/www.boost.org/doc/libs/1_45_0/doc/html/signals2.html?referer=');">Boost signal library</a></p>
<p><a href="http://doc.qt.nokia.com/4.7/signalsandslots.html" target="_blank" onclick="pageTracker._trackPageview('/outgoing/doc.qt.nokia.com/4.7/signalsandslots.html?referer=');">Qt signals/slots</a></p>
]]></content:encoded>
			<wfw:commentRss>http://anki3d.org/the-perfect-logging-system-for-cpp-apps/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Status report: OpenGL 3.3 core &amp; XML files</title>
		<link>http://anki3d.org/progress-report-opengl-3-3-core-xml-files/</link>
		<comments>http://anki3d.org/progress-report-opengl-3-3-core-xml-files/#comments</comments>
		<pubDate>Fri, 03 Dec 2010 10:43:14 +0000</pubDate>
		<dc:creator>Panos Christopoulos Charitos</dc:creator>
				<category><![CDATA[Status reports]]></category>
		<category><![CDATA[animation]]></category>
		<category><![CDATA[boost]]></category>
		<category><![CDATA[OpenGL]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://anki3d.org/?p=86</guid>
		<description><![CDATA[OpenGL 3.3 core After a few weeks of coding I&#8217;ve moved the renderer from OpenGL 2.something to OpenGL 3.3 core. This process proved to be a not so easy task mainly because I had to modify a few shaders and &#8230;<p class="read-more"><a href="http://anki3d.org/progress-report-opengl-3-3-core-xml-files/">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<h3>OpenGL 3.3 core</h3>
<p>After a few weeks of coding I&#8217;ve moved the renderer from OpenGL 2.something to OpenGL 3.3 core. This process proved to be a not so easy task mainly because I had to modify a few shaders and change the way the meshes get rendered. One thing to note about 3.3 is the way of rendering geometry. It doesn&#8217;t support immediate mode ofcource, it doesnt support vertex arrays and it doesnt support the use of vertex buffer objects (VBOs) outside vertex array objects (VAOs). The changes in the shaders were not that difficult. The modifications were to remove the varyings and replace them with in/out variables, remove the gl_FragColor and add a few layouts for some attributes. The shaders worked without causing major headaches.</p>
<p>With the transition to 3.3 core I found an opportunity to change the organisation of meshes and materials. The meshes now contain only the VBOs and the materials do not have any information about the depth passes. A mesh can now have multiple compatible materials. The container that has meshes, materials, skeletons and animations is called model.</p>
<p>The work in the renderer is not yet complete. I have to rewrite the  debug rendering stage and for that I need think of an optimal way to  draw debug primitives (mainly lines).</p>
<h3>XML files</h3>
<p>One more thing is the new file format for materials. I&#8217;ve moved away from the custom formats and its now the materials are in XML. To parse the XML AnKi uses the boost property_tree container. The model files are in XML as well and only the lights are left to be done.</p>
<h3>Future</h3>
<p>For the future AnKi has quite a lengthy TODO list</p>
<ul>
<li>Modify the hardware skinning by implement texture buffers and use them as bone transformations in the shaders</li>
<li>Optimize the animation by the means of transform feedback (details will follow)</li>
<li>Finish the shredder tool that splits meshes in little octree cubes</li>
<li>Implement animation blending</li>
</ul>
<p><span id="more-86"></span></p>
<p>This is an example of a material file:</p>
<pre class="brush: xml; title: Code; notranslate">&lt;material&gt;
	&lt;shaderProg&gt;
		&lt;customMsSProg&gt;
			&lt;defines&gt;
				&lt;define&gt;DIFFUSE_MAPPING&lt;/define&gt;
				&lt;define&gt;NORMAL_MAPPING&lt;/define&gt;
				&lt;define&gt;ENVIRONMENT_MAPPING&lt;/define&gt;
			&lt;/defines&gt;
		&lt;/customMsSProg&gt;
	&lt;/shaderProg&gt;

	&lt;userDefinedVars&gt;
		&lt;userDefinedVar&gt;
			&lt;name&gt;diffuseMap&lt;/name&gt;
			&lt;value&gt;
				&lt;texture&gt;meshes/horse/horse.diff.png&lt;/texture&gt;
			&lt;/value&gt;
		&lt;/userDefinedVar&gt;

		&lt;userDefinedVar&gt;
			&lt;name&gt;normalMap&lt;/name&gt;
			&lt;value&gt;
				&lt;texture&gt;meshes/horse/horse.norm.png&lt;/texture&gt;
			&lt;/value&gt;
		&lt;/userDefinedVar&gt;

		&lt;userDefinedVar&gt;
			&lt;name&gt;environmentMap&lt;/name&gt;
			&lt;value&gt;
				&lt;texture&gt;textures/env/env_map_3.png&lt;/texture&gt;
			&lt;/value&gt;
		&lt;/userDefinedVar&gt;

		&lt;userDefinedVar&gt;
			&lt;name&gt;specularCol&lt;/name&gt;
			&lt;value&gt;
				&lt;vec3&gt;&lt;x&gt;1.0&lt;/x&gt;&lt;y&gt;2.0&lt;/y&gt;&lt;z&gt;-0.8&lt;/z&gt;&lt;/vec3&gt;
			&lt;/value&gt;
		&lt;/userDefinedVar&gt;

		&lt;userDefinedVar&gt;
			&lt;name&gt;diffuseCol&lt;/name&gt;
			&lt;value&gt;
				&lt;vec3&gt;&lt;x&gt;1.0&lt;/x&gt;&lt;y&gt;1.0&lt;/y&gt;&lt;z&gt;1.0&lt;/z&gt;&lt;/vec3&gt;
			&lt;/value&gt;
		&lt;/userDefinedVar&gt;

		&lt;userDefinedVar&gt;
			&lt;name&gt;shininess&lt;/name&gt;
			&lt;value&gt;
				&lt;float&gt;90.0&lt;/float&gt;
			&lt;/value&gt;
		&lt;/userDefinedVar&gt;
	&lt;/userDefinedVars&gt;
&lt;/material&gt;</pre>
]]></content:encoded>
			<wfw:commentRss>http://anki3d.org/progress-report-opengl-3-3-core-xml-files/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Why boost python for AnKi?</title>
		<link>http://anki3d.org/why-boost-python-for-anki/</link>
		<comments>http://anki3d.org/why-boost-python-for-anki/#comments</comments>
		<pubDate>Wed, 27 Oct 2010 09:24:28 +0000</pubDate>
		<dc:creator>Panos Christopoulos Charitos</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[AnKi]]></category>
		<category><![CDATA[boost]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[scripting]]></category>

		<guid isPermaLink="false">http://anki3d.org/?p=75</guid>
		<description><![CDATA[Every game engine, at some level, needs to be able to compile and run code at runtime. You need, for example, to trigger a custom event when the hero pushed a button. The button should contain a bunch of code &#8230;<p class="read-more"><a href="http://anki3d.org/why-boost-python-for-anki/">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<p>Every game engine, at some level, needs to be able to compile and run code at runtime. You need, for example, to trigger a custom event when the hero pushed a button. The button should contain a bunch of code that may call an elevator or/and open a door or/and even kill the hero. All these custom events are possible by embedding a scripting language on top of the native language the game uses. Most of the game developers use native languages like C/C++ (very popular), Objective C (iPhone) and Java (other mobile platforms and rarely on PC). As for the scripting languages, the developers face many more choices with custom scripting languages (Unreal Script), lua, javascript (through various implementations), Python, Squirrel and more.</p>
<p>AnKi, just like every other game engine, needed a scripting language and the searching/prototyping started almost a year back. We have made prototypes for lua, javascript (V8 and SpiderMonkey), squirrel, Qt script, Python and others. Lets see why we chose boost python over others&#8230;<br />
<span id="more-75"></span></p>
<h1>Key elements of choosing a scripting language</h1>
<p>Lets analyze what are the key elements of choosing a scripting language for a game:</p>
<ul>
<li> <strong>Glue code</strong>. The embedding is a process that, unfortunately, comes with additional development cost. The developer needs to expose (aka wrap) classes and functions by writing glue code. This is not an easy task and it varies from API to API. For example squirrel was designed to be an embedded language in games and its fairly easy to write glue code. Python on the other hand is not. Using python C API is very unproductive.</li>
<li> <strong>Speed</strong>. Some interpreter implementations offer faster responsiveness over others. For example Google&#8217;s V8 engine offers JIT (just in time) compilation, a feature that made Google&#8217;s Chrome one of the fastest browsers out there.</li>
<li><strong>Platform support</strong>. The language binaries/libraries should be portable among popular gaming platforms like PC and consoles. In most cases this is not a problem because most of the interpreters are written in C/C++ which makes them fairly portable. There is a big &#8220;but&#8221; though. Some interpreters support JIT compilation, a feature that implies generation of machine dependent code at runtime. One example is Google&#8217;s V8 again. In order to use V8 you have to build only 32bit executables of your application. Ok that not a big deal, the big deal is that the generated  code is for x86 and ARM platforms and it wont run in console hardware without major modifications.</li>
<li><strong>Popularity</strong>: Being a popular language means that there are lots of tutorials, documentation and people who can help you. Some languages are fairly new like Squirrel and they lack excessive support. Its a risk to use a language and have to switch later because of lack of support.</li>
</ul>
<h1>Pros and cons of the tested implementations</h1>
<p>Lets see what are the pros and cons of all these implementations:</p>
<ul>
<li><strong>Lua</strong>: Fast, small memory footprint, very popular among game developers, moderate glue code writing, weird syntax (IMHO)</li>
<li><strong>Qt script</strong>: Speed is undetermined, javascript, zero usage among game developers, very easy to write glue code, difficult to port to consoles, Qt dependent</li>
<li><strong>V8</strong>: Smoking fast, javascript, moderate writing of glue code, non portable, bad documentation</li>
<li><strong>SpiderMonkey family</strong>: Fast to smoking fast, javascript, moderate writing of glue code, the SpiderMonkey is portable, the TraceMonkey is portable but the JägerMonkey may not be due to JIT compiler, good documentation</li>
<li><strong>Squirrel</strong>: Speed is undetermined, glue code writing is easy, designed for game embedding, not that many developers behind it</li>
<li><strong>Python</strong>: Not the fastest around, huge in non-game developers and popular among game developers, lots of glue code when using C API but very very easy with boost python</li>
</ul>
<p>*Note: As we can see javascript is very popular with various implementations but for games it may be a no go due to luck of support of operator overloading. The math library in AnKi uses operator overloading and to map this in javascripts requires different notations. It may be ugly.</p>
<h1>Why boost python?</h1>
<p>After creating (and still creating) prototypes we choose boost python. Boost python is a very smart wrapper on top of Python&#8217;s C API that makes the writing of glue code very very easy. Python is the most popular scripting language around, it may be slower than lua but for the py3k (Python 3.x) there is a JIT compiler under development. On the downside, boost python is template oriented and most of the time you find yourself in front of compiler errors with obfuscated cause. Also the documentation is lucking.</p>
<h1>References</h1>
<p><a href="http://squirrel-lang.org" onclick="pageTracker._trackPageview('/outgoing/squirrel-lang.org?referer=');">http://squirrel-lang.org</a><br />
<a href="http://www.boost.org/doc/libs/1_44_0/libs/python/doc/index.html" onclick="pageTracker._trackPageview('/outgoing/www.boost.org/doc/libs/1_44_0/libs/python/doc/index.html?referer=');">http://www.boost.org/doc/libs/1_44_0/libs/python/doc/index.html</a><br />
<a href="http://code.google.com/apis/v8/embed.html" onclick="pageTracker._trackPageview('/outgoing/code.google.com/apis/v8/embed.html?referer=');">http://code.google.com/apis/v8/embed.html</a><br />
<a href="https://developer.mozilla.org/en/JavaScript_C_Engine_Embedder%27s_Guide" onclick="pageTracker._trackPageview('/outgoing/developer.mozilla.org/en/JavaScript_C_Engine_Embedder_27s_Guide?referer=');">https://developer.mozilla.org/en/JavaScript_C_Engine_Embedder%27s_Guide</a></p>
]]></content:encoded>
			<wfw:commentRss>http://anki3d.org/why-boost-python-for-anki/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The nessesarity of binary files</title>
		<link>http://anki3d.org/the-nessesarity-of-binary-files/</link>
		<comments>http://anki3d.org/the-nessesarity-of-binary-files/#comments</comments>
		<pubDate>Fri, 15 Oct 2010 14:41:28 +0000</pubDate>
		<dc:creator>Panos Christopoulos Charitos</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[C++]]></category>

		<guid isPermaLink="false">http://anki3d.org/?p=65</guid>
		<description><![CDATA[Until now AnKi used to read all the assets from text files. The mesh (containing the geometry), the skeleton, the skeleton animation, the light properties, the materials etc etc were all text files. Text files are pretty nice for debugging &#8230;<p class="read-more"><a href="http://anki3d.org/the-nessesarity-of-binary-files/">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<p>Until now AnKi used to read all the assets from text files. The mesh (containing the geometry), the skeleton, the skeleton animation, the light properties, the materials etc etc were all text files. Text files are pretty nice for debugging and importing but they contain lots of unnecessary information and they take lots of time to load, not to mention the size. If we take into account that some of the AnKi assets are automatically generated we see that text files are simple inadequate for some cases.</p>
<p><span id="more-65"></span></p>
<p>For that reason we&#8217;ve created a versatile class called BinaryStream. Binary stream inherits the well known C++ iostream which allows the class to behave as a text stream as well. On top of that there are a few methods that extract binary data from binary files. It can extract unsigned integers (32bit), floats (32bit) and special strings. One thing to note about the numbers is the endianness also known as byte order. Byte order is a common problem when it comes to memory representation of numbers. BinaryStream class addresses this issue by taking into account the file&#8217;s byte order as well as the machine&#8217;s byte order. The strings are a special case of strings that resemble Pascal strings. The first 32bits are the string&#8217;s length and the rest is the following characters.</p>
<p>AnKi binary files also contain a header before everything. The header is an 8byte identifier and it ensures that we are reading the right file type. For example meshes have the &#8220;ANKIMESH&#8221; and the skeletons the &#8220;ANKISKEL&#8221;.</p>
<p>Here is a sample of how we use the BinaryStream in mesh loading:</p>
<pre class="brush: cpp; title: Code; notranslate">// Open the file
std::fstream file(filename, std::fstream::in | std::fstream::binary);

if(!file.is_open())
{
	throw EXCEPTION(&quot;Cannot open file \&quot;&quot; + filename + &quot;\&quot;&quot;);
}

BinaryStream bs(file.rdbuf());

// Magic word
char magic[8];
bs.read(magic, 8);
if(bs.fail() || memcmp(magic, &quot;ANKIMESH&quot;, 8))
{
	throw MESH_EXCEPTION(&quot;Incorrect magic word&quot;);
}

// Mesh name
std::string meshName = bs.readString();

// Material name
std::string materialName = bs.readString();
if(materialName.length() &gt; 0)
{
	material.loadRsrc(materialName.c_str());
}

// Verts num
uint vertsNum = bs.readUint();
vertCoords.resize(vertsNum);</pre>
<p>If you want suggestions of how to load binary files check the BinaryStream in the SVN repository</p>
]]></content:encoded>
			<wfw:commentRss>http://anki3d.org/the-nessesarity-of-binary-files/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>C++ exceptions and overhead</title>
		<link>http://anki3d.org/cpp-exceptions/</link>
		<comments>http://anki3d.org/cpp-exceptions/#comments</comments>
		<pubDate>Tue, 05 Oct 2010 20:47:40 +0000</pubDate>
		<dc:creator>Panos Christopoulos Charitos</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[C++]]></category>

		<guid isPermaLink="false">http://anki3d.org/?p=41</guid>
		<description><![CDATA[There are numerous ways for error handling in C++. From day one AnKi used an almost C-like way to handle errors and the standard error to report them. The truth is that we never cared to recover from errors, the &#8230;<p class="read-more"><a href="http://anki3d.org/cpp-exceptions/">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<p>There are numerous ways for error handling in C++. From day one AnKi used an almost C-like way to handle errors and the standard error to report them. The truth is that we never cared to recover from errors, the only concern was and still is to report the errors and be able to identify the cause of the problem without the use of a debugger. What about exceptions then? Are they a nice way for error handling? Lets see&#8230;</p>
<p><span id="more-41"></span></p>
<h1>The C-like way of error handling</h1>
<p>The main idea is to return true on success and if an error happens it will be reported by the function that caught it first.</p>
<pre class="brush: cpp; title: Code; notranslate">bool doSomething(...)
{
	...
	if(error)
	{
		cerr &lt;&lt; &quot;Something went wrong&quot; &lt;&lt; endl;
		return false;
	}
	...
	return true;
}</pre>
<p>If someone calls the function &#8220;doSomething&#8221; it needs to check the return flag to check is an error happened. It is possible to report the error again and have a nice backtrace of the error.</p>
<blockquote><p>Error (Texture.cpp:126 createEmpty2D): OpenGL Err: invalid value<br />
Fatal (Ms.cpp:28 init): Failed to create one MS FAI. See prev error. Bye!</p></blockquote>
<p>In the above example the function <em>createEmpty2D</em> failed for some reason, reported the error and returned false. The <em>init</em> function, who is the caller of the <em>createEmpty2D</em>, caught the false and re-reported the error. With this simple trick we have a way to keep a backtrace and immediately we know who failed and who started it.</p>
<h1>Exceptions</h1>
<p>For some subsystems this C-like way of handling errors is ok but its very dirty for library-like subsystems. Exceptions is a nice alternative. We throw an exception and the caller is responsible of handling it. I wont go on with details of how exceptions work, this is not a tutorial but this is how <em>doSomething</em> looks like:</p>
<pre class="brush: cpp; title: Code; notranslate">void doSomething(...)
{
	...
	if(error)
	{
		throw std::runtime_error(&quot;Something went wrong&quot;);
	}
	...
}</pre>
<p>Obviously the code is less cause you save the returns, also you wont find yourself in the unpleasant position of forgetting a return or returning the incorrect boolean. There is an issue of a certain overhead when using exceptions. Lets see how much overhead exceptions add.</p>
<p>I&#8217;ve created a simple example and tried to read the assembly code.</p>
<pre class="brush: cpp; title: Code; notranslate">try
{
	printf(&quot;in\n&quot;);
	int r = rand() % 2 + 1;
	doSomething(r);
	printf(&quot;out\n&quot;);
}
catch(std::exception&amp; e)
{
	printf(&quot;in\n&quot;);
	fprintf(stderr, &quot;%s\n&quot;, e.what());
	printf(&quot;out\n&quot;);
}</pre>
<p>The assembly:</p>
<pre class="brush: plain; title: Code; notranslate">			printf(&quot;in\n&quot;);
0x000000000040263a &lt;main+42&gt;:  mov    $0x405a82,%edi
0x000000000040263f &lt;main+47&gt;:  callq  0x402280 &lt;puts@plt&gt;
			int r = rand() % 2 + 1;
0x0000000000402644 &lt;main+52&gt;:  callq  0x402040 &lt;rand@plt&gt;
0x0000000000402649 &lt;main+57&gt;:  mov    %eax,%edx
0x000000000040264b &lt;main+59&gt;:  sar    $0x1f,%edx
0x000000000040264e &lt;main+62&gt;:  shr    $0x1f,%edx
0x0000000000402651 &lt;main+65&gt;:  add    %edx,%eax
0x0000000000402653 &lt;main+67&gt;:  and    $0x1,%eax
0x0000000000402656 &lt;main+70&gt;:  sub    %edx,%eax
0x0000000000402658 &lt;main+72&gt;:  add    $0x1,%eax
0x000000000040265b &lt;main+75&gt;:  mov    %eax,-0x18(%rbp)
			doSomething(r);
0x000000000040265e &lt;main+78&gt;:  mov    -0x18(%rbp),%eax
0x0000000000402661 &lt;main+81&gt;:  mov    %eax,%edi
0x0000000000402663 &lt;main+83&gt;:  callq  0x402494 &lt;_Z11doSomethingi&gt;
			printf(&quot;out\n&quot;);
0x0000000000402668 &lt;main+88&gt;:  mov    $0x405a85,%edi
0x000000000040266d &lt;main+93&gt;:  callq  0x402280 &lt;puts@plt&gt;
0x0000000000402672 &lt;main+98&gt;:  jmpq   0x4026f7 &lt;main+231&gt;
0x0000000000402677 &lt;main+103&gt;: cmp    $0x1,%rdx
0x000000000040267b &lt;main+107&gt;: je     0x402685 &lt;main+117&gt;
0x000000000040267d &lt;main+109&gt;: mov    %rax,%rdi
0x0000000000402680 &lt;main+112&gt;: callq  0x402330 &lt;_Unwind_Resume@plt&gt;
		catch(std::exception&amp; e)
0x0000000000402685 &lt;main+117&gt;: mov    %rax,%rdi
0x0000000000402688 &lt;main+120&gt;: callq  0x402060 &lt;__cxa_begin_catch@plt&gt;
0x000000000040268d &lt;main+125&gt;: mov    %rax,-0x20(%rbp)
0x00000000004026d8 &lt;main+200&gt;: callq  0x4022c0 &lt;__cxa_end_catch@plt&gt;
0x00000000004026dd &lt;main+205&gt;: jmp    0x4026f7 &lt;main+231&gt;
0x00000000004026df &lt;main+207&gt;: mov    %edx,%ebx
0x00000000004026e1 &lt;main+209&gt;: mov    %rax,%r12
0x00000000004026e4 &lt;main+212&gt;: callq  0x4022c0 &lt;__cxa_end_catch@plt&gt;
0x00000000004026e9 &lt;main+217&gt;: mov    %r12,%rax
0x00000000004026ec &lt;main+220&gt;: movslq %ebx,%rdx
0x00000000004026ef &lt;main+223&gt;: mov    %rax,%rdi
0x00000000004026f2 &lt;main+226&gt;: callq  0x402330 &lt;_Unwind_Resume@plt&gt;
			printf(&quot;in\n&quot;);
0x0000000000402691 &lt;main+129&gt;: mov    $0x405a82,%edi
0x0000000000402696 &lt;main+134&gt;: callq  0x402280 &lt;puts@plt&gt;
			fprintf(stderr, &quot;%s\n&quot;, e.what());
0x000000000040269b &lt;main+139&gt;: mov    -0x20(%rbp),%rax
0x000000000040269f &lt;main+143&gt;: mov    (%rax),%rax
0x00000000004026a2 &lt;main+146&gt;: add    $0x10,%rax
0x00000000004026a6 &lt;main+150&gt;: mov    (%rax),%rdx
0x00000000004026a9 &lt;main+153&gt;: mov    -0x20(%rbp),%rax
0x00000000004026ad &lt;main+157&gt;: mov    %rax,%rdi
0x00000000004026b0 &lt;main+160&gt;: callq  *%rdx
0x00000000004026b2 &lt;main+162&gt;: mov    %rax,%rdx
0x00000000004026b5 &lt;main+165&gt;: mov    0x205ddc(%rip),%rax        # 0x608498 &lt;stderr@@GLIBC_2.2.5&gt;
0x00000000004026bc &lt;main+172&gt;: mov    $0x405a89,%esi
0x00000000004026c1 &lt;main+177&gt;: mov    %rax,%rdi
0x00000000004026c4 &lt;main+180&gt;: mov    $0x0,%eax
0x00000000004026c9 &lt;main+185&gt;: callq  0x402240 &lt;fprintf@plt&gt;
			printf(&quot;out\n&quot;);
0x00000000004026ce &lt;main+190&gt;: mov    $0x405a85,%edi
0x00000000004026d3 &lt;main+195&gt;: callq  0x402280 &lt;puts@plt&gt;</pre>
<p>For doSomething there is practically no difference, its like a normal call. The try branch does not add any overhead, nevertheless, the catch does.</p>
<p>Lets try another test case. We have the two different samples to compare them. Note that in both cases we wont get an exception, this means that we will never go into a catch block.</p>
<pre class="brush: cpp; title: Code; notranslate">for(int i=0; i&lt;BIG_NUMBER; i++)
{
	try
	{
		int r = rand() % 2 + 1;
		doSomething(r);
	}
	catch(std::exception&amp; e)
	{
		fprintf(stderr, &quot;%s\n&quot;, e.what());
		break;
	}
}</pre>
<pre class="brush: cpp; title: Code; notranslate">try
{
	for(int i=0; i&lt;BIG_NUMBER; i++)
	{
		int r = rand() % 2 + 1;
		doSomething(r);
	}
}
catch(std::exception&amp; e)
{
	fprintf(stderr, &quot;%s\n&quot;, e.what());
}</pre>
<p>The thing to now is where the try/catch block is located. In the first case we have the try/catch inside the loop and in the second outside. After some benchmarks we saw that practically there is no speed difference. This indicates that the try block does not add any significant overhead.</p>
<h1>Conclusion</h1>
<p>In AnKi we dont care to recover from an error. The only thing that matters is to report the error correctly and nothing else. Exceptions make the code more simple, I guess, and in our case they dont add any noticeable overhead.</p>
]]></content:encoded>
			<wfw:commentRss>http://anki3d.org/cpp-exceptions/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

