Rapezoidal shape to rectangular area texture map

I have a question. Thanks

In the picture on the left, I cut a trapezoidal shape (red area)

I want to compress it to the rectangular area (blue area) on the right. Is there any good texture mapping method to keep the texture unchanged

I think ofxHomography is what you need

I have this shader from long time ago it takes a quad from a texture and fill a shape in draw method (usually a rectangle)

draw() {
drawrect(x,y,w,h)
}

the bilinear part I understand the math, the perspective method I have copied from somewhere in the web and donâ€™t know exactly how this works. rsrsrsrs

//-----------------------------------------------------------------
#version 120

uniform float time;
uniform vec2 mouse;
uniform vec2 resolution;

uniform float params[16];

// params[0] â†’ faderPos (0.0 â†’ 1.0)
// params[1] â†’ faderMode (0 = fadeIn tex0 â€“ 1 = mixer tex0 and tex1)
// params[2] â†’ someParam (some param)
// params[15] â†’ texelMode (0 = bilinear â€“ 1 = perspective)

uniform sampler2DRect tex0;
uniform vec2 size0;

uniform sampler2DRect tex1;
uniform vec2 size1;

vec2 getPerspectiveVertex(vec2 p, float q[8]) {

``````float T = (q[4] - q[2]) * (q[5] - q[7]) - (q[4] - q[6]) * (q[5] - q[3]);

float G = ((q[4] - q[0]) * (q[5] - q[7]) - (q[4] - q[6]) * (q[5] - q[1])) / T;
float H = ((q[4] - q[2]) * (q[5] - q[1]) - (q[4] - q[0]) * (q[5] - q[3])) / T;

float A = G * (q[2] - q[0]);
float D = G * (q[3] - q[1]);
float B = H * (q[6] - q[0]);
float E = H * (q[7] - q[1]);

G -= 1.0;
H -= 1.0;

T = G * p.x + H * p.y + 1.0;
return vec2((A * p.x + B * p.y) / T + q[0], (D * p.x + E * p.y) / T + q[1]);
``````

}

vec2 getBilinearVertex(vec2 p, float q[8]) {

``````vec2 vr, vs;

vr.x = (1.0 - p.x) * q[0] + p.x * q[2];
vr.y = (1.0 - p.x) * q[1] + p.x * q[3];
vs.x = (1.0 - p.x) * q[6] + p.x * q[4];
vs.y = (1.0 - p.x) * q[7] + p.x * q[5];

return vec2((1.0 - p.y) * vr.x + p.y * vs.x, (1.0 - p.y) * vr.y + p.y * vs.y);
``````

}

void main(void) {

``````float faderPos  = params[0];
float texelMode = params[15];

vec2 pos = gl_FragCoord.xy / resolution.xy;

vec4 srcA;
vec4 srcB = vec4(0.0,0.0,0.0,0.0);

if(texelMode == 1.0) {
srcA = texture2DRect( tex0, getPerspectiveVertex(pos, quad0));
} else {
srcA = texture2DRect( tex0, getBilinearVertex(pos, quad0));
}

if(texelMode == 1.0) {
srcB = texture2DRect( tex1, getPerspectiveVertex(pos, quad1));
} else {
srcB = texture2DRect( tex1, getBilinearVertex(pos, quad1));
}
}

``````

}