Skip to content

Commit ad1b0cb

Browse files
committed
bloom demo
1 parent d5bcea8 commit ad1b0cb

File tree

2 files changed

+173
-17
lines changed

2 files changed

+173
-17
lines changed
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
load_library :PixelFlow
2+
java_import 'com.thomasdiewald.pixelflow.java.DwPixelFlow'
3+
java_import 'com.thomasdiewald.pixelflow.java.imageprocessing.filter.DwFilter'
4+
#
5+
# PixelFlow | Copyright (C) 2016 Thomas Diewald - http://thomasdiewald.com
6+
#
7+
# A Processing/Java library for high performance GPU-Computing (GLSL).
8+
# MIT License: https://opensource.org/licenses/MIT
9+
#
10+
#
11+
# Bloom Shader applied as post-processing effect on a simple 2D scene.
12+
#
13+
VIEWPORT_W = 1280
14+
VIEWPORT_H = 720
15+
VIEWPORT_X = 230
16+
VIEWPORT_Y = 0
17+
18+
attr_reader :context, :filter, :pg_render, :pg_luminance, :pg_bloom, :font12
19+
attr_reader :display_mode
20+
21+
def settings
22+
size(VIEWPORT_W, VIEWPORT_H, P2D)
23+
smooth(0)
24+
end
25+
26+
def setup
27+
surface.setLocation(VIEWPORT_X, VIEWPORT_Y)
28+
@display_mode = '1'
29+
@context = DwPixelFlow.new(self)
30+
context.print
31+
context.printGL
32+
@filter = DwFilter.new(context)
33+
@pg_render = create_graphics(width, height, P2D)
34+
pg_render.smooth(8)
35+
@pg_luminance = create_graphics(width, height, P2D)
36+
pg_luminance.smooth(8)
37+
@pg_bloom = create_graphics(width, height, P2D)
38+
pg_bloom.smooth(8)
39+
@font12 = create_font("SourceCodePro-Regular", 12)
40+
background(0)
41+
# frame_rate(60)
42+
frame_rate(1000)
43+
end
44+
45+
def draw
46+
# just draw something into pg_src_A
47+
pg_render.begin_draw
48+
pg_render.blend_mode(BLEND) # default
49+
pg_render.rect_mode(CENTER)
50+
pg_render.background(0)
51+
num_x = 20
52+
num_y = (num_x / (width / height)).ceil
53+
size_x = (width - 1) / num_x.to_f
54+
size_y = (height - 1) / num_y.to_f
55+
scale_xy = 0.4
56+
grid(num_y, num_x) do |y, x| # using the JRubyArt convenience grid method
57+
px = x * size_x + size_x * 0.5
58+
py = y * size_y + size_y * 0.5
59+
norm_x = x / (num_x - 1).to_f
60+
norm_y = y / (num_y - 1).to_f
61+
rgb_r = 255 - 255 * norm_x * norm_y
62+
rgb_g = norm_x * 255
63+
rgb_b = norm_y * 255
64+
col = color(rgb_r, rgb_g, rgb_b)
65+
pg_render.push_matrix
66+
pg_render.translate(px, py)
67+
pg_render.stroke_weight(2)
68+
pg_render.stroke(col)
69+
if( ((x^y)&1) == 0)
70+
pg_render.fill(col)
71+
else
72+
pg_render.no_fill
73+
end
74+
pg_render.rect(0, 0, size_x * scale_xy, size_y * scale_xy, 0)
75+
pg_render.pop_matrix
76+
end
77+
pg_render.stroke(255)
78+
pg_render.fill(0, 240)
79+
pg_render.stroke_weight(2)
80+
pg_render.ellipse(mouse_x, mouse_y, height / 4, height / 4)
81+
pg_render.end_draw
82+
unless display_mode == '0'
83+
# luminance pass
84+
filter.luminance_threshold.param.threshold = 0.0 # when 0, all colors are used
85+
filter.luminance_threshold.param.exponent = 5
86+
filter.luminance_threshold.apply(pg_render, pg_luminance)
87+
88+
# bloom pass
89+
# if the original image is used as source, the previous luminance pass
90+
# can just be skipped
91+
filter.bloom.param.mult = map1d(mouse_x, 0..width, 0..10)
92+
filter.bloom.param.radius = map1d(mouse_y, 0..height, 0..1)
93+
filter.bloom.apply(pg_luminance, pg_bloom, pg_render)
94+
end
95+
case(display_mode)
96+
when '2'
97+
filter.copy.apply(pg_bloom, pg_render)
98+
when '3'
99+
filter.copy.apply(pg_luminance, pg_render)
100+
when '4'
101+
filter.copy.apply(filter.bloom.gaussianpyramid.tex_blur[0], pg_render)
102+
when '5'
103+
filter.copy.apply(filter.bloom.gaussianpyramid.tex_blur[1], pg_render)
104+
when '6'
105+
filter.copy.apply(filter.bloom.gaussianpyramid.tex_blur[2], pg_render)
106+
when '7'
107+
filter.copy.apply(filter.bloom.gaussianpyramid.tex_blur[3], pg_render)
108+
when '8'
109+
filter.copy.apply(filter.bloom.gaussianpyramid.tex_blur[4], pg_render)
110+
end
111+
# display result
112+
blend_mode(REPLACE)
113+
background(0)
114+
image(pg_render, 0, 0)
115+
116+
# mouse position hints
117+
s = 3
118+
blend_mode(BLEND)
119+
stroke_cap(SQUARE)
120+
stroke_weight(s * 2)
121+
stroke(0, 50)
122+
line(0, s, width, s)
123+
line(s, 0, s, height)
124+
stroke(0, 160)
125+
line(0, s, mouse_x, s)
126+
line(s, 0, s, mouse_y)
127+
# info
128+
format_string = 'Bloom Demo [size %d/%d] [mode %d] [frame %d] [fps: (%6.2f)]'
129+
fps = format(format_string, pg_render.width, pg_render.height, display_mode, frame_count, frame_rate)
130+
surface.set_title(fps)
131+
# ke_y hints
132+
text_font(font12)
133+
tx = 15
134+
ty = 15
135+
gap = 15
136+
stroke(255)
137+
text(fps, tx, ty += gap)
138+
text("'0' bloom OFF" , tx, ty += gap)
139+
text("'1' Bloom ON" , tx, ty += gap)
140+
text("'2' bloom only", tx, ty += gap)
141+
text("'3' Luminance" , tx, ty += gap)
142+
text("'4' Blur[0]" , tx, ty += gap)
143+
text("'5' Blur[1]" , tx, ty += gap)
144+
text("'6' Blur[2]" , tx, ty += gap)
145+
text("'7' Blur[3]" , tx, ty += gap)
146+
text("'8' Blur[4]" , tx, ty += gap)
147+
text("mouseX: bloom multiplier" , tx, ty += gap)
148+
text("mouseY: bloom radius" , tx, ty += gap)
149+
end
150+
151+
def key_released
152+
case key
153+
when '0'..'8'
154+
@display_mode = key
155+
end
156+
end

external_library/java/PixelFlow/reaction_diffusion.rb

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
load_library :PixelFlow
22

3-
# PixelFlow | Copyright (C) 2016 Thomas Diewald - http://thomasdiewald.com
4-
# translated to JRubyArt by MArtihn Prout
3+
# PixelFlow | Copyright (C) 2016-17 Thomas Diewald - http://thomasdiewald.com
4+
# translated to JRubyArt by Martin Prout
55
# A Processing/Java library for high performance GPU-Computing (GLSL).
66
# MIT License: https://opensource.org/licenses/MIT
77
module Reaction
@@ -50,29 +50,29 @@ def setup
5050
# tex_grayscott.resize(context, GL2::GL_RG16_SNORM, width, height, GL2::GL_RG, GL2::GL_FLOAT, GL2::GL_NEAREST, 2, 2)
5151

5252
# glsl shader
53-
@shader_grayscott = context.createShader(data_path('grayscott.frag'))
54-
@shader_render = context.createShader(data_path('render.frag'))
53+
@shader_grayscott = context.create_shader(data_path('grayscott.frag'))
54+
@shader_render = context.create_shader(data_path('render.frag'))
5555
# init
5656
@tex_render = create_graphics(width, height, P2D)
5757
tex_render.smooth(0)
58-
tex_render.beginDraw
59-
tex_render.textureSampling(2)
60-
tex_render.blendMode(REPLACE)
58+
tex_render.begin_draw
59+
tex_render.texture_sampling(2)
60+
tex_render.blend_mode(REPLACE)
6161
tex_render.clear
62-
tex_render.noStroke
62+
tex_render.no_stroke
6363
tex_render.background(0x00FF0000) # NB: we can use hexadecimal in ruby
6464
tex_render.fill(0x0000FF00)
65-
tex_render.noStroke
66-
tex_render.rectMode(CENTER)
65+
tex_render.no_stroke
66+
tex_render.rect_mode(CENTER)
6767
tex_render.rect(width / 2, height / 2, 20, 20)
68-
tex_render.endDraw
68+
tex_render.end_draw
6969
# copy initial data to source texture
7070
DwFilter.get(context).copy.apply(tex_render, tex_grayscott.src)
7171
frame_rate(1_000)
7272
end
7373

7474
def reaction_diffusion_pass
75-
context.beginDraw(tex_grayscott.dst)
75+
context.begin_draw(tex_grayscott.dst)
7676
shader_grayscott.begin
7777
shader_grayscott.uniform1f('dA', 1.0)
7878
shader_grayscott.uniform1f('dB', 0.5)
@@ -81,9 +81,9 @@ def reaction_diffusion_pass
8181
shader_grayscott.uniform1f('dt', 1)
8282
shader_grayscott.uniform2f('wh_rcp', 1.0 / width, 1.0 / height)
8383
shader_grayscott.uniformTexture('tex', tex_grayscott.src)
84-
shader_grayscott.drawFullScreenQuad
84+
shader_grayscott.draw_full_screen_quad
8585
shader_grayscott.end
86-
context.endDraw('reaction_diffusion_pass')
86+
context.end_draw('reaction_diffusion_pass')
8787
tex_grayscott.swap
8888
@pass += 1
8989
end
@@ -93,16 +93,16 @@ def draw
9393
context.begin
9494
100.times { reaction_diffusion_pass }
9595
# create display texture
96-
context.beginDraw(tex_render)
96+
context.begin_draw(tex_render)
9797
shader_render.begin
9898
shader_render.uniform2f('wh_rcp', 1.0 / width, 1.0 / height)
9999
shader_render.uniformTexture('tex', tex_grayscott.src)
100100
shader_render.drawFullScreenQuad
101101
shader_render.end
102-
context.endDraw('render')
102+
context.end_draw('render')
103103
context.end
104104
# put it on the screen
105-
blendMode(REPLACE)
105+
blend_mode(REPLACE)
106106
image(tex_render, 0, 0)
107107
save_frame(data_path('grayscott1.jpg')) if frame_count == 1_000
108108
format_string = 'Reaction Diffusion [size %d/%d] [frame %d] [fps: (%6.2f)]'

0 commit comments

Comments
 (0)