#ifndef _LIBMORPH_H_
#define _LIBMORPH_H_

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

/* gee not enums but flags */

/*
typedef
enum {
	MORPH_POLYGON_INDICES,
	MORPH_TRIANGLE_INDICES,
	MORPH_VERTICES,
	MORPH_SURFACES,

	MORPH_DOUBLE,
	MORPH_FLOAT,
	
	MORPH_BYTE,
	MORPH_SHORT,
	MORPH_LONG,
	MORPH_LONGLONG
} MorphFlags;
*/

typedef enum {
	MORPH_DBL_SIDE	= 1 << 0,
	MORPH_SMOOTH	= 1 << 1
} MorphSurfFlags;

typedef struct	_MorphModel MorphModel;
typedef struct	_MorphSurface MorphSurface;
typedef void	(* MorphUnrefFunc)( MorphModel* m );

struct _MorphSurface {
	int		ref_count;
	MorphUnrefFunc*	unref_fn;
	MorphSurface*	next;
	unsigned char*	name;
	int		triangle_count;
	unsigned char	color[ 4 ];	/* base for diffuse/specular -- kick it out */
	MorphSurfFlags	flags;
	float		diffuse;	/* kickout */
	unsigned long	_diffuse[ 4 ];	/* take off _ */
	float		specular;	/* kickout */
	unsigned long	_specular[ 4 ];	/* take off _ */
	int		specular_exp;	/* aka glossines; 0..128 */
	//float		smooth_angle;
};

struct _MorphModel {
	int		ref_count;
	MorphUnrefFunc*	unref_fn;
	float*		vertices;
	float*		vnormals;
	unsigned int	vertices_len;
	unsigned short*	polygon_indices;
	unsigned int	polygon_indices_len;
	unsigned short*	triangle_indices;
	float*		triangle_normals;
	unsigned int	triangle_indices_len;
	MorphSurface*	surfaces;
	unsigned int	surface_count;
};

MorphModel*	morph_model_load	( const char* f );
/*
void		morph_model_save	( const char* f );
*/
MorphModel*	morph_model_new		(
					MorphUnrefFunc*	unref_fn
/*,
					float*		vertices,
					unsigned short*	polygon_indices,
					unsigned short*	triangle_indices,
					MorphSurface*	surfaces
*/
					);

void		morph_model_ref		( MorphModel* m );
void		morph_model_unref	( MorphModel* m );
/*
MorphModel*	morph_model_duplicate	( const MorphModel* m );
*/
void		morph_model_destroy	( MorphModel* m );
unsigned short*	morph_make_triangles	( MorphModel* m );
/*
int		morph_make_polygons	( MorphModel* m );
*/

void		morph_byteswap32	( void* ptr, unsigned int size );
void		morph_byteswap16	( void* ptr, unsigned int size );
/*
void		morph_calc_normals	( MorphModel* m );
*/
void		morph_calc_vnormals	( MorphModel* m );

#ifndef FALSE
#define FALSE	(0)
#endif

#ifndef TRUE
#define TRUE	(!FALSE)
#endif

#undef	ABS
#define ABS(a)	   (((a) < 0) ? -(a) : (a))

#ifdef	WORDS_BIGENDIAN
#  define M_16BW_FROM_LE( ptr, siz )	morph_byteswap16( ptr, siz )
#  define M_32BW_FROM_LE( ptr, siz )	morph_byteswap32( ptr, siz )
#  define M_16BW_FROM_BE( ptr, siz )
#  define M_32BW_FROM_BE( ptr, siz )
#else
#  define M_16BW_FROM_LE( ptr, siz )
#  define M_32BW_FROM_LE( ptr, siz )
#  define M_16BW_FROM_BE( ptr, siz )	morph_byteswap16( ptr, siz )
#  define M_32BW_FROM_BE( ptr, siz )	morph_byteswap32( ptr, siz )
#endif

#endif /* _LIBMORPH_H_ */

