<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:planet="http://planet.intertwingly.net/" xmlns:indexing="urn:atom-extension:indexing" indexing:index="no"><access:restriction xmlns:access="http://www.bloglines.com/about/specs/fac-1.0" relationship="deny"/>
  <title>Planet WebGL</title>
  <updated>2010-02-09T03:00:05Z</updated>
  <generator uri="http://intertwingly.net/code/venus/">Venus</generator>
  <author>
    <name>Christopher Blizzard</name>
    <email>blizzard@mozilla.com</email>
  </author>
  <id>http://planet-webgl.com/atom.xml</id>
  <link href="http://planet-webgl.com/atom.xml" rel="self" type="application/atom+xml"/>
  <link href="http://planet-webgl.com/" rel="alternate"/>

  <entry xml:lang="en">
    <id>http://learningwebgl.com/blog/?p=1703</id>
    <link href="http://learningwebgl.com/blog/?p=1703" rel="alternate" type="text/html"/>
    <title>WebGL around the net, 8 Feb 2010</title>
    <summary>One big one today, and two smaller ones:

There have been a lot of fantastic games and demos made for WebGL, but that makes it particularly cool to see someone building something different.  iChemLabs have just released a 3D library for modelling molecules using WebGL (click the “3D” button at the top to get the [...]</summary>
    <content type="xhtml"><div xmlns="http://www.w3.org/1999/xhtml"><p>One big one today, and two smaller ones:</p>
<ul>
<li>There have been a lot of fantastic games and demos made for WebGL, but that makes it particularly cool to see someone building something different.  iChemLabs have just released a <a href="http://web.chemdoodle.com/">3D library for modelling molecules using WebGL</a> (click the “3D” button at the top to get the WebGL version).  They have <a href="http://web.chemdoodle.com/overview3D.php">an overview page</a> showing a bunch of different kinds of views of various molecules, and <a href="http://web.chemdoodle.com/molgrabber3d.php">a page demonstrating their MolGrabber3D canvas</a> where you can pick the drug whose molucule you want to see and can change the kind of representation you want to see it in.  There’s also <a href="http://web.chemdoodle.com/3dcwc.html">a nice 8-minute overview video</a>, so you can see what they’re up to even if you don’t have WebGL setup on your machine, and <a href="http://www.macresearch.org/3d-chemdoodle-web-components">a written overview</a> for the MacResearch community site.  So that’s chemistry done, any astrophysicists or geologists out there working with WebGL?  :-)</li>
<li>Not strictly WebGL, but it’s using it under the hood: Andor Salga has built <a href="http://matrix.senecac.on.ca/~asalga/DPS911/release0.5/demo/IDE3D/">a web-based mini-IDE</a> for the <a href="http://processingjs.org/">Processing.js</a> port of the <a href="http://processing.org/">Processing</a> 3D language.</li>
<li>A simple demo: drawing <a href="http://www.c3dl.org/wp-content/effects_all_3_browsers/effect_test2/">cartoon animations</a> using C3DL.</li>
</ul></div>
    </content>
    <updated>2010-02-08T14:45:00Z</updated>
    <category term="Links"/>
    <author>
      <name>giles</name>
    </author>
    <source>
      <id>http://learningwebgl.com/blog</id>
      <link href="http://learningwebgl.com/blog/?feed=rss2" rel="self" type="application/atom+xml"/>
      <link href="http://learningwebgl.com/blog" rel="alternate" type="text/html"/>
      <subtitle>...lessons 'n' links...</subtitle>
      <title>Learning WebGL</title>
      <updated>2010-02-09T02:20:03Z</updated>
    </source>
  </entry>

  <entry xml:lang="en">
    <id>http://learningwebgl.com/blog/?p=1698</id>
    <link href="http://learningwebgl.com/blog/?p=1698" rel="alternate" type="text/html"/>
    <title>WebGL around the net, 5 Feb 2010</title>
    <summary>Some interesting new projects today!

In the earliest WebGL demos, Vladimir Vukićević used the Sylvester library for vector and matrix maths; it’s convenient, well-written, well-documented, and open source.  Pretty much everyone else followed suit.  However, its very general-purpose nature means that it can’t be as fast as a specialised library focusing on the specific [...]</summary>
    <content type="xhtml"><div xmlns="http://www.w3.org/1999/xhtml"><p>Some interesting new projects today!</p>
<ul>
<li>In the earliest WebGL demos, Vladimir Vukićević used the <a href="http://sylvester.jcoglan.com/">Sylvester library</a> for vector and matrix maths; it’s convenient, well-written, well-documented, and open source.  Pretty much everyone else followed suit.  However, its very general-purpose nature means that it can’t be as fast as a specialised library focusing on the specific kinds of calculations we need for 3D graphics.  <a href="http://blog.vlad1.com/2010/02/05/mjs-simple-vector-and-matrix-math-for-js/">So, he’s started a new project called mjs</a>, to provide just the really simple vector and matrix maths we need, and to do it really quickly.  He’s getting pretty encouraging results on his benchmarks, with JITed calculations being more than ten times as quick, and un-JITed at least twice as fast.  [UPDATE I've tried porting the upcoming lesson 14 to it, and it works fine.  No performance figures yet, though.]</li>
<li>Another important part of building 3D scenes is getting models designed in 3D graphics programs into your JavaScript code.  People are working on tools to import existing 3D graphics formats like <a href="http://techblog.floorplanner.com/2010/01/27/introducing-js3ds-a-javascript-parser-for-3ds/">3DS</a>, but an alternative route is to get the 3D modelling tool to output JSON or some other format you can easily read.  Paul Brunt’s <a href="http://www.glge.org/">GLGE</a> has some support for doing this from Blender, but <a href="http://www.rozengain.com/blog/2010/02/04/blender-to-webgl-javascript-exporter/">Dennis Ippel has just announced a project that makes exporting from Blender to WebGL code its main focus</a>.  So far it exports to SceneJS, but a generic WebGL version is on its way.  It’s not ready to download yet, but will be soon.</li>
<li>Another 3D library for WebGL, this one with its own editor!  <a href="http://www.ambiera.com/copperlicht/index.html">CopperLicht</a> is definitely worth a look.</li>
</ul></div>
    </content>
    <updated>2010-02-05T14:29:11Z</updated>
    <category term="Uncategorized"/>
    <author>
      <name>giles</name>
    </author>
    <source>
      <id>http://learningwebgl.com/blog</id>
      <link href="http://learningwebgl.com/blog/?feed=rss2" rel="self" type="application/atom+xml"/>
      <link href="http://learningwebgl.com/blog" rel="alternate" type="text/html"/>
      <subtitle>...lessons 'n' links...</subtitle>
      <title>Learning WebGL</title>
      <updated>2010-02-09T02:20:03Z</updated>
    </source>
  </entry>

  <entry xml:lang="en">
    <id>http://learningwebgl.com/blog/?p=1695</id>
    <link href="http://learningwebgl.com/blog/?p=1695" rel="alternate" type="text/html"/>
    <title>WebGL around the net, 4 Feb 2010</title>
    <summary>For today, one new link and a couple of older things that I managed to miss first time around:

A new interview with a WebGL library creator at 3D test: this time, it’s Marco Di Benedetto on SpiderGL.  Well worth a read.
GLGE has forums!  It looks like a good place not just for discussing [...]</summary>
    <content type="xhtml"><div xmlns="http://www.w3.org/1999/xhtml"><p>For today, one new link and a couple of older things that I managed to miss first time around:</p>
<ul>
<li>A new interview with a WebGL library creator at 3D test: this time, it’s <a href="http://www.3d-test.com/interviews/webgl_spiderGL_1.htm">Marco Di Benedetto on SpiderGL</a>.  Well worth a read.</li>
<li><a href="http://www.glge.org/forum/">GLGE has forums</a>!  It looks like a good place not just for discussing the library, but also WebGL in general.</li>
<li>Peter Strohm has added more to his <a href="http://peter-strohm.de/webgl/">WebGL lessons in German</a>; there are now tutorials on adding <a href="http://peter-strohm.de/webgl/webgltutorial5.html">interaction</a>, and <a href="http://peter-strohm.de/webgl/webgltutorial6.html">textures</a>.</li>
<li><a href="http://code.google.com/p/gwt-g3d/">gwt-g3d</a>, one of the three independent projects to create a Google Web Toolkit binding for WebGL, now includes ports of my first 12 lessons as part of its demos.  I’m flattered :-) — but additionally, that suggests things have moved on quite a bit since it was announced at the end of December.  Its competitors, <a href="http://sourceforge.net/projects/wgt/">WGT</a> and <a href="http://code.google.com/p/gwtgl/">GwtGL</a> seem to be coming along nicely too.</li>
</ul></div>
    </content>
    <updated>2010-02-04T19:59:00Z</updated>
    <category term="Links"/>
    <author>
      <name>giles</name>
    </author>
    <source>
      <id>http://learningwebgl.com/blog</id>
      <link href="http://learningwebgl.com/blog/?feed=rss2" rel="self" type="application/atom+xml"/>
      <link href="http://learningwebgl.com/blog" rel="alternate" type="text/html"/>
      <subtitle>...lessons 'n' links...</subtitle>
      <title>Learning WebGL</title>
      <updated>2010-02-09T02:20:03Z</updated>
    </source>
  </entry>

  <entry xml:lang="en">
    <id>http://learningwebgl.com/blog/?p=1690</id>
    <link href="http://learningwebgl.com/blog/?p=1690" rel="alternate" type="text/html"/>
    <title>WebGL around the net, 2 Feb 2010</title>
    <summary>It looks like WebGL is going to get a bit of a push at the Game Developers’ Conference in San Francisco in March: there are three sessions highlighting it, one from Barthold Lichtenbelt of NVIDIA on OpenGL — Featuring WebGL, one from Tom Olson of Khronos on Khronos Mobile — Featuring WebGL, and one from [...]</summary>
    <content type="xhtml"><div xmlns="http://www.w3.org/1999/xhtml"><ul>
<li>It looks like WebGL is going to get a bit of a push at the Game Developers’ Conference in San Francisco in March: there are three sessions highlighting it, one from Barthold Lichtenbelt of NVIDIA on <a href="https://www.cmpevents.com/GD10/a.asp?option=C&amp;V=11&amp;SessID=10553">OpenGL — Featuring WebGL</a>, one from Tom Olson of Khronos on <a href="https://www.cmpevents.com/GD10/a.asp?option=C&amp;V=11&amp;SessID=10555">Khronos Mobile — Featuring WebGL</a>, and one from Mark Barnes of Khronos on <a href="https://www.cmpevents.com/GD10/a.asp?option=C&amp;V=11&amp;SessID=10554">COLLADA — Featuring WebGL</a>.  Interestingly, there doesn’t seem to be anything on WebGL on its own.</li>
<li>A new demo, this time from Marko Ivankovic: <a href="http://code.google.com/p/mivankovic-blog/wiki/WebGLTestpage">fractal mountain generation</a>.</li>
</ul>
<p>Finally, some good news about the next tutorial on this blog: <a href="http://learningwebgl.com/lessons/lesson14">the page for lesson 14</a> — which covers specular highlights — is pretty much done, as is <a href="http://www.youtube.com/watch?v=OSjVRA2BQJU">the video</a> for people who don’t have WebGL-enabled browsers.  Now it’s “just” a question of writing about it :-S</p></div>
    </content>
    <updated>2010-02-02T14:03:43Z</updated>
    <category term="Links"/>
    <category term="Upcoming lessons"/>
    <author>
      <name>giles</name>
    </author>
    <source>
      <id>http://learningwebgl.com/blog</id>
      <link href="http://learningwebgl.com/blog/?feed=rss2" rel="self" type="application/atom+xml"/>
      <link href="http://learningwebgl.com/blog" rel="alternate" type="text/html"/>
      <subtitle>...lessons 'n' links...</subtitle>
      <title>Learning WebGL</title>
      <updated>2010-02-09T02:20:03Z</updated>
    </source>
  </entry>

  <entry xml:lang="en">
    <id>http://learningwebgl.com/blog/?p=1680</id>
    <link href="http://learningwebgl.com/blog/?p=1680" rel="alternate" type="text/html"/>
    <title>WebGL around the net, 1 Feb 2010</title>
    <summary>A game!  From the C3DL team, 3D Asteroids.
Also from the C3DL guys, an update to keep their particle systems working, which leads neatly on to…
…this N-body simulation from Mike Bostock.  Look out, though — it works in Chrome but it crashed last night’s Minefield for me.
More good news from Paul Brunt: GLGE can [...]</summary>
    <content type="xhtml"><div xmlns="http://www.w3.org/1999/xhtml"><ul>
<li>A game!  From the C3DL team, <a href="http://www.c3dl.org/index.php/c3dl-dev/asteroids-in-3d-and-a-bit-of-2d/">3D Asteroids</a>.</li>
<li>Also from the C3DL guys, <a href="http://www.c3dl.org/index.php/c3dl-dev/another-demo-updated/">an update to keep their particle systems working</a>, which leads neatly on to…</li>
<li>…<a href="http://graphics.stanford.edu/~mbostock/physics-webgl.html">this N-body simulation</a> from Mike Bostock.  Look out, though — it works in Chrome but it crashed last night’s Minefield for me.</li>
<li>More good news from Paul Brunt: <a href="http://www.glge.org/glge-text-rendering-demo/">GLGE can now render text</a>!</li>
<li>A new real-world use for WebGL: from Helmut Dersch, <a href="http://webuser.hs-furtwangen.de/~dersch/PTViewerNG/PTViewerNG.html">DIY Streetview in WebGL</a> (and also <a href="http://www.diy-streetview.org/data/development/20100130/webgl/PTViewerNG.htm">here</a>).</li>
</ul></div>
    </content>
    <updated>2010-02-01T14:28:04Z</updated>
    <category term="Links"/>
    <author>
      <name>giles</name>
    </author>
    <source>
      <id>http://learningwebgl.com/blog</id>
      <link href="http://learningwebgl.com/blog/?feed=rss2" rel="self" type="application/atom+xml"/>
      <link href="http://learningwebgl.com/blog" rel="alternate" type="text/html"/>
      <subtitle>...lessons 'n' links...</subtitle>
      <title>Learning WebGL</title>
      <updated>2010-02-09T02:20:03Z</updated>
    </source>
  </entry>

  <entry xml:lang="en">
    <id>http://www.c3dl.org/?p=2271</id>
    <link href="http://www.c3dl.org/index.php/c3dl-dev/asteroids-in-3d-and-a-bit-of-2d/" rel="alternate" type="text/html"/>
    <title>Asteroids in 3D… and a bit of 2D</title>
    <summary>So I’ve been working on this little demo in preparation for the upcoming (very very soon…docs left only) release of C3DL’s webGL release.  I adapted it from Peter Callaghan’s Asteroids game and the models were from an old demo made as part of our user testing last year.

This demo is kind of cool because [...]</summary>
    <content type="xhtml"><div xmlns="http://www.w3.org/1999/xhtml">So I’ve been working on this little demo in preparation for the upcoming (very very soon…docs left only) release of C3DL’s webGL release.  I adapted it from Peter Callaghan’s Asteroids game and the models were from an old demo made as part of our user testing last year.

This demo is kind of cool because it demonstrates many of the features of C3DL and puts it all together in one.  Namely it uses:
<ul>
	<li>collada model loading – you can export to collada from a good number of modeling programs including blender, 3ds max and google sketchup</li>
	<li>particle systems – watch the rocks when they explode!</li>
	<li>lighting – ok… this is not featured well… I’ll try to see if I can do better with it</li>
	<li>picking – click on rock to identify which one</li>
</ul>
I will probably add something to that uses the effects system as that is one key component not highlighted.

Its not quite done yet but I figured I’d link it anyways since I won’t have time to finish it till probably Wednesday…. I still have to add scoring and end game functionalities, the keys should be changed to a and d instead of using arrows,  and I think my health bar is messed up when it drops below 25 but… its a start.  I also have not tested well on webkit (there was a bug of constantly rotating ship at one point but have not been able to reproduce it) and I can’t seem to run chrome on my desktop atm so no testing there.

This demo uses webGL in the main display and I painted the hud with 2D canvas.

Click on the asteroids to shoot them.  Use arrow keys to rotate the ship.

<a href="https://cs.senecac.on.ca/~catherine.leung/c3dl/Asteroids3/asteroids.html">https://cs.senecac.on.ca/~catherine.leung/c3dl/Asteroids3/asteroids.html
</a>
NOTE:  To see this demo you need a web browser that supports webGL and it must be enabled.</div>
    </content>
    <updated>2010-02-01T11:12:55Z</updated>
    <category term="c3dl development"/>
    <author>
      <name>Cathy Leung</name>
    </author>
    <source>
      <id>http://www.c3dl.org</id>
      <link href="http://www.c3dl.org/index.php/category/c3dl-dev/feed/" rel="self" type="application/atom+xml"/>
      <link href="http://www.c3dl.org" rel="alternate" type="text/html"/>
      <subtitle>where 3D is born!</subtitle>
      <title>Canvas 3d JS Library » c3dl development</title>
      <updated>2010-02-01T11:40:04Z</updated>
    </source>
  </entry>

  <entry xml:lang="en">
    <id>http://www.c3dl.org/?p=2259</id>
    <link href="http://www.c3dl.org/index.php/c3dl-dev/another-demo-updated/" rel="alternate" type="text/html"/>
    <title>Another demo updated</title>
    <summary>I’ve just finished updating the particle systems demo.  There wasn’t anything wrong with the demo itself, but there was an array in the particle system class that wasn’t being initialized properly that suddenly started causing Minefield to crash.  The strange thing is that until recently Minefield wasn’t complaining about it.  But as [...]</summary>
    <content type="xhtml"><div xmlns="http://www.w3.org/1999/xhtml"><p>I’ve just finished updating the <a href="http://www.c3dl.org/index.php/webgl-demos/particle-systems-demo">particle systems demo</a>.  There wasn’t anything wrong with the demo itself, but there was an array in the particle system class that wasn’t being initialized properly that suddenly started causing Minefield to crash.  The strange thing is that until recently Minefield wasn’t complaining about it.  But as of the nightly build on the 23rd it didn’t just complain, it outright crashed.  By populating the array with 0s when it is initialized, I’ve got particles working again.  The misleading problem was that this happened at the same time I was fixing an issue with colours for Chromium was preventing it from displaying.  It started crashing one browser at the same time I fixed a problem for another.  It turns out that was caused because colour values were being read out of a file and split into an array, but they were never explicitly made into floating point values.  Minefield and Webkit understood them as such anyway, but Chromium didn’t like that.  A temporary solution was to multiply the values by 1 before using them, but that isn’t a very good solution.  Once I actually traced the problem to its source, a quick parseFloat took care of it.  Slightly slower, but more correct.</p><p/></div>
    </content>
    <updated>2010-01-28T21:25:35Z</updated>
    <category term="c3dl development"/>
    <author>
      <name>peter</name>
    </author>
    <source>
      <id>http://www.c3dl.org</id>
      <link href="http://www.c3dl.org/index.php/category/c3dl-dev/feed/" rel="self" type="application/atom+xml"/>
      <link href="http://www.c3dl.org" rel="alternate" type="text/html"/>
      <subtitle>where 3D is born!</subtitle>
      <title>Canvas 3d JS Library » c3dl development</title>
      <updated>2010-02-01T11:40:04Z</updated>
    </source>
  </entry>

  <entry xml:lang="en">
    <id>http://learningwebgl.com/blog/?p=1664</id>
    <link href="http://learningwebgl.com/blog/?p=1664" rel="alternate" type="text/html"/>
    <title>WebGL around the net, 28 Jan 2010</title>
    <summary>A lot of new stuff over the last three days:

Over at 3D test, an interview with Paul Brunt of GLGE fame — well worth reading.
On thing that’s going to be important for WebGL going forward is the ability to load up objects that have been designed in 3D modeling tools like Blender, Autodesk 3D Studio [...]</summary>
    <content type="xhtml"><div xmlns="http://www.w3.org/1999/xhtml"><p>A lot of new stuff over the last three days:</p>
<ul>
<li>Over at 3D test, <a href="http://www.3d-test.com/interviews/webgl_GLGE_1.htm">an interview with Paul Brunt</a> of <a href="http://www.glge.org/">GLGE</a> fame — well worth reading.</li>
<li>On thing that’s going to be important for WebGL going forward is the ability to load up objects that have been designed in 3D modeling tools like Blender, Autodesk 3D Studio or Google SketchUp.  There are two ways of doing this — by converting the tools’ files into a JavaScript-friendly format like JSON, or by writing code to load the files directly in JavaScript.  Tim Knip has started work on the latter kind: <a href="http://techblog.floorplanner.com/2010/01/27/introducing-js3ds-a-javascript-parser-for-3ds/">js3ds, a 3DS import library for JavaScript</a>.</li>
<li>A very neat (Firefox-only) demo from murphy: <a href="http://murfy.de/read/webgl-drawWindow">using a web page as a texture on a WebGL object</a>.</li>
<li>An impressive (new?) X3DOM demo that I’ve not seen before: <a href="http://x3dom.org/x3dom/example/x3dom_shadows.xhtml">floating objects with shadows</a>.</li>
<li>Some interesting news about the WebGL spec — <a href="http://blog.vlad1.com/2009/11/06/canvasarraybuffer-and-canvasarray/">a while back</a>,  Vladimir Vukićević suggested that the <code>WebGLArrayBuffer</code> and <code>WebGL*Array</code> types would be useful outside WebGL.  He’s now started moving their definition into <a href="http://people.mozilla.com/~vladimir/jsvec/TypedArray-spec.html">a specification of their own</a>.  If this goes ahead, and they are broken out of WebGL, then we’ll probably have to change our pages once again — after all, if they’re not WebGL-specific then they won’t have “WebGL” in their names.  One to keep an eye on…</li>
<li>Finally, Benjamin DeLillo reports that <a href="http://bjartr.blogspot.com/2010/01/webglu-now-on-github.html">he’s moved WebGLU to github</a> (an excellent idea) and shows that the latest version of his library makes his 50-line demo <a href="http://bjartr.blogspot.com/2010/01/remember-that-50-line-webgl-demo-now.html">an even more impressive 25 lines</a>!</li>
</ul></div>
    </content>
    <updated>2010-01-28T14:43:44Z</updated>
    <category term="Links"/>
    <author>
      <name>giles</name>
    </author>
    <source>
      <id>http://learningwebgl.com/blog</id>
      <link href="http://learningwebgl.com/blog/?feed=rss2" rel="self" type="application/atom+xml"/>
      <link href="http://learningwebgl.com/blog" rel="alternate" type="text/html"/>
      <subtitle>...lessons 'n' links...</subtitle>
      <title>Learning WebGL</title>
      <updated>2010-02-09T02:20:03Z</updated>
    </source>
  </entry>

  <entry>
    <id>tag:blogger.com,1999:blog-1513616564513865425.post-3670440867095566457</id>
    <link href="http://bjartr.blogspot.com/feeds/3670440867095566457/comments/default" rel="replies" type="application/atom+xml"/>
    <link href="http://bjartr.blogspot.com/2010/01/remember-that-50-line-webgl-demo-now.html#comment-form" rel="replies" type="text/html"/>
    <link href="http://www.blogger.com/feeds/1513616564513865425/posts/default/3670440867095566457" rel="edit" type="application/atom+xml"/>
    <link href="http://www.blogger.com/feeds/1513616564513865425/posts/default/3670440867095566457" rel="self" type="application/atom+xml"/>
    <link href="http://bjartr.blogspot.com/2010/01/remember-that-50-line-webgl-demo-now.html" rel="alternate" type="text/html"/>
    <title>Remember that 50 line WebGL demo? Now it's 25 lines</title>
    <summary>So a while back I did a 50 line demo to show how little code it takes to make WebGL content when using the WebGLU library. Since then I've simplified and consolidated WebGLU in addition to adding new features. One of the biggest (code length wise) was to load shaders from a file and add a default shader as well as a couple other simple shaders. Hopefully WebGLU will grow to include many cool and</summary>
    <updated>2010-01-26T04:54:10Z</updated>
    <published>2010-01-26T04:11:00Z</published>
    <author>
      <name>Bjartr</name>
      <email>noreply@blogger.com</email>
      <uri>http://www.blogger.com/profile/04409321734144736503</uri>
    </author>
    <source>
      <id>tag:blogger.com,1999:blog-1513616564513865425</id>
      <author>
        <name>Bjartr</name>
        <email>noreply@blogger.com</email>
        <uri>http://www.blogger.com/profile/04409321734144736503</uri>
      </author>
      <link href="http://bjartr.blogspot.com/feeds/posts/default" rel="http://schemas.google.com/g/2005#feed" type="application/atom+xml"/>
      <link href="http://www.blogger.com/feeds/1513616564513865425/posts/default" rel="self" type="application/atom+xml"/>
      <link href="http://bjartr.blogspot.com/" rel="alternate" type="text/html"/>
      <link href="http://pubsubhubbub.appspot.com/" rel="hub" type="text/html"/>
      <subtitle>Benjamin DeLillo's Project Blog</subtitle>
      <title>One Geek's Blog</title>
      <updated>2010-02-08T20:23:49Z</updated>
    </source>
  </entry>

  <entry>
    <id>tag:blogger.com,1999:blog-1513616564513865425.post-6143343438058987974</id>
    <link href="http://bjartr.blogspot.com/feeds/6143343438058987974/comments/default" rel="replies" type="application/atom+xml"/>
    <link href="http://bjartr.blogspot.com/2010/01/long-delayed-webglu-update-some-360.html#comment-form" rel="replies" type="text/html"/>
    <link href="http://www.blogger.com/feeds/1513616564513865425/posts/default/6143343438058987974" rel="edit" type="application/atom+xml"/>
    <link href="http://www.blogger.com/feeds/1513616564513865425/posts/default/6143343438058987974" rel="self" type="application/atom+xml"/>
    <link href="http://bjartr.blogspot.com/2010/01/long-delayed-webglu-update-some-360.html" rel="alternate" type="text/html"/>
    <title>A long delayed WebGLU update, some 360 degree video, and that's just for starters</title>
    <summary>Yes, I'm still here and actively developing my WebGLU library. For those of you who don't know what WebGLU is, it's a library for making WebGL application development easy and fun with minimal coding.  Since my last post WebGLU has gone through a major refactor adding several new features and cleaning up old ones. Plus, I've got a neat demo to show off. Both a live version and a video are</summary>
    <updated>2010-01-26T04:53:27Z</updated>
    <published>2010-01-22T00:18:00Z</published>
    <author>
      <name>Bjartr</name>
      <email>noreply@blogger.com</email>
      <uri>http://www.blogger.com/profile/04409321734144736503</uri>
    </author>
    <source>
      <id>tag:blogger.com,1999:blog-1513616564513865425</id>
      <author>
        <name>Bjartr</name>
        <email>noreply@blogger.com</email>
        <uri>http://www.blogger.com/profile/04409321734144736503</uri>
      </author>
      <link href="http://bjartr.blogspot.com/feeds/posts/default" rel="http://schemas.google.com/g/2005#feed" type="application/atom+xml"/>
      <link href="http://www.blogger.com/feeds/1513616564513865425/posts/default" rel="self" type="application/atom+xml"/>
      <link href="http://bjartr.blogspot.com/" rel="alternate" type="text/html"/>
      <link href="http://pubsubhubbub.appspot.com/" rel="hub" type="text/html"/>
      <subtitle>Benjamin DeLillo's Project Blog</subtitle>
      <title>One Geek's Blog</title>
      <updated>2010-02-08T20:23:49Z</updated>
    </source>
  </entry>

  <entry>
    <id>tag:blogger.com,1999:blog-1513616564513865425.post-4534909547907606793</id>
    <link href="http://bjartr.blogspot.com/feeds/4534909547907606793/comments/default" rel="replies" type="application/atom+xml"/>
    <link href="http://bjartr.blogspot.com/2010/01/webglu-now-on-github.html#comment-form" rel="replies" type="text/html"/>
    <link href="http://www.blogger.com/feeds/1513616564513865425/posts/default/4534909547907606793" rel="edit" type="application/atom+xml"/>
    <link href="http://www.blogger.com/feeds/1513616564513865425/posts/default/4534909547907606793" rel="self" type="application/atom+xml"/>
    <link href="http://bjartr.blogspot.com/2010/01/webglu-now-on-github.html" rel="alternate" type="text/html"/>
    <title>WebGLU now on GitHub</title>
    <summary>WebGLU, the library that makes WebGL development easy, has been moved to GitHub. I've gotten several requests for this move. Seems like the move couldn't hurt, so after some deliberation I've decided to do it.

In performing this move I've cleaned up the examples folder so that it now contains working examples which include texture mapping, keyframed animation, and the videosphere. I've also</summary>
    <updated>2010-01-26T03:31:20Z</updated>
    <published>2010-01-26T03:05:00Z</published>
    <category scheme="http://www.blogger.com/atom/ns#" term="webgl"/>
    <category scheme="http://www.blogger.com/atom/ns#" term="git"/>
    <category scheme="http://www.blogger.com/atom/ns#" term="github"/>
    <category scheme="http://www.blogger.com/atom/ns#" term="webglu"/>
    <author>
      <name>Bjartr</name>
      <email>noreply@blogger.com</email>
      <uri>http://www.blogger.com/profile/04409321734144736503</uri>
    </author>
    <source>
      <id>tag:blogger.com,1999:blog-1513616564513865425</id>
      <author>
        <name>Bjartr</name>
        <email>noreply@blogger.com</email>
        <uri>http://www.blogger.com/profile/04409321734144736503</uri>
      </author>
      <link href="http://bjartr.blogspot.com/feeds/posts/default" rel="http://schemas.google.com/g/2005#feed" type="application/atom+xml"/>
      <link href="http://www.blogger.com/feeds/1513616564513865425/posts/default" rel="self" type="application/atom+xml"/>
      <link href="http://bjartr.blogspot.com/" rel="alternate" type="text/html"/>
      <link href="http://pubsubhubbub.appspot.com/" rel="hub" type="text/html"/>
      <subtitle>Benjamin DeLillo's Project Blog</subtitle>
      <title>One Geek's Blog</title>
      <updated>2010-02-08T20:23:49Z</updated>
    </source>
  </entry>

  <entry xml:lang="en">
    <id>http://learningwebgl.com/blog/?p=1651</id>
    <link href="http://learningwebgl.com/blog/?p=1651" rel="alternate" type="text/html"/>
    <title>WebGL around the net, 25 Jan 2010</title>
    <summary>Three framework updates today!

Benjamin DeLillo has been quiet for a while, but he’s been working hard on WebGLU, adding (among other things) keyframe animation, image and video-based textures, shader loading from external files, and the beginning stages of import capabilities for .obj files (a common format for 3D models).  His blog post gives more [...]</summary>
    <content type="xhtml"><div xmlns="http://www.w3.org/1999/xhtml"><p>Three framework updates today!</p>
<ul>
<li>Benjamin DeLillo has been quiet for a while, but he’s been working hard on WebGLU, adding (among other things) keyframe animation, image and video-based textures, shader loading from external files, and the beginning stages of import capabilities for .obj files (a common format for 3D models).  <a href="http://bjartr.blogspot.com/2010/01/long-delayed-webglu-update-some-360.html">His blog post gives more information</a>, including a very cool example of a 360° video.</li>
<li>Paul Brunt’s GLGE improves in leaps and bounds; <a href="http://www.glge.org/fog-now-supported/">he’s now added fog effects</a>.</li>
<li><a href="http://lindsaystanleykay.blogspot.com/2010/01/scenejs-environment-nodes.html">Lindsay Kay’s SceneJS</a> has now gained the ability to override certain rendering parameters in specific subtrees of the scene graph.</li>
</ul></div>
    </content>
    <updated>2010-01-25T19:46:06Z</updated>
    <category term="Links"/>
    <author>
      <name>giles</name>
    </author>
    <source>
      <id>http://learningwebgl.com/blog</id>
      <link href="http://learningwebgl.com/blog/?feed=rss2" rel="self" type="application/atom+xml"/>
      <link href="http://learningwebgl.com/blog" rel="alternate" type="text/html"/>
      <subtitle>...lessons 'n' links...</subtitle>
      <title>Learning WebGL</title>
      <updated>2010-02-09T02:20:03Z</updated>
    </source>
  </entry>

  <entry xml:lang="en">
    <id>http://learningwebgl.com/blog/?p=1523</id>
    <link href="http://learningwebgl.com/blog/?p=1523" rel="alternate" type="text/html"/>
    <title>WebGL Lesson 13 – per-fragment lighting and multiple programs</title>
    <summary>&lt;&lt; Lesson 12
Welcome to my number thirteen in my series of WebGL tutorials!  In it, we’ll cover per-fragment lighting, which is harder work for the graphics card than the per-vertex lighting we’ve been doing so far, but gives much more realistic results.  We’ll also look at how you can switch the shaders used [...]</summary>
    <content type="xhtml"><div xmlns="http://www.w3.org/1999/xhtml"><p><span style="float: left; padding: 0px 20px 0px 0px;"><a href="http://learningwebgl.com/blog/?p=1359">&lt;&lt; Lesson 12</a></span><br/><br/>
Welcome to my number thirteen in my series of WebGL tutorials!  In it, we’ll cover per-fragment lighting, which is harder work for the graphics card than the per-vertex lighting we’ve been doing so far, but gives much more realistic results.  We’ll also look at how you can switch the shaders used by your code by changing which WebGL program object is in use.</p>
<p>Here’s what the lesson looks like when run on a browser that supports WebGL:</p>
<p>&lt;object height="344" width="425"&gt;&lt;param name="movie" value="http://www.youtube.com/v/IKFLr-7WnEA&amp;amp;hl=en_US&amp;amp;fs=1&amp;amp;"&gt;&lt;param name="allowFullScreen" value="true"&gt;&lt;param name="allowscriptaccess" value="always"&gt;&lt;embed allowfullscreen="true" allowscriptaccess="always" height="344" src="http://www.youtube.com/v/IKFLr-7WnEA&amp;amp;hl=en_US&amp;amp;fs=1&amp;amp;" type="application/x-shockwave-flash" width="425"&gt;&lt;/embed&gt;&lt;/object&gt;</p>
<p><a href="http://learningwebgl.com/lessons/lesson13/index.html">Click here and you’ll see the live WebGL version</a>, if you’ve got a browser that supports it; <a href="http://learningwebgl.com/blog/?p=11">here’s how to get one</a> if you don’t.  You’ll see a sphere and cube orbiting; both will probably be white for a few moments while the textures load, but once that’s done you should see that the sphere is the moon and the cube a (not-to-scale) wooden crate; the scene is similar to the one we had for <a href="http://learningwebgl.com/blog/?p=1359">lesson 12</a>, but we’re closer to the orbiting objects so that you can see more clearly what they look like.  As before, both are illuminated by a point light source that is in between them, and if you want to change the light’s position, colour, etc., there are fields beneath the WebGL canvas, along with checkboxes to switch lighting on and off, to switch between per-fragment and per-pixel lighting, and to use or not use the textures.</p>
<p>Try toggling the per-fragment lighting on and off.  You should be able to see the difference on the crate pretty easily; the centre is obviously brighter with it switched on.  The difference with the moon is more subtle; the edges where the lighting fades out are smoother and less ragged with per-fragment lighting than they are with per-vertex.  You will probably be able to see this more easily if you switch off the textures.</p>
<p>More on how it all works below…</p>
<p><span id="more-1523"/></p>
<p>The usual <a href="http://learningwebgl.com/blog/?page_id=29">warning</a>: these lessons are targeted at people with a reasonable amount of programming knowledge, but no real experience in 3D graphics; the aim is to get you up and running, with a good understanding of what’s going on in the code, so that you can start producing your own 3D Web pages as quickly as possible.  If you haven’t read the previous tutorials already, you should probably do so before reading this one — here I will only explain the new stuff.  The lesson is based on <a href="http://learningwebgl.com/blog/?p=1359">lesson 12</a>, so you should make sure that you understand that one (and please do post a comment on  that post if anything’s unclear about it!)</p>
<p>There may be bugs and misconceptions in this tutorial.  If you spot anything wrong, let me know in the comments and I’ll correct it ASAP.</p>
<p>There are two ways you can get the code for this example; just “View Source” while you’re looking at the live version, or if you use GitHub, you can clone it (and the other lessons) from <a href="http://github.com/gpjt/webgl-lessons">the repository there</a>.  </p>
<p>Let’s kick off by describing exactly why it’s worth taking up more graphics processor power by coding per-fragment lighting.  <img src="http://learningwebgl.com/lessons/lesson07/point.png" style="float: left;"/>You may remember the diagram to the left from <a href="http://learningwebgl.com/blog/?p=684">lesson 7</a>.  As you know, the brightness of a surface is determined by the angle between its normal and the incoming rays of light from the light source.  Now, so far, our lighting has been calculated in the vertex shader by combining the normals specified for each vertex with the lighting direction from it.  This has provided a light-weighting factor, which we’ve passed from the vertex shader to the fragment shader in a varying variable, and there used to vary the brightness of the colour of the fragment appropriately.  This light-weighting factor, like all varying variables, will have been linearly interpolated by the WebGL system to provide values for it for the fragments that lie between the vertices; so, in the diagram, B will be quite bright because the light is parallel with the normal there, A will be dimmer because the light is reaching it at more of an angle, and points in between will shade smoothly between bright and dim.  This will look just right.</p>
<p><img src="http://learningwebgl.com/lessons/lesson13/point-lighting-in-middle.png" style="float: right;"/>But now imagine that the light is higher up, as in the diagram to the right.  A and C will be dim, as the light reaches them at an angle.  We are calculating lighting at the vertices only, and so point B will have the average brightness of A and C, so it will also be dim.  This is, of course, wrong — the light is parallel the surface’s normal at B, so it should actually be brighter than either of them.  So, in order to calculate the lighting at fragments between vertices, we obviously need to calculate it separately for each fragment.</p>
<p>Calculating the lighting for each fragment means that for each one we need its location (to work out the direction of the light) and its normal; we can get these by passing them from the vertex shader to the fragment shader.  They will both be linearly interpolated, so the positions will lie along a straight line between the vertices, and the normals will vary smoothly.  That straight line is just what we want, and because the normals at A and C are the same, the normals will be the same for all of the fragments, which is perfect too.</p>
<p>So, that all explains why the cube in our web page looks better and more realistic with per-fragment lighting.  But there’s another benefit, and this is that it gives a great effect to shapes made up of flat planes that are meant to approximate curved surfaces, like our sphere.  If the normals at two vertices are different, then the smoothly-changing normals at the intervening fragments will give the effect of a curving surface.  When considered in this way, per-fragment lighting is a form of what is called Phong shading, and <a href="http://en.wikipedia.org/wiki/File:Phong-shading-sample.jpg">this picture on Wikipedia</a> shows the effect better than I could explain in several thousand words.  You can see this in the demo; if you use per-vertex lighting, you can see that the edge of the shadow (where the point light stops having an effect and the ambient lighting takes over) looks a bit “ragged”.  This is because the sphere is made up of many triangles, and you can see their edges.  When you switch on per-fragment lighting, you can see that the edge of this transition is smoother, giving a better effect of roundness. </p>
<p>Right, that’s the theory out of the way — let’s take a look at the code!  The shaders are at the top of the file, so lets take a look at them first.  Because this example uses either per-vertex or per-fragment lighting, depending on the setting of the “per-vertex” checkbox, it has vertex and fragment shaders for each kind (it would be possible to write shaders that could do both, but they would be harder to read).  The way that we switch between them is something we’ll come to later, but for now you should just note that we distinguish between them by using different <code>id</code> tags when defining them as scripts in the web page.  The first to appear are the shaders for per-vertex lighting, and they’re exactly the same as the ones we’ve been using since lesson 7, so I will just show their <code>script</code> tags so that you can match them up with what you will see if you’re following through in the file:</p>
<pre>&lt;script id="per-vertex-lighting-fs" type="x-shader/x-fragment"&gt;
</pre>
<pre>&lt;script id="per-vertex-lighting-vs" type="x-shader/x-vertex"&gt;
</pre>
<p>Next comes the fragment shader for per-fragment lighting.</p>
<pre>&lt;script id="per-fragment-lighting-fs" type="x-shader/x-fragment"&gt;
  varying vec2 vTextureCoord;
  varying vec4 vTransformedNormal;
  varying vec4 vPosition;

  uniform bool uUseLighting;
  uniform bool uUseTextures;

  uniform vec3 uAmbientColor;

  uniform vec3 uPointLightingLocation;
  uniform vec3 uPointLightingColor;

  uniform sampler2D uSampler;

  void main(void) {
    vec3 lightWeighting;
    if (!uUseLighting) {
      lightWeighting = vec3(1.0, 1.0, 1.0);
    } else {
      vec3 lightDirection = normalize(uPointLightingLocation - vPosition.xyz);

      float directionalLightWeighting = max(dot(normalize(vTransformedNormal.xyz), lightDirection), 0.0);
      lightWeighting = uAmbientColor + uPointLightingColor * directionalLightWeighting;
    }

    vec4 fragmentColor;
    if (uUseTextures) {
      fragmentColor = texture2D(uSampler, vec2(vTextureCoord.s, 1.0 - vTextureCoord.t));
    } else {
      fragmentColor = vec4(1.0, 1.0, 1.0, 1.0);
    }
    gl_FragColor = vec4(fragmentColor.rgb * lightWeighting, fragmentColor.a);
  }
&lt;/script&gt;
</pre>
<p>You can see that this is very similar to the vertex shaders that we’ve been using so far; it does exactly the same calculations to work out the direction of the light and to then combine that with the normal to calculated a light weighting.  The difference is that the inputs to this calculation now come from varying variables rather than per-vertex attributes, and the resulting weighting is immediately combined with the texture colour from the sample rather than passed out for processing later.  It’s also worth noting that we have to normalise the varying variable that contains the interpolated normal; normalising, you will remember, adjusts a vector so that its length is one unit.  This is because interpolating between two length-one vectors does not necessarily give you a length-one vector, just a vector that points in the right direction.  Normalising them fixes that.  (Thanks to Glut for pointing that out <a href="http://learningwebgl.com/blog/?p=1523&amp;cpage=1#comment-1262">in the comments</a>.)</p>
<p>Because all of the heavy lifting is being done by the fragment shader, the vertex shader for per-fragment lighting is really simple:</p>
<pre>&lt;script id="per-fragment-lighting-vs" type="x-shader/x-vertex"&gt;
  attribute vec3 aVertexPosition;
  attribute vec3 aVertexNormal;
  attribute vec2 aTextureCoord;

  uniform mat4 uMVMatrix;
  uniform mat4 uPMatrix;
  uniform mat4 uNMatrix;

  varying vec2 vTextureCoord;
  varying vec4 vTransformedNormal;
  varying vec4 vPosition;

  void main(void) {
    vPosition = uMVMatrix * vec4(aVertexPosition, 1.0);
    gl_Position = uPMatrix * vPosition;
    vTextureCoord = aTextureCoord;
    vTransformedNormal = uNMatrix * vec4(aVertexNormal, 1.0);
  }
&lt;/script&gt;
</pre>
<p>We still need to work out the vertex’s location after the application of the model-view matrix and multiply the normal by the normal matrix, but now we just stash them away in varying variables for later use in the fragment shader. </p>
<p>That’s it for the shaders!  The rest of the code will be pretty familiar from the previous lessons, with one exception.  So far, we’ve only used one vertex shader and one fragment shader per WebGL page.  This one uses two pairs, one for per-vertex lighting and one for per-fragment lighting.  Now, you may remember from lesson 1 that the WebGL program object that we use to pass our shader code up to the graphics card can have only one fragment shader and one vertex shader.  What this means is that we need to have two programs, and switch which one we use based on the setting of the “per-fragment” checkbox.</p>
<p>The way we do this is simple; our <code>initShaders</code> function is changed to look like this:</p>
<pre>  var currentProgram;
  var perVertexProgram;
  var perFragmentProgram;
  function initShaders() {
    perVertexProgram = createProgram("per-vertex-lighting-fs", "per-vertex-lighting-vs");
    perFragmentProgram = createProgram("per-fragment-lighting-fs", "per-fragment-lighting-vs");
  }
</pre>
<p>So, we have two programs in separate global variables, one for per-vertex lighting and one for per-fragment, and a separate <code>currentProgram</code> variable to store the one that’s currently in use.  The <code>createProgram</code> we use to create them is simply a parameterised version of the code we used to have in <code>initShaders</code>, so I won’t duplicate it here.</p>
<p>We then switch in the appropriate program right at the start of the <code>drawScene</code> function: </p>
<pre>  function drawScene() {
    gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)

    perspective(45, 1.0, 0.1, 100.0);

    var perFragmentLighting = document.getElementById("per-fragment").checked;
    if (perFragmentLighting) {
      currentProgram = perFragmentProgram;
    } else {
      currentProgram = perVertexProgram;
    }
    gl.useProgram(currentProgram);
</pre>
<p>We have to do this before anything else because when we do drawing code (for example setting uniforms or attaching buffers of per-vertex attributes to attributes) we need the current program to be appropriately set up, as otherwise we might use the wrong program:</p>
<pre>    var lighting = document.getElementById("lighting").checked;
    gl.uniform1i(currentProgram.useLightingUniform, lighting);
</pre>
<p>You can see that this means that for each call to <code>drawScene</code> we use one and only one program; it differs only between calls.  If you’re wondering whether or not you could use different shader programs at different times within <code>drawScene</code>, so that different parts of the scene were drawn with different ones — perhaps some of your scene might use per-vertex lighting and some per-pixel — the answer is yes!  It wasn’t needed for this example, but is perfectly valid and can be useful.</p>
<p>Anyway — with that explained, that’s it for this lesson!  You now know how to use multiple programs to switch shaders, and how to code per-pixel lighting.  Next time we’ll look at the last bit of lighting that was mentioned in lesson 7: specular highlights.</p>
<p><span style="float: left; padding: 0px 20px 0px 0px;"><a href="http://learningwebgl.com/blog/?p=1359">&lt;&lt; Lesson 12</a></span><br/></p>
<p><em>Acknowledgments: As before, the texture-map for the moon <a href="http://maps.jpl.nasa.gov/">comes from NASA’s JPL website</a>, and the code to generate a sphere is based on <a href="https://cvs.khronos.org/svn/repos/registry/trunk/public/webgl/sdk/demos/webkit/Earth.html">this demo</a>, which was originally by the WebKit team.  Many thanks to both!</em></p></div>
    </content>
    <updated>2010-01-24T01:13:51Z</updated>
    <category term="Lessons"/>
    <author>
      <name>giles</name>
    </author>
    <source>
      <id>http://learningwebgl.com/blog</id>
      <link href="http://learningwebgl.com/blog/?feed=rss2" rel="self" type="application/atom+xml"/>
      <link href="http://learningwebgl.com/blog" rel="alternate" type="text/html"/>
      <subtitle>...lessons 'n' links...</subtitle>
      <title>Learning WebGL</title>
      <updated>2010-02-09T02:20:03Z</updated>
    </source>
  </entry>

  <entry xml:lang="en">
    <id>http://www.c3dl.org/?p=2156</id>
    <link href="http://www.c3dl.org/index.php/c3dl-dev/simplifying-the-interface/" rel="alternate" type="text/html"/>
    <title>Simplifying the Interface</title>
    <summary>Something we need to do is simplify the C3DL library interface. When approaching such tasks, one method I found extremely useful was to analyze the code in terms of the interface, or that is from the user’s perspective. To do this I first created a basic demo of a spinning cube by writing the necessary [...]</summary>
    <content type="xhtml"><div xmlns="http://www.w3.org/1999/xhtml">Something we need to do is simplify the C3DL library interface. When approaching such tasks, one method I found extremely useful was to analyze the code in terms of the interface, or that is from the user’s perspective. To do this I first created a basic demo of a spinning cube by writing the necessary HTML and JavaScript files. As for the HTML, it will need to be something like this:<br/>
(I emphasized the code which potentially could be changed)

<pre>&lt;html&gt;

  &lt;head&gt;
    &lt;title&gt;Cavas3D Demo&lt;/title&gt;
    <b>&lt;script&gt;var SCRIPT_PATH = '../../canvas3dapi/'&lt;/script&gt;</b>
    &lt;script src="../../canvas3dapi/c3dapi.js"&gt;&lt;/script&gt;
    <b>&lt;script src="basic_demo.js"&gt;&lt;/script&gt;</b>
  &lt;/head&gt;

  &lt;body&gt;
    <b>&lt;canvas id="demo" width="500" height="500"&gt;&lt;/canvas&gt;</b>
  &lt;/body&gt;

&lt;/html&gt;</pre>

It would be great if this could be simplified to something like this:

<pre>&lt;html&gt;

  &lt;head&gt;
    &lt;title&gt;Cavas3D Demo&lt;/title&gt;
    &lt;script src="../../canvas3dapi/c3dapi.js"&gt;&lt;/script&gt;
  &lt;/head&gt;

  &lt;body&gt;
    <b>&lt;canvas datasrc="basic_demo.js" id="demo" width="500" height="500"&gt;
    &lt;/canvas&gt;</b>
  &lt;/body&gt;

&lt;/html&gt;</pre>

I removed the somewhat unintuitive SCRIPT_PATH variable and moved the basic_demo.js resource into the canvas tag making it more obvious which .js file is associated to which canvas. These changes haven’t actually been made, I’m just playing with possible changes, brainstorming which parts <i>might</i> be able to be modified.<br/>
<br/>
Now, very few changes were made here and it will probably require a significant amount of effort to make it work. But this work is justified by the fact that the user’s experience and first impression will be more positive. If the user has to spend more than a few minutes trying to get a simple example to render, they’ll probably look for alternatives.<br/>
<br/>
Let’s see how the JavaScript could be changed. Right now, you need to write something like this:<br/>
(Again, I emphasized the code which potentially could be changed)

<pre><b>c3dl.addModel('cube.dae');
c3dl.addMainCallBack(mainDemo, 'demo');</b>

function mainDemo(canvasName)
{
  var scn = new c3dl.Scene();		
  <b>scn.setCanvasTag(canvasName);
  var renderer = new c3dl.OpenGLES20();
  scn.setRenderer(renderer);
  scn.init();</b>
  
  var cam = new c3dl.FreeCamera();
  cam.setPosition([0,0,50]);
  cam.setLookAtPoint([0,0,-1]);

  var cube = new c3dl.Collada();
  cube.init('cube.dae');
  cube.setAngularVel([0.001,0.001,0.0]);

  scn.addObjectToScene(cube);
  scn.setCamera(cam);
  scn.startScene();
}</pre>

Some changes I had in mind involved getting rid of the global c3dl.addModel() and c3dl.addMainCallback() calls and changing OpenGLES20 to just Renderer. The Renderer is an interesting problem. When <a href="http://www.khronos.org/webgl/">WebGL</a> started out as <a href="https://wiki.mozilla.org/Canvas:3D">Canvas3D</a>, we called the renderer OpenGLES11, then later it became OpenGLES20 and now it <i>could probably</i> be WebGL. Considering how much it has changed and will change, we’ll have to invest some time to make this stop. One simple solution is to abstract the renderer.<br/>
<br/>
For example, if we change OpenGLES20 class to Renderer, the user will no longer need to be concerned what underlying rendering method is used. If a visitor loads the user’s demo, but visitor’s browser doesn’t support WebGL (i.e. it’s I.E.), the demo still loads. This of course only works if we accommodate for this case. So if the browser is IE, we use DirectX. If the browser supports WebGL, we use that. The end result is the user’s page will create the appropriate renderer without them needing to worry about the user’s browser. So let’s look at what lines changed:

<pre>function mainDemo(canvasName)
{
  var scn = new c3dl.Scene();
  <b>var renderer = new c3dl.Renderer();
  scn.init(renderer);</b>
  
  var cam = new c3dl.FreeCamera();
  cam.setPosition([0,0,50]);
  cam.setLookAtPoint([0,0,-1]);

  var cube = new c3dl.Collada();
  cube.init('cube.dae');
  cube.setAngularVel([0.001,0.001,0.001]);

  scn.addObjectToScene(cube);  
  scn.setCamera(cam);
  scn.startScene();
}</pre>

In terms of the lines trimmed, I’m not certain how much of it is viable. The reason some of the seemingly redundant code is required was because I couldn’t devise any alternative at the time. Looking at it from a different perspective, I should be able to come up with something. In the end we’ll have an interface which is more flexible, easier to understand and of course simple.</div>
    </content>
    <updated>2010-01-23T22:37:24Z</updated>
    <category term="c3dl development"/>
    <author>
      <name>Andor Salga</name>
    </author>
    <source>
      <id>http://www.c3dl.org</id>
      <link href="http://www.c3dl.org/index.php/category/c3dl-dev/feed/" rel="self" type="application/atom+xml"/>
      <link href="http://www.c3dl.org" rel="alternate" type="text/html"/>
      <subtitle>where 3D is born!</subtitle>
      <title>Canvas 3d JS Library » c3dl development</title>
      <updated>2010-02-01T11:40:04Z</updated>
    </source>
  </entry>

  <entry xml:lang="en">
    <id>http://learningwebgl.com/blog/?p=1606</id>
    <link href="http://learningwebgl.com/blog/?p=1606" rel="alternate" type="text/html"/>
    <title>Retrospective changes: storing attributes and uniforms on the program</title>
    <summary>For lesson 13, I needed to be able to easily switch the WebGL program object (which manages the shaders) so that you can see the effects of per-vertex or per-pixel lighting just by toggling a checkbox.  Obviously, changing which program you’re using means that the code that draws the scene needs to use the [...]</summary>
    <content type="xhtml"><div xmlns="http://www.w3.org/1999/xhtml"><p>For <a href="http://learningwebgl.com/lessons/lesson13/">lesson 13</a>, I needed to be able to easily switch the WebGL program object (which manages the shaders) so that you can see the effects of per-vertex or per-pixel lighting just by toggling a checkbox.  Obviously, changing which program you’re using means that the code that draws the scene needs to use the current program’s attribute and uniform locations.  One easy way to do this was to keep the attribute and the uniform locations as fields of the program object; this not only made it easier to switch everything across in one go by just changing a <code>currentProgram</code> object, but it also gave me a chance to change things so that we get the uniform locations in one go while initialising, instead of getting them every repaint; as <a href="http://ewgl.wordpress.com/2010/01/11/uniforms/">Easy WebGL pointed out the other day</a>, getting uniforms can be costly in terms of processor power, so it’s worth avoiding doing it unnecessarily.</p>
<p>Anyhow, this was such a nice change that I decided to push it back into the older lessons.  Here’s an example of what the new code looks like — first, the new <code>initShaders</code> from <a href="http://learningwebgl.com/blog/?p=28">lesson 1</a>:</p>
<pre>  var shaderProgram;
  function initShaders() {
    var fragmentShader = getShader(gl, "shader-fs");
    var vertexShader = getShader(gl, "shader-vs");

    shaderProgram = gl.createProgram();
    gl.attachShader(shaderProgram, vertexShader);
    gl.attachShader(shaderProgram, fragmentShader);
    gl.linkProgram(shaderProgram);

    if (!gl.getProgramParameter(shaderProgram, gl.LINK_STATUS)) {
      alert("Could not initialise shaders");
    }

    gl.useProgram(shaderProgram);

    <span style="color: red;">shaderProgram.</span>vertexPositionAttribute = gl.getAttribLocation(shaderProgram, "aVertexPosition");
    gl.enableVertexAttribArray(<span style="color: red;">shaderProgram</span>.vertexPositionAttribute);

    <span style="color: red;">shaderProgram.pMatrixUniform = gl.getUniformLocation(shaderProgram, "uPMatrix");
    shaderProgram.mvMatrixUniform = gl.getUniformLocation(shaderProgram, "uMVMatrix");</span>
  }
</pre>
<p>So, you can see that it stores the attributes and uniforms as fields in the program object.  This means that <code>setMatrixUniforms</code> can be simplified, as it no longer needs to look up uniforms:</p>
<pre>  function setMatrixUniforms() {
    gl.uniformMatrix4fv(<span style="color: red;">shaderProgram.pMatrixUniform</span>, false, new WebGLFloatArray(pMatrix.flatten()));
    gl.uniformMatrix4fv(<span style="color: red;">shaderProgram.mvMatrixUniform</span>, false, new WebGLFloatArray(mvMatrix.flatten()));
  }
</pre>
<p>…and also, the code in <code>drawScene</code> needs a small adjustment to pick up the attribute locations from the program object rather than the no-longer-existent global variables:</p>
<pre>    gl.bindBuffer(gl.ARRAY_BUFFER, triangleVertexPositionBuffer);
    gl.vertexAttribPointer(<span style="color: red;">shaderProgram.</span>vertexPositionAttribute, triangleVertexPositionBuffer.itemSize, gl.FLOAT, false, 0, 0);
    setMatrixUniforms();
    gl.drawArrays(gl.TRIANGLES, 0, triangleVertexPositionBuffer.numItems);
</pre>
<p>Right, hopefully I can get round to writing up lesson 13 properly now, it’s been too long coming!</p></div>
    </content>
    <updated>2010-01-22T14:39:00Z</updated>
    <category term="Retrospective changes"/>
    <author>
      <name>giles</name>
    </author>
    <source>
      <id>http://learningwebgl.com/blog</id>
      <link href="http://learningwebgl.com/blog/?feed=rss2" rel="self" type="application/atom+xml"/>
      <link href="http://learningwebgl.com/blog" rel="alternate" type="text/html"/>
      <subtitle>...lessons 'n' links...</subtitle>
      <title>Learning WebGL</title>
      <updated>2010-02-09T02:20:03Z</updated>
    </source>
  </entry>

  <entry xml:lang="en">
    <id>http://learningwebgl.com/blog/?p=1565</id>
    <link href="http://learningwebgl.com/blog/?p=1565" rel="alternate" type="text/html"/>
    <title>WebGL around the net, 21 Jan 2010</title>
    <summary>Lots of YouTube stuff today:

Another very cool graphics-card overloading page from the demo scene guys, who specialise in creating very cool scenes using jaw-droppingly tiny amounts of code, all in the fragment shaders.  This one’s called Four Dollar Plastic Laminator, it’s apparently a 1k demo (meaning it’s all coded in just one kilobyte of [...]</summary>
    <content type="xhtml"><div xmlns="http://www.w3.org/1999/xhtml"><p>Lots of YouTube stuff today:</p>
<ul>
<li>Another very cool graphics-card overloading page from the demo scene guys, who specialise in creating very cool scenes using jaw-droppingly tiny amounts of code, all in the fragment shaders.  This one’s called <a href="http://wakaba.c3.cx/w/four_dollar_plastic_laminator">Four Dollar Plastic Laminator</a>, it’s apparently a 1k demo (meaning it’s all coded in just one kilobyte of fragment shader!), and if your GPU’s not powerful enough to view it, <a href="http://www.youtube.com/watch?v=COluewhIUG4">you can see it on YouTube here</a>.</li>
<li><a href="http://www.youtube.com/watch?v=16P6_e7VUmw">A very cool demo</a> in a different way; this one is unfortunately only on YouTube, but it shows a model taken from World of Warcraft, exported to X3D, and then displayed in a Web browser using <a href="http://www.x3dom.org/">X3DOM</a>, which is built on WebGL.  Very impressive!</li>
<li>Finally, <a href="http://www.youtube.com/watch?v=E07lHZv3Wqk">two</a> <a href="http://www.youtube.com/watch?v=xSGJkSCimro">examples</a> of terrain mapping in WebGL, which both look pretty promising.</li>
</ul></div>
    </content>
    <updated>2010-01-21T20:08:49Z</updated>
    <category term="Links"/>
    <author>
      <name>giles</name>
    </author>
    <source>
      <id>http://learningwebgl.com/blog</id>
      <link href="http://learningwebgl.com/blog/?feed=rss2" rel="self" type="application/atom+xml"/>
      <link href="http://learningwebgl.com/blog" rel="alternate" type="text/html"/>
      <subtitle>...lessons 'n' links...</subtitle>
      <title>Learning WebGL</title>
      <updated>2010-02-09T02:20:03Z</updated>
    </source>
  </entry>

  <entry xml:lang="en">
    <id>http://learningwebgl.com/blog/?p=1569</id>
    <link href="http://learningwebgl.com/blog/?p=1569" rel="alternate" type="text/html"/>
    <title>Do textures work for you?</title>
    <summary>Apparently, gl.enable(gl.TEXTURE_2D) is invalid WebGL, and is absolutely not needed (according to the spec) to make textures work.  
However, pipy says in the comments that, at least for him, textures don’t appear without it.  Both pipy and Kenneth Russell suspect that the cause might be the graphics driver. [UPDATE: sounds like that was [...]</summary>
    <content type="xhtml"><div xmlns="http://www.w3.org/1999/xhtml"><p>Apparently, <code>gl.enable(gl.TEXTURE_2D)</code> is invalid WebGL, and is absolutely not needed (according to the spec) to make textures work.  </p>
<p>However, <a href="http://learningwebgl.com/blog/?p=684&amp;cpage=1#comment-833">pipy says in the comments</a> that, at least for him, textures don’t appear without it.  Both pipy and <a href="http://www.khronos.org/webgl/public-mailing-list/archives/1001/msg00178.html">Kenneth Russell</a> suspect that the cause might be the graphics driver. [UPDATE: sounds like that was it: he's managed to find an updated driver that fixes it for him. <a href="http://learningwebgl.com/blog/?p=1569&amp;cpage=1#comment-1054">See his comment for more</a>.]</p>
<p>If this is a widespread problem then until the drivers are fixed, it needs to be addressed by people building WebGL pages (or maybe even in the browsers).  So it would be great to get a sample of how many people are affected: if you have a WebGL-enabled browser, could you <a href="http://learningwebgl.com/lessons/lesson07/index.html">have a quick look at this demo</a>, and then post a comment here saying whether or not you see the wooden crate texture on the spinning cube, and what OS/graphics card/browser combination you’re using?  Both positive and negative results much appreciated!</p></div>
    </content>
    <updated>2010-01-21T13:37:24Z</updated>
    <category term="Troubleshooting"/>
    <author>
      <name>giles</name>
    </author>
    <source>
      <id>http://learningwebgl.com/blog</id>
      <link href="http://learningwebgl.com/blog/?feed=rss2" rel="self" type="application/atom+xml"/>
      <link href="http://learningwebgl.com/blog" rel="alternate" type="text/html"/>
      <subtitle>...lessons 'n' links...</subtitle>
      <title>Learning WebGL</title>
      <updated>2010-02-08T02:40:03Z</updated>
    </source>
  </entry>

  <entry xml:lang="en">
    <id>http://www.c3dl.org/?p=2146</id>
    <link href="http://www.c3dl.org/index.php/c3dl-dev/updating-demos/" rel="alternate" type="text/html"/>
    <title>Updating Demos</title>
    <summary>I’m continuing to update the demos with the copy of the library that works cross-browser (and will continue to do so).  There was a slight delay in the motion capture demo due to the difference in how the browsers read xml files.  We had been using:

xmlDoc = document.implementation.createDocument("","",null);
xmlDoc.async = false;
xmlDoc.load(xmlFile);

but Safari and Chrome [...]</summary>
    <content type="xhtml"><div xmlns="http://www.w3.org/1999/xhtml"><p>I’m continuing to update the demos with the copy of the library that works cross-browser (and will continue to do so).  There was a slight delay in the motion capture demo due to the difference in how the browsers read xml files.  We had been using:</p>
<pre>xmlDoc = document.implementation.createDocument("","",null);
xmlDoc.async = false;
xmlDoc.load(xmlFile);
</pre>
<p>but Safari and Chrome don’t accept that.  After a quick search (ignoring the pages that make it sound more complicated than it is and want to sell you code to take care of it) I found the answer to be quite simple.</p>
<pre>xmlDoc = document.implementation.createDocument("","",null);
xmlDoc.async = false;
var xmlReq = new XMLHttpRequest();
xmlReq.open("GET", xmlFile, false);
xmlReq.send(null);
xmlDoc=xmlReq.responseXML;
</pre>
<p>It ends up as a few extra lines, but it works for all three.  This was a problem with the code for that individual page, but it seems to be the kind of thing we can expect when trying to write complex code for multiple browsers.</p>

<p>I’ll be working on the particle systems demo next, as it also has a bit of code that is firefox only.</p></div>
    </content>
    <updated>2010-01-19T15:40:09Z</updated>
    <category term="c3dl development"/>
    <author>
      <name>peter</name>
    </author>
    <source>
      <id>http://www.c3dl.org</id>
      <link href="http://www.c3dl.org/index.php/category/c3dl-dev/feed/" rel="self" type="application/atom+xml"/>
      <link href="http://www.c3dl.org" rel="alternate" type="text/html"/>
      <subtitle>where 3D is born!</subtitle>
      <title>Canvas 3d JS Library » c3dl development</title>
      <updated>2010-02-01T11:40:04Z</updated>
    </source>
  </entry>

  <entry xml:lang="en">
    <id>http://learningwebgl.com/blog/?p=1558</id>
    <link href="http://learningwebgl.com/blog/?p=1558" rel="alternate" type="text/html"/>
    <title>WebGL around the net, 18 Jan 2010</title>
    <summary>More news from Lindsay Kay about SceneJS — he’s just released version 0.4.0, which has some serious performance improvements, most interestingly including a replacement of the Sylvester matrix/vector library with some streamlined 3D-specific code.  Sounds very promising!
Paul Brunt’s GLGE framework is also moving rapidly forward, and now supports picking — that is, the ability [...]</summary>
    <content type="xhtml"><div xmlns="http://www.w3.org/1999/xhtml"><ul>
<li>More news from Lindsay Kay about <a href="http://www.scenejs.com/">SceneJS</a> — he’s just released version 0.4.0, which has some serious performance improvements, most interestingly including a replacement of the Sylvester matrix/vector library with some streamlined 3D-specific code.  Sounds very promising!</li>
<li>Paul Brunt’s GLGE framework is also moving rapidly forward, and <a href="http://www.paulbrunt.co.uk/2010/01/17/webglpicking-in-glge/">now supports picking</a> — that is, the ability for the user to select an object in the 3D scene, which is essential for most serious 3D apps.</li>
<li>News about the C3DL library too: it’s now <a href="http://www.c3dl.org/index.php/c3dl-news/cross-browser-progress-update/">working cross-browser</a>.</li>
<li>On the subject of established 3D systems, there seems to be a report percolating around a couple of German <a href="http://www.virtual-world.info/content/view/1174/130/">news</a> <a href="http://jetzt.sueddeutsche.de/texte/anzeigen/495268">sites</a> that Second Life 2.0 will be rewritten in WebGL.  Unfortunately the only reference cited is <a href="https://blogs.secondlife.com/community/features/blog/2010/01/12/responding-to-your-feedback">a blog post that doesn’t mention it</a>, so maybe someone got hold of the wrong end of the stick.   Or maybe the combination of my schoolboy German plus Google Translate is leading me astray when I try to read the articles.  Does anyone know any more? [UPDATE <a href="http://www.anty.info/">anty</a> explains in the comments here that the author of the first article explains in the comments there that he believes that this will happen, but has no hard evidence.]</li>
</ul></div>
    </content>
    <updated>2010-01-18T18:27:44Z</updated>
    <category term="Links"/>
    <author>
      <name>giles</name>
    </author>
    <source>
      <id>http://learningwebgl.com/blog</id>
      <link href="http://learningwebgl.com/blog/?feed=rss2" rel="self" type="application/atom+xml"/>
      <link href="http://learningwebgl.com/blog" rel="alternate" type="text/html"/>
      <subtitle>...lessons 'n' links...</subtitle>
      <title>Learning WebGL</title>
      <updated>2010-02-04T20:00:04Z</updated>
    </source>
  </entry>

  <entry xml:lang="en">
    <id>http://learningwebgl.com/blog/?p=1552</id>
    <link href="http://learningwebgl.com/blog/?p=1552" rel="alternate" type="text/html"/>
    <title>WebGL around the net, 15 Jan 2010</title>
    <summary>Two updates today:

Eric Shepherd’s tutorial on WebGL at the Mozilla Developer Center, which I think only started a week ago, is progressing by leaps and bounds: so far, he’s covered similar ground to the first seven lessons here, but his latest is very impressive indeed: how to animate textures by mapping an Ogg video onto [...]</summary>
    <content type="xhtml"><div xmlns="http://www.w3.org/1999/xhtml"><p>Two updates today:</p>
<ul>
<li>Eric Shepherd’s <a href="https://developer.mozilla.org/en/WebGL">tutorial on WebGL at the Mozilla Developer Center</a>, which I think only started <a href="http://learningwebgl.com/blog/?p=1500">a week ago</a>, is progressing by leaps and bounds: so far, he’s covered similar ground to the first seven lessons here, but his latest is very impressive indeed: how to <a href="https://developer.mozilla.org/en/WebGL/Animating_textures_in_WebGL">animate textures by mapping an Ogg video onto the faces of a rotating cube</a>.  Cool stuff! — though, perhaps unsurprisingly, it doesn’t work in Chrome yet…  If you’re following the lessons here (or even if you’re not :-) I’d definitely recommend you follow Eric’s stuff too.</li>
<li>Inigo Quilez has been adding new shader demos to <a href="http://www.iquilezles.org/apps/shadertoy/">the shader toy page</a> he released a little while back: most impressive is the Mandelbulb (a sort of 3D Mandelbrot set), which drew some incredibly detailed pictures on my desktop machine before it crashed the graphics card :-S</li>
</ul>
<p>Have a great weekend!</p></div>
    </content>
    <updated>2010-01-15T18:50:13Z</updated>
    <category term="Links"/>
    <author>
      <name>giles</name>
    </author>
    <source>
      <id>http://learningwebgl.com/blog</id>
      <link href="http://learningwebgl.com/blog/?feed=rss2" rel="self" type="application/atom+xml"/>
      <link href="http://learningwebgl.com/blog" rel="alternate" type="text/html"/>
      <subtitle>...lessons 'n' links...</subtitle>
      <title>Learning WebGL</title>
      <updated>2010-02-02T16:20:05Z</updated>
    </source>
  </entry>

  <entry xml:lang="en">
    <id>http://www.c3dl.org/?p=2114</id>
    <link href="http://www.c3dl.org/index.php/c3dl-news/cross-browser-progress-update/" rel="alternate" type="text/html"/>
    <title>Cross-browser progress update</title>
    <summary>I finally have our library working properly on Firefox (Gecko/20100114 Minefield/3.7a1pre), Safari (Version 4.0.4 (5531.21.10, r53178)) and Chrome (Chromium 4.0.299.0 (36242))

I’ve updated the orbiter demo with the new code, and will shortly be updating some of the older demos.  There are still a few minor issues to deal with, such as the moon not [...]</summary>
    <content type="xhtml"><div xmlns="http://www.w3.org/1999/xhtml"><p>I finally have our library working properly on Firefox (Gecko/20100114 Minefield/3.7a1pre), Safari (Version 4.0.4 (5531.21.10, r53178)) and Chrome (Chromium 4.0.299.0 (36242))</p>

<p>I’ve updated the <a href="http://www.c3dl.org/index.php/webgl-demos/cross-browser-orbiter/">orbiter demo</a> with the new code, and will shortly be updating some of the older demos.  There are still a few minor issues to deal with, such as the moon not showing up properly sometimes, but we’re not getting exceptions anymore.</p>

<p>Another of these issues (Chrome only) has to do with reading floating point values from collada files.  When we try to use the values, it throws an exception, but if I multiply the values by 1 before we use them, it works.  I’ll have to spend a little more time trying to figure out if this is caused by something I’m doing wrong, or if it might be a bug.</p><p/></div>
    </content>
    <updated>2010-01-15T18:28:33Z</updated>
    <category term="C3DL News"/>
    <category term="c3dl development"/>
    <author>
      <name>peter</name>
    </author>
    <source>
      <id>http://www.c3dl.org</id>
      <link href="http://www.c3dl.org/index.php/category/c3dl-dev/feed/" rel="self" type="application/atom+xml"/>
      <link href="http://www.c3dl.org" rel="alternate" type="text/html"/>
      <subtitle>where 3D is born!</subtitle>
      <title>Canvas 3d JS Library » c3dl development</title>
      <updated>2010-02-01T11:40:04Z</updated>
    </source>
  </entry>

  <entry xml:lang="en">
    <id>http://learningwebgl.com/blog/?p=1534</id>
    <link href="http://learningwebgl.com/blog/?p=1534" rel="alternate" type="text/html"/>
    <title>Changing back and forth…</title>
    <summary>As they say, it’s six of one and half a dozen of the other… I’ve made some more retrospective changes.  The good news first:
On the WebGL Wiki, Gman just removed the code saying gl.enable(gl.TEXTURE_2D) from the tutorial, saying that it’s not required, or indeed valid WebGL!  I checked on the mailing list, and [...]</summary>
    <content type="xhtml"><div xmlns="http://www.w3.org/1999/xhtml"><p>As they say, it’s six of one and half a dozen of the other… I’ve made some more retrospective changes.  The good news first:</p>
<p>On the <a href="http://www.khronos.org/webgl/wiki/Main_Page">WebGL Wiki</a>, <a href="http://www.khronos.org/webgl/wiki_1_15/index.php?title=User:Gman">Gman</a> just <a href="http://www.khronos.org/webgl/wiki_1_15/index.php?title=Tutorial&amp;diff=132&amp;oldid=67">removed</a> the code saying <code>gl.enable(gl.TEXTURE_2D)</code> from the tutorial, saying that it’s not required, or indeed valid WebGL!  I checked on the mailing list, and that’s entirely correct.  The <code>TEXTURE_2D</code> constant was misplaced in the spec, which might have been the cause of the confusion.  So I’ve removed that from all the lessons, and you can get rid of it from your code too :-)</p>
<p>From the good news to the neutral news: a number of people who were running Safari on Macs found that they couldn’t see the texture, or indeed the lighting, for lessons <a href="http://learningwebgl.com/blog/?p=1253">11</a> and <a href="http://learningwebgl.com/blog/?p=1359">12</a>.  I’ve finally worked out why: Safari doesn’t like PNG textures, or at least the specific PNGs I was using.  I need to put together a good test to reproduce this so that I can report it to the WebKit team, but for now I’ve switched to using GIFs for all of my textures, and you may want to think of doing likewise.  A problem, but there’s an easy workaround for everyone to use in the meantime.</p>
<p>From the neutral news to the, well, not so great.  As I <a href="http://learningwebgl.com/blog/?p=1508">said</a> the other day, WebKit <a href="https://bugs.webkit.org/show_bug.cgi?id=32620">doesn’t seem to support</a> the <code>flipY</code> flag for <code>texImage2D</code>.  So I’ve backed out the <a href="http://learningwebgl.com/blog/?p=1487">change</a> that I’d made to use that, and once again the textures are being flipped by code in the fragment shaders.</p></div>
    </content>
    <updated>2010-01-14T19:28:02Z</updated>
    <category term="Retrospective changes"/>
    <category term="Troubleshooting"/>
    <author>
      <name>giles</name>
    </author>
    <source>
      <id>http://learningwebgl.com/blog</id>
      <link href="http://learningwebgl.com/blog/?feed=rss2" rel="self" type="application/atom+xml"/>
      <link href="http://learningwebgl.com/blog" rel="alternate" type="text/html"/>
      <subtitle>...lessons 'n' links...</subtitle>
      <title>Learning WebGL</title>
      <updated>2010-02-01T16:00:03Z</updated>
    </source>
  </entry>

  <entry xml:lang="en">
    <id>http://learningwebgl.com/blog/?p=1528</id>
    <link href="http://learningwebgl.com/blog/?p=1528" rel="alternate" type="text/html"/>
    <title>WebGL around the net, 14 Jan 2010</title>
    <summary>Two new links for today:

I’d been wondering how long it would be before someone put together a multi-player WebGL game (or at least demo); it looks like Peter Strohm has managed to get the first one done! (Minefield only right now.)
Don’t know how I missed this one on the EWGL blog — some musings on [...]</summary>
    <content type="xhtml"><div xmlns="http://www.w3.org/1999/xhtml"><p>Two new links for today:</p>
<ul>
<li>I’d been wondering how long it would be before someone put together <a href="http://petor.bplaced.net/webgl/multiuser/">a multi-player WebGL game (or at least demo)</a>; it looks like Peter Strohm has managed to get the first one done! (Minefield only right now.)</li>
<li>Don’t know how I missed this one on the EWGL blog — <a href="http://ewgl.wordpress.com/2010/01/11/uniforms/">some musings on the costliness of looking up uniform locations</a>, explaining why you shouldn’t do it every time you repaint the canvas (which is something my own code is definitely guilty of).  Useful stuff, and perhaps reason enough for another round of retrospective changes here…</li>
</ul>
<p>On the subject of the lessons, I’ve finished the first cut of the code for <a href="http://learningwebgl.com/lessons/lesson13/">lesson 13, which introduces per-fragment lighting</a>.  Let me know what you think!</p></div>
    </content>
    <updated>2010-01-14T18:44:17Z</updated>
    <category term="Links"/>
    <author>
      <name>giles</name>
    </author>
    <source>
      <id>http://learningwebgl.com/blog</id>
      <link href="http://learningwebgl.com/blog/?feed=rss2" rel="self" type="application/atom+xml"/>
      <link href="http://learningwebgl.com/blog" rel="alternate" type="text/html"/>
      <subtitle>...lessons 'n' links...</subtitle>
      <title>Learning WebGL</title>
      <updated>2010-01-29T16:40:03Z</updated>
    </source>
  </entry>

  <entry xml:lang="en">
    <id>http://learningwebgl.com/blog/?p=1512</id>
    <link href="http://learningwebgl.com/blog/?p=1512" rel="alternate" type="text/html"/>
    <title>WebGL around the net, 13 Jan 2010</title>
    <summary>Today we’ve three bits of news about frameworks that make WebGL programming easier.

Paul Brunt has managed to add shadows to GLGE!  Shadows require some fairly complex trickery with frame buffers to work properly, and unfortunately part of that isn’t implemented yet in Firefox (or, it seems, in the other WebGL implementations).  However, Paul’s [...]</summary>
    <content type="xhtml"><div xmlns="http://www.w3.org/1999/xhtml"><p>Today we’ve three bits of news about frameworks that make WebGL programming easier.</p>
<ul>
<li>Paul Brunt has managed to <a href="http://www.glge.org/implemented_shadows/">add shadows to GLGE</a>!  Shadows require some fairly complex trickery with frame buffers to work properly, and unfortunately part of that isn’t implemented yet in Firefox (or, it seems, in the other WebGL implementations).  However, Paul’s found a workaround, and the result is pretty impressive!</li>
<li>Lindsay Kay has <a href="http://lindsaystanleykay.blogspot.com/2010/01/collada-parser-for-scenejs.html">released version 0.3.0 of SceneJS</a>.  This includes a framework to make it easy to import “assets” — that is, pre-packaged files containing the details of objects including their vertex positions, normals, and so on — into your scene.  So far it only supports JSON format, but Lindsay’s working on COLLADA, which is supported by many 3D design tools. [UPDATE] More about this from Lindsay in the comments.</li>
<li>Marco Di Benedetto at the Visual Computing Lab in Pisa has released a very early version of <a href="http://spidergl.org">a WebGL-based graphics library called SpiderGL</a>; simple stuff so far, but it already has support for render-to-texture using frame buffer objects, which is pretty advanced stuff :-)  [UPDATE] Marco gave me a corrected URL in the comments, so I’ve fixed the link.  His new site also has a better description of SpiderGL: “The philosophy behind SpiderGL is : to provide typical structures and algorithms for realtime rendering to developers of 3D graphics web application, without forcing them to comply with some specific paradigm (i.e. scene graphs) nor preventing low level access to the underlying graphics layer (WebGL).”  Sounds good to me; check out the comments for more, including a very exciting list of features!</li>
</ul></div>
    </content>
    <updated>2010-01-13T18:56:31Z</updated>
    <category term="Links"/>
    <author>
      <name>giles</name>
    </author>
    <source>
      <id>http://learningwebgl.com/blog</id>
      <link href="http://learningwebgl.com/blog/?feed=rss2" rel="self" type="application/atom+xml"/>
      <link href="http://learningwebgl.com/blog" rel="alternate" type="text/html"/>
      <subtitle>...lessons 'n' links...</subtitle>
      <title>Learning WebGL</title>
      <updated>2010-01-28T15:20:04Z</updated>
    </source>
  </entry>

  <entry xml:lang="en">
    <id>http://learningwebgl.com/blog/?p=1508</id>
    <link href="http://learningwebgl.com/blog/?p=1508" rel="alternate" type="text/html"/>
    <title>Cruft dropped</title>
    <summary>I’ve removed the compatibility cruft from all of the lessons and examples on this site, and updated the WebGL Cookbook’s page about initialising your WebGL context.  Here’s to cleaner, simpler code!
The bad news is that I’ve discovered that WebKit doesn’t yet support the flipY flag that I was so happy to find in the [...]</summary>
    <content type="xhtml"><div xmlns="http://www.w3.org/1999/xhtml"><p>I’ve removed the <a href="http://learningwebgl.com/blog/?p=1489">compatibility cruft</a> from all of the lessons and examples on this site, and updated the WebGL Cookbook’s page about <a href="http://learningwebgl.com/cookbook/index.php/How_to_initialise_WebGL">initialising your WebGL context</a>.  Here’s to cleaner, simpler code!</p>
<p>The bad news is that I’ve discovered that WebKit <a href="https://bugs.webkit.org/show_bug.cgi?id=32620">doesn’t yet support</a> the <code>flipY</code> flag that <a href="http://learningwebgl.com/blog/?p=1487">I was so happy to find</a> in the parameter list for <code>texImage2D</code>.  So that retrospective change will have to go.  A pity :-(</p></div>
    </content>
    <updated>2010-01-12T18:33:42Z</updated>
    <category term="API changes"/>
    <category term="Retrospective changes"/>
    <author>
      <name>giles</name>
    </author>
    <source>
      <id>http://learningwebgl.com/blog</id>
      <link href="http://learningwebgl.com/blog/?feed=rss2" rel="self" type="application/atom+xml"/>
      <link href="http://learningwebgl.com/blog" rel="alternate" type="text/html"/>
      <subtitle>...lessons 'n' links...</subtitle>
      <title>Learning WebGL</title>
      <updated>2010-01-25T15:40:06Z</updated>
    </source>
  </entry>

  <entry xml:lang="en">
    <id>http://learningwebgl.com/blog/?p=1500</id>
    <link href="http://learningwebgl.com/blog/?p=1500" rel="alternate" type="text/html"/>
    <title>WebGL around the net, 8 Jan 2010</title>
    <summary>Just two quick links for today:

Eric Shepherd, Developer Documentation Lead at Mozilla, is starting a series of articles on WebGL; just one so far, but it’s looking promising!
Chris Marrin has added a new “User Contributions” section to the Khronos WebGL wiki: a great place for more permanent record of the kind of stuff I’m linking [...]</summary>
    <content type="xhtml"><div xmlns="http://www.w3.org/1999/xhtml"><p>Just two quick links for today:</p>
<ul>
<li>Eric Shepherd, Developer Documentation Lead at Mozilla, is starting a series of articles on WebGL; <a href="https://developer.mozilla.org/en/WebGL/Getting_started_with_WebGL">just one so far</a>, but it’s looking promising!</li>
<li>Chris Marrin has added <a href="http://www.khronos.org/webgl/wiki/User_Contributions">a new “User Contributions” section</a> to the Khronos WebGL wiki: a great place for more permanent record of the kind of stuff I’m linking to in my WebGL roundups.  I’ll have to copy some of the older ones over there. [UPDATE: I've put a few up there, but there are way too many cool demos in <a href="http://learningwebgl.com/blog/?cat=6">my roundup archives</a> to add all of the ones that deserve it, at least last thing on a Friday evening.]</li>
</ul>
<p>Have a great weekend!</p></div>
    </content>
    <updated>2010-01-08T20:53:47Z</updated>
    <category term="Links"/>
    <author>
      <name>giles</name>
    </author>
    <source>
      <id>http://learningwebgl.com/blog</id>
      <link href="http://learningwebgl.com/blog/?feed=rss2" rel="self" type="application/atom+xml"/>
      <link href="http://learningwebgl.com/blog" rel="alternate" type="text/html"/>
      <subtitle>...lessons 'n' links...</subtitle>
      <title>Learning WebGL</title>
      <updated>2010-01-22T14:40:02Z</updated>
    </source>
  </entry>

  <entry xml:lang="en">
    <id>http://learningwebgl.com/blog/?p=1487</id>
    <link href="http://learningwebgl.com/blog/?p=1487" rel="alternate" type="text/html"/>
    <title>A quick retrospective change to the lessons: image flipping</title>
    <summary>In the comments to lesson 5, where we introduce textures, rotoglup pointed out that adding an operation per-fragment to flip the texture vertically (which is required because its coordinates increase as you move down the GIF image, while we want maths-like coordinates that increase as you go upwards) is a bit of an overhead for [...]</summary>
    <content type="xhtml"><div xmlns="http://www.w3.org/1999/xhtml"><p>In the comments to lesson 5, where we introduce textures, rotoglup <a href="http://learningwebgl.com/blog/?p=507&amp;cpage=1#comment-168">pointed out</a> that adding an operation per-fragment to flip the texture vertically (which is required because its coordinates increase as you move down the GIF image, while we want maths-like coordinates that increase as you go upwards) is a bit of an overhead for something that should be done as a one-off.  Rob just commented on the same post, saying how it would be done in OpenGL, and that reminded me that I’d been intending to search the spec to see if there was a better way of flipping textures.  Lo and behold, I discovered that I’d missed out on a rather useful parameter for <code>texImage2D</code>:</p>
<pre>texImage2D(target, level, image, <span style="color: red;">flipY</span>, asPremultipliedAlpha)
</pre>
<p>To be fair, there was no spec when I wrote the lesson :-)</p>
<p>Anyway, I’ve been through all of the lessons, replaced <code>1.0 - vTextureCoord.t</code> with <code>vTextureCoord.t</code> in the fragment shaders, and added <code>true</code> to the end of all of the calls to <code>texImage2D</code>.</p>
<p>It’s fun to remove bad code :-)</p></div>
    </content>
    <updated>2010-01-07T19:51:10Z</updated>
    <category term="Troubleshooting"/>
    <author>
      <name>giles</name>
    </author>
    <source>
      <id>http://learningwebgl.com/blog</id>
      <link href="http://learningwebgl.com/blog/?feed=rss2" rel="self" type="application/atom+xml"/>
      <link href="http://learningwebgl.com/blog" rel="alternate" type="text/html"/>
      <subtitle>...lessons 'n' links...</subtitle>
      <title>Learning WebGL</title>
      <updated>2010-01-07T20:00:06Z</updated>
    </source>
  </entry>

  <entry xml:lang="en">
    <id>http://learningwebgl.com/blog/?p=1471</id>
    <link href="http://learningwebgl.com/blog/?p=1471" rel="alternate" type="text/html"/>
    <title>WebGL around the net, 7 Jan 2010</title>
    <summary>A good mix of stuff today!

Aaron Babcock has written a version of a classic Flash game to run on the graphics card using WebGL.  It’s called Falling Sand; click on one of the palette of materials at the bottom of the page and then drag on the canvas to paint it, add a few [...]</summary>
    <content type="xhtml"><div xmlns="http://www.w3.org/1999/xhtml"><p>A good mix of stuff today!</p>
<ul>
<li>Aaron Babcock has written a version of a classic Flash game to run on the graphics card using WebGL.  It’s called <a href="http://fallingsandgpgpu.appspot.com/index.html">Falling Sand</a>; click on one of the palette of materials at the bottom of the page and then drag on the canvas to paint it, add a few bits of different materials, and then sit back and watch… mesmerising!</li>
<li>Another hypnotic demo, this one from Emanuele Ruffaldi: a <a href="http://www.teslacore.it/projects/webgl/demoLife/">WebGL game of life</a>.</li>
<li>Things are hotting up in the race to support WebGL in Google Web Toolkit: there’s <a href="http://code.google.com/p/gwtgl/">another project to get this working</a>, this time from Soenke Sothmann and Steffen Schafer, to add to the two we saw at the end of last year.  As Soenke says, “competition stimulates business :-)”</li>
<li>Finally, a neat demo from murphy; a <a href="http://murfy.de/webgl/timecrate.html">spinning cube taking its texture from a 2D canvas</a> that shows the time.  I particularly like this one because the code is easy to read for anyone who’s been following the lessons here :-), and it also uses a better setting for the mipmaps (which I should probably retrospectively update the tutorials to use)</li>
</ul>
<p>That’s all for today!  One final thing — tomorrow I’ll be collating the results of the browser tests to see if we can finally get rid of the compatibility cruft in our WebGL code — so if you haven’t done so already, I’d be really grateful if you could <a href="http://learningwebgl.com/tests/cruftcheck20100104.html">check out this page</a> and then <a href="http://learningwebgl.com/blog/?p=1423">post a comment on this post</a> saying what OS and browser you’re using, and what the results were.  Thanks!</p></div>
    </content>
    <updated>2010-01-07T19:02:06Z</updated>
    <category term="Links"/>
    <author>
      <name>giles</name>
    </author>
    <source>
      <id>http://learningwebgl.com/blog</id>
      <link href="http://learningwebgl.com/blog/?feed=rss2" rel="self" type="application/atom+xml"/>
      <link href="http://learningwebgl.com/blog" rel="alternate" type="text/html"/>
      <subtitle>...lessons 'n' links...</subtitle>
      <title>Learning WebGL</title>
      <updated>2010-01-07T20:00:06Z</updated>
    </source>
  </entry>

  <entry xml:lang="en">
    <id>http://learningwebgl.com/blog/?p=1454</id>
    <link href="http://learningwebgl.com/blog/?p=1454" rel="alternate" type="text/html"/>
    <title>WebGL around the net, 5 Jan 2010</title>
    <summary>Just one new link for today: QBox, by Sandro Paganotti.  It’s a photo slideshow app, which displays a number of photos on the sides of a slowly-rotating cube.</summary>
    <content type="xhtml"><div xmlns="http://www.w3.org/1999/xhtml"><p>Just one new link for today: <a href="http://sandropaganotti.com/qbox-a-webgl-photo-slideshow-for-your-website/">QBox, by Sandro Paganotti</a>.  It’s a photo slideshow app, which displays a number of photos <a href="http://sandropaganotti.com/wp-content/goodies/demos/qbox/">on the sides of a slowly-rotating cube</a>.</p></div>
    </content>
    <updated>2010-01-05T19:03:19Z</updated>
    <category term="Links"/>
    <author>
      <name>giles</name>
    </author>
    <source>
      <id>http://learningwebgl.com/blog</id>
      <link href="http://learningwebgl.com/blog/?feed=rss2" rel="self" type="application/atom+xml"/>
      <link href="http://learningwebgl.com/blog" rel="alternate" type="text/html"/>
      <subtitle>...lessons 'n' links...</subtitle>
      <title>Learning WebGL</title>
      <updated>2010-01-07T20:00:06Z</updated>
    </source>
  </entry>

  <entry xml:lang="en">
    <id>http://learningwebgl.com/blog/?p=1359</id>
    <link href="http://learningwebgl.com/blog/?p=1359" rel="alternate" type="text/html"/>
    <title>WebGL Lesson 12 – point lighting</title>
    <summary>&lt;&lt; Lesson 11
Welcome to my number twelve in my series of WebGL tutorials, the second one that isn’t based on the NeHe OpenGL tutorials.  In it, we’ll go through point lighting, which is pretty simple, but is important and will lead on to interesting things later.  Point lighting, as you might expect, is [...]</summary>
    <content type="xhtml"><div xmlns="http://www.w3.org/1999/xhtml"><p><span style="float: left; padding: 0px 20px 0px 0px;"><a href="http://learningwebgl.com/blog/?p=1253">&lt;&lt; Lesson 11</a></span><br/><br/>
Welcome to my number twelve in my series of WebGL tutorials, the second one that isn’t based on <a href="http://nehe.gamedev.net/">the NeHe OpenGL tutorials</a>.  In it, we’ll go through point lighting, which is pretty simple, but is important and will lead on to interesting things later.  Point lighting, as you might expect, is lighting that comes from a particular point within a scene — unlike the directional lighting we’ve been using so far, which comes from some point outside the scene.</p>
<p>Here’s what the lesson looks like when run on a browser that supports WebGL:</p>
<p>&lt;object height="344" width="425"&gt;&lt;param name="movie" value="http://www.youtube.com/v/t-JPgkg2itQ&amp;amp;hl=en_US&amp;amp;fs=1&amp;amp;"&gt;&lt;param name="allowFullScreen" value="true"&gt;&lt;param name="allowscriptaccess" value="always"&gt;&lt;embed allowfullscreen="true" allowscriptaccess="always" height="344" src="http://www.youtube.com/v/t-JPgkg2itQ&amp;amp;hl=en_US&amp;amp;fs=1&amp;amp;" type="application/x-shockwave-flash" width="425"&gt;&lt;/embed&gt;&lt;/object&gt;</p>
<p><a href="http://learningwebgl.com/lessons/lesson12/index.html">Click here and you’ll see the live WebGL version</a>, if you’ve got a browser that supports it; <a href="http://learningwebgl.com/blog/?p=11">here’s how to get one</a> if you don’t.  You’ll see a sphere and cube orbiting; both will probably be white for a few moments while the textures load, but once that’s done you should see that the sphere is the moon and the cube a (not-to-scale) wooden crate.  Both are illuminated by a point light source that is in between them.  If you want to change the light’s position, colour, etc., there are fields beneath the WebGL canvas.</p>
<p>More on how it all works below…</p>
<p><span id="more-1359"/></p>
<p>The usual <a href="http://learningwebgl.com/blog/?page_id=29">warning</a>: these lessons are targeted at people with a reasonable amount of programming knowledge, but no real experience in 3D graphics; the aim is to get you up and running, with a good understanding of what’s going on in the code, so that you can start producing your own 3D Web pages as quickly as possible.  If you haven’t read the previous tutorials already, you should probably do so before reading this one — here I will only explain the new stuff.  The lesson is based on <a href="http://learningwebgl.com/blog/?p=1253">lesson 11</a>, so you should make sure that you understand that one (and please do post a comment on  that post if anything’s unclear about it!)</p>
<p>There may be bugs and misconceptions in this tutorial.  If you spot anything wrong, let me know in the comments and I’ll correct it ASAP.</p>
<p>There are two ways you can get the code for this example; just “View Source” while you’re looking at the live version, or if you use GitHub, you can clone it (and the other lessons) from <a href="http://github.com/gpjt/webgl-lessons">the repository there</a>.  </p>
<p>Let’s kick off by describing exactly what we’re trying to do with point lighting; the difference between it and directional lighting is that the light comes from a point within the scene.  A moment’s thought should make it clear that this means that the angle from which it comes is different at every point in the scene.  So, the obvious way to model it is to calculate the direction toward the light’s location for each vertex and then to just do exactly the same calculations as we did for directional lighting.  And that’s what we do!</p>
<p>(You might be thinking, at this point, that perhaps it would be even better to calculate the direction to the light not just for every vertex, but for the points between vertices — that is, for the fragments.  And you’d be quite right in thinking that; lighting like that is harder work for the graphics card, but it looks much better.  And it’s what we’ll move on to in the next lesson :-)</p>
<p>Now we’ve determined what to do, it’s worth looking once again at <a href="http://learningwebgl.com/lessons/lesson12/index.html">this lesson’s demo page</a> and noting one more thing: there’s no actual object in the scene at the point where the light is coming from. If you want to have an object that appears to be casting light (say, the sun in the centre of the solar system) then you need to define the light source and the object separately.  Doing the object should be pretty easy based on the previous lessons, so in this walkthough I’ll only explain how the point light source works.  As you might expect from the description above, it’s actually really simple; most of the differences between this page and <a href="http://learningwebgl.com/blog/?p=1253">lesson 11</a>’s are simply to draw the cube and make it and the sphere orbit…</p>
<p>As usual, we’ll start at the bottom of the source HTML file and work our way up through the differences between this file and lesson 11’s.  The first set of changes are in the HTML <code>body</code>, where the fields where you could enter a light direction have changed to be the position of the light.  This is simple enough that there’s no point in showing them here, so let’s move on up to <code>webGLStart</code>.  Once again, the changes are simple — this lesson has no mouse-based controls, so we have no mouse-handling code, and the function formerly known as <code>initTexture</code> is now called <code>initTextures</code> because it’s going to load two of them.  Not very exciting…</p>
<p>Moving a little further up, the <code>tick</code> function has gained a new call, to <code>animate</code>, so that our scene updates over time:</p>
<pre>  function tick() {
    drawScene();
    <span style="color: red;">animate();</span>
  }
</pre>
<p>Above that is the animate function itself, which simply updates two global variables that describe how far around their orbits the moon and the cube are in such a manner that they orbit at 50°/second:</p>
<pre>  var lastTime = 0;
  function animate() {
    var timeNow = new Date().getTime();
    if (lastTime != 0) {
      var elapsed = timeNow - lastTime;

      moonAngle += 0.05 * elapsed;
      cubeAngle += 0.05 * elapsed;
    }
    lastTime = timeNow;
  }
</pre>
<p>The next function up is <code>drawScene</code>, which has a few interesting changes.  It starts off with the normal boilerplate code to clear the canvas and set up our perspective matrix, and then has code identical to lesson 11’s to check whether the lighting checkbox is checked and to send the ambient lighting colour to the graphics card:</p>
<pre>  function drawScene() {
    gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)

    perspective(45, 1.0, 0.1, 100.0);

    var lighting = document.getElementById("lighting").checked;
    gl.uniform1i(gl.getUniformLocation(shaderProgram, "uUseLighting"), lighting);
    if (lighting) {
      gl.uniform3f(
        gl.getUniformLocation(shaderProgram, "uAmbientColor"),
        parseFloat(document.getElementById("ambientR").value),
        parseFloat(document.getElementById("ambientG").value),
        parseFloat(document.getElementById("ambientB").value)
      );
</pre>
<p>Next, we push the position of our point light up to the graphics card in a uniform.  This is equivalent to the code that pushed the lighting direction up in lesson 11; the difference is in something that was taken away rather than something added.  When we sent the lighting direction to the graphics card, we needed to turn it into a unit vector (that is, scale it so that its length was one unit) and reverse its direction.  No need for anything like that here: we just push the coordinates of the light directly up:</p>
<pre>      gl.uniform3f(
        gl.getUniformLocation(shaderProgram, "uPointLightingLocation"),
        parseFloat(document.getElementById("lightPositionX").value),
        parseFloat(document.getElementById("lightPositionY").value),
        parseFloat(document.getElementById("lightPositionZ").value)
      );
</pre>
<p>Next, we do the same for the point light’s colour, and we’re done with the lighting code in drawScene.</p>
<pre>      gl.uniform3f(
        gl.getUniformLocation(shaderProgram, "uPointLightingColor"),
        parseFloat(document.getElementById("pointR").value),
        parseFloat(document.getElementById("pointG").value),
        parseFloat(document.getElementById("pointB").value)
      );
    }
</pre>
<p>Next, we actually draw the sphere and the cube in the appropriate positions:</p>
<pre>    mvTranslate([0, 0, -20]);

    mvPushMatrix();
    mvRotate(moonAngle, [0, 1, 0]);
    mvTranslate([5, 0, 0]);
    gl.activeTexture(gl.TEXTURE0);
    gl.bindTexture(gl.TEXTURE_2D, moonTexture);
    gl.uniform1i(gl.getUniformLocation(shaderProgram, "uSampler"), 0);

    gl.bindBuffer(gl.ARRAY_BUFFER, moonVertexPositionBuffer);
    gl.vertexAttribPointer(vertexPositionAttribute, moonVertexPositionBuffer.itemSize, gl.FLOAT, false, 0, 0);

    gl.bindBuffer(gl.ARRAY_BUFFER, moonVertexTextureCoordBuffer);
    gl.vertexAttribPointer(textureCoordAttribute, moonVertexTextureCoordBuffer.itemSize, gl.FLOAT, false, 0, 0);

    gl.bindBuffer(gl.ARRAY_BUFFER, moonVertexNormalBuffer);
    gl.vertexAttribPointer(vertexNormalAttribute, moonVertexNormalBuffer.itemSize, gl.FLOAT, false, 0, 0);

    gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, moonVertexIndexBuffer);
    setMatrixUniforms();
    gl.drawElements(gl.TRIANGLES, moonVertexIndexBuffer.numItems, gl.UNSIGNED_SHORT, 0);
    mvPopMatrix();

    mvPushMatrix();
    mvRotate(cubeAngle, [0, 1, 0]);
    mvTranslate([5, 0, 0]);
    gl.bindBuffer(gl.ARRAY_BUFFER, cubeVertexPositionBuffer);
    gl.vertexAttribPointer(vertexPositionAttribute, cubeVertexPositionBuffer.itemSize, gl.FLOAT, false, 0, 0);

    gl.bindBuffer(gl.ARRAY_BUFFER, cubeVertexNormalBuffer);
    gl.vertexAttribPointer(vertexNormalAttribute, cubeVertexNormalBuffer.itemSize, gl.FLOAT, false, 0, 0);

    gl.bindBuffer(gl.ARRAY_BUFFER, cubeVertexTextureCoordBuffer);
    gl.vertexAttribPointer(textureCoordAttribute, cubeVertexTextureCoordBuffer.itemSize, gl.FLOAT, false, 0, 0);

    gl.activeTexture(gl.TEXTURE0);
    gl.bindTexture(gl.TEXTURE_2D, crateTexture);
    gl.uniform1i(gl.getUniformLocation(shaderProgram, "uSampler"), 0);

    gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, cubeVertexIndexBuffer);
    setMatrixUniforms();
    gl.drawElements(gl.TRIANGLES, cubeVertexIndexBuffer.numItems, gl.UNSIGNED_SHORT, 0);
    mvPopMatrix();
  }
</pre>
<p>So, that’s <code>drawScene</code>.  Moving further up the code, you will see that <code>initBuffers</code> has gained our standard code for generating buffers for a cube as well as the code for a sphere, and even further up that <code>initTextures</code> is now loading two textures instead of just one.</p>
<p>The next, and in fact the final, change in the file is the most important one.  If you scroll up to the top, where the vertex shader is, you’ll see that it has a few small changes, and its these that make the difference for this lesson.  Working through from the top, with changes in red:</p>
<pre>  attribute vec3 aVertexPosition;
  attribute vec3 aVertexNormal;
  attribute vec2 aTextureCoord;

  uniform mat4 uMVMatrix;
  uniform mat4 uPMatrix;
  uniform mat4 uNMatrix;

  uniform vec3 uAmbientColor;

  <span style="color: red;">uniform vec3 uPointLightingLocation;
  uniform vec3 uPointLightingColor;</span>
</pre>
<p>So, we have uniforms for the lighting location and colour to replace the old lighting direction and colour.  Next:</p>
<pre>  uniform bool uUseLighting;

  varying vec2 vTextureCoord;
  varying vec3 vLightWeighting;

  void main(void) {
    <span style="color: red;">vec4 mvPosition = uMVMatrix * vec4(aVertexPosition, 1.0);
    gl_Position = uPMatrix * mvPosition;</span>
</pre>
<p>What we’ve done here is split our old code in two.  In all of our vertex shaders so far, we’ve applied the model-view matrix and the projection matrix to the vertex position in one go, like this:</p>
<pre>    // Code from lesson 11
    gl_Position = uPMatrix * uMVMatrix * vec4(aVertexPosition, 1.0);
</pre>
<p>Now, we’re storing the intermediate value, the position of the vertex with the current model-view matrix applied but before it has been adjusted to allow for perspective.  This is used in the next bit:</p>
<pre>    vTextureCoord = aTextureCoord;

    if (!uUseLighting) {
      vLightWeighting = vec3(1.0, 1.0, 1.0);
    } else {
      <span style="color: red;">vec3 lightDirection = normalize(uPointLightingLocation - mvPosition.xyz);</span>
</pre>
<p>The light’s position is in terms of the world coordinates, and the vertex position, once it’s been multiplied by the model-view matrix, is also in terms of world coordinates.  We need to work out the direction of the point light from our current vertex in terms of these coordinates, and to work out the direction from one point to another, we just need to subtract them; once that’s done, we need to normalise the direction vector so that, just like our old lighting direction vector, it has a length of one.  Once that’s done, all of the pieces are in place to do a calculation that’s identical to the one we were doing for directional lighting, with just a few variable names changed:</p>
<pre>      vec4 transformedNormal = uNMatrix * vec4(aVertexNormal, 1.0);
      float directionalLightWeighting = max(dot(transformedNormal.xyz, <span style="color: red;">lightDirection</span>), 0.0);
      vLightWeighting = uAmbientColor + <span style="color: red;">uPointLightingColor</span> * directionalLightWeighting;
</pre>
<p>And that’s it!  You now know how to write shaders to provide point lighting.</p>
<p>That’s it for now; next time we’ll look at lighting again, improving the realism of our scene by making the lighting work per-fragment instead of per-vertex.</p>
<p><span style="float: left; padding: 0px 20px 0px 0px;"><a href="http://learningwebgl.com/blog/?p=1253">&lt;&lt; Lesson 11</a></span><br/></p>
<p><em>Acknowledgments: As before, the texture-map for the moon <a href="http://maps.jpl.nasa.gov/">comes from NASA’s JPL website</a>, and the code to generate a sphere is based on <a href="https://cvs.khronos.org/svn/repos/registry/trunk/public/webgl/sdk/demos/webkit/Earth.html">this demo</a>, which was originally by the WebKit team.  Many thanks to both!</em></p></div>
    </content>
    <updated>2010-01-05T15:18:35Z</updated>
    <category term="Lessons"/>
    <author>
      <name>giles</name>
    </author>
    <source>
      <id>http://learningwebgl.com/blog</id>
      <link href="http://learningwebgl.com/blog/?feed=rss2" rel="self" type="application/atom+xml"/>
      <link href="http://learningwebgl.com/blog" rel="alternate" type="text/html"/>
      <subtitle>...lessons 'n' links...</subtitle>
      <title>Learning WebGL</title>
      <updated>2010-01-07T20:00:06Z</updated>
    </source>
  </entry>
</feed>
