<?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>Alejandro Segovia Azapian</title>
	<atom:link href="http://www.alejandrosegovia.net/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.alejandrosegovia.net</link>
	<description>C++ and Graphics</description>
	<lastBuildDate>Tue, 07 Feb 2012 11:09:15 +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>Light Scattering</title>
		<link>http://www.alejandrosegovia.net/2012/02/06/light-scattering/</link>
		<comments>http://www.alejandrosegovia.net/2012/02/06/light-scattering/#comments</comments>
		<pubDate>Mon, 06 Feb 2012 08:00:08 +0000</pubDate>
		<dc:creator>Alex</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[OpenGL]]></category>
		<category><![CDATA[Vortex-Engine]]></category>

		<guid isPermaLink="false">http://www.alejandrosegovia.net/?p=128</guid>
		<description><![CDATA[When we introduced Programmable Pipeline support to Vortex Engine, we claimed that better visual effects could now be introduced into the renderer. With the introduction of Render-to-Texture functionality into Vortex 2.0, coupled with the power of Shaders, we can now make good on our promise. Light Scattering (also known as &#8220;God Rays&#8221;) is a prime [...]]]></description>
			<content:encoded><![CDATA[<p>When we introduced Programmable Pipeline support to Vortex Engine, we claimed that better visual effects could now be introduced into the renderer. With the <a href="http://www.alejandrosegovia.net/2012/01/30/vortex-fbos">introduction of Render-to-Texture functionality</a> into Vortex 2.0, coupled with the power of Shaders, we can now make good on our promise.</p>
<p>Light Scattering (also known as &#8220;God Rays&#8221;) is a prime example of what can be achieved with Shaders and Render-to-Texture capabilities.</p>
<p>In the following image, a room consisting of an art gallery with tall pillars is depicted. We want to convey the effect of sun light coming from outside, illuminating the inner nave. Enter Light Scattering:</p>
<div id="attachment_133" class="wp-caption aligncenter" style="width: 655px"><a href="http://www.alejandrosegovia.net/wp-content/uploads/2012/02/ls_0c.png"><img class="size-large wp-image-133" title="Light Scattering" src="http://www.alejandrosegovia.net/wp-content/uploads/2012/02/ls_0c-1024x872.png" alt="" width="645" height="549" /></a><p class="wp-caption-text">A Light Scattering algorithm is used for improving the visual experience. The scene is rendered using Vortex 2.0. Room courtesy of RealityFrontier. (Click to Enlarge)</p></div>
<p>&nbsp;</p>
<p>It can be seen in the picture above how the the effect, although subtle, brings more life to the rendered scene. There is still much room to improve the visual results, though, particularily with the results of Kenny Mitchel&#8217;s article on GPU Gems 3.</p>
<p>There is also room for optimization. Currently, the scene is rendered in realtime at an average of 187 frames per second, producing 1024&#215;1024 images on a NVIDIA GeForce GTX465. Moving the algorithm to mobile devices, although easy from a coding perspective, might require extra work to achieve a high frame rate on the embedded GPU.</p>
<p>Here are three more captures from different angles.</p>
<div id="attachment_130" class="wp-caption alignleft" style="width: 310px"><a href="http://www.alejandrosegovia.net/wp-content/uploads/2012/01/ls_09.png"><img class="size-medium wp-image-130 " title="Light Scattering" src="http://www.alejandrosegovia.net/wp-content/uploads/2012/01/ls_09-300x255.png" alt="" width="300" height="255" /></a><p class="wp-caption-text">(Click to Enlarge)</p></div>
<p>&nbsp;</p>
<div id="attachment_131" class="wp-caption alignright" style="width: 310px"><a href="http://www.alejandrosegovia.net/wp-content/uploads/2012/01/ls_0a.png"><img class=" wp-image-131" title="Light Scattering" src="http://www.alejandrosegovia.net/wp-content/uploads/2012/01/ls_0a-300x255.png" alt="" width="300" height="255" /></a><p class="wp-caption-text">(Click to Enlarge)</p></div>
<div id="attachment_129" class="wp-caption alignleft" style="width: 310px"><a href="http://www.alejandrosegovia.net/wp-content/uploads/2012/01/ls_0b.png"><img class="size-medium wp-image-129 " title="Light Scattering" src="http://www.alejandrosegovia.net/wp-content/uploads/2012/01/ls_0b-300x255.png" alt="" width="300" height="255" /></a><p class="wp-caption-text">(Click to Enlarge)</p></div>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.alejandrosegovia.net/2012/02/06/light-scattering/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Vortex adds Render-to-Texture capabilities</title>
		<link>http://www.alejandrosegovia.net/2012/01/30/vortex-fbos/</link>
		<comments>http://www.alejandrosegovia.net/2012/01/30/vortex-fbos/#comments</comments>
		<pubDate>Mon, 30 Jan 2012 08:00:12 +0000</pubDate>
		<dc:creator>Alex</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[OpenGL]]></category>
		<category><![CDATA[Vortex-Engine]]></category>

		<guid isPermaLink="false">http://www.alejandrosegovia.net/?p=125</guid>
		<description><![CDATA[I&#8217;m very glad to announce Vortex now supports render-to-texture capabilities by means of Framebuffer Objects : )]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m very glad to announce Vortex now supports render-to-texture capabilities by means of Framebuffer Objects : )</p>
<div id="attachment_126" class="wp-caption aligncenter" style="width: 602px"><a href="http://www.alejandrosegovia.net/wp-content/uploads/2012/01/fbo1.png"><img class="size-full wp-image-126" title="Vortex Framebuffer Object Support" src="http://www.alejandrosegovia.net/wp-content/uploads/2012/01/fbo1.png" alt="" width="592" height="614" /></a><p class="wp-caption-text">Vortex Render-to-Texture support: a knight is rendered on a texture that is then mapped on a cube. All rendering is done on the GPU, avoiding expensive copies to RAM.</p></div>
]]></content:encoded>
			<wfw:commentRss>http://www.alejandrosegovia.net/2012/01/30/vortex-fbos/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Implementing sqrt</title>
		<link>http://www.alejandrosegovia.net/2012/01/23/implementing-sqrt/</link>
		<comments>http://www.alejandrosegovia.net/2012/01/23/implementing-sqrt/#comments</comments>
		<pubDate>Mon, 23 Jan 2012 08:00:06 +0000</pubDate>
		<dc:creator>Alex</dc:creator>
				<category><![CDATA[C]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[Math]]></category>

		<guid isPermaLink="false">http://www.alejandrosegovia.net/?p=123</guid>
		<description><![CDATA[I was reading some post about interview questions of 2011 and came across one that stated &#8220;find the square root of a number&#8221;. Assuming we can&#8217;t use the sqrtf function of the standard C math library, let&#8217;s see how we can calculate the square root of a number x. Given n, we know that its [...]]]></description>
			<content:encoded><![CDATA[<p>I was reading some post about interview questions of 2011 and came across one that stated &#8220;find the square root of a number&#8221;.</p>
<p>Assuming we can&#8217;t use the sqrtf function of the standard C math library, let&#8217;s see how we can calculate the square root of a number <strong>x</strong>.</p>
<p>Given <strong>n</strong>, we know that its square root is a number <strong>x</strong> that holds:</p>
<img src='http://s.wordpress.com/latex.php?latex=%5Csqrt%7Bn%7D%20%3D%20x&#038;bg=ffffff&#038;fg=444444&#038;s=0' alt='\sqrt{n} = x' title='\sqrt{n} = x' class='latex' />
<p>Let&#8217;s work on this equation a little. Raise both sides to the second power:</p>
<img src='http://s.wordpress.com/latex.php?latex=n%20%3D%20x%5E2&#038;bg=ffffff&#038;fg=444444&#038;s=0' alt='n = x^2' title='n = x^2' class='latex' />
<p>Move to the left of the equality:</p>
<img src='http://s.wordpress.com/latex.php?latex=0%20%3D%20x%5E2%20-%20n%20&#038;bg=ffffff&#038;fg=444444&#038;s=0' alt='0 = x^2 - n ' title='0 = x^2 - n ' class='latex' />
<p>If we found the roots of this last equation somehow, we would have found the square root of <strong>n</strong>. We can do this by using the Newton-Raphson iteration.</p>
<p>The Newton-Raphson iteration states that we can find the root of an equation using the following formula iteratively:</p>
<img src='http://s.wordpress.com/latex.php?latex=x_%7Bn%2B1%7D%20%3D%20x_n%20-%20%5Cfrac%7Bf%28x%29%7D%7Bf%27%28x%29%7D%20&#038;bg=ffffff&#038;fg=444444&#038;s=0' alt='x_{n+1} = x_n - \frac{f(x)}{f&#039;(x)} ' title='x_{n+1} = x_n - \frac{f(x)}{f&#039;(x)} ' class='latex' />
<p>Where <strong>f&#8217;(x)</strong> is the derivative of function <strong>f(x)</strong>. We will approximate the derivative using the definition of derivative at a point (we could also note that the derivative could be trivially calculated; this method is more general).</p>
<img src='http://s.wordpress.com/latex.php?latex=f%27%28x%29%20%3D%20%5Cfrac%7Bf%28x%2Bh%29%20-%20f%28x%29%7D%7Bh%7D%20&#038;bg=ffffff&#038;fg=444444&#038;s=0' alt='f&#039;(x) = \frac{f(x+h) - f(x)}{h} ' title='f&#039;(x) = \frac{f(x+h) - f(x)}{h} ' class='latex' />
<p>The error of the Newton-Raphson iteration is given by:</p>
<img src='http://s.wordpress.com/latex.php?latex=%7Cx_%7Bn%2B1%7D%20-%20x_n%7C%20&#038;bg=ffffff&#038;fg=444444&#038;s=0' alt='|x_{n+1} - x_n| ' title='|x_{n+1} - x_n| ' class='latex' />
<p>Starting with a hardcoded seed value, we will perform this iteration in a loop until the error is less than a given value. I have chosen to iterate until the error is less than 1&#215;10^(-10): 0.00000000001.</p>
<p>Let us see what a tentative &#8220;pythonesque&#8221; pseudocode for this loop could be:</p>
<pre class="brush:python">
def sqrt(n):
    f = function(x*x - n)
    x = 1 # seed
    xant = 0
    do:
        f1 = (f.eval(x+h) - f.eval(x)) / h
        xant = x
        x = x - f.eval(x)/f1
    while abs(x - xant) > err;
    return x
</pre>
<p>Assuming we have a symbolic function type, that loop does not seem too difficult. In order to code this in C, since the equation is always the same, I will hardcode it as a plain function.</p>
<pre class="brush:cpp">
typedef double real; // change to float for single precision

real f(real x, real n)
{
    return x*x - n;
}

real sqrt(real n)
{
    real err = 0.00000000001f;
    real h = 0.01f;

    real x = 1.0f; // seed
    real xant = 0.0f;

    do
    {
        xant = x;
        real df = (f(x+h, n) - f(x, n))/h;
        x = x - f(x, n)/df;
    }
    while (abs(x - xant) > err);

    return x;
}
</pre>
<p>Here are the results of running our custom square root function, compared to the standard version provided with the C programming language:</p>
<pre class="brush:bash">
[ale@syaoran sqrt]$ ./sqrt 1.0
Custom sqrt of: 1 = 1
libm sqrt of: 1 = 1

[ale@syaoran sqrt]$ ./sqrt 2.0
Custom sqrt of: 2 = 1.41421
libm sqrt of: 2 = 1.41421

[ale@syaoran sqrt]$ ./sqrt 4.0
Custom sqrt of: 4 = 2
libm sqrt of: 4 = 2

[ale@syaoran sqrt]$ ./sqrt 16.0
Custom sqrt of: 16 = 4
libm sqrt of: 16 = 4

[ale@syaoran sqrt]$ ./sqrt 32.0
Custom sqrt of: 32 = 5.65685
libm sqrt of: 32 = 5.65685

[ale@syaoran sqrt]$ ./sqrt 100.0
Custom sqrt of: 100 = 10
libm sqrt of: 100 = 10

[ale@syaoran sqrt]$ ./sqrt 1000000.0
Custom sqrt of: 1e+06 = 1000
libm sqrt of: 1e+06 = 1000
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.alejandrosegovia.net/2012/01/23/implementing-sqrt/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A short detour along the way&#8230;</title>
		<link>http://www.alejandrosegovia.net/2012/01/02/a-short-detour-along-the-way/</link>
		<comments>http://www.alejandrosegovia.net/2012/01/02/a-short-detour-along-the-way/#comments</comments>
		<pubDate>Mon, 02 Jan 2012 08:00:00 +0000</pubDate>
		<dc:creator>Alex</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[iOS]]></category>
		<category><![CDATA[OpenGL]]></category>
		<category><![CDATA[Vortex-Engine]]></category>

		<guid isPermaLink="false">http://www.alejandrosegovia.net/?p=119</guid>
		<description><![CDATA[I wanted to improve the Stencil Shadow Volumes code a little bit and enable it for the Programmable Pipeline in Vortex, however, I had to take a small detour to fix an issue related to mobile device support. It turns out that OpenGL ES, the 3D library that Vortex uses to render its graphics on [...]]]></description>
			<content:encoded><![CDATA[<p>I wanted to improve the Stencil Shadow Volumes code a little bit and enable it for the Programmable Pipeline in Vortex, however, I had to take a small detour to fix an issue related to mobile device support.</p>
<p>It turns out that OpenGL ES, the 3D library that Vortex uses to render its graphics on mobile devices, does not support rendering indexed geometry for indices larger than 16 bits. Keep this in mind when developing for mobile devices such as the iPhone or iPad.</p>
<p>I can completely understand the reasoning behind this decision. 32-bit indices could be considered too much data to submit to the GPU in a mobile device. Furthermore, they are not strictly necessary, as they could be replaced (if needed) by splitting the geometry into two groups defined by 16-bit indices.</p>
<p>The solution I devised, which is now part of Vortex 2.0, is to allow the user to specify the data size for the indices when defining the geometry. This provides the flexibility to use 32, 16 or 8 bit indices. You can even have several geometric objects with different index sizes in a scene.</p>
<p>The advantage of leveraging this mechanism is that now it is very easy to fine-tune the number of bytes used for index representation for improving performance. For example, using 16-bit indices instead of 32-bit indices would make no difference for representing models composed of less than 65536 vertices, while requiring a copy of just half the number of bytes to the GPU.</p>
<p>In the extreme case of 8-bit indices we would be constrained to only 256 vertices, but we would be sending only one fourth of the data to the GPU.</p>
<p>This was mostly plumbing work, so no new picture this time. Stay tuned for more updates!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.alejandrosegovia.net/2012/01/02/a-short-detour-along-the-way/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Stencil Shadow Volumes</title>
		<link>http://www.alejandrosegovia.net/2011/12/26/stencil-shadow-volumes/</link>
		<comments>http://www.alejandrosegovia.net/2011/12/26/stencil-shadow-volumes/#comments</comments>
		<pubDate>Mon, 26 Dec 2011 08:00:32 +0000</pubDate>
		<dc:creator>Alex</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[OpenGL]]></category>
		<category><![CDATA[Vortex-Engine]]></category>

		<guid isPermaLink="false">http://www.alejandrosegovia.net/?p=114</guid>
		<description><![CDATA[I&#8217;ve built Stencil Shadow Volumes into the Vortex Engine. Shadows are a very interesting feature to implement in an renderer, as they provide important visual cues that help depict the relationship between objects in a scene. Notice in the following image how the shadow tells our brains that the Knight is standing on the floor [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve built Stencil Shadow Volumes into the Vortex Engine.</p>
<p>Shadows are a very interesting feature to implement in an renderer, as they provide important visual cues that help depict the relationship between objects in a scene. Notice in the following image how the shadow tells our brains that the Knight is standing on the floor (as opposed to hovering over it).</p>
<div id="attachment_115" class="wp-caption aligncenter" style="width: 690px"><a href="http://www.alejandrosegovia.net/wp-content/uploads/2011/12/sv3.png"><img class=" wp-image-115 " title="Stencil Shadows" src="http://www.alejandrosegovia.net/wp-content/uploads/2011/12/sv3.png" alt="" width="680" height="900" /></a><p class="wp-caption-text">A Knight, lit by a green light, casts a shadow on a tiled floor.</p></div>
<p>The implementation is at this point no more than a prototype and requires significant more testing, however, since the visual results are very appealing, I wanted to share some of the images.</p>
<p>I personally have a bias towards using Shadows Volumes instead of Shadow Maps; I think the former algoritm leaves out much of the guesswork that Shadow Maps require. Furthermore, Shadow Volumes are not subject to some of the limitations of Shadow Maps, such as the map&#8217;s resolution.</p>
<p>Another point for Shadow Volumes is the fact that they provide a natural way to implement <del datetime="2012-01-26T11:46:12+00:00">&#8220;soft shadows&#8221;:</del> shadows that are not completely black but rather transparent. The following image corresponds to the same scene but with two minor changes: the light has been changed to white and the floor texture is different. Notice how we can see the floor texture englobed in the Knight&#8217;s translucent shadow.</p>
<div id="attachment_116" class="wp-caption aligncenter" style="width: 690px"><a href="http://www.alejandrosegovia.net/wp-content/uploads/2011/12/sv5.png"><img class=" wp-image-116" title="Stencil Shadow Volumes" src="http://www.alejandrosegovia.net/wp-content/uploads/2011/12/sv5.png" alt="" width="680" height="900" /></a><p class="wp-caption-text">The same knight, lit by a white light this time, casts a soft shadow on an industrial floor. Notice the shadow &quot;translucency&quot;.</p></div>
<p>I hope we can have Shadow Volumes available for both rendering pipelines as part of Vortex 2.0.</p>
<p>Edit: Thanks to Gabriel G. for noting the term &#8220;soft shadows&#8221; was being used incorrectly.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.alejandrosegovia.net/2011/12/26/stencil-shadow-volumes/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Objective-C Blocks</title>
		<link>http://www.alejandrosegovia.net/2011/11/28/objective-c-blocks/</link>
		<comments>http://www.alejandrosegovia.net/2011/11/28/objective-c-blocks/#comments</comments>
		<pubDate>Mon, 28 Nov 2011 08:00:06 +0000</pubDate>
		<dc:creator>Alex</dc:creator>
				<category><![CDATA[iOS]]></category>
		<category><![CDATA[Objective-C]]></category>

		<guid isPermaLink="false">http://www.alejandrosegovia.net/?p=112</guid>
		<description><![CDATA[Lately, I have been playing with Objective-C blocks. These are easy to use constructs that enable defining functions &#8220;on the fly&#8221; when programming in Objective-C and are very similar to Python&#8217;s lambdas. The syntax might look a little weird at first, but it is not that different from working with function pointers in C. Here&#8217;s [...]]]></description>
			<content:encoded><![CDATA[<p>Lately, I have been playing with Objective-C blocks.</p>
<p>These are easy to use constructs that enable defining functions &#8220;on the fly&#8221; when programming in Objective-C and are very similar to Python&#8217;s lambdas.</p>
<p>The syntax might look a little weird at first, but it is not that different from working with function pointers in C.</p>
<p>Here&#8217;s a simple example of a function <strong>f</strong> that receives a block <strong>b</strong> as a parameter and calls it. The only restriction imposed on b by f is that it must be a block that takes an int argument and returns an int.</p>
<pre class="brush:cpp">
#include &lt;stdio.h&gt;

// f is a function that receives a block:
void f(int (^b)(int))
{
	b(0); //call the block
}

int main(int argc, char* argv[])
{
	// Define and pass a block to f():
	f(^(int p){ printf("%d\n", p); return 0; });

	return 0;
}
</pre>
<p>The output of this program is just the int supplied by f to the block b.</p>
<p>Let&#8217;s try a more interesting example. Here, as inspired by <a href="http://cocoasamurai.blogspot.com/2011/02/practical-design-patterns-with-blocks.html">this post</a>, we use blocks to define a general-purpose<strong> for_each</strong> function that receives a list of objects and a block and then applies the block on each element of the list.</p>
<p>In order to define a general-purpose for_each function, we take advantage of Objective-C&#8217;s dynamic typing and name our types <strong>id</strong>.</p>
<pre class="brush:cpp">
void for_each(NSArray* array, void (^b)(id))
{
	for (id obj in array)
	{
		b(obj);
	}
}
</pre>
<p>Now, let&#8217;s develop a short program to test the for_each. This program creates a list of strings and uses the for_each function to output all of its contents.</p>
<pre class="brush:cpp">
int main(int argc, char* argv[])
{
	// Default autorelease pool:
	NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];

	// Create an array. The contents could be anything,
	// as long as the block can handle them:
	NSArray* array =
	[NSArray arrayWithObjects:@"a", @"b", @"c", @"d", @"e", nil];

	// Print every string instance:
	for_each(array,
		^(id str){ printf("%s\n", [str UTF8String]); });

	[pool release];

	return 0;
}
</pre>
<p>Suppose now that we wanted to print out strings in uppercase. No problem, we can keep all the same structure, we just need to change the block:</p>
<pre class="brush:cpp">
	for_each(array,
		^(id str){
			printf("%s\n", [[str uppercaseString] UTF8String]);
		});
</pre>
<p>So, what happens if we place an object of a type different from a NSString instance in the array? Well, the for_each is very general and doesn&#8217;t know anything about the array&#8217;s elements or the block&#8217;s internals. Thus, if there is a problem, it will be triggered inside the block code. </p>
<p>Imagine we attempt to send <em>uppercaseString</em> to an object that does not recognize that message. If this happens, an error will be triggered at runtime and abort() will be called, canceling our program.</p>
<p>As we move into dynamic coding, the code becomes more flexible, but we must be more careful not to trigger runtime errors in our programs. It&#8217;s important that we develop our blocks to be consistent with our data structures.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.alejandrosegovia.net/2011/11/28/objective-c-blocks/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Exploring the Android NDK</title>
		<link>http://www.alejandrosegovia.net/2011/11/21/exploring-the-android-ndk/</link>
		<comments>http://www.alejandrosegovia.net/2011/11/21/exploring-the-android-ndk/#comments</comments>
		<pubDate>Mon, 21 Nov 2011 08:00:30 +0000</pubDate>
		<dc:creator>Alex</dc:creator>
				<category><![CDATA[Android]]></category>
		<category><![CDATA[C]]></category>

		<guid isPermaLink="false">http://www.alejandrosegovia.net/?p=107</guid>
		<description><![CDATA[I have been testing the Android development tools. From what I have learned, the tools are separated into two main products: the Android Software Development Kit (SDK) and the Android Native Development Kit (NDK). The SDK was the first development toolkit for Android, and it only allowed applications to be written using the Java programming [...]]]></description>
			<content:encoded><![CDATA[<p>I have been testing the Android development tools. From what I have learned, the tools are separated into two main products: the Android Software Development Kit (SDK) and the Android Native Development Kit (NDK).</p>
<p>The SDK was the first development toolkit for Android, and it only allowed applications to be written using the Java programming language. The NDK was released some time later as a toolchain to enable developers to write parts of their applications using native programming languages (C and C++).</p>
<p>One of the first programs I developed to get a feeling of what the Android SDK looked like was an OpenGL ES App that painted the background using a color degrade. I wrote it a couple of months ago, but today was the first time I ran it on a real Android device.</p>
<p>The resulting image can be seen in the following picture:</p>
<div id="attachment_108" class="wp-caption aligncenter" style="width: 594px"><a href="http://www.alejandrosegovia.net/wp-content/uploads/2011/11/AndroidGL.png"><img src="http://www.alejandrosegovia.net/wp-content/uploads/2011/11/AndroidGL-1024x640.png" alt="" title="Android OpenGL Test" width="584" height="365" class="size-large wp-image-108" /></a><p class="wp-caption-text">Android OpenGL ES Test</p></div>
<p>Other than trying the SDK, what I really wanted to do was to experience how hard it would be to rewrite part of the App in C and then having both integrated. It turns out adding components built using the NDK is not very hard (for pure C code), so I decided to try moving all the rendering code to a plain C function.</p>
<p>I started a new project and coded all the rendering logic in a C function that I called &#8220;<em>render</em>&#8220;. Then, the NDK was used to compile the C code into a JNI-compliant shared library and, finally, I wrote a simple Java wrapper that calls into the shared library&#8217;s <em>render</em> function to do all the drawing.</p>
<p>The wrapper is responsible for creating the Android &#8220;Activity&#8221;, setting up an OpenGL ES context, and calling the native C function. The native C function clears the background and does all the drawing.</p>
<p>Getting all the JNI requirements in place in order to have the Java runtime recognize the native library and call a native method was not too hard, but it was not trivial either. It is definitely much more complicated that calling native libraries from Objective-C or even Python. After a few tries, the bond was made:</p>
<div id="attachment_109" class="wp-caption aligncenter" style="width: 594px"><a href="http://www.alejandrosegovia.net/wp-content/uploads/2011/11/nativeTest.png"><img src="http://www.alejandrosegovia.net/wp-content/uploads/2011/11/nativeTest-1024x640.png" alt="" title="Android Native OpenGL Test" width="584" height="365" class="size-large wp-image-109" /></a><p class="wp-caption-text">Android OpenGL ES rendering from C code. </p></div>
<p>Clearly this is a very simple example where the C code could be tailored to fit JNI&#8217;s requirements from the start. I expect porting an existing C++ codebase to be much more difficult. However, I am looking forward to continue delving into Android&#8217;s development tools.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.alejandrosegovia.net/2011/11/21/exploring-the-android-ndk/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Digital Signage Expo</title>
		<link>http://www.alejandrosegovia.net/2011/11/14/digital-signage-expo/</link>
		<comments>http://www.alejandrosegovia.net/2011/11/14/digital-signage-expo/#comments</comments>
		<pubDate>Mon, 14 Nov 2011 13:54:11 +0000</pubDate>
		<dc:creator>Alex</dc:creator>
				<category><![CDATA[Video]]></category>

		<guid isPermaLink="false">http://www.alejandrosegovia.net/?p=105</guid>
		<description><![CDATA[]]></description>
			<content:encoded><![CDATA[<p><iframe width="560" height="315" src="http://www.youtube.com/embed/vOhiNjAoAD0" frameborder="0" allowfullscreen></iframe></p>
]]></content:encoded>
			<wfw:commentRss>http://www.alejandrosegovia.net/2011/11/14/digital-signage-expo/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>An Angel with Generated Normals</title>
		<link>http://www.alejandrosegovia.net/2011/11/07/an-angel-with-generated-normals/</link>
		<comments>http://www.alejandrosegovia.net/2011/11/07/an-angel-with-generated-normals/#comments</comments>
		<pubDate>Mon, 07 Nov 2011 14:26:08 +0000</pubDate>
		<dc:creator>Alex</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[Vortex-Engine]]></category>

		<guid isPermaLink="false">http://www.alejandrosegovia.net/?p=100</guid>
		<description><![CDATA[Sometimes the 3D models we have to display provide no information other than vertex data for the triangles they define. This might be enough to approximate the shape of the object, but having no normal data, we are severely limited if we want to apply realistic lighting on the object&#8217;s surface. Without lighting and textures, [...]]]></description>
			<content:encoded><![CDATA[<p>Sometimes the 3D models we have to display provide no information other than vertex data for the triangles they define.</p>
<p>This might be enough to approximate the shape of the object, but having no normal data, we are severely limited if we want to apply realistic lighting on the object&#8217;s surface. Without lighting and textures, it&#8217;s very hard to depict the 3D shape of objects.</p>
<div id="attachment_101" class="wp-caption aligncenter" style="width: 690px"><a href="http://www.alejandrosegovia.net/wp-content/uploads/2011/11/angel_no_normals.png"><img src="http://www.alejandrosegovia.net/wp-content/uploads/2011/11/angel_no_normals.png" alt="Angel without Normals" title="Angel without Normals" width="680" height="702" class="size-full wp-image-101" /></a><p class="wp-caption-text">An Angel Model lit but with no Normal Data.</p></div>
<p>To help solve this problem, Vortex now provides a simple Normal generation algorithm that &#8220;deduces&#8221; smooth per-vertex normals from the geometric configuration of the 3D model. The results are nothing short of astonishing.</p>
<div id="attachment_102" class="wp-caption aligncenter" style="width: 690px"><a href="http://www.alejandrosegovia.net/wp-content/uploads/2011/11/angel_generated_normals.png"><img src="http://www.alejandrosegovia.net/wp-content/uploads/2011/11/angel_generated_normals.png" alt="Angel with Generated Normals" title="Angel with Generated Normals" width="680" height="702" class="size-full wp-image-102" /></a><p class="wp-caption-text">The same Angel Model lit after Normal data was generated automatically by Vortex.</p></div>
<p>Normal generation does come at a cost, however. The 3D model depicted in the figures above is composed of 237,018 vertices shared among 474,048 triangles.</p>
<p>In order to produce smooth normals, the generation algorithm must study the triangle adjacency for every vertex and produce exactly one normal for every one.</p>
<p>On the machine the algorithm was developed, a Core 2 Duo @ 2.66 GHz with 4GB of RAM and for the Angel model, this process takes about 841.334 seconds, that is about <strong>14 minutes</strong>!</p>
<p>The good news is that given a model, its Normals only need to be generated once. Once the application has the normal data, it can cache it and reuse it every time the model is to be added to a scene, avoiding the computation time altogether.</p>
<p>This gives the developer the opportunity to generate all Normal data offline and then having it attached to the model at runtime.</p>
<p>The Angel model was downloaded from: <a href="http://www.cc.gatech.edu/projects/large_models/angel.html">http://www.cc.gatech.edu/projects/large_models/angel.html</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.alejandrosegovia.net/2011/11/07/an-angel-with-generated-normals/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Brief Introduction to Objective-C</title>
		<link>http://www.alejandrosegovia.net/2011/11/04/brief-introduction-to-objective-c/</link>
		<comments>http://www.alejandrosegovia.net/2011/11/04/brief-introduction-to-objective-c/#comments</comments>
		<pubDate>Fri, 04 Nov 2011 14:43:12 +0000</pubDate>
		<dc:creator>Alex</dc:creator>
				<category><![CDATA[Objective-C]]></category>

		<guid isPermaLink="false">http://www.alejandrosegovia.net/?p=99</guid>
		<description><![CDATA[Objective-C is a superset of the C programming language that adds object-oriented constructs. Unlike C++, which was influenced by Simula, Objective-C was influenced by Smalltalk, something we can definitely see in its braces syntax. Just like C, Objective-C keeps the tradition of separating the source code in two files: a header that defines an interface [...]]]></description>
			<content:encoded><![CDATA[<p>Objective-C is a superset of the C programming language that adds object-oriented constructs. Unlike C++, which was influenced by Simula, Objective-C was influenced by Smalltalk, something we can definitely see in its braces syntax.</p>
<p>Just like C, Objective-C keeps the tradition of separating the source code in two files: a header that defines an interface (.h) and a source file that provides the implementation of said interfaces (.m).</p>
<p>Contrary to popular belief, you do not need a computer running Mac OS X to develop Objective-C programs. The GNUstep project is an open source implementation of the OpenStep reference that provides a basic runtime environment and an implementation of part of the library, including Strings, Arrays and Dictionaries. GNUstep is available for Linux, other UNIX-based OSs and even Windows.</p>
<p>Here&#8217;s a short example that defines a 2D point structure with two operations: initialize from a given pair of (x,y) coordinates and printing to stdout.</p>
<pre class="brush:cpp">
#import &lt;Foundation/Foundation.h&gt;

@interface Point2D : NSObject
{
	float x;
	float y;
}

- (id) initWithX:(float)xval andY:(float)yval;
- (void) print;

@end
</pre>
<p>Now, we implement this interface in its own file:</p>
<pre class="brush:cpp">

#import "point2d.h"
#include &lt;stdio.h&gt;

@implementation Point2D

- (id) initWithX:(float)xval andY:(float)yval;
{
	self = [super init];
	if (self)
	{
		x = xval;
		y = yval;
	}
	return self;
}

- (void) print
{
	printf("(%.2f, %.2f)\n", x, y);
}

@end
</pre>
<p>Finally, let me show you how to instantiate and send messages to a Point2D object.</p>
<pre class="brush:cpp">
#import "point2d.h"
int main()
{
	NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];

	Point2D* p = [[Point2D alloc] initWithX:1.0f andY:2.0f];

	[p print];
	[p release];

	[pool release];
	return 0;
}
</pre>
<p>Although the Autorelease pool was not really necessary, it is a good practice to have one in place.</p>
<p>If you are trying this example on a Mac, then it&#8217;s relatively simple to get it to compile and run. Using GNUstep it gets a little harder however. Here&#8217;s a simple Makefile for building this application:</p>
<pre class="brush:bash">
CC=gcc -x objective-c

CFLAGS=-I$(GNUSTEP_HOME)/Local/Library/Headers/ -Wall
LDFLAGS=-L$(GNUSTEP_HOME)/Local/Library/Libraries/ -lgnustep-base

all:
	$(CC) $(CFLAGS) $(LDFLAGS) main.m
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.alejandrosegovia.net/2011/11/04/brief-introduction-to-objective-c/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

