| 1 |
greg |
2.10 |
/* RCSid $Id: mesh.h,v 2.9 2003/07/14 22:23:59 schorsch Exp $ */ |
| 2 |
greg |
2.1 |
/* |
| 3 |
|
|
* Header for compact triangle mesh geometry |
| 4 |
|
|
* |
| 5 |
|
|
* Include after standard.h, object.h and octree.h |
| 6 |
|
|
*/ |
| 7 |
schorsch |
2.5 |
#ifndef _RAD_MESH_H_ |
| 8 |
|
|
#define _RAD_MESH_H_ |
| 9 |
schorsch |
2.9 |
|
| 10 |
|
|
#include "lookup.h" |
| 11 |
|
|
|
| 12 |
schorsch |
2.5 |
#ifdef __cplusplus |
| 13 |
|
|
extern "C" { |
| 14 |
|
|
#endif |
| 15 |
greg |
2.3 |
|
| 16 |
greg |
2.1 |
#ifndef BYTE |
| 17 |
|
|
#define BYTE unsigned char |
| 18 |
|
|
#endif |
| 19 |
|
|
|
| 20 |
|
|
/* |
| 21 |
|
|
* Vertex space is minimized without compromising accuracy by using a |
| 22 |
|
|
* 4-byte unsigned int to indicate position in the enclosing octree cube. |
| 23 |
|
|
* The same trick is used for any local (u,v) coordinates, whose limits |
| 24 |
|
|
* are recorded separately in the parent MESH structure. The uvlimit's |
| 25 |
|
|
* in the MESH structure are set such that (0,0) is out of range, so |
| 26 |
|
|
* we use this to indicate an unspecified local coordinate. |
| 27 |
|
|
* A vertex normal, if specified, is stored in a single 4-byte |
| 28 |
|
|
* integer using the codec in dircode.c. The encodedir() function |
| 29 |
|
|
* never generates 0, so we can use this for unspecified normals. |
| 30 |
|
|
* |
| 31 |
|
|
* Vertex ID's are encoded using the bottom 8 bits of a 4-byte integer |
| 32 |
greg |
2.2 |
* to index a vertex in a patch indicated by the 22 bits above (8-29). |
| 33 |
|
|
* For triangle ID's, the top 22 bits (10-31) indicate the patch, and |
| 34 |
|
|
* the bit 9 (0x200) indicates whether the triangle joins patches. |
| 35 |
greg |
2.1 |
* If not, then the bottom 9 bits index into the local PTri array. |
| 36 |
greg |
2.2 |
* If it's a joiner, then the 8th bit indicates whether the triangle joins |
| 37 |
greg |
2.1 |
* two patches, in which case the bottom 8 bits index the PJoin2 array. |
| 38 |
|
|
* Otherwise, the bottom 8 bits index the PJoin1 array. |
| 39 |
|
|
* |
| 40 |
|
|
* These shenanigans minimize vertex reference memory requirements |
| 41 |
|
|
* in compiled mesh structures, where the octree leaves contain sets |
| 42 |
|
|
* of triangle ID's rather than the more usual objects. It seems like |
| 43 |
|
|
* a lot of effort, but it can reduce mesh storage by a factor of 3 |
| 44 |
|
|
* or more. This is important, as the whole point is to model very |
| 45 |
|
|
* complicated geometry with this structure, and memory is the main |
| 46 |
greg |
2.3 |
* limitation. |
| 47 |
greg |
2.1 |
*/ |
| 48 |
|
|
|
| 49 |
|
|
/* A triangle mesh patch */ |
| 50 |
|
|
typedef struct { |
| 51 |
greg |
2.6 |
uint32 (*xyz)[3]; /* up to 256 patch vertices */ |
| 52 |
|
|
int32 *norm; /* vertex normals */ |
| 53 |
|
|
uint32 (*uv)[2]; /* vertex local coordinates */ |
| 54 |
greg |
2.1 |
struct PTri { |
| 55 |
|
|
BYTE v1, v2, v3; /* local vertices */ |
| 56 |
|
|
} *tri; /* local triangles */ |
| 57 |
greg |
2.3 |
short solemat; /* sole material */ |
| 58 |
greg |
2.6 |
int16 *trimat; /* or local material indices */ |
| 59 |
greg |
2.1 |
struct PJoin1 { |
| 60 |
greg |
2.6 |
int32 v1j; /* non-local vertex */ |
| 61 |
|
|
int16 mat; /* material index */ |
| 62 |
greg |
2.1 |
BYTE v2, v3; /* local vertices */ |
| 63 |
|
|
} *j1tri; /* joiner triangles */ |
| 64 |
|
|
struct PJoin2 { |
| 65 |
greg |
2.6 |
int32 v1j, v2j; /* non-local vertices */ |
| 66 |
|
|
int16 mat; /* material index */ |
| 67 |
greg |
2.1 |
BYTE v3; /* local vertex */ |
| 68 |
|
|
} *j2tri; /* double joiner triangles */ |
| 69 |
|
|
short nverts; /* vertex count */ |
| 70 |
greg |
2.10 |
short ntris; /* local triangle count */ |
| 71 |
greg |
2.1 |
short nj1tris; /* joiner triangle count */ |
| 72 |
|
|
short nj2tris; /* double joiner triangle count */ |
| 73 |
|
|
} MESHPATCH; |
| 74 |
|
|
|
| 75 |
|
|
/* A loaded mesh */ |
| 76 |
|
|
typedef struct mesh { |
| 77 |
|
|
char *name; /* mesh file name */ |
| 78 |
|
|
int nref; /* reference count */ |
| 79 |
|
|
int ldflags; /* what we've loaded */ |
| 80 |
|
|
CUBE mcube; /* bounds and octree */ |
| 81 |
schorsch |
2.7 |
RREAL uvlim[2][2]; /* local coordinate extrema */ |
| 82 |
greg |
2.3 |
OBJECT mat0; /* base material index */ |
| 83 |
|
|
OBJECT nmats; /* number of materials */ |
| 84 |
greg |
2.1 |
MESHPATCH *patch; /* mesh patch list */ |
| 85 |
|
|
int npatches; /* number of mesh patches */ |
| 86 |
greg |
2.3 |
OBJREC *pseudo; /* mesh pseudo objects */ |
| 87 |
|
|
LUTAB lut; /* vertex lookup table */ |
| 88 |
greg |
2.1 |
struct mesh *next; /* next mesh in list */ |
| 89 |
|
|
} MESH; |
| 90 |
|
|
|
| 91 |
|
|
/* A mesh instance */ |
| 92 |
|
|
typedef struct { |
| 93 |
|
|
FULLXF x; /* forward and backward transforms */ |
| 94 |
|
|
MESH *msh; /* mesh object reference */ |
| 95 |
|
|
} MESHINST; |
| 96 |
|
|
|
| 97 |
|
|
/* vertex flags */ |
| 98 |
|
|
#define MT_V 01 |
| 99 |
|
|
#define MT_N 02 |
| 100 |
|
|
#define MT_UV 04 |
| 101 |
|
|
#define MT_ALL 07 |
| 102 |
|
|
|
| 103 |
|
|
/* A mesh vertex */ |
| 104 |
|
|
typedef struct { |
| 105 |
|
|
int fl; /* setting flags */ |
| 106 |
|
|
FVECT v; /* vertex location */ |
| 107 |
|
|
FVECT n; /* vertex normal */ |
| 108 |
schorsch |
2.7 |
RREAL uv[2]; /* local coordinates */ |
| 109 |
greg |
2.1 |
} MESHVERT; |
| 110 |
|
|
|
| 111 |
|
|
/* mesh format identifier */ |
| 112 |
|
|
#define MESHFMT "Radiance_tmesh" |
| 113 |
|
|
/* magic number for mesh files */ |
| 114 |
greg |
2.3 |
#define MESHMAGIC ( 1 *MAXOBJSIZ+311) /* increment first value */ |
| 115 |
greg |
2.1 |
|
| 116 |
|
|
|
| 117 |
|
|
extern MESH *getmesh(char *mname, int flags); |
| 118 |
|
|
extern MESHINST *getmeshinst(OBJREC *o, int flags); |
| 119 |
greg |
2.6 |
extern int getmeshtrivid(int32 tvid[3], OBJECT *mo, |
| 120 |
greg |
2.3 |
MESH *mp, OBJECT ti); |
| 121 |
greg |
2.6 |
extern int getmeshvert(MESHVERT *vp, MESH *mp, int32 vid, int what); |
| 122 |
greg |
2.3 |
extern int getmeshtri(MESHVERT tv[3], OBJECT *mo, |
| 123 |
|
|
MESH *mp, OBJECT ti, int what); |
| 124 |
|
|
extern OBJREC *getmeshpseudo(MESH *mp, OBJECT mo); |
| 125 |
greg |
2.6 |
extern int32 addmeshvert(MESH *mp, MESHVERT *vp); |
| 126 |
greg |
2.3 |
extern OBJECT addmeshtri(MESH *mp, MESHVERT tv[3], OBJECT mo); |
| 127 |
|
|
extern char *checkmesh(MESH *mp); |
| 128 |
greg |
2.1 |
extern void printmeshstats(MESH *ms, FILE *fp); |
| 129 |
|
|
extern void freemesh(MESH *ms); |
| 130 |
|
|
extern void freemeshinst(OBJREC *o); |
| 131 |
|
|
extern void readmesh(MESH *mp, char *path, int flags); |
| 132 |
|
|
extern void writemesh(MESH *mp, FILE *fp); |
| 133 |
|
|
|
| 134 |
schorsch |
2.5 |
|
| 135 |
|
|
#ifdef __cplusplus |
| 136 |
|
|
} |
| 137 |
|
|
#endif |
| 138 |
|
|
#endif /* _RAD_MESH_H_ */ |
| 139 |
|
|
|