<?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; opengl</title>
	<atom:link href="http://insanitydesign.com/wp/tag/opengl/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>WebGL &#8211; Part 2: In the beginning there was&#8230;</title>
		<link>http://insanitydesign.com/wp/2011/02/27/webgl-part-2-in-the-beginning-there-was/</link>
		<comments>http://insanitydesign.com/wp/2011/02/27/webgl-part-2-in-the-beginning-there-was/#comments</comments>
		<pubDate>Sun, 27 Feb 2011 10:24:39 +0000</pubDate>
		<dc:creator>NeA</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Graphics]]></category>
		<category><![CDATA[Internet]]></category>
		<category><![CDATA[WebGL]]></category>
		<category><![CDATA[altdevblogaday]]></category>
		<category><![CDATA[canvas]]></category>
		<category><![CDATA[html5]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[opengl]]></category>
		<category><![CDATA[webgl]]></category>

		<guid isPermaLink="false">http://insanitydesign.com/wp/?p=818</guid>
		<description><![CDATA[&#8230;a Triangle! In the previous part I presented WebGL, a new competitor in the graphics programming market. You got to know some history and parallel branches in the history of 3D graphics in the browser environment. Now, in this part you will see how to get a WebGL application into your browser. The beginning Last [...]]]></description>
			<content:encoded><![CDATA[<p>
&#8230;a Triangle! In the <a href="http://insanitydesign.com/wp/2011/02/13/webgl-part-1-a-new-challenger-appears/">previous part</a> I presented WebGL, a new competitor in the graphics programming market. You got to know some history and parallel branches in the history of 3D graphics in the browser environment. Now, in this part you will see how to get a WebGL application into <strong>your</strong> browser.
</p>
<h3>The beginning</h3>
<p>
Last time was historical, this time we will get practical. In this part we will write one HTML page with embedded JavaScript code to draw a simple triangle through WebGL into an HTML5 Canvas. For that we will see how to set up a canvas, create a WebGL context, embed shaders and create a draw loop to present our triangle. Especially the shaders are an important topic regarding WebGL as WebGL is based on OpenGL ES 2.0 and OpenGL ES 2.0 does not support the fixed function transformation and fragment pipeline of OpenGL ES 1.x. Therefore, everything has to be done through shaders.
</p>
<p>
<em>Note: What I present is an overview and user guide of WebGL, not OpenGL (nor OpenGL ES Shading Language)! WebGL could be seen as a wrapper for OpenGL ES 2.0 in your browser (and in the current state programmable by JavaScript). Therefore, I will not explain every OpenGL command or speciality. What is pointed out is just how to get your OpenGL knowledge into your browser. General programming knowledge and some HTML/JavaScript expertise may help. If you need help with OpenGL please refer to the masses of great tutorials in the net, especially <a href="http://nehe.gamedev.net/">NeHe</a> and the <a href="http://opengles-book.com/">OpenGL ES 2.0 Programming Guide</a> (the main inspiration for this)</em>.
</p>
<h3>The Page</h3>
<p>
First of all we will set up our frame, our basic HTML file which we will use throughout this post. To write and develop HTML, JavaScript and therefore WebGL yourself you are pretty open to what to use. You can use a normal text editor or rely on a WebDevelopment environment but there are no distinct WebGL IDEs.<br />
For WebDevelopment you can use <a href="http://www.aptana.com/">Aptana</a> or the direct <a href="http://eclipse.org/downloads/packages/eclipse-ide-javascript-web-developers/heliossr2">Eclipse JavaScript Development Branch IDE</a>. Both are fully fledged IDEs with great support, refactoring, auto-completion etc. Always a good simple helper is also <a href="http://notepad-plus-plus.org/">Notepad++</a>.<br />
Everything shown here has been developed in Eclipse, Notepad++ and tested in <a href="http://www.google.com/chrome">Chrome 9</a>. Chrome 9 is currently the only release browser download with enabled WebGL and has very good Developer Tools. Both is recommended for JavaScript development as debugging is kind of a pain in the a** (we will come to this in later parts).
</p>
<p>
If you have everything ready, have a look at the initial HTML scaffold:
</p>
<div class="geshi no html">
<ol>
<li class="li1">
<div class="de1">&lt;!DOCTYPE html&gt;
</div>
</li>
<li class="li1">
<div class="de1">&lt;html&gt;
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;&lt;head&gt;
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=UTF-8&quot; /&gt;
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &lt;meta name=&quot;keywords&quot; content=&quot;altdev,webgl,insanitydesign&quot; /&gt;
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &lt;meta name=&quot;author&quot; content=&quot;INsanityDesign&quot; /&gt;
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &lt;title&gt;WebGL &#8211; Part 2: In the beginning there was&#8230;&lt;/title&gt;
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &lt;style type=&quot;text/css&quot;&gt;
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;#canvas {
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; width: 640px;
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; height: 480px;
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;}
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &lt;/style&gt;
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &lt;script type=&quot;text/javascript&quot; src=&quot;webgl-utils.js&quot; /&gt;
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &lt;script type=&quot;text/javascript&quot;&gt;
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;&#8230;
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &lt;/script&gt;
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;&lt;/head&gt;
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;&lt;body&gt;
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &lt;canvas id=&quot;canvas&quot;&gt;
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &lt;/canvas&gt;
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;&lt;/body&gt;
</div>
</li>
<li class="li1">
<div class="de1">&lt;/html&gt;</div>
</li>
</ol>
</div>
<p>
This is the scaffold we will use throughout this part to show how to embed WebGL in a HTML file. You can copy it and save it as e.g. an <em>webgl-part2.html</em> file to open it with your WebGL enabled browser. At the moment, there is nothing to see or do. We will consequently fill it up and start using WebGL to draw our triangle.
</p>
<p>
One thing you might notice immediately if you have seen or written HTML pages before is the first line: &lt;!DOCTYPE html&gt;! This is the reduced Doctype used to init a HTML 5 page. You no longer have to write these long <em>Doctype XHTML 1.0 transitional</em> etc. Just that! Afterwards comes pretty general HTML stuff and nothing very special for our test.<br />
Between lines 11 and 14 we define the width and height of our canvas we want to draw in through <a href="http://en.wikipedia.org/wiki/CSS">CSS</a>. For now and the following we will stick with a classic 640&#215;480. There are several ways to define the width and height of the canvas. As it is an HTML entity you can use everything that HTML allows. Regarding the WebGL drawing viewport we can refer to this size later on, increase or decrease the <em>glViewport</em>.<br />
At line 17 you can see how to embed external JavaScript files into the page. We already embed one <em>.js</em> file in this case: webgl-utils.js! These utils provide some common methods for creating a WebGL context on a canvas. These are cross-browser compatible and actively maintained at Khronos, therefore it is unnecessary to rewrite these by yourself. You can download the original file from the <a href="https://cvs.khronos.org/svn/repos/registry/trunk/public/webgl/sdk/demos/common/webgl-utils.js">Khronos CVS</a>. Download and save the file at the same location where you put your HTML file. I will rely on these in this and the following parts.<br />
The lines 25 and 26 define the initial canvas which we will use to draw in. It is just a simple HTML entity. We identify it by the <em>id</em> attribute as &#8220;canvas&#8221; but the naming is up to you (but you would have change other occurrences).
</p>
<p>
Now, the interesting things will happen in between line 19 and 21. This is where the magic will occur. In this showcase we will fill it part by part to get our triangle in the browser. Every code shown in the following should be copied in between here (or download the full file at the end).
</p>
<h3>Global Definition</h3>
<p>
At first, we will add some global variables that we will use in the following WebGL application. In addition, we define our triangle vertices and the mandatory fragment and vertex shader (add this where the dots <em>&#8230;</em> are).
</p>
<div class="geshi no javascript">
<ol>
<li class="li1">
<div class="de1"><span class="sy0">&lt;</span>script type=<span class="st0">&quot;text/javascript&quot;</span><span class="sy0">&gt;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="co1">//Some global variables</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw2">var</span> gl;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw2">var</span> width;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw2">var</span> height;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw2">var</span> canvas;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="co1">//Our triangle</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw2">var</span> vertsTriangle = <span class="kw2">new</span> Float32Array<span class="br0">&#40;</span><span class="br0">&#91;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="nu0">0.0</span>, <span class="nu0">0.7</span>, <span class="nu0">0.0</span>, <span class="co1">//Top</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="nu0">-0.7</span>, <span class="nu0">-0.7</span>, <span class="nu0">0.0</span>,<span class="co1">//Bottom Left</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="nu0">0.7</span>, <span class="nu0">-0.7</span>, <span class="nu0">0.0</span> &nbsp;<span class="co1">//Bottom Right</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#93;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw2">var</span> vertsTriangleBuffer;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="co1">//Fragment Shader</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw2">var</span> shaderFS = <span class="st0">&quot;precision highp float;<span class="es0">\n</span><span class="es0">\</span></span></div>
</li>
<li class="li1">
<div class="de1"><span class="st0"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;void main() {<span class="es0">\n</span><span class="es0">\</span></span></div>
</li>
<li class="li1">
<div class="de1"><span class="st0"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;gl_FragColor = vec4(1.0, 1.0, 0.2, 1.0);<span class="es0">\n</span><span class="es0">\</span></span></div>
</li>
<li class="li1">
<div class="de1"><span class="st0"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}<span class="es0">\n</span>&quot;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="co1">//Vertex Shader &nbsp; &nbsp; </span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw2">var</span> shaderVS = <span class="st0">&quot;attribute vec4 position;<span class="es0">\n</span><span class="es0">\</span></span></div>
</li>
<li class="li1">
<div class="de1"><span class="st0"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;void main() {<span class="es0">\n</span><span class="es0">\</span></span></div>
</li>
<li class="li1">
<div class="de1"><span class="st0"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;gl_Position = position;<span class="es0">\n</span><span class="es0">\</span></span></div>
</li>
<li class="li1">
<div class="de1"><span class="st0"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}<span class="es0">\n</span>&quot;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="co1">//The shader program</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw2">var</span> shaderProgram;</div>
</li>
</ol>
</div>
<p>
We define handles for the created GL context we want to re-use throughout this page as well as the width and height of the canvas into which we want to draw. Do not forget the canvas itself, of course. Afterwards we set up the three vertices of our triangle in <em>vertsTriangle</em>.<br />
You may have noticed that we do not type the <em>width/height</em> or <em>gl</em> handle. All variables are just noted as (mutable) <em>var</em>&#8216;s. As JavaScript is a dynamically typed language this is fine. The definition happens as soon as it assigned. But it is never strongly typed for dynamic variables and therefore can rely on different sets of types. Nevertheless, you can also type your variables.<br />
As JavaScript is required to be more and more with high performance inside your browser an initiative started to define some specific arrays to speed up the applications. These <a href="https://developer.mozilla.org/en/JavaScript_typed_arrays">Typed Arrays</a> such as the used <em>Float32Array</em> are still in a draft state as well as WebGL but will be released and supported probably in parallel to it.
</p>
<p>
After the vertices array you can see the fragment and the vertex shader code. Here, it is assigned as String to a JavaScript variable. If you would copy these and remove the special symbols you will get a normal shader as you might know. Here, we explicitly define the end of each line through <em>\n\</em>. This looks odd but is required to compile it later on as the shader source is required to be an array. Therefore, you could also write:
</p>
<div class="geshi no javascript">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">var</span> shaderFS = <span class="br0">&#91;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="st0">&quot;precision highp float;&quot;</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="st0">&quot;void main() {&quot;</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="st0">&quot;gl_FragColor = vec4(1.0, 1.0, 0.2, 1.0);&quot;</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="st0">&quot;}&quot;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#93;</span>;</div>
</li>
</ol>
</div>
<p>
Both things would work. It may seem pretty odd to write everything directly into these arrays/variables but for now it&#8217;s enough as we only need one shader each. In a ladder part we will see how to load/reload specific shaders into your application.<br />
The last thing we already pre-define is a variable for our later assigned shader program.
</p>
<h3>Entry point</h3>
<p>
JavaScript itself and in its browser environment have no specific entry point such as a classic main method. Therefore, we have to create an entry point ourself based on the principles of the <a href="http://en.wikipedia.org/wiki/Document_Object_Model">DOM</a> loading of the WebPage. As we want to start our WebGL application as soon as the actual body of the page (with our canvas) has been loaded, we add a trigger to the body itself. The trigger is docked onto the <em>onLoad</em> delegate of the body:
</p>
<div class="geshi no javascript">
<ol>
<li class="li1">
<div class="de1"><span class="sy0">&lt;</span>body <span class="kw3">onload</span>=<span class="st0">&quot;main();&quot;</span><span class="sy0">&gt;</span></div>
</li>
</ol>
</div>
<p>
By that we listen for the body to be loaded (the canvas has to be created in the DOM up to that) and fire our main() method where we start with our part of the application:
</p>
<div class="geshi no javascript">
<ol>
<li class="li1">
<div class="de1"><span class="coMULTI">/**</span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">&nbsp;* The main entry point</span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">&nbsp;*/</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">function</span> main<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="co1">//</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; canvas = document.<span class="me1">getElementById</span><span class="br0">&#40;</span><span class="st0">&quot;canvas&quot;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; gl = WebGLUtils.<span class="me1">setupWebGL</span><span class="br0">&#40;</span>canvas<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="co1">//Couldn&#39;t setup GL</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">if</span><span class="br0">&#40;</span><span class="sy0">!</span>gl<span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw3">alert</span><span class="br0">&#40;</span><span class="st0">&quot;No WebGL!&quot;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">return</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="co1">//</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; width = canvas.<span class="me1">width</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; height = canvas.<span class="me1">height</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="co1">//</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">if</span><span class="br0">&#40;</span><span class="sy0">!</span>init<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw3">alert</span><span class="br0">&#40;</span><span class="st0">&quot;Could not init!&quot;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">return</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="co1">//</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; draw<span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>
In the first 8 lines we init our canvas and the WebGL context for it. First, we retrieve the canvas element from the DOM through its id <em>&#8220;canvas&#8221;</em>. Then we call a method from the downloaded WebGL Utils to create and enable the WebGL context of that canvas. Please have a look at <a href="http://insanitydesign.com/wp/2011/02/13/webgl-part-1-a-new-challenger-appears/">part 1</a> to see what is done in that method. Basically, we utilize the generalized <em>setupWebGL(canvas)</em> method to be cross-browser compatible as there is still no distinct method to init a WebGL context.<br />
Afterwards we retrieve the width and height from the canvas (as defined in the CSS) to reuse these in our <em>glViewport</em> later on. Then we fire a general init() method to setup everything WebGL we need:
</p>
<div class="geshi no javascript">
<ol>
<li class="li1">
<div class="de1"><span class="coMULTI">/**</span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">&nbsp;* Init our shaders, buffers and any additional setup</span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">&nbsp;*/</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">function</span> init<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="co1">//</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">if</span><span class="br0">&#40;</span><span class="sy0">!</span>initShaders<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw3">alert</span><span class="br0">&#40;</span><span class="st0">&quot;Could not init shaders!&quot;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">return</span> <span class="kw2">false</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="co1">//</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">if</span><span class="br0">&#40;</span><span class="sy0">!</span>initBuffers<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw3">alert</span><span class="br0">&#40;</span><span class="st0">&quot;Could not init buffers!&quot;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">return</span> <span class="kw2">false</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="co1">//</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; gl.<span class="me1">clearColor</span><span class="br0">&#40;</span><span class="nu0">0.0</span>, <span class="nu0">0.0</span>, <span class="nu0">0.0</span>, <span class="nu0">1.0</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; gl.<span class="me1">viewport</span><span class="br0">&#40;</span><span class="nu0">0</span>, <span class="nu0">0</span>, width, height<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; gl.<span class="me1">clearDepth</span><span class="br0">&#40;</span><span class="nu0">1.0</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="co1">//</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">return</span> <span class="kw2">true</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>
In general, the init method follows three steps:</p>
<ul>
<li>Init our shaders</li>
<li>Init our buffers (our triangle buffer)</li>
<li>Set up the clear color, depth and our viewport</li>
</ul>
<p>Step 3 should be self-explanatory, therefore we will continue with loading and setting up our shaders.
</p>
<h3>Load the Shaders</h3>
<p>
You already saw our two shader variables. Now, we want to compile, attach and link these. For that we require two methods: <em>initShaders()</em> and <em>createShader()</em>.
</p>
<div class="geshi no javascript">
<ol>
<li class="li1">
<div class="de1"><span class="coMULTI">/**</span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">&nbsp;* Init our shaders, load them, create the program and attach them</span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">&nbsp;*/</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">function</span> initShaders<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="co1">//</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw2">var</span> fragmentShader = createShader<span class="br0">&#40;</span>gl.<span class="me1">FRAGMENT_SHADER</span>, shaderFS<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw2">var</span> vertexShader = createShader<span class="br0">&#40;</span>gl.<span class="me1">VERTEX_SHADER</span>, shaderVS<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="co1">//</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; shaderProgram = gl.<span class="me1">createProgram</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">if</span><span class="br0">&#40;</span>shaderProgram == <span class="kw2">null</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw3">alert</span><span class="br0">&#40;</span><span class="st0">&quot;No Shader Program!&quot;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">return</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="co1">//</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; gl.<span class="me1">attachShader</span><span class="br0">&#40;</span>shaderProgram, vertexShader<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; gl.<span class="me1">attachShader</span><span class="br0">&#40;</span>shaderProgram, fragmentShader<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; gl.<span class="me1">linkProgram</span><span class="br0">&#40;</span>shaderProgram<span class="br0">&#41;</span>; &nbsp; &nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="co1">//</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">if</span><span class="br0">&#40;</span><span class="sy0">!</span>gl.<span class="me1">getProgramParameter</span><span class="br0">&#40;</span>shaderProgram, gl.<span class="me1">LINK_STATUS</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw3">alert</span><span class="br0">&#40;</span><span class="st0">&quot;Could not link shader!&quot;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; gl.<span class="me1">deleteProgram</span><span class="br0">&#40;</span>shaderProgram<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">return</span> <span class="kw2">false</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="co1">//</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; gl.<span class="me1">useProgram</span><span class="br0">&#40;</span>shaderProgram<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="co1">//</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; shaderProgram.<span class="me1">position</span> = gl.<span class="me1">getAttribLocation</span><span class="br0">&#40;</span>shaderProgram, <span class="st0">&quot;position&quot;</span><span class="br0">&#41;</span>; &nbsp; &nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; gl.<span class="me1">enableVertexAttribArray</span><span class="br0">&#40;</span>shaderProgram.<span class="me1">position</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">return</span> <span class="kw2">true</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">/**</span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">&nbsp;*</span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">&nbsp;*/</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">function</span> createShader<span class="br0">&#40;</span>shaderType, shaderSource<span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="co1">//Create a shader</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw2">var</span> shader = gl.<span class="me1">createShader</span><span class="br0">&#40;</span>shaderType<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="co1">//</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">if</span><span class="br0">&#40;</span>shader == <span class="kw2">null</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">return</span> <span class="kw2">null</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="co1">//</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; gl.<span class="me1">shaderSource</span><span class="br0">&#40;</span>shader, shaderSource<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; gl.<span class="me1">compileShader</span><span class="br0">&#40;</span>shader<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="co1">//</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">if</span><span class="br0">&#40;</span><span class="sy0">!</span>gl.<span class="me1">getShaderParameter</span><span class="br0">&#40;</span>shader, gl.<span class="me1">COMPILE_STATUS</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw3">alert</span><span class="br0">&#40;</span><span class="st0">&quot;Could not compile shader!&quot;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; gl.<span class="me1">deleteShader</span><span class="br0">&#40;</span>shader<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">return</span> <span class="kw2">null</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="co1">//</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">return</span> shader;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>
At the beginning of <em>initShaders</em> we call our <em>createShader</em> method with the type of the shader and the according shader source variable. The called method createShader does exactly what the name suggests: It creates a shader based on the given type, compiles the given source and returns the created <em>glShader</em>.<br />
Actually nothing special here as it is just a helper method. If we would hand over the shaderSource in a different format we will probably extend this method to convert the given shader source to a common format. But for now this is fine.<br />
One speciality you can find is the error handling: Throughout the page you can find alerts and empty returns. As we have no specific exit() call in JavaScript or HTML (there is a possibility, but we will not use it later on) we alert the user, firing a MessageBox and empty return. As catched in higher instances this will result in a jump out of the application which leads to a manually &#8220;stopped&#8221; application. As we entered by ourself through the main() method, we can jump out any time we want. An alert() may not be the nicest way to show errors and exit the application but it at least gives feedback. Later we will see how to combine HTML entities, CSS and WebGL and will use the normal HTML functionality to fire error messages and according to the error lead the user to a solution or mail these errors to the developer.
</p>
<p>
After we loaded and created our shader we start creating our program at line 10. Again we check for an error that may have occurred. If no error happened we attach both shader objects, link the program and if nothing happened, use it.<br />
If you know OpenGL you might say: Still nothing that special here!&#8230; and this is intended. WebGL was intended as a low-level programming opportunity without too many specialities away from the original uses of OpenGL. This was done with VRML and many other special plug-ins but never lead to what was expected as commonly used 3D in the browser.
</p>
<p>
Nevertheless, one speciality about JavaScript is being used now: The dynamic prototyping. You can see at the end that we return &#8220;the index of the generic vertex attribute that is bound to that attribute variable&#8221; and we assign it to the <em>shaderProgram</em> as <em>position</em>.
</p>
<div class="geshi no javascript">
<ol>
<li class="li1">
<div class="de1">shaderProgram.<span class="me1">position</span> = gl.<span class="me1">getAttribLocation</span><span class="br0">&#40;</span>shaderProgram, <span class="st0">&quot;position&quot;</span><span class="br0">&#41;</span>; &nbsp; &nbsp;</div>
</li>
<li class="li1">
<div class="de1">gl.<span class="me1">enableVertexAttribArray</span><span class="br0">&#40;</span>shaderProgram.<span class="me1">position</span><span class="br0">&#41;</span>;</div>
</li>
</ol>
</div>
<p>
This is possible even if we haven&#8217;t predefined that variable in the shaderProgram object. We dynamically extend and create a new prototype by assigning the position without prior definition.<br />
This should never be used as <em>best practice</em> as there is no contract we can rely on, e.g. through a specifically defined class, structure or interface. In this simple case as we do not need reusable and generic objects we just assign the handle for later reuse in our own &#8220;knowledge space&#8221;. Regarding classes and interfaces for contracts, these are all possible in JavaScript and we will come to that in a later part.
</p>
<p>
But now our shaders are (hopefully) all loaded and assigned so that we can continue setting up our triangle vertices buffer.
</p>
<h3>Setup the Buffer</h3>
<p>
To set up the buffer based on our vertices we go straight forward:
</p>
<div class="geshi no javascript">
<ol>
<li class="li1">
<div class="de1"><span class="coMULTI">/**</span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">&nbsp;* Init our required buffers (in our case for our triangle)</span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">&nbsp;*/</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">function</span> initBuffers<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="co1">//</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; vertsTriangleBuffer = gl.<span class="me1">createBuffer</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; gl.<span class="me1">bindBuffer</span><span class="br0">&#40;</span>gl.<span class="me1">ARRAY_BUFFER</span>, vertsTriangleBuffer<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; gl.<span class="me1">bufferData</span><span class="br0">&#40;</span>gl.<span class="me1">ARRAY_BUFFER</span>, vertsTriangle, gl.<span class="me1">STATIC_DRAW</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="co1">//</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">return</span> <span class="kw2">true</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>
Absolutely nothing special here! We create the buffer and assign it to our global variable, bind and set the according data. This will become more complicated in later parts, when we define structures, load models etc. But as we just want to draw one triangle this is enough.
</p>
<h3>Let&#8217;s draw</h3>
<p>
What we really want to do after all this work now is to draw. Therefore, we define our draw() method:
</p>
<div class="geshi no javascript">
<ol>
<li class="li1">
<div class="de1"><span class="coMULTI">/**</span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">&nbsp;* Our draw/render main loop method</span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">&nbsp;*/</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">function</span> draw<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span> </div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="co1">// request render to be called for the next frame.</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; window.<span class="me1">requestAnimFrame</span><span class="br0">&#40;</span>draw, canvas<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="co1">//</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; gl.<span class="me1">clear</span><span class="br0">&#40;</span>gl.<span class="me1">COLOR_BUFFER_BIT</span> <span class="sy0">|</span> gl.<span class="me1">GL_DEPTH_BUFFER_BIT</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="co1">//</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; gl.<span class="me1">bindBuffer</span><span class="br0">&#40;</span>gl.<span class="me1">ARRAY_BUFFER</span>, vertsTriangleBuffer<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; gl.<span class="me1">vertexAttribPointer</span><span class="br0">&#40;</span>shaderProgram.<span class="me1">position</span>, <span class="nu0">3</span>, gl.<span class="me1">FLOAT</span>, <span class="kw2">false</span>, <span class="nu0">0</span>, <span class="nu0">0</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="co1">//</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; gl.<span class="me1">drawArrays</span><span class="br0">&#40;</span>gl.<span class="me1">TRIANGLES</span>, <span class="nu0">0</span>, <span class="nu0">3</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>
As you can see no immediate drawing here for this very simple example. This again relates to WebGL and its origin OpenGL ES 2.0: Besides no fixed-function pipeline there is no immediate drawing. We just clear, bind our buffer and draw!<br />
But wait, how do we draw? I said we have no specific entry point and created one ourself. If we have no specific entry point we probably also have no specific <em>draw() loop</em> we can rely on. Again, we have to create on our own. Here again we will rely on a method from the WebGL Utils and the according best practice from <a href="https://www.khronos.org/webgl/wiki/FAQ#What_is_the_recommended_way_to_implement_a_rendering_loop.3F">Khronos</a>.
</p>
<p>
In the end of our main method, after all initiations we call our draw() method once:
</p>
<div class="geshi no javascript">
<ol>
<li class="li1">
<div class="de1">&nbsp; <span class="co1">//</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; draw<span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>
After the initial call, in the first line of the draw method we call up a utils method: <em>requestAnimFrame(draw, canvas)</em>! This cross-browser compatible method takes the canvas in which to draw and the according method as callback. Inside is a timeout that will recall and keep the loop running. Currently this is the recommended way to do a loop. But it has to be noted that this not final. You can see the drawbacks immediately in the timeout and in the call directly in the draw method. For now until we see a final call and the draft has been approved we will stick to that as it is nevertheless cross-browser compatible and works fine.
</p>
<p>
Now, if you copied and setup everything correctly you should see the following: <a href="http://insanitydesign.com/wp/wp-content/uploads/webgl-part2-triangle.png"><img src="http://insanitydesign.com/wp/wp-content/uploads/webgl-part2-triangle-300x224.png" alt="" title="WebGL - Part 2: Triangle" width="300" height="224" class="aligncenter size-medium wp-image-855" /></a>
</p>
<p>
<em>You can download the full source <a href='http://insanitydesign.com/wp/wp-content/uploads/webgl-part2.html'>here</a>. If you downloaded the <a href="https://cvs.khronos.org/svn/repos/registry/trunk/public/webgl/sdk/demos/common/webgl-utils.js">webgl-utils.js</a> put it at the same location as the HTML file.</em>
</p>
<p>
This is it&#8230; at least for now. There are still some &#8220;not so ideal&#8221; things here as this is neither best practice nor the only way to program JavaScript and therefore WebGL. Everything is embedded directly into the HTML page, everything is handled as one. The shaders are odd string variables and the process is very focussed around &#8220;just work&#8221;. But this was intended for now. Now, you have a page you can use as an initial foundation for further experiments. For example, try fooling around with the shaders and see what happens.
</p>
<p>
Next time we will get into User Input and show how to make the triangle move to the rhythm of our key strokes (and build up our own matrix). In the following parts afterwards I will present how to load textures, load models and operate with several shader files, dynamic loading and obfuscated JavaScript.
</p>
<p>
<em>Written for <a href="http://altdevblogaday.org/">#AltDevBlogADay</a></em></p>
]]></content:encoded>
			<wfw:commentRss>http://insanitydesign.com/wp/2011/02/27/webgl-part-2-in-the-beginning-there-was/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>WebGL &#8211; Part 1: A new challenger appears&#8230;</title>
		<link>http://insanitydesign.com/wp/2011/02/13/webgl-part-1-a-new-challenger-appears/</link>
		<comments>http://insanitydesign.com/wp/2011/02/13/webgl-part-1-a-new-challenger-appears/#comments</comments>
		<pubDate>Sun, 13 Feb 2011 22:51:53 +0000</pubDate>
		<dc:creator>NeA</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Graphics]]></category>
		<category><![CDATA[Internet]]></category>
		<category><![CDATA[WebGL]]></category>
		<category><![CDATA[altdevblogaday]]></category>
		<category><![CDATA[canvas]]></category>
		<category><![CDATA[html5]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[opengl]]></category>
		<category><![CDATA[vrml]]></category>
		<category><![CDATA[webgl]]></category>

		<guid isPermaLink="false">http://insanitydesign.com/wp/?p=719</guid>
		<description><![CDATA[As I did not want to be as somewhat polemic as last time I checked several different pragmatic ideas I could post about. I weighted each against the other but with the news that the final draft of WebGL was released I decided to go this route and present some information, basics and a furthermore [...]]]></description>
			<content:encoded><![CDATA[<p>
As I did not want to be as somewhat polemic as <a href="http://altdevblogaday.org/there-are-no-casual-games">last time</a> I checked several different pragmatic ideas I could post about. I weighted each against the other but with the news that the <a href="https://www.khronos.org/registry/webgl/specs/latest/">final draft</a> of WebGL was released I decided to go this route and present some information, basics and a furthermore small tutorial in the following parts about WebGL!
</p>
<h3>A new challenger appears</h3>
<p>
Twelve years after HTML4 and eleven years after XHTML1.0 the <a href="http://www.w3.org/">WorldWideWeb Consortium</a> &#8220;finally&#8221; dropped all efforts to standardize another, a different and more XML-based standard for the Web and continued where they left off to extend HTML4 to built it up to the requirements of our current time. As we are easily able to define that our current web-browsing habits are influenced by multimedia-based content with <a href="http://www.youtube.com/">YouTube</a>, <a href="http://www.facebook.com">Facebook</a>, <a href="http://www.farmville.com/">Farmville</a>, Podcasts, &#8230; the new standard would have to be more multimedia flexible. And W3C listened!
</p>
<p>
The consequent successor <a href="http://dev.w3.org/html5/spec/Overview.html">HTML5</a> was finally started in 2008. Now, <strong>only</strong> three years later the standard is seeing the finishing line and most released drafts have already been implemented in common browsers such as <a href="http://www.mozilla.com/en-US/firefox/">Firefox</a>, <a href="http://www.google.com/chrome">Chrome</a> or <a href="http://www.apple.com/safari/">Safari</a> through WebKit. It provides special video, audio and canvas elements, support for codecs such as <a href="http://www.theora.org/">OGG Theora</a>/Vorbis and MP4 (still a little dependent on decisions and implementation as it is still not final, possibly <a href="http://www.webmproject.org/">WebM</a>), semantic support based on RDFa, sectioning etc. and a native 3D API named WebGL!
</p>
<p><a href="http://insanitydesign.com/wp/wp-content/uploads/WebGL-Logo.png" target="_blank"><img src="http://insanitydesign.com/wp/wp-content/uploads/WebGL-Logo.png" alt="" title="WebGL" width="156" height="72" style="width: 156px; border: 0; text-decoration: none; padding-top: 10px;" align="right" class="alignright size-full wp-image-787" /></a>WebGL is a specification, independent from HTML5 but depending on the <a href="http://en.wikipedia.org/wiki/Canvas_element">Canvas element</a> of the standard. It is implemented as a context of the canvas and can be programmed (for now, actually more will be possible) through JavaScript and shaders. Speed and some specifics are dependent on the actual implementation i.e. the Browser but in general interchangeable in-between different Webengines.
</p>
<p>
Therefore, WebGL offers native in-browser support for 3D development. But actually the idea of 3D in your Browser is not a new challenge for our beloved internet&#8230;
</p>
<h3>3D on the Web</h3>
<p>
Already in the early days of the WorldWideWeb several different developers, companies and initiatives tried to establish 3D in the browser to provide a more persuasive environment for the user, correlating what was currently being established in games, that became more and more 3D focused during these years.<br />
Already released in 1995 the <a href="http://web3d.org/x3d/specifications/#vrml97">Virtual Reality Modeling Language</a> (VRML) tried to bring 3D to the Web. VRML 1.0 allowed to define the geometry, textures, materials etc. and especially scripts to visualize a 3D scene in a common textfile format. This scene was additionally able to retrieve additional data from different URLs as well as to react to events and triggers through special Java/JavaScript programs in its <em>script</em> nodes. Later it was extended to allow animations, sounds and other multimedia assets in 1997 with the VRML 2.0 standard.<br />
The problem with VRML was mainly that it required a plugin to be installed in your browser as well as much processing power. Most plugins weren&#8217;t optimized and the scenes described in a very high-level style required too much load. There were some initiatives but besides some world viewers and websites of authorities no one really jumped onto VRML.
</p>
<p>
In 2001 the <a href="http://www.web3d.org">Web3D Consortium</a> built a new high-level abstraction for 3D in the Browser: X3D. X3D extended VRML with more standards and especially profiles, shaders, gis and networking. It still requires a <a href="http://www.bitmanagement.com/en/products/interactive-3d-clients/bs-contact">plugin</a> or player and was therefore not as perceived as wished for.
</p>
<p>
As WebGL is more established and well-supported already by nearly every big browser on the market the Web3D initiative now jumps onto that train without giving up VRML and X3D. With <a href="http://www.x3dom.org/">X3DOM</a> the initial specification and implementation is now being made available on your browser without the need of plugin a but with the power of WebGL and Canvas.
</p>
<p>
Nevertheless, in this multi-part series I will only focus on WebGL directly. If you want to know more about these &#8220;historic&#8221; approaches just follow the given links.
</p>
<h3>OpenGL in the Browser</h3>
<p>
In contrast to VRML or X3DOM <a href="https://www.khronos.org/registry/webgl/specs/latest/">WebGL</a> follows a more direct (&#8220;low-level&#8221; in comparison) approach to 3D in the Browser. As the name suggests WebGL is based on OpenGL and specified by <a href="https://www.khronos.org/">The Khronos Group</a>. It is based on OpenGL ES 2.0 (nowadays good known from iOS and Android) and supports GLSL in version <a href="http://www.khronos.org/registry/gles/specs/2.0/GLSL_ES_Specification_1.0.17.pdf">ES1.0</a>. It docks onto the HTML5 <em>canvas</em> as a drawing context and with that it is fully integrated in the Document Object Model (DOM) and therefore allows full interaction with the general HTML Layout and Objects (e.g. overlaying, CSS styles, &#8230;).
</p>
<p>
As mentioned, as well as OpenGL WebGL is low-level and fully based around shaders. Therefore, things like defining simple geometry and scenes in an easy XML format as it is possible with X3D is not part of the WebGL standard itself. Possible implementations and APIs on top are nevertheless feasible (and already developed to some degree).
</p>
<p>
You can nearly cope everything you know about <a href="http://en.wikipedia.org/wiki/OpenGL_ES">OpenGL ES 2.0</a> into WebGL. But there are some limitations (besides what is currently still different in-between the different Browser implementations). One important difference is that WebGL has no support for non-power-of-two textures (you could disable mipmapping). You have to resize dynamically. But in general you can feel with WebGL as you do with ES2.0 in other programming languages. It even looks similar. Let&#8217;s have a look at an exemplary draw() method (taken from the <a href="http://www.amazon.com/OpenGL-ES-2-0-Programming-Guide/dp/0321502795/ref=sr_1_1?ie=UTF8&#038;qid=1297612469&#038;sr=8-1">OpenGL ES 2.0 Programming Guide</a>):
</p>
<div class="geshi no javascript">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">function</span> Draw<span class="br0">&#40;</span>esContext<span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="kw2">var</span> userData = esContext.<span class="me1">userData</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="co1">// Set the viewport</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;gl.<span class="me1">viewport</span><span class="br0">&#40;</span> <span class="nu0">0</span>, <span class="nu0">0</span>, esContext.<span class="me1">width</span>, esContext.<span class="me1">height</span> <span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="co1">// Clear the color buffer</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;gl.<span class="me1">clear</span><span class="br0">&#40;</span> gl.<span class="me1">COLOR_BUFFER_BIT</span> <span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="co1">// Use the program object</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;gl.<span class="me1">useProgram</span><span class="br0">&#40;</span> userData.<span class="me1">programObject</span> <span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="co1">// Load the vertex position</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;gl.<span class="me1">bindBuffer</span><span class="br0">&#40;</span> gl.<span class="me1">ARRAY_BUFFER</span>, userData.<span class="me1">vertPosObject</span> <span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;gl.<span class="me1">vertexAttribPointer</span><span class="br0">&#40;</span> userData.<span class="me1">positionLoc</span>, <span class="nu0">3</span>, gl.<span class="me1">FLOAT</span>, <span class="kw2">false</span>, <span class="nu0">0</span>, <span class="nu0">0</span> <span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;gl.<span class="me1">enableVertexAttribArray</span><span class="br0">&#40;</span> userData.<span class="me1">positionLoc</span> <span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="co1">// Load the index buffer</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;gl.<span class="me1">bindBuffer</span><span class="br0">&#40;</span> gl.<span class="me1">ELEMENT_ARRAY_BUFFER</span>, userData.<span class="me1">indicesObject</span> <span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="co1">// Load the MVP matrix</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;gl.<span class="me1">uniformMatrix4fv</span><span class="br0">&#40;</span> userData.<span class="me1">mvpLoc</span>, <span class="kw2">false</span>, getAsFloat32Array<span class="br0">&#40;</span>userData.<span class="me1">mvpMatrix</span><span class="br0">&#41;</span> <span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="co1">// Draw the cube</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;gl.<span class="me1">drawElements</span><span class="br0">&#40;</span> gl.<span class="me1">TRIANGLES</span>, userData.<span class="me1">numIndices</span>, gl.<span class="me1">UNSIGNED_SHORT</span>, <span class="nu0">0</span> <span class="br0">&#41;</span>; </div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>
Looks pretty familiar, doesn&#8217;t it? A description of what this all means and how to really program it in JavaScript and embed it into a WebSite will follow in the coming parts.
</p>
<h3>Browser support</h3>
<p><strong>For the User</strong><br />
First of all you have to be able to see and test WebGL developments. Currently the easiest way to use WebGL is to install the current <a href="http://www.google.com/chrome">Chrome Browser</a>. Since version 9 it has enabled WebGL support in its stable release. If you want to use other Browsers such as Mozilla or Safari you would have to get the according nightly builds. In each case you might need to configure the according download. For a small &#8220;manual&#8221; have a look <a href="http://khronos.org/webgl/wiki/Getting_a_WebGL_Implementation">here</a> or be lazy and use Chrome.
</p>
<p><strong>For the Developer</strong><br />
If you want to develop WebGL in a supporting Browser, you check for and enable WebGL through JavaScript by actually just trying and checking for success. There are two things you have to do:</p>
<ul>
<li>Check if the browser supports WebGL in general</li>
<li>Find/Get a canvas and enable the WebGL context for it</li>
</ul>
<p>
To check if your browser or more specifically the current window you are in allows WebGL you check for a specific window flag:
</p>
<div class="geshi no javascript">
<ol>
<li class="li1">
<div class="de1"><span class="kw1">if</span><span class="br0">&#40;</span>window.<span class="me1">WebGLRenderingContext</span><span class="br0">&#41;</span> <span class="co1">//This Window supports WebGL in general!</span></div>
</li>
</ol>
</div>
<p>
If this context returns true at least the Browser does support WebGL. Now, you need to check a canvas and enable the WebGL context in that canvas. Normally, you would specify a <em>&#60;canvas&#62;</em> element and hand this to a check and enabling function:
</p>
<div class="geshi no javascript">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">function</span><span class="br0">&#40;</span>canvas<span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="co1">//Several names to check because of still diverse implementations</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw2">var</span> names = <span class="br0">&#91;</span><span class="st0">&quot;webgl&quot;</span>, <span class="st0">&quot;experimental-webgl&quot;</span>, <span class="st0">&quot;webkit-3d&quot;</span>, <span class="st0">&quot;moz-webgl&quot;</span><span class="br0">&#93;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="co1">//The context we want to fill</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw2">var</span> context = <span class="kw2">null</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="co1">//Iterate over all names and test for success</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">for</span><span class="br0">&#40;</span><span class="kw2">var</span> i = <span class="nu0">0</span>; i <span class="sy0">&lt;</span> names.<span class="me1">length</span>; ++i<span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">try</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; context = canvas.<span class="me1">getContext</span><span class="br0">&#40;</span>names<span class="br0">&#91;</span>i<span class="br0">&#93;</span><span class="br0">&#41;</span>; <span class="co1">//Optional parameters can be handed to the implementation</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span> <span class="kw1">catch</span><span class="br0">&#40;</span>e<span class="br0">&#41;</span> <span class="br0">&#123;</span><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="co1">//As soon as we got one -&gt; SUCCESS!</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span>context<span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="kw1">break</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">return</span> context;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>
If no context can be received but the <em>WebGLRenderingContext</em> says it may do WebGL, some other problems occurred. If the browser supports, you have enabled and retrieved the WebGL context and can start developing applications in your browser with fancy JavaScript.
</p>
<h3>The &#8220;new&#8221; power of JavaScript</h3>
<p>
Actually, nothing is &#8220;new&#8221; about JavaScript. The JavaScript used to first make a clock tick down on a Website, later to &#8220;do AJAX&#8221; or now to develop WebGL is pretty much all the same and based around the <a href="http://www.ecmascript.org/">ECMAScript</a> standard. Even if it contains <a href="http://www.oracle.com/technetwork/java/index.html">Java</a> in its name it has nothing to do with it. The former LiveScript is supposedly just named JavaScript to establish it with the already established Java&#8230; and probably because of a somehow Netscape/Sun partnership. But do not hesitate: The interpretation of JavaScript during runtime has been incredibly speeded up especially over the last years as more and more client programming is now part of normal websites and administrative interfaces reload and rebuild many things on-the-fly.
</p>
<p>
For JavaScript itself, it is first and foremost a prototyping programming language. This means that instead of developing a class-based architecture structure that is instanciated and dependent to the initial implementation and used throughout your development, in JavaScript you &#8220;re-use&#8221; the same classes for all instances with all given methods (called behaviours) that are added at first or also during runtime. These can be extended by the developer. Further the instances made from the structure are the states. In general you could say that prototyping is based on copying: It copies the &#8220;prototype&#8221; over and over again! Now, the most important thing about the prototype implementation of JavaScript is that it also holds a link to its &#8220;prototype&#8221; and therefore derives recursively through the prototyping hierarchy to find what we request.
</p>
<p>
A very simple example: We define a Blog!
</p>
<div class="geshi no javascript">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">var</span> personalBlog = <span class="br0">&#123;</span> title=<span class="st0">&quot;Personal Blog&quot;</span>, author=<span class="st0">&quot;Savas Ziplies&quot;</span> <span class="br0">&#125;</span>;</div>
</li>
</ol>
</div>
<p>
Now we define a second blog:
</p>
<div class="geshi no javascript">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">var</span> altDevBlog = <span class="br0">&#123;</span> title=<span class="st0">&quot;AltDev Blog&quot;</span> <span class="br0">&#125;</span>;</div>
</li>
</ol>
</div>
<p>
and assign our personalBlog as prototype:
</p>
<div class="geshi no javascript">
<ol>
<li class="li1">
<div class="de1"><span class="co1">//This does not work in all JavaScript implementation (as not all have the direct prototype to be settable), but for the example we rely on it!</span></div>
</li>
<li class="li1">
<div class="de1">altDevBlog.__proto__ = personalBlog;</div>
</li>
</ol>
</div>
<p>
Now we query
</p>
<div class="geshi no javascript">
<ol>
<li class="li1">
<div class="de1"><span class="kw3">alert</span><span class="br0">&#40;</span>altDevBlog.<span class="me1">author</span> + <span class="st0">&quot;@&quot;</span> + altDevBlog.<span class="me1">title</span><span class="br0">&#41;</span>;</div>
</li>
</ol>
</div>
<p>
and will get the following result: <em>Savas Ziplies@AltDev Blog</em>! As you can see, even if I have not defined &#8220;author&#8221; in <em>altDevBlog</em> I may still access it as I specifically set the prototype of <em>altDevBlog</em> to <em>personalBlog</em>, therefore the &#8220;author&#8221; not available is resolved at the prototype <em>personalBlog</em> and printed. This hierarchy can go on and on forever and can be a performance issue if not consciously developed against. But in general prototyping and JavaScript itself offers many patterns that go well with WebGL and designs in general.
</p>
<p>
Therefore, JavaScript depends on a little different &#8220;thinking&#8221; approach. But besides that JavaScript also provides &#8220;somehow&#8221; object-oriented principles, delegation, dynamic typing, closures, run-time evaluation (very helpful indeed) and <a href="http://www.google.com/search?q=javascript">much more</a>. Actually, JavaScript is a very powerful and somewhat interesting programming language but just seems awkward to most people because of its syntax, the name and because it is an interpreted language. Nevertheless, you can do practically everything with it, also relying on nearly every <a href="http://en.wikipedia.org/wiki/Design_pattern_(computer_science)">design pattern</a> described.
</p>
<h3>To be concluded&#8230;</h3>
<p>
As I do not want to write a book as I did last time (yeah, hard to believe ^^&#8217;), this is it for today. You now know some history, the basic standard and how to &#8220;use&#8221; WebGL applications. The next parts will feature of course further explanations of WebGL, an additional introduction to JavaScript (and some of the creepy stuff you can do with it), presenting already existing WebGL libraries and an exemplary tutorial game developed fully in WebGL (as well as some twists and tweaks around networking and dynamic loading). In addition we will try to clarify how WebGL stands against other 3D in the Browser such as Flash3D, Unity3D and Shiva3D.
</p>
<p>
To already give you a taste of what to come and what is possible with WebGL, have a look at these publicly available <a href="http://code.google.com/p/webglsamples/">WebGL Samples over at Googlecode</a>, the astonishing <a href="http://bodybrowser.googlelabs.com/body.html#">Google Body example</a> or the nice presentation of <a href="http://www.glge.org/">GLGE</a> in the video.
</p>
<p><iframe align="center" title="YouTube video player" width="600" height="380" src="http://www.youtube.com/embed/Vva36undIss" frameborder="0" allowfullscreen></iframe></p>
<p><em>Written for <a href="http://altdevblogaday.org/">#AltDevBlogADay</a></em></p>
]]></content:encoded>
			<wfw:commentRss>http://insanitydesign.com/wp/2011/02/13/webgl-part-1-a-new-challenger-appears/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>New Android OpenGL ES Port &#8211; Stencil Shadows</title>
		<link>http://insanitydesign.com/wp/2009/12/08/new-android-opengl-es-port-stencil-shadows/</link>
		<comments>http://insanitydesign.com/wp/2009/12/08/new-android-opengl-es-port-stencil-shadows/#comments</comments>
		<pubDate>Tue, 08 Dec 2009 19:24:09 +0000</pubDate>
		<dc:creator>NeA</dc:creator>
				<category><![CDATA[Android]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[apron]]></category>
		<category><![CDATA[opengl]]></category>
		<category><![CDATA[stencil shadows]]></category>

		<guid isPermaLink="false">http://insanitydesign.com/wp/?p=476</guid>
		<description><![CDATA[Greetings to all of you. Unfortunately long time no &#8220;port&#8221; see! I had a lot on my mind (something got posted beneath ^^) and a lot to do (many travels, many projects, new horizon). But now I am back in full glorious &#8220;in&#8221;sanity to enhance your (Android) imagination ^^# So, first let&#8217;s recapture some things [...]]]></description>
			<content:encoded><![CDATA[<p>
Greetings to all of you. Unfortunately long time no <em>&#8220;port&#8221;</em> see! I had a lot on my mind (something got posted beneath ^^) and a lot to do (many travels, many projects, new horizon). But now I am back in full glorious &#8220;in&#8221;sanity to enhance your (Android) imagination ^^#
</p>
<p>
So, first let&#8217;s recapture some things about my <a href="http://insanitydesign.com/wp/projects/nehe-android-ports/">NeHe Android Ports</a>: To my surprise they were accepted good and got some publicity. I am happy about that&#8230; not so much that I had too little time to continue the series like I hoped for. But now I am back and will continue to translate as many NeHe lessons as possible.
</p>
<p>
But first, I uploaded another port but no NeHe Lesson. The new port is based on code from Apron and his <a href="http://www.morrowland.com/apron/tut_gl.php">OpenGL tutorial codes</a> and deals with <a href="http://en.wikipedia.org/wiki/Stencil_shadows">Stencil Shadows</a>. This is definitely something interesting and missing besides what is covered by the NeHe tutorials so far. Therefore, I decided to do this first and then continue with other ports. So, head over to the new section of <a href="http://www.insanitydesign.com/wp/android-tutorials">general Android tutorials and ports</a> under <a href="http://insanitydesign.com/wp/projects/">Projects</a> and check out finest stencil shadows on your phone.
</p>
<p>
<em>PS:</em> I will continue with the NeHe lessons but I will also keep an eye open for other feature/functionality tutorials that could be interesting to know about and are relevant for 3D development on mobile phones.</p>
]]></content:encoded>
			<wfw:commentRss>http://insanitydesign.com/wp/2009/12/08/new-android-opengl-es-port-stencil-shadows/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Android and the (OpenGL) issues</title>
		<link>http://insanitydesign.com/wp/2009/12/06/android-and-the-opengl-issues/</link>
		<comments>http://insanitydesign.com/wp/2009/12/06/android-and-the-opengl-issues/#comments</comments>
		<pubDate>Sun, 06 Dec 2009 19:15:12 +0000</pubDate>
		<dc:creator>NeA</dc:creator>
				<category><![CDATA[Android]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Games]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[jogl]]></category>
		<category><![CDATA[lwjgl]]></category>
		<category><![CDATA[opengl]]></category>

		<guid isPermaLink="false">http://insanitydesign.com/wp/?p=487</guid>
		<description><![CDATA[As most people know I pretty much dig Android. I also have been a friend of OpenGL. As I come from the Java side (we have cookies ^^&#8217;) and there are two good wrapper implementations with JOGL and LWJGL, I found the perfect combination in OpenGL on Android, hence the tutorial ports. Over the time [...]]]></description>
			<content:encoded><![CDATA[<p>
As most people know I pretty much dig Android. I also have been a friend of OpenGL. As I come from the Java side (<a href="http://www.thinkgeek.com/tshirts-apparel/unisex/popculture/939f/">we have cookies ^^&#8217;</a>) and there are two good wrapper implementations with <a href="http://kenai.com/projects/jogl/pages/Home">JOGL</a> and <a href="http://lwjgl.org/">LWJGL</a>, I found the perfect combination in OpenGL on Android, hence the <a href="http://insanitydesign.com/wp/projects/">tutorial ports</a>.
</p>
<p>
Over the time I did some tests on Android. Not only with <a href="http://www.opengl.org/">OpenGL</a> but mainly overall to see what could work and how. Unfortunately Android is far from perfect (what ever is?) and has some issues also affecting OpenGL development on Android.
</p>
<p><strong>3D Games on Android&#8230;</strong><br />
<a href="http://insanitydesign.com/wp/wp-content/uploads/Ravensword_Town_Blacksmith.jpg" target="_blank"><img src="http://insanitydesign.com/wp/wp-content/uploads/Ravensword_Town_Blacksmith-150x150.jpg" alt="Ravensword - Town Blacksmith" title="Ravensword - Town Blacksmith" style="width: 150px; border: 0; text-decoration: none; padding-top: 5px;" align="right" class="alignright size-thumbnail wp-image-465" /></a>Games are probably the main market working with 3D on Mobile Phones nowadays. On the <a href="http://www.apple.com/iphone/">iPhone</a> we have some great examples of cool and nice looking 3D games such as <a href="http://www.ravensword.com/">Ravensword</a> and many others. They look good and also run very smoothly (especially on a 3GS, loading times are most of the times much better).<br />
Now, for Android we also have some good Game examples with the just announced winner of the <a href="http://code.google.com/android/adc/gallery_winners.html">Android Developer Challenge 2</a> in the category of games: <a href="http://www.youtube.com/watch?v=cK_d-8Cohvg&#038;feature=player_embedded">Speed Forge 3D</a>. The really beautiful <a href="http://www.mobygames.com/game/playstation/wipeout">WipeOut</a> clone gives you a nice 3D game example on Android. Another example is <a href="http://www.bendroid.com/mystique2.html">Mystique</a> by <a href="http://www.bendroid.com/">Bendroid</a>. The 3D Ego-Survival-Horror in the style of <a href="http://www.mobygames.com/game/silent-hill">Silent Hill</a> and <a href="http://www.imdb.com/title/tt0178868/">The Ring</a> is sure to give you some creeps if you play it in a dark room, at night, <a href="http://www.mobygames.com/game/dos/7th-guest">feeling lonely</a>.
</p>
<p><strong>&#8230;and the issues!</strong><br />
Now if you compare Speed Forge 3D or Mystique with Ravensword or <a href="http://www.gameloft.com/">Gameloft&#8217;s</a> upcoming <a href="http://www.youtube.com/watch?v=UGV1H_3ou7o&#038;feature=player_embedded">N.O.V.A</a> you probably will get jealous and drool all over the videos. No question: SF3D and Mystique are great, Kudos to the developers, but compared to the iPhone they are far behind.<br />
The big question should be: <strong><em>Why?</em></strong> And the answer is neither simple nor a single one in my opinion.
</p>
<p><strong>1. Heterogeneous Phone Market</strong><br />
One big thing coming up with the release of new Android handhelds is the inconsistency throughout the phones. Android itself is a specification and a referential implementation. Now, most phone manufacturer alter some things or add their own implementations of something such as OpenGL. Therefore, it is already the case that with every new release of a mobile phone based on Android you see updates of many Apps popping up because of problems or even crashes on the new device.<br />
Let&#8217;s have a look at Speed Forge 3D: I have a <a href="http://www.htc.com/uk/product/magic/overview.html">HTC Magic</a> and it runs&#8230; OK. Not absolutely fluently but playable. Now, a friend of mine has a <a href="http://www.samsung.com/uk/support/detail/supportPrdDetail.do?menu=SP01&#038;prd_ia_cd=01013000&#038;prd_mdl_cd=&#038;prd_mdl_name=GT-I7500">Samsung Galaxy</a> (or i7500) and he can barely play it until it crashes. Now the <a href="http://www.motorola.com/Consumers/US-EN/Consumer-Product-and-Services/Mobile-Phones/Motorola-DROID-US-EN">Droid</a> from Motorola entered the market with another graphics chip and a larger screen. Many new <em>&#8220;properties&#8221;</em> to test against and to deal with. The iPhone on the other side may have been updated over the time but the environment stayed nearly the same. And something that runs on the 3GS will also run on the 3G, just load a little longer. So, as time goes by more and more Android devices will come out and will further diverse the market.
</p>
<p><strong>2. The Dalvik VM</strong><br />
As always when it comes to <a href="http://java.sun.com/">Java</a> you will hear people say: </p>
<blockquote><p>It&#8217;s slow because it&#8217;s Java!<br />
Java will always be slow!<br />
Forget Java, use the <a href="http://developer.android.com/sdk/ndk/1.6_r1/index.html">NDK</a>!
</p></blockquote>
<p><em>(just saw the same <a href="http://groups.google.com/group/android-developers/browse_thread/thread/1431c67bdf33fabe">posts</a> pop up on the newsgroups and forums again)</em><br />
They will argue that a Virtual Machine can never give the speed you need (some also tend to forget that .net is a VM interpreted environment). They will argue that the Garbage Collector slows everything down. And if we look back about 10 years they really had a point, but nowadays that may not be total crap but is just not that easy to say.</p>
<p><a href="http://insanitydesign.com/wp/wp-content/uploads/proto2_04.jpg" target="_blank"><img class="shutterset_openglissues" src="http://insanitydesign.com/wp/wp-content/uploads/proto2_04-150x150.jpg" alt="Poisonville by Bigpoint" title="Poisonville by Bigpoint" style="width: 150px; border: 0; text-decoration: none; padding-top: 5px; margin: 5px;" align="left" class="alignleft size-thumbnail wp-image-465" /></a>First of all let&#8217;s have a look around at Java-based 3D games, based on the very popular, updated and fast libraries JOGL and/or LWJGL. There would be the experiment <a href="http://bytonic.de/html/jake2.html">Jake2</a>: A Quake2 clone made in Java based on JOGL and LWJGL to have a comparison. If you have a look at the <a href="http://bytonic.de/html/benchmarks.html">benchmarks</a> you will see it is hard to argue that Java is that slower over the native implementation. And engines such as <a href="http://www.jmonkeyengine.com/">jMonkeyEngine</a> support the Java faction. The demos over there such as <a href="http://www.youtube.com/watch?v=_CC7eFIzQi4">Spirits</a> and other games like <a href="http://tribaltrouble.com/">Trible Trouble</a> and <a href="http://www.poisonville.com/">Poisonville</a> on the other side show that there are very good possibilities to use this object-oriented programming language as your development base.<br />
The thing is that not Java per sÃ© is the problem but as with every programming language the code optimization and the execution environment, in this case the Virtual Machine. Even Tim Sweeney from <a href="http://www.epicgames.com/">Epic Games</a> <a href="http://www.cs.princeton.edu/~dpw/popl/06/Tim-POPL.pp">suggests</a> that functional programming and Garbage Collection is the future. The newest Sun Java VM and other VM implementations show what is possible to do with optimization within a managed environment. A good <a href="http://en.wikipedia.org/wiki/Garbage_collection_%28computer_science%29">Garbage Collector</a> (GC) and a very good <a href="http://en.wikipedia.org/wiki/Just_in_time_compiler">Just-in-Time</a> (JIT) Compiler such as in Suns current Java implementation speed up not only the application but also development itself because of less time spending for the memory management.<br />
<em><strong>But why still the speed problems?</strong></em> As already said, even if Java is interpreted and helps with the memory management you still have to think about what is happening beneath. Most applications tend to just create one instance after the other and do not really care about memory management. This may work on a desktop system but will bring problems to you on Mobile Phones. Things like weak references, caches, volatile and transient are forgotten but highly important terms in Java development. Especially Mobile Applications still have to really care about memory even with all this technically highly fledged phones. Care about the memory more than about computation on modern smartphones.</p>
<p>Nevertheless, even with these more or less <em>&#8220;simple&#8221;</em> tricks Android still has some problems that you need to remember during development. Most of these result from the <a href="http://en.wikipedia.org/wiki/Dalvik_virtual_machine">Dalvik VM</a>. The current Dalvik implementation on Android phones does not support JIT nor is the Garbage Collector any good.<br />
As no JIT is available all code is interpreted on time and not pre-compiled or anything. On a limited device such as a mobile phone this results in slowdowns. Therefore, it is important to pre-load everything you need and cache as much as you can to have a fluent gaming experience during e.g. a level.<br />
Another thing is the current GC in Dalvik: <em>Avoid it ^^&#8217;</em> Sure, you cannot really avoid the GC directly but try to not release too many objects during gameplay (or fire it by yourself) as the GC will slow your phone so down that even the OS will need time to react. This is also something that can be managed with caching and a good manual memory management. Currently, up to Android 1.6 an application may use up to 12MB of memory. With Android 2.0 this has been upped above 20MB, but as a game should support many <strong><em>platform versions</em></strong> (the issue why I am posting this), 12MB will probably be the limit for a while.
</p>
<p><strong>3. The Android OpenGL ES implementations</strong><br />
Another thing that bothers the development of <a href="http://en.wikipedia.org/wiki/OpenGL_ES">OpenGL ES</a> applications on Android is the again heterogeneous availability of specifications. Android itself by SDK supports OpenGL ES 1.0 and 1.1. The official current OpenGL ES version is already at 2.0. While 1.1 is backwards compatible to 1.0, 2.0 has <em>no</em> backwards compatibility. OK, its to note that 2.0 is somehow pretty fresh and you cannot ask for the newest technology in Mobile Phones. But most Android phones at the moment only support 1.0, for example the HTC Magic whereas the Motorola Droid should be able (hardware-based) to support 2.0.</p>
<p>As 2.0 introduced programmable shaders to the hardware the step is a large one. That is probably also the reason why OpenGL ES 2.0 is the foundation for <a href="http://en.wikipedia.org/wiki/WebGL">WebGL</a>, the upcoming and already beta-available 3D JavaScript interpreter for Browsers such as <a href="http://www.mozilla.org/firefox">Firefox</a> and the <a href="http://webkit.org/">WebKit</a> Engine. So, even if you argue that the newest technology may not be always available in Mobile Phones, as 2.0 is progressing so fast at the moment the diversity between 1.0 on current phones and 2.0 on more and more platforms can result in <em>&#8220;support&#8221;</em> or portability problems of applications.<br />
The support/portability problem is one of the issues. But more relevant is the functionality available in 1.0 and 1.1. The iPhone is based on OpenGL ES 1.1 and by that it has a big advantage over most Android phones: <a href="http://en.wikipedia.org/wiki/Vertex_Buffer_Object">Vertex Buffer Objects</a> (VBO)! As posted before my tutorial ports, OpenGL ES does not support primitive rendering (glBegin/glEnd). This is good as it will nevertheless be dropped out of the OpenGL standard. In OpenGL ES you have to use Vertex Arrays and/or Vertex Buffer Objects, which are faster anyway. <strong><em>BUT</em></strong> 1.0 only supports Vertex Arrays and no VBOs. This is one big disadvantage not only in speed but in development. You can argue that it is possible to program an application that supports both with tagging the sections but as it comes to mobile development you always try to comprehend everything as much as possible without acting on too many specifics.<br />
Two other important things when it comes to game development are that 1.0 does not support multi-texturing on its own (there are ways, but those are tricks) and it does not support the automatic mipmap generation. Both are necessities if you want to do such nice things like Ravensword or N.O.V.A.<br />
Another thing are the phone specific OpenGL ES implementations. Not only versions but also manufacturers provide different implementations with their phones. For example the Samsung Galaxy has a known very weak OpenGL implementation. There are already several published articles on how to add the HTC implementation to a rooted Galaxy. And as more and more manufactures will enter the Android market in the coming month this will probably get worse.</p>
<p><em>So, what can be concluded from this?</em> Actually not much but <em>1.0 &lt;= 1.1 &lt; 2.0</em>! There are some tricks here and there to deal with some things under 1.0 but overall you just have to accept the fact and optimize around what is missing. Or you limit your development to 1.1 (at least for now). But by that you will loose many phones such as the Magic or G1, which probably are the most widespread Android phones at the moment.
</p>
<p><strong>4. General and Specific OpenGL (ES) Paradigms and Tactics</strong><br />
As with every programming language or library there are several <a href="http://en.wikipedia.org/wiki/Best_practices">Best Practices</a> that should be remembered during development. The same thing applies to OpenGL. There are many posts in the net on how to optimize your OpenGL code and you should definitely use <a href="http://www.google.com/">Google</a> to keep you up-to-date. I just want to point out two important things (in my opinion) for mobile development.<br /> <br />
The first thing is to reduce your <em>draw calls</em>! This is not limited to mobile devices but OpenGL in general. But because of the limited hardware versus a Desktop system, this is even more important. Each draw call is not necessarily bad but in sum are slow. Now, most people do enormous, cool architectures with self-drawing objects that are instanced over and over again. This is nice from an object-oriented point of view but is bad on a mobile device. If you want to do something good and fast for Android work with overall handlers. This relates to what I said about caching. To have super classes, handlers or factories that collect all changed elements or all objects that need to be drawn and only that Object fires the draw call is the way to go. This may look like a limitation but this is not only good for the speed of your application but also reduces the possibility of asynchronous errors when instances of deep objects are computed while the rest already handles another request.<br />
The second thing correlates with what I said about memory handling and caching: Reduce your <em>texture binding calls</em>! To rebind your textures again and again and switch them back and forth is also bad for the overall performance. I know that it is tempting and Android with its nice resource loader somehow invites you to have hundreds of single texture files and load them again and again. But this will get you into trouble really fast. First of all, reduce the calls by utilizing again a super class, a texture handler with some kind of <a href="http://en.wikipedia.org/wiki/Caching">caching</a> mechanism. Another possibility is to merge several textures into one large texture and offset the texture mapping coordinates accordingly. This is easy to do and just requires a small asset pipeline but the advantage is huge.</p>
<p>Both things suggest some very important points that have to be taken into account in the architecture and early development. Therefore, always tend to fulfil the mentioned points already while you are planing.
</p>
<p><strong>5. General Mobile Development Paradigms</strong><br />
As well as for OpenGL there are some rules regarding Mobile Development in general. There are also several posts on the net about this topic but just to sum up what I already coped in the above text: <strong><em>A Mobile Device is no Desktop!</em></strong><br />
Many people come from the Desktop development and are real geniuses there. They make the best applications, fast, functionality perfect and have wonderful graphical user interfaces. BUT what works on a Desktop does not necessarily work like that on a mobile device.<br />
<a href="http://java.sun.com/javame/index.jsp">JavaME</a> made it necessary to go back to Java 1.3.1 programming style; To forget about Generics; To forget about Enumerations and just use the &#8220;old ways&#8221;. Now, with something like the Android SDK, which allows to use nearly the full Java5 specification people just copy their desktop applications into the Android projects and this will make problems. All the things I pointed out above result from that premise as the desktop is the more forgiving (because more powerful) platform.<br />
Things like creating hundreds of instances, using hundreds of different files, not working with in-memory data but reloading everything over and over again are bad&#8230; bad&#8230; and even worse, really bad! Do not get fooled by all the technical specifications of the mobile devices. You cannot compare the <em>CPU</em> and the <em>RAM</em> with the desktop pendent. I recommend to have a look &#8220;back&#8221; at the JME development and the experience people made there. Do not copy it one-to-one but use it as a guideline and convert it into a comprehensible Android project. Most problems, most errors, most failures that are posted and reported result from the unused experience made in that mobile development area. Reuse that knowledge, reuse their principles, their ideas and you will avoid many problems. And as there are enough <em>&#8220;new&#8221;</em> problems do not trip into the old ones.
</p>
<p><strong>In conclusion</strong><br />
Please do not get me wrong: My opinion about Android is high, I have one on my own and I love it. Love developing on it, love the SDK updates. The only thing I want to point out is that development on Android phones and Android itself has to start to keep these five things in mind. The next three to six month mark an important state with Android where to go and with at least the noted points you should be good to go.<br />
So, go out there and really work on it to do the best f***in&#8217; 3D Android game of them all (and comment about it here ^^&#8217;)!</p>
]]></content:encoded>
			<wfw:commentRss>http://insanitydesign.com/wp/2009/12/06/android-and-the-opengl-issues/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>The first 10 NeHe Android tutorials</title>
		<link>http://insanitydesign.com/wp/2009/08/08/the-first-10-nehe-android-tutorials/</link>
		<comments>http://insanitydesign.com/wp/2009/08/08/the-first-10-nehe-android-tutorials/#comments</comments>
		<pubDate>Sat, 08 Aug 2009 22:14:09 +0000</pubDate>
		<dc:creator>NeA</dc:creator>
				<category><![CDATA[Android]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[nehe]]></category>
		<category><![CDATA[opengl]]></category>
		<category><![CDATA[opengl es]]></category>
		<category><![CDATA[port]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://insanitydesign.com/wp/?p=447</guid>
		<description><![CDATA[Finally! This is again a short but important post. The first 10 NeHe Android ports have been completed. You can find these over at the project page. This is an important Milestone of the porting, because now you can get a complete overview of the most important things you need to build your own application. [...]]]></description>
			<content:encoded><![CDATA[<p>
Finally! This is again a short but important post. The first 10 NeHe Android ports have been completed. You can find these over at the <a href="http://insanitydesign.com/wp/projects/nehe-android-ports/">project page</a>.
</p>
<p>
This is an important Milestone of the porting, because now you can get a complete overview of the most important things you need to build your own application. I will continue with the porting. Also, I am currently looking at some other tutorials I have learned OpenGL with besides NeHe, and I will try to find time to port these too, as they focus around other specifics (e.g. Models, Animation, Camera, etc.).
</p>
<p>
So, stay tuned for more and head over to the current <a href="http://insanitydesign.com/wp/projects/nehe-android-ports/">ports</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://insanitydesign.com/wp/2009/08/08/the-first-10-nehe-android-tutorials/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Two new NeHe Android Ports</title>
		<link>http://insanitydesign.com/wp/2009/08/06/two-new-nehe-android-ports/</link>
		<comments>http://insanitydesign.com/wp/2009/08/06/two-new-nehe-android-ports/#comments</comments>
		<pubDate>Thu, 06 Aug 2009 19:20:17 +0000</pubDate>
		<dc:creator>NeA</dc:creator>
				<category><![CDATA[Android]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[blending]]></category>
		<category><![CDATA[fog]]></category>
		<category><![CDATA[nehe]]></category>
		<category><![CDATA[opengl]]></category>
		<category><![CDATA[opengl es]]></category>
		<category><![CDATA[stars]]></category>
		<category><![CDATA[tutorials]]></category>

		<guid isPermaLink="false">http://insanitydesign.com/wp/?p=431</guid>
		<description><![CDATA[Just a short notice: I added two additional NeHe Android Ports. One is Lesson 09, showing some more blending and working with many star objects. Therefore, continuing the tutorials. The other is Lesson 16. This I added as it only requires minor changes to the tutorials 07 and/or 08. This was a quick port but [...]]]></description>
			<content:encoded><![CDATA[<p>
Just a short notice: I added two additional <a href="http://insanitydesign.com/wp/projects/nehe-android-ports/">NeHe Android Ports</a>. One is Lesson 09, showing some more blending and working with many star objects. Therefore, continuing the tutorials. The other is Lesson 16. This I added as it only requires minor changes to the tutorials 07 and/or 08. This was a quick port but maybe represents a nice &#8220;<a href="http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=16">Cool Looking Fog</a>&#8221; ^^.
</p>
<p>
So, just head over to the <a href="http://insanitydesign.com/wp/projects/nehe-android-ports/">page</a> and have a look. Again, more to come&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://insanitydesign.com/wp/2009/08/06/two-new-nehe-android-ports/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>NeHe Android Ports</title>
		<link>http://insanitydesign.com/wp/2009/08/02/nehe-android-ports/</link>
		<comments>http://insanitydesign.com/wp/2009/08/02/nehe-android-ports/#comments</comments>
		<pubDate>Sun, 02 Aug 2009 21:32:14 +0000</pubDate>
		<dc:creator>NeA</dc:creator>
				<category><![CDATA[Android]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[nehe]]></category>
		<category><![CDATA[opengl]]></category>
		<category><![CDATA[opengl es]]></category>
		<category><![CDATA[tutorials]]></category>

		<guid isPermaLink="false">http://insanitydesign.com/wp/?p=401</guid>
		<description><![CDATA[As some may have noticed, I am experimenting with Android, especially with its OpenGL functionality. As I am not inexperienced with OpenGL and Java through my &#8220;playing around&#8221; with JOGL, I thought I may get a quick access to Android and OpenGL. Cannot be that hard&#8230; it is not exactly hard, but it is different. [...]]]></description>
			<content:encoded><![CDATA[<p>
<a href="http://insanitydesign.com/wp/projects/nehe-android-ports/"><img src="http://insanitydesign.com/wp/wp-content/uploads/icon.png" alt="NeHe Android Ports" title="NeHe Android Ports" style="width: 50px; border: 0; text-decoration: none; padding-top: 5px;" align="left" class="alignleft size-full wp-image-404" /></a>As some may have noticed, I am experimenting with Android, especially with its <a href="http://en.wikipedia.org/wiki/OpenGL">OpenGL</a> functionality. As I am not inexperienced with OpenGL and Java through my &#8220;playing around&#8221; with <a href="https://jogl.dev.java.net/">JOGL</a>, I thought I may get a quick access to Android and OpenGL. Cannot be that hard&#8230; it is not exactly hard, but it is different. Android supports <a href="http://en.wikipedia.org/wiki/OpenGL_ES">OpenGL ES</a> as part of its system, framework and SDK. OpenGL ES is not OpenGL, and principles as well as things you do with OpenGL to quickly achieve results cannot be done with OpenGL ES the same way.
</p>
<p>
<a href="http://nehe.gamedev.net/"><img src="http://insanitydesign.com/wp/wp-content/uploads/nehebutton.jpg" alt="NeHe Productions" title="NeHe Productions" style="width: 88px; border: 0; text-decoration: none; padding-top: 10px;" align="right" class="alignright size-full wp-image-404" /></a>There are actually some minor OpenGL ES tutorials out there and some very basic Android OpenGL tutorials, but none of them show a wide range of functionality. As I am personally a big fan of probably the best OpenGL tutorials in the net, <a href="http://nehe.gamedev.net/">NeHe</a>, I wanted to stick with these and try to adapt these onto Android. This is not exactly the most difficult task but you have to think around the corner at once. The NeHe tutorials are more OpenGL functionality test beds, to show of what could be done and could be combined. The style is straight forwards and very easy to understand but does not work without some rework with Android.
</p>
<p>
Therefore, I started porting the NeHe tutorials to Android. First, just for myself, to get used to Android and OpenGL, but I think they may be helpful to others, too. That is why I started a <a href="http://insanitydesign.com/wp/projects/nehe-android-ports/">project page</a> for this, currently coping the first eight tutorials of NeHe ported to Android with a little explanation what has been changed regarding the platform requirements. I will continue to expand the tutorials and port all others that can be done on Android. I hope anybody looking for help in that area can be helped with the ports.<br />
If any questions should occur, problems, comments, good or bad, just comment to the <a href="http://insanitydesign.com/wp/projects/nehe-android-ports/">NeHe Android Ports page</a> and I will get to it. In the meantime I will continue experimenting and porting the rest of the NeHe tutorials. I try to keep this up-to-date on a regularly basis but I cannot promise anything.</p>
]]></content:encoded>
			<wfw:commentRss>http://insanitydesign.com/wp/2009/08/02/nehe-android-ports/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<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>

