<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>Rozengain.com - Creative Technology Blog</title>
	
	<link>http://www.rozengain.com/blog</link>
	<description />
	<lastBuildDate>Wed, 30 May 2012 17:18:46 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/rozengain" /><feedburner:info uri="rozengain" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
		<title>Rajawali Tutorial 24: Using Geometry Data To Position And Rotate Objects</title>
		<link>http://feedproxy.google.com/~r/rozengain/~3/x9xJUEszuWE/</link>
		<comments>http://www.rozengain.com/blog/2012/05/30/rajawali-tutorial-24-using-geometry-data-to-position-and-rotate-objects/#comments</comments>
		<pubDate>Wed, 30 May 2012 16:49:46 +0000</pubDate>
		<dc:creator>Dennis</dc:creator>
				<category><![CDATA[3D]]></category>
		<category><![CDATA[Android]]></category>
		<category><![CDATA[Rajawali]]></category>
		<category><![CDATA[OpenGL ES]]></category>

		<guid isPermaLink="false">http://www.rozengain.com/blog/?p=1042</guid>
		<description><![CDATA[This is a small example that shows how to access geometry data and how to use it to position and rotate objects. In this example I will create a sphere and use its vertices and normals. I will place a spike at each vertex position and then I will use the corresponding normal to rotate [...]]]></description>
			<content:encoded><![CDATA[<p>This is a small example that shows how to access geometry data and how to use it to position and rotate objects.<br />
In this example I will create a sphere and use its vertices and normals. I will place a spike at each vertex position and then I will use the corresponding normal to rotate the spike so that it faces the same direction as the vertex normal.</p>
<p>It will look like this:</p>
<p><img class="alignnone" src="/files/rajawali/rajawali-spikey-ball.jpg" alt="" width="600" height="575" /></p>
<p>To access the vertex and normal data we first need to call the getGeometry() method on the BaseObject3D. This gives us access to methods like getVertices(), getIndices(), getTextureCoords(), getColors(), etc. The getVertices() and getNormals() methods return a FloatBuffer each:</p>
<pre><code>// -- create a sphere so we can use its vertices and normals
BaseObject3D sphere = new Sphere(1, 16, 8);
// -- get vertex buffer
FloatBuffer vertBuffer = sphere.getGeometry().getVertices();
// -- get the normal buffer
FloatBuffer normBuffer = sphere.getGeometry().getNormals();
</code></pre>
<p>Now we can loop through the buffers and get its data. The vertices and normals are x, y and z coordinates that can be accessed by calling get(index), get(index+1), get(index+2) on the FloatBuffer object:</p>
<pre><code>int numVerts = vertBuffer.limit();
for(int i=0; i&lt;numVerts; i+=3) {
	Number3D position = new Number3D(vertBuffer.get(i), vertBuffer.get(i+1), vertBuffer.get(i+2));
	Number3D normal = new Number3D(normBuffer.get(i), normBuffer.get(i+1), normBuffer.get(i+2));

	// -- ... do other stuff ...
}
</code></pre>
<p>For instance, we can use the vertex buffer values to position the spikes:</p>
<pre><code>spike.setPosition(vertBuffer.get(i), vertBuffer.get(i+1), vertBuffer.get(i+2));
</code></pre>
<p>This way the spikes are nicely distributed across the sphere. But they&#8217;re all pointing in the same direction.<br />
Next, let&#8217;s set up the orientation so that they face the same direction as the normal:</p>
<pre><code>// -- get the normal so we can orient the spike to the normal
Number3D normal = new Number3D(-normBuffer.get(i), normBuffer.get(i+1), normBuffer.get(i+2));
// -- get the rotation axis. The upAxis is a Number3D(0, 1, 0) (y up)
Number3D axis = Number3D.cross(upAxis, normal);
// -- get the rotation angle
float angle = MathUtil.radiansToDegrees((float)Math.acos(Number3D.dot(upAxis, normal)));
// -- create the quaternion
Quaternion q = new Quaternion();
q.fromAngleAxis(angle, axis);
// -- set the orientation so that it is aligned with the current normal
spike.setOrientation(q);
</code></pre>
<p>&#8230; and now they all face the same direction. Schweet.<br />
As usual, the code can be found on Github:</p>
<p><a title="RajawaliUsingGeometryDataRenderer.java on Github" href="https://github.com/MasDennis/RajawaliExamples/blob/master/src/com/monyetmabuk/rajawali/tutorials/RajawaliUsingGeometryDataRenderer.java">RajawaliUsingGeometryDataRenderer.java</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.rozengain.com/blog/2012/05/30/rajawali-tutorial-24-using-geometry-data-to-position-and-rotate-objects/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.rozengain.com/blog/2012/05/30/rajawali-tutorial-24-using-geometry-data-to-position-and-rotate-objects/</feedburner:origLink></item>
		<item>
		<title>Rajawali Tutorial 23: Custom Vertex Shader</title>
		<link>http://feedproxy.google.com/~r/rozengain/~3/zws4J_JpMp4/</link>
		<comments>http://www.rozengain.com/blog/2012/05/16/rajawali-tutorial-23-custom-vertex-shader/#comments</comments>
		<pubDate>Wed, 16 May 2012 14:54:23 +0000</pubDate>
		<dc:creator>Dennis</dc:creator>
				<category><![CDATA[3D]]></category>
		<category><![CDATA[Android]]></category>
		<category><![CDATA[Rajawali]]></category>
		<category><![CDATA[GLSL]]></category>
		<category><![CDATA[OpenGL ES]]></category>

		<guid isPermaLink="false">http://www.rozengain.com/blog/?p=1022</guid>
		<description><![CDATA[In this tutorial we&#8217;ll take a look at how you can create a custom vertex shader. GLSL shaders are a very powerful feature of OpenGL ES 2.0. In tutorial 7 we created a custom fragment shader. This time we&#8217;ll manipulate a sphere&#8217;s vertex positions using a vertex shader. First we&#8217;ll create a regular sphere and then [...]]]></description>
			<content:encoded><![CDATA[<p>In this tutorial we&#8217;ll take a look at how you can create a custom vertex shader. GLSL shaders are a very powerful feature of OpenGL ES 2.0. In <a href="http://www.rozengain.com/blog/2012/02/22/rajawali-tutorial-7-creating-a-custom-material-glsl-shader/">tutorial 7</a> we created a custom fragment shader. This time we&#8217;ll manipulate a sphere&#8217;s vertex positions using a vertex shader.</p>
<p>First we&#8217;ll create a regular sphere and then we&#8217;ll attach a custom material to it. To keep things simple we&#8217;ll extend the <a title="SimpleMaterial class" href="https://github.com/MasDennis/Rajawali/blob/master/src/rajawali/materials/SimpleMaterial.java">SimpleMaterial</a> class and use its fragment shader. Like so:</p>
<pre><code>public class CustomVertexShaderMaterial extends SimpleMaterial {
	...
	// -- we'll pass our custom vertex shader and SimpleMaterials'
	//     fragment shader into the superclass constructor.
	public CustomVertexShaderMaterial() {
		super(mVShader, SimpleMaterial.mFShader);
	}
</code></pre>
<p>Because we want the vertices to be animated we&#8217;ll use a time parameter. In the shader this will be a uniform:</p>
<pre><code>uniform float uTime;</code></pre>
<p>In the material class we need to get a handle to this uniform. This can be done by overriding the setShaders method and calling getUniformLocation():</p>
<pre><code>public void setShaders(String vertexShader, String fragmentShader)
{
	super.setShaders(vertexShader, fragmentShader);
	muTimeHandle = getUniformLocation("uTime");
}
</code></pre>
<p>We also create a setter so that the time can be updated from the main renderer class. The time value will then be passed to the vertex shader in the useProgram() method which is called every frame:</p>
<pre><code>public void useProgram() {
	super.useProgram();
	// -- make sure that time updates every frame
	GLES20.glUniform1f(muTimeHandle, mTime);
}

public void setTime(float time) {
	mTime = time;
}
</code></pre>
<p>Now on to the vertex shader. We want the vertex position to change through time by using trigonometric functions. This should give a nice organic blob effect. The changed vertex position will consist of the original vertex position plus a normalized vector that&#8217;s pointing in the same direction but is affected by trigonometric functions. Its always better explained in code :O Here&#8217;s the complete vertex shader:</p>
<pre><code>// -- normalized axis vectors. we'll use these to
//    get the angles
const vec3 cXaxis = vec3(1.0, 0.0, 0.0);
const vec3 cYaxis = vec3(0.0, 1.0, 0.0);
const vec3 cZaxis = vec3(0.0, 0.0, 1.0);
// -- the amplitude of the 'wave' effect
const float cStrength = 0.5;

uniform mat4 uMVPMatrix;
uniform float uTime;

attribute vec4 aPosition;
attribute vec2 aTextureCoord;
attribute vec4 aColor;

varying vec2 vTextureCoord;
varying vec4 vColor;		

void main() {
	// -- normalized direction from the origin (0,0,0)

	vec3 directionVec = normalize(vec3(aPosition));

	// -- the angle between this vertex and the x, y, z angles

	float xangle = dot(cXaxis, directionVec) * 5.0;
	float yangle = dot(cYaxis, directionVec) * 6.0;
	float zangle = dot(cZaxis, directionVec) * 4.5;
	vec4 timeVec = aPosition;
	float time = uTime * .05;

	// -- cos &amp; sin calculations for each of the angles
	//    change some numbers here &amp; there to get the
	//    desired effect.

	float cosx = cos(time + xangle);
	float sinx = sin(time + xangle);
	float cosy = cos(time + yangle);
	float siny = sin(time + yangle);
	float cosz = cos(time + zangle);
	float sinz = sin(time + zangle);

	// -- multiply all the parameters to get the final
	//    vertex position

	timeVec.x += directionVec.x * cosx * siny * cosz * cStrength;
	timeVec.y += directionVec.y * sinx * cosy * sinz * cStrength;
	timeVec.z += directionVec.z * sinx * cosy * cosz * cStrength;
	gl_Position = uMVPMatrix * timeVec;
	vTextureCoord = aTextureCoord;

	// -- use the (normalized) direction vector as the
	//    vertex color to get a nice colorful effect

	vColor = vec4(directionVec, 1.0);
}
</code></pre>
<p>Notice that a nice effect can be created by using the normalized direction vector as the current vertex&#8217;s color <img src='http://www.rozengain.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p>Here&#8217;s what it looks like:</p>
<p><img class="alignnone" title="Custom Vertex Shader Blob" src="/files/rajawali/rajawali-vertex-shader-blob.jpg" alt="Custom Vertex Shader Blob" width="600" height="338" /></p>
<p><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="560" height="315" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="allowFullScreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="src" value="http://www.youtube.com/v/LNKLIfgfjZ4?version=3&amp;hl=en_US" /><param name="allowfullscreen" value="true" /><embed type="application/x-shockwave-flash" width="560" height="315" src="http://www.youtube.com/v/LNKLIfgfjZ4?version=3&amp;hl=en_US" allowscriptaccess="always" allowfullscreen="true"></embed></object></p>
<p>The full source code can be found on Github, as usual:</p>
<ul>
<li><a href="https://github.com/MasDennis/RajawaliExamples/blob/master/src/com/monyetmabuk/rajawali/tutorials/RajawaliVertexShaderActivity.java">RajawaliVertexShaderActivity.java</a></li>
<li><a href="https://github.com/MasDennis/RajawaliExamples/blob/master/src/com/monyetmabuk/rajawali/tutorials/RajawaliVertexShaderRenderer.java">RajawaliVertexShaderRenderer.java</a></li>
<li><a href="https://github.com/MasDennis/RajawaliExamples/blob/master/src/com/monyetmabuk/rajawali/tutorials/CustomVertexShaderMaterial.java">CustomVertexShaderMaterial.java</a></li>
</ul>
<p>Go to the next tutorial, <a href="/blog/2012/05/30/rajawali-tutorial-24-using-geometry-data-to-position-and-rotate-objects/">Rajawali Tutorial 24: Using Geometry Data To Position And Rotate Objects</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.rozengain.com/blog/2012/05/16/rajawali-tutorial-23-custom-vertex-shader/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		<feedburner:origLink>http://www.rozengain.com/blog/2012/05/16/rajawali-tutorial-23-custom-vertex-shader/</feedburner:origLink></item>
		<item>
		<title>Microsoft Kinect SDK Wrapper For Unity Crash Bug Fix</title>
		<link>http://feedproxy.google.com/~r/rozengain/~3/rqzdKvqYAwI/</link>
		<comments>http://www.rozengain.com/blog/2012/05/10/microsoft-kinect-sdk-wrapper-for-unity-crash-bug-fix/#comments</comments>
		<pubDate>Thu, 10 May 2012 10:50:06 +0000</pubDate>
		<dc:creator>Dennis</dc:creator>
				<category><![CDATA[3D]]></category>
		<category><![CDATA[Kinect]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[Unity]]></category>

		<guid isPermaLink="false">http://www.rozengain.com/blog/?p=979</guid>
		<description><![CDATA[There&#8217;s a great free Kinect SDK wrapper available for Unity. It&#8217;s free &#38; open source but there are still a few problems getting it to run with the 1.0 SDK (as opposed to the beta). The first problem is that it is pointing to the wrong dll file. When you get this exception: DllNotFoundException: C:\Program [...]]]></description>
			<content:encoded><![CDATA[<p>There&#8217;s a great <a title="Microsoft SDK Wrapper for Unity" href="https://channel9.msdn.com/coding4fun/kinect/Unity-and-the-Kinect-SDK">free Kinect SDK wrapper available for Unity</a>. It&#8217;s free &amp; open source but there are still a few problems getting it to run with the 1.0 SDK (as opposed to the beta).</p>
<p>The first problem is that it is pointing to the wrong dll file. When you get this exception:</p>
<pre><code>DllNotFoundException: C:\Program Files (x86)\Microsoft Research KinectSDK\MSRKINECTNUI.DLL</code></pre>
<p>You should open the file <em>KinectInterop.cs</em> and changes all dll paths to:</p>
<pre><code>C:\Windows\System32\Kinect10.dll</code></pre>
<p>This will fix all compiler errors and it should run without problems.</p>
<p>However, it will only run once. When you run it the second time Unity will freeze and you will have to kill the process. Not very convenient.</p>
<p>This is caused by a bug in the Microsoft SDK. According to <a href="http://www.microsoft.com/en-us/kinectforwindows/develop/release-notes.aspx#_6._known_issues">this page</a> the problem is:</p>
<pre><code>If C++ code is executing NuiInitializa/NuiShutdown multiple times through
the application's lifetime, SetDeviceStatusCallback should be called once,
before invoking those calls.
</code></pre>
<p>So apparently a single call to <em>SetDeviceStatusCallback()</em> should fix the problem. To be able to call this method we need to add some code to the <em>KinectInterop.cs</em> file. First of all we need to add an empty struct:</p>
<pre><code>public struct NuiStatusProc
{
}
</code></pre>
<p>Then we need to link the native method. In the NativeMethods class add:</p>
<pre><code>
[DllImportAttribute(@"C:\Windows\System32\Kinect10.dll", EntryPoint = "NuiSetDeviceStatusCallback")]
	    public static extern void NuiSetDeviceStatusCallback(NuiStatusProc callback);
</code></pre>
<p>Now open the file <em>KinectSensor.cs</em> and add this line to the <em>void Awake()</em> method (just before the line &#8220;catch (Exception e)&#8221;):</p>
<pre><code>
NativeMethods.NuiSetDeviceStatusCallback(new NuiStatusProc());
</code></pre>
<p>Now everything should run fine. If it doesn&#8217;t let me know :O</p>
]]></content:encoded>
			<wfw:commentRss>http://www.rozengain.com/blog/2012/05/10/microsoft-kinect-sdk-wrapper-for-unity-crash-bug-fix/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		<feedburner:origLink>http://www.rozengain.com/blog/2012/05/10/microsoft-kinect-sdk-wrapper-for-unity-crash-bug-fix/</feedburner:origLink></item>
		<item>
		<title>Rajawali Tutorial 22: More Optimisation</title>
		<link>http://feedproxy.google.com/~r/rozengain/~3/F9Eoy5qthao/</link>
		<comments>http://www.rozengain.com/blog/2012/05/03/rajawali-tutorial-22-more-optimisation/#comments</comments>
		<pubDate>Thu, 03 May 2012 08:21:11 +0000</pubDate>
		<dc:creator>Dennis</dc:creator>
				<category><![CDATA[3D]]></category>
		<category><![CDATA[Android]]></category>
		<category><![CDATA[Rajawali]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[OpenGL ES]]></category>

		<guid isPermaLink="false">http://www.rozengain.com/blog/?p=974</guid>
		<description><![CDATA[This is the uber-optimisation. Communication between the CPU and the GPU is minimal. It is a bit harder to do though. Instead of using the normal setPosition(), setScale() and setRotation() methods you&#8217;ll have to write a shader that does this. Here&#8217;s a video of 2000 planes that are combined into one object but have individual [...]]]></description>
			<content:encoded><![CDATA[<p>This is the uber-optimisation. Communication between the CPU and the GPU is minimal. It is a bit harder to do though.<br />
Instead of using the normal setPosition(), setScale() and setRotation() methods you&#8217;ll have to write a shader that does this.<br />
Here&#8217;s a video of 2000 planes that are combined into one object but have individual textures and rotations:</p>
<p><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="560" height="315" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="allowFullScreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="src" value="http://www.youtube.com/v/Iqg4U6yoabg?version=3&amp;hl=en_US" /><param name="allowfullscreen" value="true" /><embed type="application/x-shockwave-flash" width="560" height="315" src="http://www.youtube.com/v/Iqg4U6yoabg?version=3&amp;hl=en_US" allowscriptaccess="always" allowfullscreen="true"></embed></object></p>
<p>If you would do this the normal way (create 2000 Plane objects with individual textures) your device would melt in your hands. When you do it the way described here, it will run on full speed and make a happy dance. Now that&#8217;s a bold promise.</p>
<p>Like I said, this is not an easy optimisation if you&#8217;re not very familiar with creating your own geometry and shaders. I&#8217;ll attempt to explain it anyway. Please post a comment if you have any questions.</p>
<p>In order to create the geometries for each separate plane we&#8217;ll need to create a custom BaseObject3D. We are going to create the usual vertex, normal, color and texture coordinates buffers manually. On top of that we&#8217;ll create new buffers for the positions and rotations.</p>
<p>First, create the new class that inherits from BaseObject3D:</p>
<pre><code>public class PlanesGalore extends BaseObject3D {
</code></pre>
<p>Then add our buffers &amp; the buffer handles to the class variable declarations:</p>
<pre><code>protected FloatBuffer mPlanePositions;
protected FloatBuffer mRotationSpeeds;
protected int mPlanePositionsBufferHandle;
protected int mRotationSpeedsBufferHandle;
protected PlanesGaloreMaterial mGaloreMat;
</code></pre>
<p>In the init() method we create the arrays that will contain all relevant data. These will fed into FloatBuffers and IntBuffers after that:</p>
<pre><code>final int numPlanes = 2000;
final float planeSize = .3f;

int numVertices = numPlanes * 4;
float[] vertices = new float[numVertices * 3];
float[] textureCoords = new float[numVertices * 2];
float[] normals = new float[numVertices * 3];
float[] planePositions = new float[numVertices * 3];
float[] rotationSpeeds = new float[numVertices];
float[] colors = new float[numVertices * 4];
int[] indices = new int[numPlanes * 6];
</code></pre>
<p>Next up is the loop that fills the arrays with all the relevant data. I won&#8217;t expand much on this, but it creates all the individual planes (vertices, indices, textures coordinates, normals, random rotations, random positions):</p>
<pre><code>for (int i = 0; i &lt; numPlanes; ++i) {
	Number3D r = new Number3D(-10f + (Math.random() * 20f), -10 + (Math.random() * 20f), (Math.random() * 80f));
	int randColor = 0xff000000 + (int) (0xffffff * Math.random());

	int vIndex = i * 4 * 3;
	vertices[vIndex + 0] = -planeSize;
	vertices[vIndex + 1] = planeSize;
	vertices[vIndex + 2] = 0;
	vertices[vIndex + 3] = planeSize;
	vertices[vIndex + 4] = planeSize;
	vertices[vIndex + 5] = 0;
	vertices[vIndex + 6] = planeSize;
	vertices[vIndex + 7] = -planeSize;
	vertices[vIndex + 8] = 0;
	vertices[vIndex + 9] = -planeSize;
	vertices[vIndex + 10] = -planeSize;
	vertices[vIndex + 11] = 0;

	for (int j = 0; j &lt; 12; j += 3) {
		normals[vIndex + j] = 0;
		normals[vIndex + j + 1] = 0;
		normals[vIndex + j + 2] = 1;

		planePositions[vIndex + j] = r.x;
		planePositions[vIndex + j + 1] = r.y;
		planePositions[vIndex + j + 2] = r.z;
	}

	vIndex = i * 4 * 4;

	for (int j = 0; j &lt; 16; j += 4) {
		colors[vIndex + j] = Color.red(randColor) / 255f;
		colors[vIndex + j + 1] = Color.green(randColor) / 255f;
		colors[vIndex + j + 2] = Color.blue(randColor) / 255f;
		colors[vIndex + j + 3] = 1.0f;
	}

	vIndex = i * 4 * 2;

	float u1 = .25f * (int) Math.floor(Math.random() * 4f);
	float v1 = .25f * (int) Math.floor(Math.random() * 4f);
	float u2 = u1 + .25f;
	float v2 = v1 + .25f;

	textureCoords[vIndex + 0] = u2;
	textureCoords[vIndex + 1] = v1;
	textureCoords[vIndex + 2] = u1;
	textureCoords[vIndex + 3] = v1;
	textureCoords[vIndex + 4] = u1;
	textureCoords[vIndex + 5] = v2;
	textureCoords[vIndex + 6] = u2;
	textureCoords[vIndex + 7] = v2;

	vIndex = i * 4;
	int iindex = i * 6;
	indices[iindex + 0] = (short) (vIndex + 0);
	indices[iindex + 1] = (short) (vIndex + 1);
	indices[iindex + 2] = (short) (vIndex + 3);
	indices[iindex + 3] = (short) (vIndex + 1);
	indices[iindex + 4] = (short) (vIndex + 2);
	indices[iindex + 5] = (short) (vIndex + 3);

	float rotationSpeed = -1f + (float) (Math.random() * 2f);
	rotationSpeeds[vIndex + 0] = rotationSpeed;
	rotationSpeeds[vIndex + 1] = rotationSpeed;
	rotationSpeeds[vIndex + 2] = rotationSpeed;
	rotationSpeeds[vIndex + 3] = rotationSpeed;
}
</code></pre>
<p>One important thing to note here is how the texture coordinates are being calculated. Instead of using a separate texture for each plane, the textures are combined into a texture atlas. In this case, I&#8217;ve put 16 textures into a single texture. Each plane then gets texture coordinates that correspond to one these textures.</p>
<p>Finally, the usual data can be passed on to the superclass:</p>
<pre><code>setData(vertices, normals, textureCoords, colors, indices);
</code></pre>
<p>Since we have custom position and rotation speed arrays we&#8217;ll need to create two Buffers and obtain a handle from the OpenGL context:</p>
<pre><code>mPlanePositions = ByteBuffer.allocateDirect(planePositions.length * Geometry3D.FLOAT_SIZE_BYTES).order(ByteOrder.nativeOrder()).asFloatBuffer();
mPlanePositions.put(planePositions).position(0);

mRotationSpeeds = ByteBuffer.allocateDirect(rotationSpeeds.length * Geometry3D.FLOAT_SIZE_BYTES).order(ByteOrder.nativeOrder()).asFloatBuffer();
mRotationSpeeds.put(rotationSpeeds).position(0);

mPlanePositionsBufferHandle = mGeometry.createBuffer(BufferType.FLOAT_BUFFER, mPlanePositions, GLES20.GL_ARRAY_BUFFER);
GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, 0);

mRotationSpeedsBufferHandle = mGeometry.createBuffer(BufferType.FLOAT_BUFFER, mRotationSpeeds, GLES20.GL_ARRAY_BUFFER);
GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, 0);
</code></pre>
<p>We&#8217;ll also have to write a custom shader. Creating a custom shader is the topic of <a title="Rajawali Tutorial 7: Creating a Custom Material / GLSL Shader" href="/blog/2012/02/22/rajawali-tutorial-7-creating-a-custom-material-glsl-shader/">this tutorial</a> so please read that first.</p>
<p>In this shader we&#8217;ll need to add two new attributes to the vertex shader:</p>
<pre><code>attribute vec4 aPlanePosition;
attribute float aRotationSpeed;
</code></pre>
<p>Then in the main() method we apply the rotation matrices manually:</p>
<pre><code>float rotation = uTime * aRotationSpeed;

// -- first rotate around the z axis
mat4 mz = mat4(1.0);
mz[0][0] = cos(rotation);
mz[0][1] = sin(rotation);
mz[1][0] = -sin(rotation);
mz[1][1] = cos(rotation);

// -- then rotate around the y axis
mat4 my = mat4(1.0);
my[0][0] = cos(rotation);
my[0][2] = -sin(rotation);
my[2][0] = sin(rotation);
my[2][2] = cos(rotation);

// -- rotate the vertex before translating it
vec4 rotPos = aPosition * mz * my;

// -- now translate it to the plane position
gl_Position = uMVPMatrix * (rotPos + aPlanePosition);
</code></pre>
<p>It&#8217;s more work than Object3D.setRenderChildrenAsBatch() but the performance gain is significant.</p>
<p>This example is part of the Rajawali Examples app, <a href="https://play.google.com/store/apps/details?id=com.monyetmabuk.rajawali.tutorials">get it from Google Play</a>.</p>
<p>The full source code can be viewed on Github:</p>
<ul>
<li><a href="https://github.com/MasDennis/RajawaliExamples/blob/master/src/com/monyetmabuk/rajawali/tutorials/Rajawali2000PlanesRenderer.java">Rajawali2000PlanesRenderer.java</a></li>
<li><a href="https://github.com/MasDennis/RajawaliExamples/blob/master/src/com/monyetmabuk/rajawali/tutorials/planes/PlanesGalore.java">PlanesGalore.java</a></li>
<li><a href="https://github.com/MasDennis/RajawaliExamples/blob/master/src/com/monyetmabuk/rajawali/tutorials/planes/PlanesGaloreMaterial.java">PlanesGaloreMaterial.java</a></li>
</ul>
<p>On to <a title="Rajawali Tutorial 23: Custom Vertex Shader" href="http://www.rozengain.com/blog/2012/05/16/rajawali-tutorial-23-custom-vertex-shader/">Rajawali Tutorial 23: Custom Vertex Shader</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.rozengain.com/blog/2012/05/03/rajawali-tutorial-22-more-optimisation/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://www.rozengain.com/blog/2012/05/03/rajawali-tutorial-22-more-optimisation/</feedburner:origLink></item>
		<item>
		<title>Rajawali Tutorial 21: Fog</title>
		<link>http://feedproxy.google.com/~r/rozengain/~3/ojjwTtQbdGA/</link>
		<comments>http://www.rozengain.com/blog/2012/05/02/rajawali-tutorial-21-fog/#comments</comments>
		<pubDate>Wed, 02 May 2012 06:59:02 +0000</pubDate>
		<dc:creator>Dennis</dc:creator>
				<category><![CDATA[3D]]></category>
		<category><![CDATA[Android]]></category>
		<category><![CDATA[Rajawali]]></category>
		<category><![CDATA[Fog]]></category>
		<category><![CDATA[OpenGL ES]]></category>

		<guid isPermaLink="false">http://www.rozengain.com/blog/?p=961</guid>
		<description><![CDATA[Here&#8217;s one of the last things that Min3D had but Rajawali didn&#8217;t. In the past few months Rajawali has shaped up to be way better than Min3D. It&#8217;s been a very productive few months and you can expect a lot of new features in the near future. If you have any suggestions just give me [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignnone" title="Rajawali Fog" src="/files/rajawali/rajawali-fog.jpg" alt="Rajawali Fog" width="600" height="360" /></p>
<p>Here&#8217;s one of the last things that Min3D had but Rajawali didn&#8217;t. In the past few months Rajawali has shaped up to be way better than Min3D. It&#8217;s been a very productive few months and you can expect a lot of new features in the near future. If you have any suggestions just give me a shout <img src='http://www.rozengain.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p>So here&#8217;s fog. There&#8217;s not much to it. The most important thing is to enable fog on the RajawaliRenderer:</p>
<pre><code>setFogEnabled(true);
</code></pre>
<p>It&#8217;s important that you do this before creating any materials. If you don&#8217;t there will be no fog. This has to do with shader optimisation.<br />
The other fog parameters can be set on the camera:</p>
<pre><code>mCamera.setFogNear(5);
mCamera.setFogFar(15);
mCamera.setFogColor(0x999999);
</code></pre>
<p>Setting the background color to be the same color as the fog color makes it complete:</p>
<pre><code>setBackgroundColor(0x999999);
</code></pre>
<p>As usual, the full example can be found on Github: <a title="Rajawali Fog Example" href="https://github.com/MasDennis/RajawaliExamples/blob/master/src/com/monyetmabuk/rajawali/tutorials/RajawaliFogRenderer.java">RajawaliFogRenderer.java</a></p>
<p>On to <a href="http://www.rozengain.com/blog/2012/05/03/rajawali-tutorial-22-more-optimisation/">Rajawali Tutorial 22: More Optimisation</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.rozengain.com/blog/2012/05/02/rajawali-tutorial-21-fog/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		<feedburner:origLink>http://www.rozengain.com/blog/2012/05/02/rajawali-tutorial-21-fog/</feedburner:origLink></item>
		<item>
		<title>Rajawali Tutorial 20: Animated Sprites</title>
		<link>http://feedproxy.google.com/~r/rozengain/~3/7Xh_6FrO6Hk/</link>
		<comments>http://www.rozengain.com/blog/2012/04/26/rajawali-tutorial-20-animated-sprites/#comments</comments>
		<pubDate>Thu, 26 Apr 2012 18:24:51 +0000</pubDate>
		<dc:creator>Dennis</dc:creator>
				<category><![CDATA[3D]]></category>
		<category><![CDATA[Android]]></category>
		<category><![CDATA[Rajawali]]></category>
		<category><![CDATA[OpenGL ES]]></category>

		<guid isPermaLink="false">http://www.rozengain.com/blog/?p=955</guid>
		<description><![CDATA[The particle shader has just been updated with some code that&#8217;ll allow you to use sprite sheets. Sprite sheets are basically separate animation frames tiled in a single texture. The shader has logic that loops through these frames at a specified speed. The shader takes these arguments: setCurrentFrame(): the current frame setTileSize(): the size (in [...]]]></description>
			<content:encoded><![CDATA[<p><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="560" height="315" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="allowFullScreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="src" value="http://www.youtube.com/v/bARIz7iDHgc?version=3&amp;hl=en_US" /><param name="allowfullscreen" value="true" /><embed type="application/x-shockwave-flash" width="560" height="315" src="http://www.youtube.com/v/bARIz7iDHgc?version=3&amp;hl=en_US" allowscriptaccess="always" allowfullscreen="true"></embed></object></p>
<p>The particle shader has just been updated with some code that&#8217;ll allow you to use sprite sheets.</p>
<p>Sprite sheets are basically separate animation frames tiled in a single texture. The shader has logic that loops through these frames at a specified speed.</p>
<p>The shader takes these arguments:</p>
<ul>
<li>setCurrentFrame(): the current frame</li>
<li>setTileSize(): the size (in normalized coordinates) of one tile. Tiles must be square. If there are 8 rows of tiles (so 8 * 8 = 64 tiles in total) then the tile size is 1 / 8 = 0.125</li>
<li>setNumTileRows(): the number of rows in one texture. If we follow the example in the previous bullet point then this should be 8.</li>
<li>setAnimOffsets(): this is a FloatBuffer with frame offsets for each separate &#8216;particle&#8217;. Setting this to a random value results in the effect in the above video.</li>
</ul>
<p>The full source code for this can be found on Github as usual:</p>
<ul>
<li><a href="https://github.com/MasDennis/RajawaliExamples/blob/master/src/com/monyetmabuk/rajawali/tutorials/ExampleParticleSystem2.java">ExampleParticleSystem2.java</a></li>
<li><a href="https://github.com/MasDennis/RajawaliExamples/blob/master/src/com/monyetmabuk/rajawali/tutorials/RajawaliAnimatedSpritesRenderer.java">RajawaliAnimatedSpritesRenderer.java</a></li>
</ul>
<p>This demo is also included in the <a href="https://play.google.com/store/apps/details?id=com.monyetmabuk.rajawali.tutorials">Rajawali examples</a> app on Google Play.</p>
<p>On to <a href="http://www.rozengain.com/blog/2012/05/02/rajawali-tutorial-21-fog/">Rajawali Tutorial 21: Fog</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.rozengain.com/blog/2012/04/26/rajawali-tutorial-20-animated-sprites/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.rozengain.com/blog/2012/04/26/rajawali-tutorial-20-animated-sprites/</feedburner:origLink></item>
		<item>
		<title>Rajawali Tutorial 19: Catmull-Rom Splines</title>
		<link>http://feedproxy.google.com/~r/rozengain/~3/hdAVOrJwf_A/</link>
		<comments>http://www.rozengain.com/blog/2012/04/26/rajawali-tutorial-19-catmull-rom-splines/#comments</comments>
		<pubDate>Thu, 26 Apr 2012 17:34:44 +0000</pubDate>
		<dc:creator>Dennis</dc:creator>
				<category><![CDATA[3D]]></category>
		<category><![CDATA[Android]]></category>
		<category><![CDATA[Rajawali]]></category>
		<category><![CDATA[Catmull-Rom]]></category>
		<category><![CDATA[OpenGL ES]]></category>
		<category><![CDATA[Splines]]></category>

		<guid isPermaLink="false">http://www.rozengain.com/blog/?p=947</guid>
		<description><![CDATA[Bezier path animations can look really nice but they can be a huge pain in the *** when you need to constuct them. Catmull-Rom splines can make your life much easier. This spline is named after two people, one of them being Edwin Catmull who is the founder and president of Pixar Animation Studios. So [...]]]></description>
			<content:encoded><![CDATA[<p>Bezier path animations can look really nice but they can be a huge pain in the *** when you need to constuct them.</p>
<p>Catmull-Rom splines can make your life much easier. This spline is named after two people, one of them being Edwin Catmull who is the founder and president of Pixar Animation Studios. So yeah, it must be good then <img src='http://www.rozengain.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>A Catmull-Rom spline consists of two control points and at least two &#8216;normal&#8217; points. So if you pass in less than four points it will throw up on you. Don&#8217;t say I didn&#8217;t warn you :O</p>
<p>All you need to do is create an instance of CatmullRomPath3D and call addPoint() a couple of times:</p>
<pre><code>CatmullRomPath3D path = new CatmullRomPath3D();
path.addPoint(new Number3D(2, 0, -2));
path.addPoint(new Number3D(6, 3, 0));
path.addPoint(new Number3D(1, -2, 0));
path.addPoint(new Number3D(-8, -4, 10));
path.addPoint(new Number3D(0, 0, 12));
path.addPoint(new Number3D(11, 6, 8));
</code></pre>
<p>This line can now be drawn by stitching together line segments or you can use it as an animation path.<br />
To accomplish the latter, just pass the path into the TranslateAnimation3D constructor to create a nice animation:</p>
<pre><code>TranslateAnimation3D anim = new TranslateAnimation3D(path);
anim.setDuration(12000);
anim.setRepeatCount(Animation3D.INFINITE);
anim.setRepeatMode(Animation3D.REVERSE);
// -- orient to path
anim.setOrientToPath(true);
anim.setTransformable3D(arrow);
anim.start();
</code></pre>
<p>Passing &#8216;true&#8217; to the setOrientToPath() method will set the object&#8217;s orientation to current path segment.</p>
<p>To draw the path you can call CatmullRomPath3D&#8217;s calculatePoint() method to get the interpolated point. Once you&#8217;ve filled the Stack with Number3Ds you can pass it on to the Line3D constructor to visualize the spline:</p>
<pre><code>Stack linePoints = new Stack();
for (int i = 0; i &lt; 100; i++) {
	linePoints.add(path.calculatePoint(i / 100f));
}
Line3D line = new Line3D(linePoints, 1, 0xffffffff);
SimpleMaterial material = new SimpleMaterial();
material.setUseColor(true);
line.setMaterial(material);
addChild(line);
</code></pre>
<p>Which will result into something similar like this:</p>
<p><img class="alignnone" title="Visualizing Catmull-Rom Splines with Rajawali" src="/files/rajawali/rajawali-catmull-rom-spline.jpg" alt="Visualizing Catmull-Rom Splines with Rajawali" width="500" height="313" /></p>
<p>The shape in the first image can be seen in <a title="The Rajawali Examples app on Google Play" href="https://play.google.com/store/apps/details?id=com.monyetmabuk.rajawali.tutorials">the examples app</a>.</p>
<p>The source code can be found <a href="https://github.com/MasDennis/RajawaliExamples/blob/master/src/com/monyetmabuk/rajawali/tutorials/RajawaliCatmullRomRenderer.java">on Github</a>.</p>
<p>On to <a href="http://www.rozengain.com/blog/2012/04/26/rajawali-tutorial-20-animated-sprites/">Rajawali Tutorial 20: Animated Sprites</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.rozengain.com/blog/2012/04/26/rajawali-tutorial-19-catmull-rom-splines/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.rozengain.com/blog/2012/04/26/rajawali-tutorial-19-catmull-rom-splines/</feedburner:origLink></item>
		<item>
		<title>Rajawali Tutorial 18: Drawing Lines</title>
		<link>http://feedproxy.google.com/~r/rozengain/~3/zmpGHbZThPI/</link>
		<comments>http://www.rozengain.com/blog/2012/04/26/rajawali-tutorial-18-drawing-lines/#comments</comments>
		<pubDate>Thu, 26 Apr 2012 17:10:24 +0000</pubDate>
		<dc:creator>Dennis</dc:creator>
				<category><![CDATA[3D]]></category>
		<category><![CDATA[Android]]></category>
		<category><![CDATA[Rajawali]]></category>
		<category><![CDATA[OpenGL ES]]></category>

		<guid isPermaLink="false">http://www.rozengain.com/blog/?p=935</guid>
		<description><![CDATA[A line in Rajawali consists of one or more line segments. First you need to specify all the points that make up the line. Like so: Stack points = new Stack(); points.add(new Number3D(2, 0, 0)); points.add(new Number3D(4, 0, 0)); points.add(new Number3D(2, 2, 0)); points.add(new Number3D(2, 0, 6)); // ... etc ... This Stack can then [...]]]></description>
			<content:encoded><![CDATA[<p>A line in Rajawali consists of one or more line segments. First you need to specify all the points that make up the line. Like so:</p>
<pre><code>Stack points = new Stack();
points.add(new Number3D(2, 0, 0));
points.add(new Number3D(4, 0, 0));
points.add(new Number3D(2, 2, 0));
points.add(new Number3D(2, 0, 6));
// ... etc ...
</code></pre>
<p>This Stack can then be passed into the constructor of the Line3D class along with a thickness and a color:</p>
<pre><code>Line3D line = new Line3D(points, 1, 0xffffff00);
SimpleMaterial material = new SimpleMaterial();
material.setUseColor(true);
whirl.setMaterial(material);
addChild(whirl);
</code></pre>
<p>It&#8217;s easy as that. Using line segments like this you can create nice looking shapes like this:<br />
<img class="alignnone" title="Drawing Lines with the Rajawali Framework" src="/files/rajawali/rajawali-lines.jpg" alt="Drawing Lines with the Rajawali Framework" width="500" height="313" /></p>
<p>It&#8217;s also relatively easy to draw bezier curves. You first need to create an instance of BezierPath3D and then pass in the points and control points:</p>
<pre><code>BezierPath3D bezierPath = new BezierPath3D();
bezierPath.addPoint(new Number3D(0, -4, 0), new Number3D(-2, -4, .2f), new Number3D(4, 4, 4), new Number3D(-2, 4, 4.5f));
bezierPath.addPoint(new Number3D(-2, 4, 4.5f), new Number3D(2, -2, -2), new Number3D(4, 4, 4), new Number3D(-2, 4, 4.5f));
</code></pre>
<p>Now we can interpolate through these points and create line segments. The more segments, the smoother the line.</p>
<pre><code>// -- again, create a Stack of Number3Ds
Stack points = new Stack();

// -- the more segments, the smoother the line
int numLineSegments = 100;

for(int i=0; i &lt; numLineSegments; i++) {
	// -- BezierPath3D's calculatePoint() method calculates and interpolated
	//     Number3D. It accepts values between 0 and 1, hence i / numLineSegments
	points.push(bezierPath.calculatePoint(i / (float)numLineSegments));
}

// -- create out bezier curve
Line3D line = new Line3D(points, 1, 0x00ff00);
SimpleMaterial material = new SimpleMaterial();
material.setUseColor(true);
line.setMaterial(material);
addChild(line);</code></pre>
<p>&#8230; and here&#8217;s the result of that:</p>
<p><img class="alignnone" title="Drawing Bezier Curves with Rajawali" src="/files/rajawali/rajawali-bezier-curve.jpg" alt="Drawing Bezier Curves with Rajawali" width="500" height="313" /></p>
<p>The shape in the first image can be seen in <a title="The Rajawali Examples app on Google Play" href="https://play.google.com/store/apps/details?id=com.monyetmabuk.rajawali.tutorials">the examples app</a>.</p>
<p>The source code can be found <a href="https://github.com/MasDennis/RajawaliExamples/blob/master/src/com/monyetmabuk/rajawali/tutorials/RajawaliLinesRenderer.java">on Github</a>.</p>
<p>On to <a href="http://www.rozengain.com/blog/2012/04/26/rajawali-tutorial-19-catmull-rom-splines/">Rajawali Tutorial 19: Catmull-Rom Splines</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.rozengain.com/blog/2012/04/26/rajawali-tutorial-18-drawing-lines/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		<feedburner:origLink>http://www.rozengain.com/blog/2012/04/26/rajawali-tutorial-18-drawing-lines/</feedburner:origLink></item>
		<item>
		<title>Rajawali Tutorial 17: Importing .Obj Files</title>
		<link>http://feedproxy.google.com/~r/rozengain/~3/Id_7Y5CPuoU/</link>
		<comments>http://www.rozengain.com/blog/2012/04/13/rajawali-tutorial-17-importing-obj-files/#comments</comments>
		<pubDate>Fri, 13 Apr 2012 14:23:18 +0000</pubDate>
		<dc:creator>Dennis</dc:creator>
				<category><![CDATA[3D]]></category>
		<category><![CDATA[Android]]></category>
		<category><![CDATA[Rajawali]]></category>
		<category><![CDATA[OpenGL ES]]></category>

		<guid isPermaLink="false">http://www.rozengain.com/blog/?p=909</guid>
		<description><![CDATA[The basics have been covered in an earlier tutorial but more info about this was requested. There are some specific things to take into account when dealing with .obj files. The most important thing is that the model should be triangulated. Rajawali doesn&#8217;t accept quads, only tris. In Blender, this is an option you can select [...]]]></description>
			<content:encoded><![CDATA[<p>The basics have been covered in an earlier tutorial but more info about this was requested. There are some specific things to take into account when dealing with .obj files.</p>
<p>The most important thing is that the model should be triangulated. Rajawali doesn&#8217;t accept quads, only tris. In <a href="http://www.blender.org">Blender</a>, this is an option you can select in the exporter. In a program like <a title="MeshLab" href="http://meshlab.sourceforge.net/">MeshLab</a>, this is done automatically.</p>
<p>At the moment, Rajawali also doesn&#8217;t support per-face textures. This is on the todo list.</p>
<p>The options that should be checked when exporting from blender are:</p>
<ul>
<li>Apply Modifiers</li>
<li>Include Normals</li>
<li>Include UVs</li>
<li>Write Materials (if applicable)</li>
<li>Triangulate Faces</li>
<li>Objects as OBJ Objects</li>
</ul>
<p>The files should be written to your &#8220;res/raw&#8221; folder in your ADT project. Usually you&#8217;ll get errors in the console when you do this. The Android SDK ignores file extensions so it&#8217;ll regard the .obj and .mtl files as duplicates. The way to fix this is to rename the files. For instance:</p>
<ul>
<li>myobject.obj &gt; myobject_obj</li>
<li>myobject.mtl &gt; myobject_mtl</li>
</ul>
<p>The parser replaces any dots in file names, so this should be picked up automatically by the parser. Path fragments in front of file names (also texture paths) are discarded so you can leave them as is.</p>
<p>The texture file paths in the .mtl files are stripped off periods and path fragments as well. The textures need to be placed in the res/drawable-nodpi folder.</p>
<p>If it still throws errors check if there are any funny characters or unsupported texture formats (like bmp).</p>
<p>Just as a reminder, here&#8217;s the code that takes care of the parsing:</p>
<pre><code>ObjParser objParser = new ObjParser(mContext.getResources(), mTextureManager, R.raw.myobject_obj);
objParser.parse();
BaseObject3D mObject = objParser.getParsedObject();
mObject.setLight(mLight);
addChild(mObject);
</code></pre>
<p>Or check the <a href="https://github.com/MasDennis/RajawaliExamples/blob/master/src/com/monyetmabuk/rajawali/tutorials/RajawaliLoadModelRenderer.java">source file here</a> on Github.</p>
<p>On to <a href="http://www.rozengain.com/blog/2012/04/26/rajawali-tutorial-18-drawing-lines/">Rajawali Tutorial 18: Drawing Lines</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.rozengain.com/blog/2012/04/13/rajawali-tutorial-17-importing-obj-files/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		<feedburner:origLink>http://www.rozengain.com/blog/2012/04/13/rajawali-tutorial-17-importing-obj-files/</feedburner:origLink></item>
		<item>
		<title>Rajawali Tutorial 16: Collision Detection</title>
		<link>http://feedproxy.google.com/~r/rozengain/~3/6jk01YjuDX0/</link>
		<comments>http://www.rozengain.com/blog/2012/04/13/rajawali-tutorial-16-collision-detection/#comments</comments>
		<pubDate>Fri, 13 Apr 2012 06:56:54 +0000</pubDate>
		<dc:creator>Dennis</dc:creator>
				<category><![CDATA[3D]]></category>
		<category><![CDATA[Android]]></category>
		<category><![CDATA[Rajawali]]></category>
		<category><![CDATA[Collision Detection]]></category>
		<category><![CDATA[OpenGL ES]]></category>

		<guid isPermaLink="false">http://www.rozengain.com/blog/?p=899</guid>
		<description><![CDATA[Rajawali supports simple collision detection using bounding boxes and bounding spheres. The Geometry3D class contains two methods that can be used to retrieve the bounding volumes: // -- myObject is of type BaseObject3D // -- get the bounding box IBoundingVolume boundingBox = myObject.getGeometry().getBoundingBox(); // -- or get the bounding sphere IBoundingVolume boundingSphere = myObject.getGeometry().getBoundingSphere(); Both [...]]]></description>
			<content:encoded><![CDATA[<p>Rajawali supports simple collision detection using bounding boxes and bounding spheres. The Geometry3D class contains two methods that can be used to retrieve the bounding volumes:</p>
<pre><code>// -- myObject is of type BaseObject3D
// -- get the bounding box

IBoundingVolume boundingBox = myObject.getGeometry().getBoundingBox();

// -- or get the bounding sphere

IBoundingVolume boundingSphere = myObject.getGeometry().getBoundingSphere();
</code></pre>
<p>Both the BoundingBox and BoundingSphere classes have a transform() method that takes the current model&#8217;s model matrix as the only parameter. This is needed to translate, rotate and scale the bounding volume.</p>
<pre><code>boundingBox.transform(myObject.getModelMatrix());
</code></pre>
<p>Once the bounding volume has been transformed we can check if it intersects with another bounding volume (of the same kind):</p>
<pre><code>boolean intersects = boundingBox.intersectsWith(otherBoundingBox);
if(intersects)
     // -- collision!
else
    // -- no collision <img src='http://www.rozengain.com/blog/wp-includes/images/smilies/icon_sad.gif' alt=':-(' class='wp-smiley' />
</code></pre>
<p>The full version of this can be found on Github:</p>
<ul>
<li><a title="Rajawali Collision Detection Activity source file" href="https://github.com/MasDennis/RajawaliExamples/blob/master/src/com/monyetmabuk/rajawali/tutorials/RajawaliCollisionDetectionActivity.java">RajawaliCollisionDetectionActivity.java</a></li>
<li><a title="Rajawali Collision Detection Renderer source file" href="https://github.com/MasDennis/RajawaliExamples/blob/master/src/com/monyetmabuk/rajawali/tutorials/RajawaliCollisionDetectionRenderer.java">RajawaliCollisionDetectionRenderer.java</a></li>
</ul>
<p>As usual, all suggestions for features and improvements are welcome. Or if you&#8217;re a collision detection expert and you&#8217;re willing to make this more advanced, why not <a href="https://github.com/MasDennis/Rajawali">fork Rajawali on Github</a> and make a contribution?</p>
<p>On to <a href="http://www.rozengain.com/blog/2012/04/13/rajawali-tutorial-17-importing-obj-files/">Rajawali Tutorial 17: Importing .Obj Files</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.rozengain.com/blog/2012/04/13/rajawali-tutorial-16-collision-detection/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://www.rozengain.com/blog/2012/04/13/rajawali-tutorial-16-collision-detection/</feedburner:origLink></item>
	</channel>
</rss>

