Featuring distance().
One of the important games we play while writing a script or software — or other kinds of black boxes, too — is, “How can we tell what’s going on inside?”
Modern debuggers in modern programming languages make this a nearly-solved problem. Sometimes. Maybe it’s a new language and the tools aren’t there yet, or maybe you’re too cheap to purchase them. Or maybe you just like debugging with print
statements. That’s cool.
In the case of Pixel Bender, I’ve been lately using several float2
variables to represent locations on the input or output. It turns out to be quite handy to drop “print” markers right in the output, to better see what’s going on.
The trick is extremely simple. If the distance to float2
value is within a few pixels, draw a color. Here’s the code to do it. It should be done last, after other pixel computations. That way it can be easily commented out or deleted before shipping.
float p1Dist = distance(p1,outCoord()); if(p1Dist < 3.0) dst.rgb = float3(1,0,0); // draw red else if (p1Dist < 5.0) dst.rgb = float3(0,0,0); // black outline
Handy!
Haven't figured out how to stamp little text figures into the output yet. You'd need to decompose your desired output on a per-pixel decision tree... would be an interesting logic-reduction problem, but perhaps impractical. It would be possible and useful to stamp 3 floating point values as "special debug pixels" at (0,0) and other easy to find locations.
At any rate, here's the whole kernel, ready for copy-and-pasting.
<languageVersion : 1.0;> kernel DebugMarkers<namespace:"ns";vendor:"omino.com";version:1;> { input image4 src; output pixel4 dst; parameter float2 p1<minValue:float2(0,0);maxValue:float2(400,400);>; parameter float2 p2<minValue:float2(0,0);maxValue:float2(400,400);>; void evaluatePixel() { float2 co = outCoord(); dst = sampleNearest(src,co); float p1Dist = distance(p1,co); if(p1Dist < 3.0) dst.rgb = float3(1,0,0); else if (p1Dist < 5.0) dst.rgb = float3(0,0,0); float p2Dist = distance(p2,co); if(p2Dist < 3.0) dst.rgb = float3(0,1,0); else if(p2Dist < 5.0) dst.rgb = float3(1,1,1); } }