Skip to content

Commit 691f481

Browse files
committed
Simplify code using ArcBall and GfxRender classes
1 parent d25359a commit 691f481

File tree

1 file changed

+30
-36
lines changed

1 file changed

+30
-36
lines changed

processing_app/basics/form/rgb_cube.rb

Lines changed: 30 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
java_import 'monkstone.vecmath.GfxRender'
12
# RGB Cube.
23
#
34
# The three primary colors of the additive color model are red, green, and blue.
@@ -8,63 +9,56 @@
89
def setup
910
sketch_title 'RGB Cube'
1011
no_stroke
11-
color_mode RGB, 2
12-
@xmag = 0
13-
@ymag = 0
14-
@new_xmag = 0
15-
@new_ymag = 0
16-
# Math.since each point is used three times
12+
ArcBall.init(self, width / 2, height / 2)
13+
1714
@box_points = {
18-
top_front_left: [-1, 1, 1],
19-
top_front_right: [1, 1, 1],
20-
top_back_right: [1, 1, -1],
21-
top_back_left: [-1, 1, -1],
22-
bottom_front_left: [-1, -1, 1],
23-
bottom_front_right: [1, -1, 1],
24-
bottom_back_right: [1, -1, -1],
25-
bottom_back_left: [-1, -1, -1]
15+
top_front_left: Vec3D.new(-90, 90, 90),
16+
top_front_right: Vec3D.new(90, 90, 90),
17+
top_back_right: Vec3D.new(90, 90, -90),
18+
top_back_left: Vec3D.new(-90, 90, -90),
19+
bottom_front_left: Vec3D.new(-90, -90, 90),
20+
bottom_front_right: Vec3D.new(90, -90, 90),
21+
bottom_back_right: Vec3D.new(90, -90, -90),
22+
bottom_back_left: Vec3D.new(-90, -90, -90)
2623
}
2724
# a box from defined points
2825
@box = {
29-
top: [box_points[:top_front_left], box_points[:top_front_right], box_points[:top_back_right], box_points[:top_back_left]],
30-
front: [box_points[:top_front_left], box_points[:top_front_right], box_points[:bottom_front_right], box_points[:bottom_front_left]],
31-
left: [box_points[:top_front_left], box_points[:bottom_front_left], box_points[:bottom_back_left], box_points[:top_back_left]],
32-
back: [box_points[:top_back_left], box_points[:top_back_right], box_points[:bottom_back_right], box_points[:bottom_back_left]],
33-
right: [box_points[:top_back_right], box_points[:bottom_back_right], box_points[:bottom_front_right], box_points[:top_front_right]],
34-
bottom: [box_points[:bottom_front_left], box_points[:bottom_front_right], box_points[:bottom_back_right], box_points[:bottom_back_left]]
26+
top: [box_points[:top_front_left], box_points[:top_front_right], box_points[:top_back_right],
27+
box_points[:top_back_left]],
28+
front: [box_points[:top_front_left], box_points[:top_front_right], box_points[:bottom_front_right],
29+
box_points[:bottom_front_left]],
30+
left: [box_points[:top_front_left], box_points[:bottom_front_left], box_points[:bottom_back_left],
31+
box_points[:top_back_left]],
32+
back: [box_points[:top_back_left], box_points[:top_back_right], box_points[:bottom_back_right],
33+
box_points[:bottom_back_left]],
34+
right: [box_points[:top_back_right], box_points[:bottom_back_right], box_points[:bottom_front_right],
35+
box_points[:top_front_right]],
36+
bottom: [box_points[:bottom_front_left], box_points[:bottom_front_right], box_points[:bottom_back_right],
37+
box_points[:bottom_back_left]]
3538
}
3639
end
3740

3841
def draw
3942
background 1
40-
push_matrix
41-
translate width / 2, height / 2, -30
42-
@new_xmag = mouse_x.to_f / width * TAU
43-
@new_ymag = mouse_y.to_f / height * TAU
44-
diff = @xmag - @new_xmag
45-
@xmag -= diff / 4 if diff.abs > 0.01
46-
diff = @ymag - @new_ymag
47-
@ymag -= diff / 4 if diff.abs > 0.01
48-
rotate_x(-@ymag)
49-
rotate_y(-@xmag)
50-
scale 90
5143
begin_shape QUADS
5244
%i[top front left back right bottom].each do |s|
5345
@box[s].each do |p|
5446
fill_from_points p
55-
vertex_from_points p
47+
p.to_vertex(renderer)
5648
end
5749
end
5850
end_shape
59-
pop_matrix
6051
end
6152

6253
def fill_from_points(points)
63-
fill points[0] + 1, points[1] + 1, points[2] + 1 # "+1" translates -1,1 to 0,2
54+
red = map1d(points.x, -90..90, 0..255)
55+
blue = map1d(points.y, -90..90, 0..255)
56+
green = map1d(points.z, -90..90, 0..255)
57+
fill(red, blue, green)
6458
end
6559

66-
def vertex_from_points(points)
67-
vertex(*points)
60+
def renderer
61+
@renderer ||= GfxRender.new(g)
6862
end
6963

7064
def settings

0 commit comments

Comments
 (0)