/* example use of pmap_param to compute parameterization of a polygon */
#include <stdio.h>
#include <stdlib.h>
#include "poly.h"

int main(int argc, char **argv)
{
    static poly p = {
        4,
      /* u  v  sx  sy  x  y  z */
        {
          {0, 0,  0,  0, 4, 2, 6},        /* vert[0] */
          {1, 0, 40,  0, 5, 3, 8},        /* vert[1] */
          {1, 1, 30, 30, 5, 7,12},        /* vert[2] */
          {0, 1, 10, 20, 4, 6,10}         /* vert[3] */
        }
        /*
         * note: sx and sy are ignored,
         * This test polygon is a square in texture space (u,v)
         * and a planar parallelogram in object space (x,y,z)
         */
    };
    double TO[3][4];
    double obj[4], u, v;
    int i;

    /* compute texture to object transform TO */
    pmap_param(&p, TO);

    for (i=0; i<p.n; i++) {
        u = p.vert[i].u;
        v = p.vert[i].v;
        obj[0] = u*TO[0][0] + v*TO[1][0] + TO[2][0];
        obj[1] = u*TO[0][1] + v*TO[1][1] + TO[2][1];
        obj[2] = u*TO[0][2] + v*TO[1][2] + TO[2][2];
        obj[3] = u*TO[0][3] + v*TO[1][3] + TO[2][3];
        obj[0] /= obj[3];
        obj[1] /= obj[3];
        obj[2] /= obj[3];
        printf("tex(%g,%g) transforms to obj(%g,%g,%g)\n",
            u, v, obj[0], obj[1], obj[2]);
    }
    exit(0);
    return 0;
}
