Thursday, February 24, 2011

WAMBot on ABC Catalyst

ABC Catalyst did a segment on the MAGIC 2010 competition, you can watch the Catalyst RobotWars MAGIC2010 segment on their website.

A more in-depth explanation of the problems we faced: The robots were stored overnight on site in a tent, and unfortunately the next day a lot of moisture had gathered in the electronics. Since we had used cheap GPS units, a number of them then failed on the day. The software we had written was not robust enough to cope with the faulty units and so the robots would think they were in an incorrect location. As the fleet of robots could not agree on their locations the map data was incomplete, and subsequently the robots were difficult to navigate.

Other software bugs, short battery life and communications drops also hindered the performance.

Next time: More testing, and focus on robustness and fallback options, and buy equipment with an IP rating of 32 or more.

A short clip of the interview is on youtube:

Sunday, February 20, 2011

Newton Physics Engine

In an unexpected move the Newton Physics Engine has been open sourced with the zlib licence. I haven't been following this engines developments closely since the 1.x versions. Julio has released the 3.0 version over svn as well.

Finally, a chance to figure out some of Newton's strange behaviour I noticed in the PAL physics engine benchmarks.

Saturday, February 12, 2011

Sphere effect in WebGL

This effect is quit interesting to decompose. There are a few versions out there with some confusing formulations and constants, however in essence they all come from the same principles. This version is based on one by Luis Gonzalez, which in turn was based Iñigo Quilez's version (formula 14). (iq emailed me to tell me about his windows-only program, but the data file is plain-text - lots more effects to decompose! Thanks iq!)

First, we start with our trusty radius calculation:

vec2 p = -1.0 + 2.0 * gl_FragCoord.xy / resolution.xy;
float r =sqrt(dot(p,p));

Now to generate a sphere-like looking object we want to create something that looks somewhat like a parabola when we take a slice of it. By careful choice of our equations we should be able to construct the appropriate curve. First, since we are creating a sphere we need to create the outline of the sphere, which of course will be a circle. We can use the fact that the square-root of a negative number will be 'undefined' (complex) to generate the outline of our sphere:

float f = sqrt(1.0 - r*r);
gl_FragColor = vec4(f,0.0,0.0, 1.0);

Inside the 'sphere' (inverted below)

This generates a circle from a re-arrangment of the circle equation (simplified for unit circle):

If you apply a linear texture to this, you will notice that it seems like we are looking at the 'inside' of a parabola, and to get something that looks like a sphere, we need to be looking from the 'outside'. We can fix this by inverting our previous result:

vec2 p = -1.0 + 2.0 * gl_FragCoord.xy / resolution.xy;
vec2 uv;
float r =sqrt(dot(p,p));
float f = 1.0 - sqrt(1.0 - r*r);
uv.x = p.x*f;
uv.y = p.y*f;
gl_FragColor = vec4(texture2D(tex,uv).xyz, 1.0);

Gee, that looks vaguely sphereoid already. We just need to make it look more 3D. Well, lets try generating a sphere from the equation of a circle:

r = sqrt(x*x+y*y);
z = r*r;

Recall from the tunnel tutorial that to project from 3D to 2D we simply need to divide by z. We can use the 3D equation of the sphere to calculate the Z depth of the sphere, (i.e. divide by r*r).

Its starting to look good, now we just need to finish it off with a perspective divide and we have our final result (we can also factor out the redundant square-root):

vec2 p = -1.0 + 2.0 * gl_FragCoord.xy / resolution.xy;
vec2 uv;
float r = dot(p,p);
float f = (1.0-sqrt(1.0-r))/(r);
uv.x = p.x*f + time;
uv.y = p.y*f + time;
gl_FragColor = vec4(texture2D(tex,uv).xyz, 1.0);

It's not raytracing, but it looks pretty good.
Your browser doesn't appear to support the HTML5 <canvas> element.

Monday, February 07, 2011

Ripple effect in WebGL

There is really nothing special to the ripple effect, in essence it is just an evaluation of the sombrero function. This is just a 2D version of the sinc function.

Below is a graph of the sin function, followed by sinc.
Left: sin(x);   Right: sin(x) / x

Extending this to 2D we have:
r = sqrt (x*x + y*y);
z = sin (r) / r;

Plotting this as (x,y,z) we have the sombrero function:

To turn it into a ripple-like looking effect we just need to scale down the "height" of the function, and animate it by adding time into the sin term to generate a repeating pulse. The complete GLSL code is:

#ifdef GL_ES
precision highp float;

uniform float time;
uniform vec2 resolution;
uniform sampler2D tex;

void main(void) {
vec2 cPos = -1.0 + 2.0 * gl_FragCoord.xy / resolution.xy;
float cLength = length(cPos);

vec2 uv = gl_FragCoord.xy/resolution.xy+(cPos/cLength)*cos(cLength*12.0-time*4.0)*0.03;
vec3 col = texture2D(tex,uv).xyz;

gl_FragColor = vec4(col,1.0);

Your browser doesn't appear to support the HTML5 <canvas> element.

Sunday, February 06, 2011

Despaire by Iguana ported to Mac OSX

One of my favourite old demos is Despair by Iguana. It features a number of nice effects including realtime raytracing, blended particles, and many more effects that were ahead of its time (It was released in 1996!). The source code for the demo was released and ported to 32bit by David 'mac' from ThreePixels. So I took it upon myself to port it from 32bit windows to 64bit OSX / *nix.
  1. First I translated David's version to VS2010 and ported it to use SDL and the latest BASS, compiling it on windows, to ensure it was all running.
  2. I stripped out all the non-portable assembly code. Unfortunately none of this code was commented, and the few comments that were in there, were all in spanish. (I think)
  3. I installed Intel C++ Composer XE 2011 for Mac OS* X (The Intel C++ compiler on OSX), to keep the code compatible with VS as far as possible, and created a makefile.
  4. I ported the code across to OSX. Nothing too special here, replacing a few windows and DOS function calls with their POSIX equivalents. This part of the port followed the same process as porting any other windows application to another platform. (I'd already done the port to SDL)
  5. I requested (and received! Thanks Ian!) a 64bit copy of BASS for OSX.
  6. Success! A stripped down version of Despair ran on OSX, very few effects actually worked. Now the fun (pain?) could begin.
  7. I looked through all the assembly code and realised quickly that I would never be able to port it all. Instead, I figured I would take an educated guess at the functionality.
  8. I noted some assembly code "RenderBandScan" and took a quick look and spotted a:
    and eax,0F0F0F0F0h
    Immediately, I recalled the old trick of using bitwise and and shifts to quickly sum 4 pixels at once, and re-implemented this in a simple C loop.
  9. The next routine was "AADump", a little trickier this time, but again a well known pattern:
     mov cl,[esi+1]
     mov bl,[esi+320]
     add eax,ecx
     mov dl,[esi+321]
     add eax,ebx
     add ecx,edx
     add eax,edx
     mov dl,[esi+2]
     shr eax,2
    The pattern of adds and a shift for quickly "blurring" an image, from that basis I just re-implemented a blur-copy routine, and all was well.
  10. I came across the cryptic "DumpScanASM" routine. Luckily, there was a section of c-code commented out that I could just drop back in to replace this. Phew!
  11. The next function was "RenderScan". The name was quite self-explanatory, however I noted a fair bit of odd '127' in there. Knowing the effect was alpha blending some blobs, I deduced the C code:

    for (i=0;i127)
    sum = 127;

  12. The second last effect that needed translation from assembly to C was "FB_Draw320x200". This was a bit cryptic, but essentially it was a fixed-point texture sampling routine with a number of clever bit-shifts. I didn't quite get this one converted correctly.
  13. Finally, the greets section is a rotozoomer, texture mapping and blending routine written in pure assembly. This is where I call it a day. I'm not converting this beast! (If anyone else wants to you will need to re-code: DrawGrScan, and VSTM_DumpScan2000.. good luck!)
Here is a zip of the OSX executable for the Despair/Iguana demo along with the ported source code and make files. Youtube video below:

Wednesday, February 02, 2011

Ported Trip! (4k intro) to OSX

There are very few products from the demoscene that are available on Mac OSX.

In preparation for some more challenging ports of old demoscene products across to OSX I thought I would start with one of my own productions. Unfortunately, I historically used DirectX, but I did do one OpenGL production, Trip! for Syntax 2007. And now, the Mac OSX (Intel) port of Trip! for Syntax 2007, unfortunately nowhere near 4k anymore. (Warning: Plenty of black/white flashes, the illusions will only work if you stare at the center of the screen)