00001
00002
00003
00004
00005
00006 #ifndef MESH_H
00007 #define MESH_H
00008
00009 #include<Object.h>
00010 #include<Material.h>
00011 #include<AABoundingBox.h>
00012 #include<RenderContext.h>
00013
00014 #include<vector>
00015
00016 class Matrix4;
00017 class CollisionHull;
00018
00021 class Mesh : public Object
00022 {
00023 public:
00024
00025 typedef vector<Vertex3> VertexList;
00026 typedef vector<Vertex3> NormalList;
00027 typedef vector<uint16> FaceList;
00028 typedef vector<uint32> FaceIndexList;
00029
00032 class TriangleIterator {
00033 public:
00034 TriangleIterator(const FaceList::iterator fb, bool strip)
00035 : face(fb), isStrip(strip), invert(false)
00036 {
00037 if (isStrip && (*face == *(face+2))) {
00038 ++face;
00039 invert = !invert;
00040 }
00041 }
00042
00043 bool operator<(const FaceList::iterator end) {
00044 return (face+2 < end);
00045 }
00046 TriangleIterator &operator++() {
00047 if (isStrip) {
00048 invert = !invert;
00049 ++face;
00050 if (*face == *(face+2)) {
00051 ++face;
00052 invert = !invert;
00053 }
00054 } else {
00055 face += 3;
00056 }
00057 return *this;
00058 }
00059 uint16 a() { return (invert ? *(face+2) : *face); }
00060 uint16 b() { return *(face+1); }
00061 uint16 c() { return (invert ? *face : *(face+2)); }
00062
00063 private:
00064 bool isStrip;
00065 bool invert;
00066 FaceList::iterator face;
00067 };
00068
00069 Mesh();
00070 ~Mesh();
00071
00072 int nFaces() {
00073 return n;
00074 }
00075 int nVertices() {
00076 return vertex.size();
00077 }
00078 int nStrips();
00079 VertexList &vertexArray() {
00080 return vertex;
00081 }
00082 FaceList &faceArray() {
00083 return face;
00084 }
00085 NormalList &normalArray() {
00086 return faceNormal;
00087 }
00088 RefMaterial &getMaterial() {
00089 return material;
00090 }
00091 void setMaterial(const RefMaterial &m) {
00092 material = m;
00093 }
00094 void setMaterial(Material *m) {
00095 material = m;
00096 }
00097 bool hasTriStrips();
00098 void generateVertexNormals(bool gen);
00099
00100 void clear();
00101 void invertFace(unsigned int i);
00102 void startStrips();
00103 void endStrip();
00104 void deStripe();
00105 void calculate();
00106 void calculateFaces();
00107
00108 void transform(Matrix4 &M);
00109 void renderSimple(RenderContext &rc, bool pushTriNames);
00110
00111
00112 void render(
00113 RenderContext &rc,
00114 RenderContext::Visibility visibility = RenderContext::PartlyVisible);
00115 float collide(const CollisionHull &hull, Vertex3 &nearestIntersection);
00116 real intersect(const Ray &ray) const;
00117 void read(WDFFile &f);
00118 void write(WDFFile &f);
00119 void readChunk(WDFFile::Chunk &chunk, WDFFile &f);
00120
00121 private:
00124 typedef enum MeshBitTypes {
00125 NormalPerFace = 0x0001,
00126 NormalPerVertex = 0x0002,
00127 TriStrips = 0x0004,
00128 };
00129
00130 void calculateBound();
00131
00132 uint32 n;
00133 uint16 type;
00134 bool generateNormals;
00135
00136 VertexList vertex;
00137 FaceList face;
00138 FaceIndexList faceIndex;
00139 NormalList faceNormal;
00140 NormalList vertexNormal;
00141
00142 RefMaterial material;
00143 };
00144 #endif