<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>Jayway Team Blog</title>
	
	<link>http://blog.jayway.com</link>
	<description>Sharing Experience</description>
	<lastBuildDate>Fri, 05 Mar 2010 11:34:10 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/jayway/posts" /><feedburner:info uri="jayway/posts" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
		<title>Performance nightmare</title>
		<link>http://feedproxy.google.com/~r/jayway/posts/~3/vdNaGzzWxRQ/</link>
		<comments>http://blog.jayway.com/2010/03/05/performance-nightmare/#comments</comments>
		<pubDate>Fri, 05 Mar 2010 06:52:17 +0000</pubDate>
		<dc:creator>Björn Carlsson</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[Tips & Tricks]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://blog.jayway.com/?p=4810</guid>
		<description><![CDATA[22 to 0.3 seconds!
I found a simple solution to a very common problem. While profiling I found that remove in this little method took a lot of the time.
   public void MovePropertyFirst(IProperty property)
   {
      properties.Remove(property);
      properties.Insert(0, property);
   }
The reason [...]]]></description>
			<content:encoded><![CDATA[<p>22 to 0.3 seconds!</p>
<p>I found a simple solution to a very common problem. While profiling I found that remove in this little method took a lot of the time.</p>
<pre>   public void MovePropertyFirst(IProperty property)
   {
      properties.Remove(property);
      properties.Insert(0, property);
   }</pre>
<p>The reason was, that it's time consuming to compare the properties by equality, for complex types. I think that in most (?) cases we want to delete the very same instance we supply as parameter to the remove method.</p>
<p>So I implemented a ReferenceEquals based remove, and by that I cut the time spent in remove by about 72 times!</p>
<pre>  public static bool RemoveFrom&lt;T&gt;(this IList&lt;T&gt; list, T itemToRemove)
  {
    if(list.Count == 0)
      return false;
    int index = 0;
    foreach (var item in list)
    {
      if (ReferenceEquals(item, itemToRemove))
      {
        list.RemoveAt(index);
        return true;
      }
      index++;
    }
    Debug.Assert(false, "Item to delete not found");
    //equality based fall back
    return list.Remove(itemToRemove);
  }</pre>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/jayway/posts?a=vdNaGzzWxRQ:RIdM0HRgN1o:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/jayway/posts?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/jayway/posts?a=vdNaGzzWxRQ:RIdM0HRgN1o:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/jayway/posts?i=vdNaGzzWxRQ:RIdM0HRgN1o:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/jayway/posts?a=vdNaGzzWxRQ:RIdM0HRgN1o:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/jayway/posts?d=yIl2AUoC8zA" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/jayway/posts/~4/vdNaGzzWxRQ" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blog.jayway.com/2010/03/05/performance-nightmare/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<feedburner:origLink>http://blog.jayway.com/2010/03/05/performance-nightmare/</feedburner:origLink></item>
		<item>
		<title>OpenGL ES Tutorial for Android – Part V – More on Meshes</title>
		<link>http://feedproxy.google.com/~r/jayway/posts/~3/E08WMsyM9TY/</link>
		<comments>http://blog.jayway.com/2010/02/15/opengl-es-tutorial-for-android-%e2%80%93-part-v/#comments</comments>
		<pubDate>Mon, 15 Feb 2010 21:12:20 +0000</pubDate>
		<dc:creator>Per-Erik Bergman</dc:creator>
				<category><![CDATA[Android]]></category>
		<category><![CDATA[Embedded]]></category>
		<category><![CDATA[opengl es]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://blog.jayway.com/?p=2590</guid>
		<description><![CDATA[I have a feeling that some of you have tried my tutorials and then thought "This is a 3D tutorial, but why is everything in 2D?". So in this tutorial we will make some real 3D meshes. This is also necessary for the following tutorials. 
When I started I had problems with finding out how [...]]]></description>
			<content:encoded><![CDATA[<p>I have a feeling that some of you have tried my tutorials and then thought "This is a 3D tutorial, but why is everything in 2D?". So in this tutorial we will make some real 3D meshes. This is also necessary for the following tutorials. </p>
<p>When I started I had problems with finding out how to programmatic make different meshes like cubes, cones and so on. I needed this so I easy easy could put my scenes together. So this tutorial will show how to make some of the basic primitives. They might not be the most effective way of creating them but it is a way of doing them.</p>
<p>Starting point will be from the source of the second tutorial. I will show you plane and cube and then give you a couple of hint for additional primitives.</p>
<h2>Design</h2>
<p>A good place to start when designing an OpenGL framework is to use the composite pattern. This is a start of how I would proceed:</p>
<p><a href="http://blog.jayway.com/wordpress/wp-content/uploads/2010/02/composite.png" rel="lightbox"><img src="http://blog.jayway.com/wordpress/wp-content/uploads/2010/02/composite.png" alt="" title="composite" width="593" height="216" class="aligncenter size-full wp-image-4795" /></a></p>
<p>Let's start making out pattern.</p>
<h2>Mesh</h2>
<p>It's a good idea to have a common base for your meshes. So let us start by creating a class called Mesh.</p>
<pre class="java"><span style="color: #000000; font-weight: bold;">package</span> se.<span style="color: #006600;">jayway</span>.<span style="color: #006600;">opengl</span>.<span style="color: #006600;">tutorial</span>.<span style="color: #006600;">mesh</span>;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Mesh <span style="color: #66cc66;">&#123;</span>
&nbsp;
<span style="color: #66cc66;">&#125;</span></pre>
<p>We add the draw function from previous example, since I when over this function in a previous tutorial I just show it here:</p>
<pre class="java">    <span style="color: #808080; font-style: italic;">// Our vertex buffer.</span>
    <span style="color: #000000; font-weight: bold;">private</span> FloatBuffer verticesBuffer = <span style="color: #000000; font-weight: bold;">null</span>;
&nbsp;
    <span style="color: #808080; font-style: italic;">// Our index buffer.</span>
    <span style="color: #000000; font-weight: bold;">private</span> ShortBuffer indicesBuffer = <span style="color: #000000; font-weight: bold;">null</span>;
&nbsp;
    <span style="color: #808080; font-style: italic;">// The number of indices.</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #993333;">int</span> numOfIndices = <span style="color: #cc66cc;">-1</span>;
&nbsp;
    <span style="color: #808080; font-style: italic;">// Flat Color</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #993333;">float</span><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> rgba = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #993333;">float</span><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#123;</span><span style="color: #cc66cc;">1</span>.0f, <span style="color: #cc66cc;">1</span>.0f, <span style="color: #cc66cc;">1</span>.0f, <span style="color: #cc66cc;">1</span>.0f<span style="color: #66cc66;">&#125;</span>;
&nbsp;
    <span style="color: #808080; font-style: italic;">// Smooth Colors</span>
    <span style="color: #000000; font-weight: bold;">private</span> FloatBuffer colorBuffer = <span style="color: #000000; font-weight: bold;">null</span>;
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> draw<span style="color: #66cc66;">&#40;</span>GL10 gl<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
        <span style="color: #808080; font-style: italic;">// Counter-clockwise winding.</span>
	gl.<span style="color: #006600;">glFrontFace</span><span style="color: #66cc66;">&#40;</span>GL10.<span style="color: #006600;">GL_CCW</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #808080; font-style: italic;">// Enable face culling.</span>
	gl.<span style="color: #006600;">glEnable</span><span style="color: #66cc66;">&#40;</span>GL10.<span style="color: #006600;">GL_CULL_FACE</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #808080; font-style: italic;">// What faces to remove with the face culling.</span>
	gl.<span style="color: #006600;">glCullFace</span><span style="color: #66cc66;">&#40;</span>GL10.<span style="color: #006600;">GL_BACK</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #808080; font-style: italic;">// Enabled the vertices buffer for writing and to be used during</span>
	<span style="color: #808080; font-style: italic;">// rendering.</span>
	gl.<span style="color: #006600;">glEnableClientState</span><span style="color: #66cc66;">&#40;</span>GL10.<span style="color: #006600;">GL_VERTEX_ARRAY</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #808080; font-style: italic;">// Specifies the location and data format of an array of vertex</span>
	<span style="color: #808080; font-style: italic;">// coordinates to use when rendering.</span>
	gl.<span style="color: #006600;">glVertexPointer</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">3</span>, GL10.<span style="color: #006600;">GL_FLOAT</span>, <span style="color: #cc66cc;">0</span>, verticesBuffer<span style="color: #66cc66;">&#41;</span>;
        <span style="color: #808080; font-style: italic;">// Set flat color</span>
        gl.<span style="color: #006600;">glColor4f</span><span style="color: #66cc66;">&#40;</span>rgba<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span>, rgba<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span>, rgba<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#93;</span>, rgba<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #808080; font-style: italic;">// Smooth color</span>
        <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span> colorBuffer != <span style="color: #000000; font-weight: bold;">null</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
            <span style="color: #808080; font-style: italic;">// Enable the color array buffer to be used during rendering.</span>
            gl.<span style="color: #006600;">glEnableClientState</span><span style="color: #66cc66;">&#40;</span>GL10.<span style="color: #006600;">GL_COLOR_ARRAY</span><span style="color: #66cc66;">&#41;</span>;
            <span style="color: #808080; font-style: italic;">// Point out the where the color buffer is.</span>
            gl.<span style="color: #006600;">glColorPointer</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">4</span>, GL10.<span style="color: #006600;">GL_FLOAT</span>, <span style="color: #cc66cc;">0</span>, colorBuffer<span style="color: #66cc66;">&#41;</span>;
        <span style="color: #66cc66;">&#125;</span>
	gl.<span style="color: #006600;">glDrawElements</span><span style="color: #66cc66;">&#40;</span>GL10.<span style="color: #006600;">GL_TRIANGLES</span>, numOfIndices,
		GL10.<span style="color: #006600;">GL_UNSIGNED_SHORT</span>, indicesBuffer<span style="color: #66cc66;">&#41;</span>;
	<span style="color: #808080; font-style: italic;">// Disable the vertices buffer.</span>
	gl.<span style="color: #006600;">glDisableClientState</span><span style="color: #66cc66;">&#40;</span>GL10.<span style="color: #006600;">GL_VERTEX_ARRAY</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #808080; font-style: italic;">// Disable face culling.</span>
	gl.<span style="color: #006600;">glDisable</span><span style="color: #66cc66;">&#40;</span>GL10.<span style="color: #006600;">GL_CULL_FACE</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span></pre>
<p>We need functions where the subclasses can set the vertices and the indices. These function contains nothing new and are pretty much the same as you seen in earlier tutorials.</p>
<pre class="java">    <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #993333;">void</span> setVertices<span style="color: #66cc66;">&#40;</span><span style="color: #993333;">float</span><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> vertices<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">// a float is 4 bytes, therefore we multiply the number if</span>
	<span style="color: #808080; font-style: italic;">// vertices with 4.</span>
	ByteBuffer vbb = ByteBuffer.<span style="color: #006600;">allocateDirect</span><span style="color: #66cc66;">&#40;</span>vertices.<span style="color: #006600;">length</span> * <span style="color: #cc66cc;">4</span><span style="color: #66cc66;">&#41;</span>;
	vbb.<span style="color: #006600;">order</span><span style="color: #66cc66;">&#40;</span>ByteOrder.<span style="color: #006600;">nativeOrder</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
	verticesBuffer = vbb.<span style="color: #006600;">asFloatBuffer</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
	verticesBuffer.<span style="color: #006600;">put</span><span style="color: #66cc66;">&#40;</span>vertices<span style="color: #66cc66;">&#41;</span>;
	verticesBuffer.<span style="color: #006600;">position</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #993333;">void</span> setIndices<span style="color: #66cc66;">&#40;</span><span style="color: #993333;">short</span><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> indices<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">// short is 2 bytes, therefore we multiply the number if</span>
	<span style="color: #808080; font-style: italic;">// vertices with 2.</span>
	ByteBuffer ibb = ByteBuffer.<span style="color: #006600;">allocateDirect</span><span style="color: #66cc66;">&#40;</span>indices.<span style="color: #006600;">length</span> * <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span>;
	ibb.<span style="color: #006600;">order</span><span style="color: #66cc66;">&#40;</span>ByteOrder.<span style="color: #006600;">nativeOrder</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
	indicesBuffer = ibb.<span style="color: #006600;">asShortBuffer</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
	indicesBuffer.<span style="color: #006600;">put</span><span style="color: #66cc66;">&#40;</span>indices<span style="color: #66cc66;">&#41;</span>;
	indicesBuffer.<span style="color: #006600;">position</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>;
	numOfIndices = indices.<span style="color: #006600;">length</span>;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #993333;">void</span> setColor<span style="color: #66cc66;">&#40;</span><span style="color: #993333;">float</span> red, <span style="color: #993333;">float</span> green, <span style="color: #993333;">float</span> blue, <span style="color: #993333;">float</span> alpha<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
        <span style="color: #808080; font-style: italic;">// Setting the flat color.</span>
        rgba<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span> = red;
        rgba<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span> = green;
        rgba<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#93;</span> = blue;
        rgba<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#93;</span> = alpha;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #993333;">void</span> setColors<span style="color: #66cc66;">&#40;</span><span style="color: #993333;">float</span><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> colors<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">// float has 4 bytes.</span>
	ByteBuffer cbb = ByteBuffer.<span style="color: #006600;">allocateDirect</span><span style="color: #66cc66;">&#40;</span>colors.<span style="color: #006600;">length</span> * <span style="color: #cc66cc;">4</span><span style="color: #66cc66;">&#41;</span>;
	cbb.<span style="color: #006600;">order</span><span style="color: #66cc66;">&#40;</span>ByteOrder.<span style="color: #006600;">nativeOrder</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
	colorBuffer = cbb.<span style="color: #006600;">asFloatBuffer</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
	colorBuffer.<span style="color: #006600;">put</span><span style="color: #66cc66;">&#40;</span>colors<span style="color: #66cc66;">&#41;</span>;
	colorBuffer.<span style="color: #006600;">position</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span></pre>
<p>We need to add a couple of things. When we start working with multiple meshes we need to be able to move and rotate them individual so let us add translation and rotation parameters:</p>
<pre class="java">    <span style="color: #808080; font-style: italic;">// Translate params.</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">float</span> x = <span style="color: #cc66cc;">0</span>;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">float</span> y = <span style="color: #cc66cc;">0</span>;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">float</span> z = <span style="color: #cc66cc;">0</span>;
&nbsp;
    <span style="color: #808080; font-style: italic;">// Rotate params.</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">float</span> rx = <span style="color: #cc66cc;">0</span>;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">float</span> ry = <span style="color: #cc66cc;">0</span>;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">float</span> rz = <span style="color: #cc66cc;">0</span>;</pre>
<p>And use them in the draw function add this lines just before the gl.glDrawElements call.</p>
<pre class="java">    gl.<span style="color: #006600;">glTranslatef</span><span style="color: #66cc66;">&#40;</span>x, y, z<span style="color: #66cc66;">&#41;</span>;
    gl.<span style="color: #006600;">glRotatef</span><span style="color: #66cc66;">&#40;</span>rx, <span style="color: #cc66cc;">1</span>, <span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>;
    gl.<span style="color: #006600;">glRotatef</span><span style="color: #66cc66;">&#40;</span>ry, <span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">1</span>, <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>;
    gl.<span style="color: #006600;">glRotatef</span><span style="color: #66cc66;">&#40;</span>rz, <span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>;</pre>
<h2>Plane</h2>
<p>Let us start making a plane an quite easy task you might think and it kinda is. But to make it more interesting and more useful we need to be able to create it with some different settings like: width, depth, how many width segments and how many depth segments. </p>
<p>Just so we have the same terminology, width is the length over the x-axis, depth is over the z-axis and height is over the y-axis. Look at the image below as a visual input.<br />
<div id="attachment_4695" class="wp-caption alignright" style="width: 270px"><a href="http://blog.jayway.com/wordpress/wp-content/uploads/2010/02/terminology_1.png" rel="lightbox"><img src="http://blog.jayway.com/wordpress/wp-content/uploads/2010/02/terminology_1.png" alt="Width, height and depth." title="Width, height and depth." width="260" height="252" class="size-full wp-image-4695" /></a><p class="wp-caption-text">Width, height and depth.</p></div></p>
<p>Segments is how many parts the length should be divided by. This is useful if you need to make a surface that is not total even. If you create a plane over x, y and make z not all be 0 say you give z a random span from -0.1 to 0.1 you will get something you could use as a ground plane in a game just put a nice texture on it.<br />
<div id="attachment_4696" class="wp-caption alignright" style="width: 270px"><a href="http://blog.jayway.com/wordpress/wp-content/uploads/2010/02/terminology_2.png" rel="lightbox"><img src="http://blog.jayway.com/wordpress/wp-content/uploads/2010/02/terminology_2.png" alt="Segments." title="Segments." width="260" height="252" class="size-full wp-image-4696" /></a><p class="wp-caption-text">Segments.</p></div></p>
<p>Looking at the image above you see that the different segments gives you squares. Since we like it to be triangles so just split them up into 2 triangles.</p>
<p>I hate frameworks and classes that don't have a default setup and easy class constructors I try to always have more then one constructor. The constructors I will put in this plane is:</p>
<p>For an easy and quick setup:</p>
<pre class="java"><span style="color: #808080; font-style: italic;">// Gives you a plane that is 1 unit wide and 1 unit high with just one segment over width and height.</span>
<span style="color: #000000; font-weight: bold;">public</span> Plane<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span></pre>
<p>An easy one just to change the size:</p>
<pre class="java"> <span style="color: #808080; font-style: italic;">// Let you decide the size of the plane but still only one segment.</span>
<span style="color: #000000; font-weight: bold;">public</span> Plane<span style="color: #66cc66;">&#40;</span><span style="color: #993333;">float</span> width, <span style="color: #993333;">float</span> height<span style="color: #66cc66;">&#41;</span></pre>
<p>And finally one for setting up the plane with different segments:</p>
<pre class="java"><span style="color: #808080; font-style: italic;">// For alla your settings.</span>
<span style="color: #000000; font-weight: bold;">public</span> Plane<span style="color: #66cc66;">&#40;</span><span style="color: #993333;">float</span> width, <span style="color: #993333;">float</span> height, <span style="color: #993333;">int</span> widthSegments, <span style="color: #993333;">int</span> heightSegments<span style="color: #66cc66;">&#41;</span></pre>
<p>If I in theory would construct a plane that is 1 unit wide and 1 units high with 4 segments in both width and height direction it would look like this images:<br />
<a href="http://blog.jayway.com/wordpress/wp-content/uploads/2010/02/plane.png" rel="lightbox"><img src="http://blog.jayway.com/wordpress/wp-content/uploads/2010/02/plane-300x133.png" alt="" title="plane" width="300" height="133" class="aligncenter size-medium wp-image-4755" /></a><br />
The one to the left shows the segments and the one to the right show us the faces we need to create.</p>
<pre class="java"><span style="color: #000000; font-weight: bold;">package</span> se.<span style="color: #006600;">jayway</span>.<span style="color: #006600;">opengl</span>.<span style="color: #006600;">tutorial</span>.<span style="color: #006600;">mesh</span>;
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Plane <span style="color: #000000; font-weight: bold;">extends</span> Mesh <span style="color: #66cc66;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> Plane<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">this</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span>, <span style="color: #cc66cc;">1</span>, <span style="color: #cc66cc;">1</span>, <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> Plane<span style="color: #66cc66;">&#40;</span><span style="color: #993333;">float</span> width, <span style="color: #993333;">float</span> height<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">this</span><span style="color: #66cc66;">&#40;</span>width, height, <span style="color: #cc66cc;">1</span>, <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> Plane<span style="color: #66cc66;">&#40;</span><span style="color: #993333;">float</span> width, <span style="color: #993333;">float</span> height, <span style="color: #993333;">int</span> widthSegments,
		<span style="color: #993333;">int</span> heightSegments<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
	<span style="color: #993333;">float</span><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> vertices = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #993333;">float</span><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#40;</span>widthSegments + <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span> * <span style="color: #66cc66;">&#40;</span>heightSegments + <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>
			* <span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#93;</span>;
	<span style="color: #993333;">short</span><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> indices = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #993333;">short</span><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#40;</span>widthSegments + <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span> * <span style="color: #66cc66;">&#40;</span>heightSegments + <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>
			* <span style="color: #cc66cc;">6</span><span style="color: #66cc66;">&#93;</span>;
&nbsp;
	<span style="color: #993333;">float</span> xOffset = width / <span style="color: #cc66cc;">-2</span>;
	<span style="color: #993333;">float</span> yOffset = height / <span style="color: #cc66cc;">-2</span>;
	<span style="color: #993333;">float</span> xWidth = width / <span style="color: #66cc66;">&#40;</span>widthSegments<span style="color: #66cc66;">&#41;</span>;
	<span style="color: #993333;">float</span> yHeight = height / <span style="color: #66cc66;">&#40;</span>heightSegments<span style="color: #66cc66;">&#41;</span>;
	<span style="color: #993333;">int</span> currentVertex = <span style="color: #cc66cc;">0</span>;
	<span style="color: #993333;">int</span> currentIndex = <span style="color: #cc66cc;">0</span>;
	<span style="color: #993333;">short</span> w = <span style="color: #66cc66;">&#40;</span><span style="color: #993333;">short</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>widthSegments + <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span><span style="color: #993333;">int</span> y = <span style="color: #cc66cc;">0</span>; y &lt; heightSegments + <span style="color: #cc66cc;">1</span>; y++<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
            <span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span><span style="color: #993333;">int</span> x = <span style="color: #cc66cc;">0</span>; x &lt; widthSegments + <span style="color: #cc66cc;">1</span>; x++<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
	        vertices<span style="color: #66cc66;">&#91;</span>currentVertex<span style="color: #66cc66;">&#93;</span> = xOffset + x * xWidth;
		vertices<span style="color: #66cc66;">&#91;</span>currentVertex + <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span> = yOffset + y * yHeight;
		vertices<span style="color: #66cc66;">&#91;</span>currentVertex + <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#93;</span> = <span style="color: #cc66cc;">0</span>;
		currentVertex += <span style="color: #cc66cc;">3</span>;
&nbsp;
		<span style="color: #993333;">int</span> n = y * <span style="color: #66cc66;">&#40;</span>widthSegments + <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span> + x;
&nbsp;
		<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>y &lt; heightSegments &amp;&amp; x &lt; widthSegments<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
		    <span style="color: #808080; font-style: italic;">// Face one</span>
		    indices<span style="color: #66cc66;">&#91;</span>currentIndex<span style="color: #66cc66;">&#93;</span> = <span style="color: #66cc66;">&#40;</span><span style="color: #993333;">short</span><span style="color: #66cc66;">&#41;</span> n;
		    indices<span style="color: #66cc66;">&#91;</span>currentIndex + <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span> = <span style="color: #66cc66;">&#40;</span><span style="color: #993333;">short</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>n + <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>;
		    indices<span style="color: #66cc66;">&#91;</span>currentIndex + <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#93;</span> = <span style="color: #66cc66;">&#40;</span><span style="color: #993333;">short</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>n + w<span style="color: #66cc66;">&#41;</span>;
		    <span style="color: #808080; font-style: italic;">// Face two</span>
		    indices<span style="color: #66cc66;">&#91;</span>currentIndex + <span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#93;</span> = <span style="color: #66cc66;">&#40;</span><span style="color: #993333;">short</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>n + <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>;
		    indices<span style="color: #66cc66;">&#91;</span>currentIndex + <span style="color: #cc66cc;">4</span><span style="color: #66cc66;">&#93;</span> = <span style="color: #66cc66;">&#40;</span><span style="color: #993333;">short</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>n + <span style="color: #cc66cc;">1</span> + w<span style="color: #66cc66;">&#41;</span>;
		    indices<span style="color: #66cc66;">&#91;</span>currentIndex + <span style="color: #cc66cc;">5</span><span style="color: #66cc66;">&#93;</span> = <span style="color: #66cc66;">&#40;</span><span style="color: #993333;">short</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>n + <span style="color: #cc66cc;">1</span> + w - <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		    currentIndex += <span style="color: #cc66cc;">6</span>;
		<span style="color: #66cc66;">&#125;</span>
	    <span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	setIndices<span style="color: #66cc66;">&#40;</span>indices<span style="color: #66cc66;">&#41;</span>;
	setVertices<span style="color: #66cc66;">&#40;</span>vertices<span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre>
<h2>Cube</h2>
<p>The next step I think a cube will be nice. I will only make a cube that you can set: height, width and depth on but I suggest you as a practice make it with segments just as we did with the plane. </p>
<p>The constructor will look like this:</p>
<pre class="java"><span style="color: #000000; font-weight: bold;">public</span> Cube<span style="color: #66cc66;">&#40;</span><span style="color: #993333;">float</span> width, <span style="color: #993333;">float</span> height, <span style="color: #993333;">float</span> depth<span style="color: #66cc66;">&#41;</span></pre>
<p>And since I'm not doing this with any segments the constructor will be quite easy.</p>
<pre class="java"><span style="color: #000000; font-weight: bold;">package</span> se.<span style="color: #006600;">jayway</span>.<span style="color: #006600;">opengl</span>.<span style="color: #006600;">tutorial</span>.<span style="color: #006600;">mesh</span>;
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Cube <span style="color: #000000; font-weight: bold;">extends</span> Mesh <span style="color: #66cc66;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> Cube<span style="color: #66cc66;">&#40;</span><span style="color: #993333;">float</span> width, <span style="color: #993333;">float</span> height, <span style="color: #993333;">float</span> depth<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
        width  /= <span style="color: #cc66cc;">2</span>;
        height /= <span style="color: #cc66cc;">2</span>;
        depth  /= <span style="color: #cc66cc;">2</span>;
&nbsp;
        <span style="color: #993333;">float</span> vertices<span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> = <span style="color: #66cc66;">&#123;</span> -width, -height, -depth, <span style="color: #808080; font-style: italic;">// 0</span>
                              width, -height, -depth, <span style="color: #808080; font-style: italic;">// 1</span>
                              width,  height, -depth, <span style="color: #808080; font-style: italic;">// 2</span>
                             -width,  height, -depth, <span style="color: #808080; font-style: italic;">// 3</span>
                             -width, -height,  depth, <span style="color: #808080; font-style: italic;">// 4</span>
                              width, -height,  depth, <span style="color: #808080; font-style: italic;">// 5</span>
                              width,  height,  depth, <span style="color: #808080; font-style: italic;">// 6</span>
                             -width,  height,  depth, <span style="color: #808080; font-style: italic;">// 7</span>
        <span style="color: #66cc66;">&#125;</span>;
&nbsp;
        <span style="color: #993333;">short</span> indices<span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> = <span style="color: #66cc66;">&#123;</span> <span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">4</span>, <span style="color: #cc66cc;">5</span>,
                            <span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">5</span>, <span style="color: #cc66cc;">1</span>,
                            <span style="color: #cc66cc;">1</span>, <span style="color: #cc66cc;">5</span>, <span style="color: #cc66cc;">6</span>,
                            <span style="color: #cc66cc;">1</span>, <span style="color: #cc66cc;">6</span>, <span style="color: #cc66cc;">2</span>,
                            <span style="color: #cc66cc;">2</span>, <span style="color: #cc66cc;">6</span>, <span style="color: #cc66cc;">7</span>,
                            <span style="color: #cc66cc;">2</span>, <span style="color: #cc66cc;">7</span>, <span style="color: #cc66cc;">3</span>,
                            <span style="color: #cc66cc;">3</span>, <span style="color: #cc66cc;">7</span>, <span style="color: #cc66cc;">4</span>,
                            <span style="color: #cc66cc;">3</span>, <span style="color: #cc66cc;">4</span>, <span style="color: #cc66cc;">0</span>,
                            <span style="color: #cc66cc;">4</span>, <span style="color: #cc66cc;">7</span>, <span style="color: #cc66cc;">6</span>,
                            <span style="color: #cc66cc;">4</span>, <span style="color: #cc66cc;">6</span>, <span style="color: #cc66cc;">5</span>,
                            <span style="color: #cc66cc;">3</span>, <span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">1</span>,
                            <span style="color: #cc66cc;">3</span>, <span style="color: #cc66cc;">1</span>, <span style="color: #cc66cc;">2</span>, <span style="color: #66cc66;">&#125;</span>;
&nbsp;
        setIndices<span style="color: #66cc66;">&#40;</span>indices<span style="color: #66cc66;">&#41;</span>;
        setVertices<span style="color: #66cc66;">&#40;</span>vertices<span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre>
<p>If you like to make it with segments the constructor could look like this:</p>
<pre class="java"><span style="color: #000000; font-weight: bold;">public</span> Cube<span style="color: #66cc66;">&#40;</span><span style="color: #993333;">float</span> width, <span style="color: #993333;">float</span> height, <span style="color: #993333;">float</span> depth,
                 <span style="color: #993333;">int</span> widthSegments, <span style="color: #993333;">int</span> heightSegments, <span style="color: #993333;">int</span> depthSegments<span style="color: #66cc66;">&#41;</span></pre>
<p>Since we now have a plane that replaces the Square class ( in the code from tutorial II ) I will just remove it and in OpenGLRenderer change the square to a cube...</p>
<pre class="java"><span style="color: #000000; font-weight: bold;">public</span> OpenGLRenderer<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
    <span style="color: #808080; font-style: italic;">// Initialize our cube.</span>
    cube = <span style="color: #000000; font-weight: bold;">new</span> Cube<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span>, <span style="color: #cc66cc;">1</span>, <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>;
    cube.<span style="color: #006600;">rx</span> = <span style="color: #cc66cc;">45</span>;
    cube.<span style="color: #006600;">ry</span> = <span style="color: #cc66cc;">45</span>;
<span style="color: #66cc66;">&#125;</span></pre>
<p>... and render it.</p>
<pre class="java"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> onDrawFrame<span style="color: #66cc66;">&#40;</span>GL10 gl<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
    ...
    <span style="color: #808080; font-style: italic;">// Draw our cube.</span>
    cube.<span style="color: #006600;">draw</span><span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre>
<h2>Group</h2>
<p>A group is really good to have when setting up and controlling your 3D scene. What a group really do is to distribute all commands sent to the group to all it's children. You can see the implementation of a simple group here:</p>
<pre class="java"><span style="color: #000000; font-weight: bold;">package</span> se.<span style="color: #006600;">jayway</span>.<span style="color: #006600;">opengl</span>.<span style="color: #006600;">tutorial</span>.<span style="color: #006600;">mesh</span>;
&nbsp;
<span style="color: #a1a100;">import java.util.Vector;</span>
&nbsp;
<span style="color: #a1a100;">import javax.microedition.khronos.opengles.GL10;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AGroup+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">Group</span></a> <span style="color: #000000; font-weight: bold;">extends</span> Mesh <span style="color: #66cc66;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</span> Vector&lt;Mesh&gt; children = <span style="color: #000000; font-weight: bold;">new</span> Vector&lt;Mesh&gt;<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> draw<span style="color: #66cc66;">&#40;</span>GL10 gl<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
        <span style="color: #993333;">int</span> size = children.<span style="color: #006600;">size</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #b1b100;">for</span><span style="color: #66cc66;">&#40;</span> <span style="color: #993333;">int</span> i = <span style="color: #cc66cc;">0</span>; i &lt; size; i++<span style="color: #66cc66;">&#41;</span>
            children.<span style="color: #006600;">get</span><span style="color: #66cc66;">&#40;</span>i<span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">draw</span><span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> add<span style="color: #66cc66;">&#40;</span><span style="color: #993333;">int</span> location, Mesh object<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
        children.<span style="color: #006600;">add</span><span style="color: #66cc66;">&#40;</span>location, object<span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">boolean</span> add<span style="color: #66cc66;">&#40;</span>Mesh object<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> children.<span style="color: #006600;">add</span><span style="color: #66cc66;">&#40;</span>object<span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> clear<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
        children.<span style="color: #006600;">clear</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> Mesh get<span style="color: #66cc66;">&#40;</span><span style="color: #993333;">int</span> location<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> children.<span style="color: #006600;">get</span><span style="color: #66cc66;">&#40;</span>location<span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> Mesh remove<span style="color: #66cc66;">&#40;</span><span style="color: #993333;">int</span> location<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> children.<span style="color: #006600;">remove</span><span style="color: #66cc66;">&#40;</span>location<span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">boolean</span> remove<span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AObject+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">Object</span></a> object<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> children.<span style="color: #006600;">remove</span><span style="color: #66cc66;">&#40;</span>object<span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">int</span> size<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> children.<span style="color: #006600;">size</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre>
<p>Make the renderer work with a group as a root node and add your cube to it.</p>
<pre class="java"><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AGroup+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">Group</span></a> group = <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AGroup+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">Group</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
Cube cube = <span style="color: #000000; font-weight: bold;">new</span> Cube<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span>, <span style="color: #cc66cc;">1</span>, <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>;
cube.<span style="color: #006600;">rx</span> = <span style="color: #cc66cc;">45</span>;
cube.<span style="color: #006600;">ry</span> = <span style="color: #cc66cc;">45</span>;
group.<span style="color: #006600;">add</span><span style="color: #66cc66;">&#40;</span>cube<span style="color: #66cc66;">&#41;</span>;
root = group;</pre>
<p>And draw our scene:</p>
<pre class="java"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> onDrawFrame<span style="color: #66cc66;">&#40;</span>GL10 gl<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
    ...
    <span style="color: #808080; font-style: italic;">// Draw our scene.</span>
    root.<span style="color: #006600;">draw</span><span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre>
<h2>Suggestions</h2>
<p>It's always a good idea to have different primitives ready to use when you starting up a new project. My experience tell me that in 9 times of 10 you won't have any meshes from the graphic people when you start coding so it's really good to have some meshes to work with as place holders. I'll give you a hint of the way to start with your own meshes library by giving you an idea of how I would do it.</p>
<p>Creating your own meshes is a really good way of getting to know vertices and indices really close up.</p>
<h3>Cone</h3>
<p>After you have gotten your cube up and ready to go my suggestion is that you move onto a cone. A cone with the right settings could be more then just a cone. if you give is 3-4 sides it will be a pyramid. If you give it the same base and top radius it becomes a cylinder. So you can see why it is so useful. Take a look at this image and see what the this cone can do.<br />
<a href="http://blog.jayway.com/wordpress/wp-content/uploads/2010/02/cpc.png" rel="lightbox"><img src="http://blog.jayway.com/wordpress/wp-content/uploads/2010/02/cpc-300x109.png" alt="" title="cpc" width="300" height="109" class="aligncenter size-medium wp-image-4759" /></a></p>
<pre class="java"><span style="color: #000000; font-weight: bold;">public</span> Cone<span style="color: #66cc66;">&#40;</span><span style="color: #993333;">float</span> baseRadius, <span style="color: #993333;">float</span> topRadius, <span style="color: #993333;">float</span> height, <span style="color: #993333;">int</span> numberOfSides<span style="color: #66cc66;">&#41;</span></pre>
<h3>Pyramid</h3>
<pre class="java"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Pyramid <span style="color: #000000; font-weight: bold;">extends</span> Cone <span style="color: #66cc66;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> Pyramid<span style="color: #66cc66;">&#40;</span><span style="color: #993333;">float</span> baseRadius, <span style="color: #993333;">float</span> height<span style="color: #66cc66;">&#41;</span>  <span style="color: #66cc66;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">super</span><span style="color: #66cc66;">&#40;</span>baseRadius, <span style="color: #cc66cc;">0</span>, height, <span style="color: #cc66cc;">4</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre>
<h3>Cylinder</h3>
<pre class="java"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Cylinder <span style="color: #000000; font-weight: bold;">extends</span> Cone <span style="color: #66cc66;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> Cylinder<span style="color: #66cc66;">&#40;</span><span style="color: #993333;">float</span> radius, <span style="color: #993333;">float</span> height<span style="color: #66cc66;">&#41;</span>  <span style="color: #66cc66;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">super</span><span style="color: #66cc66;">&#40;</span>radius, radius, height, <span style="color: #cc66cc;">16</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre>
<h3>One more thing</h3>
<p>Dividing up surfaces is a good thing to know about and by now you know how to divide up a regular square. To divide up a triangle look at the images below. It is a bit different and it might be a bit harder to implement.<br />
<a href="http://blog.jayway.com/wordpress/wp-content/uploads/2010/02/triangle_seg.png" rel="lightbox"><img src="http://blog.jayway.com/wordpress/wp-content/uploads/2010/02/triangle_seg-300x154.png" alt="" title="triangle_seg" width="300" height="154" class="aligncenter size-medium wp-image-4761" /></a></p>
<h2>References</h2>
<p>The info used in this tutorial is collected from:<br />
<a href="http://developer.android.com/">Android Developers</a><br />
<a href="http://www.khronos.org/opengles/sdk/1.1/docs/man/">OpenGL ES 1.1 Reference Pages</a></p>
<p>You can download the source for this tutorial here: <a href='http://blog.jayway.com/wordpress/wp-content/uploads/2010/02/Tutorial_Part_V.zip'>Tutorial_Part_V</a></p>
<p>Previous tutorial: <a href="http://blog.jayway.com/2010/01/14/opengl-es-tutorial-for-android-%E2%80%93-part-iv-adding-colors/">OpenGL ES Tutorial for Android – Part IV – Adding colors</a><br />
Next tutorial: <a href=""></a></p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/jayway/posts?a=E08WMsyM9TY:9-toIwoc524:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/jayway/posts?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/jayway/posts?a=E08WMsyM9TY:9-toIwoc524:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/jayway/posts?i=E08WMsyM9TY:9-toIwoc524:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/jayway/posts?a=E08WMsyM9TY:9-toIwoc524:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/jayway/posts?d=yIl2AUoC8zA" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/jayway/posts/~4/E08WMsyM9TY" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blog.jayway.com/2010/02/15/opengl-es-tutorial-for-android-%e2%80%93-part-v/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		<feedburner:origLink>http://blog.jayway.com/2010/02/15/opengl-es-tutorial-for-android-%e2%80%93-part-v/</feedburner:origLink></item>
		<item>
		<title>Building OSGi Bundles with Scala and Gradle</title>
		<link>http://feedproxy.google.com/~r/jayway/posts/~3/AkHDzOTj29c/</link>
		<comments>http://blog.jayway.com/2010/02/09/building-osgi-bundles-with-scala-and-gradle/#comments</comments>
		<pubDate>Tue, 09 Feb 2010 19:56:02 +0000</pubDate>
		<dc:creator>Michael Kober</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[build systems]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[scala]]></category>

		<guid isPermaLink="false">http://blog.jayway.com/?p=4712</guid>
		<description><![CDATA[There already some good blog posts about how to build OSGi bundles in Scala, among others "An OSGi Bundle… built in Scala" of Neil Bartlett and "OSGi, Maven and Scala" of Gavin Bong using pax-construct. Here is another alternative using Gradle. Gradle is an open-source build system providing the expressiveness of a Groovy based DSL [...]]]></description>
			<content:encoded><![CDATA[<p>There already some good blog posts about how to build OSGi bundles in Scala, among others <a href="http://neilbartlett.name/blog/2007/04/06/an-osgi-bundle-built-in-scala/">"An OSGi Bundle… built in Scala"</a> of Neil Bartlett and <a href="http://www.raverun.com/writings/tech/scalaosgi/">"OSGi, Maven and Scala"</a> of Gavin Bong using pax-construct. Here is another alternative using <a href="http://gradle.org">Gradle</a>. Gradle is an open-source build system providing the expressiveness of a Groovy based DSL together with build-by-convention support for the major build scenarios in the Java/Groovy world. Above that there is even support for Scala.</p>
<p>If you want to follow the example you need:<br />
gradle 0.8<br />
scala 2.7.7<br />
apache felix framework 2.0.1 (or your favourite OSGi container)</p>
<p>Instead of a HelloWorld I'll take Neill's BundleActivator example that prints out a list of currently installed bundles when started.</p>
<pre class="brush:scala">
package com.jayway.osgi.hello

import org.osgi.framework._

class Activator extends BundleActivator {  

    def start( context: BundleContext ) {
          var bundleNames = context.getBundles()
                   .map   (b => b.getSymbolicName())
                   .filter(b => b != context.getBundle())
          println("Installed bundles: " + bundleNames.toString())
     }    

     def stop( context: BundleContext )  {
     }
 }
</pre>
<p>Gradle comes with both Scala and OSGi plugins that make it quite easy to build the bundle. The Scala plugin assumes the same standard project layout as used in Maven, i.e java production code will reside in the 'src/main/java', scala production code in the 'src/main/scala' folder. So I put the Activator class in 'src/main/scala' and add the following build.gradle file in my project folder:</p>
<pre class="brush:groovy">
group = 'com.jayway'
version = '1.0.0'

usePlugin 'scala'
usePlugin 'osgi'

repositories {
  mavenCentral()  // http://repo1.maven.org/maven2/
  mavenRepo(urls: 'http://www.ibiblio.org/maven2/')
  mavenRepo(urls: 'http://scala-tools.org/repo-releases/')
}

dependencies {
    // Libraries needed to run the scala tools
    scalaTools 'org.scala-lang:scala-compiler:2.7.7'
    scalaTools 'org.scala-lang:scala-library:2.7.7'

    compile 'org.scala-lang:scala-library:2.7.7'
    // osgi framework
    compile 'org.apache.felix:org.apache.felix.framework:2.0.1'
}

configure(jar.osgi) {
    // generate the bundle manifest
    version = '1.0.0'
    name = 'scala osgi bundle built with gradle'
    symbolicName = 'com.jayway.osgi.hello'
    instruction 'Require-Bundle', 'scala.library'
    instruction 'Bundle-Activator', 'com.jayway.osgi.hello.Activator'
    instruction 'Import-Package', 'org.osgi.framework'
    instruction 'Export-Package', 'com.jayway.osgi.hello'
    instruction 'Bundle-Vendor', 'JAYWAY AB'
}
</pre>
<p>The Gradle OSGi plugin makes use of the <a href="http://www.aqute.biz/Code/Bnd">BND tool</a> to generate the bundle manifest. I added the scala library OSGi bundle as required and imported the package org.osgi.framework.  Now I'm good to go and call gradle from the commandline:</p>
<pre>
C:\ws-scala-test\com.jayway.osgi.hello>gradle build
</pre>
<h2>Running on apache felix 2.0.1</h2>
<p>Now start the osgi container and install the bundles starting with the scala library as OSGi bundle: </p>
<pre>
C:\felix-framework-2.0.1>java -jar bin/felix.jar

Welcome to Felix
================

->install http://www.scala-lang.org/downloads/distrib/files/scala.library_2.7.7.final.jar
Bundle ID: 24

-> install file:C:\ws-scala-test\com.jayway.osgi.hello\build\libs\com.jayway.osgi.hello-1.0.0.jar
Bundle ID: 25

-> ps
START LEVEL 1
   ID   State         Level  Name
[   0] [Active     ] [    0] System Bundle (2.0.1)
[   1] [Active     ] [    1] Apache Felix Bundle Repository (1.4.2)
[   2] [Active     ] [    1] Apache Felix Shell Service (1.4.1)
[   3] [Active     ] [    1] Apache Felix Shell TUI (1.4.1)
[  24] [Resolved   ] [    1] Scala Library (2.7.7.final)
[  25] [Installed  ] [    1] scala osgi bundle built with gradle (1.0.0)

-> start 25
Installed bundles: Array(org.apache.felix.framework, org.apache.felix.bundlerepository, org.apache.felix.shell, org.apa
he.felix.shell.tui, scala.library, com.jayway.osgi.hello)
</pre>
<h2>Running on eclipse equinox 3.5.1</h2>
<p>If you want to use equinox instead of felix you could change the dependencies like this:</p>
<pre class="brush:groovy">
compile 'org.eclipse:osgi:3.5.1-R35x_v20090827'
</pre>
<p>I had some problems to find a maven repository containing the current equinox osgi bundles (3.5.1). If you are running maven you can publish the bundles from your eclipse installation to your local maven repository:</p>
<pre>
mvn eclipse:to-maven -DeclipseDir="[eclipse-installation-dir]"
</pre>
<p>Finally add your local repository to your build.gradle:</p>
<pre class="brush:groovy">
mavenRepo(urls: 'file://[path-to-user-home]/.m2/repository/')
</pre>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/jayway/posts?a=AkHDzOTj29c:5ED3TKC3-24:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/jayway/posts?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/jayway/posts?a=AkHDzOTj29c:5ED3TKC3-24:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/jayway/posts?i=AkHDzOTj29c:5ED3TKC3-24:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/jayway/posts?a=AkHDzOTj29c:5ED3TKC3-24:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/jayway/posts?d=yIl2AUoC8zA" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/jayway/posts/~4/AkHDzOTj29c" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blog.jayway.com/2010/02/09/building-osgi-bundles-with-scala-and-gradle/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<feedburner:origLink>http://blog.jayway.com/2010/02/09/building-osgi-bundles-with-scala-and-gradle/</feedburner:origLink></item>
		<item>
		<title>Continuos Integration for XCode projects</title>
		<link>http://feedproxy.google.com/~r/jayway/posts/~3/JLi5fPJzsHQ/</link>
		<comments>http://blog.jayway.com/2010/01/31/continuos-integration-for-xcode-projects/#comments</comments>
		<pubDate>Sun, 31 Jan 2010 17:35:59 +0000</pubDate>
		<dc:creator>Christian Hedin</dc:creator>
				<category><![CDATA[Agile]]></category>
		<category><![CDATA[Testing]]></category>
		<category><![CDATA[automated testing]]></category>
		<category><![CDATA[hudson]]></category>
		<category><![CDATA[objective-c]]></category>
		<category><![CDATA[tools]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[xcode]]></category>

		<guid isPermaLink="false">http://blog.jayway.com/?p=4624</guid>
		<description><![CDATA[Continuos Integration is the practice of integrating changes from many people as often as possible. Instead of merging changes once a month and spending time handling merge errors you try integrate every day, perhaps even every hour. Each integration is built and tested on a server. If there are build errors or test failures, you [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://martinfowler.com/articles/continuousIntegration.html" title="Continuous Integration">Continuos Integration</a> is the practice of integrating changes from many people as often as possible. Instead of merging changes once a month and spending time handling merge errors you try integrate every day, perhaps even every hour. Each integration is built and tested on a server. If there are build errors or test failures, you and your team will be notified right away.
</p>
<p>
This is the second part of the blog post I wrote about <a href="http://blog.jayway.com/2010/01/15/test-driven-development-in-xcode/">TDD in XCode</a>. Make sure you've followed that tutorial before continuing here, or at least have a project of your own with OCUnit tests ready.
</p>
<p>
The server which we'll use is called <a href="https://hudson.dev.java.net/" title="Hudson">Hudson</a> and is very popular in the Java community. It does not readily support XCode projects but it does allow shell commands, so we'll use that to make our Calculator project compatible. We also want to fetch new versions of the source code as soon as it's checked in. We'll be a bit fancy and use <a href="http://git-scm.com/" title="Git - Fast Version Control System">Git</a> as source control software.	Here's a summary of what we want to do:
</p>
<ol>
<li>Download and install the Hudson continuos integration server</li>
<li>Download, install and setup the Git source control software as well as the Git plugin for Hudson</li>
<li>Download and install a script that converts OCUnit test results to JUnit format</li>
<li>Setup a new job in Hudson that checks out our XCode project and builds and tests it</li>
<li>Set up a notification and try it out</li>
</ol>
<h2>Downloading and Installing Hudson</h2>
<p>
In order to keep the instructions in this tutorial simple, we will have the source code repository and the build server itself on localhost. In real life you'll probably want to setup a dedicated machine.</p>
<p>Head over to the <a href="https://hudson.dev.java.net/" title="Hudson">Hudson web site</a> and download your copy of the server. Follow their included installation instructions, it's quite simple. Once you have Hudson up and running you can visit <a href="http://localhots:8080">http://localhost:8080</a> to see the welcome screen. By the way, when you're ready to install Hudson on a real remote server I recommend running it as a Servlet inside <a href="http://tomcat.apache.org/" title="Apache Tomcat - Welcome!">Apache Tomcat</a> instead.
</p>
<h2>Downloading and Setting Up Git</h2>
<p>Next we want to install Git. There are several ways to install it. The simplest might be to use <a href="http://code.google.com/p/git-osx-installer/">the OS X installer</a>, but I personally prefer <a href="http://www.macports.org/" title="The MacPorts Project">MacPorts</a>. Either way, go ahead and install it and make sure that you can use the "git" command from Terminal.app when you're done.
</p>
<p>
In the Terminal, change directory to your XCode project folder and type:</p>
<pre class="brush:bash">
		git init
	</pre>
<p>You now have a working source code repository in your project folder. Neat. Before you continue create a text file called <em>.gitignore</em> and let it contain:</p>
<pre class="brush:plain">
		build/*
		*.pbxuser
		*.mode1v3
		.DS_Store
		test-reports/*
	</pre>
<p>	That's a listing of the files and folders that should not be version controlled, since they are temporary or just used for XCode. Feel free to add any other files that you think only should be present locally. Now add all files (that aren't ignored) using:</p>
<pre class="brush:bash">
		git add *
	</pre>
<p>	and finally commit them to a first version using:</p>
<pre class="brush:bash">
		git commit -m "Initial import"
	</pre>
</p>
<h2>Fetching the Script</h2>
<p>
We need to fetch one more thing, <a href="http://github.com/ciryon/OCUnit2JUnit/blob/master/ocunit2junit.rb">ocunit2junit.rb</a>, which is a little Ruby script that I wrote. Since you now have git, you can also download it using git (or <em>clone the repo</em>, which is the correct term). Change directory to /tmp and then type like this:</p>
<pre class="brush:bash">git clone git://github.com/ciryon/OCUnit2JUnit.git</pre>
<p>Copy the script from /tmp/OCUnit2JUnit/ anywhere, like to /usr/local/bin/. Make sure it's executable by typing <em>chmod +x ocunit2junit.rb</em>.</p>
<p>The script will parse output from xcodebuild (the console command for building your XCode project) and look for output from OCUnit. The test results (success, failure, time passed etc) will be saved in the "test-reports" folder in the same XML format that JUnit uses. This also happens to be a format that Hudson understands.
</p>
<h2>Setting Up Hudson</h2>
<p>
Now head back to your Hudson welcome screen. Select <em>Manage Hudson</em>, <em>Manage plugins</em> and check "Hudson GIT plugin" under <em>Available</em>. Hudson will automatically download and install the plugin. Afterwards just restart Hudson.
</p>
<p>
We're ready to create a job in Hudson for our project. From the main screen select "New job", Build a free-style software project and call it "Calculator". There are some settings which you should fill in:</p>
<ul>
<li>
			Select to use git as source code management tool. The URL should be the file path to your project, like:
<pre class="brush:bash">/Users/youruser/XCodeProjects/Calculator/</pre>
</li>
<li>
			Select to poll SCM with Schedule:
<pre class="brush:bash">* * * * *</pre>
<p>			That means it'll check every minute if there are changes. If you want to check less often, do that.
		</li>
<li>
			Add new build step and select to execute shell. Use this command (make sure to point to the version of the iPhone SDK you want to use):</p>
<pre class="brush:plain">xcodebuild -target "UnitTests" -sdk /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator3.1.2.sdk/ -configuration "Debug" | /usr/local/bin/ocunit2junit.rb</pre>
</li>
<li>
			Select to also publish a JUnit test result report and the Test report XMLs reside at
<pre class="brush:plain">test-reports/*.xml</pre>
</li>
</ul>
<h2>Trying it out</h2>
<p>This is the moment of truth. Try to change your CalculatorTest.m so that one of the test cases fail. Type:</p>
<pre class="brush:bash">
	git -a commit CalculatorTest.m "Broke the test, on purpose"
</pre>
<p>and see what happens. If all goes well, and you have entered everything correctly,  Hudson should automatically notice there's a new version available, build it and try to run the test suite. Check the results and how it reports the test failure. Nice, eh? Fix the error and commit the test class again to see how Hudson reacts.
</p>
<p>
There are many settings, plugins and cool things to try out in Hudson. Go into Manage Hudson-> Configure system and setup your email settings, to allow Hudson to send mail. Then go back to configure your Calculator job and at the bottom select E-mail Notification. Enter your own email address and see how it works after you commits a file that breaks the build. If you don't want a mail, you can get an instant message, or perhaps have the server play a loud annoying sound? Everyone in your team should be aware of when a build fails, so it can be fixed right away. This is one of the great advantages of having a continuos integration server.</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/jayway/posts?a=JLi5fPJzsHQ:teoxWYHubN0:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/jayway/posts?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/jayway/posts?a=JLi5fPJzsHQ:teoxWYHubN0:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/jayway/posts?i=JLi5fPJzsHQ:teoxWYHubN0:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/jayway/posts?a=JLi5fPJzsHQ:teoxWYHubN0:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/jayway/posts?d=yIl2AUoC8zA" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/jayway/posts/~4/JLi5fPJzsHQ" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blog.jayway.com/2010/01/31/continuos-integration-for-xcode-projects/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		<feedburner:origLink>http://blog.jayway.com/2010/01/31/continuos-integration-for-xcode-projects/</feedburner:origLink></item>
		<item>
		<title>Maven, FindBugs and Dashboard Reports</title>
		<link>http://feedproxy.google.com/~r/jayway/posts/~3/iFPI6J-V04A/</link>
		<comments>http://blog.jayway.com/2010/01/29/maven-findbugs-and-dashboard-reports/#comments</comments>
		<pubDate>Fri, 29 Jan 2010 20:02:59 +0000</pubDate>
		<dc:creator>Davor Crnomat</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Testing]]></category>
		<category><![CDATA[Tips & Tricks]]></category>
		<category><![CDATA[maven]]></category>
		<category><![CDATA[tools]]></category>

		<guid isPermaLink="false">http://blog.jayway.com/?p=4571</guid>
		<description><![CDATA[There are a few simple steps to get nice graphic presentations of FindBugs results using Maven.

First, to enable FindBugs reporting in Maven, just add report section to your pom files, something like example below, but of course, you can do your own configuration.]]></description>
			<content:encoded><![CDATA[<p>There are a few simple steps to get nice graphic presentations of FindBugs results using Maven.</p>
<p>First, to enable FindBugs reporting in Maven, just add report section to your pom files, something like example below, but of course, you can do your own configuration.</p>
<pre class="brush:xml">
&lt;reporting&gt;
  &lt;plugins&gt;
    &lt;plugin&gt;
      &lt;groupId&gt;org.codehaus.mojo&lt;/groupId&gt;
      &lt;artifactId&gt;findbugs-maven-plugin&lt;/artifactId&gt;
      &lt;version&gt;2.3&lt;/version&gt;
      &lt;configuration&gt;
        &lt;excludeFilterFile&gt;exclude.xml&lt;/excludeFilterFile&gt;
        &lt;findbugsXmlOutput&gt;true&lt;/findbugsXmlOutput&gt;
        &lt;findbugsXmlWithMessages&gt;true&lt;/findbugsXmlWithMessages&gt;
        &lt;threshold&gt;Low&lt;/threshold&gt;
        &lt;effort&gt;Max&lt;/effort&gt;
        &lt;xmlOutput&gt;true&lt;/xmlOutput&gt;
      &lt;/configuration&gt;
    &lt;/plugin&gt;
  &lt;/plugins&gt;
&lt;/reporting&gt;
</pre>
<p>Then you can just use command <strong>mvn site </strong>if you want to generate<strong> </strong>more comprehensive project information or <strong>mvn findbugs:findbugs </strong>for only FindBugs reports.</p>
<p>Finally use <strong>mvn dashboard:dashboard</strong> to generate charts.</p>
<p>Voila. We have got nice graphic reports.<br />
Well, not so nice. We have bugs we need to fix.</p>
<p><a href="http://blog.jayway.com/wordpress/wp-content/uploads/2010/01/findbugs_report.jpg" rel="lightbox"><img class="size-full wp-image-4572 alignleft" title="findbugs_report" src="http://blog.jayway.com/wordpress/wp-content/uploads/2010/01/findbugs_report.jpg" alt="FindBugs report charts" width="621" height="686" /></a></p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/jayway/posts?a=iFPI6J-V04A:rZWxtl1R94o:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/jayway/posts?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/jayway/posts?a=iFPI6J-V04A:rZWxtl1R94o:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/jayway/posts?i=iFPI6J-V04A:rZWxtl1R94o:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/jayway/posts?a=iFPI6J-V04A:rZWxtl1R94o:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/jayway/posts?d=yIl2AUoC8zA" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/jayway/posts/~4/iFPI6J-V04A" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blog.jayway.com/2010/01/29/maven-findbugs-and-dashboard-reports/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		<feedburner:origLink>http://blog.jayway.com/2010/01/29/maven-findbugs-and-dashboard-reports/</feedburner:origLink></item>
		<item>
		<title>Sneaky throw</title>
		<link>http://feedproxy.google.com/~r/jayway/posts/~3/KmssAHbTnrQ/</link>
		<comments>http://blog.jayway.com/2010/01/29/sneaky-throw/#comments</comments>
		<pubDate>Fri, 29 Jan 2010 15:10:03 +0000</pubDate>
		<dc:creator>Jan Kronquist</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[exception]]></category>
		<category><![CDATA[jayview]]></category>
		<category><![CDATA[tricks]]></category>

		<guid isPermaLink="false">http://blog.jayway.com/?p=4517</guid>
		<description><![CDATA[The latest issue of our magazine Jayview is out with brand new look and feel. My contribution was a cool piece of code that gets rid of those annoying exceptions. However, during layouting the link to the source disappeared. Credit should of course go to Reinier Zwitserloot and his mail on Java Posse. 

public class [...]]]></description>
			<content:encoded><![CDATA[<p>The latest issue of <a href="http://jayway.se/jayview">our magazine Jayview</a> is out with brand new look and feel. My contribution was a cool piece of code that gets rid of those annoying exceptions. However, during layouting the link to the source disappeared. Credit should of course go to <a href="http://zwitserloot.com/">Reinier Zwitserloot</a> and <a href="http://www.mail-archive.com/javaposse@googlegroups.com/msg05984.html">his mail on Java Posse</a>. </p>
<pre class="brush:java">
public class Sneak {
    public static RuntimeException sneakyThrow(Throwable t) {
        if ( t == null ) throw new NullPointerException("t");
        Sneak.&lt;RuntimeException&gt;sneakyThrow0(t);
        return null;
    }

    @SuppressWarnings("unchecked")
    private static &lt;T extends Throwable&gt; void sneakyThrow0(Throwable t) throws T {
        throw (T)t;
    }
}
</pre>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/jayway/posts?a=KmssAHbTnrQ:XruAs6WgF7s:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/jayway/posts?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/jayway/posts?a=KmssAHbTnrQ:XruAs6WgF7s:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/jayway/posts?i=KmssAHbTnrQ:XruAs6WgF7s:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/jayway/posts?a=KmssAHbTnrQ:XruAs6WgF7s:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/jayway/posts?d=yIl2AUoC8zA" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/jayway/posts/~4/KmssAHbTnrQ" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blog.jayway.com/2010/01/29/sneaky-throw/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://blog.jayway.com/2010/01/29/sneaky-throw/</feedburner:origLink></item>
		<item>
		<title>iPad, the Future, and the Luxury of Starting Over</title>
		<link>http://feedproxy.google.com/~r/jayway/posts/~3/0paO_Ki1jWI/</link>
		<comments>http://blog.jayway.com/2010/01/28/ipad-the-future-and-the-luxury-of-starting-over/#comments</comments>
		<pubDate>Thu, 28 Jan 2010 20:02:59 +0000</pubDate>
		<dc:creator>Fredrik Olsson</dc:creator>
				<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[Events]]></category>
		<category><![CDATA[apple]]></category>
		<category><![CDATA[cocoa touch]]></category>
		<category><![CDATA[frameworks]]></category>
		<category><![CDATA[innovation]]></category>
		<category><![CDATA[ipad]]></category>
		<category><![CDATA[iphone]]></category>

		<guid isPermaLink="false">http://blog.jayway.com/?p=4550</guid>
		<description><![CDATA[A luxury that you seldom have in the world of software development is the luxury of starting over. I am not talking about throwing away everything and start from scratch. But just taking what you have, and all the experiences learned. Apply some major refactoring to make what works really shine, and without care of [...]]]></description>
			<content:encoded><![CDATA[<p>A luxury that you seldom have in the world of software development is the luxury of starting over. I am not talking about throwing away everything and start from scratch. But just taking what you have, and all the experiences learned. Apply some major refactoring to make what works really shine, and without care of dependent clients throw out everything that turned out to be irreparable.</p>
<p>The <a href="http://www.apple.com/ipad/">iPad</a> as presented by Apple on the <a href="http://www.apple.com/ipad/">special january 2010 event</a> is a showcase of how Apple has been given this luxury. Not just once but over and over again. Allowing for iterating, and improving with each restart.</p>
<h3>The origins</h3>
<p>The evolution of the software running the iPad can be traced back along with <a href="http://en.wikipedia.org/wiki/Steve_Jobs">Steve Jobs</a> himself. With the first <a href="http://en.wikipedia.org/wiki/Macintosh">Macintosh</a> from 1984 he was one of the many talented people behind ushering the graphical user interface into the world of normal people. </p>
<p>Not entirely by his own choice he founded <a href="http://en.wikipedia.org/wiki/NeXT">NeXT</a> in 1985, and got the luxury of starting over, equipped with the experience from the Macintosh project. The lessons learned resulted in the NeXT computers in 1989, a system way ahead of it's time, with <a href="http://en.wikipedia.org/wiki/NeXTSTEP">NextStep's</a> object oriented frameworks, before OOP had even caught on with the main stream. Remember this was before <a href="http://en.wikipedia.org/wiki/Windows_3.0">Windows 3.0</a>.</p>
<p>In late 1996 Apple bought NeXT and all their assets, including Steve Jobs as the CEO. Now the experience learned from developing the NeXT computers and the NextStep operating system could be used again. With the luxury of a reset once again, throwing out anything that did not work, and clean up the parts that worked well. The new operating system rising from the ashes would be named <a href="http://en.wikipedia.org/wiki/Mac_OS_X">Mac OS X</a>, and the application development frameworks be called <a href="http://developer.apple.com/cocoa/">Cocoa</a>.</p>
<h3>The tech as used today</h3>
<p>Cocoa is the primary application development framework for Mac OS X to this day. It is an umbrella framework, containing:</p>
<ul>
<li><a href="http://developer.apple.com/mac/library/navigation/index.html#section=Frameworks&topic=Foundation">Foundation</a> - For the non-visual low level components.</li>
<li><a href="http://developer.apple.com/mac/library/navigation/index.html#section=Frameworks&topic=Application%20Kit">AppKit</a> - For the components to create a desktop application with mouse and keyboard</li>
<li><a href="http://developer.apple.com/mac/library/navigation/index.html#section=Frameworks&topic=Core%20Data">Core Data</a> - For managing application data as a graph database.</li>
</ul>
<p>The application development framework for <a href="http://en.wikipedia.org/wiki/IPhone_OS">iPhone OS</a> is called <a href="http://developer.apple.com/iphone/library/navigation/index.html#section=Frameworks&topic=Cocoa%20Touch%20Layer">Cocoa Touch</a>. It is basically all the nice parts from Foundation, and since iPhone OS 3.0 also Core Data. But AppKit is missing, instead replaced by <a href="http://developer.apple.com/iphone/library/navigation/index.html#section=Frameworks&topic=UIKit">UIKit</a>. UIKit are the components needed to create a graphical user interface based on multitouch. Not a dumbed down version of AppKit, but a rewrite that is re-using the experience from AppKit. Refining what works well, removing what did not turn out so well, and adding what could not easily be added to AppKit.</p>
<p>Apple gave themselves the luxury of starting over yet again, not burdened by backward compaitibility. The new form factor of the iPhone can not inspire any realistic hopes for application compatibility with the Mac computers. </p>
<h3>Where can this lead?</h3>
<p>Mac OS X and the Cocoa frameworks where good from a tech standpoint, and for us nerds. But not ready for prime-time by the average user until at least version 10.2, or 10.3 if you are really honest. Many dismissed the first versions of Mac OS X, while others saw the potential. I see the same thing with iPad and iPhone OS, it will be dismissed for now, and others will see a great potential.</p>
<p>I think that the iPad is Apple's vision for the future of computing. Mice to be replaced by multi-touch. The keyword being <strong>multi</strong>-touch. A touch device is to a multi-touch device what a black and white TV is to a modern color HD TV. Sure they both display pictures, the basic idea is there, but the execution is on a different level. In 1984 with the first Macintosh a computer mouse was a novelty. Apple want to replace the secondary input of the mouse, with direct input and manipulation using your very fingers. Making the mouse computer mouse a parenthesis in the history of computing.</p>
<p><strong>The iPhone OS is the successor to Mac OS X.</strong></p>
<p>There I said it. There will never be a Mac OX X 11.0, and probably not a Mac OS X 10.8 either. That line of succession is over, the new blood line to reign is the offspring that is iPhone OS. A fresh start, a luxury in our industry, and also a necessity in order to take the great leaps to the future.</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/jayway/posts?a=0paO_Ki1jWI:h0mDBJHRRDI:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/jayway/posts?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/jayway/posts?a=0paO_Ki1jWI:h0mDBJHRRDI:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/jayway/posts?i=0paO_Ki1jWI:h0mDBJHRRDI:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/jayway/posts?a=0paO_Ki1jWI:h0mDBJHRRDI:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/jayway/posts?d=yIl2AUoC8zA" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/jayway/posts/~4/0paO_Ki1jWI" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blog.jayway.com/2010/01/28/ipad-the-future-and-the-luxury-of-starting-over/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		<feedburner:origLink>http://blog.jayway.com/2010/01/28/ipad-the-future-and-the-luxury-of-starting-over/</feedburner:origLink></item>
		<item>
		<title>The easy way to test Android applications</title>
		<link>http://feedproxy.google.com/~r/jayway/posts/~3/omuyxWxESl8/</link>
		<comments>http://blog.jayway.com/2010/01/28/the-easy-way-to-test-android-applications-2/#comments</comments>
		<pubDate>Thu, 28 Jan 2010 09:44:44 +0000</pubDate>
		<dc:creator>Renas Reda</dc:creator>
				<category><![CDATA[Android]]></category>
		<category><![CDATA[Testing]]></category>
		<category><![CDATA[automated testing]]></category>
		<category><![CDATA[frameworks]]></category>
		<category><![CDATA[junit]]></category>
		<category><![CDATA[open source]]></category>
		<category><![CDATA[tools]]></category>

		<guid isPermaLink="false">http://blog.jayway.com/?p=4536</guid>
		<description><![CDATA[I’m going to guess that most of you know what instrumentation is. In the event that you don't, instrumentation is a feature in which specific monitoring of the interactions between an application and the system is made possible. Instrumentation also makes it possible to write test cases that interact with the application. The problem with [...]]]></description>
			<content:encoded><![CDATA[<p>I’m going to guess that most of you know what instrumentation is. In the event that you don't, instrumentation is a feature in which specific monitoring of the interactions between an application and the system is made possible. Instrumentation also makes it possible to write test cases that interact with the application. The problem with instrumentation, however, is that it is incredibly hard to write solid test cases for applications bigger than the typical “Hello World!” application. A tremendous amount of technical details must be taken into account in order to write a good test case. Often, developers quickly realize that it will take almost as long to write a comprehensive test case as it took to write the whole application. I, myself, came to recognize the very same thing when I first started looking into how to use instrumentation tests with the android application project that I’m currently working on.</p>
<p>I soon came to understand that I would not be able to take advantage of all the wonderful possibilities that instrumentation offers. The reason for that is quite simple; the application that we are in the process of developing is not only extensive but also complicated with multiple activities, self-defined intents, and hundreds of views that also include scrollable lists. It would not make sense for me to spend a month writing one single test case that would only take 20 seconds to test manually. That is how Robotium-Solo was born. I needed a test framework that would help me write good and powerful test cases that emulated real users. The test case should be able to do what a real user does: click on anything that is clickable, look for irregularities, automatically move from activity to activity, etc. More importantly, I should not have to spend more than 10 minutes writing a test case that involves more then one activity.</p>
<p>With the help of Robotium-Solo a test case spanning over multiple activities could look like this:</p>
<pre class="brush:java">public void testTextIsSaved() throws Exception {
   solo.clickOnText("Other");
   solo.clickOnButton("Edit");
   assertTrue(solo.searchText("Edit Window"));
   solo.enterText(0, "Some text for testing purposes")
   solo.clickOnButton("Save");
   assertTrue(solo.searchText("Changes have been made successfully"));
   solo.clickOnButton("Ok");
   assertTrue(solo.searchText("Some text for testing purposes"));
}
</pre>
<p>As you can see, I don’t have to specify any technical details or tell Robotium-Solo where to look for something, such as scrolling down a list when needed. It handles the above and more all on its own.</p>
<p>If you are interested in writing test cases of similar nature have a look at http://www.robotium.org. It makes writing powerful test cases a breeze.</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/jayway/posts?a=omuyxWxESl8:s0Uv8-z6yq4:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/jayway/posts?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/jayway/posts?a=omuyxWxESl8:s0Uv8-z6yq4:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/jayway/posts?i=omuyxWxESl8:s0Uv8-z6yq4:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/jayway/posts?a=omuyxWxESl8:s0Uv8-z6yq4:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/jayway/posts?d=yIl2AUoC8zA" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/jayway/posts/~4/omuyxWxESl8" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blog.jayway.com/2010/01/28/the-easy-way-to-test-android-applications-2/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<feedburner:origLink>http://blog.jayway.com/2010/01/28/the-easy-way-to-test-android-applications-2/</feedburner:origLink></item>
		<item>
		<title>Boosting Android performance using JNI</title>
		<link>http://feedproxy.google.com/~r/jayway/posts/~3/mNyz3BXFjYA/</link>
		<comments>http://blog.jayway.com/2010/01/25/boosting-android-performance-using-jni/#comments</comments>
		<pubDate>Mon, 25 Jan 2010 13:03:12 +0000</pubDate>
		<dc:creator>Mattias Rosberg</dc:creator>
				<category><![CDATA[Android]]></category>
		<category><![CDATA[Embedded]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://blog.jayway.com/?p=4390</guid>
		<description><![CDATA[JNI or Java Native Interface is the interface between the Java code running in a JVM and the native code running outside the JVM. It works both ways, that is you can use JNI to call native code from your Java programs and to call Java code from your native code. The native code normally [...]]]></description>
			<content:encoded><![CDATA[<p>JNI or Java Native Interface is the interface between the Java code running in a JVM and the native code running outside the JVM. It works both ways, that is you can use JNI to call native code from your Java programs and to call Java code from your native code. The native code normally resides within a library (.so file) and is typically written in C/C++.</p>
<p>There's a few reasons when to use JNI in a Java program</p>
<ul>
<li><strong>Wrap lower level functionality</strong> - Classes in the Android Application Framework uses JNI to interface with the underlying platform and hardware like Camera, Sensors and GPS.</li>
<li><strong>Interface native legacy code</strong> - You might have some old legacy code written in C/C++ and you don't want to waste your time porting the code to Java. With JNI you can create an interface class in Java that exposes the functionality of your legacy code.</li>
<li><strong>Bypass performance bottlenecks</strong> - Execute heavy number crunching in native code and get rid of the overhead that the instruction interpretation in the JVM introduces.
</ul>
<p>On Android, in order to prevent fragmentation, we are only allowed to use the following libraries in our native code.</p>
<ul>
<li>libc (C library) headers</li>
<li>libm (math library) headers</li>
<li>JNI interface headers</li>
<li>libz (Zlib compression) headers</li>
<li>liblog (Android logging) header</li>
<li>OpenGL ES 1.1 (3D graphics library) headers (since 1.6)</li>
<li>A Minimal set of headers for C++ support</li>
</ul>
<p>In this blog I will demonstrate how to transform a time consuming Java method with a lot of number crunching into a native declared method where the real work is performed in native code. Here's the time consuming Java method</p>
<pre class="brush:java">
public double compare(int[] sourceData,int[] targetData, double targetError) {
  double error = 0.0D;
  for (int index = 0; index < targetData.length; index++) {
    int c1 = sourceData[index];
    int c2 = targetData[index];
    int b = (c1 >> 16 & 255) - (c2 >> 16 & 255);
    int g = (c1 >> 8 & 255) - (c2 >> 8 & 255);
    int r = (c1 & 255) - (c2 & 255);
    error += r * r + g * g + b * b;
    if (error > targetError)
      return error;
  }
  return error;
}
</pre>
<p>The <strong>sourceData</strong> and <strong>targetData</strong> arguments represents the pixels of two Bitmaps. In short the method calculates the sum of the square distance in color between two images, pixel by pixel. If we compare two 200x200 pixels images the <strong>for-loop</strong> will run 40000 times! This is a typical candidate for when to use JNI.</p>
<h3>What to do in Native</h3>
<p>You can use the <a href="http://blog.jayway.com/wordpress/wp-content/uploads/2010/01/boilerplate.c">boilerplate.c</a> file as boilerplate code for all your native c functions. </p>
<h4>Implement the native function</h4>
<p>This is what the function will look like when written in C.</p>
<pre class="brush:c">
static jdouble compareNative(JNIEnv *env, jobject thiz, jintArray sourceArr,
                                                jintArray targetArr, jdouble targetError){
  jdouble error = 0.0;
  int index, c1, c2, b, g, r  = 0;
  jint *sarr, *tarr;

  sarr = (*env)->GetIntArrayElements(env, sourceArr, NULL);
  tarr = (*env)->GetIntArrayElements(env, targetArr, NULL);

  if (sarr == NULL || tarr == NULL) {
         return targetError; /* exception occurred */
  }

  int size = (*env)->GetArrayLength(env, sourceArr);

  for (index = 0; index < size; index++) {
    c1 = sarr[index];
    c2 = tarr[index];
    b = (c1 >> 16 & 255) - (c2 >> 16 & 255);
    g = (c1 >> 8 & 255) - (c2 >> 8 & 255);
    r = (c1 & 255) - (c2 & 255);
    error += r * r + g * g + b * b;
    if (error > targetError){
      (*env)->ReleaseIntArrayElements(env, sourceArr, sarr, 0);
      (*env)->ReleaseIntArrayElements(env, targetArr, tarr, 0);
      return error;
    }
  }
 (*env)->ReleaseIntArrayElements(env, sourceArr, sarr, 0);
 (*env)->ReleaseIntArrayElements(env, targetArr, tarr, 0);
 return error;
}
</pre>
<p>All native functions must have the JNIEnv (a reference to the virtual machine itself) and the jobject (a reference to the "this pointer" of the Java object where the native method call comes from) as the first two arguments. Then we can add our own arguments. For more information on how to write native code for JNI see the <a href="http://java.sun.com/docs/books/jni/">JNI Reference Documentation</a>.</p>
<h4>Register your native functions</h4>
<p>We need a way to make the virtual machine direct the calls to the native declared Java method to our native C function. This is done using the <strong>registerNatives</strong> function of the JNIEnv. If you use the boilerplate C code from above you only have to do two things.</p>
<ol>
<li>Set the classpath variable to the full class name of your Java class (including package name). Replace the dots with slashes.</li>
<li>For each native declared method in Java, insert a JNINativeMethod struct into the <strong>methods[]</strong> array.</li>
</ol>
<p>For our example it will look like this</p>
<pre class="brush:c">
static const char *classPathName = "com/jayway/MyComparator";

static JNINativeMethod methods[] = {
  // nameOfNativeMethod, methodSignature, methodPointer
  {"compare", "([I[ID)D", (void*)compareNative },
};
</pre>
<p>The first parameter is the name of the native declared Java method, the second is the signature of the native declared Java method and the last parameter is the function pointer to the C function to execute when the native declared Java function is executed.</p>
<p>The signature of a Java method can be determined using the <strong>javap</strong> tool from SUN's Java SDK or you can create it yourself using the following table, <a href="http://journals.ecs.soton.ac.uk/java/tutorial/native1.1/implementing/method.html">Java VM Type Signatures</a>.</p>
<h4>Build using Android NDK</h4>
<p>To make things really simple when developing JNI code Google has released the <a href="http://developer.android.com/sdk/ndk/1.6_r1/index.html">Android Native Development Kit (NDK)</a>. It's easy to setup and use. Just setup a new project according to the NDK documentation and build your project. The resulting <strong>libs</strong> folder should be included into your Android project.</p>
<h3>What to do in Java</h3>
<p>In order to transform our Java method into a native declared method simply add the word <strong>native</strong> in the method signature and remove the implementation of the method.</p>
<pre class="java">&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">native</span> <span style="color: #993333;">double</span> compare<span style="color: #66cc66;">&#40;</span><span style="color: #993333;">int</span><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> sourceData, <span style="color: #993333;">int</span><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> targetData,
			<span style="color: #993333;">double</span> targetError<span style="color: #66cc66;">&#41;</span>;
&nbsp;</pre>
<p>To make sure that the virtual machine loads our library into memory and register our native functions we have to add the following code.</p>
<pre class="brush:java">
static{
  System.loadLibrary("comparejni");
}
</pre>
<p>This tells the virtual machine to load the library <strong>lib</strong>comparejni<strong>.so</strong> from the underlying operating system. The OnLoad function is executed on the library and our native functions gets registered with the virtual machine.</p>
<h3>The Result</h3>
<p>After some simple benchmarking I found that the native declared method executed about 2-3 times faster than the original method executing within Dalvik. For larger images the improvement might be even bigger since a call to a native declared method takes more time than calling a normal Java method. Eventually, when Dalvik supports JIT compilation, we would probably get the above performance boost for free without calling native functions. But bear in mind, most devices on the market today will never get an upgraded Dalvik with JIT.</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/jayway/posts?a=mNyz3BXFjYA:wySkZ09Rvh4:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/jayway/posts?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/jayway/posts?a=mNyz3BXFjYA:wySkZ09Rvh4:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/jayway/posts?i=mNyz3BXFjYA:wySkZ09Rvh4:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/jayway/posts?a=mNyz3BXFjYA:wySkZ09Rvh4:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/jayway/posts?d=yIl2AUoC8zA" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/jayway/posts/~4/mNyz3BXFjYA" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blog.jayway.com/2010/01/25/boosting-android-performance-using-jni/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<feedburner:origLink>http://blog.jayway.com/2010/01/25/boosting-android-performance-using-jni/</feedburner:origLink></item>
		<item>
		<title>Getting around static typing in Scala</title>
		<link>http://feedproxy.google.com/~r/jayway/posts/~3/qBv2RTzVCDI/</link>
		<comments>http://blog.jayway.com/2010/01/23/getting-around-static-typing-in-scala/#comments</comments>
		<pubDate>Sat, 23 Jan 2010 19:43:12 +0000</pubDate>
		<dc:creator>Jan Kronquist</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[collections]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[scala]]></category>

		<guid isPermaLink="false">http://blog.jayway.com/?p=4503</guid>
		<description><![CDATA[I really like static typing, but sometimes it can get in your way. For instance if you have a collection of objects and you want to perform an operation on the objects of a certain subclass you can run into problems. ]]></description>
			<content:encoded><![CDATA[<p>I really like static typing, but sometimes it can get in your way. For instance if you have a collection of objects and you want to perform an operation on the objects of a certain subclass you can run into problems. I've run the following code in Scala 2.8</p>
<pre class="brush:scala">
// this is easy since everything are Strings
scala> List("one", "two", "three")
res0: List[java.lang.String] = List(one, two, three)

scala> res0.foreach(x => println(x.length()))
3
3
5

// with a mixed collection it gets harder
scala> List("one", 2, "three", 4.0)
res2: List[Any] = List(one, 2, three, 4.0)

scala> res2.foreach(x => println(x.length()))
&lt;console&gt;:6: error: value length is not a member of Any

// it is not enough to just filter out the objects that are Strings
scala> res2.filter(_.isInstanceOf[String])
res4: List[Any] = List(one, three)

// as we still have a List of Any
scala> res4.foreach(x => println(x.length()))
&lt;console&gt;:7: error: value length is not a member of Any

// both filtering and mapping is needed
scala> res2.filter(_.isInstanceOf[String]).map(_.asInstanceOf[String])
res6: List[String] = List(one, three)

// finally it works
scala> res6.foreach(x => println(x.length()))
3
5

// another option is to use pattern matching
scala> res2.foreach(_ match { case x:String => println(x.length); case _ => })
3
5
</pre>
<p>I wanted a more elegant solution with less boiler plate so after playing around with <a href="http://www.artima.com/weblogs/viewpost.jsp?thread=179766">implicit conversions</a> and <a href="http://scala-blogs.org/2008/10/manifests-reified-types.html">reified types</a> I managed to come up with rather nice solution:</p>
<pre class="brush:scala">
// define a class that implements a restrictTo method
scala> class IterableWithRestrictTo[A](from:Iterable[A]) {
     | def restrictTo[B](implicit m: scala.reflect.Manifest[B]) = {
     | from.filter(m.erasure.isInstance(_)).map(_.asInstanceOf[B])
     | }
     | }
defined class IterableWithRestrictTo

// define implicit conversion
scala> implicit def makeIterableWithRestrictTo[A](from : Iterable[A]) = {
     | new IterableWithRestrictTo(from)
     | }
makeIterableWithRestrictTo: [A](from: Iterable[A])IterableWithRestrictTo[A]

scala> List("one", 2, "three", 4.0)
res0: List[Any] = List(one, 2, three, 4.0)

// restrict to objects of type String
scala> res0.restrictTo[String]
res1: Iterable[String] = List(one, three)

// works since the collection have correct type
scala> res1.foreach(x => println(x.length()))
3
5
</pre>
<p>Using the name restrictTo clearly shows that not only is there a type conversion, but also a restriction to a subset of the elements. Notice that the manifest is needed to get the reified type, since the type B is actually erased at compile time. Read more about <a href="http://scala-blogs.org/2008/10/manifests-reified-types.html">reified types in Scala</a>.</p>
<p>Let me know you have another solution! </p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/jayway/posts?a=qBv2RTzVCDI:XFCHV42uin0:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/jayway/posts?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/jayway/posts?a=qBv2RTzVCDI:XFCHV42uin0:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/jayway/posts?i=qBv2RTzVCDI:XFCHV42uin0:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/jayway/posts?a=qBv2RTzVCDI:XFCHV42uin0:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/jayway/posts?d=yIl2AUoC8zA" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/jayway/posts/~4/qBv2RTzVCDI" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blog.jayway.com/2010/01/23/getting-around-static-typing-in-scala/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		<feedburner:origLink>http://blog.jayway.com/2010/01/23/getting-around-static-typing-in-scala/</feedburner:origLink></item>
	</channel>
</rss>
