/*
    Crystal Space utility library: 3D formats import library
    Based on IVCON by John Burkardt, used with permission
    C++ Class and CS interface: Bruce Williams

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public
    License along with this library; if not, write to the Free
    Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

*/
#ifndef __ASE2SPR_H__
#define __ASE2SPR_H__

#include <ctype.h>
//#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

/*
  The "brain.iv" file requires MAX_FACE = 75448, MAX_COR3 = 37608.
*/

// these numbers need to be adjusted

#define DEBUG
#define MAX_COLOR 10000
#define MAX_COR3 10000
#define MAX_FACE 30000
#define MAX_INCHARS 1000
#define MAX_LEVEL 20
#define MAX_LINE 4000
#define MAX_ORDER 10
#define MAX_TEXMAP 10

#ifndef MAX
#define MAX(a,b) ( (a)>(b) ? (a) : (b) )
#endif

#ifndef MIN
#define MIN(a,b) ( (a)>(b) ? (b) : (a) )
#endif

/*
  COR3(3,MAX_COR3), the coordinates of nodes.

  COR3_NORMAL(3,MAX_COR3), normal vectors associated with nodes.

  COR3_RGB(3,MAX_COR3), RGB colors associated with nodes.

  FACE(MAX_ORDER,MAX_FACE) contains the index of the I-th node making up face J.

  FACE_MAT(MAX_ORDER,MAX_FACE); the material of the I-th node making up face J.

  FACE_NORMAL(3,MAX_FACE), the face normal vectors.

  FACE_ORDER(MAX_FACE), the number of vertices per face.

  LINE_DEX(MAX_LINE), node indices, denoting polylines, each terminated by -1.

  LINE_MAT(MAX_LINE), index into RGBCOLOR for line color.

  MAX_COR3, the maximum number of points.

  MAX_FACE, the maximum number of faces.

  MAX_LINE, the maximum number of line definition items.

  MAX_ORDER, the maximum number of vertices per face.

  MAX_TEXMAP, the maximum number of texture maps.

  NUM_COR3, the number of points.

  NUM_FACE, the number of faces.

  NUM_LINE, the number of line definition items.

  VERTEX_NORMAL(3,MAX_ORDER,MAX_FACE), normals at vertices. 

  VERTEX_RGB(3,MAX_ORDER,MAX_FACE), colors associated with vertices. 
*/

// format readers subclass this in order to provide functionality
// for changing frames


class converter  
{
public:
    converter();
    virtual ~converter();

    //void set_reverse_normals( int yesno );

    int spr_write ( FILE *fileout, const char* objectname );
    int spr_write_header( FILE *fileout, const char* objectname );
    int spr_write_frame ( FILE *fileout, int frameNumber );
    int spr_write_footer ( FILE *fileout, int frameNumber );

public:
    FILE* logfile;

    char   filein_name[81];
    char   fileout_name[81];
    
    float  cor3[3][MAX_COR3];
    float  cor3_normal[3][MAX_COR3];
    float  cor3_rgb[3][MAX_COR3];
    float  cor3_uv[2][MAX_COR3];
    float  temp_cor3[3][MAX_COR3];
    float  temp_cor3_uv[3][MAX_COR3];
    
    int    face[MAX_ORDER][MAX_FACE];
    int    face_flags[MAX_FACE];
    int    face_mat[MAX_ORDER][MAX_FACE];
    int    face_texnode[MAX_ORDER][MAX_FACE];
    float  face_normal[3][MAX_FACE];
    int    face_object[MAX_FACE];
    int    face_order[MAX_FACE];
    int    face_smooth[MAX_FACE];
    char   input[MAX_INCHARS];
    char   levnam[MAX_LEVEL][MAX_INCHARS];
    int    line_dex[MAX_LINE];
    int    line_mat[MAX_LINE];
    int    list[MAX_COR3];
    char   mat_name[81];
    int    max_order2;
    float  normal_temp[3][MAX_ORDER*MAX_FACE];
    
    char   texmap_name[MAX_TEXMAP][81];
    float  transform_mat[4][4];
    float  vertex_normal[3][MAX_ORDER][MAX_FACE];
    float  vertex_uv[2][MAX_ORDER][MAX_FACE];
    float  face_uv[2][MAX_ORDER][MAX_FACE];
    float  vertex_rgb[3][MAX_ORDER][MAX_FACE];
    
    int    num_bad;
    int    num_color;
    int    num_comment;
    int    num_cor3;
    int    num_cor3_uv;
    int    temp_num_cor3;
    int    temp_num_cor3_uv;
    int    num_dup;
    int    num_face;
    int    num_group;
    int    num_line;
    int    num_object;
    int    num_texmap;
    int    num_text;
    char   object_name[81];
    
private:
    int    i;
    int    iarg;
    int    icor3;
    int    ierror;
    int    iface;
    int    ivert;
    int    revnorm;
    
    int    byte_swap;
    float  origin[3];
    float  pivot[3];
    float  rgbcolor[3][MAX_COLOR];
    char   temp_name[81];
    // functions
    
public:
    void               data_init ( void );
    int                ase_convert ( FILE *filein, char *fileout_name );
    void               scale_model (float x, float y, float z);
};


#endif // __ASE2SPR_H__
