|
1 | 1 | require 'toxiclibs' |
2 | 2 |
|
3 | | -attr_reader :gfx, :mesh, :spherical |
| 3 | +####### |
| 4 | +# After Paul Bourke see http://paulbourke.net/geometry/sphericalh/ |
| 5 | +# radius = |
| 6 | +# sin(m0*phi)**m1 + cos(m2*phi)**m3 + sin(m4*theta)**m5 + cos(m6*theta)**m7 |
| 7 | +# where phi = (0..PI) and theta = (0..TWO_PI) |
| 8 | +# As implemented by Karsten Schmidt aka toxi/postspectacular |
| 9 | +####### |
4 | 10 |
|
5 | | -def setup |
| 11 | +attr_reader :gfx, :mesh, :spherical, :param |
| 12 | + |
| 13 | +def setup |
6 | 14 | sketch_title 'Spherical Harmonics Mesh Builder' |
7 | 15 | ArcBall.init(self) |
8 | | - @mesh = randomize_mesh |
9 | | - @gfx = Gfx::MeshToVBO.new(self) |
| 16 | + @param = [8, 4, 1, 5, 1, 4, 0, 0] # default function parameters (m0..m7) |
| 17 | + @mesh = spherical_mesh(param) |
| 18 | + @gfx = Gfx::MeshToVBO.new(self) # Mesh to vertex buffer object converter |
10 | 19 | no_stroke |
11 | | - @spherical = gfx.mesh_to_shape(mesh, true) |
| 20 | + @spherical = gfx.mesh_to_shape(mesh, true) # white |
12 | 21 | end |
13 | 22 |
|
14 | 23 | def draw |
15 | | - background(0) |
| 24 | + background(0) |
16 | 25 | lights |
17 | 26 | shininess(16) |
18 | | - directionalLight(255, 255, 255, 0, -1, 1) |
| 27 | + directional_light(255, 255, 255, 0, -1, 1) |
19 | 28 | specular(255) |
20 | | - fill(255) |
21 | | - noStroke |
22 | 29 | shape(spherical) |
23 | 30 | end |
24 | 31 |
|
25 | | -def keyPressed |
26 | | - if (key == 'r') |
27 | | - @mesh = randomize_mesh |
28 | | - no_stroke |
29 | | - @spherical = gfx.mesh_to_colored_shape(mesh, true) |
30 | | - end |
| 32 | +def key_pressed |
| 33 | + return unless (key == 'r') |
| 34 | + @mesh = spherical_mesh(random_parameters) |
| 35 | + no_stroke |
| 36 | + @spherical = gfx.mesh_to_colored_shape(mesh, true) # harmonic colors |
| 37 | +end |
| 38 | + |
| 39 | +def random_parameters |
| 40 | + (0..8).map { rand(0..8) } |
31 | 41 | end |
32 | 42 |
|
33 | | -def randomize_mesh |
34 | | - m = (0..8).map { rand(0..8) } |
35 | | - b = SurfaceMeshBuilder.new(SphericalHarmonics.new(m.to_java(:float))) |
| 43 | +def spherical_mesh(param) |
| 44 | + b = SurfaceMeshBuilder.new(SphericalHarmonics.new(param.to_java(:float))) |
36 | 45 | b.create_mesh(nil, 80, 60) |
37 | 46 | end |
38 | 47 |
|
|
0 commit comments