|
| 1 | +#!/usr/bin/env jruby |
| 2 | +# frozen_string_literal: true |
| 3 | + |
1 | 4 | require 'picrate'
|
| 5 | +require 'arcball' |
| 6 | +java_import 'monkstone.vecmath.GfxRender' |
2 | 7 | # RGB Cube.
|
3 | 8 | #
|
4 | 9 | # The three primary colors of the additive color model are red, green, and blue.
|
5 | 10 | # This RGB color cube displays smooth transitions between these colors.
|
6 | 11 | class RgbCube < Processing::App
|
7 |
| - |
8 | 12 | attr_reader :box_points
|
9 | 13 |
|
10 | 14 | def setup
|
11 | 15 | sketch_title 'RGB Cube'
|
12 | 16 | no_stroke
|
13 |
| - color_mode RGB, 2 |
14 |
| - @xmag = 0 |
15 |
| - @ymag = 0 |
16 |
| - @new_xmag = 0 |
17 |
| - @new_ymag = 0 |
18 |
| - # Math.since each point is used three times |
| 17 | + Processing::ArcBall.init(self, width / 2, height / 2) |
| 18 | + |
19 | 19 | @box_points = {
|
20 |
| - top_front_left: [-1, 1, 1], |
21 |
| - top_front_right: [1, 1, 1], |
22 |
| - top_back_right: [1, 1, -1], |
23 |
| - top_back_left: [-1, 1, -1], |
24 |
| - bottom_front_left: [-1, -1, 1], |
25 |
| - bottom_front_right: [1, -1, 1], |
26 |
| - bottom_back_right: [1, -1, -1], |
27 |
| - 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) |
28 | 28 | }
|
29 | 29 | # a box from defined points
|
30 | 30 | @box = {
|
31 |
| - top: [box_points[:top_front_left], box_points[:top_front_right], box_points[:top_back_right], box_points[:top_back_left]], |
32 |
| - front: [box_points[:top_front_left], box_points[:top_front_right], box_points[:bottom_front_right], box_points[:bottom_front_left]], |
33 |
| - left: [box_points[:top_front_left], box_points[:bottom_front_left], box_points[:bottom_back_left], box_points[:top_back_left]], |
34 |
| - back: [box_points[:top_back_left], box_points[:top_back_right], box_points[:bottom_back_right], box_points[:bottom_back_left]], |
35 |
| - right: [box_points[:top_back_right], box_points[:bottom_back_right], box_points[:bottom_front_right], box_points[:top_front_right]], |
36 |
| - 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]] |
37 | 43 | }
|
38 | 44 | end
|
39 | 45 |
|
40 | 46 | def draw
|
41 | 47 | background 1
|
42 |
| - push_matrix |
43 |
| - translate width / 2, height / 2, -30 |
44 |
| - @new_xmag = mouse_x.to_f / width * TAU |
45 |
| - @new_ymag = mouse_y.to_f / height * TAU |
46 |
| - diff = @xmag - @new_xmag |
47 |
| - @xmag -= diff / 4 if diff.abs > 0.01 |
48 |
| - diff = @ymag - @new_ymag |
49 |
| - @ymag -= diff / 4 if diff.abs > 0.01 |
50 |
| - rotate_x(-@ymag) |
51 |
| - rotate_y(-@xmag) |
52 |
| - scale 90 |
53 | 48 | begin_shape QUADS
|
54 |
| - %i(top front left back right bottom).each do |s| |
| 49 | + %i[top front left back right bottom].each do |s| |
55 | 50 | @box[s].each do |p|
|
56 | 51 | fill_from_points p
|
57 |
| - vertex_from_points p |
| 52 | + p.to_vertex(renderer) |
58 | 53 | end
|
59 | 54 | end
|
60 | 55 | end_shape
|
61 |
| - pop_matrix |
62 | 56 | end
|
63 | 57 |
|
64 | 58 | def fill_from_points(points)
|
65 |
| - 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) |
66 | 63 | end
|
67 | 64 |
|
68 |
| - def vertex_from_points(points) |
69 |
| - vertex(*points) |
| 65 | + def renderer |
| 66 | + @renderer ||= GfxRender.new(g) |
70 | 67 | end
|
71 | 68 |
|
72 | 69 | def settings
|
|
0 commit comments