/* This program demonstrates a series of morphs of a superquadric */

#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include "def.h"
#include "lib.h"

static int size_factor = 4;

static void
make_superq_file(FILE *outfile, double pow1, double pow2)
{
   COORD4  back_color, obj_color;
   COORD4  backg[4], light;
   COORD4  from, at, up;
   COORD4  center_pt;
   double  lscale, radius, r0, r1;
   int i;

   lib_set_output_file(outfile);
   lib_set_raytracer(OUTPUT_POLYRAY);
   lib_set_polygonalization(size_factor, size_factor);

   /* set radius of sphere which would enclose entire object */
   radius = 1.5;

   /* output viewpoint */
   SET_COORD(from, 1, 5.0, 1.5);
   SET_COORD(at, 0.0, 0.0, 0.0);
   SET_COORD(up, 0.0, 0.0, 1.0);
   lib_output_viewpoint(&from, &at, &up, 45.0, 1.0, 0.01, 128, 128);

   SET_COORD(back_color, 0.0, 0.0, 0.0);
   lib_output_background_color(&back_color);

   /* output light sources */
   lscale = 1.0 / sqrt(3.0);
   SET_COORD4(light, 4.0, 3.0, 2.0, lscale);
   lib_output_light(&light);
   SET_COORD4(light, 1.0, -4.0, 4.0, lscale);
   lib_output_light(&light);
   SET_COORD4(light, -3.0, 1.0, 5.0, lscale);
   lib_output_light(&light);

   /* output floor polygon - beige */
   SET_COORD(back_color, 1.0, 0.75, 0.33);
   lib_output_color(&back_color, 0.2, 0.8, 0.0, 0.0, 0.0, 0.0, 1.0);
   r0 =  radius * 20.0;
   r1 = -radius;
   SET_COORD(backg[0],  r0,  r0, r1);
   SET_COORD(backg[1], -r0,  r0, r1);
   SET_COORD(backg[2], -r0, -r0, r1);
   SET_COORD(backg[3],  r0, -r0, r1);
   lib_output_polygon(4, backg);

   /* output superquadric at location defined by center */
   SET_COORD(obj_color, 1.0, 0.5, 0.8);
   SET_COORD4(center_pt, 0.0, 0.0, 0.0, 1.0);
   lib_output_color(&obj_color, 0.2, 0.7, 0.0, 0.3, 5.0, 0.0, 1.0);
   lib_output_sq_sphere(&center_pt, radius, radius, radius, pow1, pow2);
}

int
main(int argc, char *argv[])
{
   FILE *outfile;
   int i, cnt = 30;
   double power;
   char txt[64];

   /* First loop: turn from a sphere into a octagonal pinchy */
   for (i=0;i<cnt;i++) {
      outfile = fopen("superq.pi", "w");
      power = 1.0 + 2.0 * (double)i / (double)cnt;
      make_superq_file(outfile, power, power);
      fclose(outfile);
      system("polyray superq.pi");
      sprintf(txt, "ren out.tga out%03d.tga", i);
      system(txt);
      }

   /* Second loop: turn from a octagonal pinchy into a pinched double cone */
   for (i=0;i<cnt;i++) {
      outfile = fopen("superq.pi", "w");
      power = 3.0 - 2.0 * (double)i / (double)cnt;
      make_superq_file(outfile, 3.0, power);
      fclose(outfile);
      system("polyray superq.pi");
      sprintf(txt, "ren out.tga out%03d.tga", i + 30);
      system(txt);
      }

   /* Third loop: turn from a pinched double cone into an cylinder */
   for (i=0;i<cnt;i++) {
      outfile = fopen("superq.pi", "w");
      power = 3.0 - 3.0 * (double)i / (double)cnt;
      make_superq_file(outfile, power, 1.0);
      fclose(outfile);
      system("polyray superq.pi");
      sprintf(txt, "ren out.tga out%03d.tga", i + 60);
      system(txt);
      }

   /* Fourth loop: turn from a cylinder into a box */
   for (i=0;i<cnt;i++) {
      outfile = fopen("superq.pi", "w");
      power = 1.0 - (double)i / (double)cnt;
      make_superq_file(outfile, 0.0, power);
      fclose(outfile);
      system("polyray superq.pi");
      sprintf(txt, "ren out.tga out%03d.tga", i + 90);
      system(txt);
      }

   /* Fifth loop: turn from a box into a pillow shape */
   for (i=0;i<cnt;i++) {
      outfile = fopen("superq.pi", "w");
      power = (double)i / (double)cnt;
      make_superq_file(outfile, power, 0.0);
      fclose(outfile);
      system("polyray superq.pi");
      sprintf(txt, "ren out.tga out%03d.tga", i + 120);
      system(txt);
      }

   /* Sixth loop: turn from a pillow into a sphere */
   for (i=0;i<cnt;i++) {
      outfile = fopen("superq.pi", "w");
      power = (double)i / (double)cnt;
      make_superq_file(outfile, 1.0, power);
      fclose(outfile);
      system("polyray superq.pi");
      sprintf(txt, "ren out.tga out%03d.tga", i + 150);
      system(txt);
      }

   /* Now use DTA to assemble the animation */
   system("dta out*.tga");

   /* Finally, remove all the Targa files */
   system("del out*.tga");
}
