Thursday, April 30, 2009

3D Supershape

Over the last few years I was looking into the 3D Supershape formula described by Paul Bourke here and originally developed by Johan Gielis. I love the shape of the objects that are a result of those and therefore I always wanted to use it to create my own demos after I saw the one from Jetro Lauha (http://jet.ro/creations). Here is my first attempt to generate C source out of the equations:

Suitable C pseudo code could be:

float r = pow(pow(fabs(cos(m * o / 4)) / a, n2) + pow(fabs(sin(m * o / 4)) / b, n3), 1 / n1);

The result of this calculation is in polar coordinates. Please note the difference between the equation and the C code. The equation has a negative power value, the C doesn't. To extend this result into 3D, the spherical product of several superformulas is used. For example, the 3D parametric surface is obtained multiplying two superformulas S1and S2. The coordinates are defined by the relations:

The sphere mapping code uses two r values:

point->x = (float)(cosf(t) * cosf(p) / r1 / r2);
point->y = (float)(sinf(t) * cosf(p) / r1 / r2);
point->z = (float)(sinf(p) / r2);

Because r1 and r2 had a positive power value in the C code above we have to divide by those variables here. Here is a Mathematica render of this code:

10 comments:

Andy Firth said...

the jet.ro/creations URL has an extra space in it between / and creations so doesn't work.

i love playing around this these types of constructs btw... i'm assuming much like many other graphics programmers... they represent a staple "geometry" library when faced with testing without an artist as a guinea pig

Wolfgang Engel said...

Thanks! Fixed it.

Wolfgang Engel said...

I am actually more fascinated by the look of the 3D Supershape objects ... they look so cool that I want to use this for a game or a demo :-) ... at some point.

matt said...

There's a wealth of information like this on paul's site.
sounds like its time for you to join the demoscene, wolfgang!

Wolfgang Engel said...

This is why I referred to it.

JayFoxRox said...

Any chance to see you at a demoparty or have you been to one yet?
Somehow its kinda weird. I was always interested in GTA and found out that you were a graphics developer at R* after reading (and liking) your posts at gamedev. Then I got interested in the demoscene and saw you posting San Angelos for the iPhone. Now you got a Beagle Board, only half a year after I got mine in preparation for the Pandora. :P
PS: Did you also pre-order a Pandora btw?

Wolfgang Engel said...

Yep I also ordered the Pandora ... no I won't be on a demoparty but on SIGGRAPH :-)

Anonymous said...

You can also apply the same formula to a toroid, for cool variations.

http://machinesdontcare.wordpress.com/2008/11/08/superformula-toroidal-mapping/

I adapted my code from HLSL shader code written for the realtime graphics application VVVV. You might want to check out the vvvv site, it's got some nice stuff there, and has been a real source of inspiration (and code) for me for a few years now.

a|x

Jordan T-H said...

I don't suppose you still have the Mathematica code for this?

Wolfgang Engel said...

I was looking for this two weeks ago. It got lost in a hard drive melt of my backup system in 2010 ...