## Public API Reference

- Main Page
- Modules
- Namespaces
- Classes
- Files
- Related Pages

- File List
- File Members

# csgeom/polymesh.h

Go to the documentation of this file.

    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 "csgeom/box.h"
    00026 #include "csgeom/tri.h"
    00027 #include "csgeom/vector3.h"
    00028 #include "csutil/flags.h"
    00029 #include "csutil/scf_implementation.h"
    00030
    00031 #include "igeom/polymesh.h"
    00032
    00033 struct csTriangle;
    00034
    00047 class CS_CRYSTALSPACE_EXPORT csPolygonMesh :
    00048   public scfImplementation1<csPolygonMesh, iPolygonMesh>
    00049 {
    00050 private:
    00051   uint32 change_nr;
    00052
    00053   int vt_count;
    00054   csVector3* vt;
    00055   bool delete_vt;       // If true this class is responsible for cleanup.
    00056
    00057   int po_count;
    00058   csMeshedPolygon* po;
    00059   bool delete_po;       // If true this class is responsible for cleanup.
    00060
    00061   int* po_indices;      // Index table used in 'po'.
    00062   bool delete_po_indices;
    00063
    00064   csFlags flags;
    00065
    00066   // Not given by default but automatically calculated.
    00067   csTriangle* triangles;
    00068   int triangle_count;
    00069
    00070   void Triangulate ();
    00071
    00072 public:
    00076   csPolygonMesh () : scfImplementationType(this)
    00077   {
    00078     change_nr = 0;
    00079     vt = 0;
    00080     vt_count = 0;
    00081     delete_vt = false;
    00082     po = 0;
    00083     po_count = 0;
    00084     delete_po = false;
    00085     po_indices = 0;
    00086     delete_po_indices = false;
    00087     triangles = 0;
    00088     triangle_count = 0;
    00089   }
    00090
    00091   virtual ~csPolygonMesh ()
    00092   {
    00093     if (delete_vt) delete[] vt;
    00094     if (delete_po) delete[] po;
    00095     if (delete_po_indices) delete[] po_indices;
    00096     delete[] triangles;
    00097   }
    00098
    00105   void SetVertices (csVector3* vt, int vt_count, bool delete_vt)
    00106   {
    00107     csPolygonMesh::vt = vt;
    00108     csPolygonMesh::vt_count = vt_count;
    00109     csPolygonMesh::delete_vt = delete_vt;
    00110     ShapeChanged ();
    00111   }
    00112
    00119   void SetPolygons (csMeshedPolygon* po, int po_count, bool delete_po)
    00120   {
    00121     csPolygonMesh::po = po;
    00122     csPolygonMesh::po_count = po_count;
    00123     csPolygonMesh::delete_po = delete_po;
    00124     ShapeChanged ();
    00125   }
    00126
    00130   void SetPolygonIndices (int* po_indices, bool delete_po_indices)
    00131   {
    00132     csPolygonMesh::po_indices = po_indices;
    00133     csPolygonMesh::delete_po_indices = delete_po_indices;
    00134     ShapeChanged ();
    00135   }
    00136
    00142   void SetPolygonIndexCount (int po_index_count)
    00143   {
    00144     po_indices = new int[po_index_count];
    00145     delete_po_indices = true;
    00146     ShapeChanged ();
    00147   }
    00148
    00150   int* GetPolygonIndices ()
    00151   {
    00152     return po_indices;
    00153   }
    00154
    00160   void SetVertexCount (int vt_count)
    00161   {
    00162     vt = new csVector3[vt_count];
    00163     csPolygonMesh::vt_count = vt_count;
    00164     delete_vt = true;
    00165     ShapeChanged ();
    00166   }
    00167
    00173   void SetPolygonCount (int po_count)
    00174   {
    00175     po = new csMeshedPolygon[po_count];
    00176     csPolygonMesh::po_count = po_count;
    00177     delete_po = true;
    00178     ShapeChanged ();
    00179   }
    00180
    00184   void ShapeChanged ()
    00185   {
    00186     change_nr++;
    00187   }
    00188
    00189   virtual int GetVertexCount () { return vt_count; }
    00190   virtual csVector3* GetVertices () { return vt; }
    00191   virtual int GetPolygonCount () { return po_count; }
    00192   virtual csMeshedPolygon* GetPolygons () { return po; }
    00193   virtual int GetTriangleCount ()
    00194   {
    00195     Triangulate ();
    00196     return triangle_count;
    00197   }
    00198   virtual csTriangle* GetTriangles ()
    00199   {
    00200     Triangulate ();
    00201     return triangles;
    00202   }
    00203   virtual void Lock () { }
    00204   virtual void Unlock () { }
    00205   virtual csFlags& GetFlags () { return flags; }
    00206   virtual uint32 GetChangeNumber () const { return change_nr; }
    00207 };
    00208
    00212 class CS_CRYSTALSPACE_EXPORT csPolygonMeshBox :
    00213   public virtual scfImplementation1<csPolygonMeshBox,iPolygonMesh>
    00214 {
    00215 private:
    00216   csVector3 vertices[8];
    00217   csMeshedPolygon polygons[6];
    00218   csTriangle* triangles;
    00219   int vertex_indices[4*6];
    00220   uint32 change_nr;
    00221   csFlags flags;
    00222
    00223 public:
    00227   csPolygonMeshBox (const csBox3& box) : scfImplementationType(this)
    00228   {
    00229     change_nr = 0;
    00230     int i;
    00231     for (i = 0 ; i < 6 ; i++)
    00232     {
    00233       polygons[i].num_vertices = 4;
    00234       polygons[i].vertices = &vertex_indices[i*4];
    00235     }
    00236     vertex_indices[0*4+0] = 4;
    00237     vertex_indices[0*4+1] = 5;
    00238     vertex_indices[0*4+2] = 1;
    00239     vertex_indices[0*4+3] = 0;
    00240     vertex_indices[1*4+0] = 5;
    00241     vertex_indices[1*4+1] = 7;
    00242     vertex_indices[1*4+2] = 3;
    00243     vertex_indices[1*4+3] = 1;
    00244     vertex_indices[2*4+0] = 7;
    00245     vertex_indices[2*4+1] = 6;
    00246     vertex_indices[2*4+2] = 2;
    00247     vertex_indices[2*4+3] = 3;
    00248     vertex_indices[3*4+0] = 6;
    00249     vertex_indices[3*4+1] = 4;
    00250     vertex_indices[3*4+2] = 0;
    00251     vertex_indices[3*4+3] = 2;
    00252     vertex_indices[4*4+0] = 6;
    00253     vertex_indices[4*4+1] = 7;
    00254     vertex_indices[4*4+2] = 5;
    00255     vertex_indices[4*4+3] = 4;
    00256     vertex_indices[5*4+0] = 0;
    00257     vertex_indices[5*4+1] = 1;
    00258     vertex_indices[5*4+2] = 3;
    00259     vertex_indices[5*4+3] = 2;
    00260     SetBox (box);
    00261
    00262     flags.SetAll (CS_POLYMESH_CLOSED | CS_POLYMESH_CONVEX
    00263         | CS_POLYMESH_TRIANGLEMESH);
    00264     triangles = 0;
    00265   }
    00266
    00267   virtual ~csPolygonMeshBox ()
    00268   {
    00269     delete[] triangles;
    00270   }
    00271
    00275   void SetBox (const csBox3& box)
    00276   {
    00277     change_nr++;
    00278     vertices[0] = box.GetCorner (0);
    00279     vertices[1] = box.GetCorner (1);
    00280     vertices[2] = box.GetCorner (2);
    00281     vertices[3] = box.GetCorner (3);
    00282     vertices[4] = box.GetCorner (4);
    00283     vertices[5] = box.GetCorner (5);
    00284     vertices[6] = box.GetCorner (6);
    00285     vertices[7] = box.GetCorner (7);
    00286   }
    00287
    00288   virtual int GetVertexCount () { return 8; }
    00289   virtual csVector3* GetVertices () { return vertices; }
    00290   virtual int GetPolygonCount () { return 6; }
    00291   virtual csMeshedPolygon* GetPolygons () { return polygons; }
    00292   virtual int GetTriangleCount () { return 12; }
    00293   virtual csTriangle* GetTriangles ();
    00294   virtual void Lock () { }
    00295   virtual void Unlock () { }
    00296   virtual csFlags& GetFlags () { return flags; }
    00297   virtual uint32 GetChangeNumber () const { return change_nr; }
    00298 };
    00299
    00300
    00301
    00304 #endif // __CS_CSGEOM_POLYMESH_H__
    00305