|
| 1 | +java_import 'monkstone.vecmath.GfxRender' |
1 | 2 | # RGB Cube. |
2 | 3 | # |
3 | 4 | # The three primary colors of the additive color model are red, green, and blue. |
|
8 | 9 | def setup |
9 | 10 | sketch_title 'RGB Cube' |
10 | 11 | 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 | + |
17 | 14 | @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) |
26 | 23 | } |
27 | 24 | # a box from defined points |
28 | 25 | @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]] |
35 | 38 | } |
36 | 39 | end |
37 | 40 |
|
38 | 41 | def draw |
39 | 42 | 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 |
51 | 43 | begin_shape QUADS |
52 | 44 | %i[top front left back right bottom].each do |s| |
53 | 45 | @box[s].each do |p| |
54 | 46 | fill_from_points p |
55 | | - vertex_from_points p |
| 47 | + p.to_vertex(renderer) |
56 | 48 | end |
57 | 49 | end |
58 | 50 | end_shape |
59 | | - pop_matrix |
60 | 51 | end |
61 | 52 |
|
62 | 53 | 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) |
64 | 58 | end |
65 | 59 |
|
66 | | -def vertex_from_points(points) |
67 | | - vertex(*points) |
| 60 | +def renderer |
| 61 | + @renderer ||= GfxRender.new(g) |
68 | 62 | end |
69 | 63 |
|
70 | 64 | def settings |
|
0 commit comments