CrystalSpace

Public API Reference

Main Page | Modules | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members | Related Pages

polymesh.h

00001 /*
00002     Crystal Space 3D engine
00003     Copyright (C) 2003 by Jorrit Tyberghein
00004 
00005     This library is free software; you can redistribute it and/or
00006     modify it under the terms of the GNU Library General Public
00007     License as published by the Free Software Foundation; either
00008     version 2 of the License, or (at your option) any later version.
00009 
00010     This library is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013     Library General Public License for more details.
00014 
00015     You should have received a copy of the GNU Library General Public
00016     License along with this library; if not, write to the Free
00017     Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00018 */
00019 
00020 #ifndef __CS_CSGEOM_POLYMESH_H__
00021 #define __CS_CSGEOM_POLYMESH_H__
00022 
00023 #include "csextern.h"
00024 
00025 #include "igeom/polymesh.h"
00026 #include "csgeom/vector3.h"
00027 #include "csgeom/box.h"
00028 #include "csgeom/tri.h"
00029 
00039 class CS_CSGEOM_EXPORT csPolygonMesh : public iPolygonMesh
00040 {
00041 private:
00042   uint32 change_nr;
00043 
00044   int vt_count;
00045   csVector3* vt;
00046   bool delete_vt;       // If true this class is responsible for cleanup.
00047 
00048   int po_count;
00049   csMeshedPolygon* po;
00050   bool delete_po;       // If true this class is responsible for cleanup.
00051 
00052   int* po_indices;      // Index table used in 'po'.
00053   bool delete_po_indices;
00054 
00055   csFlags flags;
00056 
00057   // Not given by default but automatically calculated.
00058   csTriangle* triangles;
00059   int triangle_count;
00060 
00061   void Triangulate ();
00062 
00063 public:
00067   csPolygonMesh ()
00068   {
00069     SCF_CONSTRUCT_IBASE (0);
00070     change_nr = 0;
00071     vt = 0;
00072     vt_count = 0;
00073     delete_vt = false;
00074     po = 0;
00075     po_count = 0;
00076     delete_po = false;
00077     po_indices = 0;
00078     delete_po_indices = false;
00079     triangles = 0;
00080     triangle_count = 0;
00081   }
00082 
00083   virtual ~csPolygonMesh ()
00084   {
00085     if (delete_vt) delete[] vt;
00086     if (delete_po) delete[] po;
00087     if (delete_po_indices) delete[] po_indices;
00088     delete[] triangles;
00089     SCF_DESTRUCT_IBASE();
00090   }
00091 
00098   void SetVertices (csVector3* vt, int vt_count, bool delete_vt)
00099   {
00100     csPolygonMesh::vt = vt;
00101     csPolygonMesh::vt_count = vt_count;
00102     csPolygonMesh::delete_vt = delete_vt;
00103     ShapeChanged ();
00104   }
00105 
00112   void SetPolygons (csMeshedPolygon* po, int po_count, bool delete_po)
00113   {
00114     csPolygonMesh::po = po;
00115     csPolygonMesh::po_count = po_count;
00116     csPolygonMesh::delete_po = delete_po;
00117     ShapeChanged ();
00118   }
00119 
00123   void SetPolygonIndices (int* po_indices, bool delete_po_indices)
00124   {
00125     csPolygonMesh::po_indices = po_indices;
00126     csPolygonMesh::delete_po_indices = delete_po_indices;
00127     ShapeChanged ();
00128   }
00129 
00135   void SetPolygonIndexCount (int po_index_count)
00136   {
00137     po_indices = new int[po_index_count];
00138     delete_po_indices = true;
00139     ShapeChanged ();
00140   }
00141 
00143   int* GetPolygonIndices ()
00144   {
00145     return po_indices;
00146   }
00147 
00153   void SetVertexCount (int vt_count)
00154   {
00155     vt = new csVector3[vt_count];
00156     csPolygonMesh::vt_count = vt_count;
00157     delete_vt = true;
00158     ShapeChanged ();
00159   }
00160 
00166   void SetPolygonCount (int po_count)
00167   {
00168     po = new csMeshedPolygon[po_count];
00169     csPolygonMesh::po_count = po_count;
00170     delete_po = true;
00171     ShapeChanged ();
00172   }
00173 
00177   void ShapeChanged ()
00178   {
00179     change_nr++;
00180   }
00181 
00182   SCF_DECLARE_IBASE;
00183 
00184   virtual int GetVertexCount () { return vt_count; }
00185   virtual csVector3* GetVertices () { return vt; }
00186   virtual int GetPolygonCount () { return po_count; }
00187   virtual csMeshedPolygon* GetPolygons () { return po; }
00188   virtual int GetTriangleCount ()
00189   {
00190     Triangulate ();
00191     return triangle_count;
00192   }
00193   virtual csTriangle* GetTriangles ()
00194   {
00195     Triangulate ();
00196     return triangles;
00197   }
00198   virtual void Lock () { }
00199   virtual void Unlock () { }
00200   virtual csFlags& GetFlags () { return flags; }
00201   virtual uint32 GetChangeNumber () const { return change_nr; }
00202 };
00203 
00207 class CS_CSGEOM_EXPORT csPolygonMeshBox : public iPolygonMesh
00208 {
00209 private:
00210   csVector3 vertices[8];
00211   csMeshedPolygon polygons[6];
00212   csTriangle* triangles;
00213   int vertex_indices[4*6];
00214   uint32 change_nr;
00215   csFlags flags;
00216 
00217 public:
00221   csPolygonMeshBox (const csBox3& box)
00222   {
00223     SCF_CONSTRUCT_IBASE (0);
00224     change_nr = 0;
00225     int i;
00226     for (i = 0 ; i < 6 ; i++)
00227     {
00228       polygons[i].num_vertices = 4;
00229       polygons[i].vertices = &vertex_indices[i*4];
00230     }
00231     vertex_indices[0*4+0] = 4;
00232     vertex_indices[0*4+1] = 5;
00233     vertex_indices[0*4+2] = 1;
00234     vertex_indices[0*4+3] = 0;
00235     vertex_indices[1*4+0] = 5;
00236     vertex_indices[1*4+1] = 7;
00237     vertex_indices[1*4+2] = 3;
00238     vertex_indices[1*4+3] = 1;
00239     vertex_indices[2*4+0] = 7;
00240     vertex_indices[2*4+1] = 6;
00241     vertex_indices[2*4+2] = 2;
00242     vertex_indices[2*4+3] = 3;
00243     vertex_indices[3*4+0] = 6;
00244     vertex_indices[3*4+1] = 4;
00245     vertex_indices[3*4+2] = 0;
00246     vertex_indices[3*4+3] = 2;
00247     vertex_indices[4*4+0] = 6;
00248     vertex_indices[4*4+1] = 7;
00249     vertex_indices[4*4+2] = 5;
00250     vertex_indices[4*4+3] = 4;
00251     vertex_indices[5*4+0] = 0;
00252     vertex_indices[5*4+1] = 1;
00253     vertex_indices[5*4+2] = 3;
00254     vertex_indices[5*4+3] = 2;
00255     SetBox (box);
00256 
00257     flags.SetAll (CS_POLYMESH_CLOSED | CS_POLYMESH_CONVEX
00258         | CS_POLYMESH_TRIANGLEMESH);
00259     triangles = 0;
00260   }
00261 
00262   virtual ~csPolygonMeshBox ()
00263   {
00264     delete[] triangles;
00265     SCF_DESTRUCT_IBASE();
00266   }
00267 
00271   void SetBox (const csBox3& box)
00272   {
00273     change_nr++;
00274     vertices[0] = box.GetCorner (0);
00275     vertices[1] = box.GetCorner (1);
00276     vertices[2] = box.GetCorner (2);
00277     vertices[3] = box.GetCorner (3);
00278     vertices[4] = box.GetCorner (4);
00279     vertices[5] = box.GetCorner (5);
00280     vertices[6] = box.GetCorner (6);
00281     vertices[7] = box.GetCorner (7);
00282   }
00283 
00284   SCF_DECLARE_IBASE;
00285 
00286   virtual int GetVertexCount () { return 8; }
00287   virtual csVector3* GetVertices () { return vertices; }
00288   virtual int GetPolygonCount () { return 6; }
00289   virtual csMeshedPolygon* GetPolygons () { return polygons; }
00290   virtual int GetTriangleCount () { return 12; }
00291   virtual csTriangle* GetTriangles ();
00292   virtual void Lock () { }
00293   virtual void Unlock () { }
00294   virtual csFlags& GetFlags () { return flags; }
00295   virtual uint32 GetChangeNumber () const { return change_nr; }
00296 };
00297 
00298 
00299 
00302 #endif // __CS_CSGEOM_POLYMESH_H__
00303 

Generated for Crystal Space by doxygen 1.3.9.1