<?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>INsanityDesign &#187; mipmap</title>
	<atom:link href="http://insanitydesign.com/wp/tag/mipmap/feed/" rel="self" type="application/rss+xml" />
	<link>http://insanitydesign.com/wp</link>
	<description>The Imagination Enhancer</description>
	<lastBuildDate>Sat, 31 Dec 2011 22:14:05 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Android OpenGL ES &#8211; Mipmaps</title>
		<link>http://insanitydesign.com/wp/2009/08/01/android-opengl-es-mipmaps/</link>
		<comments>http://insanitydesign.com/wp/2009/08/01/android-opengl-es-mipmaps/#comments</comments>
		<pubDate>Sat, 01 Aug 2009 22:02:08 +0000</pubDate>
		<dc:creator>NeA</dc:creator>
				<category><![CDATA[Android]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Internet]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[mipmap]]></category>
		<category><![CDATA[opengl]]></category>
		<category><![CDATA[opengl es]]></category>

		<guid isPermaLink="false">http://insanitydesign.com/wp/?p=128</guid>
		<description><![CDATA[I am currently a little bit experimenting with Android and its SDK as I am proud owner of a HTC Magic as well as I am pretty impressed by the SDK, Android itself and the ideas and principles it follows. More precisely I am playing around with the graphical functionalities of Android, especially OpenGL. Android [...]]]></description>
			<content:encoded><![CDATA[<p>
I am currently a little bit experimenting with <a href="http://www.android.com/">Android</a> and its <a href="http://developer.android.com/">SDK</a> as I am proud owner of a <a href="http://www.htc.com/uk/product/magic/overview.html">HTC Magic</a> as well as I am pretty impressed by the SDK, Android itself and the ideas and principles it follows.<br />
More precisely I am playing around with the graphical functionalities of Android, especially <a href="http://www.opengl.org/">OpenGL</a>. Android contains an OpenGL ES implementation, therefore I am playing around with some OpenGL codes and test the capabilities and results.
</p>
<p>
So, I was testing some texturing and of course wanted to test the mipmapping possibilities. Here I tripped over some &#8220;problems&#8221; you have to avoid by going another way.<br />
In a probably classical way you could use <em>glu</em> to build the <a href="http://en.wikipedia.org/wiki/Mipmap">mipmaps</a>
</p>
<pre class="brush: java; title: ; notranslate">
gluBuild2DMipmaps(GL_TEXTURE_2D, 3, sizeX, sizeY, GL_RGB, GL_UNSIGNED_BYTE, data);
</pre>
<p>
You hand it the width and height and the texture data and it creates all your needed mipmaps. Problem solved! Unfortunately Android only provides a very small <a href="http://developer.android.com/reference/android/opengl/GLU.html">GLU</a> implementation with some helper functions and does not provide you that functionality. Therefore, you would have to go another way.<br />
A possibility on an Android system could be to test for <a href="http://developer.android.com/reference/javax/microedition/khronos/opengles/GL11.html">version 1.1 of your GL</a> and use the according hint, as 1.1. adds automatic mipmap generation
</p>
<pre class="brush: java; title: ; notranslate">
gl.glBindTexture(GL10.GL_TEXTURE_2D, textureID);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR_MIPMAP_NEAREST);
if(gl instanceof GL11) {
  gl.glTexParameterf(GL11.GL_TEXTURE_2D, GL11.GL_GENERATE_MIPMAP, GL11.GL_TRUE);
  GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
} else {
  buildMipmap(gl, bitmap);
}
</pre>
<p>
For the emulator this might work but not every mobile device running Android must have a GL11 instance. Therefore, this solution can work but does not necessarily have to.<br />
Mike Miller had a similar problem/idea and suggested the following functionality
</p>
<pre class="brush: java; title: ; notranslate">
private void buildMipmap(GL10 gl, Bitmap bitmap) {
  //
  int level = 0;
  //
  int height = bitmap.getHeight();
  int width = bitmap.getWidth();

  //
  int[] textures = new int[1];
  gl.glGenTextures(1, textures, 0);
  textureID = textures[0];

  //
  gl.glBindTexture(GL10.GL_TEXTURE_2D, textureId);

  //
  while(height &gt;= 1 || width &gt;= 1) {
    //First of all, generate the texture from our bitmap and set it to the according level
    GLUtils.texImage2D(GL10.GL_TEXTURE_2D, level, bitmap, 0);

    //
    if(height == 1 || width == 1) {
      break;
    }

    //Increase the mipmap level
    level++;

    //
    height /= 2;
    width /= 2;
    Bitmap bitmap2 = Bitmap.createScaledBitmap(bitmap, width, height, true);

    //Clean up
    bitmap.recycle();
    bitmap = bitmap2;
  }

  return textureID;
}
</pre>
<p>
The code is pretty straight forward. It just creates always the mottled bitmap and adds it using GLUtils to the according level, beginning from 0 to whatever level the size 1 for width or height will be.<br />
Unfortunately again, this did not work for me. I tried this in an existing code I had, a special plain project just for mipmap testing, with different AVDs and with different circumstances. Nothing worked! As soon as I added another layer the texture stayed white. I am aware that as soon as you set the texture parameters for mipmapping
</p>
<pre class="brush: java; title: ; notranslate">
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR_MIPMAP_NEAREST);
</pre>
<p>
it wants all layers. The function posted by Mike provides these but still it did not work for me. I had no idea why it did not work, because that it is a common solution, trivial and eventually should work. (Nevertheless, I solved it. Read at the end of this post why it did not work for me.)<br />
I do not remember why I tried the following, but after many hours and lost hair I replaced the line of <code>GLUtils.texImage2D(GL10.GL_TEXTURE_2D, level, bmp, 0);</code> with the following, actually resembling what GLUUtils should provide, but based on an older SDK version
</p>
<pre class="brush: java; title: ; notranslate">
ByteBuffer bytebuf = ByteBuffer.allocateDirect(bmp.getHeight() * bmp.getWidth() * 4);
bytebuf.order(ByteOrder.nativeOrder());
IntBuffer pixelbuf = bytebuf.asIntBuffer();

for(int y = 0; y &lt; bmp.getHeight(); y++)
  for(int x = 0; x &lt; bmp.getWidth(); x++) {
    pixelbuf.put(bmp.getPixel(x, y));
  }
pixelbuf.position(0);
bytebuf.position(0);

gl.glTexImage2D(GL10.GL_TEXTURE_2D, level, GL10.GL_RGBA, bmp.getWidth(), bmp.getHeight(), 0, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, pixelbuf);
</pre>
<p>
This basic self-implementation of what should be done by GLUtils works for me. But please note that it is just a working example and does is neither optimized nor necessarily the finite solution to this task.
</p>
<p>
I also figured out why the GLUtils did not work for me: It was only in the Emulator and it was only on one of my machines. And especially that machine it did not work on has a very basic, cheap, old graphics card. But for a whole overview and if you step into the same trap, I posted everything so you know what could be a reason and how to solve it.
</p>
<p>
So, now after all these tests I have done I have three implementations that work for me. On the one hand side, the use of GL11 and the self-implementation of a mipmap building method in two ways. In <strong>conclusion</strong> after all these tests and also minor performance tests I did, use one of the suggested methods from above or let me know of other possibilities. I would start with the GL11 test and then go over to the implementations. GLUtils <em>should</em> always work. Hope this helps&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://insanitydesign.com/wp/2009/08/01/android-opengl-es-mipmaps/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
	</channel>
</rss>

