Skip to content

Instantly share code, notes, and snippets.

@FeepingCreature
Created May 23, 2012 12:26
Show Gist options
  • Save FeepingCreature/2774994 to your computer and use it in GitHub Desktop.
Save FeepingCreature/2774994 to your computer and use it in GitHub Desktop.
auto mksphere(int depth) {
alias V = vec3f;
(V,V,V)[auto~] res;
if false return res[]; // return type hint
if (!depth) {
auto points = [for tup <- cross([-1, 1] x 3): V tup].eval[];
for auto tup <- [
(0, 1, 3), (0, 3, 2),
(4, 5, 7), (4, 7, 6),
(0, 1, 5), (0, 5, 4),
(2, 3, 7), (2, 7, 6),
(0, 2, 6), (0, 6, 4),
(1, 3, 7), (1, 7, 5)
] {
res ~= points[tup];
}
} else {
auto next = mksphere (depth - 1);
onSuccess next.free;
V half(V a, b) { return (a*0.5)+(b*0.5); }
for auto val <- next {
auto halves = (val[0].half val[1], val[1].half val[2], val[2].half val[0]);
res ~= (val[0], halves[0], halves[2]);
res ~= (val[1], halves[0], halves[1]);
res ~= (val[2], halves[1], halves[2]);
res ~= (halves[2], halves[1], halves[0]);
}
}
for ref v <- res {
v[0] /= |v[0]|;
v[1] /= |v[1]|;
v[2] /= |v[2]|;
}
return res[];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment