fileReader.H 5.74 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:     fileReader.H
* CLASS:        FileReader 
* DESCRIPTION:  A class of file readers for each desired format 
* AUTHOR:       Mike Goslin
* CREATED:      2/7/95
* REVISIONS:
*
*******************************************************************************/

#ifndef _FILEREADER_HPP_
#define _FILEREADER_HPP_

#include <stdio.h>
#include "dataBase.H"
#include "lsb.h"
#include "fileWriter.H"

typedef unsigned char uchar;

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

#define NOMTLIND		4094

class FltReader : public FileReader
{
	char 		*buffer,
			*bufend;
	long		 bufsize;
	mgVertTable	*vtable;
	uint initHeaderBead(void);
	uint loadFltFile(const char* name);
	char *traverse(Bead *parent, char *tree, char *end, long depth);
	char *traverseObj(char *ptr, char *end, long depth);
	void traverseSubPoly(char **nptr, char *ptr, char *end, long depth);
 	char *traversePoly(mgPolygon *poly);	
	void LookAhead(char *ptr, mgComment **c, mgMatrix **m,
			      mgReplicate **r, mgLongID **hLongID);
    public:
	FltReader(void);
	DataBase *Read(char *_fname);
};

#define NLOD    3
#define BUFFER_SIZE 256
#define PROGNAME "lsb2pp"
#define CURRENT_FILE_POSITION 	1
#define FILE_BEGINNING		0
#define DEFAULT_COLOR_DELTA	5

typedef float Vec4[4];
typedef float Vec3[3];
typedef float Vec2[2];

class LsReader : public FileReader
{
	int         	gStructCount;
	int         	gPgFlags;
	int         	fUsage;
	int         	fPrependFileName2StructName;
	int 		useLongData;
	char		outfname[ID_LENGTH];
 	FILE		*outfile;
 	int		doRadTxtr; 
	TxtrWriter	*txtrWriter;
	int 		levels,
			deepestlevels; // Used for calculating texture values
	uint		leafNumber;
	long		textureFilePos;
	ulong		curTxtrId;
	ulong 		curPolyId;
	uint		txtrIndex;
	ulong		txtrXoff,
			txtrYoff;
	uint		doAvgFaceColor;
	uint		doColorPerVertPoly;
	int 		minDepthForTexture,
			colorDelta;

	float area(Vec2 p0, Vec2 p1, Vec2 p2);
	uint pointInPoly(Vec2 p0, Vec2 p1, Vec2 p2, Vec2 point);
	int readSignature(FILE *fp);
	void V_normalize(float v[3]);
	int readHeader(FILE *fp, LSBheader *header);
	int readMaterials(FILE *fp, ulong nmaterials, LSmaterial **materials);
	int readLayers(FILE *fp, ulong nlayers, LSlayer **layers);
    	int readTextures(FILE *fp, ulong ntexturesinput, ulong ntextures, 
							LStexture **textures);
	int readPatchLeaf(FILE *fp, ulong *verts, ulong *midVerts);
	void initPatchNodes(LSpatchNode *node, ulong level, Vec3 *coords,
			    Vec2 *texCoords, ulong *lodPrimNums,
			    ulong *lodLevels, int nlods);
	int readPatchCluster(FILE *fp, ushort *flags, Vec4 plane,
			    ulong *materialId, ulong *layerId, 
			    ulong *textureId, ulong *numTopNodes, 
			    ulong *numNodes, ulong *numTopVerts,
			    ulong *numVertices);
	int loadPatchVertex(FILE *fp, Vec4 *color);
	void triangleBuildTriIndex(LSpatchNode *node, ushort **curIndex);
	void quadBuildTriIndex(LSpatchNode *node, ushort **curIndex);
	void buildIndexForTris(LSpatchNode *node, ulong curLevel,	
			       ulong maxLevel, ushort **curIndex);
	void findColor(LSpatchNode *node, ulong curLevel, ulong maxLevel,
		      Vec3 sample, txtrData& value, Vec3 *coords, Vec4 *colors);
	int loadTopPatchVertex(FILE *fp, Vec4 *color, Vec3 *coord, 
			       Vec2 *texcoord);
 	LSpatchNode *loadPatchNodes(FILE *fp, LSpatchNode *root,
				    LSpatchNode **nodes,
				    LSpatchVertex *vertices);	
	int loadPatchClusters(FILE *fp, LSBheader *lsbHeader, 
			      LSmaterial *materials, LSlayer *layers,
			      LStexture *textures, LSpatchCluster *clusters,
			      ulong *nTris);
    public:
	LsReader(void);
        void enableRadTexture(void) { doRadTxtr = 1; }
	void enableAvgFaceColor(void) { doAvgFaceColor = 1; }
	void enableColorPerVertPoly(void) { doColorPerVertPoly = 1; }
	void MinDepthForTexture(int value) { minDepthForTexture = value; } 
	void ColorDelta(int value) { colorDelta = value; }	
	DataBase *Read(char *_fname);
};

double Distance(Vec3 p0, Vec3 p1);

#endif