<?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>Promethe's Blog &#187; Snippets</title>
	<atom:link href="http://blog.promethe.net/category/snippets/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.promethe.net</link>
	<description>Web, RIAs and chocolate spaghettis...</description>
	<lastBuildDate>Tue, 29 Jun 2010 09:24:39 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Google Maps 3D Overlay</title>
		<link>http://blog.promethe.net/2010/02/03/google-maps-3d-overlay/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=google-maps-3d-overlay</link>
		<comments>http://blog.promethe.net/2010/02/03/google-maps-3d-overlay/#comments</comments>
		<pubDate>Wed, 03 Feb 2010 13:50:11 +0000</pubDate>
		<dc:creator>Promethe</dc:creator>
				<category><![CDATA[Flash]]></category>
		<category><![CDATA[Snippets]]></category>

		<guid isPermaLink="false">http://blog.promethe.net/?p=630</guid>
		<description><![CDATA[The Flash Google Maps API offers what they call a 3D map. It's nothing more than a component that enables a perspective view of a classic 2D map with the associated controls. Here is how it works: The 3D view is engine independant and uses Flash 10 3D maths and "drawTriangles" method if available The [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://blog.promethe.net/wp-content/uploads/2010/02/minko_gmaps_eiffel.png" rel="shadowbox[post-630];player=img;"></a>The Flash Google Maps API offers what they call a 3D map. It's nothing more than a component that enables a perspective view of a classic 2D map with the associated controls. Here is how it works:</p>
<ol>
<li>The 3D view is engine independant and uses Flash 10 3D maths and "drawTriangles" method if available</li>
<li>The API provides everything one needs to integrate 3D graphics as an overlay using the library of its choice</li>
<li>It works just the same as the good old 2D Google Maps API</li>
</ol>
<p>The first point is very important. It ensures the library is lightweight and does not include any third party software you wouldn't want to use.</p>
<p>The second is what makes the magic possible: the API exposes the viewport and camera data and provides methods to convert latitude/longitude into 3D world coordinates. With a little math and a few hours of debugging it is then easy to wrap it with the 3D engine of your choice.</p>
<h4>The Experiment</h4>
<p style="text-align: center;"><a href="http://blog.promethe.net/wp-content/uploads/2010/02/Map3D.swf" rel="shadowbox[gmaps];width=600;height=440"><img class="aligncenter" title="minko_gmaps_eiffel" src="http://blog.promethe.net/wp-content/uploads/2010/02/minko_gmaps_eiffel.png" alt="" width="550" height="310" /></a></p>
<p>You can use CTRL + mouse drag or SHIFT + mouse drag to look around.</p>
<h4>The Code</h4>
<p>You might want to read the official <a href="http://code.google.com/intl/fr-FR/apis/maps/documentation/flash/intro.html" target="_blank">Google Maps Flash API documentation</a> first, including the dedicated <a href="http://code.google.com/intl/fr-FR/apis/maps/documentation/flash/3d-maps.html" target="_blank">3D maps section</a>.</p>
<h5>The Viewport</h5>
<p>The Google Maps API provides the viewport width and height and the focal length. I use the field-of-view instead of the focal length but there is a simple formula to convert from one to the other:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #6699cc; font-weight: bold;">var</span> fieldOfView <span style="color: #000000; font-weight: bold;">:</span> <span style="color: #004993;">Number</span> = 2. <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #004993;">Math</span>.<span style="color: #004993;">atan</span><span style="color: #000000;">&#40;</span>viewportWidth <span style="color: #000000; font-weight: bold;">/</span> <span style="color: #000000;">&#40;</span>2. <span style="color: #000000; font-weight: bold;">*</span> focalLength<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;</pre></div></div>

<p>Another issue is to find proper values for the near and far clipping planes. I chose 0.000001 for the near plane and 0.2 for the far plane. Those values are extremely low and might cause floating point inconsistencies. The best thing to do would be to scale the whole scene to be able to use reasonable clipping values. Maybe for a later version! This problem does not seem to affect the experiment though...<br />
The following function presents how to get the relevant viewport values from the Google Maps API:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> buildViewport<span style="color: #000000;">&#40;</span>myMap <span style="color: #000000; font-weight: bold;">:</span> Map3D<span style="color: #000000;">&#41;</span> <span style="color: #000000; font-weight: bold;">:</span> <span style="color: #0033ff; font-weight: bold;">void</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #6699cc; font-weight: bold;">var</span> g <span style="color: #000000; font-weight: bold;">:</span> TransformationGeometry = myMap.camera.getTransformationGeometry<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
	<span style="color: #6699cc; font-weight: bold;">var</span> <span style="color: #004993;">width</span> <span style="color: #000000; font-weight: bold;">:</span> <span style="color: #004993;">Number</span> = g.viewportSize.<span style="color: #004993;">x</span>;
	<span style="color: #6699cc; font-weight: bold;">var</span> <span style="color: #004993;">height</span> <span style="color: #000000; font-weight: bold;">:</span> <span style="color: #004993;">Number</span> = g.viewportSize.<span style="color: #004993;">y</span>;
	<span style="color: #6699cc; font-weight: bold;">var</span> fieldOfView <span style="color: #000000; font-weight: bold;">:</span> <span style="color: #004993;">Number</span> = 2. <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #004993;">Math</span>.<span style="color: #004993;">atan</span><span style="color: #000000;">&#40;</span>g.viewportSize.<span style="color: #004993;">y</span> <span style="color: #000000; font-weight: bold;">/</span> <span style="color: #000000;">&#40;</span>2. <span style="color: #000000; font-weight: bold;">*</span> g.focalLength<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
	<span style="color: #009900;">// build viewport...</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<h5>The Camera</h5>
<p>The following function retrieves the camera position and look-at vectors from the Google Maps API:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> buildCamera<span style="color: #000000;">&#40;</span>myMap <span style="color: #000000; font-weight: bold;">:</span> Map3D<span style="color: #000000;">&#41;</span> <span style="color: #000000; font-weight: bold;">:</span> <span style="color: #0033ff; font-weight: bold;">void</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #6699cc; font-weight: bold;">var</span> cam <span style="color: #000000; font-weight: bold;">:</span> ICamera = myMap.camera;
	<span style="color: #6699cc; font-weight: bold;">var</span> mapCenter <span style="color: #000000; font-weight: bold;">:</span> <span style="color: #004993;">Point</span> = cam.latLngToWorld<span style="color: #000000;">&#40;</span>cam.center<span style="color: #000000;">&#41;</span>;
	<span style="color: #6699cc; font-weight: bold;">var</span> g <span style="color: #000000; font-weight: bold;">:</span> TransformationGeometry = cam.getTransformationGeometry<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
	<span style="color: #6699cc; font-weight: bold;">var</span> pos <span style="color: #000000; font-weight: bold;">:</span> Point3D = g.cameraPosition;
	<span style="color: #6699cc; font-weight: bold;">var</span> zAxis <span style="color: #000000; font-weight: bold;">:</span> Point3D = g.cameraZAxis;
	<span style="color: #6699cc; font-weight: bold;">var</span> yaw <span style="color: #000000; font-weight: bold;">:</span> <span style="color: #004993;">Number</span> = cam.attitude.yaw <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000;">&#40;</span><span style="color: #004993;">Math</span>.<span style="color: #004993;">PI</span> <span style="color: #000000; font-weight: bold;">/</span> 180.<span style="color: #000000;">&#41;</span>;
	<span style="color: #6699cc; font-weight: bold;">var</span> lookAt <span style="color: #000000; font-weight: bold;">:</span> Vector3D = <span style="color: #0033ff; font-weight: bold;">new</span> Vector3D<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
	<span style="color: #6699cc; font-weight: bold;">var</span> eye <span style="color: #000000; font-weight: bold;">:</span> Vector3D = <span style="color: #0033ff; font-weight: bold;">new</span> Vector3D<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
	eye.<span style="color: #004993;">x</span> = pos.<span style="color: #004993;">x</span> <span style="color: #000000; font-weight: bold;">-</span> <span style="color: #000000; font-weight:bold;">128</span>;
	eye.<span style="color: #004993;">y</span> = pos.z;
	eye.z = <span style="color: #000000; font-weight:bold;">128</span> <span style="color: #000000; font-weight: bold;">-</span> pos.<span style="color: #004993;">y</span>;
&nbsp;
	<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight: bold;">!</span>zAxis.<span style="color: #004993;">x</span> <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> <span style="color: #000000; font-weight: bold;">!</span>zAxis.<span style="color: #004993;">y</span><span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		lookAt.<span style="color: #004993;">x</span> = eye.<span style="color: #004993;">x</span> <span style="color: #000000; font-weight: bold;">+</span> EPSILON <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #004993;">Math</span>.<span style="color: #004993;">sin</span><span style="color: #000000;">&#40;</span>yaw<span style="color: #000000;">&#41;</span>;
		lookAt.z = eye.z <span style="color: #000000; font-weight: bold;">+</span> EPSILON <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #004993;">Math</span>.<span style="color: #004993;">cos</span><span style="color: #000000;">&#40;</span>yaw<span style="color: #000000;">&#41;</span>;
	<span style="color: #000000;">&#125;</span>
	<span style="color: #0033ff; font-weight: bold;">else</span>
	<span style="color: #000000;">&#123;</span>
		lookAt.<span style="color: #004993;">x</span> = eye.<span style="color: #004993;">x</span> <span style="color: #000000; font-weight: bold;">-</span> zAxis.<span style="color: #004993;">x</span>;
		lookAt.z = eye.z <span style="color: #000000; font-weight: bold;">+</span> zAxis.<span style="color: #004993;">y</span>;
	<span style="color: #000000;">&#125;</span>
&nbsp;
	lookAt.<span style="color: #004993;">y</span> = eye.<span style="color: #004993;">y</span> <span style="color: #000000; font-weight: bold;">-</span> zAxis.z;
&nbsp;
	<span style="color: #009900;">// build camera using eye position, look-at and Vector3D.Y_AXIS as the up vector</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>I prefer computing the eye position and look-at vectors and using them to create the transform matrix rather than just creating the matrix directly. This way, you can expose the actual camera parameters (even if in read-only) and still use the transform matrix to do the math.</p>
<h5>The Scene</h5>
<p>The last step is to make it possible to position 3D objects on the map. What you actually want to do is to be able to add objects specifying their latitude/longitude instead of their actual (x, y, z) coordinates. Then again, the Google Maps API does it just fine:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> computePosition<span style="color: #000000;">&#40;</span>myLatitude <span style="color: #000000; font-weight: bold;">:</span> <span style="color: #004993;">Number</span>, myLongitude <span style="color: #000000; font-weight: bold;">:</span> <span style="color: #004993;">Number</span><span style="color: #000000;">&#41;</span> <span style="color: #000000; font-weight: bold;">:</span> Vector3D
<span style="color: #000000;">&#123;</span>
	<span style="color: #6699cc; font-weight: bold;">var</span> pos <span style="color: #000000; font-weight: bold;">:</span> <span style="color: #004993;">Point</span> = _map.camera.latLngToWorld<span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">new</span> LatLng<span style="color: #000000;">&#40;</span>myLatitude, myLongitude<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
	<span style="color: #0033ff; font-weight: bold;">return</span> <span style="color: #0033ff; font-weight: bold;">new</span> Vector3D<span style="color: #000000;">&#40;</span>pos.<span style="color: #004993;">x</span> <span style="color: #000000; font-weight: bold;">-</span> 128., 0., 128. <span style="color: #000000; font-weight: bold;">-</span> pos.<span style="color: #004993;">y</span><span style="color: #000000;">&#41;</span>;
<span style="color: #000000;">&#125;</span></pre></div></div>

<h4>Know Issues</h4>
<p>As said previously, the extremely low far and near clipping plane values might introduce float point inconsistencies. Therefore, some clipping glitches might appear and frustum culling is performed only on the far clipping plane.</p>
<h4>Credits</h4>
<p>Credits goes to Marc Fahmi for the low-polygon 3D model of the Eiffel Tower.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.promethe.net/2010/02/03/google-maps-3d-overlay/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Frustum Culling in Flash 10</title>
		<link>http://blog.promethe.net/2009/12/13/frustum-culling-in-flash-10/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=frustum-culling-in-flash-10</link>
		<comments>http://blog.promethe.net/2009/12/13/frustum-culling-in-flash-10/#comments</comments>
		<pubDate>Sun, 13 Dec 2009 15:43:05 +0000</pubDate>
		<dc:creator>Promethe</dc:creator>
				<category><![CDATA[Flash]]></category>
		<category><![CDATA[Snippets]]></category>
		<category><![CDATA[3D]]></category>
		<category><![CDATA[ActionScript 3.0]]></category>
		<category><![CDATA[experiment]]></category>
		<category><![CDATA[Flash 10]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://blog.promethe.net/?p=558</guid>
		<description><![CDATA[Update: corrected a few glitches in the bounding sphere creation routine. Optimization is always important. But when it comes to 3D for the Flash Platform, it's an everyday battle. The first ideas that come to mind are to avoid: redrawing the same regions : each pixel value must be set once and only once rendering [...]]]></description>
			<content:encoded><![CDATA[<p><strong><span style="color:#ff0000">Update:</span></strong> corrected a few glitches in the bounding sphere creation routine.</p>
<p>Optimization is always important. But when it comes to 3D for the Flash Platform, it's an everyday battle. The first ideas that come to mind are to avoid:</p>
<ol>
<li>redrawing the same regions : each pixel value must be set once and only once</li>
<li>rendering invisible objects : objects that are out of sight still take a lot of CPU horsepower</li>
</ol>
<p>While Flash takes care of the first one in its very renderer, the second one is not handled. But that is something we can easily address!</p>
<h4 style="text-align: left;">The Technic</h4>
<p>The method is called "frustum culling". The big picture is that every mesh is approximated using a bounding volume (typically a sphere or a box). If the bounding volume is visible, the corresponding mesh is rendered. The two following pictures show the frustum culling caught in action:</p>
<div id="attachment_569" class="wp-caption aligncenter" style="width: 510px"><a href="http://blog.promethe.net/wp-content/uploads/2009/12/frustum_culling_scene.PNG" rel="shadowbox[post-558];player=img;"><img class="size-full wp-image-569" title="frustum_culling_scene" src="http://blog.promethe.net/wp-content/uploads/2009/12/frustum_culling_scene.PNG" alt="The mesh is visible" width="500" height="375" /></a><p class="wp-caption-text">The mesh is visible: TPS counter indicates 18900 triangles per seconds</p></div>
<div id="attachment_568" class="wp-caption aligncenter" style="width: 510px"><a href="http://blog.promethe.net/wp-content/uploads/2009/12/frustum_culling_acting.PNG" rel="shadowbox[post-558];player=img;"><img class="size-full wp-image-568" title="frustum_culling_acting" src="http://blog.promethe.net/wp-content/uploads/2009/12/frustum_culling_acting.PNG" alt="Frustum culling in action (TPS counter indicates 0!)" width="500" height="375" /></a><p class="wp-caption-text">The mesh is out of sight: frustum culling is acting and TPS counter indicates 0!</p></div>
<p><span id="more-558"></span></p>
<h5>1. The frustum</h5>
<p>First things first: before performing frustum culling, you need to build the frustum! The frustum is the 3D shape that contains everything the camera can see. It's made of 6 planes and looks like a truncated pyramid:</p>
<div id="attachment_563" class="wp-caption aligncenter" style="width: 517px"><a href="http://blog.promethe.net/wp-content/uploads/2009/12/view_frustum.png" rel="shadowbox[post-558];player=img;"><img class="size-full wp-image-563" title="view_frustum" src="http://blog.promethe.net/wp-content/uploads/2009/12/view_frustum.png" alt="The view frustum" width="507" height="302" /></a><p class="wp-caption-text">The view frustum</p></div>
<p>Each plane is defined by a plane equation: ax + by + cz + d, where (a, b, c) is the normal of the plane and d the distance on that normal. The 6 planes must be extracted from one of the Matrix3D object used to define the transform from one space to another (world to camera for example). Here are the 6 plane equations:</p>
<ul>
<li>left: (a = m14 + m11, b = m24 + m21, c = m34 + m31, d = m44 + m41)</li>
<li>right: (a = m14 − m11, b = m24 − m21, c = m34 − m31, d = m44 − m41)</li>
<li>bottom: (a = m14 + m12, b = m24 + m22, c = m34 + m32, d = m44 + m42)</li>
<li>top: (a = m14 − m12, b = m24 − m22, c = m34 − m32, d = m44 − m42)</li>
<li>near: (a = m13, b = m23, c = m33, d = m43)</li>
<li>far: (a = m13, b = m23, c = m33, d = m43)</li>
</ul>
<p>The source matrix you chose defines the coordinates systems of the extracted planes:</p>
<ul>
<li>the projection matrix provides planes in view (or camera) space</li>
<li>the view * projection matrix provides planes in world space</li>
<li>the world * view * projection matrix provides planes in object (or local) space</li>
</ul>
<h5>2. Bounding volumes</h5>
<p>Testing every polygon against the frustum is a very expensive task. In order to speed things up, we will rather test a primitive shape that bounds the mesh: a "bounding volume". Common bounding volumes are spheres or boxes. I'll focus on the first one in the rest of the article.</p>
<p>A bounding sphere is pretty simple to implement: you just need to store the center of the sphere and it's radius. That being said, a BoundingSphere class would just contain the center as a Vector3D and the radius as a Number. When you have all the vertices of a mesh, building the bounding sphere is easy :</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> computeBoundingSphere<span style="color: #000000;">&#40;</span>myVertices <span style="color: #000000; font-weight: bold;">:</span> Vector.<span style="color: #000000; font-weight: bold;">&lt;</span>Number<span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000; font-weight: bold;">:</span> BoundingSphere
<span style="color: #000000;">&#123;</span>
	<span style="color: #6699cc; font-weight: bold;">var</span> <span style="color: #004993;">min</span> <span style="color: #000000; font-weight: bold;">:</span> Vector3D = <span style="color: #0033ff; font-weight: bold;">new</span> Vector3D<span style="color: #000000;">&#40;</span><span style="color: #004993;">Number</span>.<span style="color: #004993;">MAX_VALUE</span>, <span style="color: #004993;">Number</span>.<span style="color: #004993;">MAX_VALUE</span>, <span style="color: #004993;">Number</span>.<span style="color: #004993;">MAX_VALUE</span><span style="color: #000000;">&#41;</span>;
	<span style="color: #6699cc; font-weight: bold;">var</span> <span style="color: #004993;">max</span> <span style="color: #000000; font-weight: bold;">:</span> Vector3D = <span style="color: #0033ff; font-weight: bold;">new</span> Vector3D<span style="color: #000000;">&#40;</span><span style="color: #004993;">Number</span>.<span style="color: #004993;">MIN_VALUE</span>, <span style="color: #004993;">Number</span>.<span style="color: #004993;">MIN_VALUE</span>, <span style="color: #004993;">Number</span>.<span style="color: #004993;">MIN_VALUE</span><span style="color: #000000;">&#41;</span>;
	<span style="color: #6699cc; font-weight: bold;">var</span> <span style="color: #004993;">length</span> <span style="color: #000000; font-weight: bold;">:</span> <span style="color: #004993;">int</span> = myVertices.<span style="color: #004993;">length</span>;
	<span style="color: #6699cc; font-weight: bold;">var</span> center <span style="color: #000000; font-weight: bold;">:</span> Vector3D = <span style="color: #0033ff; font-weight: bold;">null</span>;
	<span style="color: #6699cc; font-weight: bold;">var</span> radius <span style="color: #000000; font-weight: bold;">:</span> <span style="color: #004993;">Number</span> = <span style="color: #0033ff; font-weight: bold;">null</span>;
&nbsp;
	<span style="color: #0033ff; font-weight: bold;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #6699cc; font-weight: bold;">var</span> i <span style="color: #000000; font-weight: bold;">:</span> <span style="color: #004993;">uint</span> = <span style="color: #000000; font-weight:bold;">0</span>; i <span style="color: #000000; font-weight: bold;">&lt;</span> <span style="color: #004993;">length</span>; i <span style="color: #000000; font-weight: bold;">+</span>= <span style="color: #000000; font-weight:bold;">3</span><span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #004993;">min</span>.<span style="color: #004993;">x</span> = myVertices <span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span> <span style="color: #000000; font-weight: bold;">&lt;</span> <span style="color: #004993;">min</span>.<span style="color: #004993;">x</span> <span style="color: #000000; font-weight: bold;">?</span> myVertices<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span> <span style="color: #000000; font-weight: bold;">:</span> <span style="color: #004993;">min</span>.<span style="color: #004993;">x</span>;
		<span style="color: #004993;">min</span>.<span style="color: #004993;">y</span> = myVertices <span style="color: #000000;">&#91;</span><span style="color: #004993;">uint</span><span style="color: #000000;">&#40;</span>i <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span> <span style="color: #000000; font-weight: bold;">&lt;</span> <span style="color: #004993;">min</span>.<span style="color: #004993;">y</span> <span style="color: #000000; font-weight: bold;">?</span> myVertices<span style="color: #000000;">&#91;</span><span style="color: #004993;">int</span><span style="color: #000000;">&#40;</span>i <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span> <span style="color: #000000; font-weight: bold;">:</span> <span style="color: #004993;">min</span>.<span style="color: #004993;">y</span>;
		<span style="color: #004993;">min</span>.z = myVertices <span style="color: #000000;">&#91;</span><span style="color: #004993;">uint</span><span style="color: #000000;">&#40;</span>i <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #000000; font-weight:bold;">2</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span> <span style="color: #000000; font-weight: bold;">&lt;</span> <span style="color: #004993;">min</span>.z <span style="color: #000000; font-weight: bold;">?</span> myVertices<span style="color: #000000;">&#91;</span><span style="color: #004993;">int</span><span style="color: #000000;">&#40;</span>i <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #000000; font-weight:bold;">2</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span> <span style="color: #000000; font-weight: bold;">:</span> <span style="color: #004993;">min</span>.z;
&nbsp;
 		<span style="color: #004993;">max</span>.<span style="color: #004993;">x</span> = myVertices <span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span> <span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #004993;">max</span>.<span style="color: #004993;">x</span> <span style="color: #000000; font-weight: bold;">?</span> myVertices <span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span> <span style="color: #000000; font-weight: bold;">:</span> <span style="color: #004993;">max</span>.<span style="color: #004993;">x</span>;
		<span style="color: #004993;">max</span>.<span style="color: #004993;">y</span> = myVertices <span style="color: #000000;">&#91;</span><span style="color: #004993;">uint</span><span style="color: #000000;">&#40;</span>i <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span> <span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #004993;">max</span>.<span style="color: #004993;">y</span> <span style="color: #000000; font-weight: bold;">?</span> myVertices<span style="color: #000000;">&#91;</span><span style="color: #004993;">int</span><span style="color: #000000;">&#40;</span>i <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span> <span style="color: #000000; font-weight: bold;">:</span> <span style="color: #004993;">max</span>.<span style="color: #004993;">y</span>;
		<span style="color: #004993;">max</span>.z = myVertices <span style="color: #000000;">&#91;</span><span style="color: #004993;">uint</span><span style="color: #000000;">&#40;</span>i <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #000000; font-weight:bold;">2</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span> <span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #004993;">max</span>.z <span style="color: #000000; font-weight: bold;">?</span> myVertices<span style="color: #000000;">&#91;</span><span style="color: #004993;">int</span><span style="color: #000000;">&#40;</span>i <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #000000; font-weight:bold;">2</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span> <span style="color: #000000; font-weight: bold;">:</span> <span style="color: #004993;">max</span>.z;
	<span style="color: #000000;">&#125;</span>
&nbsp;
	center = <span style="color: #0033ff; font-weight: bold;">new</span> Vector3D<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>myMax.<span style="color: #004993;">x</span> <span style="color: #000000; font-weight: bold;">+</span> myMin.<span style="color: #004993;">x</span><span style="color: #000000;">&#41;</span> <span style="color: #000000; font-weight: bold;">/</span> <span style="color: #000000; font-weight:bold;">2.0</span>, <span style="color: #000000;">&#40;</span>myMax.<span style="color: #004993;">y</span> <span style="color: #000000; font-weight: bold;">+</span> myMin.<span style="color: #004993;">y</span><span style="color: #000000;">&#41;</span> <span style="color: #000000; font-weight: bold;">/</span> <span style="color: #000000; font-weight:bold;">2.0</span>, <span style="color: #000000;">&#40;</span>myMax.z <span style="color: #000000; font-weight: bold;">+</span> myMin.z<span style="color: #000000;">&#41;</span> <span style="color: #000000; font-weight: bold;">/</span> <span style="color: #000000; font-weight:bold;">2.0</span><span style="color: #000000;">&#41;</span>;
        radius = <span style="color: #004993;">Math</span>.<span style="color: #004993;">max</span><span style="color: #000000;">&#40;</span>myMax.<span style="color: #004993;">x</span> <span style="color: #000000; font-weight: bold;">-</span> center.<span style="color: #004993;">x</span>, myMax.<span style="color: #004993;">y</span> <span style="color: #000000; font-weight: bold;">-</span> center.<span style="color: #004993;">y</span>, myMax.z <span style="color: #000000; font-weight: bold;">-</span> center.z,
		          center.<span style="color: #004993;">x</span> <span style="color: #000000; font-weight: bold;">-</span> myMin.<span style="color: #004993;">x</span>, center.<span style="color: #004993;">y</span> <span style="color: #000000; font-weight: bold;">-</span> myMin.<span style="color: #004993;">y</span>, center.z <span style="color: #000000; font-weight: bold;">-</span> myMin.z<span style="color: #000000;">&#41;</span>;	
&nbsp;
	<span style="color: #0033ff; font-weight: bold;">return</span> <span style="color: #0033ff; font-weight: bold;">new</span> BoundingSphere<span style="color: #000000;">&#40;</span>center, radius<span style="color: #000000;">&#41;</span>;
<span style="color: #000000;">&#125;</span></pre></div></div>

<h5>3. Point to plane distance</h5>
<p>The plane equation (ax + by + cz + d) can be stored as a Vector3D object. Indeed, the Vector3D implements x, y and z but also a w Number attribute. This last one is supposed to store the homogenous coordinate. Nonetheless, we can use those 4 Number attributes to store the (a, b, c, d) values that defines our plane equation.</p>
<p>In order to test whether a bounding volume is inside or outside the frustum, we must be able to compute the distance of this object to the plane. To do so, we use the dot product of the point with the plane normal (which must be normalized first) :</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> planeToPointDistance<span style="color: #000000;">&#40;</span>myPlane <span style="color: #000000; font-weight: bold;">:</span> Vector3D, myPoint <span style="color: #000000; font-weight: bold;">:</span> Vector3D<span style="color: #000000;">&#41;</span> <span style="color: #000000; font-weight: bold;">:</span> <span style="color: #004993;">Number</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #0033ff; font-weight: bold;">return</span> myPlane.<span style="color: #004993;">x</span> <span style="color: #000000; font-weight: bold;">*</span> myPoint.<span style="color: #004993;">x</span> <span style="color: #000000; font-weight: bold;">+</span> myPlane.<span style="color: #004993;">y</span> <span style="color: #000000; font-weight: bold;">*</span> myPoint.<span style="color: #004993;">y</span> <span style="color: #000000; font-weight: bold;">+</span> myPlane.z <span style="color: #000000; font-weight: bold;">*</span> myPoint.z <span style="color: #000000; font-weight: bold;">+</span> myPlane.w;
<span style="color: #000000;">&#125;</span></pre></div></div>

<h5>4. The test function</h5>
<p>Now that our frustum and our bounding volumes are ready, we can add this little test function that checks whether an object is visible or not:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> testBoundingSphere<span style="color: #000000;">&#40;</span>myPlanes      <span style="color: #000000; font-weight: bold;">:</span> Vector.,
                                   mySphere      <span style="color: #000000; font-weight: bold;">:</span> BoundingSphere<span style="color: #000000;">&#41;</span> <span style="color: #000000; font-weight: bold;">:</span> <span style="color: #004993;">Boolean</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #6699cc; font-weight: bold;">var</span> <span style="color: #004993;">length</span> <span style="color: #000000; font-weight: bold;">:</span> <span style="color: #004993;">int</span> = myPlanes.<span style="color: #004993;">length</span>;
&nbsp;
	<span style="color: #0033ff; font-weight: bold;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #6699cc; font-weight: bold;">var</span> i <span style="color: #000000; font-weight: bold;">:</span> <span style="color: #004993;">uint</span> = <span style="color: #000000; font-weight:bold;">0</span>; i <span style="color: #000000; font-weight: bold;">&lt;</span> <span style="color: #004993;">length</span>; <span style="color: #000000; font-weight: bold;">++</span>i<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #6699cc; font-weight: bold;">var</span> <span style="color: #004993;">d</span> 	<span style="color: #000000; font-weight: bold;">:</span> <span style="color: #004993;">Number</span> = planeToPointDistance<span style="color: #000000;">&#40;</span>myPlanes<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
		<span style="color: #009900;">// bouding sphere is out of the frustum</span>
		<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #004993;">d</span> <span style="color: #000000; font-weight: bold;">+</span> mySphere.radius <span style="color: #000000; font-weight: bold;">&lt;</span> <span style="color: #000000; font-weight:bold;">0.0</span><span style="color: #000000;">&#41;</span>
			<span style="color: #0033ff; font-weight: bold;">return</span> <span style="color: #0033ff; font-weight: bold;">false</span>;
		<span style="color: #009900;">// bounding sphere is spanning</span>
		<span style="color: #0033ff; font-weight: bold;">else</span> <span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #004993;">d</span> <span style="color: #000000; font-weight: bold;">-</span> mySphere.radius <span style="color: #000000; font-weight: bold;">&lt;</span>= <span style="color: #000000; font-weight:bold;">0.0</span><span style="color: #000000;">&#41;</span>
			<span style="color: #0033ff; font-weight: bold;">return</span> <span style="color: #0033ff; font-weight: bold;">true</span>;
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #009900;">// bounding sphere is inside the frustum</span>
	<span style="color: #0033ff; font-weight: bold;">return</span> <span style="color: #0033ff; font-weight: bold;">true</span>;
<span style="color: #000000;">&#125;</span></pre></div></div>

<h4>The Experiment</h4>
<p>The following experiment demonstrates frustum culling in action (click on the picture to open the application) :</p>
<div id="attachment_560" class="wp-caption aligncenter" style="width: 510px"><a rel="shadowbox[tekkaman];width=500;height=375]" href="http://blog.promethe.net/wp-content/uploads/2009/12/Tekkaman.swf"><img class="size-full wp-image-560" title="frustum_culling" src="http://blog.promethe.net/wp-content/uploads/2009/12/frustum_culling.png" alt="frustum_culling" width="500" height="375" /></a><p class="wp-caption-text">Frustum culling experiment (use the arrow keys to move)</p></div>
<p style="text-align: left;">Use the arrow keys to move the robot across the scene. You can notice the TPS counter drops down to zero whenever the character is out of sight (on the near left or near right side of the screen). When the TPS indicates 0, no triangles are rendered and CPU usage drops down to 0%. It actually means that the rest of the rendering process has a minimal overhead!</p>
<h4>And it's even more awesome...</h4>
<p style="text-align: left;">This experiment also introduce my brand new mini-debugger inspired of <a href="http://mrdoob.com/80/Stats">Mr. Doob's work</a>. I replaced the maximum memory footprint counter with a per-second rendered triangles counter (or TPS counter). CPU usage or framerate alone are not absolute measurements but the TPS counter reflects the true performance of the application the old fashioned way: the more polygons per second the better. I also updated the timer to display the processing time and the overall rendering time.</p>
<p style="text-align: left;">You can compare this experiment to the <a href="http://blog.promethe.net/2009/10/07/quake-2-3d-models-in-flash-10/">"old" one I made up a few weeks ago</a>: the displayed 3D model features 30% more polygons but the CPU usage is the same.</p>
<p style="text-align: left;">The rendering process is done in such a way that absolutely no computation occurs on invisible objects, not even the slightest animation or z-sorting operation.</p>
<p style="text-align: left;">I enabled dynamic bounding volumes for animated meshes: if you place the mesh on the edges of the viewing space, you might notice that the TPS counter sometimes goes down to zero to raise up again to a few hundreds/thousands of triangles per second. The explanation is simple: the visibility test is done dynamically and follows the mesh animations. If the animation drives the mesh out of the viewing frustum, culling operates and it is not renderered.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.promethe.net/2009/12/13/frustum-culling-in-flash-10/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Edit XML Namespaces with sed</title>
		<link>http://blog.promethe.net/2009/06/13/edit-xml-namespaces-with-sed/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=edit-xml-namespaces-with-sed</link>
		<comments>http://blog.promethe.net/2009/06/13/edit-xml-namespaces-with-sed/#comments</comments>
		<pubDate>Sat, 13 Jun 2009 13:54:29 +0000</pubDate>
		<dc:creator>Promethe</dc:creator>
				<category><![CDATA[Snippets]]></category>
		<category><![CDATA[shell script]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://blog.promethe.net/?p=346</guid>
		<description><![CDATA[Even if you are a high level programing language user, you sometimes have to use some good old tools to gain some productivity. Sed is one of those tools that might come to be handy. I recently needed to change the xmlns attribute of a lot of XML/XSL files and doing it file by file [...]]]></description>
			<content:encoded><![CDATA[<p>Even if you are a high level programing language user, you sometimes have to use some good old tools to gain some productivity. <a href="http://en.wikipedia.org/wiki/Sed" target="_blank">Sed</a> is one of those tools that might come to be handy.</p>
<p>I recently needed to change the xmlns attribute of a lot of XML/XSL files and doing it file by file would have been a real mess. So I wrote this little piece of script shell:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#! /bin/sh</span>
&nbsp;
<span style="color: #007800;">NAMESPACE_NAME</span>=$<span style="color: #000000;">1</span>
<span style="color: #007800;">NAMESPACE_URI</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">echo</span> $<span style="color: #000000;">2</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">sed</span> <span style="color: #660033;">-e</span> <span style="color: #ff0000;">'s/\//\\\//g'</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
<span style="color: #007800;">FILE</span>=$<span style="color: #000000;">3</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> x<span style="color: #800000;">${NAMESPACE_NAME}</span> = <span style="color: #ff0000;">'x'</span> <span style="color: #660033;">-o</span> x<span style="color: #800000;">${NAMESPACE_URI}</span> = <span style="color: #ff0000;">'x'</span> <span style="color: #660033;">-o</span> <span style="color: #000000; font-weight: bold;">!</span> <span style="color: #660033;">-f</span> <span style="color: #800000;">${FILE}</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
    <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">'Usage:'</span> <span style="color: #800000;">${0}</span> <span style="color: #ff0000;">'NAMESPACE_NAME NAMESPACE_URI FILE'</span>
    <span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000;">42</span>
<span style="color: #000000; font-weight: bold;">fi</span>
&nbsp;
<span style="color: #c20cb9; font-weight: bold;">cat</span> <span style="color: #007800;">$FILE</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">sed</span> <span style="color: #660033;">-e</span> s<span style="color: #000000; font-weight: bold;">/</span>xmlns:<span style="color: #800000;">${NAMESPACE_NAME}</span>=<span style="color: #000000; font-weight: bold;">\&quot;</span>.<span style="color: #000000; font-weight: bold;">*</span><span style="color: #000000; font-weight: bold;">\&quot;</span><span style="color: #000000; font-weight: bold;">/</span>xmlns:<span style="color: #800000;">${NAMESPACE_NAME}</span><span style="color: #ff0000;">'=&quot;'</span><span style="color: #800000;">${NAMESPACE_URI}</span><span style="color: #000000; font-weight: bold;">\&quot;</span><span style="color: #000000; font-weight: bold;">/</span>g <span style="color: #000000; font-weight: bold;">&amp;</span>gt; <span style="color: #007800;">$FILE</span></pre></div></div>

<p>You can run the script with the following command line:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">.<span style="color: #000000; font-weight: bold;">/</span>change_namespace.sh myns http:<span style="color: #000000; font-weight: bold;">//</span>my.namespace.uri my_file.xml</pre></div></div>

<p>Where :</p>
<ul>
<li>myns is the namespace name (html, xsl, xhtml, etc...)</li>
<li>http://my.namespace.uri is the URI of the namespace</li>
<li>my_file.xml is the file to edit</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://blog.promethe.net/2009/06/13/edit-xml-namespaces-with-sed/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
