r/FastLED • u/RubiCubix • Nov 21 '23
Discussion FastLED with native support for subpixel positioning
Hi,
I have been experimenting a bit with subpixel positioning on my Teensy 4.1 with a 64x64 led matrix after being inspired by this code by sutaburosu.
https://www.reddit.com/r/FastLED/comments/h7s96r/subpixel_positioning_wu_pixels/
So far I have only used it for some effects in particular and it works realy well. It gives a very nice smooth transition effect.
It would have been nice if this was natively supported by the FastLED library where you for instance could set the size of the "virtual" resolution as a parameter in the library so that the resolution that you work with in the code is higher in general.
So if I have a 64x64 LED Matrix I can configure the virtual resolution to be 10 times larger so that the virtual drawing area is 640x640.
By doing that you get 10 fractions between each pixel.
Are there any similar solutions already existing? Is there anything that prevents this from being made, some difficulties that I haven't thought about?
2
u/RubiCubix Nov 22 '23 edited Nov 22 '23
Thanks for your quick and informative responses . It makes perfect sense when you lay it out like that. It's much more efficient to handle this for each particular graphics primitive then the whole drawing area. I have some programming experience but I am quite new to Teensy controllers and controlling rgbs but I find it very interesting and amusing to work with.
As you mention, FastLED does not provide any graphics primitives so maybe it would be more relevant to have this implemented in any of the libraries that provides these such as FastLED-GFX or similar. Maybe it could be relevant for the LEDSprites library by AaronLiddiment as well.
Currently I have a simple application with different 2D primitives moving around and I want to achieve smoother movements. What I have in mind is modified drawing methods for these primitives that takes for instance float values or maybe scaled integer values and then these modified drawing methods does the math to calculate what to write to the Frame-buffer.
I'll see what I can come up with for this particular application.
Thank you for informing me of the requirement to use gamma correction to properly handle supersampling. I'll look into that if I get time. And thank you for the examples, I am sure they will give me some inspiration.
10
u/UrbanPugEsq Nov 21 '23
You’re talking about supersampling, or oversampling. It’s not an unreasonable approach given unlimited memory and computing power. However, the devices we tend to run LEDs on are fairly low powered devices.
To do what you want, you’d need to write your patterns to a big array, then, for each virtual pixel you’re doing some extra math to calculate the actual pixel, then you’re copying the result to a small array, which gets pushed to the led.
All of that adds a significant overhead to a small embedded device.
If you want to try it, it’s not all that hard to code up. I’d suggest starting at 2x or 4x as opposed to 10x. Remember that the number of virtual pixels increases to the square. So, going from 64x64 to 640x640 might seem like 10x more but it’s really 100x more pixels.
I’ve had success in getting smooth patterns by calculating the value of a pixel based on its distance to an object (point, line, or other shape) where the object is defined using floats or fixed point math.
4
u/sutaburosu [stavros] Nov 21 '23 edited Nov 21 '23
Thanks to /u/UrbanPugEsq for pointing out the major problems with rendering at a higher resolution and then down-sampling. This was the problem that wu_pixel code was intended to work around, by never needing the higher resolution intermediate buffer and instead providing the same end result without it.
Thus far FastLED provides no 2D graphics primitives, so setting a precedent by including this would impose a burden on the development team that they, rightfully, may not want to bear.
To do super-sampling correctly requires gamma correction. FastLED is fastidiously correct in many regards, but it currently has no concept of gamma correction, so that is another blocker to this code being integrated into the library. It's worth noting that the wu_pixel code also does not use gamma correction, and I'm sure it would look a lot better if it did.
Rendering 2D primitives (e.g. lines, circles, ellipses, bezier curves) with a 256x super-sampled appearance without needing a 65,536 times bigger frame-buffer has been a solved problem for decades now. What is needed is a sufficiently skilled and motivated programmer to create a library to complement FastLED with these techniques. I tried, and I quickly reached the limits of my smol, smooth brain. But I was aiming for fixed-point implementations; if your MCU has a fast FPU, you can pretty much copy the code out of white-papers unchanged.
That said, perhaps you might find some my old and neglected sketches, of which I am not proud, to be a source of inspiration:
3
u/Finndersen Nov 23 '23
I've developed a library for making patterns and mapping them to LED strip segments which supports sub-LED positioning/resolution with automatic interpolation and scaling. It only works for 1D patterns/effects at the moment but I'm planning on adding support for 2D matrices. It does support spatial (2D/3D) effects and mapping so depending on how the pattern works it could be implemented that way (each LED pixel will have a 2D location on a coordinate system and you can set colour value depending on that location). This uses floats for coordinate positions so has high resolution
https://github.com/Finndersen/LEDuino
0
u/[deleted] Nov 22 '23
[deleted] — view deleted comment