<?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>Benny's Blog</title>
	<atom:link href="http://blog.projectnibble.org/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.projectnibble.org</link>
	<description>House Of Code</description>
	<lastBuildDate>Sun, 14 Feb 2010 18:06:59 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=abc</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Papervision3D star (sun) tutorial and source</title>
		<link>http://blog.projectnibble.org/2010/02/11/papervision3d-star-sun-tutorial-and-source/</link>
		<comments>http://blog.projectnibble.org/2010/02/11/papervision3d-star-sun-tutorial-and-source/#comments</comments>
		<pubDate>Thu, 11 Feb 2010 22:33:04 +0000</pubDate>
		<dc:creator>Benny Bottema</dc:creator>
				<category><![CDATA[Papervision3D]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[star]]></category>
		<category><![CDATA[sun]]></category>

		<guid isPermaLink="false">http://blog.projectnibble.org/?p=423</guid>
		<description><![CDATA[Read how by combing Perlin noise and a trick using two planes, you can make a real-time animated glaring sun corona in Papervision3D.]]></description>
			<content:encoded><![CDATA[<p>If you are looking for a cool sun effect (no pun intended) with flaring corona, you&#8217;re in the right place. This post isn&#8217;t about some practical sun in some shooter though: it would require some tweaking to make it useful in such context, as the animation is rather cpu heavy due to its massive corona noise textures.</p>
<p>No, this post is about combining cool PV3D features in order to achieve a nice looking animated sun. I’ve used the trunk of the papervision code repository, revision 851 (it works up to r910, after that <a href="http://code.google.com/p/papervision3d/issues/detail?id=250">a bug was introduced</a>). Note that I&#8217;m no PV3D guru; I&#8217;m just sharing what I&#8217;ve learned so far.</p>
<div style="text-align:center">
<ul>
<li><a href="http://projectnibble.org/dump/blog/papervision3d_sun/suntutorial.rar">download sun source</a></li>
</ul>
<p>
<object width="400" height="400">
<param name="movie" value="http://projectnibble.org/dump/blog/papervision3d_sun/Suntutorial.swf"></param>
<param name="quality" value="high"></param>
<param name="wmode" value="window"></param>
<param name="menu" value="false"></param>
<param name="bgcolor" value="#000000"></param>
<embed type="application/x-shockwave-flash" width="400" height="400" src="http://projectnibble.org/dump/blog/papervision3d_sun/Suntutorial.swf" quality="high" bgcolor="#000000" wmode="window" menu="false" ></embed>
</object>
<br />
<span style="color:999999">(note: this sun uses the skybox from the <a href="http://blog.projectnibble.org/2009/05/22/easy-papervision3d-skybox-tutorial-and-source/">skybox tutorial</a>)</span>
</div>
<p>Closely watch that corona for a while!</p>
<p><span id="more-423"></span></p>
<h2>How it works</h2>
<ul>In short: the sun exist entirely of planes, controlled by alpha ratios and always facing the camera:</p>
<li>one plane with a gradient texture white to red with glow</li>
<li>two planes with a perlin texture, that resize and tag eachother as to provide a continuous outwards motion</li>
<li>one plane that acts as a mask for the other planes to provide a perfect circle giving the illusion of a round star</li>
</ul>
<h3>The star&#8217;s gradient</h3>
<p>Actually, it took quite some tweaking to get a nice gradient of white in the middle and yellow to red on the edge. It&#8217;s a basic <a href="http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/display/Graphics.html#beginGradientFill()">Sprite gradient fill</a>, using three colors, alpha layering and a specific color ratio array to get the last two colors (yellow and red) on the edge. Then I&#8217;m applying this gradient <em>twice</em> on the same canvas to get the result I want when the flaring corona&#8217;s are added. I couldn&#8217;t get it the way I wanted with the alpha array alone (let me know if you got a better way), but this worked and the gradients are only calculated and drawn once.</p>
<p>Drawing it twice means less transparency and that means more dense flares as we&#8217;ll see later on when we&#8217;re combining the star glow layer and corona layer using <a href="http://www.bukisa.com/articles/38637_papervision-3d-programming-tutorial-blend-modes">BlendMode.ADD</a> (which is also the reason why we need MovieMaterial here: otherwise they won&#8217;t interact on layer-level).</p>
<div style="text-align:center">
<img src="http://projectnibble.org/dump/blog/papervision3d_sun/sunglow.png" alt="sunglow" />
</div>
<pre  name="code" class="java">
// pseudo code
var sunColors:Array = [0xfefefe, 0xFAEB61, 0xff0000]; // white, orange/yellow, red
var sunAlphas:Array = [1, .8, 0]; // white part opaque, 20% from the edge out fading out
var sunRatios:Array = [0x85, 0xAA, 0xDD]; // all white except for the edges

var size:Number = 1000;
var sunglowMaterial:MovieMaterial = createSunglowMaterial(size, sunColors, sunAlphas, sunRatios);
var sun:Plane = new Plane(sunglowMaterial, size, size, null, null);

/**
 * Creates a disc texture using radial alpha. Draw twice for extra intensity of the colors.
 */
private function createSunglowMaterial(size:Number, colors:Array, alphas:Array, ratios:Array):MovieMaterial {
	var mat:Matrix = new Matrix();
	mat.createGradientBox(size, size);
	var sunTexture:Sprite = new Sprite();
	for (var i:Number = 0; i < 2; i++) {
		sunTexture.graphics.beginGradientFill(GradientType.RADIAL, colors, alphas, ratios, mat);
		sunTexture.graphics.drawRect(0, 0, size, size);
		sunTexture.graphics.endFill();
	}
	return new MovieMaterial(sunTexture, true);
}
</pre>
<h3>The star's flaring corona</h3>
<p>The corona was tricky. I needed to find some way to create a seamless and continuous outward motion of a Perlin noise map that would function as a flare animation. In the end I settled for a two plane solution where one slowly grows and finally fades out at which point the second fades in, rotated randomly and grows until it fades out again. Rinse and repeat. When calibrated carefully, the resizing, rotating and fading is imperceptible to the eye, due to the naturally distributed Perlin noise:</p>
<div style="text-align:center">

<object width="300" height="300">
<param name="movie" value="http://projectnibble.org/dump/blog/papervision3d_sun/imperceptible%20corona.swf"></param>
<param name="quality" value="high"></param>
<param name="wmode" value="window"></param>
<param name="menu" value="false"></param>
<param name="bgcolor" value="#000000"></param>
<embed type="application/x-shockwave-flash" width="300" height="300" src="http://projectnibble.org/dump/blog/papervision3d_sun/imperceptible%20corona.swf" quality="high" bgcolor="#000000" wmode="window" menu="false" ></embed>
</object>
<br />
<span style="color:999999">(imperceptible version)</span><br />
<br />

<object width="300" height="300">
<param name="movie" value="http://projectnibble.org/dump/blog/papervision3d_sun/perceptible%20corona.swf"></param>
<param name="quality" value="high"></param>
<param name="wmode" value="window"></param>
<param name="menu" value="false"></param>
<param name="bgcolor" value="#000000"></param>
<embed type="application/x-shockwave-flash" width="300" height="300" src="http://projectnibble.org/dump/blog/papervision3d_sun/perceptible%20corona.swf" quality="high" bgcolor="#000000" wmode="window" menu="false" ></embed>
</object>
<br />
<span style="color:999999">(perceptible version)</span><br />
<br />

<object width="200" height="200">
<param name="movie" value="http://projectnibble.org/dump/blog/papervision3d_sun/sun%20final%20corner.swf"></param>
<param name="quality" value="high"></param>
<param name="wmode" value="window"></param>
<param name="menu" value="false"></param>
<param name="bgcolor" value="#000000"></param>
<embed type="application/x-shockwave-flash" width="200" height="200" src="http://projectnibble.org/dump/blog/papervision3d_sun/sun%20final%20corner.swf" quality="high" bgcolor="#000000" wmode="window" menu="false" ></embed>
</object>
<br />
<span style="color:999999">(result)</span>
</div>
<p>The Perlin noise is added with a simple Perlin noise command as follows:</p>
<pre  name="code" class="java">
var coronaMaterial1:BitmapMaterial = createCoronaMaterial(size / 1.25, size / 100, SEGMENTS);
var coronaMaterial2:BitmapMaterial = createCoronaMaterial(size / 1.25, size / 100, SEGMENTS);
var corona1:Plane = new Plane(coronaMaterial1);
var corona2:Plane = new Plane(coronaMaterial2);

/**
 * Creates a perlin noise texture to simulate the flaring flames on the sun's outer glow.
 */
private function createCoronaMaterial(textureSize:Number, noiseSize:Number, segments:Number):BitmapMaterial {
	var coronaBitmap:BitmapData = new BitmapData(textureSize, textureSize);
	coronaBitmap.perlinNoise(noiseSize, noiseSize, 4, 61, true, true, 1, false);
	return new BitmapMaterial(coronaBitmap, true);
}
</pre>
<p>The algorithm to animate the two planes is included in the attached sources.</p>
<h3>The corona and star glow combined and masked by a perfect circle</h3>
<p>The planes are all on the same depth and level. This works because we're using alpha ratios to determine where one plane's visibility starts and the other disappears. The star glow textures stands on its own, it is inherently radial and as such doesn't need a circle mask. This leaves the Perlin noise textures which need masking. To do this, I've put them in separate layers, which are then both masked by a third layer.</p>
<pre  name="code" class="java">
/*
 * use root corona layer so we can perform an alpha mask to both child layers at once
 *     - contains child corona layers so we can control individual alpha properties
 *         - contains child corona planes so we can apply a perlin noise texture to it for 'flaring' effect
 */
var coronaLayer:ViewportLayer = new ViewportLayer(viewport, null);
coronaChildLayer1 = new ViewportLayer(viewport, null);
coronaChildLayer2 = new ViewportLayer(viewport, null);
coronaLayer.addLayer(coronaChildLayer1);
coronaLayer.addLayer(coronaChildLayer2);
coronaChildLayer1.addDisplayObject3D(corona1);
coronaChildLayer2.addDisplayObject3D(corona2);
viewport.containerSprite.addLayer(coronaLayer);
// give the corona of the sun a 'flaring' effect
coronaLayer.blendMode = BlendMode.ADD;

// only show the corona part of the flaring perlin noise planes
var coronaMaskLayer:ViewportLayer = viewport.getChildLayer(coronaMask);
coronaLayer.cacheAsBitmap = true;
coronaMaskLayer.cacheAsBitmap = true;
coronaLayer.mask = coronaMaskLayer;
</pre>
<p>On a final note, I'm using the skybox from the <a href="http://blog.projectnibble.org/2009/05/22/easy-papervision3d-skybox-tutorial-and-source/">skybox tutorial</a> and also the <a href="http://blog.projectnibble.org/2009/06/15/papervision3d-displayobject3d-helper-baseclass-for-realtime-updates/">RealtimeDisplayObject3D helper baseclass</a> for the sun's scene updates (which is why the coronas keep animating smoothly even at low FPS).</p>
<p><a href="http://blog.projectnibble.org/2010/02/01/papervision3d-star-sun-tutorial-and-source/trackback/">trackback</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.projectnibble.org/2010/02/11/papervision3d-star-sun-tutorial-and-source/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Want to play Mass Effect 2, but lost your Mass Effect 1 save games?</title>
		<link>http://blog.projectnibble.org/2010/01/30/want-to-play-mass-effect-2-but-lost-your-mass-effect-1-save-games/</link>
		<comments>http://blog.projectnibble.org/2010/01/30/want-to-play-mass-effect-2-but-lost-your-mass-effect-1-save-games/#comments</comments>
		<pubDate>Sat, 30 Jan 2010 22:21:40 +0000</pubDate>
		<dc:creator>Benny Bottema</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Mass Effect]]></category>

		<guid isPermaLink="false">http://blog.projectnibble.org/?p=413</guid>
		<description><![CDATA[Want to play Mass Effect 2, but lost your Mass Effect 1 save games? Get new save games at masseffectsaves.com!]]></description>
			<content:encoded><![CDATA[<p>Yes, I play video games too. I won&#8217;t miss out on <a href="http://masseffect.bioware.com/">Mass Effect</a>, which I think portraits a world almost as intricate as any good sci-fi novel (oh yeah I read those too, you should try <a href="http://www.amazon.com/Fire-Upon-Deep-Zones-Thought/dp/0812515285/ref=sr_1_1?ie=UTF8&#038;s=books&#038;qid=1264888373&#038;sr=1-1">A Fire Upon the Deep</a>, by Vernor Vinge). </p>
<p>Since Mass Effect is going to be a trilogy (<a href="http://www.shacknews.com/onearticle.x/62032">and more</a> apparently), Bioware included a system that will use old save games as input for continuity in upcoming games, including ME2. Note that you&#8217;ll still be able to pick a different face and class.</p>
<h2>Get your Mass Effect 1 save games at masseffectsaves.com</h2>
<p>I didn&#8217;t know there would be an option to use ME1 save games as input until recently and I&#8217;ve deleted my save games a long time ago. No undelete program would find them. <a href="http://www.annakie.com/">Annakie</a> recognized this would be a problem and started a <a href="http://www.masseffectsaves.com/">small site</a> that offers various save games each with a different set of key choices made over time in ME1.</p>
<p>How awesome is that!</p>
<p>Take your pick as the list is growing fast and the website is becoming a popular hub for Shepards.</p>
<p><a href="http://blog.projectnibble.org/2010/01/30/want-to-play-mass-effect-2-but-lost-your-mass-effect-1-save-games/trackback/">trackback</a></p>
<p><img src="http://projectnibble.org/dump/blog/images/tali.jpg" alt="Mass Effect 2" /></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.projectnibble.org/2010/01/30/want-to-play-mass-effect-2-but-lost-your-mass-effect-1-save-games/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to connect a bluetooth keyboard in Ubuntu 9.04 Jaunty and 9.10 Karmic (workaround)</title>
		<link>http://blog.projectnibble.org/2010/01/28/how-to-connect-a-bluetooth-keyboard-in-ubuntu-9-04-jaunty-and-9-10-karmic-workaround/</link>
		<comments>http://blog.projectnibble.org/2010/01/28/how-to-connect-a-bluetooth-keyboard-in-ubuntu-9-04-jaunty-and-9-10-karmic-workaround/#comments</comments>
		<pubDate>Thu, 28 Jan 2010 21:54:57 +0000</pubDate>
		<dc:creator>Benny Bottema</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[bluetooth]]></category>
		<category><![CDATA[jaunty]]></category>
		<category><![CDATA[karmic]]></category>
		<category><![CDATA[keyboard]]></category>
		<category><![CDATA[ubuntu]]></category>

		<guid isPermaLink="false">http://blog.projectnibble.org/?p=374</guid>
		<description><![CDATA[Here&#8217;s a quick workaround for connecting and reconnecting a bluetooth keyboard (or any bluetooth device for that matter) in Ubuntu Jaunty and Karmic, even after rebooting. In short it&#8217;s a shell script that keeps connecting to your device in the background on a regular interval. Dirty? Absolutely. Does it work? Absolutely, no bluetooth configuration required [...]]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s a quick workaround for connecting and reconnecting a bluetooth keyboard (or any bluetooth device for that matter) in Ubuntu Jaunty and Karmic, even after rebooting. In short it&#8217;s a shell script that keeps connecting to your device in the background on a regular interval. Dirty? Absolutely. Does it work? Absolutely, no bluetooth configuration required at all.</p>
<p><span id="more-374"></span></p>
<p>Originally, this whole workaround came from a <a href="http://ubuntuforums.org/showthread.php?p=8739447#post8174091">post on ubuntuforums.org</a> by Unkie, which I modified only slightly (I&#8217;m plantface there). This workaround revolves around <a href="http://linuxcommand.org/man_pages/hidd1.html">hidd</a>, which was removed in Ubuntu 9.04 which you can get back as follows:</p>
<pre  name="code">sudo apt-get install bluez-compat</pre>
<p>Hidd is a tool that works like a charm for scanning and connecting to bluetooth devices. It&#8217;s still used these days because  the Ubuntu dev team somehow manages to kill bluetooth support  every new version. There are so many tutorials and flavors floating around to get a bluetooth device working and apparently with so little success that hidd remains popular. With 9.04 they gave bluetooth an <a href="http://ubuntuforums.org/showthread.php?t=952168">overhaul</a> and deprecated hidd.</p>
<p>Anyway, here&#8217;s the shell script that keeps connecting your keyboard. There&#8217;s one catch though: I haven&#8217;t figured out how to let the keyboard sleep, awake and reconnect itself, so you&#8217;ll need to hit the reset button on the keyboard to reveal itself to the bluetooth adapter looking for it. Reconnecting this way requires thus one extra manual action before it reconnects (same thing goes for when you rebooted). But it reconnects when you want it to, which is what I&#8217;m content with for the moment, considering the alternative.</p>
<ol>
<li>
First create a new file for the script:</p>
<pre  name="code">sudo nano /etc/keyboard.sh</pre>
<p>Add the following script:</p>
<pre  name="code">
while (sleep 10)
do
sudo hidd --connect AA:BB:CC:DD:EE:FF > /dev/null
done
</pre>
<p>Close and save. </p>
<p style="font-size: 4pt; background-color: rgb(225,225,225);"><strong>Note</strong>: that AA:BB address should be the MAC address of your keyboard. You can find it using the new bluetooth tool <i>hcitool scan</i> (make sure your keyboard is findable by hitting its reset switch) or with <i>hidd &#8211;search</i>, which coincidently will automatically attempt to connect your device while scanning it.</p>
</li>
<li>
Next we&#8217;ll make sure it&#8217;ll run in the background when booted. Create a new boot entry:</p>
<pre  name="code">sudo nano /etc/init.d/keyboard</pre>
<p>Add the following script:</p>
<pre  name="code">
#!/bin/sh

/etc/keyboard.sh &#038;

exit 0
</pre>
<p>Close and save.
</li>
<li>
Give both files execution rights with <i>chmod +x {filename}</i>
</li>
<li>
Now run the following command:</p>
<pre  name="code">sudo update-rc.d keyboard defaults</pre>
</li>
</ol>
<p>Reboot and check if it works. Remember to hit your keyboard&#8217;s reset switch when you want Ubuntu to connect your keyboard.</p>
<p><a href="http://blog.projectnibble.org/2010/01/28/how-to-connect-bluetooth-keyboard-in-ubuntu-9-04-jaunty-workaround/trackback/">trackback</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.projectnibble.org/2010/01/28/how-to-connect-a-bluetooth-keyboard-in-ubuntu-9-04-jaunty-and-9-10-karmic-workaround/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Excluding resources and test classes from war/ear in Eclipse</title>
		<link>http://blog.projectnibble.org/2009/08/24/excluding-test-classes-from-war-ear/</link>
		<comments>http://blog.projectnibble.org/2009/08/24/excluding-test-classes-from-war-ear/#comments</comments>
		<pubDate>Mon, 24 Aug 2009 15:15:24 +0000</pubDate>
		<dc:creator>Benny Bottema</dc:creator>
				<category><![CDATA[eclipse]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://blog.projectnibble.org/?p=358</guid>
		<description><![CDATA[Today I was trying to figure out how to exclude certain resources/classes from the war that was being deployed to my embedded server in Eclipse. I couldn&#8217;t find out how to do this in the Eclipse settings file. 
Apparently, it has nothing to do with the source folder you see in the Project Explorer view, [...]]]></description>
			<content:encoded><![CDATA[<p>Today I was trying to figure out how to exclude certain resources/classes from the war that was being deployed to my embedded server in Eclipse. I couldn&#8217;t find out how to do this in the Eclipse settings file. </p>
<p>Apparently, it has nothing to do with the source folder you see in the Project Explorer view, or the export settings in the Buildpath dialogs: instead you need to edit the Eclipse settings file “project-war\.settings\org.eclipse.wst.common.component” (viewable in the Navigator view) and modify the deployed source folders in there. Changes there do *not* reflect in the Project explorer (at least not with my version of Eclipse, Galileo). I’ve removed the test sourcefolder from there and everything was honky dory.</p>
<pre  name="code" class="xml">
&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;project-modules id=&quot;moduleCoreId&quot; project-version=&quot;1.5.0&quot;&gt;
  &lt;wb-module deploy-name=&quot;foo-war&quot;&gt;
    &lt;wb-resource deploy-path=&quot;/&quot; source-path=&quot;/src/main/webapp&quot;/&gt;
    &lt;wb-resource deploy-path=&quot;/WEB-INF/classes&quot; source-path=&quot;/src/main/java&quot;/&gt;
    &lt;wb-resource deploy-path=&quot;/WEB-INF/classes&quot; source-path=&quot;/src/main/resources&quot;/&gt;
    &lt;property name=&quot;context-root&quot; value=&quot;foo&quot;/&gt;
    &lt;property name=&quot;java-output-path&quot;/&gt;
  &lt;/wb-module&gt;
&lt;/project-modules&gt;
</pre>
<p><a href="http://blog.projectnibble.org/2009/08/24/excluding-test-classes-from-war-ear/trackback/">trackback</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.projectnibble.org/2009/08/24/excluding-test-classes-from-war-ear/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Repost: Make Eclipse ignore .svn directories</title>
		<link>http://blog.projectnibble.org/2009/08/11/repost-make-eclipse-ignore-svn-directories/</link>
		<comments>http://blog.projectnibble.org/2009/08/11/repost-make-eclipse-ignore-svn-directories/#comments</comments>
		<pubDate>Tue, 11 Aug 2009 11:37:28 +0000</pubDate>
		<dc:creator>Benny Bottema</dc:creator>
				<category><![CDATA[eclipse]]></category>

		<guid isPermaLink="false">http://blog.projectnibble.org/?p=353</guid>
		<description><![CDATA[Eclipse has the annoying little problem where Eclipse will copy .svn folders to the target/classes output folder and then complain about duplicate resources. After wading through some vague solutions, here&#8217;s a clear and simple post I found to solve this annoyance.
Make Eclipse ignore .svn directories
Window -> Preferences…, Java -> Compiler -> Building. Under “Output folder” [...]]]></description>
			<content:encoded><![CDATA[<p>Eclipse has the annoying little problem where Eclipse will copy .svn folders to the target/classes output folder and then complain about duplicate resources. After wading through some vague solutions, here&#8217;s a clear and simple post I found to solve this annoyance.</p>
<p><a href="http://radfordbw.squidpower.com/2007/04/12/make-eclipse-ignore-svn-directories/">Make Eclipse ignore .svn directories</a></p>
<blockquote><p>Window -> Preferences…, Java -> Compiler -> Building. Under “Output folder” add “, .svn/” to “Filtered Resources” (so that you get “*.launch, .svn/”).</p></blockquote>
<p>It still works like a charm. Thanks Brian.</p>
<p><a href="http://blog.projectnibble.org/2009/08/11/repost-make-eclipse-ignore-svn-directories/trackback/">trackback</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.projectnibble.org/2009/08/11/repost-make-eclipse-ignore-svn-directories/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Java generic paged lazy List with JSF/JPA example implementation</title>
		<link>http://blog.projectnibble.org/2009/07/22/java-generic-paged-lazy-list-with-jsfjpa-example-implementation/</link>
		<comments>http://blog.projectnibble.org/2009/07/22/java-generic-paged-lazy-list-with-jsfjpa-example-implementation/#comments</comments>
		<pubDate>Wed, 22 Jul 2009 12:58:53 +0000</pubDate>
		<dc:creator>Benny Bottema</dc:creator>
				<category><![CDATA[JSF/Facelets]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[JPA]]></category>
		<category><![CDATA[JSF]]></category>
		<category><![CDATA[lazy loading]]></category>
		<category><![CDATA[paging]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[richfaces]]></category>

		<guid isPermaLink="false">http://blog.projectnibble.org/?p=310</guid>
		<description><![CDATA[Here&#8217;s a list implementation I came up with to enable true paged data fetching completely transparent to any user. It works independent of persistence layers, such as JPA implementations etc.

download PagedList


Lazy loading with the PagedList
Originally I made the PagedList because I needed paged data fetching using JMS queues for a JSF Rich datatable and datascroller; [...]]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s a list implementation I came up with to enable true paged data fetching completely transparent to any user. It works independent of persistence layers, such as JPA implementations etc.</p>
<ul>
<li><a href="http://projectnibble.org/dump/blog/pagedlist/PagedList.rar">download PagedList</a></li>
</ul>
<p><span id="more-310"></span></p>
<h3>Lazy loading with the PagedList</h3>
<p>Originally I made the PagedList because I needed paged data fetching using JMS queues for a JSF Rich datatable and datascroller; a common problem in JSF but less commonly solved, even less documented and less still solved in an elegant way. Most people are able to solve this problem by using an <a href="https://www.hibernate.org/43.html">OpenSessionInView</a> antipattern (keeping Hibernate-oid session open until the view requests the lazy loaded data) but since I&#8217;m using JMS queue, this was not an option. I decided to circumvent the entire JSF to JMS queue integration by providing my data in a paging lazy list. It works beautifully.</p>
<pre  name="code" class="java">
public class PagedList&lt;Dto, QueryParameters&gt; extends AbstractList&lt;Dto&gt; {

    private final QueryParameters queryParameters;

    private final PagedDataProvider&lt;Dto, QueryParameters&gt; pagedDataProvider;

    private final Map&lt;Integer, List&lt;Dto&gt;&gt; fetchedPages;

    private final int dataSize;

    private final int pageSize;

    public PagedList(PagedDataProvider&lt;Dto, QueryParameters&gt; pagedDataProvider, QueryParameters queryParameters) {
        this.pagedDataProvider = pagedDataProvider;
        this.queryParameters = queryParameters;
        fetchedPages = new HashMap&lt;Integer, List&lt;Dto&gt;&gt;();
        dataSize = pagedDataProvider.getDataSize();
        pageSize = pagedDataProvider.getPageSize();
    }

    public Dto get(int index) {
        int pageNr = (int) Math.floor(index / pageSize);
        if (fetchedPages.get(pageNr) == null) {
            fetchedPages.put(pageNr, pagedDataProvider.provide(pageNr, queryParameters));
        }
        return fetchedPages.get(pageNr).get(index % pageSize);
    }

    public int size() {
        return dataSize;
    }
}
</pre>
<p>It is different from <a href="http://commons.apache.org/collections/apidocs/org/apache/commons/collections/list/LazyList.html">Apache&#8217;s LazyList</a> (and many other implementations) in that it relies on an external dataprovider to provide the dataset&#8217;s total size, pagesize and data provision itself. For example, using my lazy list version the <i>size()</i> method actually returns the total size of data potentially available, not just physically available in the list at the present. This way the list works completely transparent to its users. Also, there is no simple factory being passed in that creates dummy objects or something: actual remote data is being fecthed in pages by a data provider. Pages being fetched are independent of each other; when requesting page 5, page 1 through 4 are not need or fetched.</p>
<p>The PagedList works in combo with a <em>PagedDataProvider</em> interface that external dataproviders (some dao for example) could implement. So it has no dependencies whatsoever on some specific implementation. The paged data provider provides the total size of the dataset, the page size and pages of the dataset itself when needed.</p>
<h3>Example implementation for JSF and JPA dao</h3>
<p>Here&#8217;s an example implementation which ties a JSF Richfaces datatable to a JPA dao using a PagedList. I cut out the service layer, view handlers/controllers and whatnot for sake of simplicity.</p>
<pre  name="code" class="xhtml">
&lt;rich:dataTable id="searchResults" value="#{appleService.apples}" var="apple"&gt;
...
&lt;/rich:dataTable&gt;
</pre>
<pre  name="code" class="java">
/**
 * JPA dao which acts as paged data provider for the paged list.
 *
 * Relies on two JPA named queries specified on the Entity being fetched.
 *
 * @author Benny Bottema
 * @see PagedDataProvider
 */
public class AppleJPADao implements AppleDao, PagedDataProvider&lt;AppleDto, AppleQueryParameters&gt; {

    // entitymanager stuff omitted for example (real world: injected by Spring)

    private static final int PAGESIZE = 50;

    /**
     * @see AppleDao#getAllApples(String, String)
     */
    public List&lt;AppleDto&gt; getAllApples(String type, String quality) {
        // optional query parameters: can be null
        AppleQueryParameters params = new AppleQueryParameters();
        params.setAppleType(type);
        params.setAppleQuality(quality);
        return new PagedList&lt;AppleDto, AppleQueryParameters&gt;(this, params);
    }

    /**
     * @see PagedDataProvider#provide(int, Object)
     */
    public List&lt;AppleDto&gt; provide(int page, AppleQueryParameters queryCriteria) {
        Query query = entityManager.createNamedQuery("Apple.findAll");
        query.setParameter("appleType", queryCriteria.getAppleType());
        query.setParameter("appleQuality", queryCriteria.getAppleQuality());
        // JPA's paging mechanism
        query.setFirstResult(PAGESIZE * page);
        query.setMaxResults(PAGESIZE);
        return (List&lt;AppleDto>) query.getResultList();
    }

    /**
     * @see PagedDataProvider#getDataSize()
     */
    public int getDataSize() {
        Query countQuery = entityManager.createNamedQuery("Apple.count");
        return ((Long) countQuery.getSingleResult()).intValue();
    }

    /**
     * @see PagedDataProvider#provide()
     */
    public int getPageSize() {
        return PAGESIZE;
    }
}
</pre>
<h3>Known issues</h3>
<p>So far there is only one known issue, which is more of a problem with lazy loading in general: synchronizing the lazy loading list with the remote dataset to avoid becoming stale when changes happen to the dataset. Example: when the dataset changes, the size of the list is not being updated and the <i>get()</i> method may try to fetch wrong or even unavailable data throwing an exception. Since I&#8217;m using the paged list in a limited way, I&#8217;ve not yet encountered this problem. </p>
<p>One way would be to solve this problem for the Rich datable and datascroller specifically is to have a a4j poll component that listens for dataset changes on the server and refreshes the entire datatable as necessary. Just thinking out loud here&#8230;</p>
<p><a href="http://blog.projectnibble.org/2009/07/22/java-generic-paged-lazy-list-with-jsfjpa-example-implementation">trackback</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.projectnibble.org/2009/07/22/java-generic-paged-lazy-list-with-jsfjpa-example-implementation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Papervision3D clouded planet Earth tutorial and source</title>
		<link>http://blog.projectnibble.org/2009/06/19/papervision3d-clouded-planet-earth-tutorial-and-source/</link>
		<comments>http://blog.projectnibble.org/2009/06/19/papervision3d-clouded-planet-earth-tutorial-and-source/#comments</comments>
		<pubDate>Fri, 19 Jun 2009 17:00:17 +0000</pubDate>
		<dc:creator>Benny Bottema</dc:creator>
				<category><![CDATA[Papervision3D]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[clouds]]></category>
		<category><![CDATA[earth]]></category>
		<category><![CDATA[game design]]></category>
		<category><![CDATA[perlin noise]]></category>
		<category><![CDATA[planet]]></category>

		<guid isPermaLink="false">http://blog.projectnibble.org/?p=209</guid>
		<description><![CDATA[Here's a simulated Earth in Papervision3D, using realtime maintained clouds and a nice Earth glow.]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s a tutorial about a possible way to create planets and indeed Earth. I&#8217;ll briefly explain the main textures used and from then on go through the code step-by-step to explain what I did and why.</p>
<p>I had to tone down the size and fps, and leave out the bumpmap for this demo because my Opera browser was halting when I added the other images and clouds swf demo :P</p>
<p>
<object width="500" height="250">
<param name="movie" value="http://projectnibble.org/dump/blog/papervision3d_earth/Planettutorial.swf"></param>
<param name="quality" value="high"></param>
<param name="wmode" value="window"></param>
<param name="menu" value="false"></param>
<param name="bgcolor" value="#000000"></param>
<embed type="application/x-shockwave-flash" width="500" height="250" src="http://projectnibble.org/dump/blog/papervision3d_earth/Planettutorial.swf" quality="high" bgcolor="#000000" wmode="window" menu="false" ></embed>
</object>
<br />
<span style="color:#999999">(notice the dynamic clouds and the glow around the globe)</span></p>
<ul>
<li><a href="http://projectnibble.org/dump/blog/papervision3d_earth/earthtutorial.rar">Download Clouded planet Earth source</a></li>
</ul>
<p>On my core 2 duo laptop I get about 20-25 fps whith a slightly larger stage size and the movie set to 30 fps. Although the planet looks very nice to me, I also realize it isn&#8217;t very practical for games or other realtime simulations. To get better fps, you can remove the bumpmap texture, replace the realtime clouds with a fixed texture, user fewer segments in the spheres, zoom out more, or use lower resolution textures for land- and cloud materials. A combination of two or more of these suggestions should easily get you above 35 fps.</p>
<p><span id="more-209"></span></p>
<h2>Used Ingredients</h2>
<p>I&#8217;ll get into the explanations later, but here are the ingredients used in this example:</p>
<p>- A sphere for geological data (land texture)<br />
- Another sphere for clouds<br />
- two planes that mask a perfect circle for the spheres<br />
- a plane that contains the fresnell glow on top</p>
<p>On the spheres I&#8217;ve used two phongshaded materials of which one has the land texture with a bumpmap for mountains and stuff, and the other contains the clouds texture with a runtime maintained perlin noise map applied to the alpha channel.</p>
<p>On the masking planes I&#8217;ve used a simple gradient circle shaped fill on the alpha channel.</p>
<p>On the glow plane I&#8217;ve used a gradient circle fill with a blue color and alpha channel for nice fading.</p>
<p>There&#8217;s also a pointlight in there that represents the sun (hits earth top-right).</p>
<h2>Main textures</h2>
<h3>Earth surface</h3>
<p>I took the public Earth surface texture (<a href="http://visibleearth.nasa.gov/view_set.php?categoryID=2364">Blue Marble</a>) from NASA, which I used on the sphere. On the rightside you can see it applied to the sphere with phongshader.</p>
<div style="width: 600px;">
<img src="http://projectnibble.org/dump/blog/papervision3d_earth/earth.jpg" alt="NASA's Earth surface texture" style="float:left;"/><img src="http://projectnibble.org/dump/blog/papervision3d_earth/phongshaded_geological_texture.png" alt="Phong shaded earth texture" style="height:188px; float:right"/>
</div>
<div style="clear:both"> </div>
<p>The shown image is about the half of the size I used for the used texture. One thing I&#8217;ve noticed is how large the texture needs to be to get some decent quality when applied to a sphere. Maybe there&#8217;s some room for optimization there&#8230;</p>
<h3>Clouds surface</h3>
<p><img src="http://projectnibble.org/dump/blog/papervision3d_earth/clouds.jpg" alt="NASA's cloud coverage texture"/></p>
<p>For the clouds I initially started with the public clouds texture from NASA, but when I applied the dynamic visibility map it turned out I didn&#8217;t get enough cloud coverage, because in effect now two visibly maps were applied: nature&#8217;s own, hardcoded in the texture, and my own applied in runtime.</p>
<p>So I ended up mirroring the original clouds texture in both dimensions to get much more cloud coverage, so that when I apply my own runtime visibility map I get about as much cloud coverage as in the original image. When applied to the sphere, it is also subject to the Phong shader I&#8217;ve used.</p>
<div style="background-image: url('http://projectnibble.org/dump/blog/papervision3d_earth/earth.jpg'); width: 600px; height: 188px; background-repeat: no-repeat; line-height: 0px;"> <br />
<img src="http://projectnibble.org/dump/blog/papervision3d_earth/clouds2.png" alt="Expanded cloud coverage with alpha channel" style="float:left"/><img src="http://projectnibble.org/dump/blog/papervision3d_earth/phongshaded_clouds_texture.png" alt="Phong shaded cloud coverage" style="height:188px; float:right"/>
</div>
<div style="clear:both"> </div>
<p>I&#8217;ve increased overall brightness to get more out of the less dense clouds, but I&#8217;ve also increased contrast to get the clear shapes of the clouds. Then I&#8217;ve applied the image itself as its own alpha channel and played around with its visibility until it became what you see above.</p>
<p>For the runtime visibility map I&#8217;ve used the ever amazing <a href="http://polygeek.com/1780_flex_explorer-bitmapdata-perlin-noise">perlin noise effect</a>. Since perlin noise works with a phase, I can use that to animate perlin noise and apply the results to the alpha channel of the clouds texture. On a straight texture, it looks like the following example:</p>
<p><a href="http://projectnibble.org/dump/blog/papervision3d_earth/realtimeclouds.html" target="_blank"><img src="http://projectnibble.org/dump/blog/papervision3d_earth/realtime_clouds.png" alt="Realtime clouds texture demo" /></a><br />
<span style="color:#999999">(click to see in action)</span></p>
<p>You can probably imagine how hard this must be to render each frame: a large clouds texture with alpha channel modified each frame by a new perlin noise map, with a phong shader applied. I see how this won&#8217;t work in a game, so I&#8217;ll probably use a fixed clouds texture in the future, but right now I&#8217;m satisfied with the effect.</p>
<h2>Code walkthrough</h2>
<p>Aside from the actual structuring of the code and Papervision3D usage, there are some interesting aspects to this demo. For example, there&#8217;s the Earth&#8217;s light glow, and the use of masking discs to disguise the low quality of the spheres. I&#8217;ve explained everything in details on this on the following page:</p>
<ul>
<li><a href="/papervision3d-clouded-planet-earth-tutorial-and-source-code-walkthrough/">Read the Earth code walkthrough</a></li>
</ul>
<p>If anyone has suggestion on how to improve performance, please let me know!</p>
<p><a href="http://blog.projectnibble.org/2009/06/19/papervision3d-clouded-planet-earth-tutorial-and-source/trackback/">trackback</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.projectnibble.org/2009/06/19/papervision3d-clouded-planet-earth-tutorial-and-source/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Papervision3D DisplayObject3D helper baseclass for realtime updates</title>
		<link>http://blog.projectnibble.org/2009/06/15/papervision3d-displayobject3d-helper-baseclass-for-realtime-updates/</link>
		<comments>http://blog.projectnibble.org/2009/06/15/papervision3d-displayobject3d-helper-baseclass-for-realtime-updates/#comments</comments>
		<pubDate>Mon, 15 Jun 2009 14:36:34 +0000</pubDate>
		<dc:creator>Benny Bottema</dc:creator>
				<category><![CDATA[Papervision3D]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[real time updates]]></category>

		<guid isPermaLink="false">http://blog.projectnibble.org/?p=281</guid>
		<description><![CDATA[Here's a small class I made for adding realtime updates to DisplayObject3D objects. This class allows for easy time-based updates instead of the traditional frame-based updates.]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s a small class I made for adding realtime updates to DisplayObject3D objects. This class allows for easy time-based updates instead of the traditional frame-based updates.</p>
<ul>
<li><a href="http://projectnibble.org/dump/blog/papervision3d_realtime/RealtimeDisplayObject3D.as">RealtimeDisplayObject3D.as</a></li>
</ul>
<p>I use the convention of adding update methods to all (my own) DisplayObject3D objects, which then update themselves according to preconfigured settings (eg. passed in the constructor). I pass in a Camera3D and that&#8217;s it, let the objects do their own thing. I&#8217;m making use of this convention by extracting this update method to a baseclass which then can do time-based updates instead.</p>
<p><span id="more-281"></span></p>
<p>It only works for new classes you create that normally would extend DisplayObject3D, such as a composite parent DisplayObject3D. For example, if you were to create an <a href="http://blog.projectnibble.org/2009/06/19/papervision3d-clouded-planet-earth-tutorial-and-source/">Earth object in PV3D</a>, you could create a new DisplayObject3D container class which adds a planet, clouds and a glow to itself as child objects. This Earth object could then extend our special class, RealtimeDisplayObject3D, for controlling time-based updates.</p>
<pre  name="code" class="java">
public class Planet extends RealtimeDisplayObject3D {

	private var earth:DisplayObject3D;

	// define: 1 update every 200ms (5 updates per second) and process 1 value per second
	public function Planet() {
		super(200, 1);
		// add planet, clouds and glow etc.
	}

	// rotates Earth 5 times per second, with a stepsize of 0.2 (1 second / updates per second)
	protected override function realtimeUpdate(camera:Camera3D, stepsize:Number):void {
		earth.rotate(stepsize);
	}
}
</pre>
<p>With the planet now extending our RealtimeDisplayObject3D class instead of DisplayObject3D, you can provide realistic rotation speed that is not influenced by the FPS the movie is pulling. Instead, it works by applying a set value-per-second (phase speed) and a required update frequency per second; the phase speed is spread out over the number of updates per second.</p>
<p>There is one requirement though: <strong>the fps the movie is pulling cannot go below the updates per second specified</strong>, or the updates per second is capped by the frames per second. If you specify 200ms for each update, this means 5 updates per second (1000 / 200). This won&#8217;t be a problem. If however you define 50ms for each update, this means 20 updates per second (1000 / 50). You must ensure that your movie is running on 20 frames per second or more.</p>
<h2>Combining time-based updates with frame-based updates</h2>
<p>You can still update normally on frame-based timetable by overriding the <i>update(camera)</i> method of the baseclass. Or, you can combine both type of updates by overriding <i>update(camera)</i>, do a frame-based update, call <i>super.update(camera)</i> and override the <i>realtimeUpdate(camera)</i> method as we did before. Here&#8217;s an example of that:</p>
<pre  name="code" class="java">
public class Planet extends RealtimeDisplayObject3D {

	private var earth:DisplayObject3D;

	// define: 1 update every 200ms (5 updates per second) and process 1 value per second
	public function Planet() {
		super(200, 1);
		// add planet, clouds and glow etc.
	}

	// makes the planet face the camera each and every frame
	protected override function update(camera:Camera3D, stepsize:Number):void {
		earth.lookAt(camera);
		super.update(camera);
	}

	// rotates Earth 5 times per second, with a stepsize of 0.2 (1 second / updates per second)
	protected override function realtimeUpdate(camera:Camera3D, stepsize:Number):void {
		earth.rotate(stepsize);
	}
}
</pre>
<p><a href="http://blog.projectnibble.org/2009/06/15/papervision3d-displayobject3d-helper-baseclass-for-realtime-updates/trackback/">trackback</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.projectnibble.org/2009/06/15/papervision3d-displayobject3d-helper-baseclass-for-realtime-updates/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Easy Papervision3D space dust tutorial and source</title>
		<link>http://blog.projectnibble.org/2009/05/29/easy-papervision3d-space-dust-tutorial-and-source/</link>
		<comments>http://blog.projectnibble.org/2009/05/29/easy-papervision3d-space-dust-tutorial-and-source/#comments</comments>
		<pubDate>Fri, 29 May 2009 17:43:53 +0000</pubDate>
		<dc:creator>Benny Bottema</dc:creator>
				<category><![CDATA[Papervision3D]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[game design]]></category>
		<category><![CDATA[space dust]]></category>

		<guid isPermaLink="false">http://blog.projectnibble.org/?p=161</guid>
		<description><![CDATA[Many space shooters use this concept where you are the pilot that looks out from the front window, lasering down enemies for bounty, right. Ever noticed spacedust? It&#8217;s very subtle when it is used, but it adds to the realism of outer space, making the space feel less empty, less static.
Here&#8217;s how you can do [...]]]></description>
			<content:encoded><![CDATA[<p>Many space shooters use this concept where you are the pilot that looks out from the front window, lasering down enemies for bounty, right. Ever noticed spacedust? It&#8217;s very subtle when it is used, but it adds to the realism of outer space, making the space feel less empty, less static.</p>
<p>Here&#8217;s how you can do that in Papervision3D, using ParticleFields. I’ve used the trunk of the papervision code repository, revision 851, but it should work with the last release without to much hassle. Note that I&#8217;m no PV3D guru; I just started to learn this stuff and I&#8217;m just sharing what I&#8217;ve learned so far.</p>
<div style="text-align:center">
<ul>
<li><a href="http://projectnibble.org/dump/blog/papervision3d_spacedust/spacedusttutorial.rar">download spacedust source</a> </li>
</ul>
<p>
<object width="600" height="300">
<param name="movie" value="http://projectnibble.org/dump/blog/papervision3d_spacedust/Spacedusttutorial.swf"></param>
<param name="quality" value="high"></param>
<param name="wmode" value="window"></param>
<param name="menu" value="false"></param>
<param name="bgcolor" value="#000000"></param>
<embed type="application/x-shockwave-flash" width="600" height="300" src="http://projectnibble.org/dump/blog/papervision3d_spacedust/Spacedusttutorial.swf" quality="high" bgcolor="#000000" wmode="window" menu="false" ></embed>
</object>
<br />
<span style="color:999999">(note: space dust uses the skybox from the <a href="http://blog.projectnibble.org/2009/05/22/easy-papervision3d-skybox-tutorial-and-source/">skybox tutorial</a>)</span>
</div>
<ul>Here&#8217;s how it works: </p>
<li>divide space into a 3d grid and parts of the spacedust cloud on 8 gridpoints directly around the camera (the ship), we&#8217;ll call these dustpockets</li>
<li>when the camera moves, dustpockets are removed and created to keep only the 8 gridpoints around the ship occupied</li>
</ul>
<p><span id="more-161"></span></p>
<h3>Divide space into a 3d grid and snap dust pockets on it</h3>
<p>We&#8217;re not actually creating a grid, but we&#8217;re going to say things like: &#8220;Give me the closest 3d point behind the ship in the upper left corner on the invisible grid&#8221;, so that we can create a dust pocket in that position. Sounds complicated? Luckily, the math is easy:</p>
<p>Let&#8217;s say there&#8217;s only the x-axis. Now from 0 to 100, I want the gridpoint on the left of 32 on grid with size 5. Here is how it would look:</p>
<div style="text-align:center">
<img src="http://projectnibble.org/dump/blog/papervision3d_spacedust/grid_x_example.png" alt="gridpoint 30 lies left of x-value 32 on a grid of size 5" />
</div>
<p>As you can see, the x-value 30 is the value to the left of 32 on a grid with size 5. Here&#8217;s the equation to get to that answer:</p>
<p><code>gridpoint left: Xgrid = Xvalue - (Xvalue % gridsize)<br />
gridpoint right: Xgrid = Xvalue - (Xvalue % gridsize) + gridsize</code></p>
<p>Example gridpoint left for value 32 and grid size 5:<br />
<code>gridpoint left: Xgrid = 32 - (32 % 5)<br />
gridpoint left: Xgrid = 32 - (2)<br />
gridpoint left: Xgrid = 30</code></p>
<p>Example gridpoint right for value 32 and grid size 5:<br />
<code>gridpoint left: Xgrid = 32 - (32 % 5) + 5<br />
gridpoint left: Xgrid = 32 - (2) + 5<br />
gridpoint left: Xgrid = 35</code></p>
<p>We&#8217;re going to apply this kind of math to find all 8 gridpoints around the camera. So not just the x-axis, but y and z-axis as well.</p>
<p>The reason we need these 8 gridpoints is because we can&#8217;t just create a single cloud. Since the ship can move in any direction and look in any direction, but we can&#8217;t fill all of space with dust particles due to memory restrictions, we need to dynamically &#8216;move&#8217; the cloud with the ship, while keeping the dust particles on the same place. The only way to do this is by dividing the single dust cloud into several small ones and create them as the ship goes. At the same time we need to keep the number of particles down, so we need the minimum dust pockets and still cover all sides by create 8 small clouds that together form a big cloud and can grow in any direction without draining too much memory and cpu power.</p>
<p>Here&#8217;s how we&#8217;re going to place the clouds:</p>
<p><img src="http://projectnibble.org/dump/blog/papervision3d_spacedust/3d%20spacedust%20grid.png" alt="space dust pockets around the ship on a grid" /></p>
<p>Here&#8217;s the code to do this:</p>
<pre  name="code" class="java">
public function update(fieldsize:Number = 2500):void {
	// calculate segment (dustpocket) size
	const FIELDSIZE_POCKET:Number = fieldsize / 5;

	// determine the 8 gridpositions around the ship (eg. 1: front bottom left, 2: behind top right)
	const pocketPositions:Array = new Array();
	pocketPositions.push(calcGridPosition(camera.position, FIELDSIZE_POCKET, false, false, false));
	pocketPositions.push(calcGridPosition(camera.position, FIELDSIZE_POCKET, true, false, false));
	pocketPositions.push(calcGridPosition(camera.position, FIELDSIZE_POCKET, false, true, false));
	pocketPositions.push(calcGridPosition(camera.position, FIELDSIZE_POCKET, false, false, true));
	pocketPositions.push(calcGridPosition(camera.position, FIELDSIZE_POCKET, true, true, false));
	pocketPositions.push(calcGridPosition(camera.position, FIELDSIZE_POCKET, true, false, true));
	pocketPositions.push(calcGridPosition(camera.position, FIELDSIZE_POCKET, false, true, true));
	pocketPositions.push(calcGridPosition(camera.position, FIELDSIZE_POCKET, true, true, true));
}

/**
 * Calculates a gridpoint around the ship.
 */
private function calcGridPosition(position:Number3D, gridSize:Number, xMin:Boolean, yMin:Boolean, zMin:Boolean):Number3D {
	const gridPosition:Number3D = new Number3D();
	gridPosition.x = (position.x - (position.x % gridSize)) + ((xMin) ? 0 : gridSize);
	gridPosition.y = (position.y - (position.y % gridSize)) + ((yMin) ? 0 : gridSize);
	gridPosition.z = (position.z - (position.z % gridSize)) + ((zMin) ? 0 : gridSize);
	return gridPosition;
}
</pre>
<p>This is really all there is to it. The only thing we need to do now is create new dust pockets for gridpoints without one and remove dust pockets for points that are not within range anymore. Over and over again. Here&#8217;s the entire Spacedust class:</p>
<pre  name="code" class="java">
package org.codemonkey.spacedusttutorial.papervision3d {
	import flash.utils.Dictionary;
	import org.papervision3d.cameras.Camera3D;
	import org.papervision3d.core.math.Number3D;
	import org.papervision3d.materials.special.ParticleMaterial;
	import org.papervision3d.objects.special.ParticleField;
	import org.papervision3d.scenes.Scene3D;

	/**
	 * Spacedust simulator. Creates 8 segments of the dustcloud around the given camera at any point in space.
	 * When the camera moves, new dustpockets are generated and old ones (out of sight) are removed.
	 *
	 * @author Benny Bottema
	 */
	public class Spacedust {

		private static const DEFAULT_PARTICLEMATERIAL:ParticleMaterial = new ParticleMaterial(0xffffff, .5, ParticleMaterial.SHAPE_CIRCLE);

		private var basic3DSetup:Basic3DSetup;
		private var camera:Camera3D;
		private var spacedustCloud:Dictionary;

		private var fieldsize:Number;
		private var particleCount:Number;
		private var dustMaterial:ParticleMaterial = new ParticleMaterial(0xffffff, .5, ParticleMaterial.SHAPE_CIRCLE);

		public function Spacedust(basic3DSetup:Basic3DSetup, camera:Camera3D = null, fieldsize:Number = 2500, particleCount:Number = 400, dustMaterial:ParticleMaterial = null) {
			spacedustCloud = new Dictionary();
			this.basic3DSetup = basic3DSetup;
			this.camera = (camera != null) ? camera : basic3DSetup.camera;
			this.fieldsize = fieldsize;
			this.particleCount = particleCount;
			this.dustMaterial = (dustMaterial != null) ? dustMaterial : DEFAULT_PARTICLEMATERIAL;
		}

		public function update():void {
			// calculate segment (dustpocket) size
			const FIELDSIZE_POCKET:Number = fieldsize / 5;

			// determine the 8 gridpositions around the ship (eg. 1: front bottom left, 2: behind top right)
			var pocketPositions:Array = new Array();
			pocketPositions.push(calcGridPosition(camera.position, FIELDSIZE_POCKET, false, false, false));
			pocketPositions.push(calcGridPosition(camera.position, FIELDSIZE_POCKET, true, false, false));
			pocketPositions.push(calcGridPosition(camera.position, FIELDSIZE_POCKET, false, true, false));
			pocketPositions.push(calcGridPosition(camera.position, FIELDSIZE_POCKET, false, false, true));
			pocketPositions.push(calcGridPosition(camera.position, FIELDSIZE_POCKET, true, true, false));
			pocketPositions.push(calcGridPosition(camera.position, FIELDSIZE_POCKET, true, false, true));
			pocketPositions.push(calcGridPosition(camera.position, FIELDSIZE_POCKET, false, true, true));
			pocketPositions.push(calcGridPosition(camera.position, FIELDSIZE_POCKET, true, true, true));

			var newSpacedustCloud:Dictionary = new Dictionary();

			// create new dustfields or transfer existing ones (and remove existing ones the scene)
			const particlesPerPocket:Number = particleCount / pocketPositions.length;
			for each (var pocketPosition:Number3D in pocketPositions) {
				var pocketKey:String = pocketPosition.toString();
				var reusableDustPocket:ParticleField = spacedustCloud[pocketKey];
				// remove so we can delete the particles from the remaining particlefields (which means they are out of sight)
				delete spacedustCloud[pocketKey];
				// add the removed particlefield to the new dustcloud or create one if dustpocket is new for te current gridposition around the ship
				if (reusableDustPocket != null) {
					basic3DSetup.removeFromScene(reusableDustPocket);
					newSpacedustCloud[pocketKey] = reusableDustPocket;
				} else {
					newSpacedustCloud[pocketKey] = new ParticleField(dustMaterial, particlesPerPocket, 2, fieldsize, fieldsize, fieldsize);
					newSpacedustCloud[pocketKey].x = pocketPosition.x;
					newSpacedustCloud[pocketKey].y = pocketPosition.y;
					newSpacedustCloud[pocketKey].z = pocketPosition.z;
				}
			}

			// manually remove all the remaining obsolete particles from the old cloud (they are out of sight)
			for each (var oldDustPocket:ParticleField in spacedustCloud) {
				oldDustPocket.removeAllParticles();
			}

			// now add the old dustpockets which are still within sight, plus the new dustpockets to the scene
			for each (var newDustPocket:ParticleField in newSpacedustCloud) {
				basic3DSetup.addToScene(newDustPocket);
			}

			// replace the old cloud with the new cloud
			spacedustCloud = newSpacedustCloud;
		}

		/**
		 * Calculates a gridpoint around the ship.
		 */
		private function calcGridPosition(position:Number3D, gridSize:Number, xMin:Boolean, yMin:Boolean, zMin:Boolean):Number3D {
			const gridPosition:Number3D = new Number3D();
			gridPosition.x = (position.x - (position.x % gridSize)) + ((xMin) ? 0 : gridSize);
			gridPosition.y = (position.y - (position.y % gridSize)) + ((yMin) ? 0 : gridSize);
			gridPosition.z = (position.z - (position.z % gridSize)) + ((zMin) ? 0 : gridSize);
			return gridPosition;
		}
	}
}
</pre>
<ul>
<li><a href="http://projectnibble.org/dump/blog/papervision3d_spacedust/Spacedust.as">Download Spacedust.as</a></li>
<li><a href="http://projectnibble.org/dump/blog/papervision3d_spacedust/spacedusttutorial.rar">download entire spacedust source</a> </li>
</ul>
<p>Notice the particel fields we&#8217;re using to create the eight dust pockets with.</p>
<p><code>new ParticleField(dustMaterial, particlesPerPocket, 2, fieldsize, fieldsize, fieldsize);</code></p>
<p>Also, the <i>Basic3DSetup</i> class I&#8217;ve used is simply a convenience class with default camera, scene, viewport etc. It&#8217;s much like Papervision&#8217;s own <a href="http://pv3d.org/2008/12/06/what-is-basicview/">BasicView</a> -which I didn&#8217;t know about at the time- except even cleaner.</p>
<p>The Spacedust class accepts an optional camera to position the dust cloud around. Since this approach is meant to create the illusion of infinite space dust, it doesn&#8217;t make sense to create clouds on non-camera&#8217;s. Also, you can specify your own particle count and dust material in the constructor of the Spacedust class. The example at the top of the page uses only the defaults.</p>
<p><a href="http://blog.projectnibble.org/2009/05/29/easy-papervision3d-space-dust-tutorial-and-source/trackback/">trackback</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.projectnibble.org/2009/05/29/easy-papervision3d-space-dust-tutorial-and-source/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>JSF is a diamond in the rough, you just need to make it shine</title>
		<link>http://blog.projectnibble.org/2009/05/24/jsf-is-a-diamond-in-the-rough-you-just-need-to-make-it-shine/</link>
		<comments>http://blog.projectnibble.org/2009/05/24/jsf-is-a-diamond-in-the-rough-you-just-need-to-make-it-shine/#comments</comments>
		<pubDate>Sun, 24 May 2009 13:30:53 +0000</pubDate>
		<dc:creator>Benny Bottema</dc:creator>
				<category><![CDATA[JSF/Facelets]]></category>
		<category><![CDATA[PrettyFaces]]></category>
		<category><![CDATA[a4j]]></category>
		<category><![CDATA[facelets]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[JSF]]></category>
		<category><![CDATA[richfaces]]></category>
		<category><![CDATA[seam]]></category>
		<category><![CDATA[tomahawk]]></category>

		<guid isPermaLink="false">http://blog.projectnibble.org/?p=108</guid>
		<description><![CDATA[
[RE: JSF – Still pretty much a steaming pile of donkey turd] &#8211; I was replying to Wille Faler’s post about why JSF sucks, when the comment was getting too large, so I made it into a post on my own blog.


JSF is hard to learn yes, but in my opinion it doesn&#8217;t suck as [...]]]></description>
			<content:encoded><![CDATA[<div style="color:#999999">
<p>[RE: JSF – Still pretty much a steaming pile of donkey turd] &#8211; I was replying to <a href="http://faler.wordpress.com/2007/03/13/jsf-still-pretty-much-a-steaming-pile-of-donkey-turd/">Wille Faler’s post about why JSF sucks</a>, when the comment was getting too large, so I made it into a post on my own blog.</p>
</div>
<hr />
<p><b>JSF is hard to learn yes, but in my opinion it doesn&#8217;t suck as bad as Wille says. All the points Wille mentions can be solved by certain libraries or write-once reusable solutions (I&#8217;ve included my &#8216;magical&#8217; combination of frameworks on the bottom). JSF is a diamond in the rough, you just need to make it shine.</b></p>
<p><span id="more-108"></span></p>
<h3>The devil is in the details</h3>
<p>There are a couple of pitfalls you need to know about with JSF. It&#8217;s all really a matter of having read a good book about JSF that points them out, or having learned them the hard way. Luckily I&#8217;ve had some guidance by experienced colleagues of mine; I&#8217;m guessing that&#8217;s the missing factor in Wille&#8217;s situation.</p>
<p>Referring to the pitfalls you need to know about, I&#8217;m talking about the common beginner&#8217;s problems like using JSTL c:tags that cause havoc when combined with those of jsf and facelets. I&#8217;m talking about the Ajax4Jsf details as Wille mentioned: once you know what to use it&#8217;s easy. Those 48 attributes? most of them are inherited and never used and you can forget about them, and that&#8217;s the learning curve. Once you know the right ones, it&#8217;s using exactly those, over and over again. And things like how you can incorporate Spring security and stuff like that. Another pitfall that comes to mind is how action=&#8221;" work quirky with navigation rules when using the array notation (action=&#8221;controller['action']&#8220;). And ofcourse there&#8217;s the issue of <a href="http://blog.projectnibble.org/2008/07/26/parameterized-jsf-facelets-validators/">parameterized JSF validators</a>, which needs some understanding. All pitfalls that can baffle a beginner or intermediate.</p>
<p>The thing with JSF is there are a number of little things that are hard to solve, but once you&#8217;re past that it <i>does</i> make you much more productive. You just have to have a solid base to start from each project. The pitfalls learning curve is definitely a downside of JSF, but <a href="http://blogs.jsfcentral.com/jsf2group/entry/jsf_2_primer">JSF 2.0</a> is in the making to solve all these issues.</p>
<h3>Bad performance or beginner&#8217;s mistake?</h3>
<p>Then there is this performance issue Wille mentions, where I think he missed the target: JSF does not have to be slow at all. It really depends on how you treat your backing beans. JSF has the weird tendency to call setters/getters multiple times, which seems utterly pointless (it&#8217;s related to resolving valuebindings in nested components). I&#8217;ve made the mistake before to directly retrieve values from a controller/service in my views instead of a static model (backing bean): this is what makes things slow. JSF in itself isn&#8217;t that slow for a webapp: yes there are many redundant calls but we&#8217;re not dealing with a realtime simulation application, webapps do just fine. On a sidenote, it also depends on the implementation&#8230; JSF is a reference specification, while there are various implementations of the framework (IBM&#8217;s, Sun&#8217;s etc.)</p>
<h3>SEO friendly JSF?</h3>
<p>Ahh, but for the <a href="http://blog.projectnibble.org/2009/03/20/bookmarkable-and-seo-friendly-jsf/">JSF Search Engine Friendliness</a>. That&#8217;s solved too, somewhat. There&#8217;s is a framework called <a href="http://ocpsoft.com/prettyfaces/">PrettyFaces</a> which allows you to dynamically generate/interpret pretty GET urls with bean values. It&#8217;s a great framework that solves a common problem, but there still exists the problem that existing jsf/facelets/richfaces components are not generating SEO HTML code. That&#8217;s really the fault of the component makers, not JSF, though JSF definitely isn&#8217;t innocent with its POST obsession: PrettyFaces fixes at least that.</p>
<h3>Common sense: use frameworks to avoid most pitfalls</h3>
<p>Once you&#8217;ve got those kind of things down and use a good framework combo, JSF is straightforward. You won&#8217;t really need to know about the various phases and you can be very productive if you&#8217;ve set up the jsf/facelets components right. I&#8217;ve worked with JSF intensively for about a year now in various projects with various implementations and I hardly ever need to use phaselisteners and that sort of stuff. The rare cases where you do need them, you can define these things ones and reuse them in other projects. With this approach I&#8217;ve created a template project with some standard libraries, phaselisteners, security set up, facelets components etc. and I&#8217;m up and running in no time.</p>
<p>I haven&#8217;t sanitized or otherwise cleaned up the template project so I won&#8217;t put that up for the time being, but at least I can tell you what frameworks are in it by default. </p>
<ul>The magical combination that works for me and my colleagues: </p>
<li>JSF (MVC)</li>
<li>Facelets (Templating, use of XHTML)</li>
<li><a href="http://livedemo.exadel.com/richfaces-demo/richfaces/inputNumberSlider.jsf?c=inputNumberSlider&#038;tab=usage">Richfaces</a> (component suite)</li>
<li><a href="http://livedemo.exadel.com/richfaces-demo/richfaces/commandButton.jsf?c=commandButton&#038;tab=usage">Richfaces A4J</a> (Ajax support)</li>
<li><a href="http://myfaces.apache.org/tomahawk/index.html">Tomahawk</a> (only for <a href="http://myfaces.apache.org/tomahawk/extensionsFilter.html">file uploads with the Extensions Filter</a>)</li>
<li>PrettyFaces (for indexable GET urls)</li>
</ul>
<p>Now that I&#8217;ve mentioned these, you should check out <a href="http://www.jboss.com/products/seam/">JBoss Seam</a> as well, which basically is supposed to do everything the above libraries do combined, plus a couple of things more like solving the <a href="http://matthiaswessendorf.wordpress.com/2008/11/19/conversation-scope-and-jsf-or-your-favorite-web-framework/">conversational scope</a> issue. I haven&#8217;t worked with this framework myself though so I can&#8217;t speak from experience.</p>
<h3>Final note</h3>
<p>Although I agree JSF has some annoying pitfalls, shortcomings and a high learning curve, it&#8217;s been out there for enough time to pass for supplement frameworks to pop up from under the ground to solve most of these problems.</p>
<p>With the right combination, most classic counter arguments have become obsolete. Maybe it&#8217;s time for critics to take JSF as is: incomplete but ultimately making you more productive if combined with the right frameworks.</p>
<p><a href="http://blog.projectnibble.org/2009/05/24/jsf-is-a-diamond-in-the-rough-you-just-need-to-make-it-shine/trackback/">trackback</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.projectnibble.org/2009/05/24/jsf-is-a-diamond-in-the-rough-you-just-need-to-make-it-shine/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
	</channel>
</rss>
