|
1 | | -#!/usr/bin/env jruby |
2 | | - |
3 | | -require 'propane' |
4 | | -# |
5 | | -# PixelFlow | Copyright (C) 2017 Thomas Diewald (www.thomasdiewald.com) |
6 | | -# Translated to JRubyArt by Martin Prout |
7 | | -# |
8 | | -# src - www.github.com/diwi/PixelFlow |
9 | | -# |
10 | | -# A Processing/Java library for high performance GPU-Computing. |
11 | | -# MIT License: https://opensource.org/licenses/MIT |
12 | | -# |
13 | | -class SkylightBasic < Propane::App |
14 | | - load_libraries :peasycam, :pixel_flow |
15 | | - |
16 | | - module Skylight # Namespace for java classes |
17 | | - java_import 'com.thomasdiewald.pixelflow.java.DwPixelFlow' |
18 | | - # java_import 'com.thomasdiewald.pixelflow.java.render.skylight.DwSceneDisplay' |
19 | | - java_import 'com.thomasdiewald.pixelflow.java.render.skylight.DwSkyLight' |
20 | | - java_import 'com.thomasdiewald.pixelflow.java.utils.DwBoundingSphere' |
21 | | - java_import 'com.thomasdiewald.pixelflow.java.utils.DwVertexRecorder' |
22 | | - java_import 'peasy.PeasyCam' |
23 | | - end |
24 | | - |
25 | | - include Skylight |
26 | | - # Basic setup for the Skylight renderer. |
27 | | - # |
28 | | - # Its important to compute or define a most optimal bounding-sphere for the |
29 | | - # scene. self can be done manually or automatically, as shown in self example. |
30 | | - # |
31 | | - # Any existing sketch utilizing the P3D renderer can be extended to use the |
32 | | - # Skylight renderer. |
33 | | - # |
34 | | - VIEWPORT_W = 1280 |
35 | | - VIEWPORT_H = 720 |
36 | | - VIEWPORT_X = 230 |
37 | | - VIEWPORT_Y = 0 |
38 | | - attr_reader :peasycam, :shape, :skylight, :cam_active, :cam_pos |
39 | | - |
40 | | - def settings |
41 | | - size(VIEWPORT_W, VIEWPORT_H, P3D) |
42 | | - smooth(0) |
43 | | - end |
44 | | - |
45 | | - def setup |
46 | | - surface.setLocation(VIEWPORT_X, VIEWPORT_Y) |
47 | | - # camera |
48 | | - @peasycam = PeasyCam.new(self, -4.083, -6.096, 7.000, 1500) |
49 | | - peasycam.set_rotations(1.085, -0.477, 2.910) |
50 | | - peasycam.set_distance(100) |
51 | | - @cam_pos = [0, 0, 0] |
52 | | - @cam_active = false |
53 | | - # projection |
54 | | - perspective(60 * DEG_TO_RAD, width / height.to_f, 2, 5000) |
55 | | - # load obj file into shape-object |
56 | | - @shape = load_shape(data_path('skylight_demo_scene.obj')) |
57 | | - # record list of vertices of the given shape |
58 | | - vertex_recorder = DwVertexRecorder.new(self, shape) |
59 | | - # compute scene bounding-sphere |
60 | | - scene_bs = DwBoundingSphere.new |
61 | | - scene_bs.compute(vertex_recorder.verts, vertex_recorder.verts_count) |
62 | | - # used for centering and re-scaling the scene |
63 | | - mat_scene_bounds = scene_bs.getUnitSphereMatrix |
64 | | - # library context |
65 | | - context = DwPixelFlow.new(self) |
66 | | - context.print |
67 | | - context.printGL |
68 | | - # callback DwSceneDisplay for rendering scene, implementa interface as a proc |
69 | | - display = -> (canvas) do |
70 | | - if(canvas == skylight.renderer.pg_render) |
71 | | - canvas.background(32) |
72 | | - end |
73 | | - canvas.shape(shape) |
74 | | - end |
75 | | - |
76 | | - # init skylight renderer |
77 | | - @skylight = DwSkyLight.new(context, display, mat_scene_bounds) |
78 | | - # parameters for sky-light |
79 | | - skylight.sky.param.iterations = 50 |
80 | | - skylight.sky.param.solar_azimuth = 0 |
81 | | - skylight.sky.param.solar_zenith = 0 |
82 | | - skylight.sky.param.sample_focus = 1 # full sphere sampling |
83 | | - skylight.sky.param.intensity = 1.0 |
84 | | - skylight.sky.param.rgb = [1.0, 1.0, 1.0] |
85 | | - skylight.sky.param.shadowmap_size = 256 # quality vs. performance |
86 | | - # parameters for sun-light |
87 | | - skylight.sun.param.iterations = 50 |
88 | | - skylight.sun.param.solar_azimuth = 45 |
89 | | - skylight.sun.param.solar_zenith = 55 |
90 | | - skylight.sun.param.sample_focus = 0.05 |
91 | | - skylight.sun.param.intensity = 1.0 |
92 | | - skylight.sun.param.rgb = [1.0, 1.0, 1.0] |
93 | | - skylight.sun.param.shadowmap_size = 512 |
94 | | - frame_rate(1000) |
95 | | - end |
96 | | - |
97 | | - def draw |
98 | | - # when the camera moves, the renderer restarts |
99 | | - update_cam_active |
100 | | - skylight.reset if(cam_active) |
101 | | - # update renderer |
102 | | - skylight.update |
103 | | - peasycam.beginHUD |
104 | | - # display result |
105 | | - image(skylight.renderer.pg_render, 0, 0) |
106 | | - # image(skylight.sky.getSrc, 0, 0) |
107 | | - peasycam.endHUD |
108 | | - # some info, window title |
109 | | - sun_pass = skylight.sun.RENDER_PASS |
110 | | - sky_pass = skylight.sky.RENDER_PASS |
111 | | - title_format = 'Basic Skylight | sun: %d sky: %d fps: %6.2f' |
112 | | - surface.set_title(format(title_format, sun_pass, sky_pass, frame_rate)) |
113 | | - end |
114 | | - |
115 | | - def update_cam_active |
116 | | - cam_pos_curr = peasycam.getPosition |
117 | | - @cam_active = false |
118 | | - @cam_active |= cam_pos_curr[0] != cam_pos[0] |
119 | | - @cam_active |= cam_pos_curr[1] != cam_pos[1] |
120 | | - @cam_active |= cam_pos_curr[2] != cam_pos[2] |
121 | | - @cam_pos = cam_pos_curr |
122 | | - end |
123 | | - |
124 | | - def print_camera |
125 | | - pos = peasycam.get_position |
126 | | - rot = peasycam.get_rotations |
127 | | - lat = peasycam.get_look_at |
128 | | - dis = peasycam.get_distance |
129 | | - cam_format = '%s: (%7.3f, %7.3f, %7.3f)' |
130 | | - dist_format = 'distance: (%7.3f)' |
131 | | - puts format(cam_format, 'position', pos[0], pos[1], pos[2]) |
132 | | - puts format(cam_format, 'rotation', rot[0], rot[1], rot[2]) |
133 | | - puts format(cam_format, 'look_at', lat[0], lat[1], lat[2]) |
134 | | - puts format(dist_format, dis) |
135 | | - end |
136 | | - |
137 | | - def key_released |
138 | | - print_camera |
139 | | - end |
140 | | -end |
141 | | - |
142 | | -SkylightBasic.new |
| 1 | +#!/usr/bin/env jruby |
| 2 | + |
| 3 | +require 'propane' |
| 4 | +# |
| 5 | +# PixelFlow | Copyright (C) 2017 Thomas Diewald (www.thomasdiewald.com) |
| 6 | +# Translated to propane by Martin Prout |
| 7 | +# |
| 8 | +# src - www.github.com/diwi/PixelFlow |
| 9 | +# |
| 10 | +# A Processing/Java library for high performance GPU-Computing. |
| 11 | +# MIT License: https://opensource.org/licenses/MIT |
| 12 | +# |
| 13 | +class SkylightBasic < Propane::App |
| 14 | + load_libraries :peasycam, :pixel_flow |
| 15 | + # Basic setup for the Skylight renderer. |
| 16 | + # |
| 17 | + # Its important to compute or define a most optimal bounding-sphere for the |
| 18 | + # scene. self can be done manually or automatically, as shown in self example. |
| 19 | + # |
| 20 | + # Any existing sketch utilizing the P3D renderer can be extended to use the |
| 21 | + # Skylight renderer. |
| 22 | + module Skylight # Namespace for java classes |
| 23 | + java_import 'com.thomasdiewald.pixelflow.java.DwPixelFlow' |
| 24 | + java_import 'com.thomasdiewald.pixelflow.java.render.skylight.DwSkyLight' |
| 25 | + java_import 'com.thomasdiewald.pixelflow.java.utils.DwBoundingSphere' |
| 26 | + java_import 'com.thomasdiewald.pixelflow.java.utils.DwVertexRecorder' |
| 27 | + java_import 'peasy.PeasyCam' |
| 28 | + end |
| 29 | + |
| 30 | + include Skylight # so we don't need fully qualified names for java classes |
| 31 | + |
| 32 | + VIEWPORT_W = 1280 |
| 33 | + VIEWPORT_H = 720 |
| 34 | + VIEWPORT_X = 230 |
| 35 | + VIEWPORT_Y = 0 |
| 36 | + attr_reader :peasycam, :shape, :skylight, :cam_active, :cam_pos |
| 37 | + |
| 38 | + def settings |
| 39 | + size(VIEWPORT_W, VIEWPORT_H, P3D) |
| 40 | + smooth(0) |
| 41 | + end |
| 42 | + |
| 43 | + def setup |
| 44 | + surface.setLocation(VIEWPORT_X, VIEWPORT_Y) |
| 45 | + # camera |
| 46 | + @peasycam = PeasyCam.new(self, -4.083, -6.096, 7.000, 1500) |
| 47 | + peasycam.set_rotations(1.085, -0.477, 2.910) |
| 48 | + peasycam.set_distance(100) |
| 49 | + @cam_pos = [0, 0, 0] |
| 50 | + @cam_active = false |
| 51 | + # projection |
| 52 | + perspective(60 * DEG_TO_RAD, width / height.to_f, 2, 5000) |
| 53 | + # load obj file into shape-object |
| 54 | + @shape = load_shape(data_path('skylight_demo_scene.obj')) |
| 55 | + # record list of vertices of the given shape |
| 56 | + vertex_recorder = DwVertexRecorder.new(self, shape) |
| 57 | + # compute scene bounding-sphere |
| 58 | + scene_bs = DwBoundingSphere.new |
| 59 | + scene_bs.compute(vertex_recorder.verts, vertex_recorder.verts_count) |
| 60 | + # used for centering and re-scaling the scene |
| 61 | + mat_scene_bounds = scene_bs.getUnitSphereMatrix |
| 62 | + # library context |
| 63 | + context = DwPixelFlow.new(self) |
| 64 | + context.print |
| 65 | + context.printGL |
| 66 | + # callback for rendering scene, implements DwSceneDisplay interface |
| 67 | + display = lambda do |canvas| |
| 68 | + canvas.background(32) if canvas == skylight.renderer.pg_render |
| 69 | + canvas.shape(shape) |
| 70 | + end |
| 71 | + # init skylight renderer |
| 72 | + @skylight = DwSkyLight.new(context, display, mat_scene_bounds) |
| 73 | + # parameters for sky-light |
| 74 | + param = skylight.sky.param |
| 75 | + param.iterations = 50 |
| 76 | + param.solar_azimuth = 0 |
| 77 | + param.solar_zenith = 0 |
| 78 | + param.sample_focus = 1 # full sphere sampling |
| 79 | + param.intensity = 1.0 |
| 80 | + param.rgb = [1.0, 1.0, 1.0] |
| 81 | + param.shadowmap_size = 256 # quality vs. performance |
| 82 | + # parameters for sun-light |
| 83 | + param = skylight.sun.param |
| 84 | + param.iterations = 50 |
| 85 | + param.solar_azimuth = 45 |
| 86 | + param.solar_zenith = 55 |
| 87 | + param.sample_focus = 0.05 |
| 88 | + param.intensity = 1.0 |
| 89 | + param.rgb = [1.0, 1.0, 1.0] |
| 90 | + param.shadowmap_size = 512 |
| 91 | + frame_rate(1000) |
| 92 | + end |
| 93 | + |
| 94 | + def draw |
| 95 | + # when the camera moves, the renderer restarts |
| 96 | + update_cam_active |
| 97 | + skylight.reset if cam_active |
| 98 | + # update renderer |
| 99 | + skylight.update |
| 100 | + peasycam.beginHUD |
| 101 | + # display result |
| 102 | + image(skylight.renderer.pg_render, 0, 0) |
| 103 | + # image(skylight.sky.getSrc, 0, 0) |
| 104 | + peasycam.endHUD |
| 105 | + # some info, window title |
| 106 | + sun_pass = skylight.sun.RENDER_PASS |
| 107 | + sky_pass = skylight.sky.RENDER_PASS |
| 108 | + title_format = 'Basic Skylight | sun: %d sky: %d fps: %6.2f' |
| 109 | + surface.set_title(format(title_format, sun_pass, sky_pass, frame_rate)) |
| 110 | + end |
| 111 | + |
| 112 | + def update_cam_active |
| 113 | + cam_pos_curr = peasycam.getPosition |
| 114 | + @cam_active = false |
| 115 | + @cam_active |= cam_pos_curr[0] != cam_pos[0] |
| 116 | + @cam_active |= cam_pos_curr[1] != cam_pos[1] |
| 117 | + @cam_active |= cam_pos_curr[2] != cam_pos[2] |
| 118 | + @cam_pos = cam_pos_curr |
| 119 | + end |
| 120 | + |
| 121 | + def print_camera |
| 122 | + pos = peasycam.get_position |
| 123 | + rot = peasycam.get_rotations |
| 124 | + lat = peasycam.get_look_at |
| 125 | + dis = peasycam.get_distance |
| 126 | + cam_format = '%s: (%7.3f, %7.3f, %7.3f)' |
| 127 | + dist_format = 'distance: (%7.3f)' |
| 128 | + puts format(cam_format, 'position', pos[0], pos[1], pos[2]) |
| 129 | + puts format(cam_format, 'rotation', rot[0], rot[1], rot[2]) |
| 130 | + puts format(cam_format, 'look_at', lat[0], lat[1], lat[2]) |
| 131 | + puts format(dist_format, dis) |
| 132 | + end |
| 133 | + |
| 134 | + def key_released |
| 135 | + print_camera |
| 136 | + end |
| 137 | +end |
| 138 | + |
| 139 | +SkylightBasic.new |
0 commit comments