correct glsl affine texture mapping

后端 未结 6 1338
一整个雨季
一整个雨季 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:42

    I had similar question ( https://gamedev.stackexchange.com/questions/174857/mapping-a-texture-to-a-2d-quadrilateral/174871 ) , and at gamedev they suggested using imaginary Z coord, which I calculate using the following C code, which appears to be working in general case (not just trapezoids):

    //usual euclidean distance
    float distance(int ax, int ay, int bx, int by) {
      int x = ax-bx;
      int y = ay-by;
      return sqrtf((float)(x*x + y*y));
    }
    
    void gfx_quad(gfx_t *dst //destination texture, we are rendering into
                 ,gfx_t *src //source texture
                 ,int *quad  // quadrilateral vertices
                 )
    {
      int *v = quad; //quad vertices
      float z = 20.0;
      float top = distance(v[0],v[1],v[2],v[3]); //top
      float bot = distance(v[4],v[5],v[6],v[7]); //bottom
      float lft = distance(v[0],v[1],v[4],v[5]); //left
      float rgt = distance(v[2],v[3],v[6],v[7]); //right
    
      // By default all vertices lie on the screen plane
      float az = 1.0;
      float bz = 1.0;
      float cz = 1.0;
      float dz = 1.0;
    
      // Move Z from screen, if based on distance ratios.
      if (top

    I'm doing it in software to scale and rotate 2d sprites, and for OpenGL 3d app you will need to do it in pixel/fragment shader, unless you will be able to map these imaginary az,bz,cz,dz into your actual 3d space and use the usual pipeline. DMGregory gave exact code for OpenGL shaders: https://gamedev.stackexchange.com/questions/148082/how-can-i-fix-zig-zagging-uv-mapping-artifacts-on-a-generated-mesh-that-tapers

提交回复
热议问题