fileWriter.H 7.15 KB
/******************************************************************************\
  Copyright 1995 The University of North Carolina at Chapel Hill.
  All Rights Reserved.

  Permission to use, copy, modify and distribute this software and its
  documentation for educational, research and non-profit purposes, without
  fee, and without a written agreement is hereby granted, provided that the
  above copyright notice and the following three paragraphs appear in all
  copies.

  IN NO EVENT SHALL THE UNIVERSITY OF NORTH CAROLINA
  AT CHAPEL HILL BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL, 
  INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING
  OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE
  UNIVERSITY OF NORTH CAROLINA HAS BEEN ADVISED OF THE POSSIBILITY OF
  SUCH DAMAGES.

  THE UNIVERSITY OF NORTH CAROLINA SPECIFICALLY DISCLAIM ANY WARRANTIES, 
  INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HERUNDER IS
  ON AN "AS IS" BASIS, AND THE UNIVERSITY OF NORTH CAROLINA HAS NO OBLIGATIONS
  TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.

  The author may be contacted via:

  US Mail:             Mike Goslin
                       Department of Computer Science
                       Sitterson Hall, CB #3175
                       University of N. Carolina
                       Chapel Hill, NC 27599-3175

  Phone:               (919)962-1719

  EMail:               goslin@cs.unc.edu

*******************************************************************************/
/*******************************************************************************
* FILENAME:     fileWriter.H
* CLASS:        FileWriter 
* DESCRIPTION:  A class of file writers for each desired format 
* AUTHOR:       Mike Goslin
* CREATED:      2/7/95
* REVISIONS:	3/17/95	- Added lightscape writer (MPG)
*
*******************************************************************************/

#ifndef _FILEWRITER_HPP_
#define _FILEWRITER_HPP_

#include <stdio.h>
#include "dataBase.H"
#include "image.H"

typedef unsigned int uint;
typedef unsigned char uchar;

class FileWriter
{
    protected:
	FILE		*file;
	char 		 fname[80];
	DataBase	*dbase;
	uint openFile(void) { if ((file = fopen(fname, "w")) == NULL)
				  return (0); else return (1); }
    public:
	virtual uint Write(char *_fname) = 0;
};

/*******************************************************************************
* MultiGen Flight format file writer (.flt)
*******************************************************************************/
#define FLT_HEADER_LENGTH 		4
#define FLT_HEADER_ID_LENGTH		8
#define FLT_HEADER_REVDATE_LENGTH	32
#define POLYGON_ID_LENGTH		8

class FltWriter : public FileWriter
{
	void writeHeader(const mgHeader& header);
	void writeColorTable(const mgColorTable& coltab);
	void writeMaterialTable(const mgMaterialTable& mattab);
	void writeTextureRef(const mgTextureRef& texref);
	void writeVertex(mgVertSharedAbs* vert);
	void writeVertex(mgVertSharedNorm* vert);
	void writeVertex(mgVertSharedNormTex* vert);
	void writeVertex(mgVertSharedAbsTex* vert);
	uint writeVertexPool(const VertexTableBead* vtable);
	void writeBead(Bead *bead);
	void writeControl(ushort *type);
	void traverse(Bead *bead);
    public:
	FltWriter(DataBase *_dbase);
	uint Write(char *_fname);
};

/******************************************************************************** Lightscape preparation format file writer (.lp)
*******************************************************************************/
#define ATTR_DIFF_REFL    		0x0001  // Diffuse Reflector
#define ATTR_DIFF_TRANS   		0x0002  // Diffuse Transmittor
#define ATTR_SPEC_REFL    		0x0004  // Specular Reflector
#define ATTR_SPEC_TRANS   		0x0008  // Specular Transmittor
#define ATTR_GLOWING      		0x0010  // Self-Emitted Luminance
#define ATTR_TEXTURED     		0x0040  // Has a texture

#define TEXTURE_ABSOLUTE_SIZE_BIT  	0x0001

#define TEXTURE_MIN_POINT        	0
#define TEXTURE_MIN_LINEAR       	1
#define TEXTURE_MIN_POINT_MM     	2
#define TEXTURE_MIN_LINEAR_MM    	3
#define TEXTURE_MIN_BILINEAR_MM  	4
#define TEXTURE_MIN_TRILINEAR_MM 	5

#define TEXTURE_MAG_POINT        	0
#define TEXTURE_MAG_LINEAR       	1

#define ENTITY_POLY     		0x0004  // Set is entity is a polygon
#define ENTITY_INSTANCE 		0x0001  // Set if entity is an instance
#define POLY_TEXATTR_INSTALLED		0x0800
#define POLY_TRIANGLE_BIT       	0x0004
#define POLY_X_AXIS			0x0000
#define POLY_Y_AXIS			0x0001
#define POLY_Z_AXIS			0x0002
#define POLY_TWO_SIDED_BIT 		0x1000

#define POLY_NOTARECEIVER_BIT   	0x0100
#define POLY_NOTANOBSTACLE_BIT  	0x0200
#define POLY_NOTAREFLECTOR_BIT  	0x0400
#define POLY_WINDOW_BIT         	0x2000
#define POLY_OPENING_BIT        	0x4000


#define ATTRIBUTE_TRAVERSAL		1	
#define BLOCK_TRAVERSAL			2	
#define ENTITY_TRAVERSAL		0	
#define MAX_VERTICES_PER_POLYGON 	32
#define FUZZ_VALUE			10e-7
#define INFINITY			10e23	

typedef struct attr {
	short		index;
	struct attr    *next;
} attribute;

typedef struct txinfo {
	short		index;
	int 		number;
	double		avgsizeU,
			avgsizeV;
	struct txinfo  *next;
} TexInfo;

class LsWriter : public FileWriter
{
	int 			 travType;
	char			*rootname;
	attribute      		*attribList;
	mgColorTable    	 colTable;
	TextureBead		*texList;
	VertexTableBead 	*vtxpool;
	double			 maxExt[3],
				 minExt[3];
	TexInfo			*textureInfo;
	void V_normalize(double v[]);
	void writeDefaults(void);
	void writeView(void);
	void writeNaturalLight(void);
	void writeTextureTable(void);
	void writeAttributeTable(void);
	void writeLayerTable(void);
	void writeSourceTable(void);
    	void writeBlock(char *name);
	void writeEntity(int flags, char *attribute=NULL);
	void writePolygon(double p0[3], double p1[3], double p2[3], 
		          char drawType, int textured);
	void writePolygon(double p0[3], double p1[3], double p2[3],
		          double p3[3], char drawType, int textured);
	void writeTextureOrientation(double v0[3], double v1[3]);
	void writeInstance(char *blockname);
	void writeGroupBlock(Bead *bead);
	void procBead(Bead *bead);
	void traverse(Bead *bead);
	void indent(void);
	short decodeAttribute(short color);
	char *decodeTextureAttribute(short texture, char attribute[80]);
	uint verifyPolygon(double vertices[MAX_VERTICES_PER_POLYGON][3], 
			   int vtxcount);
	void computeTexCoords(double verts[MAX_VERTICES_PER_POLYGON][3],
			   float tex[MAX_VERTICES_PER_POLYGON][2],
			   int vtxcount, double *v0, double *v1,
			   double& Uscale, double& Vscale);
	int updateAvgTextureScale(short texture, double Uscale, double Vscale);
    public:
	LsWriter(DataBase *_dbase);
	uint Write(char *_fname);
};

/******************************************************************************** SGI Texture-map File Writer (.rgb)  
*******************************************************************************/

#define MAX_XSIZE  2048
#define MAX_YSIZE  2048

class TxtrWriter : public FileWriter
{
	Image		       *image;
	int putbyte(unsigned char val);
	int putshort(unsigned short val);
	int putlong(unsigned long val);
    public:
	TxtrWriter(void);
	TxtrWriter(uint size);
	~TxtrWriter();
	uint Write(uint x, uint y, txtrData value);
	uint Write(char *_fname);
};

#endif