/**
 * 
 * Filename : asetospr.cpp
 * Author : Planeshift Team
 * 
 * Purpose: convert an ase file to an spr file
 *
 **/
#include <psconfig.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "ase2spr.h"


/**
 *
 * main()
 * Main function. Like any other applications, this is the entry point of the program.
 * The SysSystemDriver is created and initialized here. It also creates psEngine and 
 * initialize it. Then it calls the main loop. When the main loop breaks, it just exit.
 *
 **/
int scale;            // set to 1 if we need to scale the model
float scalex;
float scaley;
float scalez;

int main(int argc, char *argv[])
{
    if (argc < 3 || argc > 6) {
    printf
        ("\nase2spr is a program to convert an ASE files (Exported from 3DSMAX)\n");
    printf("to a Crystal Space spr file\n\n");

    printf("syntax: ase2spr <asefile> <sprfile> or \n");
    printf
        ("syntax: ase2spr <asefile> <sprfile> <scalex> <scaley> <scalez> \n");
    printf("for example: ase2spr enkidukai.ase enkidukai.spr\n\n");
    printf
        ("for example: ase2spr enkidukai.ase enkidukai.spr 0.1 0.1 0.1\n\n");

    return 0;
    }
    // check if we need to scale the model
    if (argc > 3) {
    scale = true;
    sscanf(argv[3], "%f", &scalex);
    sscanf(argv[4], "%f", &scaley);
    sscanf(argv[5], "%f", &scalez);
    } else
    scale = false;

    // Convert an ASE file
    converter *filedata = new converter;

    //char * filein_name = "d:\\Luca\\enkidukai_w5.ase";
    //char * fileout_name = "d:\\Luca\\test1.spr";

    FILE *filein = fopen(argv[1], "rb");

    // test if input file exists
    if (filein == NULL) {
    fprintf(stderr, "The input file doesn't exists\n");

    return 1;
    }
    if (filedata->ase_convert(filein, argv[2])) {
    fprintf(stderr, "Error while converting file!\n");
    return 1;
    }
    fclose(filein);
    delete filedata;

    return 0;
}


converter::converter()
:  revnorm(false)
{
#ifdef DEBUG
    // create log file
    logfile = fopen("ase2spr.log", "w");
#else
    logfile = NULL;
#endif

    // init number of lines
    num_text = 0;

    // clear data
    data_init();
}

converter::~converter()
{
    if (logfile)
        fclose(logfile);
}

// This functions is called at every frame!
void converter::data_init()
{
    int i;
    int iface;
    int ivert;
    int j;
    int k;

    for (i = 0; i < 3; i++) {
    for (j = 0; j < MAX_COR3; j++) {
        cor3[i][j] = 0.0;
        cor3_normal[i][j] = 0.0;
        temp_cor3[i][j] = 0.0;
    }
    }

    for (j = 0; j < MAX_COR3; j++) {
    cor3_rgb[0][j] = 0.299f;
    cor3_rgb[1][j] = 0.587f;
    cor3_rgb[2][j] = 0.114f;
    }

    for (i = 0; i < 2; i++) {
    for (j = 0; j < MAX_COR3; j++) {
        cor3_uv[i][j] = 0.0;
        temp_cor3_uv[i][j] = 0.0;
    }
    }

    for (iface = 0; iface < MAX_FACE; iface++) {
    for (ivert = 0; ivert < MAX_ORDER; ivert++) {
        face[ivert][iface] = 0;
        face_mat[ivert][iface] = 0;
        face_texnode[ivert][iface] = -1;
    }
    }

    for (iface = 0; iface < MAX_FACE; iface++) {
    face_flags[iface] = 6;
    }

    for (iface = 0; iface < MAX_FACE; iface++) {
    for (i = 0; i < 3; i++) {
        face_normal[i][iface] = 0;
    }
    }

    for (iface = 0; iface < MAX_FACE; iface++) {
    face_object[iface] = -1;
    face_order[iface] = 0;
    face_smooth[iface] = 1;
    }

    for (i = 0; i < MAX_LINE; i++) {
    line_dex[i] = -1;
    line_mat[i] = 0;
    }

    for (j = 0; j < MAX_ORDER * MAX_FACE; j++) {
    for (i = 0; i < 3; i++) {
        normal_temp[i][j] = 0;
    }
    }

    num_color = 0;
    num_cor3 = 0;
    num_cor3_uv = 0;
    temp_num_cor3 = 0;
    temp_num_cor3_uv = 0;
    num_face = 0;
    num_line = 0;
    num_texmap = 0;

    strcpy(object_name, "IVCON");

    for (i = 0; i < 3; i++) {
    origin[i] = 0.0;
    pivot[i] = 0.0;
    }

    for (j = 0; j < MAX_COLOR; j++) {
    rgbcolor[0][j] = 0.299f;
    rgbcolor[1][j] = 0.587f;
    rgbcolor[2][j] = 0.114f;
    }

    for (j = 0; j < MAX_TEXMAP; j++) {
    strcpy(texmap_name[j], "null");
    }

    for (i = 0; i < 4; i++) {
    for (j = 0; j < 4; j++) {
        if (i == j) {
        transform_mat[i][j] = 1.0;
        } else {
        transform_mat[i][j] = 0.0;
        }
    }
    }

    for (iface = 0; iface < MAX_FACE; iface++) {
    for (ivert = 0; ivert < MAX_ORDER; ivert++) {
        for (i = 0; i < 3; i++) {
        vertex_normal[i][ivert][iface] = 0.0;
        }
    }
    }

    for (iface = 0; iface < MAX_FACE; iface++) {
    for (ivert = 0; ivert < MAX_ORDER; ivert++) {
        for (i = 0; i < 2; i++) {
        vertex_uv[i][ivert][iface] = 0.0;
        }
    }
    }

    for (iface = 0; iface < MAX_FACE; iface++) {
    for (ivert = 0; ivert < MAX_ORDER; ivert++) {
        for (i = 0; i < 2; i++) {
        face_uv[i][ivert][iface] = 0.0;
        }
    }
    }

    for (j = 0; j < 3; j++) {
    for (k = 0; k < MAX_FACE; k++) {
        vertex_rgb[0][j][k] = 0.299f;
        vertex_rgb[1][j][k] = 0.587f;
        vertex_rgb[2][j][k] = 0.114f;
    }
    }

#ifdef DEBUG
    fprintf(logfile, "\n");
    fprintf(logfile, "DATA_INIT: Graphics data initialized.\n");
#endif

    return;
}

void converter::scale_model(float x, float y, float z)
{
    int j = 0;

    for (j = 0; j < num_cor3; j++) {
    cor3[0][j] = x * cor3[0][j];
    cor3[1][j] = y * cor3[1][j];
    cor3[2][j] = z * cor3[2][j];
    }

    return;
}
