csgeom/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 "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; // If true this class is responsible for cleanup. 00045 00046 int po_count; 00047 csMeshedPolygon* po; 00048 bool delete_po; // If true this class is responsible for cleanup. 00049 00050 int* po_indices; // Index table used in 'po'. 00051 bool delete_po_indices; 00052 00053 csFlags flags; 00054 00055 // Not given by default but automatically calculated. 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 SCF_DESTRUCT_IBASE(); 00088 } 00089 00096 void SetVertices (csVector3* vt, int vt_count, bool delete_vt) 00097 { 00098 csPolygonMesh::vt = vt; 00099 csPolygonMesh::vt_count = vt_count; 00100 csPolygonMesh::delete_vt = delete_vt; 00101 ShapeChanged (); 00102 } 00103 00110 void SetPolygons (csMeshedPolygon* po, int po_count, bool delete_po) 00111 { 00112 csPolygonMesh::po = po; 00113 csPolygonMesh::po_count = po_count; 00114 csPolygonMesh::delete_po = delete_po; 00115 ShapeChanged (); 00116 } 00117 00121 void SetPolygonIndices (int* po_indices, bool delete_po_indices) 00122 { 00123 csPolygonMesh::po_indices = po_indices; 00124 csPolygonMesh::delete_po_indices = delete_po_indices; 00125 ShapeChanged (); 00126 } 00127 00133 void SetPolygonIndexCount (int po_index_count) 00134 { 00135 po_indices = new int[po_index_count]; 00136 delete_po_indices = true; 00137 ShapeChanged (); 00138 } 00139 00141 int* GetPolygonIndices () 00142 { 00143 return po_indices; 00144 } 00145 00151 void SetVertexCount (int vt_count) 00152 { 00153 vt = new csVector3[vt_count]; 00154 csPolygonMesh::vt_count = vt_count; 00155 delete_vt = true; 00156 ShapeChanged (); 00157 } 00158 00164 void SetPolygonCount (int po_count) 00165 { 00166 po = new csMeshedPolygon[po_count]; 00167 csPolygonMesh::po_count = po_count; 00168 delete_po = true; 00169 ShapeChanged (); 00170 } 00171 00175 void ShapeChanged () 00176 { 00177 change_nr++; 00178 } 00179 00180 SCF_DECLARE_IBASE; 00181 00182 virtual int GetVertexCount () { return vt_count; } 00183 virtual csVector3* GetVertices () { return vt; } 00184 virtual int GetPolygonCount () { return po_count; } 00185 virtual csMeshedPolygon* GetPolygons () { return po; } 00186 virtual int GetTriangleCount () 00187 { 00188 Triangulate (); 00189 return triangle_count; 00190 } 00191 virtual csTriangle* GetTriangles () 00192 { 00193 Triangulate (); 00194 return triangles; 00195 } 00196 virtual void Lock () { } 00197 virtual void Unlock () { } 00198 virtual csFlags& GetFlags () { return flags; } 00199 virtual uint32 GetChangeNumber () const { return change_nr; } 00200 }; 00201 00205 class csPolygonMeshBox : public iPolygonMesh 00206 { 00207 private: 00208 csVector3 vertices[8]; 00209 csMeshedPolygon polygons[6]; 00210 csTriangle* triangles; 00211 int vertex_indices[4*6]; 00212 uint32 change_nr; 00213 csFlags flags; 00214 00215 public: 00219 csPolygonMeshBox (const csBox3& box) 00220 { 00221 SCF_CONSTRUCT_IBASE (0); 00222 change_nr = 0; 00223 int i; 00224 for (i = 0 ; i < 6 ; i++) 00225 { 00226 polygons[i].num_vertices = 4; 00227 polygons[i].vertices = &vertex_indices[i*4]; 00228 } 00229 vertex_indices[0*4+0] = 4; 00230 vertex_indices[0*4+1] = 5; 00231 vertex_indices[0*4+2] = 1; 00232 vertex_indices[0*4+3] = 0; 00233 vertex_indices[1*4+0] = 5; 00234 vertex_indices[1*4+1] = 7; 00235 vertex_indices[1*4+2] = 3; 00236 vertex_indices[1*4+3] = 1; 00237 vertex_indices[2*4+0] = 7; 00238 vertex_indices[2*4+1] = 6; 00239 vertex_indices[2*4+2] = 2; 00240 vertex_indices[2*4+3] = 3; 00241 vertex_indices[3*4+0] = 6; 00242 vertex_indices[3*4+1] = 4; 00243 vertex_indices[3*4+2] = 0; 00244 vertex_indices[3*4+3] = 2; 00245 vertex_indices[4*4+0] = 6; 00246 vertex_indices[4*4+1] = 7; 00247 vertex_indices[4*4+2] = 5; 00248 vertex_indices[4*4+3] = 4; 00249 vertex_indices[5*4+0] = 0; 00250 vertex_indices[5*4+1] = 1; 00251 vertex_indices[5*4+2] = 3; 00252 vertex_indices[5*4+3] = 2; 00253 SetBox (box); 00254 00255 flags.SetAll (CS_POLYMESH_CLOSED | CS_POLYMESH_CONVEX 00256 | CS_POLYMESH_TRIANGLEMESH); 00257 triangles = 0; 00258 } 00259 00260 virtual ~csPolygonMeshBox () 00261 { 00262 delete[] triangles; 00263 SCF_DESTRUCT_IBASE(); 00264 } 00265 00269 void SetBox (const csBox3& box) 00270 { 00271 change_nr++; 00272 vertices[0] = box.GetCorner (0); 00273 vertices[1] = box.GetCorner (1); 00274 vertices[2] = box.GetCorner (2); 00275 vertices[3] = box.GetCorner (3); 00276 vertices[4] = box.GetCorner (4); 00277 vertices[5] = box.GetCorner (5); 00278 vertices[6] = box.GetCorner (6); 00279 vertices[7] = box.GetCorner (7); 00280 } 00281 00282 SCF_DECLARE_IBASE; 00283 00284 virtual int GetVertexCount () { return 8; } 00285 virtual csVector3* GetVertices () { return vertices; } 00286 virtual int GetPolygonCount () { return 6; } 00287 virtual csMeshedPolygon* GetPolygons () { return polygons; } 00288 virtual int GetTriangleCount () { return 12; } 00289 virtual csTriangle* GetTriangles (); 00290 virtual void Lock () { } 00291 virtual void Unlock () { } 00292 virtual csFlags& GetFlags () { return flags; } 00293 virtual uint32 GetChangeNumber () const { return change_nr; } 00294 }; 00295 00296 00297 00300 #endif // __CS_CSGEOM_POLYMESH_H__ 00301
Generated for Crystal Space by doxygen 1.2.18