<?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>Marc&#039;s blog</title>
	<atom:link href="http://marc.blog.atpurpose.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://marc.blog.atpurpose.com</link>
	<description>Marc&#039;s iPhone development blog</description>
	<lastBuildDate>Mon, 02 Nov 2009 19:17:35 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.5</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Counting FPS</title>
		<link>http://marc.blog.atpurpose.com/2009/11/02/counting-fps/</link>
		<comments>http://marc.blog.atpurpose.com/2009/11/02/counting-fps/#comments</comments>
		<pubDate>Mon, 02 Nov 2009 18:42:45 +0000</pubDate>
		<dc:creator>marc</dc:creator>
				<category><![CDATA[iPhone development]]></category>

		<guid isPermaLink="false">http://marc.blog.atpurpose.com/?p=462</guid>
		<description><![CDATA[In the previous article, we saw how to create a sphere, which we&#8217;ll now use in this project as basis for a simple benchmark:

AnXcode project for the iPhone  is provided with full src code, which you can use in your own project. The core code is written in plain C, so the project should [...]]]></description>
			<content:encoded><![CDATA[<p>In the <a class="wp-caption" href="http://marc.blog.atpurpose.com/2009/10/31/creating-sphere-from-a-rectangular-mesh/" target="_self">previous article</a>, we saw how to create a sphere, which we&#8217;ll now use in this project as basis for a simple benchmark:</p>
<p><img class="aligncenter size-full wp-image-465" title="SphereBenchmark2" src="http://marc.blog.atpurpose.com/wp-content/uploads/2009/11/SphereBenchmark2.png" alt="SphereBenchmark2" width="385" height="207" /></p>
<p>An<a class="wp-caption" href="http://blog.download.atpurpose.com/SphereBenchmark.zip">Xcode project for the iPhone </a> is provided with full src code, which you can use in your own project. The core code is written in plain C, so the project should be very easy to port over to GLUT or any other framework.</p>
<p>There were several driving factors that motivated me to create this project. I wanted to learn:</p>
<p style="text-align: left; padding-left: 30px;">1 how many primitive (unlit, untextured) triangles can my iPhone render?<br />
2 how to implement FPS (Frames Per Second) counter?<br />
3 how to implement a simple HUD (Heads Up Display)?<br />
4 how to use textures?<br />
5 how to render text using OpenGL only?<br />
6 how to handle changes to screen orientations?</p>
<p style="text-align: left;">Answer 1: On my 3G iPhone the answer appears to be around 30k triangles per second  (the number of triangles per frame is printed out to stderr, so check your log). The project as it is currently setup, renders 4936 triangles per frame at 60 fps. Also notice that the grid rendered on top of the spheres has a different triangle count that the sphere itself. See<em> pen.c</em><em> </em> for more details.</p>
<p style="text-align: left;">Answer 2: To be useful, the FPS counter updates its value on the screen once per second (more often updates make the reading difficult), with a value that is an average of all sampled frames in the past 1 second. In addition to the FPS value, I personally found it useful to display a second line underneath showing the utilization, which quickly tells me if the GPU is maxed out or not, and how much more stuff I could push. See <em>fps.c</em><em> </em> for more details.</p>
<p style="text-align: left;">Answer 3: The HUD is a simple transparent rectangle, rendered using <em>glOrthof</em> projection, which uses alpha blending to allow the scene underneath it to show through. See <em>fps.c</em><em> </em> for more details.</p>
<p style="text-align: left;">Answer 4: All the GPU cares about are raw pixel values, so we can load a texture in any way we want to, or generate one procedurally. Just before uploading the pixels, however, we need to make sure that they  are right side up. Also, and this is very important or otherwise your texture will not show up, we must define <em>GL_TEXTURE_MIN_FILTER</em>, <em>GL_TEXTURE_MAG_FILTER</em>, <em>GL_TEXTURE_WRAP_S</em> and <em>GL_TEXTURE_WRAP_T</em>. Lastly, if the texture uses alpha pixels and is transparent, the blend function to use is <em>glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)</em>. See <em>fps.c</em> for more details.</p>
<p style="text-align: left;">Answer 5: To render text using only OpenGL, we need two things: font texture and the glyph positions. To generate font texture I used <a class="wp-caption" href="http://www.angelcode.com/products/bmfont/">PC utility called bmfont</a> which also generates glyph positions in several different formats. I picked xml, but then instead of hooking in xml parser I just manually created a table for the 16 glyphs I use in this project. For real apps we would obviously need to use the actual xml. See <em>EAGLView.m</em><em> </em> for more details.</p>
<p style="text-align: left;">Answer 6: The screen orientation is easily handled with <em>glRotatef (GLfloat angle, GLfloat x, GLfloat y, GLfloat z)</em> with the only caveat being that for the orthogonal projection we also need to apply translations. See <em>tilt.c</em> for more details.</p>
<p style="text-align: left;">
]]></content:encoded>
			<wfw:commentRss>http://marc.blog.atpurpose.com/2009/11/02/counting-fps/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Creating sphere from a rectangular mesh</title>
		<link>http://marc.blog.atpurpose.com/2009/10/31/creating-sphere-from-a-rectangular-mesh/</link>
		<comments>http://marc.blog.atpurpose.com/2009/10/31/creating-sphere-from-a-rectangular-mesh/#comments</comments>
		<pubDate>Sun, 01 Nov 2009 00:52:02 +0000</pubDate>
		<dc:creator>marc</dc:creator>
				<category><![CDATA[iPhone development]]></category>
		<category><![CDATA[circle]]></category>
		<category><![CDATA[mesh]]></category>
		<category><![CDATA[OpenGL]]></category>
		<category><![CDATA[sphere]]></category>

		<guid isPermaLink="false">http://marc.blog.atpurpose.com/?p=345</guid>
		<description><![CDATA[In the previous article, we saw how to create a rectangular mesh using one long triangle strip. I wrote that such a mesh can be used to wrap around a sphere and now I will show how to do it:

AnXcode project for the iPhone  is provided with full src code, which you can use [...]]]></description>
			<content:encoded><![CDATA[<p>In the <a class="wp-caption" href="http://marc.blog.atpurpose.com/2009/10/24/programatically-generating-a-rectangular-mesh-using-single-gl_triangle_strip/" target="_self">previous article</a>, we saw how to create a rectangular mesh using one long triangle strip. I wrote that such a mesh can be used to wrap around a sphere and now I will show how to do it:</p>
<p><img class="aligncenter size-full wp-image-359" title="Sphere0" src="http://marc.blog.atpurpose.com/wp-content/uploads/2009/10/Sphere0.png" alt="Sphere0" width="530" height="385" /></p>
<p>An<a class="wp-caption" href="http://blog.download.atpurpose.com/Sphere.zip">Xcode project for the iPhone </a> is provided with full src code, which you can use in your own project. The core code is written in plain C, so the project should be very easy to port over to GLUT or any other framework.</p>
<p>Note: for this project we made the choice to have <em>X-axis</em> run from left to right, <em>Z-axis</em> from the back of the screen towards the front, and our <em>Y-axis</em> to run from the bottom of the screen towards the top. Our sphere&#8217;s radius will be defined as <em>span</em>.</p>
<p>We start by wrapping the 2D mesh into a 3D tube:</p>
<p><img class="aligncenter size-full wp-image-349" title="Sphere2" src="http://marc.blog.atpurpose.com/wp-content/uploads/2009/10/Sphere2.png" alt="Sphere2" width="207" height="385" /></p>
<p>We do this by choosing two opposite edges and stitching them together by using the same vertex values. We generate point values <em>P(x,y, z)</em>, where y = [-span, ..., 0, ..., span] by using Eq. 1a as shown in Fig. 1, except that our points are <em>P(x,z)</em>, not <em>P(x,y)</em>:</p>
<p style="text-align: center;"><img class="aligncenter size-full wp-image-382" style="border: 1px solid silver;" title="Sphere8" src="http://marc.blog.atpurpose.com/wp-content/uploads/2009/10/Sphere8.png" alt="Sphere8" width="480" height="390" /></p>
<p style="text-align: left;">At this point if we were to set r=0 for the top and bottom rows, and r=<em>span</em> for the rest of the rows then using Eq. 1.a we&#8217;d generate a solid cylinder:</p>
<p style="text-align: left;"><img class="aligncenter size-full wp-image-350" title="Sphere3" src="http://marc.blog.atpurpose.com/wp-content/uploads/2009/10/Sphere3.png" alt="Sphere3" width="207" height="385" /></p>
<p style="text-align: left;">We want to create a sphere, however, so we have a little bit more work to do. Our task is to find radius for each row (for the cylinder above, we in fact got 3 rows right already: the top, bottom and the middle row). We can find the radius values using Eq. 1b from Fig. 1. where our r=<em>span</em>, y is generated by some function with range of [-span, ..., 0, ..., span].</p>
<p style="text-align: left;">A first effort to generate the <em>y</em> would be to use a simple linear function f(x)=y. If we do that, we will create this interesting sphere:</p>
<p style="text-align: center;"><img class="aligncenter size-full wp-image-413" title="Sphere9" src="http://marc.blog.atpurpose.com/wp-content/uploads/2009/10/Sphere9.png" alt="Sphere9" width="414" height="385" /></p>
<p style="text-align: left;">We generated a sphere with bold spots at top and bottom. It lacks detail at the north and south zenith, and instead allocates more triangles to the rest of the rows, which might be exactly what we want, if we wanted to use it for sky view, for example.</p>
<p style="text-align: left;">To generate &#8220;perfect&#8221; sphere, however, all we need is to do is to use the circle equation again. Conceptually, we want to use Eq. 1a again, but this time we are only interested in generating y values for half of the circle, so all we need is just one part of the equation, ie: x= r * sin(angle) where r=<em>span</em>, angle is generated by linear function with domain [-span, ..., 0, ..., span] and range [0, 180]</p>
<p style="text-align: left;">Once we calculate the <em>x</em>, we use it as our <em>y</em> back in Eq. 1b. This time we get our final sphere:</p>
<p style="text-align: left;"><img class="aligncenter size-full wp-image-414" title="Sphere10" src="http://marc.blog.atpurpose.com/wp-content/uploads/2009/10/Sphere10.png" alt="Sphere10" width="414" height="373" /></p>
<p style="text-align: left;">The code with all the pieces put together:</p>
<div style="border: 1px solid silver; margin: 5px">
<pre style="text-align: left; margin: 10px">void meshWrapAroundSphere(Mesh *mesh)
{
  GLuint vertColumns = mesh-&gt;nrOfVerticiesInRow;
  GLuint vertRows = mesh-&gt;nrOfVerticiesInColumn;

  GLfloat heightStep = (2.0f / (vertRows-1));
  GLfloat height = -1.0f;

  GLfloat angleStep = DEGREES_TO_RADIANS(360.0f / (GLfloat)(vertColumns-1));

  for (int r=0; r&lt;vertRows; r++)
  {
    GLfloat heightScaled = mesh-&gt;span * sinf(DEGREES_TO_RADIANS(height*90.0f));
    GLfloat radius = 0.0f;
    if ((r &gt; 0) &amp;&amp; (r &lt; (vertRows-1)))
    {
      radius = sqrtf((mesh-&gt;span*mesh-&gt;span)-(heightScaled*heightScaled));
    }

    GLfloat angle = angleStep;
    for (int c=0; c&lt;(vertColumns-1); c++)
    {
      MeshVertex *vertex = &amp;mesh-&gt;verticies[r*vertColumns + c];
      vertex-&gt;y = heightScaled;
      vertex-&gt;x = (radius * cosf(angle));
      vertex-&gt;z = (radius * sinf(angle));

      if (c == 0)
      {
         MeshVertex *vertex2 = &amp;mesh-&gt;verticies[r*vertColumns + (vertColumns-1)];
         vertex2-&gt;x = vertex-&gt;x;
         vertex2-&gt;y = vertex-&gt;y;
         vertex2-&gt;z = vertex-&gt;z;
      }
      angle += angleStep;
    }
    height += heightStep;
  }
}</pre>
</div>
<p style="text-align: left;"><img class="aligncenter size-full wp-image-139" title="spacer" src="http://marc.blog.atpurpose.com/wp-content/uploads/2009/10/spacer.png" alt="spacer" width="20" height="20" /></p>
<p style="text-align: left;">
<p style="text-align: left;">
<p style="text-align: left;">
<p style="text-align: left;">While researching this topic I found the following page useful: <a class="wp-caption" href="http://local.wasp.uwa.edu.au/~pbourke/miscellaneous/sphere_cylinder/" target="_blank">http://local.wasp.uwa.edu.au/~pbourke/miscellaneous/sphere_cylinder/</a></p>
<p style="text-align: left;">
<p style="text-align: left;">
<p style="text-align: center;">
<p style="text-align: center;">
]]></content:encoded>
			<wfw:commentRss>http://marc.blog.atpurpose.com/2009/10/31/creating-sphere-from-a-rectangular-mesh/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Programmatically generating a rectangular mesh using single GL_TRIANGLE_STRIP</title>
		<link>http://marc.blog.atpurpose.com/2009/10/24/programatically-generating-a-rectangular-mesh-using-single-gl_triangle_strip/</link>
		<comments>http://marc.blog.atpurpose.com/2009/10/24/programatically-generating-a-rectangular-mesh-using-single-gl_triangle_strip/#comments</comments>
		<pubDate>Sun, 25 Oct 2009 03:51:31 +0000</pubDate>
		<dc:creator>marc</dc:creator>
				<category><![CDATA[iPhone development]]></category>
		<category><![CDATA[glDrawElements]]></category>
		<category><![CDATA[GL_TRIANGLE_STRIP]]></category>
		<category><![CDATA[mesh]]></category>
		<category><![CDATA[OpenGL]]></category>
		<category><![CDATA[rectangle]]></category>
		<category><![CDATA[VBO]]></category>

		<guid isPermaLink="false">http://blog.atpurpose.com/?p=95</guid>
		<description><![CDATA[Apple recommends to use OpenGL GL_TRIANGLE_STRIP and VBOs for iPhone geometry, so when I started looking into terrain generator, the very first question I asked myself was how to programmatically generate a rectangular mesh using single GL_TRIANGLE_STRIP.  After a bit of research and coding I wrote this simple mesh generator:


An Xcode project for the iPhone  is [...]]]></description>
			<content:encoded><![CDATA[<p>Apple recommends to use OpenGL <em>GL_TRIANGLE_STRIP </em>and VBOs for iPhone geometry, so when I started looking into terrain generator, the very first question I asked myself was how to programmatically generate a rectangular mesh using single <em>GL_TRIANGLE_STRIP</em>.  After a bit of research and coding I wrote this simple mesh generator:</p>
<p style="text-align: center;"><a href="http://marc.blog.atpurpose.com/wp-content/uploads/2009/10/MeshScreenshot1.png"><img class="aligncenter size-full wp-image-184" style="margin-top: 10px; margin-bottom: 10px;" title="MeshScreenshot" src="http://marc.blog.atpurpose.com/wp-content/uploads/2009/10/MeshScreenshot1.png" alt="MeshScreenshot" /></a></p>
<p style="text-align: center;">
<p>An <a class="wp-caption" href="http://blog.download.atpurpose.com/Mesh.zip">Xcode project for the iPhone </a> is provided with full src code, which you can use in your own project. The mesh generator provides two APIs (see <em>mesh.h</em>): one which takes number of verticies in column and row, and the other one which takes desired triangle count. The core code is written in plain C, so the project should be very easy to port over to GLUT or any other framework.</p>
<p style="text-align: center;">
<p style="text-align: left;">A mesh, for our purposes is defined as a rectangular piece of geometry, defined by its vertices. A very simple example of our mesh is a 3&#215;3 rectangle defined by 9 vertices (8 triangles) &#8211; see Fig. 1:</p>
<p style="text-align: center;"><a href="http://marc.blog.atpurpose.com/wp-content/uploads/2009/10/MeshFig1.png"><img class="aligncenter size-full wp-image-326" style="border: 1px solid silver; margin-top: 10px; margin-bottom: 10px;" title="MeshFig1" src="http://marc.blog.atpurpose.com/wp-content/uploads/2009/10/MeshFig1.png" alt="MeshFig1" /></a></p>
<p style="text-align: center;">
<p style="text-align: left;">Such mesh is pretty easy to generate (see <em>meshCreate</em> in <em>mesh.c</em>) and is stored in an array of <em>MeshVertex</em> structures, with 3 <em>GLfloat</em> per entry. Those vertices will be later uploaded to GPU using <em>glBufferData</em> (see _<em>meshCreate</em> in <em>pen.c</em>). Next we need to create an array of vertex indicies that will tell the GPU how to construct our triangles &#8211; see Fig. 2:</p>
<p style="text-align: center;"><a href="http://marc.blog.atpurpose.com/wp-content/uploads/2009/10/MeshFig2.png"><img class="aligncenter size-full wp-image-231" style="border: 1px solid silver; margin-top: 10px; margin-bottom: 10px;" title="MeshFig2" src="http://marc.blog.atpurpose.com/wp-content/uploads/2009/10/MeshFig2.png" alt="MeshFig2" /></a></p>
<p style="text-align: left;">Looking at Fig. 2a and Fig. 2b it&#8217;s easy to see how the indicies for strips are generated. Triangle #1 is defined by indicies 1, 4 and 2, triangle #2 is defined by indicies 4, 2 and 5, and so on&#8230; This is what makes the triangle strips so efficient &#8211; the 1st triangle requires 3 vertices (9 <em>GLfloats</em>), but to define the next one, all we need is just one additional explicit vertex (3 <em>GLfloats</em>). The indicies are stored in an array of <em>GLushort</em>. The implications of using GLushort, for storing the indicies is that the mesh can not have more than 2^16 (ie. 65536) entries. A good optimization here would be to use the smallest, but large enough type to hold our indicies (ie, <em>GLushort</em> or <em>GLubyte</em>).</p>
<p style="text-align: left;">
<p style="text-align: left;">Now that we know how to create strips that define mesh rows, we need to figure out how to stitch the strips together to create one long strip &#8211; see Fig. 3:</p>
<p style="text-align: center;"><a href="http://marc.blog.atpurpose.com/wp-content/uploads/2009/10/MeshFig3.png"><img class="aligncenter size-full wp-image-263" style="border: 1px solid silver; margin-top: 10px; margin-bottom: 10px;" title="MeshFig3" src="http://marc.blog.atpurpose.com/wp-content/uploads/2009/10/MeshFig3.png" alt="MeshFig3" width="570" height="189" /></a></p>
<p style="text-align: left;">Our task here is to tell the GPU how to jump from the end of one strip to the beginning of the next one. The solution is to insert <strong>degenerate </strong>(zero area) triangles &#8211; such triangles will not be drawn by the GPU. A degenerate triangle is defined by 3 verticies, at least 2 of which are the same. In our case, we will repeat last index of the previous strip and first index of the next one. By inserting 2 extra indicies we create 4 additional (degenerate) triangles, ie:</p>
<p style="text-align: left;">
<p style="text-align: center;">- triangle #1 defined by indicies: 3, 6, 6</p>
<p style="text-align: center;">- triangle #2 defined by indicies: 6, 6, 4</p>
<p style="text-align: center;">- triangle #3 defined by indicies: 6, 4, 4</p>
<p style="text-align: center;">- triangle #4 defined by indicies: 4, 4, 7</p>
<p style="text-align: left;">That&#8217;s it. To render the mesh all we need to do is to call <em>glDrawElements</em> once per frame. Such a mesh can be used to render more than just terrain, we could even use it to wrap around a sphere if we wanted to. Too bad OpenGL has no such thing as<em> &#8220;GL_TRIANGLE_MESH&#8221; </em>- it wouldn&#8217;t even need the explicit index array, if it could assume only rectangular meshes.</p>
<p style="text-align: center;">
<p style="text-align: center;">
<p style="text-align: left;"><img class="aligncenter size-full wp-image-139" title="spacer" src="http://marc.blog.atpurpose.com/wp-content/uploads/2009/10/spacer.png" alt="spacer" width="20" height="20" /></p>
<p style="text-align: left;">While researching this topic I found the following discussions useful: <a class="wp-caption" href="http://www.gamedev.net/community/forums/topic.asp?topic_id=208950" target="_blank">http://www.gamedev.net/community/forums/topic.asp?topic_id=208950</a> <a class="wp-caption" href="http://www.gamedev.net/reference/articles/article1871.asp" target="_blank">http://www.gamedev.net/reference/articles/article1871.asp</a></p>
<p style="text-align: left;">
<p>For a quick overview of <em>GL_TRIANGLE_STRIP</em> please see this <a class="wp-caption" title="wikipedia entry" href="http://en.wikipedia.org/wiki/Triangle_strip" target="_blank">wikipedia entry</a>; the advantage here is that triangle strips save VRAM and lay out vertices in optimal format for the GPU, both of which contribute to higher performance (this optimization is not as important for desktop GPUs).</p>
<p style="text-align: center;">
<p>For a quick overview of VBOs please see this <a class="wp-caption" title="wikipedia entry" href="http://en.wikipedia.org/wiki/Vertex_Buffer_Object" target="_blank">wikipedia entry</a>; the theory here is that we eliminate copying of vertices from RAM (CPU) to VRAM (GPU) whenever we need to draw them, by uploading the verticies only once, which again helps the performance (today&#8217;s iPhone/iPods use integrated memory, which means that this optimization is not currently as important).</p>
<p style="text-align: left;">
<p style="text-align: left;">
]]></content:encoded>
			<wfw:commentRss>http://marc.blog.atpurpose.com/2009/10/24/programatically-generating-a-rectangular-mesh-using-single-gl_triangle_strip/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

