correct glsl affine texture mapping

后端 未结 6 1346
一整个雨季
一整个雨季 2020-12-19 10:43

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

Explanation:

\"\"

...NONE of thi

6条回答
  •  无人及你
    2020-12-19 11:20

    In case anyone's still interested, here's a C# implementation that takes a quad defined by the clockwise screen verts (x0,y0) (x1,y1) ... (x3,y3), an arbitrary pixel at (x,y) and calculates the u and v of that pixel. It was originally written to CPU-render an arbitrary quad to a texture, but it's easy enough to split the algorithm across CPU, Vertex and Pixel shaders; I've commented accordingly in the code.

                float Ax, Bx, Cx, Dx, Ay, By, Cy, Dy, A, B, C;
    
                //These are all uniforms for a given quad. Calculate on CPU.
                Ax = (x3 - x0) - (x2 - x1);
                Bx = (x0 - x1);
                Cx = (x2 - x1);
                Dx = x1;
    
                Ay = (y3 - y0) - (y2 - y1);
                By = (y0 - y1);
                Cy = (y2 - y1);
                Dy = y1;
    
                float ByCx_plus_AyDx_minus_BxCy_minus_AxDy = (By * Cx) + (Ay * Dx) - (Bx * Cy) - (Ax * Dy);
                float ByDx_minus_BxDy = (By * Dx) - (Bx * Dy);
    
                A = (Ay*Cx)-(Ax*Cy);
    
                //These must be calculated per-vertex, and passed through as interpolated values to the pixel-shader 
                B = (Ax * y) + ByCx_plus_AyDx_minus_BxCy_minus_AxDy - (Ay * x);
                C = (Bx * y) + ByDx_minus_BxDy - (By * x);
    
                //These must be calculated per-pixel using the interpolated B, C and x from the vertex shader along with some of the other uniforms.
                u = ((-B) - Mathf.Sqrt((B*B-(4.0f*A*C))))/(A*2.0f);
                v = (x - (u * Cx) - Dx)/((u*Ax)+Bx);
    

提交回复
热议问题