jit.gl.slab glsl shader with non-matching in/out dimensions
I wrote a glsl shader that transforms images of fixed input size to an output with the same aspect ratio. Now I need to generalize to different output sizes and aspects (square), and I am stumped.
The existing shader uses
// source image size (set in vertex program sh.passthrudim.vp)
float w = jit_in.texdim0.x;
float h = jit_in.texdim0.y;
void main(void)
{
// output pixel coords, shift so that (0, 0) is at center
float x = jit_in.texcoord0[0] - w / 2;
float y = jit_in.texcoord0[1] - h / 2;
...
}According to https://docs.cycling74.com/userguide/jitter/jxs_file_format/, I can get at the output pixel size using
<param name="u_viewport" type="vec2" state="VIEWPORT" />
uniform vec2 u_viewport;
float outw = u_viewport.x;
float outh = u_viewport.y; but replacing w, h with outw, outh like so
float x = jit_in.texcoord0[0] - outw / 2;
float y = jit_in.texcoord0[1] - outh / 2;shifts the image in weird ways. There must be some unit mismatch between jit_in.texcoord0 and u_viewport which I can't figure out.
jit_in.texcoord0 represents normalized texture coordinates ranging from 0.0 to 1.0,
u_viewport represents absolute pixel sizes
Thanks, in the end, the working solution for me is simply correcting the aspect ratio b/w input texture and output dim (fixed square):
// source image size (set in vertex program sh.passthrudim.vp)
float w = jit_in.texdim0.x;
float h = jit_in.texdim0.y;
float aspectcorr = 1 / (w / h);
void main(void)
{
// output pixel coords, shift so that (0, 0) is at center
float x = jit_in.texcoord0[0] - w / 2;
float y = jit_in.texcoord0[1] - h / 2;
x *= aspectcorr;
...
}I realised that u_viewport is the dimension of the output window, not the output texture, how do I get the true output dim?
What I still don't really understand is if the shader kernel (the main() function) iterates over output pixels or texture input pixels, and how that relates to the texcoord0 coordinates and their scaling. Any high-level write-up or examples on OpenGl/GLSL would be welcome here.
shader kernels iterate over the pixels in the output texture, so that will be the dim of your jit.gl.slab. By default equal to the first input texture unless you've specified adapt 0.
TEXDIM0 gives you the input of your input texture, and VIEWPORT gives you the dims of the windows. There isn't a built-in uniform that gives you the dims of your output texture (if you've set adapt 0), so you'll need to pass those in yourself to a custom param.
Happy to try and clarify further if needed.
one further clarification:
jit_in.texcoord0 represents normalized texture coordinates ranging from 0.0 to 1.0,
This is true only if the input texture is @rectangle 0, otherwise this will represent 0 to input-texture-dims