root/OpenSceneGraph/trunk/src/osgPlugins/txp/trpage_write.h @ 13041

Revision 13041, 12.8 kB (checked in by robert, 2 years ago)

Ran script to remove trailing spaces and tabs

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/* ************************
2   Copyright Terrain Experts Inc.
3   Terrain Experts Inc (TERREX) reserves all rights to this source code
4   unless otherwise specified in writing by the President of TERREX.
5   This copyright may be updated in the future, in which case that version
6   supercedes this one.
7   -------------------
8   Terrex Experts Inc.
9   4400 East Broadway #314
10   Tucson, AZ  85711
11   info@terrex.com
12   Tel: (520) 323-7990
13   ************************
14   */
15
16#ifndef _txpage_write_h_
17// {secret}
18#define _txpage_write_h_
19
20/* trpage_write.h
21    Classes that are used to write paging archives.
22    */
23
24#include <trpage_sys.h>
25#include <trpage_io.h>
26#include <trpage_swap.h>
27#include <trpage_read.h>
28
29/* Geometry Stats
30    Used with a Geometry Helper to keep track of what go built.
31    {group:Archive Writing}
32    */
33TX_EXDECL class TX_CLDECL trpgwGeomStats {
34public:
35    trpgwGeomStats(void);
36    ~trpgwGeomStats(void);
37
38    int totalTri;  // Total # of triangles
39
40    int totalQuad;  // Total # of quads
41
42    // Add up to totalTri
43    int totalStripTri;  // triangles in strips
44    int totalFanTri;    // triangles in fans
45    int totalBagTri;    // loose triangles
46
47    int numStrip;       // Number of distinct strips
48    int numFan;            // Number of distinct fans
49
50    int stripStat[15];  // Strip length stats
51    int fanStat[15];    // Fan length stats
52
53    int stripGeom;        // Number of seperate trpgGeometry nodes for strips
54    int fanGeom;        // Same for fans
55    int bagGeom;        // Same for bags
56
57    int stateChanges;    // Number of distinct material switches
58
59    // Helper functions
60    inline void AddStripStat(int val) { stripStat[MIN(14,val)]++; totalStripTri += val; totalTri += val; numStrip++;}
61    inline void AddFanStat(int val) { fanStat[MIN(14,val)]++; totalFanTri += val; totalTri += val; numFan++;}
62    inline void AddBagStat(int val) { totalBagTri += val; totalTri += val;}
63    inline void AddQuadStat(int val) { totalQuad += val; }
64};
65
66/* Geometry Helper
67    Collects up geometry and tries to form triangle strips, fans,
68     and groups of triangles.
69    Right now this looks for a very careful ordering.  If that ordering
70     isn't there you won't get useful tristrips or fans.  You can, however
71     use this class as a starting point and build something more akin
72     to the geometry builder in Performer.
73    {group:Archive Writing}
74 */
75TX_EXDECL class TX_CLDECL trpgwGeomHelper {
76public:
77    trpgwGeomHelper(void);
78    virtual ~trpgwGeomHelper(void);
79    enum {UseDouble,UseFloat};
80    trpgwGeomHelper(trpgWriteBuffer *,int dataType=UseDouble);
81    void init(trpgWriteBuffer *,int dataType=UseDouble);
82    virtual void SetMode(int);  // Takes a trpgGeometry primitive type (triangle by default)
83    virtual void Reset(void);
84    // Start/End polygon definition
85    virtual void StartPolygon(void);
86    virtual void EndPolygon(void);
87    virtual void ResetPolygon(void);  // If you change your mind about the current poly
88    // Set the current state
89    // Note: Currently you *must* set all of these
90    virtual void SetColor(trpgColor &);
91    virtual void SetTexCoord(trpg2dPoint &);
92    virtual void AddTexCoord(trpg2dPoint &); // for multiple textures
93    virtual void SetNormal(trpg3dPoint &);
94    virtual void SetMaterial(int32);
95    virtual void AddMaterial(int32); // for multiple textures
96    // Pull the state info together and add a vertex
97    virtual void AddVertex(trpg3dPoint &);
98
99    // Dump whatever we're doing and move on
100    virtual void FlushGeom(void);
101
102    // Get the Min and Max Z values
103    virtual void GetZMinMax(double &min,double &max);
104
105    // Get statistics for whatever we built
106    trpgwGeomStats *GetStats(void) { return &stats; }
107protected:
108    int mode;
109    int dataType;
110    trpgWriteBuffer *buf;
111
112    /* Builds strips and fans from the triangle array.
113        We (TERREX) are assuming a certain ordering in our vertex array
114         because we do this optimization elsewhere.  This won't work well
115         for anyone else.  What you will need to do if you want good
116         performance is to implement a more generic form of this method.
117         All you should have to do is override Optimize().  You've
118         got the triangle arrays and a guarantee that the triangles
119         have the same material.  All you really need is a decent fan/strip
120         algorithm.
121         */
122    virtual void Optimize(void);
123
124    // Reset Triangle arrays
125    virtual void ResetTri(void);
126
127    // Collections of geometry
128    trpgGeometry strips,fans,bags;
129
130    // Temporary data arrays for triangles/quads
131    std::vector<int32> matTri;
132    std::vector<trpg2dPoint> tex;
133    std::vector<trpg3dPoint> norm,vert;
134    // Data arrays for a polygon
135    std::vector<int32> matPoly;
136    std::vector<trpg2dPoint> polyTex;
137    std::vector<trpg3dPoint> polyNorm,polyVert;
138    // Single points
139    std::vector<trpg2dPoint> tmpTex;
140    trpg3dPoint tmpNorm;
141    trpgColor tmpCol;
142
143    // Geometry status built up as we go
144    trpgwGeomStats stats;
145
146    // Keeps track of min and max z values
147    double zmin,zmax;
148};
149
150/* Image Write Helper.
151    Used to manage textures being added to a TerraPage archive.
152    It can write Local and Tile Local textures and also manages
153    the names of External textures (but you have to write those yourself).
154 */
155TX_EXDECL class TX_CLDECL trpgwImageHelper {
156public:
157    trpgwImageHelper() {;};
158    trpgwImageHelper(trpgEndian ness,char *dir,trpgTexTable &,bool separateGeoTypical);
159    // construction is really here
160    virtual void Init(trpgEndian ness,char *dir,trpgTexTable &,bool separateGeoTypical);
161
162    virtual ~trpgwImageHelper(void);
163
164    // Adds an entry to the texture table for an external texture
165    virtual bool AddExternal(char *name,int &texID,bool lookForExisting=true);
166
167    /* Adds an entry to the texture table for a local texture and
168        writes the data for that texture out to one of our texture
169        archive files.
170     */
171    virtual bool AddLocal(char *name,trpgTexture::ImageType type,int sizeX,int sizeY,bool isMipmap,char *data,int &texID,bool deferWrite);
172
173    /* Replaces texture table information for a local texture and
174        writes the data for that texture out to one of our texture
175        archive files.
176        Up to you to ensure data is appropriate for the texture.
177     */
178    virtual bool ReplaceLocal(char *data,int &texID);
179
180    /* Write a Tile Local texture out to one of our texture archive files.
181        Also creates a texture template, if necessary.
182        Caller is responsible for creating the Tile Local material and
183        placing it in the appropriate tile.
184     */
185    virtual bool AddTileLocal(char *name,trpgTexture::ImageType type,int sizeX,int sizeY,bool isMipmap,char *data, int &texID,trpgwAppAddress &addr);
186
187    /* Sets the maximum advised length for a texture archive file.
188        Once the length is exceeded, the image write helper will move
189        on to the next tex file.
190     */
191    virtual void SetMaxTexFileLength(int len);
192
193    /* Texture archive files are managed by this class and will
194        be created as needed.  This method will increment to
195        the next texture file.
196        Note: This may create more files than we really need.
197     */
198    virtual trpgwAppFile * IncrementTextureFile(bool geotyp);
199
200    /* Close the current texture file and go on to one with the
201        given base name.  This is used for regenerate.
202     */
203    virtual bool DesignateTextureFile(int);
204
205    // Flush current texture output files
206    virtual bool Flush(void);
207
208    // Get a new appendable file
209    virtual trpgwAppFile* GetNewWAppFile(trpgEndian inNess,const char *fileName,bool reuse=false);
210
211    // Write the given texture data into one our local archives
212    bool WriteToArchive(const trpgTexture &tex,char *data,trpgwAppAddress &addr,bool geotyp=false);
213    // Merge block textable into a master.
214
215protected:
216
217
218    trpgEndian ness;
219    char dir[1024];
220    trpgTexTable *texTable;
221    std::vector<int> texFileIDs;
222    trpgwAppFile *texFile;
223    std::vector<int> geotypFileIDs;
224    trpgwAppFile *geotypFile;
225    bool separateGeoTypical;
226    int maxTexFileLen;
227};
228
229/* Paging Archive
230    This is a writeable paging archive.
231    It organizes where things get written and how.
232    {group:Archive Writing}
233    */
234TX_EXDECL class TX_CLDECL trpgwArchive : public trpgCheckable {
235public:
236    // Tiles can be stored as individual files (External) or grouped together (Local)
237    enum TileMode {TileLocal,TileExternal,TileExternalSaved};
238
239    // real constructor work done in Init(...) now for quasi-virtual ctor action.
240    // Add data to an existing archive
241    trpgwArchive(char *baseDir,char *name,trpg2dPoint &ll,trpg2dPoint &ur, int majorVer=TRPG_VERSION_MAJOR, int minorVer=TRPG_VERSION_MINOR);
242    virtual void Init(char *baseDir,char *name,trpg2dPoint &ll,trpg2dPoint &ur, int majorVer=TRPG_VERSION_MAJOR, int minorVer=TRPG_VERSION_MINOR);
243    // Start an archive from scratch.
244    trpgwArchive(trpgEndian ness=LittleEndian,TileMode tileMode=TileLocal,int majorVer=TRPG_VERSION_MAJOR, int minorVer=TRPG_VERSION_MINOR);
245    virtual void Init(trpgEndian ness=LittleEndian,TileMode tileMode=TileLocal,int majorVer=TRPG_VERSION_MAJOR, int minorVer=TRPG_VERSION_MINOR);
246    // dummy constructor, does nothing so subclasses can have more control
247    trpgwArchive(int ) {;};
248    virtual ~trpgwArchive(void);
249
250    // Set the maximum length for a tile file (if using them)
251    // This is only a suggestion for when to stop appending
252    virtual void SetMaxTileFileLength(int len);
253
254    // Set functions.  Have to fill all these out before writing
255    virtual bool SetHeader(const trpgHeader &);
256    virtual bool SetMaterialTable(const trpgMatTable &);
257    virtual bool SetTextureTable(const trpgTexTable &);
258    virtual bool SetModelTable(const trpgModelTable &);
259    virtual bool SetLightTable(const trpgLightTable &);
260    virtual bool SetRangeTable(const trpgRangeTable &);
261    virtual bool SetLabelPropertyTable(const trpgLabelPropertyTable &);
262    virtual bool SetSupportStyleTable(const trpgSupportStyleTable &);
263    virtual bool SetTextStyleTable(const trpgTextStyleTable &);
264
265    // Get functions.  If we're doing a regenerate we need to get at these
266    virtual trpgHeader *GetHeader();
267    virtual trpgMatTable *GetMatTable();
268    virtual trpgTexTable *GetTextureTable();
269    virtual trpgModelTable *GetModelTable();
270    virtual trpgLightTable *GetLightTable();
271    virtual trpgRangeTable *GetRangeTable();
272    virtual trpgLabelPropertyTable *GetLabelPropertyTable();
273    virtual trpgTextStyleTable *GetTextStyleTable();
274    virtual trpgSupportStyleTable *GetSupportStyleTable();
275
276
277
278    virtual bool IncrementTileFile(void);
279    virtual bool DesignateTileFile(int);
280
281    // Write functions.
282    // For now, the header is written last.
283
284    virtual bool OpenFile(const char *,const char *);
285    virtual void CloseFile(void);
286    virtual bool WriteHeader(void);
287    virtual bool CheckpointHeader(void);
288    virtual bool WriteTile(unsigned int,unsigned int,unsigned int,float zmin,float zmax,
289        const trpgMemWriteBuffer *,const trpgMemWriteBuffer *, int32& fileId, int32& fileOffset);
290//    virtual bool WriteModel(unsigned int,trpgMemWriteBuffer &);
291
292    bool isValid(void) const;
293    const char *getErrMess() const;
294    char* getDir(void){return dir;};
295    virtual trpgwImageHelper* GetNewWImageHelper(trpgEndian ness,char *dir,trpgTexTable &);
296    virtual trpgwAppFile* GetNewWAppFile(trpgEndian inNess,const char *fileName,bool reuse=false);
297    virtual trpgr_Archive* GetArchiveReader() {return new trpgr_Archive();};
298    virtual int32 WriteHeaderData(const char *dataPtr,int32 length,FILE *filehandle);
299    virtual int32 GetMagicNumber() {return TRPG_MAGIC;};
300protected:
301    // Set if we're adding to an existing archive
302    bool isRegenerate;
303
304    // Used to keep track of which tiles are in which file
305    class TileFileEntry {
306    public:
307        int x,y,lod;    // Identifying info for tile
308        float zmin,zmax;
309        int32 offset;  // Offset into file
310    };
311    class TileFile {
312    public:
313        int id;
314        std::vector<TileFileEntry> tiles;
315    };
316
317    trpgEndian ness,cpuNess;
318    int majorVersion, minorVersion;
319    // Fed in from the outside
320    char dir[1024];       // Directory where we're doing all this
321
322    // These are passed in
323
324    trpgHeader header;
325    trpgMatTable matTable;
326    trpgTexTable texTable;
327    trpgModelTable modelTable;
328    trpgLightTable lightTable;
329    trpgRangeTable rangeTable;
330    trpgTextStyleTable textStyleTable;
331    trpgSupportStyleTable supportStyleTable;
332    trpgLabelPropertyTable labelPropertyTable;
333
334
335    trpgTileTable tileTable;
336
337    int numLod;
338    TileMode tileMode;
339
340    trpgwAppFile *tileFile;
341    int tileFileCount;
342
343    std::vector<TileFile> tileFiles;
344
345    std::vector<TileFileEntry> externalTiles;
346
347    int maxTileFileLen;
348
349    // This offset is used when we're adding to an existing archive
350    trpg2iPoint addOffset;
351
352    FILE *fp;
353
354    bool firstHeaderWrite;
355
356    mutable char errMess[512];
357};
358
359#endif
Note: See TracBrowser for help on using the browser.