We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I was thinking of better ways to make the previous frame's image disppear rather than starting each off with a blank by doing background(0);
or something. I slapped a filter(BLUR);
at the end to make each previous frame diminish smoothly but it literally brought my fps from 50-60 down to under 5... just that one line. Is that supposed to be right?
I'm using Processing 2.2.1 with P3D and fullscreen. What techniques do you use to do the above?
Answers
You should use a shader for the blur. By default it goes on the CPU. Look at examples > topics> shaders > sepBlur
That should help a lot.
clankill3r is right,
BLUR
is just anint
variable "pointing" to a CPU based filter. Use a hardware accelerated PShader instead.In case of gaussian blurring I'd recommend to use a two pass approach. This way you reduce the quadratic sample kernel (9 samples) to two linear sample kernels (2 * 3 samples) while getting the same effect:
In case of box blurring I'd stick to a single pass approach that samples between the actual pixels. This practically quadruples the actual sample count and almost creates the same visual effect as the gaussian blur:
While the gaussian blur is in general more "pleasing" to the eye, the optimized box blur is much faster and creates almost the same visual effect. I'd stick to the faster box blur.
Edit: Changed the first line in the filter's main() method to gl_FragColor = something in case the graphics card driver isn't initializing the value correctly.
Great, I will try the box blur out :)
Thanks for your answers. But as someone who doesn't know much about filtering... How does one apply these examples to a sketch which which blurs the canvas?
PImage theCanvas = get()
@clankill3r: Nice! Hope it works on your Mac. ;)
@joySeeing: Just like in my code snippet, load the PShaders in setup() or some dedicated method called by setup(), render your scene in draw() and then call filter():
@Poersch
About the box blur. Why the 0.5? Why is it not 1. So you look at the next pixel. Now you look at half a pixel away. Is it so you can use the filter in such a little amount that it is hardly visible?
Also changing the value to 4 for example and run the filter multiple times looks nice :)
@Poersch
I did update it for my mac :) Using
in
andout
andtexture
instead oftexture2D
in case your interested in writing my Mac compatible shaders :)@clankill3r:
The reason why I use "half offsets" is because it will force the graphics card to interpolate between 4 pixel values instead of using 1 (actually it always interpolates a little because of the low UV precision, so the performance is lost anyways). That means my second blur approach practically samples 16 pixels instead of just 4. Plus, using offsets equal to or greater than 1 will cause artifacts (at least if you use the filter in an accumulative manner) as you are not sampling your current pixel anymore. While this can look good and be exactly what you are looking for, it's not really a blur anymore.
0.5 pixel offset:
1.0 pixel offset:
In my real life projects I always depend on at least GLSL version 1.30 (in/out/texture/etc.), but on the forums I always try to use GLSL version 1.10 because it is (by specification) supported by any OpenGL hardware - don't know why apple seems to ignore the official specs though.
Ah cool that is done really smart :)
And if I learned one thing over the years, big companies != quality.
@clankill3r
How do you re-write his examples for Mac exactly? That's why I was confused... was just getting video noise...
I only did the box blur.
vert:
frag:
@clankill3r
With a standard 2015 Macbook Pro with Intel Iris graphics card, I get
Cannot compile vertex shader: ERROR: 0:1: '' : version '410' is not supported
Thanks for your help.
Video noise..? Could you post your Code?
@Poersch
Whenever I copy your code directly to my Macbook Pro Processing 2.2.1 I get this:
Using @clankill3r 's Mac adjusted code gives me a different error.
Poersch had some code to find what GLSL version your drivers supported but I can't find it back. That might help. I have set it for 410 in my case since that's what I have and I'm not sure if the shaders I port work < 410.
Ok, well that's fine. Finally, one thing missing that blur.glsl doesn't do that BLUR does is make things eventually fade into nothingness (or the illusion of this anyawys). Any clever recommendations to make that happen?
Just to make clear: By fading into nothingness, you meant that each frame the previous blur result got blurred again right?
Probably. Such that (simple example), if you drew a picture of a square and did not draw it again on the next frame, it would become progressively harder to see the square outline as the blurring would continue.
@joySeeing: Looking at your screenshot it seems like your graphics card driver isn't initializing
gl_FragColor
withvec4(0.0)
. Updated my examples, please try them again.@Poersch 4-o-clock?
gl_FragCoord
or did the update not get threw?@clankill3r: Argh! These typos! :D (changed it)
@Poersch
Thanks. And actually. I was playing around with the numbers and if I moved .5 (on second example) up to .99, everything begins to fade away quickly. Perfect!
Thanks
Actually, I don't really know what I'm talking about. Not sure what I did to make it do this.. Hmm...
The filter blur is very slow. Do your own one direction at a time with a "window" of pixels. That way the the speed of 3 pixel blur is about the same as a 256 pixel blur. With the window of pixels, after its filled its just one on and one off and the sum in the middle remains constant. So instead of 256 operations on a 256 pixel blur, you're doing 3. The 256 pixel blur I do on a 1000 x 1000 image takes 34 ms.
@joySeeing: Yeah, that will modify the blur weighting but the image will never reach the average color (because of rounding errors) you'll need a bigger blur kernel for that. Try the 2 pass gaussian blur with a far biffer blur kernel.
@shawnlau: Yeah, but a shader based blur is pretty fast. Shouldn't take more than a few thousand nanoseconds.