Skip to content

Commit 3cdf56a

Browse files
author
monkstone
committed
refine spherical harmonics example
1 parent 124d2b3 commit 3cdf56a

File tree

1 file changed

+27
-18
lines changed

1 file changed

+27
-18
lines changed

examples/spherical_harmonics_mesh.rb

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,47 @@
11
require 'toxiclibs'
22

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+
#######
410

5-
def setup
11+
attr_reader :gfx, :mesh, :spherical, :param
12+
13+
def setup
614
sketch_title 'Spherical Harmonics Mesh Builder'
715
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
1019
no_stroke
11-
@spherical = gfx.mesh_to_shape(mesh, true)
20+
@spherical = gfx.mesh_to_shape(mesh, true) # white
1221
end
1322

1423
def draw
15-
background(0)
24+
background(0)
1625
lights
1726
shininess(16)
18-
directionalLight(255, 255, 255, 0, -1, 1)
27+
directional_light(255, 255, 255, 0, -1, 1)
1928
specular(255)
20-
fill(255)
21-
noStroke
2229
shape(spherical)
2330
end
2431

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) }
3141
end
3242

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)))
3645
b.create_mesh(nil, 80, 60)
3746
end
3847

0 commit comments

Comments
 (0)