Mesh Group.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /******************************************************************************
  2. Use 'MeshGroup' to store a group of 'Mesh' objects.
  3. /******************************************************************************/
  4. struct MeshGroup // Mesh Group (array of Meshes)
  5. {
  6. Extent ext ; // bounding box
  7. Mems<Mesh> meshes; // meshes
  8. // manage
  9. MeshGroup& del ( ); // delete
  10. MeshGroup& create(C MeshGroup &src, UInt flag_and=~0); // create from 'src', 'flag_and'=MESH_BASE_FLAG
  11. MeshGroup& create(C Mesh &src, C VecI &cells); // create from 'src' partitioned into "cells.x * cells.y * cells.z" meshes
  12. #if EE_PRIVATE
  13. MeshGroup& create(C Mesh &src, C Boxes &boxes); // create from 'src' and boxes
  14. void copyParams(C MeshGroup &src); // copy only parameters without meshes
  15. MeshGroup& include (UInt flag); // include elements specified with 'flag' MESH_BASE_FLAG
  16. #endif
  17. MeshGroup& exclude (UInt flag); // exclude elements specified with 'flag' MESH_BASE_FLAG
  18. MeshGroup& keepOnly(UInt flag); // keep only elements specified with 'flag' MESH_BASE_FLAG
  19. // get
  20. Bool is ()C {return meshes.elms()>0;} // if has any meshes
  21. UInt flag ()C; // get MESH_BASE_FLAG
  22. Int vtxs ()C; // get total number of vertexes
  23. Int edges ()C; // get total number of edges
  24. Int tris ()C; // get total number of triangles
  25. Int quads ()C; // get total number of quads
  26. Int faces ()C; // get total number of faces , faces =(triangles + quads )
  27. Int trisTotal()C; // get total number of triangles including quads, trisTotal=(triangles + quads*2)
  28. // join
  29. MeshGroup& join (Int i0, Int i1 ); // join i0-th and i1-th Mesh together
  30. MeshGroup& joinAll(Bool test_material, Bool test_draw_group, Bool test_name, UInt test_vtx_flag=0, Flt weld_pos_eps=EPS); // join all MeshParts, 'test_material'=join only those MeshParts which have the same material, 'test_draw_group'=join only those MeshParts which have the same draw group, 'test_name'=join only those MeshParts which have the same name, 'test_vtx_flag'=join only those MeshParts which have same vertex flag, 'weld_pos_eps'=epsilon used for welding vertexes after joining (use <0 to disable welding), this does not join 'meshes' together, this only joins MeshParts within the 'meshes'
  31. // transform
  32. MeshGroup& move ( C Vec &move); // move MeshGroup
  33. MeshGroup& scale (C Vec &scale ); // scale MeshGroup
  34. MeshGroup& scaleMove(C Vec &scale, C Vec &move); // scale and move MeshGroup
  35. // texture transform
  36. MeshGroup& texMove (C Vec2 &move , Byte tex_index=0); // move texture UV's
  37. MeshGroup& texScale (C Vec2 &scale, Byte tex_index=0); // scale texture UV's
  38. MeshGroup& texRotate( Flt angle, Byte tex_index=0); // rotate texture UV's
  39. #if EE_PRIVATE
  40. // texturize
  41. MeshGroup& texMap( Flt scale=1, Byte tex_index=0); // map texture UV's according to vertex XY position and scale
  42. MeshGroup& texMap(C Matrix &matrix , Byte tex_index=0); // map texture UV's according to matrix
  43. MeshGroup& texMap(C Plane &plane , Byte tex_index=0); // map texture UV's according to plane
  44. MeshGroup& texMap(C Ball &ball , Byte tex_index=0); // map texture UV's according to ball
  45. MeshGroup& texMap(C Tube &tube , Byte tex_index=0); // map texture UV's according to tube
  46. #endif
  47. // set
  48. MeshGroup& delBase ( ); // delete all software meshes (MeshBase ) in this mesh
  49. MeshGroup& delRender ( ); // delete all hardware meshes (MeshRender) in this mesh
  50. MeshGroup& setRender ( ); // set rendering version, convert all MeshBase's to MeshRender's
  51. MeshGroup& setVtxDup (UInt flag=0, Flt pos_eps=EPS, Flt nrm_cos=EPS_COL_COS); // set vertex duplicates (vtx.dup)
  52. MeshGroup& setNormals ( ); // recalculate vertex 3D normals
  53. MeshGroup& setFaceNormals( ); // recalculate triangle and quad 3D normals
  54. MeshGroup& setShader ( ); // reset shader
  55. MeshGroup& material (C MaterialPtr &material ); // set material, 'material' must point to object in constant memory address (mesh will store only the pointer to the material and later use it if needed)
  56. Bool setBox (Bool set_mesh_boxes ); // recalculate bounding box
  57. // operations
  58. #if EE_PRIVATE
  59. MeshGroup& weldVtx2D (UInt flag=0, Flt pos_eps=EPS, Flt nrm_cos=EPS_COL_COS, Flt remove_degenerate_faces_eps=EPS); // weld 2D vertexes , this function will weld vertexes together if they share the same position (ignoring Z), 'flag'=if selected elements aren't equal then don't weld (MESH_BASE_FLAG), 'remove_degenerate_faces_eps'=epsilon used for removing degenerate faces which may occur after welding vertexes (use <0 to disable removal)
  60. #endif
  61. MeshGroup& weldVtx (UInt flag=0, Flt pos_eps=EPS, Flt nrm_cos=EPS_COL_COS, Flt remove_degenerate_faces_eps=EPS); // weld 3D vertexes , this function will weld vertexes together if they share the same position , 'flag'=if selected elements aren't equal then don't weld (MESH_BASE_FLAG), 'remove_degenerate_faces_eps'=epsilon used for removing degenerate faces which may occur after welding vertexes (use <0 to disable removal)
  62. MeshGroup& weldVtxValues(UInt flag , Flt pos_eps=EPS, Flt nrm_cos=EPS_COL_COS, Flt remove_degenerate_faces_eps=EPS); // weld vertex values, this function will weld values of vertexes which share the same position , 'flag'= elements to weld (MESH_BASE_FLAG), 'remove_degenerate_faces_eps'=epsilon used for removing degenerate faces which may occur after welding vertexes (use <0 to disable removal)
  63. MeshGroup& freeOpenGLESData(); // this method is used only under OpenGL ES (on other platforms it is ignored), the method frees the software copy of the GPU data which increases available memory, however after calling this method the data can no longer be accessed on the CPU (can no longer be locked or saved to file)
  64. // convert
  65. MeshGroup& triToQuad(Flt cos=EPS_COL_COS); // triangles to quads , 'cos'=minimum cosine between 2 triangle normals to weld them into 1 quad (0..1)
  66. MeshGroup& quadToTri(Flt cos=2 ); // quads to triangles, 'cos'=minimum cosine between 2 quad triangle normals to leave them as 1 quad (0..1, value >1 converts all quads into triangles)
  67. #if EE_PRIVATE
  68. // fix
  69. MeshGroup& fixTexWrapping(Byte tex_index=0); // fix texture wrapping, fixes texture coordinates created by spherical/tube mapping
  70. MeshGroup& fixTexOffset (Byte tex_index=0); // fix texture offset , tries to reduce big texture coordinates to small ones
  71. #endif
  72. // add / remove
  73. MeshGroup& remove(Int i, Bool set_box=true); // remove i-th mesh, 'set_box'=if recalculate bounding box
  74. // optimize
  75. #if EE_PRIVATE
  76. MeshGroup& sortByMaterials (); // sort MeshParts according to their materials
  77. #endif
  78. MeshGroup& removeDegenerateFaces(Flt eps=EPS);
  79. MeshGroup& simplify(Flt intensity, Flt max_distance=1.0f, Flt max_uv=1.0f, Flt max_color=0.02f, Flt max_material=0.02f, Flt max_skin=1, Flt max_normal=PI, Bool keep_border=false, MESH_SIMPLIFY mode=SIMPLIFY_QUADRIC, Flt pos_eps=EPS, MeshGroup *dest=null, Bool *stop=null); // simplify mesh by removing vertexes/faces, 'intensity'=how much to simplify (0..1, 0=no simplification, 1=full simplification), 'max_distance'=max distance between elements to merge them (0..Inf), 'max_uv'=max allowed vertex texture UV deviations (0..1), 'max_color'=max allowed vertex color deviations (0..1), 'max_material'=max allowed vertex material deviations (0..1), 'max_skin'=max allowed vertex skin deviations (0..1), 'max_normal'=max allowed vertex normal angle deviations (0..PI), 'keep_border'=if always keep border edges (edges that have faces only on one side), 'pos_eps'=vertex position epsilon, 'dest'=destination MeshBase (if set to null then the mesh will simplify itself), 'stop'=set to 'true' on secondary thread to stop this method, returns dest
  80. MeshGroup& weldCoplanarFaces(Flt cos_face=EPS_COL_COS, Flt cos_vtx=-1, Bool safe=true, Flt max_face_length=-1); // weld coplanar faces, 'cos_face'=minimum cosine between face normals, 'cos_vtx'=minimum cosine between vertex normals, 'safe'=if process only faces without neighbors, 'max_face_length'=max allowed face length (-1=no limit)
  81. // draw
  82. #if EE_PRIVATE
  83. // helper drawing
  84. void draw2D (C Color &vtx_color, C Color &edge_color, C Color &face_color, Flt vtx_r=0.04f, Flt side_width=0.01f )C; // draw 2D
  85. void drawNormals2D( Flt length , C Color &edge_color, C Color &vtx_color=TRANSPARENT )C; // draw 2D normals
  86. void drawNormals ( Flt length , C Color &face_color, C Color &vtx_color=TRANSPARENT, C Color &tangent_color=TRANSPARENT, C Color &binormal_color=TRANSPARENT)C; // draw 3D normals
  87. #endif
  88. // default drawing, automatically uses Frustum Culling, this doesn't draw the mesh immediately, instead it adds the mesh to a draw list
  89. void draw(C MatrixM &matrix)C; // add mesh to draw list using 'matrix' matrix and no velocities, this should be called only in RM_PREPARE, when used it will automatically draw meshes in following modes when needed: RM_EARLY_Z RM_SIMPLE RM_SOLID RM_SOLID_M RM_AMBIENT RM_BLEND
  90. void drawShadow(C MatrixM &matrix)C; // add mesh to shadow draw list using 'matrix' matrix, this should be called only in RM_SHADOW
  91. // io
  92. void operator=(C Str &name) ; // load, Exit on fail
  93. Bool save (C Str &name)C; // save, false on fail
  94. Bool load (C Str &name) ; // load, false on fail
  95. Bool save (File &f, CChar *path=null)C; // save, 'path'=path at which resource is located (this is needed so that the sub-resources can be accessed with relative path), false on fail
  96. Bool load (File &f, CChar *path=null) ; // load, 'path'=path at which resource is located (this is needed so that the sub-resources can be accessed with relative path), false on fail
  97. Bool loadAdd(File &f, CChar *path=null) ; // load, 'path'=path at which resource is located (this is needed so that the sub-resources can be accessed with relative path), false on fail, this method adds the data from file to self (not replaces it)
  98. #if EE_PRIVATE
  99. Bool saveData(File &f, CChar *path=null)C; // save binary, 'path'=path at which resource is located (this is needed so that the sub-resources can be accessed with relative path), false on fail
  100. Bool loadData(File &f, CChar *path=null) ; // load binary, 'path'=path at which resource is located (this is needed so that the sub-resources can be accessed with relative path), false on fail
  101. Bool saveTxt (FileText &f, CChar *path=null)C; // save text , 'path'=path at which resource is located (this is needed so that the sub-resources can be accessed with relative path), false on fail
  102. Bool loadTxt (FileText &f, CChar *path=null) ; // load text , 'path'=path at which resource is located (this is needed so that the sub-resources can be accessed with relative path), false on fail
  103. Bool saveTxt (C Str &name )C; // save text , false on fail
  104. Bool loadTxt (C Str &name ) ; // load text , false on fail
  105. void zero();
  106. #endif
  107. MeshGroup();
  108. };
  109. /******************************************************************************/
  110. inline Int Elms(C MeshGroup &mshg) {return mshg.meshes.elms();}
  111. /******************************************************************************/
  112. #if EE_PRIVATE
  113. void ShutMesh();
  114. #endif
  115. /******************************************************************************/