/*****************************************************************************
* Definitions for the Poly3D-R program:                                      *
*****************************************************************************/

#ifndef POLY_3D_R_H
#define POLY_3D_R_H

#ifdef NO_VOID_PTR
#define VoidPtr		char *
#else
#define VoidPtr		void *
#endif /* NO_VOID_PTR */

#include "genmat.h"
#include "parser.h"
#include "gif_lib.h"

#ifndef __MSDOS__
#include "xgeneral.h"
#endif /* __MSDOS__ */

#ifndef LINE_LEN
#define LINE_LEN	255
#endif  LINE_LEN

#define FILE_NAME_LEN	80

#ifndef NULL
#define NULL	0
#endif

#ifndef TRUE
#define TRUE	-1
#define FALSE	0
#endif

#define EPSILON	1e-4  /* Make it smaller if you changed to double precision. */

#define DEFAULT_COLOR	63	       /* White - in case no color is given. */
#define DEFAULT_BACK_GROUND_COLOR  1	     /* Blue as default back ground. */
#define RGB_COLOR_GIVEN	-1			/* Or if fill RGB was given. */

/* General macro definitions for evry day usage... */

#define ABS(y)		((y) > 0 ? (y) : (-(y)))
#define SQR(y)		((y) * (y))
#define SGN(x)		((x) > 0 ? 1 : ((x) == 0 ? 0 : -1))
#define MIN(x, y)	((x) > (y) ? (y) : (x))
#define MAX(x, y)	((x) > (y) ? (x) : (y))
#define BOUND(x, Min, Max) (MIN(MAX((x), (Min)), (Max)))
#define APX_EQ(x, y)	(ABS(x - y) < EPSILON)
#define APX_PT_EQ(p1, p2)	(APX_EQ(p1[0], p2[0]) && \
				 APX_EQ(p1[1], p2[1]) && \
				 APX_EQ(p1[2], p2[2]))

#define GEN_COPY(Dest, Src, Size)       memcpy(Dest, Src, Size)

#define DEG2RAD(Deg)	((Deg) * M_PI / 180.0)
#define RAD2DEG(Rad)	((Rad) * 180.0 / M_PI)

#define DEFAULT_SCREEN_XSIZE	320
#define DEFAULT_SCREEN_YSIZE	200

#define DEFAULT_BITS_PER_PIXEL	7
#define DEFAULT_LIGHT_SOURCE	{ 1.0, 3.0, 7.0 }
#define DEFAULT_AMBIENT		0.3

#define DEFAULT_NORMAL_AVG_DEGREE	30

typedef struct ShadingInfoStruct {
    int SubSamplePixel;
    int BitsPerPixel;	     /* 2^BitsPerPixel == number of colors possible. */
    int LevelsPerColor;		/* Number of levels per each required color. */
    int DefaultColor;
    int BackGroundColor;
    int TwoSources;  /* If true - two sources at opposite direction assumed. */
    GifColorType *PColorMap;
    int *MinIntensityIndex;   /* Hold minimum intensity of this color index. */
    double LightSource[3];		/* Vector direction of light source. */
    double Ambient;
} ShadingInfoStruct;

/* The following are global setable variables (via config file poly3d-r.cfg).*/
extern int MoreFlag, GouraudFlag, BackFacingFlag, ScreenXSize, ScreenYSize,
    MouseExists, GraphDriver;
extern double NormalAvgDegree;
extern struct ShadingInfoStruct ShadingInfo;

/* Total number of polygons/verticesa to scan converted, in current scene:   */
extern int NumOfPolygons;
extern int NumOfVertices;

/* Amount scene was scaled up from normalized [-1..1] size on both X & Y:    */
extern float ScaleUpFactor;

/* Global transformation matrix: */
extern MatrixType GlblViewMat;			  /* Current view of object. */

/* All polygons to be scan convert will be inserted into this hash table     */
/* during the preprocessing (PrepareXXXX functions).			     */
extern struct PolygonStruct **PolyHashTable;

/* And finally the prototypes of the Poly3D-H.c module: */
char *MyMalloc(unsigned size);
void MyExit(int ExitCode);
void QuitGifError(void);

/* Prototypes of the PrepData.c module: */
void PrepareViewData(FileDescription **FD, int *NumOfObjects, char **Objects);
ObjectStruct *SearchObject(FileDescription **FD, char *Object);

/* Prototypes of the ColorTbl.c module: */
void PrepareColorTable(FileDescription **FD, int NumOfObjects,
					int RealNumOfObjects, char **Objects);

/* Prototypes of the EvalColr.c module: */
void EvalVrtxColors(FileDescription **FD, int NumOfObjects, char **Objects);
int UpdateEqnPolygon(PolygonStruct *PPolygon, int SetFlipDir);
int CrossProd(float Pt1[3], float Pt2[3], float Pt3[3]);
void PrintPolyContent(PolygonStruct *PPoly);

/* Prototypes of the ScnCnvrt.c module: */
void ScanConvertData(GifFileType *GifFile, GifFileType *GifMask);

#endif POLY_3D_R_H
