00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef __CS_CSGEOM_POLYMESH_H__
00021 #define __CS_CSGEOM_POLYMESH_H__
00022
00023 #include "igeom/polymesh.h"
00024 #include "csgeom/vector3.h"
00025 #include "csgeom/box.h"
00026 #include "csgeom/tri.h"
00027
00037 class csPolygonMesh : public iPolygonMesh
00038 {
00039 private:
00040 uint32 change_nr;
00041
00042 int vt_count;
00043 csVector3* vt;
00044 bool delete_vt;
00045
00046 int po_count;
00047 csMeshedPolygon* po;
00048 bool delete_po;
00049
00050 int* po_indices;
00051 bool delete_po_indices;
00052
00053 csFlags flags;
00054
00055
00056 csTriangle* triangles;
00057 int triangle_count;
00058
00059 void Triangulate ();
00060
00061 public:
00065 csPolygonMesh ()
00066 {
00067 SCF_CONSTRUCT_IBASE (0);
00068 change_nr = 0;
00069 vt = 0;
00070 vt_count = 0;
00071 delete_vt = false;
00072 po = 0;
00073 po_count = 0;
00074 delete_po = false;
00075 po_indices = 0;
00076 delete_po_indices = false;
00077 triangles = 0;
00078 triangle_count = 0;
00079 }
00080
00081 virtual ~csPolygonMesh ()
00082 {
00083 if (delete_vt) delete[] vt;
00084 if (delete_po) delete[] po;
00085 if (delete_po_indices) delete[] po_indices;
00086 delete[] triangles;
00087 }
00088
00095 void SetVertices (csVector3* vt, int vt_count, bool delete_vt)
00096 {
00097 csPolygonMesh::vt = vt;
00098 csPolygonMesh::vt_count = vt_count;
00099 csPolygonMesh::delete_vt = delete_vt;
00100 ShapeChanged ();
00101 }
00102
00109 void SetPolygons (csMeshedPolygon* po, int po_count, bool delete_po)
00110 {
00111 csPolygonMesh::po = po;
00112 csPolygonMesh::po_count = po_count;
00113 csPolygonMesh::delete_po = delete_po;
00114 ShapeChanged ();
00115 }
00116
00120 void SetPolygonIndices (int* po_indices, bool delete_po_indices)
00121 {
00122 csPolygonMesh::po_indices = po_indices;
00123 csPolygonMesh::delete_po_indices = delete_po_indices;
00124 ShapeChanged ();
00125 }
00126
00132 void SetPolygonIndexCount (int po_index_count)
00133 {
00134 po_indices = new int[po_index_count];
00135 delete_po_indices = true;
00136 ShapeChanged ();
00137 }
00138
00140 int* GetPolygonIndices ()
00141 {
00142 return po_indices;
00143 }
00144
00150 void SetVertexCount (int vt_count)
00151 {
00152 vt = new csVector3[vt_count];
00153 csPolygonMesh::vt_count = vt_count;
00154 delete_vt = true;
00155 ShapeChanged ();
00156 }
00157
00163 void SetPolygonCount (int po_count)
00164 {
00165 po = new csMeshedPolygon[po_count];
00166 csPolygonMesh::po_count = po_count;
00167 delete_po = true;
00168 ShapeChanged ();
00169 }
00170
00174 void ShapeChanged ()
00175 {
00176 change_nr++;
00177 }
00178
00179 SCF_DECLARE_IBASE;
00180
00181 virtual int GetVertexCount () { return vt_count; }
00182 virtual csVector3* GetVertices () { return vt; }
00183 virtual int GetPolygonCount () { return po_count; }
00184 virtual csMeshedPolygon* GetPolygons () { return po; }
00185 virtual int GetTriangleCount ()
00186 {
00187 Triangulate ();
00188 return triangle_count;
00189 }
00190 virtual csTriangle* GetTriangles ()
00191 {
00192 Triangulate ();
00193 return triangles;
00194 }
00195 virtual void Lock () { }
00196 virtual void Unlock () { }
00197 virtual csFlags& GetFlags () { return flags; }
00198 virtual uint32 GetChangeNumber () const { return change_nr; }
00199 };
00200
00204 class csPolygonMeshBox : public iPolygonMesh
00205 {
00206 private:
00207 csVector3 vertices[8];
00208 csMeshedPolygon polygons[6];
00209 csTriangle* triangles;
00210 int vertex_indices[4*6];
00211 uint32 change_nr;
00212 csFlags flags;
00213
00214 public:
00218 csPolygonMeshBox (const csBox3& box)
00219 {
00220 SCF_CONSTRUCT_IBASE (0);
00221 change_nr = 0;
00222 int i;
00223 for (i = 0 ; i < 6 ; i++)
00224 {
00225 polygons[i].num_vertices = 4;
00226 polygons[i].vertices = &vertex_indices[i*4];
00227 }
00228 vertex_indices[0*4+0] = 4;
00229 vertex_indices[0*4+1] = 5;
00230 vertex_indices[0*4+2] = 1;
00231 vertex_indices[0*4+3] = 0;
00232 vertex_indices[1*4+0] = 5;
00233 vertex_indices[1*4+1] = 7;
00234 vertex_indices[1*4+2] = 3;
00235 vertex_indices[1*4+3] = 1;
00236 vertex_indices[2*4+0] = 7;
00237 vertex_indices[2*4+1] = 6;
00238 vertex_indices[2*4+2] = 2;
00239 vertex_indices[2*4+3] = 3;
00240 vertex_indices[3*4+0] = 6;
00241 vertex_indices[3*4+1] = 4;
00242 vertex_indices[3*4+2] = 0;
00243 vertex_indices[3*4+3] = 2;
00244 vertex_indices[4*4+0] = 6;
00245 vertex_indices[4*4+1] = 7;
00246 vertex_indices[4*4+2] = 5;
00247 vertex_indices[4*4+3] = 4;
00248 vertex_indices[5*4+0] = 0;
00249 vertex_indices[5*4+1] = 1;
00250 vertex_indices[5*4+2] = 3;
00251 vertex_indices[5*4+3] = 2;
00252 SetBox (box);
00253
00254 flags.SetAll (CS_POLYMESH_CLOSED | CS_POLYMESH_CONVEX
00255 | CS_POLYMESH_TRIANGLEMESH);
00256 triangles = 0;
00257 }
00258
00259 virtual ~csPolygonMeshBox ()
00260 {
00261 delete[] triangles;
00262 }
00263
00267 void SetBox (const csBox3& box)
00268 {
00269 change_nr++;
00270 vertices[0] = box.GetCorner (0);
00271 vertices[1] = box.GetCorner (1);
00272 vertices[2] = box.GetCorner (2);
00273 vertices[3] = box.GetCorner (3);
00274 vertices[4] = box.GetCorner (4);
00275 vertices[5] = box.GetCorner (5);
00276 vertices[6] = box.GetCorner (6);
00277 vertices[7] = box.GetCorner (7);
00278 }
00279
00280 SCF_DECLARE_IBASE;
00281
00282 virtual int GetVertexCount () { return 8; }
00283 virtual csVector3* GetVertices () { return vertices; }
00284 virtual int GetPolygonCount () { return 6; }
00285 virtual csMeshedPolygon* GetPolygons () { return polygons; }
00286 virtual int GetTriangleCount () { return 12; }
00287 virtual csTriangle* GetTriangles ();
00288 virtual void Lock () { }
00289 virtual void Unlock () { }
00290 virtual csFlags& GetFlags () { return flags; }
00291 virtual uint32 GetChangeNumber () const { return change_nr; }
00292 };
00293
00294
00295
00298 #endif // __CS_CSGEOM_POLYMESH_H__
00299