Skip to content

Commit 96a823c

Browse files
committed
Simplify RGB cube by using Vec2D, ArcBall and GfxRender
1 parent b211b41 commit 96a823c

File tree

1 file changed

+34
-38
lines changed

1 file changed

+34
-38
lines changed

processing_app/basics/form/rgb_cube.rb

+34-38
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,69 @@
11
#!/usr/bin/env jruby
2+
# frozen_string_literal: true
3+
24
require 'propane'
5+
require 'arcball'
6+
java_import 'monkstone.vecmath.GfxRender'
37
# RGB Cube.
48
#
59
# The three primary colors of the additive color model are red, green, and blue.
610
# This RGB color cube displays smooth transitions between these colors.
711
class RgbCube < Propane::App
8-
912
attr_reader :box_points
1013

1114
def setup
1215
sketch_title 'RGB Cube'
1316
no_stroke
14-
color_mode RGB, 2
15-
@xmag = 0
16-
@ymag = 0
17-
@new_xmag = 0
18-
@new_ymag = 0
19-
# Math.since each point is used three times
17+
Processing::ArcBall.init(self, width / 2, height / 2)
18+
2019
@box_points = {
21-
top_front_left: [-1, 1, 1],
22-
top_front_right: [1, 1, 1],
23-
top_back_right: [1, 1, -1],
24-
top_back_left: [-1, 1, -1],
25-
bottom_front_left: [-1, -1, 1],
26-
bottom_front_right: [1, -1, 1],
27-
bottom_back_right: [1, -1, -1],
28-
bottom_back_left: [-1, -1, -1]
20+
top_front_left: Vec3D.new(-90, 90, 90),
21+
top_front_right: Vec3D.new(90, 90, 90),
22+
top_back_right: Vec3D.new(90, 90, -90),
23+
top_back_left: Vec3D.new(-90, 90, -90),
24+
bottom_front_left: Vec3D.new(-90, -90, 90),
25+
bottom_front_right: Vec3D.new(90, -90, 90),
26+
bottom_back_right: Vec3D.new(90, -90, -90),
27+
bottom_back_left: Vec3D.new(-90, -90, -90)
2928
}
3029
# a box from defined points
3130
@box = {
32-
top: [box_points[:top_front_left], box_points[:top_front_right], box_points[:top_back_right], box_points[:top_back_left]],
33-
front: [box_points[:top_front_left], box_points[:top_front_right], box_points[:bottom_front_right], box_points[:bottom_front_left]],
34-
left: [box_points[:top_front_left], box_points[:bottom_front_left], box_points[:bottom_back_left], box_points[:top_back_left]],
35-
back: [box_points[:top_back_left], box_points[:top_back_right], box_points[:bottom_back_right], box_points[:bottom_back_left]],
36-
right: [box_points[:top_back_right], box_points[:bottom_back_right], box_points[:bottom_front_right], box_points[:top_front_right]],
37-
bottom: [box_points[:bottom_front_left], box_points[:bottom_front_right], box_points[:bottom_back_right], box_points[:bottom_back_left]]
31+
top: [box_points[:top_front_left], box_points[:top_front_right], box_points[:top_back_right],
32+
box_points[:top_back_left]],
33+
front: [box_points[:top_front_left], box_points[:top_front_right], box_points[:bottom_front_right],
34+
box_points[:bottom_front_left]],
35+
left: [box_points[:top_front_left], box_points[:bottom_front_left], box_points[:bottom_back_left],
36+
box_points[:top_back_left]],
37+
back: [box_points[:top_back_left], box_points[:top_back_right], box_points[:bottom_back_right],
38+
box_points[:bottom_back_left]],
39+
right: [box_points[:top_back_right], box_points[:bottom_back_right], box_points[:bottom_front_right],
40+
box_points[:top_front_right]],
41+
bottom: [box_points[:bottom_front_left], box_points[:bottom_front_right], box_points[:bottom_back_right],
42+
box_points[:bottom_back_left]]
3843
}
3944
end
4045

4146
def draw
4247
background 1
43-
push_matrix
44-
translate width / 2, height / 2, -30
45-
@new_xmag = mouse_x.to_f / width * TAU
46-
@new_ymag = mouse_y.to_f / height * TAU
47-
diff = @xmag - @new_xmag
48-
@xmag -= diff / 4 if diff.abs > 0.01
49-
diff = @ymag - @new_ymag
50-
@ymag -= diff / 4 if diff.abs > 0.01
51-
rotate_x(-@ymag)
52-
rotate_y(-@xmag)
53-
scale 90
5448
begin_shape QUADS
55-
%i(top front left back right bottom).each do |s|
49+
%i[top front left back right bottom].each do |s|
5650
@box[s].each do |p|
5751
fill_from_points p
58-
vertex_from_points p
52+
p.to_vertex(renderer)
5953
end
6054
end
6155
end_shape
62-
pop_matrix
6356
end
6457

6558
def fill_from_points(points)
66-
fill points[0] + 1, points[1] + 1, points[2] + 1 # "+1" translates -1,1 to 0,2
59+
red = map1d(points.x, -90..90, 0..255)
60+
blue = map1d(points.y, -90..90, 0..255)
61+
green = map1d(points.z, -90..90, 0..255)
62+
fill(red, blue, green)
6763
end
6864

69-
def vertex_from_points(points)
70-
vertex(*points)
65+
def renderer
66+
@renderer ||= GfxRender.new(g)
7167
end
7268

7369
def settings

0 commit comments

Comments
 (0)