<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Nito &#187; Java</title>
	<atom:link href="http://blog.pirelenito.org/tag/java/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.pirelenito.org</link>
	<description>Thoughts dump yard</description>
	<lastBuildDate>Tue, 20 Jul 2010 02:18:47 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Gonp, An Android Saga: Part I</title>
		<link>http://blog.pirelenito.org/2010/04/gonp-an-android-saga-part-i/</link>
		<comments>http://blog.pirelenito.org/2010/04/gonp-an-android-saga-part-i/#comments</comments>
		<pubDate>Wed, 14 Apr 2010 11:59:45 +0000</pubDate>
		<dc:creator>nito</dc:creator>
				<category><![CDATA[Gonp Saga]]></category>
		<category><![CDATA[android]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[game]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[mobile]]></category>
		<category><![CDATA[OpenGL]]></category>
		<category><![CDATA[saga]]></category>

		<guid isPermaLink="false">http://blog.pirelenito.org/?p=107</guid>
		<description><![CDATA[Today myself and a couple of friends (Renato Besen and Vitor Pelizza) started on a saga to develop a game on Android, there is not much to see or do after our approximately three hours of work but of what is on the screenshot bellow: A green square, composed of 2 triangles and 4 points. Which might [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://blog.pirelenito.org/wp-content/uploads/2010/04/Screen-shot-2010-04-13-at-10.45.22-PM.png"><img class="size-full wp-image-111 alignleft" title="First glimpse of the running application." src="http://blog.pirelenito.org/wp-content/uploads/2010/04/Screen-shot-2010-04-13-at-10.45.22-PM.png" alt="" width="128" height="188" /></a></p>
<p>Today myself and a couple of friends (<a href="http://besen.posterous.com/" target="_blank">Renato Besen</a> and <a href="http://twitter.com/vpelizza" target="_blank">Vitor Pelizza</a>) started on a saga to develop a game on Android, there is not much to see or do after our approximately three hours of work but of what is on the screenshot bellow:</p>
<p>A green square, composed of 2 triangles and 4 points. Which might not seem much at a first glance, but there was a lot of learning in the process.</p>
<p>Through the development we followed loosely <a href="http://www.droidnova.com/android-3d-game-tutorial-part-i,312.html" target="_blank">this tutorial</a> series, but as good questioners as we are, we quickly started to put some query upon some of the tutorial&#8217;s solutions. For instance, why does the buffer creation needs all this code:</p>
<blockquote><p>ByteBuffer ibb = ByteBuffer.allocateDirect(_nrOfVertices * 2);<br />
ibb.order(ByteOrder.nativeOrder());<br />
_indexBuffer = ibb.asShortBuffer();<br />
_indexBuffer.put(_indicesArray);</p></blockquote>
<p>When we thought it could be done with something like this:</p>
<blockquote><p>ShortBuffer _indexBuffer = ShortBuffer.wrap(_indicesArray);</p></blockquote>
<p>After seeing the app crash on start we understood that all buffers used by OpenGL ES need to be of the device&#8217;s native byte order, which is not the default order when creating a Buffer through the wrap method. To solve this more elegantly we <a href="http://labs.pirelenito.org/repositories/entry/gonp/src/org/gonp/NativeBufferFactory.java">created a factory</a> for native byte order buffers.</p>
<p>Next up was drawing our Player(the square), but since OpenGL ES does not support QUADs, it would need to be composed of two triangles. After figuring out how the <a href="http://www.khronos.org/opengles/documentation/opengles1_0/html/glVertexPointer.html" target="_blank">glVertexPointer</a> and <a href="http://www.khronos.org/opengles/documentation/opengles1_0/html/glDrawElements.html" target="_blank">glDrawElements</a> methods work, we finally made our <a href="http://labs.pirelenito.org/repositories/entry/gonp/src/org/gonp/Player.java" target="_self">Player</a> class render on the screen:</p>
<blockquote><p>gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexes);<br />
gl.glDrawElements(GL10.GL_TRIANGLES, indexes.capacity() &#8230;</p></blockquote>
<p>On the first line we tell OpenGL wich are the points that it needs to render, next we pass an array containing the order that they need to be drawn.</p>
<p><span style="text-decoration: line-through;">That is it for now, but you can follow our progress through the <a href="http://labs.pirelenito.org/projects/activity/gonp">project&#8217;s Redmine</a> or by checking out the code at the <a href="http://labs.pirelenito.org/gonp/git">git repository</a>.</span></p>
<p><strong>Update</strong>: Moved the repository to <a href="http://github.com/pirelenito/gonp">github</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.pirelenito.org/2010/04/gonp-an-android-saga-part-i/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Cloning the unclonable</title>
		<link>http://blog.pirelenito.org/2010/03/cloning-the-unclonable/</link>
		<comments>http://blog.pirelenito.org/2010/03/cloning-the-unclonable/#comments</comments>
		<pubDate>Tue, 16 Mar 2010 23:42:30 +0000</pubDate>
		<dc:creator>nito</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[class]]></category>
		<category><![CDATA[clone]]></category>
		<category><![CDATA[serialization]]></category>
		<category><![CDATA[utility]]></category>

		<guid isPermaLink="false">http://blog.pirelenito.org/?p=91</guid>
		<description><![CDATA[Sometimes during development we need to save an object state so that we can latter return to it, due to a problem or a by an user undo request. In these scenarios usually some type of cloning is a good way to go. Java has a standard way of doing this through the .clone() method, [...]]]></description>
			<content:encoded><![CDATA[<p>Sometimes during development we need to save an object state so that we can latter return to it, due to a problem or a by an user undo request. In these scenarios usually some type of cloning is a good way to go.</p>
<p>Java has a standard way of doing this through the .clone() method, but in order for it to work, the classes need to implement it explicitly. Then comes a day that you need to clone some third party object that does not implement the clone() method, so what do you do?</p>
<p>Me and friend <a href="http://besen.posterous.com/" target="_blank">Renato Besen</a> came along an interesting solution during a pair, and I thought it would be fun to share. The object we wanted to clone was Serializable, so we created a class that perform a cloning through serialization, writing the object into memory and then reading it back into another new instance, effectively making a clone of it.</p>
<p>The class is generic, so the final solution is pretty neat, just call:</p>
<p style="padding-left: 30px;">new SerializationCloner&lt;ThirdPartyClass&gt;().clone(instance);</p>
<p>And that is it! You can <a href="http://labs.pirelenito.org/experiments/svn/java/cloningutility/src/SerializationCloner.java">get the source from here</a>.</p>
<p>The only obvious downside is performance, so use it with caution.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.pirelenito.org/2010/03/cloning-the-unclonable/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Java movie playback: JOGL + Fobs4JMF</title>
		<link>http://blog.pirelenito.org/2008/08/java-movie-playback-jogl-fobs4jmf/</link>
		<comments>http://blog.pirelenito.org/2008/08/java-movie-playback-jogl-fobs4jmf/#comments</comments>
		<pubDate>Wed, 20 Aug 2008 23:37:45 +0000</pubDate>
		<dc:creator>nito</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[fast]]></category>
		<category><![CDATA[Fobs4JMF]]></category>
		<category><![CDATA[how to]]></category>
		<category><![CDATA[JMF]]></category>
		<category><![CDATA[JOGL]]></category>
		<category><![CDATA[OpenGL]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[plugin]]></category>
		<category><![CDATA[RGB]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[VideoRenderer]]></category>

		<guid isPermaLink="false">http://blog.pirelenito.org/?p=12</guid>
		<description><![CDATA[Recently I had to integrate video playback on my job&#8217;s Java OpenGL engine, which uses JOGL. Java has a support to media playback through it&#8217;s Java Media Framework, which unfortunately, on it&#8217;s current version (2.1.1e) does not support many formats for video playback. So I quickly looked for alternatives, including IBM Toolkit for mpeg4, that [...]]]></description>
			<content:encoded><![CDATA[<p>Recently I had to integrate <strong>video playback</strong> on my job&#8217;s <strong>Java OpenGL</strong> engine, which uses <a href="https://jogl.dev.java.net/" target="_blank">JOGL</a>.</p>
<p>Java has a support to media playback through it&#8217;s <a href="http://java.sun.com/javase/technologies/desktop/media/jmf/" target="_blank">Java Media Framework</a>, which unfortunately, on it&#8217;s current version (2.1.1e) does not support many <a href="http://java.sun.com/javase/technologies/desktop/media/jmf/2.1.1/formats.html" target="_blank">formats for video playback</a>.</p>
<p>So I quickly looked for alternatives, including <a href="http://www.alphaworks.ibm.com/tech/tk4mpeg4" target="_blank">IBM Toolkit for mpeg4</a>, that hadn&#8217;t a sufficient production performance I was looking for, and didn&#8217;t offer an easy option for frame grabbing or plugin extensions as JMF does.</p>
<p>Next was <a href="http://fobs.sourceforge.net/" target="_blank">Fobs4JMF</a>, which is <a href="http://java.sun.com/javase/technologies/desktop/media/jmf/" target="_blank">JMF</a> + <a href="http://ffmpeg.org/" target="_blank">ffmpeg</a>. This solution was much more interesting, since it offers a <strong>wide variety of codecs</strong> (ogg, mp3, m4a, divx, xvid, h264, mov, etc) and is based on the solid ffmpeg solution to decode audio and video.</p>
<p>My implementation, uses the plug-in capabilities of JMF to extend a custom renderer that does a pixel type conversion and rendering to a texture:</p>
<div style="text-align: center;"><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="425" height="344" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="allowFullScreen" value="true" /><param name="src" value="http://www.youtube.com/v/-zh6yDyasSo&amp;hl=en&amp;fs=1" /><param name="allowfullscreen" value="true" /><embed type="application/x-shockwave-flash" width="425" height="344" src="http://www.youtube.com/v/-zh6yDyasSo&amp;hl=en&amp;fs=1" allowfullscreen="true"></embed></object></div>
<p><span id="more-12"></span></p>
<p>This custom renderer works with RGB textures, a type I seemed to made work on my two test machines:</p>
<ul>
<li>MacBook with a Integ GMA x3100 &#8211; Leopard;</li>
<li>PC with a Radeon x600 &#8211; Debian.</li>
</ul>
<p>You might wanna try different pixel types to increase the performance on different target machines.</p>
<p>First, lets describe <strong>how the Renderer works:</strong></p>
<p>It got to be an implementation of a <em>javax.media.renderer.VideoRenderer</em> since it will be installed as a <strong>plugin on JMF</strong>.</p>
<p>For the different methods we need to implement, there are a few we need to take proper care of:</p>
<ul>
<li><strong>process</strong>: this is the method JMF calls passing the movie&#8217;s current frame buffer, here we process the buffer so that we can latter render it in OpenGL;</li>
<li><strong>getSupportedInputFormats</strong>: we return RGBFormat, our target texture format;</li>
<li><strong>setInputFormat</strong>: here we simply tell JMF that the format it chooses is the one we want. Since RGB was the only one we returned as supported, there is not much to do here as well.</li>
<li><strong>getName</strong>: returns the renderer neat name!</li>
</ul>
<p>Next we need a way to access this renderer outside of the JMF world, so that we can get the texture to render it on the teapot. For this purpose our class must also be a <em>javax.media.Control,</em> then we can easily get it through an <em>getControl</em> call, such as:</p>
<blockquote><p>player.getControl(&#8220;javax.media.renderer.VideoRenderer&#8221;);</p></blockquote>
<p>So we implement:</p>
<ul>
<li><strong>getControl</strong>: returns it&#8217;s instance;</li>
<li><strong>getControls</strong>: returns an array containing only it&#8217;s instance as a valid control.</li>
</ul>
<p>The renderer implementation is <em><a href="http://github.com/pirelenito/MovieGL/blob/master/src/org/pirelenito/multimedia/jmf/plugin/RGBGLTextureRenderer.java">org.pirelenito.multimedia.jmf.plugin.RGBGLTextureRenderer</a></em>.</p>
<p>And also, to make further development easy, there is an <a href="http://github.com/pirelenito/MovieGL/blob/master/src/org/pirelenito/multimedia/jmf/plugin/IGLTextureRenderer.java">IGLTextureRenderer</a> interface with the public methods called by the Canvas:</p>
<ul>
<li><strong>render</strong>: that plots the buffer on the texture surface;</li>
<li><strong>getTexture</strong>: to retrieve the texture instance.</li>
</ul>
<p>Last but not least important, you will need to <strong>register the renderer</strong> on JMF, this is done through the <em>JMFRegistry</em> application. The easiest way to start it is inside Eclipse, where our custom renderer is already on the class path:</p>
<ul>
<li>Create a new run configuration;</li>
<li>Set the main class as: JMFRegistry;</li>
<li>Start it, and go to the Plugins tab, then Renderer;</li>
<li>Add the org.pirelenito.multimedia.jmf.plugin.RGBGLTextureRenderer;</li>
<li>Move it to the top of the list;</li>
<li>Push Commit, and you are good to go!</li>
</ul>
<p>To test it out there is also a <a href="http://github.com/pirelenito/MovieGL/blob/master/src/org/pirelenito/multimedia/jmf/MoviePlayer.java">helper class</a> to instantiate the movie player, and a <a href="http://github.com/pirelenito/MovieGL/blob/master/src/org/pirelenito/movieGL/Main.java">Main</a> class which is an OpenGL canvas used to render the teapot with the texture on its surface.</p>
<p>I am not very experienced with OpenGL, so there might be <strong>more effitient ways</strong> to do this using, for instance, <strong>PBO</strong> (Pixel Buffer Object). If you have any question or suggestions on how to improve this solution, don&#8217;t hesitate on drooping a comment!</p>
<p><a href="http://github.com/pirelenito/MovieGL">Here is the project&#8217;s repository</a>. You will need to download a few dependencies, so check the Readme file for more information.</p>
<p><span style="text-decoration: line-through;"><strong>Update:</strong> I wasn&#8217;t properly initializing the Texture. and since I want to add support for more pixel formats, it is easy for you to checkout the updated Eclipse project at: <a href="http://labs.pirelenito.org/experiments/svn/java/MovieGL/" target="_blank">http://labs.pirelenito.org/experiments/svn/java/MovieGL/</a><del datetime="2008-08-21T20:34:00+00:00"> latter this afternoon</del> now.</span></p>
<p><span style="text-decoration: line-through;"><strong>Update 2:</strong> Added the <a href="http://labs.pirelenito.org/experiments/svn/java/MovieGL/" target="_blank">repository link</a> on the image above.</span></p>
<p><span style="text-decoration: line-through;"><strong>Update 3:</strong> I&#8217;ve migrated my server to a new location, and the Subversion wasn&#8217;t up until today. Sorry.</span></p>
<p><strong>Update 4:</strong> Small review on the post.</p>
<p><strong>Update 5:</strong> Moved the repository to <a href="http://github.com/pirelenito/MovieGL">github</a>.</p>
<p>Cheers! <img src='http://blog.pirelenito.org/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://blog.pirelenito.org/2008/08/java-movie-playback-jogl-fobs4jmf/feed/</wfw:commentRss>
		<slash:comments>28</slash:comments>
		</item>
	</channel>
</rss>
