mathopenglglsltexture-mapping

correct glsl affine texture mapping


i'm trying to code correct 2D affine texture mapping in GLSL.

Explanation:

...NONE of this images is correct for my purposes. Right (labeled Correct) has perspective correction which i do not want. So this: Getting to know the Q texture coordinate solution (without further improvements) is not what I'm looking for.

I'd like to simply "stretch" texture inside quadrilateral, something like this:

enter image description here

but composed from two triangles. Any advice (GLSL) please?


Solution

  • thanks for answers, but after experimenting i found a solution.

    two triangles on the left has uv (strq) according this and two triangles on the right are modifed version of this perspective correction.

    Numbers and shader:

    tri1 = [Vec2(-0.5, -1), Vec2(0.5, -1), Vec2(1, 1)]
    tri2 = [Vec2(-0.5, -1), Vec2(1, 1), Vec2(-1, 1)]
    
    d1 = length of top edge = 2
    d2 = length of bottom edge = 1
    
    tri1_uv = [Vec4(0, 0, 0, d2 / d1), Vec4(d2 / d1, 0, 0, d2 / d1), Vec4(1, 1, 0, 1)]
    tri2_uv = [Vec4(0, 0, 0, d2 / d1), Vec4(1, 1, 0, 1), Vec4(0, 1, 0, 1)]
    

    only right triangles are rendered using this glsl shader (on left is fixed pipeline):

    void main()
    {
        gl_FragColor = texture2D(colormap, vec2(gl_TexCoord[0].x / glTexCoord[0].w, gl_TexCoord[0].y);
    }
    

    so.. only U is perspective and V is linear.