<?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; Code</title>
	<atom:link href="http://insanitydesign.com/wp/category/code/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>Under Pressure</title>
		<link>http://insanitydesign.com/wp/2011/05/12/under-pressure/</link>
		<comments>http://insanitydesign.com/wp/2011/05/12/under-pressure/#comments</comments>
		<pubDate>Thu, 12 May 2011 21:16:48 +0000</pubDate>
		<dc:creator>NeA</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[What troubles me...]]></category>
		<category><![CDATA[altdevblogaday]]></category>
		<category><![CDATA[crunchtime]]></category>
		<category><![CDATA[overtime]]></category>
		<category><![CDATA[pressure]]></category>

		<guid isPermaLink="false">http://insanitydesign.com/wp/?p=1029</guid>
		<description><![CDATA[The issue tracker is overflowing and the deadline is inexorably coming near: The milestone 4 build has to be reached! Feature-set B15 has to be fully implemented and needs to be QA approved but bugs still occur and some features haven&#8217;t even been worked on. Everything needs to be crunched in there somehow as bug [...]<div><a class="addthis_button" href="//addthis.com/bookmark.php?v=250" addthis:url='http://insanitydesign.com/wp/2011/05/12/under-pressure/' addthis:title='Under Pressure '><img src="//cache.addthis.com/cachefly/static/btn/v2/lg-share-en.gif" width="125" height="16" alt="Bookmark and Share" style="border:0"/></a></div>]]></description>
			<content:encoded><![CDATA[<p>The issue tracker is overflowing and the deadline is inexorably coming near: The milestone 4 build has to be reached! Feature-set B15 has to be fully implemented and needs to be QA approved but bugs still occur and some features haven&#8217;t even been worked on. Everything needs to be crunched in there somehow as bug fixing is not limited by the announced feature freeze&#8230; and so it happens that you go into <em>overtime</em>!</p>
<p>Because of actual events in my last weeks and months this topic just pops up again and again with me: <strong>Crunch</strong> and <strong>Overtime</strong>! Nowadays, these are even accepted as &#8220;normal&#8221; in not only Games but general IT and Development. I know only few other industries and departments that take crunch-time for granted&#8230; especially in the end of any project.</p>
<p>As soon as overtime happens it is already too late. As no project manager (should) plans with crunch-time something went wrong if it happens anyway. In some cases this is not necessarily bad. Most people do what they do, work at what they work because they like the challenge, they like the environment&#8230; they just like what they do.<br />
But no matter how much you love your work, after 12, 18, 24 hours day after day after day no <a href="www.redbull.com/">Red Bull</a> nor a single good night sleep can help to keep you really focussed and up to the task that you are actually on.</p>
<p>I do not want to go into detail why something like overtime happens but there are some things professionally and socially that I observed over the last years and especially months I want to share if you have to crunch to release a feature in dependency with others!</p>
<h3>Documentation vs. Communication (or &#8220;State the obvious&#8221;)</h3>
<blockquote><p>Pressure pushing down on me<br />
Pressing down on you no man ask for</p></blockquote>
<p><a href="http://insanitydesign.com/wp/wp-content/uploads/cpt-obvious.jpg"><img class="alignright size-medium wp-image-1058" title="Captain Obvious" src="http://insanitydesign.com/wp/wp-content/uploads/cpt-obvious-246x300.jpg" alt="" width="246" height="300" /></a>It may be so easy: You get your Game Design, your Technical Design, Interfaces, Standards etc. defined and start developing from top to bottom. In the end everything works out, interconnected and your task is finished. Great!<br />
This <a href="http://www.imdb.com/title/tt0107808/">perfect world</a> is pretty rare and in most cases does not reflect the &#8220;<a href="http://en.wikipedia.org/wiki/The_Real_World">real life</a>&#8220;. In most cases many things have to be reworked or clarified and therefore <strong>communication socially and professionally</strong> is one of the most important factors when it comes to development in larger teams.<br />
Nevertheless, especially after 12, 14 hours of work or during a night the receptivity starts to lack the focus it needs for intense communication and dialogues. People start starring at their displays trying to get around that one oddness or gaze into the coffee/energy drink creeping over the floor. People that normally question anything start developing &#8220;<em>till the end</em>&#8221; and not &#8220;<em>to finish a task successfully</em>&#8221; meaning they &#8220;crunch&#8221; all what is left into their current objective, finish it up as quickly as possible top to bottom based on the docs&#8230; and as clarification takes time if the design itself can also be interpreted in a specific implementation kind of way: It will be!<br />
So, in the end of any project, after many hours of work, during nights etc. try and start being pro-active: If you crunch with others, <strong>state the obvious</strong>! If you do overtime yourself, <strong>start questioning</strong> the most simple things! This may sound annoying but is most important as the most well-formed process is nothing worth after four+ weeks of crunching. Normal things like &#8220;Did you add the graphics of that item?&#8221; or &#8220;Have you added the <a href="http://en.wikipedia.org/wiki/I18n">i18n</a> key?&#8221; are the first things that get lost as soon as a narrowed mind is focussing on fixing a bug or finishing up a feature.</p>
<h3>Crunch in Overtime (or &#8220;The right Task at the right Time&#8221;)</h3>
<blockquote><p>Insanity laughs under pressure we&#8217;re cracking<br />
Can&#8217;t we give ourselves one more chance</p></blockquote>
<p>Don&#8217;t get me wrong: Sometimes overtime can be very healthy for a project and team if e.g. a small group of people focus on one small feature-set altogether and try to reach a goal in a given time frame. Tasks get crunched, time just passes by and everybody is happy (with some pizza and beer of course this can be a wonderful achievement).<br />
Nonetheless, very often overtime is used or has to be used to finish up tasks that are <em>unfinished</em> or even <em>untouched</em>. This leads to crunching in all the different tasks that just have to be done before a milestone or deadline is reached. So, the overtime is used to clear out the issue tracker and not to finish what the main goal was.<br />
If overtime happens use it wisely and <strong>plan what to do</strong>! You are not in your right state of mind after hours and hours of coding, drawing, layouting, &#8230; and deprivation of sleep can lead to similar effects as alcohol e.g. headache or dizziness. The efficiency may seem increased after some energy drinks but based on experience and code review&#8230; it is not! You cannot state a number but if the efficiency and focus is decreased, plan in some laborious work, some monotonic tasks, clean up and work off method sets etc. Complete new structures, concept arts (depends on the crazy creativity ^^), calculations or templates especially interfacing with others (see above) are detrimental. <strong>Crunching has to be planned</strong> and should not just occur!</p>
<h3>Social Competence (or &#8220;To Develop is Human&#8221;)</h3>
<blockquote><p>Watching some good friends<br />
Screaming &#8216;Let me out&#8217;</p></blockquote>
<p>During daytime everybody is calm, touched by the sun, always having a smile on their faces. But after 15 hours <a href="http://www.imdb.com/title/tt0116367/">from dawn till dusk</a> the smile starts to vanish from their faces as the sun sets.<br />
It is no matter how &#8220;nice&#8221; somebody is during the day: during overtime and crunching tasks every mood starts to swing. If set under pressure over weeks, sleepless for days and crunching code into a machine people get nervous and tetchy.<br />
Now it is important to be sensitive. Not only developers, artists, &#8230; in-between but also a managing director has to apply his best soft-skills and <strong>pressurize focussed but appreciative</strong>. Even ironic jokes that would cheer up anybody during daytime can break loose hell if people spend 20 hours working on one bug! This <a href="http://en.wikipedia.org/wiki/Soft_skills">emotional intelligence</a> is a major issue when it comes to delegated work. Nobody intentionally tries to <em>not</em> finish any task so do not sound like this.<br />
To loosen up a little and see the crunching time as a task of the team. Do not take it too serious&#8230; it is more important to sometimes just take a walk and have a little water cooler talk. I am a non-smoker but if it is getting dark it can be helpful to just go with the crowd and to keep together. Share thoughts, introduce pair-programming (if not already given, <em>200% more effective during overtime</em> in my opinion) and try to help each other as together the longest nights can become the best stories for the next day.</p>
<h3>Stay Focussed (or &#8220;Utopia is nowhere near&#8221;)</h3>
<blockquote><p>It&#8217;s the terror of knowing<br />
What this world is about</p></blockquote>
<p>In 90% of cases overtime and crunch-time happens because a goal has to be reached in time. A milestone, a release build, &#8230; whatever. Unfortunately, during crunch-time it often occurs that some people see this time as &#8220;additional&#8221; hours to use (see above). They try to achieve 200% and not to reach bug-free 100%. Such ideas come from management/directors but also from developers that tend to pressurize themselves. If they do not get to see their bed for days at least this time has to pay out.<br />
<strong>Always be realistic</strong> about what the goal is and try to not loose focus of what can be achieved during this overtime. As stated above crunching should be planned and therefore plan against the origin of that specific overtime. If people are under pressure it is more important to eliminate all mush and narrow down what you want to achieve. Overtime pays out in work and even for the person itself if something has been achieved. A e.g. developer that works all through the night coding and coding without having achieved what he wanted in the morning is only half the developer for the coming hours and days. But if you clearly achieve your realistic goal you are happy and produce endorphins. Your body is powered up and you can shed any sorrows of work. <em>This is the best sleep you will have for months</em>!</p>
<h3>Keep the Balance (or &#8220;The equilibrium of Life and Work&#8221;)</h3>
<blockquote><p>And love dares you to care for<br />
The people on the edge of the night</p></blockquote>
<p>Overtime happens and crunching some work, too. This can be manageable to some degree. But if your whole purpose in life is work and you are crunching everyday, hour after hour, seeing sunlight only as a reflection of your display you will &#8220;dry up&#8221;.<br />
As much as overtime has to be planned (see above), the balance of overtime, crunching and <strong>regeneration has to be maintained</strong>, too. Otherwise the productivity and benefit of the additional time decreases down to a (negative) point of no return&#8230; yes, negative. At least in many cases I have seen, people actually fixed and created productive resources and code up to a specific point where the amount of positives fell below the amount of negatives. And this just happened over one day. When days went by the amount of time that produced good quality decreases and got inferior to the amount of time producing crap. And the most important issue is: Those errors have to be cleaned up, too!<br />
This is something general and may sound corny but to keep a good Work/Life balance is most important and overtime is no contradiction to it. But crunched overtime needs different compensation to be regenerated. As mentioned, a good night sleep might not be enough for a 80 hours week. Fresh air, <strong>sunlight</strong>, healthy drinks and food are a necessity to &#8220;survive&#8221; not only the crunch-time but the time after (downfall).</p>
<h3>Post Mortem (or &#8220;The Lessons we Learned&#8221;)</h3>
<blockquote><p>This is our last dance<br />
This is ourselves<br />
Under pressure</p></blockquote>
<p>This is not necessarily something to keep in mind during a crunch-phase but afterwards. Always <strong>recapture what happened</strong>! Always try to learn from the lessons made! A retrospective or post mortem should help to pinpoint problems, miscommunication, bad planning etc. for the coming tasks and have to be used for positive and directed criticism.<br />
A review about every process, not only meta or technical processes but also socially can help to suffocate future errors. Especially critique is hard to deal with and often taken personal. But what directed criticism (a director that guides the review is most important. Reviewing, not discussing when it comes to focussed critique) should provide is what we require to grow, to evolve. Because that is what we all want: <em>To become better</em>! It may sound unfortunate but people outside ourself often provide a better view on us than we ever can.<br />
Therefore, always have a retrospective, a review, a post mortem, a lessons learned meeting, &#8230; call it whatever you (or your project management philosophy) like, but do it!</p>
<p>So, if we have a look at the lessons we learned:</p>
<ul>
<li>State the obvious</li>
<li>Plan your overtime</li>
<li>Be social</li>
<li>Be realistic</li>
<li>Keep a Work/Life balance</li>
</ul>
<p>and always recapture your work!</p>
<p>All this may sound general and soooo obvious but after weeks of overtime, pressure from the management and the deadline coming near it gets lost pretty easy.<br />
Overtime happens and sometimes it can even be fun to see &#8220;this one feature being finished&#8221;, &#8220;this one bug being fixed&#8221;, especially in a <em>nice social environment</em>. Nevertheless, if you have to crunch keep in mind that not everybody is in the right <a href="http://www.youtube.com/watch?v=WcYDBlMbU_Q">state of mind</a> and always remember some general work rules&#8230; maybe even pin them on a wall in front of you!</p>
<p><em>Written for <a href="http://altdevblogaday.org/">#AltDevBlogADay</a></em></p>
<div><a class="addthis_button" href="//addthis.com/bookmark.php?v=250" addthis:url='http://insanitydesign.com/wp/2011/05/12/under-pressure/' addthis:title='Under Pressure '><img src="//cache.addthis.com/cachefly/static/btn/v2/lg-share-en.gif" width="125" height="16" alt="Bookmark and Share" style="border:0"/></a></div>]]></content:encoded>
			<wfw:commentRss>http://insanitydesign.com/wp/2011/05/12/under-pressure/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>(Don&#8217;t Fear) The (C)Reaper</title>
		<link>http://insanitydesign.com/wp/2011/04/13/dont-fear-the-creaper/</link>
		<comments>http://insanitydesign.com/wp/2011/04/13/dont-fear-the-creaper/#comments</comments>
		<pubDate>Tue, 12 Apr 2011 22:02:52 +0000</pubDate>
		<dc:creator>NeA</dc:creator>
				<category><![CDATA[.net]]></category>
		<category><![CDATA[C/C++]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[What troubles me...]]></category>
		<category><![CDATA[altdevblogaday]]></category>
		<category><![CDATA[c/c++]]></category>
		<category><![CDATA[challenge]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[ignorance]]></category>
		<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://insanitydesign.com/wp/?p=969</guid>
		<description><![CDATA[I have to be honest: My C and C++ skills are bad! Besides some personal tries in my &#8220;early years&#8221; and lessons at the University I never had a good connection to the world of C. Nevertheless, I got my degree, became a developer, working for nearly a decade now in my young life and [...]<div><a class="addthis_button" href="//addthis.com/bookmark.php?v=250" addthis:url='http://insanitydesign.com/wp/2011/04/13/dont-fear-the-creaper/' addthis:title='(Don&#8217;t Fear) The (C)Reaper '><img src="//cache.addthis.com/cachefly/static/btn/v2/lg-share-en.gif" width="125" height="16" alt="Bookmark and Share" style="border:0"/></a></div>]]></description>
			<content:encoded><![CDATA[<p>
I have to be honest: My <a href="http://en.wikipedia.org/wiki/C_(programming_language)">C</a> and C++ skills are bad! Besides some personal tries in my &#8220;early years&#8221; and lessons at the University I never had a good connection to the world of C. Nevertheless, I got my degree, became a developer, working for nearly a decade now in my young life and I call myself a successful Software Engineer, even developing games&#8230; but in <a href="http://www.java.com/">Java</a>, JavaScript and <a href="http://msdn.microsoft.com/en-us/vcsharp/default">C#</a>. So, my dreams of getting into what I love most (<strong><em>Gaming</em></strong>) became real, doing what I like all day!<br />
But still I feel inferior to the &#8220;real&#8221; developers because of my bad C expertise and especially my personal ignorance to really focus on it.
</p>
<h3>All my history&#8230;</h3>
<p>
I got in touch with computers and gaming early in my life through my brother. I started with a C16, C64 and <a href="http://en.wikipedia.org/wiki/Amiga_500">Amiga 500</a> besides my GameBoy until I got my first (nowadays) classic PC. I was always intrigued by what was possible, the magic, playing <a href="http://stewd.io/pong/">Pong</a>, Maniac Mansion, Zork and watching Scene Demos and Cracktros from legends such as <a href="http://www.tbl.org">The Black Lotus</a> or the <a href="http://www.youtube.com/watch?v=Hp1kmaon3tk">Animators</a>. I wanted to do the same stuff, I wanted to (text-)wander through my own forests, wanted to have colorful spinning balls on the screen&#8230; so I began learning how to do so.<br />
I started off on the Amiga with Assembler, got into <a href="http://en.wikipedia.org/wiki/QBasic">QBasic</a> later, Pascal, <a href="http://www.delphigl.com/">Delphi</a> (<em>loved its structure</em>), Visual Basic (<em>quick results</em>) and very early PHP (<em>the Internet</em>) through early Web-Development tests and HTML/CSS. About ten years ago I got into Java at version 1.1 and am still on it. At every Job I had before and during my studies I was able/forced to use Java and it kept that way until today. Besides Java I had a look at and use Python, <a href="http://www.scala-lang.org/">Scala</a> (<em>what I like about Java+functional programming</em>), Actionscript, &#8230; and even Perl (just in one project) out of personal interest or for personal stuff.<br />
Just from my history my expertise developed early around object-oriented programming which appealed to me the most, so I stayed. Therefore, very early in my &#8220;<em>personal development</em>&#8221; my development expertise was already conquered by Delphi and Java that formed my view on OOP and general Application Development besides their originators being influenced by C++ (good or bad). But there were still the games I liked the most. So, I had to learn C and C++. Teach myself the language of my favourite entertainment.
</p>
<h3>Try, Fail, Ignore</h3>
<p>
I bought books about C, about C++, about Game Development, about DirectX, about OpenGL, got into boards, searched the net for every tutorial I could find, tried everything and even got some minor things to work so that something moved on my screen&#8230; and it was programmed in C++. But something <em>clicked</em> in my head, spreading bad thoughts such as: </p>
<ul>
<li>This could be easier!</li>
<li>Linkage and IDE is clumsy, <a href="http://eclipse.org/">Eclipse</a> is way superior!</li>
<li>These <a href="http://en.wikipedia.org/wiki/Design_patterns">design patterns</a> are native in Java!</li>
</ul>
<p>I read more and more, tried more and more and unfortunately failed more often. The initial fun and ambition faded away with every single compilation that turned out to not work as expected, crashed or ended up in memory leaks.<br />
Even with every interest and devotion I had to learn, to me it was &#8220;just&#8221; another syntax complicating things. Pretty much everything I learned and did I was able to reproduce in Java in less time and with more comfort and less errors. <em>I got lazy</em>!
</p>
<h3>Coding Personality</h3>
<p>
So, even if I tried to seriously learn and get into C and C++ it just did not reach me, did not touch me. From my history and my experience with other languages, IDEs and projects I knew that there were different ways to achieve nearly the same things. And it was not only my laziness from very elegant development environments or library usage, also the code itself appeared to be cryptic to my eyes.<br />
No matter if I read Java, Python or PHP code nowadays: Besides the fact that every code can be beautiful and ugly I understand Java code instantly; I recognize the Python functionality; I get what the PHP developer meant to do! Even in the last years as I was checking examples and help sites for iOS and Android <a href="http://developer.android.com/sdk/ndk/index.html">NDK</a> coding out of interest I could not get rid of the thought: I can achieve the same thing with the <a href="http://developer.android.com/sdk/index.html">Android SDK</a>! (PS: Objective-C is pretty ugly ^^)<br />
And it is not that I do not like any other languages any more: I was &#8220;forced&#8221; to use Haskell and dismissed it; tried Scala and loved it! Fooled around with Ruby and had fun; Prolog and Lisp&#8230; na; Eiffel and C#, olé!<br />
Especially C# instantly appealed to me: The syntax, the structures, the functionality and the ideas filled the holes that Java left over the years. It may be a coincidence that <a href="http://en.wikipedia.org/wiki/Anders_Hejlsberg">Anders Hejlsberg</a>, a main man behind &#8220;my&#8221; Delphi is the lead designer of C# but maybe we think alike. And with the advent of <a href="http://create.msdn.com">XNA</a> I even had a connection to game development again&#8230; and it started with a C! The commonality of course was a similar syntax, similar principles and the idea of a Virtual Machine executing and &#8220;managing&#8221; my code. No changes for specific operating systems (at least in the perfect sales world ^^), just develop and it would work&#8230; now with easy native Windows &#8220;ways&#8221;!<br />
But the thing that always struck me again were games. Even XNA seemed &#8220;unreal&#8221; for real game developments.
</p>
<h3>Games are developed in C</h3>
<p>
If I would have gotten one cent for every time I read this exact line on a board, tutorial or e-mail&#8230; you know what I would be then as you probably think the same right now. And I believed it! It was like this; It stays like this!<br />
But over time I got more experienced in developing and engineering applications and solutions and I realized that in most cases the programming language is just the tool to fulfil the requirements: And my requirement was still to make the things I have in my head!<br />
I started to look around and found games such as <a href="http://www.spiralknights.com/">Spiral Knights</a>, Puzzle Pirates, <a href="http://bytonic.de/html/jake2.html">Jake2</a> (a Quake2 Java port), <a href="http://en.wikipedia.org/wiki/Chrome_(video_game)">Chrome</a> using Java for scripting and even <a href="http://www.eveonline.com/">EVE Online</a> from <a href="http://www.ccpgames.com">CCP</a>. A Server and Client nearly 100% developed in <a href="http://www.stackless.com/">Stackless Python</a>; a dynamic programming language in a multi-micro-threaded environment. Easy to read and learn, hard to master.<br />
But probably the biggest counterexample today would be <a href="http://www.minecraft.net/">Minecraft</a>. The biggest Indie sensation last year is developed in Java and even if I never really got into the game, I admire <a href="http://notch.tumblr.com/">Notch</a> for what he did and achieved&#8230; and everything in Java. And Minecraft was not the first but <a href="http://www.wurmonline.com/">Wurm Online</a> already showed where Notch could go&#8230; in Java.
</p>
<p><iframe title="YouTube video player" width="600" height="380" align="center" src="http://www.youtube.com/embed/m_yqOoUMHPg" frameborder="0" allowfullscreen></iframe></p>
<p>
With these great examples of <strong>Games</strong> not developed in C/C++ I felt more confident in following my own way that I have successfully gone for years now.
</p>
<h3>To be or TioBe</h3>
<p>
I do not intend to defy C or C++ but if I am not required to use C for the games I want to create and other segments and industries can be conquered by languages such as Java, too (as shown in the <a href="http://www.tiobe.com/index.php/content/paperinfo/tpci/index.html">Tiobe Index</a>), why should I?<br />
Especially in enterprise environments Java is a strong candidate for projects: From a Manager perspective the Java salesman argues with operating system independence, easy extended library architecture, basic native Database framework and <a href="http://en.wikipedia.org/wiki/Swing_(Java)">UI</a> support&#8230; sold! Enterprise Java is still a keyword for international research projects today. And with JME and Android even the mobile sector is invaded by Java for years now.<br />
And with Android supporting Java as well as Microsoft supporting C# I can be everywhere: On PCs, on Consoles, on Mobile Phones and on Browsers. With languages I know, am experienced with and that appeal to me.<br />
So, do I still have to put all my power in re-learning what I already know in other languages? Where I have intensive practical knowledge? Where I can craft my dreams?
</p>
<p><a href="http://insanitydesign.com/wp/wp-content/uploads/tpci_trends.png"><img src="http://insanitydesign.com/wp/wp-content/uploads/tpci_trends.png" alt="" title="Tiobe Index" width="500px" class="aligncenter size-medium wp-image-1010" /></a></p>
<h3>Ignorance is bliss</h3>
<p>
Even with my underwhelming C skills I get along very well. Tiobe proves me right and until now I always solved the problems given to me or achieved and created what I wanted. I am working in the games industry, worked on large and international projects for big companies, wrote some publications and most results were accepted just fine. I even remember some projects and programs created by me that I am still proud of and this does not happen very often as every developer I know normally wants to change the code he wrote the second he/she finished the last line ^^.<br />
I am aware that for the last performance tweak, for the most awesome graphics engine I would have to use C (or Assembler) and I am aware that the foundation for all that I use such as the Java or the .net VM an explicit knowledge is required. Nevertheless, I do not state that nobody should use C or C++. It is just that I want to raise awareness for people that complain about people not knowing C, labelling these as non-programmers. These guys are able, too. And if they want to <a href="http://scientificninja.com/blog/write-games-not-engines">Write Games, not Engines</a> they might even be better for game logic and not &#8220;just&#8221; tools. These guys are also able to know what <em>really happens</em> underneath as that is a mandatory pre-requisite and not the knowledge of a syntax.<br />
Therefore, besides all my years trying to get into &#8220;the game&#8221; of learning C and C++ I turned out pretty well, with experience in large projects, systems and now games. I call myself a game developer. And if many decline my languages I decide for myself that (C+)Ignorance Is Bliss&#8230;
</p>
<p>
<em>Part of the Challenge: <strong>Show your ignorance!</strong> for <a href="http://www.altdevblogaday.org/">#AltDevBlogADay</a></em></p>
<div><a class="addthis_button" href="//addthis.com/bookmark.php?v=250" addthis:url='http://insanitydesign.com/wp/2011/04/13/dont-fear-the-creaper/' addthis:title='(Don&#8217;t Fear) The (C)Reaper '><img src="//cache.addthis.com/cachefly/static/btn/v2/lg-share-en.gif" width="125" height="16" alt="Bookmark and Share" style="border:0"/></a></div>]]></content:encoded>
			<wfw:commentRss>http://insanitydesign.com/wp/2011/04/13/dont-fear-the-creaper/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>3&#8230;2&#8230;1&#8230; planned!</title>
		<link>http://insanitydesign.com/wp/2011/03/29/3-2-1-planned/</link>
		<comments>http://insanitydesign.com/wp/2011/03/29/3-2-1-planned/#comments</comments>
		<pubDate>Tue, 29 Mar 2011 20:36:36 +0000</pubDate>
		<dc:creator>NeA</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[What troubles me...]]></category>
		<category><![CDATA[altdevblogaday]]></category>
		<category><![CDATA[estimation]]></category>
		<category><![CDATA[pert]]></category>
		<category><![CDATA[planning]]></category>
		<category><![CDATA[task]]></category>
		<category><![CDATA[tdd]]></category>
		<category><![CDATA[test-drive]]></category>

		<guid isPermaLink="false">http://insanitydesign.com/wp/?p=963</guid>
		<description><![CDATA[No matter what we do, if we are Agile or fall down the water&#8230; if we are senior or junior&#8230; if it is big or small&#8230; for nearly everything we do we have to define tasks and estimations to plan the days, weeks and months to come. Again, no matter what, this (especially first) planning [...]<div><a class="addthis_button" href="//addthis.com/bookmark.php?v=250" addthis:url='http://insanitydesign.com/wp/2011/03/29/3-2-1-planned/' addthis:title='3&#8230;2&#8230;1&#8230; planned! '><img src="//cache.addthis.com/cachefly/static/btn/v2/lg-share-en.gif" width="125" height="16" alt="Bookmark and Share" style="border:0"/></a></div>]]></description>
			<content:encoded><![CDATA[<p>
No matter what we do, if we are Agile or fall down the water&#8230; if we are senior or junior&#8230; if it is big or small&#8230; for nearly everything we do we have to define tasks and estimations to plan the days, weeks and months to come. Again, no matter what, this (especially first) planning is in most cases (and from personal experience <em>most</em> means 90%) pretty far from what is really required in the end. The other 10% split up into 1) the ones that planned good but not 100% correct, maybe used &#8220;proven&#8221; methodologies such as <a href="http://en.wikipedia.org/wiki/PERT">PERT</a> or just estimated +30% and 2) the ones where the planning perfectly fit the development (again in my experience normally 1%-3%).<br />
So, what you could say is to just &#8220;do it&#8221; like the 1%-3% did. This would normally be the way to go if theirs worked out. The thing is, from everything I have seen in project planning over the years: It just worked because of luck!
</p>
<p>
I think it is fair to say that I learned project planning pretty much from the practical side, always failing what I learned theoretically. No mater how much time I spend planning big projects, setting up tasks, goals, milestones, reviews, reworks, &#8230; it never got into this <em>1-3</em> frame.<br />
Even with a more agile-driven approach, small sprints, good daily tasks, weekly reviews and time consuming remodelling of the plan: If I sum up what had to be reworked every single week I was as far away as with the initial waterfall plan. All goals got achieved and &#8220;somehow&#8221; it worked out but it is disappointing for the one who planned to see his estimations being more a <em>guideline</em> than a workplan.<br />
Based on that experience I started thinking: What are the reasons for such divergence? What am I planning wrong? What do I have to change to fit the developers needs? And that is what strucked me: The Developer!
</p>
<h3>&#8230;to be busy!</h3>
<p>
With all IT projects I had to work on, the main time is consumed by the developers, the engineers, the architects of the (mostly) software projects. Of course Game Design, Art, etc. have to be taken into account but are often more parallel to what goes wrong more often: The actual development or implementation! (no question, thinking <a href="http://en.wikipedia.org/wiki/Lean_manufacturing">lean</a> everybody should care about downtimes because of unfinished output/input)<br />
As a developer myself that has to plan for others, estimate work, thinking about the production, milestones etc. none of the &#8220;theoretical&#8221; methodologies really worked out for me but just took my time. And in most cases this time is very limited. Estimations have to be given instantly to evaluate feasibility, plans have to be set-up initially to have a higher model to work and further estimate on. So, time is of the essence not only in the plan itself but also for the time to create it. And if I have to rework it all the time (real-life) I do not want to spend too much time in that phase (no time for building up charts with optimistic, pessimistic and realistic plans&#8230;).
</p>
<h3>&#8230;should be enough!</h3>
<p>
In a coincidence <a href="http://altdevblogaday.org/author/jake-simpson/">Jake Simpson</a> gave a pretty good impression of this wonderful land, where everything works out. It is known as <a href="http://altdevblogaday.org/2011/03/28/should-be-land/">Should Be Land</a>. This is normally the land where the estimations come from, too. From developers that should estimate their tasks, should give away an idea how long each of it could take to make a plan that also has to tie in with other departments (<em>lean</em> everywhere). If such an estimation fails because &#8220;36 hours should be enough!&#8221; more often others that depend on you are delayed, too.<br />
Especially inexperienced developers, <a href="http://altdevblogaday.org/2011/03/20/so-you-wanna-be-a-senior/">juniors</a> and fresh &#8220;hackers&#8221; from the backyard tend to underestimate especially the requirements in correlation with others, to plan interfaces, to build adapters to dock onto others and so on. Nevertheless, seniors aren&#8217;t better in general. All people that &#8220;program&#8221; stuff normally just plan the programming time&#8230; and they do not want to plan too much time as the developer is often assessed based on his Cph (Code per hour) output and not based on his quality of code, re-usability, extendibility or tests. The results are in many cases optimistic estimations with no or little time to even plan what you are going to develop.
</p>
<h3>&#8230;am no developer!</h3>
<p>
Another often misleading <em>planning element</em> is that (many) project managers, scrum masters, gantt-junkies, &#8230; do not have the best development background. Therefore, the estimations given are taken as fixed. <em>Experienced</em> managers add an amount of 30% and plan it in. This is unfortunate as even the best estimation cannot just be coped by some time-addition if essential points that are requirements for good development are missing.
</p>
<h3>One of Two of Three</h3>
<p>
Besides complicated methodologies or the adding of just 30%-50% of time to an initial estimation given, I split it up into the three tasks I want to see as an output from a developer: The <strong>implementation</strong> (or coding, hacking, programming, refactoring, &#8230;), the <strong>planning</strong> and the <strong>tests</strong>!</p>
<ul>
<li>The development is the actual implementation of the task. It may be the creation of a user-system, achievements, tool, crafting, &#8230; whatever comes to mind</li>
<li>The planning is the structuring of work, the evaluation of patterns, architecture and interfaces to follow during development and precedes it accordingly</li>
<li>The testing is no QA process but the personal testing of code, writing of (unit-)tests, maybe even playing the created and succeeds the development</li>
</ul>
<p>Now, instead of adding a specific amount to a given estimation I add tasks to the estimation. My input is the implementation estimation from a developer. Based on that I add two thirds as planning and one third of that as testing resulting in the three tasks of implementation, planning and testing with a weight of 1/3 of 2/3 of 3/3. For example, if an estimation is 9 hours, I add a task for planning with 6 hours and a task for testing with 2 hours.
</p>
<p>
Yes, the result is a very keen estimation but the important part for me is that it covers <em>mandatory</em> tasks that are often forgotten and is also able to compensate for possible misjudgement, unforeseen circumstances, &#8230; as the package is given as one. The creation of these tasks remind the developer what he &#8220;should&#8221; do, and the derived estimations compensate for possible problems as well as they fit the real necessity for the other tasks (at least in my experience).<br />
The tasks are important as normally you do not start hacking instantly. To evaluate existing code, interfaces and elaborate what architecture or pattern to use is often more practical and a necessity in general before starting to implement (to think something through <strong>before</strong> starting programming). To already know what the result should be helps the implementation. And the testing part may be the coders worst nightmare but again a requirement.
</p>
<p>
The most important point for me is: It&#8217;s easy! I can easily derive it in my mind, do have a most-likely accurate estimation (future may prove me wrong ^^) and won&#8217;t forget the importance of planning and testing.<br />
If you follow up different approaches the weighting can also be adapted either by mixing tasks or changing the base weight. For example, if you are following a <a href="http://en.wikipedia.org/wiki/Test_first">Test-first</a> approach you can either switch the planning and testing tasks, as the testing in TDD also compensates planning partly. Or you can change the <strong>base</strong> to 4 and plan 1/4 of 3/4 of 4/4 meaning for our example implement 8 hours, test-first for 6 hours and plan for 2 hours (bare with me as I selected easy to calculate estimations).<br />
What base to use depends on personal experience, the project and just the most important gut feeling. For me myself a third for general estimations and a fifth (1/5 of 2/5 of 5/5) for more specific tasks paid out. But all in general split up into my three main tasks I instantly have an estimation ready that fits at least my real-world.
</p>
<h3>&#8230;should work!</h3>
<p>
Please keep in mind this has no theoretically proven background but my experience over the years experimenting with different approaches and using the methodologies given in literature. Everything depends on your environment and personal likes and dislikes. It &#8220;should&#8221; work for other instances, too. I used it in several personal standalone and living project estimations and at least for now it was fitting best.<br />
In my environment, with the time given and the amount of work to do this approach works. It is never really off the track, it reminds people about planning and testing besides the actual <em>hacking</em> and helps me to easily keep track about developments without spending too much time in overblown concepts that do not fit my personal habits or the &#8220;real&#8221; developer.<br />
Of course, there are also drawbacks, such as too little/too much planning. If you split up e.g. User Stories to have tasks such as: <em>Build divideByZero() function</em>; <em>Create class object</em>; <em>Write SQL Statement for querying all users</em>; &#8230; you will end up in unnecessary tasks because of the simplicity. In such cases, the User Story &#8220;should be&#8221; the one to estimate and divide onto the tasks or you reduce the base and introduce a zero/x task.<br />
Therefore, this may not be the 100% <em>1-3</em> approach but it fits me best and therefore leads me into that frame more often as the important thing is the variance that fuels this approach&#8230; and that can make it work for you, too!
</p>
<p><em>Written for <a href="http://altdevblogaday.org/">#AltDevBlogADay</a></em></p>
<div><a class="addthis_button" href="//addthis.com/bookmark.php?v=250" addthis:url='http://insanitydesign.com/wp/2011/03/29/3-2-1-planned/' addthis:title='3&#8230;2&#8230;1&#8230; planned! '><img src="//cache.addthis.com/cachefly/static/btn/v2/lg-share-en.gif" width="125" height="16" alt="Bookmark and Share" style="border:0"/></a></div>]]></content:encoded>
			<wfw:commentRss>http://insanitydesign.com/wp/2011/03/29/3-2-1-planned/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<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 [...]<div><a class="addthis_button" href="//addthis.com/bookmark.php?v=250" addthis:url='http://insanitydesign.com/wp/2011/02/27/webgl-part-2-in-the-beginning-there-was/' addthis:title='WebGL &#8211; Part 2: In the beginning there was&#8230; '><img src="//cache.addthis.com/cachefly/static/btn/v2/lg-share-en.gif" width="125" height="16" alt="Bookmark and Share" style="border:0"/></a></div>]]></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>
<div><a class="addthis_button" href="//addthis.com/bookmark.php?v=250" addthis:url='http://insanitydesign.com/wp/2011/02/27/webgl-part-2-in-the-beginning-there-was/' addthis:title='WebGL &#8211; Part 2: In the beginning there was&#8230; '><img src="//cache.addthis.com/cachefly/static/btn/v2/lg-share-en.gif" width="125" height="16" alt="Bookmark and Share" style="border:0"/></a></div>]]></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 [...]<div><a class="addthis_button" href="//addthis.com/bookmark.php?v=250" addthis:url='http://insanitydesign.com/wp/2011/02/13/webgl-part-1-a-new-challenger-appears/' addthis:title='WebGL &#8211; Part 1: A new challenger appears&#8230; '><img src="//cache.addthis.com/cachefly/static/btn/v2/lg-share-en.gif" width="125" height="16" alt="Bookmark and Share" style="border:0"/></a></div>]]></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>
<div><a class="addthis_button" href="//addthis.com/bookmark.php?v=250" addthis:url='http://insanitydesign.com/wp/2011/02/13/webgl-part-1-a-new-challenger-appears/' addthis:title='WebGL &#8211; Part 1: A new challenger appears&#8230; '><img src="//cache.addthis.com/cachefly/static/btn/v2/lg-share-en.gif" width="125" height="16" alt="Bookmark and Share" style="border:0"/></a></div>]]></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>If you want to install Mac OS X in a VirtualBox on an Intel PC&#8230;</title>
		<link>http://insanitydesign.com/wp/2010/12/31/if-you-want-to-install-mac-os-x-in-a-virtualbox-on-an-intel-pc/</link>
		<comments>http://insanitydesign.com/wp/2010/12/31/if-you-want-to-install-mac-os-x-in-a-virtualbox-on-an-intel-pc/#comments</comments>
		<pubDate>Fri, 31 Dec 2010 16:02:10 +0000</pubDate>
		<dc:creator>NeA</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[iOS]]></category>
		<category><![CDATA[What troubles me...]]></category>
		<category><![CDATA[ipad]]></category>
		<category><![CDATA[macosx]]></category>
		<category><![CDATA[virtualbox]]></category>

		<guid isPermaLink="false">http://insanitydesign.com/wp/?p=656</guid>
		<description><![CDATA[&#8230;maybe the following can help! For about a week now I am owner of an iPad and it is actually pretty nice. Air Video Server allows me to watch all my stuff on my couch or bed and some of the games during Christmas sale are pretty nice, especially Dungeon Defenders and Angry Birds of [...]<div><a class="addthis_button" href="//addthis.com/bookmark.php?v=250" addthis:url='http://insanitydesign.com/wp/2010/12/31/if-you-want-to-install-mac-os-x-in-a-virtualbox-on-an-intel-pc/' addthis:title='If you want to install Mac OS X in a VirtualBox on an Intel PC&#8230; '><img src="//cache.addthis.com/cachefly/static/btn/v2/lg-share-en.gif" width="125" height="16" alt="Bookmark and Share" style="border:0"/></a></div>]]></description>
			<content:encoded><![CDATA[<p>
&#8230;maybe the following can help!
</p>
<p>
For about a week now I am owner of an <a href="http://www.apple.com/ipad/">iPad</a> and it is actually pretty nice. <a href="http://www.inmethod.com">Air Video Server</a> allows me to watch all my stuff on my couch or bed and some of the games during Christmas sale are pretty nice, especially <a href="http://www.slidetoplay.com/story/dungeon-defenders-first-wave-ipad-review">Dungeon Defenders</a> and <a href="http://www.slidetoplay.com/game/angry-birds-hd">Angry Birds</a> of course.
</p>
<p>
So, as I am a Developer I just want to develop and test some stuff on my iPad, to get into the iOS environment and broaden my horizon. Just out of curiosity. But the thing with nearly all Apple products in general is: They work fine&#8230; in their controlled <em>AND</em> <strong>closed</strong> environment!
</p>
<p>
If you want to develop for iOS you actually have to own an Intel Mac (i.e. Mac Mini, Macbook etc.). At least this is the only official way. There are some other possibilities such as with the <a href="http://www.dragonfiresdk.com">Dragonfire SDK</a> that builds a whole SDK for iOS development on Windows based on &#8220;normal&#8221; C/C++ or to use engines such as <a href="http://unity3d.com/">Unity</a> and <a href="http://www.stonetrip.com/">Shiva3D</a> that deploy onto iOS (actually Flash CS5 also provides an iOS publishing profile). But this is not what I want, I just want to try some things and get into the normal iOS development with XCode and Objective-C.
</p>
<p>
So as I decided to not follow some of the other ways I needed to get hold of a Mac Machine. But I am not willing to pay at least 600€ for a <a href="http://www.apple.com/macmini/">Mac Mini</a> just to &#8220;play around&#8221; with iOS. So I had to find a way to install Mac OS X Snow Leopard on my machine. As this is not supported by Apple and a little tricky I did not want to start installing it directly to my notebook but thought about trying it in a virtual machine. <em>&#8220;Good thought&#8221;</em> you might say&#8230; but tricky as I noticed!
</p>
<p>
I chose <a href="http://www.virtualbox.org/">VirtualBox</a> as the Virtual Environment to test an installation. I did some googling and actually found many MANY resources, tutorials and board messages explaining how to do it. I am pretty sure I read nearly all of them and experimented for about 3 days to get it running&#8230; day and night (got no sleep during that time as I wanted it to run). But all the time something went wrong.<br />That one didn&#8217;t boot! The other one wouldn&#8217;t get my network card right. Audio is still a big problem&#8230; and so on and so forth. But all of a sudden, as I was already giving up (really!!!) I found two excellent WebSites and Tutorials that got it working for me.
</p>
<p>
The one that started the effort again was <a href="http://flynflip.com/Hackintoshing/WIP/W510_VB.html">flyNflip</a>. It was the first very clear and simple tutorial how to get started. That was the first easy tutorial that got me to a &#8220;just working&#8221; installation. The only thing you need is to get hold of a Snow Leopard DVD. I got an update Version, it&#8217;s not too expensive.<br /> But it wouldn&#8217;t boot! What did I do wrong? Going through the comments I found the second WebSite at <a href="http://www.sysprobs.com/install-mac-snow-leopard-1063-oracle-virtualbox-32-apple-intel-pc">Sysprobs</a>. It showed nearly the same steps how to install MacOSX in a VirtualBox but went a little further. It explains the EFI problem. It gives examples and further tutorials for getting rid of issues and more.
</p>
<p>
So now I have a running <a href="http://www.apple.com/macosx/">MacOSX Snow Leopard</a> running in my VirtualBox. It&#8217;s also up to date (10.6.5) and actually somehow smooth for a virtual machine. Currently I am installing <a href="http://developer.apple.com/devcenter/ios/index.action">XCode and the iOS SDK</a> (as you can see on the picture) and I hope everything will work out. If so, maybe I will post some stuff about iPad development&#8230;
</p>
<div id="attachment_664" class="wp-caption aligncenter" style="width: 510px"><a href="http://insanitydesign.com/wp/wp-content/uploads/macosx_virtualbox_small.png"><img src="http://insanitydesign.com/wp/wp-content/uploads/macosx_virtualbox_small-300x187.png" alt="MacOSX running in a VirtualBox" title="MacOSX running in a VirtualBox" width="500" class="size-medium wp-image-664" /></a><p class="wp-caption-text">MacOSX running in a VirtualBox</p></div>
<p>
Nevertheless, I am still very curious about why Apple does try to prevent you to explore MacOSX on other Machines (actually I am pretty sure I know why ^^&#8217;). But at least for developers that want to develop iOS Apps on a Windows or Linux Machine they should give out a possibility to do so. I would never buy a Mac Machine just to do so but I am very curious to try it and maybe develop the next #1 App ^^. They nearly lost me during the process and what I did is actually neither supported nor endorsed by Apple. I hope I won&#8217;t get into trouble but as I am owning an iPad and want to give something back I hope not.
</p>
<p>
So, I will get back to my MacOSX installation (XCode and iOS SDK installation is nearly finished) and hope I provided two good resources for others that think like me.
</p>
<p>
<strong>*UPDATE*</strong> XCode and iOS SDK installed. Works fine! But I have trouble if I want to set a widescreen display resolution. 4:3 works fine (e.g. 1280&#215;1024), widescreen has some problems (e.g. 1440&#215;900). It still boots but the redrawing of the window is somehow messed up.</p>
<div><a class="addthis_button" href="//addthis.com/bookmark.php?v=250" addthis:url='http://insanitydesign.com/wp/2010/12/31/if-you-want-to-install-mac-os-x-in-a-virtualbox-on-an-intel-pc/' addthis:title='If you want to install Mac OS X in a VirtualBox on an Intel PC&#8230; '><img src="//cache.addthis.com/cachefly/static/btn/v2/lg-share-en.gif" width="125" height="16" alt="Bookmark and Share" style="border:0"/></a></div>]]></content:encoded>
			<wfw:commentRss>http://insanitydesign.com/wp/2010/12/31/if-you-want-to-install-mac-os-x-in-a-virtualbox-on-an-intel-pc/feed/</wfw:commentRss>
		<slash:comments>0</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 [...]<div><a class="addthis_button" href="//addthis.com/bookmark.php?v=250" addthis:url='http://insanitydesign.com/wp/2009/12/08/new-android-opengl-es-port-stencil-shadows/' addthis:title='New Android OpenGL ES Port &#8211; Stencil Shadows '><img src="//cache.addthis.com/cachefly/static/btn/v2/lg-share-en.gif" width="125" height="16" alt="Bookmark and Share" style="border:0"/></a></div>]]></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>
<div><a class="addthis_button" href="//addthis.com/bookmark.php?v=250" addthis:url='http://insanitydesign.com/wp/2009/12/08/new-android-opengl-es-port-stencil-shadows/' addthis:title='New Android OpenGL ES Port &#8211; Stencil Shadows '><img src="//cache.addthis.com/cachefly/static/btn/v2/lg-share-en.gif" width="125" height="16" alt="Bookmark and Share" style="border:0"/></a></div>]]></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 [...]<div><a class="addthis_button" href="//addthis.com/bookmark.php?v=250" addthis:url='http://insanitydesign.com/wp/2009/12/06/android-and-the-opengl-issues/' addthis:title='Android and the (OpenGL) issues '><img src="//cache.addthis.com/cachefly/static/btn/v2/lg-share-en.gif" width="125" height="16" alt="Bookmark and Share" style="border:0"/></a></div>]]></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>
<div><a class="addthis_button" href="//addthis.com/bookmark.php?v=250" addthis:url='http://insanitydesign.com/wp/2009/12/06/android-and-the-opengl-issues/' addthis:title='Android and the (OpenGL) issues '><img src="//cache.addthis.com/cachefly/static/btn/v2/lg-share-en.gif" width="125" height="16" alt="Bookmark and Share" style="border:0"/></a></div>]]></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. [...]<div><a class="addthis_button" href="//addthis.com/bookmark.php?v=250" addthis:url='http://insanitydesign.com/wp/2009/08/08/the-first-10-nehe-android-tutorials/' addthis:title='The first 10 NeHe Android tutorials '><img src="//cache.addthis.com/cachefly/static/btn/v2/lg-share-en.gif" width="125" height="16" alt="Bookmark and Share" style="border:0"/></a></div>]]></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>
<div><a class="addthis_button" href="//addthis.com/bookmark.php?v=250" addthis:url='http://insanitydesign.com/wp/2009/08/08/the-first-10-nehe-android-tutorials/' addthis:title='The first 10 NeHe Android tutorials '><img src="//cache.addthis.com/cachefly/static/btn/v2/lg-share-en.gif" width="125" height="16" alt="Bookmark and Share" style="border:0"/></a></div>]]></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 [...]<div><a class="addthis_button" href="//addthis.com/bookmark.php?v=250" addthis:url='http://insanitydesign.com/wp/2009/08/06/two-new-nehe-android-ports/' addthis:title='Two new NeHe Android Ports '><img src="//cache.addthis.com/cachefly/static/btn/v2/lg-share-en.gif" width="125" height="16" alt="Bookmark and Share" style="border:0"/></a></div>]]></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>
<div><a class="addthis_button" href="//addthis.com/bookmark.php?v=250" addthis:url='http://insanitydesign.com/wp/2009/08/06/two-new-nehe-android-ports/' addthis:title='Two new NeHe Android Ports '><img src="//cache.addthis.com/cachefly/static/btn/v2/lg-share-en.gif" width="125" height="16" alt="Bookmark and Share" style="border:0"/></a></div>]]></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>
	</channel>
</rss>

