32

I'm trying to make a quad sphere based on an article, which shows results like this:

correct

I can generate a cube correctly:

before

But when I convert all the points according to this formula (from the page linked above):

formula

    x = x * sqrtf(1.0 - (y*y/2.0) - (z*z/2.0) + (y*y*z*z/3.0));
    y = y * sqrtf(1.0 - (z*z/2.0) - (x*x/2.0) + (z*z*x*x/3.0));
    z = z * sqrtf(1.0 - (x*x/2.0) - (y*y/2.0) + (x*x*y*y/3.0));

My sphere looks like this:

after

As you can see, the edges of the cube still poke out too far. The cube ranges from -1 to +1 on all axes, like the article says.

Any ideas what is wrong?

Vaillancourt
  • 16,325
  • 17
  • 55
  • 61
Tom Dalling
  • 1,074
  • 1
  • 9
  • 14
  • 1
    Does your implementation contain "x = x ..." problem too or is it just here? – snake5 Nov 14 '12 at 09:55
  • 8
    Fantastic visual aids. Thankyou for including those. – doppelgreener Nov 14 '12 at 10:04
  • 2
    To answer the question in the title, you can just normalize the vertices of the cube to make it a sphere. The distribution of the vertices will probably be different from the linked method though. – msell Nov 14 '12 at 10:09
  • Related: http://gamedev.stackexchange.com/questions/7189/morph-a-sphere-to-a-cube-and-a-cube-to-a-sphere-with-glsl – House Nov 14 '12 at 16:16

1 Answers1

28

You've miswritten the formula.

x = x * sqrtf(1.0 - (y*y/2.0) - (z*z/2.0) + (y*y*z*z/3.0));
y = y * sqrtf(1.0 - (z*z/2.0) - (x*x/2.0) + (z*z*x*x/3.0));
z = z * sqrtf(1.0 - (x*x/2.0) - (y*y/2.0) + (x*x*y*y/3.0));

You modify the original x and overwrite it. Then you modify y based not on the original x but the modified x. Then you modify z based on the modified version of both of those.

Preserve the originals, and calculate this:

float dx = x * sqrtf(1.0 - (y*y/2.0) - (z*z/2.0) + (y*y*z*z/3.0));
float dy = y * sqrtf(1.0 - (z*z/2.0) - (x*x/2.0) + (z*z*x*x/3.0));
float dz = z * sqrtf(1.0 - (x*x/2.0) - (y*y/2.0) + (x*x*y*y/3.0));

Use dx, dy and dz from then on.

doppelgreener
  • 7,184
  • 7
  • 42
  • 68