|
1 | 1 | #!/usr/bin/env jruby
|
| 2 | +# frozen_string_literal: true |
| 3 | + |
2 | 4 | require 'propane'
|
| 5 | +require 'arcball' |
| 6 | +java_import 'monkstone.vecmath.GfxRender' |
3 | 7 | # RGB Cube.
|
4 | 8 | #
|
5 | 9 | # The three primary colors of the additive color model are red, green, and blue.
|
6 | 10 | # This RGB color cube displays smooth transitions between these colors.
|
7 | 11 | class RgbCube < Propane::App
|
8 |
| - |
9 | 12 | attr_reader :box_points
|
10 | 13 |
|
11 | 14 | def setup
|
12 | 15 | sketch_title 'RGB Cube'
|
13 | 16 | 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 | + |
20 | 19 | @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) |
29 | 28 | }
|
30 | 29 | # a box from defined points
|
31 | 30 | @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]] |
38 | 43 | }
|
39 | 44 | end
|
40 | 45 |
|
41 | 46 | def draw
|
42 | 47 | 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 |
54 | 48 | 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| |
56 | 50 | @box[s].each do |p|
|
57 | 51 | fill_from_points p
|
58 |
| - vertex_from_points p |
| 52 | + p.to_vertex(renderer) |
59 | 53 | end
|
60 | 54 | end
|
61 | 55 | end_shape
|
62 |
| - pop_matrix |
63 | 56 | end
|
64 | 57 |
|
65 | 58 | 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) |
67 | 63 | end
|
68 | 64 |
|
69 |
| - def vertex_from_points(points) |
70 |
| - vertex(*points) |
| 65 | + def renderer |
| 66 | + @renderer ||= GfxRender.new(g) |
71 | 67 | end
|
72 | 68 |
|
73 | 69 | def settings
|
|
0 commit comments