Mesh Part.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /******************************************************************************
  2. 'MeshPart' is a single part of a mesh.
  3. Each part contains its vertex/face data in software 'MeshBase' and hardware version 'MeshRender'.
  4. Each part contains also a 'name' and a 'material'.
  5. /******************************************************************************/
  6. enum MSHP_FLAG // Mesh Part Flag
  7. {
  8. MSHP_NO_PHYS_BODY=0x01, // if no physical body will be made out of this part (this flag is checked when creating a physical body from Mesh and ignoring all parts that have this flag enabled)
  9. MSHP_HIDDEN =0x02, // if hidden (this flag is checked in 'Sweep' functions, it is not used however for rendering, for preventing mesh parts from being rendered please use 'MeshPart.drawGroup' and 'SetDrawMask')
  10. };
  11. struct MeshPart // Mesh Base + Mesh Render
  12. {
  13. MeshBase base ; // software mesh version (on CPU)
  14. MeshRender render ; // hardware mesh version (on GPU)
  15. Char8 name[32] ; // name
  16. Byte part_flag; // MSHP_FLAG
  17. // manage
  18. MeshPart& del ( ); // delete
  19. MeshPart& create(C MeshPart &src, UInt flag_and=~0); // create from 'src', 'flag_and'=MESH_BASE_FLAG
  20. void copyParams(C MeshPart &src, Bool copy_shaders=false); // copy parameters (without meshes - 'base' and 'render'), 'copy_shaders'=if also copy shaders (if set to true then you must make sure that the 'dest.render' has all required data as 'this.render')
  21. #if EE_PRIVATE
  22. void scaleParams(Flt scale);
  23. MeshPart& include (UInt flag); // include elements specified with 'flag' MESH_BASE_FLAG
  24. #endif
  25. MeshPart& exclude (UInt flag); // exclude elements specified with 'flag' MESH_BASE_FLAG
  26. MeshPart& keepOnly(UInt flag); // keep only elements specified with 'flag' MESH_BASE_FLAG
  27. // get
  28. Bool is ( )C {return base.is() || render.is();} // if has any data
  29. UInt flag ( )C; // get MESH_BASE_FLAG
  30. UInt memUsage ( )C; // get memory usage
  31. Int vtxs ( )C; // get total number of vertexes
  32. Int edges ( )C; // get total number of edges
  33. Int tris ( )C; // get total number of triangles
  34. Int quads ( )C; // get total number of quads
  35. Int faces ( )C; // get total number of faces , faces =(triangles + quads )
  36. Int trisTotal ( )C; // get total number of triangles including quads, trisTotal=(triangles + quads*2)
  37. Bool getBox (Box &box )C; // get box encapsulating the MeshPart, this method iterates through all vertexes, false on fail (if no vertexes are present)
  38. Flt area (Vec *center=null)C; // get surface area of all mesh faces, 'center'=if specified then it will be calculated as the average surface center
  39. Bool sameMaterials(C MeshPart &part)C; // if has exactly the same materials as 'part' mesh part, this method compares materials, multi materials and material variations
  40. // set
  41. #if EE_PRIVATE
  42. MeshPart& remapMaterials(Byte new_index[4]);
  43. MeshPart& setUMM ();
  44. MeshPart& setRenderEx (Bool optimize, Bool compress, Int lod_index);
  45. #endif
  46. MeshPart& delBase ( ); // delete software mesh (MeshBase )
  47. MeshPart& delRender ( ); // delete hardware mesh (MeshRender)
  48. MeshPart& setBase (Bool only_if_empty=true ); // set software version, convert 'MeshRender' to 'MeshBase', 'only_if_empty'=perform conversion only if the MeshBase is empty (if set to false then conversion is always performed)
  49. MeshPart& setRender (Bool optimize =true, Int lod_index=0 ); // set rendering version, convert 'MeshBase' to 'MeshRender', 'optimize'=if optimize the mesh by re-ordering the vertexes/triangles for optimal processing on the GPU, 'lod_index'=index of the LOD in the mesh (used to determine quality of the shader)
  50. MeshPart& setShader ( Int lod_index=0 ); // reset shader, 'lod_index'=index of the LOD in the mesh (used to determine quality of the shader)
  51. MeshPart& setShader (RENDER_MODE mode, Shader *shader, Int variation=0); // set custom shader, 'mode'=render mode for which the shader is set, 'shader'=desired shader (use null for no shader), 'variation'=variation index for which the shader is set
  52. MeshPart& setAutoTanBin( ); // automatically calculate vertex tangents and binormals if needed, if they're not needed then they will be removed
  53. MeshPart& drawGroup(Int group, Enum *draw_group_enum=null) ; // set draw group for this mesh part (default=0), 'group'=group index and at the same time it's an enum element index from 'draw_group_enum' (allowed ranges are 0..31, value of -1 will force the mesh part to never be drawn), 'draw_group_enum'=custom enum where each element represents a different group, specifying it is optional (draw group will be set either way) however recommended (it allows for setting correct draw group upon loading the Mesh even when the Enum and its elements were changed), this enum should be set to 'Mesh.drawGroupEnum' of the Mesh containing this MeshPart, this function should be used together with 'SetDrawMask' allowing to control which MeshPart's should be included in drawing
  54. Int drawGroup( )C; // get draw group assigned to this part (0..31, or -1 if invalid)
  55. // set / get
  56. MeshPart & material(C MaterialPtr &material, Int lod_index=0); // set mesh part material, 'lod_index'=index of the LOD in the mesh (used to determine quality of the shader, if it's <0 then shader will not be reset), 'material' must point to object in constant memory address (mesh will store only the pointer to the material and later use it if needed), avoid changing materials real-time during rendering, instead consider using material variations (set them once, and later select which one to use with 'SetVariation' function)
  57. C MaterialPtr& material( )C {return _variation.material;} // get mesh part material
  58. MeshPart& variations(Int variations ); Int variations( )C; // set/get number of variations (mesh parts always have at least 1 variation), variations allow for specifying different materials for mesh parts, active variation for rendering can be specified using 'SetVariation' function
  59. MeshPart& variation (Int variation, C MaterialPtr &material, Int lod_index=0); C MaterialPtr& variation (Int variation)C; // set/get material for specified variation (calling this method with index=0 is the same as calling 'material' methods), 'lod_index'=index of the LOD in the mesh (used to determine quality of the shader, if it's <0 then shader will not be reset)
  60. MeshPart& heightmap(Flt tex_scale, Int lod_index=0); // set as heightmap with 'tex_scale' (use 0 to disable heightmap mode), 'lod_index'=index of the LOD in the mesh (used to determine quality of the shader)
  61. #if EE_PRIVATE
  62. C MaterialPtr& variationNull(Int variation)C; // set/get material for specified variation (calling this method with index=0 is the same as calling 'material' methods), if index is out of range, then null is returned
  63. void variationRemove(Int variation );
  64. void variationKeep (Int variation );
  65. void variationMove (Int variation, Int new_index);
  66. void variationRemap (C Mesh &src, C Mesh &dest ); // remap variations that were originally set to 'src' mesh, into 'dest' mesh
  67. Bool heightmap()C {return _vtx_heightmap!=0;}
  68. #endif
  69. MeshPart& multiMaterial(C MaterialPtr &m0, C MaterialPtr &m1, C MaterialPtr &m2, C MaterialPtr &m3, Int lod_index=0); // set multi materials, multi materials are used for terrain meshes allowing to blend triangles smoothly between multiple terrain materials, 'lod_index'=index of the LOD in the mesh (used to determine quality of the shader, if it's <0 then shader will not be reset), materials must point to object in constant memory address (mesh will store only the pointer to the material and later use it if needed)
  70. C MaterialPtr& multiMaterial(Int i)C; // get i-th multi material
  71. // transform
  72. MeshPart& move ( C Vec &move ); // move
  73. MeshPart& scale (C Vec &scale ); // scale
  74. MeshPart& scaleMove (C Vec &scale, C Vec &move ); // scale and move
  75. MeshPart& scaleMoveBase(C Vec &scale, C Vec &move ); // scale and move (including the 'MeshBase' but without 'MeshRender')
  76. MeshPart& transform (C Matrix3 &matrix ); // transform by matrix
  77. MeshPart& transform (C Matrix &matrix ); // transform by matrix
  78. MeshPart& animate (C MemPtrN<Matrix, 256> &matrixes ); // animate by matrixes
  79. MeshPart& animate (C AnimatedSkeleton &anim_skel); // animate by skeleton
  80. // texture transform
  81. MeshPart& texMove (C Vec2 &move , Byte tex_index=0); // move texture UV's
  82. MeshPart& texScale (C Vec2 &scale, Byte tex_index=0); // scale texture UV's
  83. MeshPart& texRotate( Flt angle, Byte tex_index=0); // rotate texture UV's
  84. // operations
  85. MeshPart& boneRemap(C MemPtr<Byte, 256> &old_to_new); // remap vertex bone/matrix indexes according to bone 'old_to_new' remap
  86. void setUsedBones(Bool (&bones)[256])C;
  87. void includeUsedBones(Bool (&bones)[256])C;
  88. MeshPart& 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)
  89. MeshPart& setLeafAttachment (C Vec2 &tex ); // this method will set leaf attachment according to texture coordinates
  90. MeshPart& setLeafAttachment (C Vec &pos ); // this method will set leaf attachment at specified position
  91. MeshPart& setRandomLeafColor (Flt variation=0.3f); // this method will set random colors for different leafs (this will only affect mesh parts which have leaf-based material), 'variation'=how different the colors should be from each other (0..1)
  92. MeshPart& setRandomLeafBending( ); // this method will set unique bending for different leafs (this will only affect mesh parts which have leaf-based material)
  93. MeshPart& setRandomLeafBending(Flt random_value ); // this method will set unique bending for different leafs (this will only affect mesh parts which have leaf-based material), 'random_value'=any random value
  94. MeshPart& delRandomLeafBending( ); // this method will remove unique bending for different leafs
  95. // draw
  96. // default drawing, doesn't use automatic Frustum Culling, this doesn't draw the mesh immediately, instead it adds the mesh to a draw list
  97. void draw(C MatrixM &matrix, C Vec &vel=VecZero, C Vec &ang_vel=VecZero)C; // add mesh to draw list using 'matrix' matrix, 'vel' velocity and 'ang_vel' angular velocity, 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
  98. void draw(C AnimatedSkeleton &anim_skel )C; // add mesh to draw list using 'anim_skel' matrixes and velocities , this should be called only in RM_PREPARE, when used it will automatically draw meshes in following modes when needed: RM_SIMPLE RM_SOLID RM_SOLID_M RM_AMBIENT RM_BLEND, 'anim_skel' must point to constant memory address (the pointer is stored through which the object can be accessed later during frame rendering)
  99. void draw(C AnimatedSkeleton &anim_skel, C Material &material )C; // add mesh to draw list using 'anim_skel' matrixes and velocities , this should be called only in RM_PREPARE, when used it will automatically draw meshes in following modes when needed: RM_SIMPLE RM_SOLID RM_SOLID_M RM_AMBIENT RM_BLEND, 'anim_skel' must point to constant memory address (the pointer is stored through which the object can be accessed later during frame rendering), 'material'=material used for rendering which overrides the default material, however for performance reasons, the default shader is used, which means that the 'material' should be similar to the default material, and if it's too different then some artifacts can occur
  100. void drawShadow(C MatrixM &matrix )C; // add mesh to shadow draw list using 'matrix' matrix , this should be called only in RM_SHADOW
  101. void drawShadow(C AnimatedSkeleton &anim_skel )C; // add mesh to shadow draw list using 'anim_skel' skeleton, this should be called only in RM_SHADOW, 'anim_skel' must point to constant memory address (the pointer is stored through which the object can be accessed later during frame rendering)
  102. void drawShadow(C AnimatedSkeleton &anim_skel, C Material &material)C; // add mesh to shadow draw list using 'anim_skel' skeleton, this should be called only in RM_SHADOW, 'anim_skel' must point to constant memory address (the pointer is stored through which the object can be accessed later during frame rendering), 'material'=material used for rendering which overrides the default material, however for performance reasons, the default shader is used, which means that the 'material' should be similar to the default material, and if it's too different then some artifacts can occur
  103. // draw blended, this is an alternative to default 'draw' (typically 'draw' draws blended meshes automatically for materials with technique in blend mode), this method however draws the mesh immediately (which allows to set custom shader parameters per draw call) and provides additional control over material color, it always uses blend shaders regardless if the material has technique set in blend mode, this can be called only in RM_BLEND rendering mode, doesn't use automatic Frustum culling
  104. void drawBlend(C Vec4 *color=null)C; // draw with current matrix, 'color'=pointer to optional Material color multiplication
  105. // draw overlay
  106. void drawOverlay(C Image &image, C Color &color=WHITE)C; // draw as overlay using 'image' and 'color', this can be called only in RM_OVERLAY, doesn't use automatic Frustum culling
  107. // draw mesh outline, this can be optionally called in RM_OUTLINE in order to outline the mesh, doesn't use automatic Frustum culling
  108. void drawOutline(C Color &color)C; // draw with current matrix
  109. // draw using the "behind" effect, this can be optionally called in RM_BEHIND, doesn't use automatic Frustum culling
  110. void drawBehind(C Color &color_perp, C Color &color_parallel)C; // draw with current matrix, 'color_perp'=color to be used for normals perpendicular to camera, 'color_parallel'=color to be used for normals parallel to camera
  111. // draw by highlight bone in mesh skin, use this for debugging, this can be called only in RM_BLEND rendering mode, doesn't use automatic Frustum culling
  112. void drawBoneHighlight(Int bone)C; // draw with current matrix, 'bone'=bone index to highlight
  113. #if EE_PRIVATE
  114. // io
  115. Bool save (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
  116. Bool load (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
  117. 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
  118. 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
  119. 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
  120. 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
  121. Bool loadOld (File &f, CChar *path=null) ;
  122. Bool loadOld0(File &f, CChar *path=null) ;
  123. void zero();
  124. #endif
  125. MeshPart& operator+=(C MeshPart &src); // add 'src'
  126. ~MeshPart() {del();}
  127. MeshPart();
  128. #if !EE_PRIVATE
  129. private:
  130. #endif
  131. struct Variation
  132. {
  133. mutable Int last_solid_instance, last_shadow_instance; // keep 'last_solid_instance' as first member, because it's used most often, have to keep 2 separate, because in forward renderer we queue solid draw calls, but before we draw them, we process shadows first
  134. MaterialPtr material;
  135. Shader *shader[RM_SHADER_NUM];
  136. #if !EE_PRIVATE
  137. Ptr frst, blst;
  138. #else
  139. void zero();
  140. void del ();
  141. Bool save(File &f, CChar *path=null)C;
  142. Bool load(File &f, CChar *path=null) ;
  143. FRST *frst;
  144. BLST *blst;
  145. void unlinkSolid ()C {last_solid_instance =-1;}
  146. void unlinkShadow()C {last_shadow_instance=-1;}
  147. void unlink ()C {unlinkSolid(); unlinkShadow();}
  148. Bool drawn ()C {return last_solid_instance>=0 || last_shadow_instance>=0;}
  149. C Material& getMaterial ( )C {return GetMaterial (material());}
  150. C Material& getShadowMaterial(Bool reuse_default)C {return GetShadowMaterial(material(), reuse_default);}
  151. #endif
  152. Variation();
  153. };
  154. Flt _vtx_heightmap;
  155. UInt _draw_mask, _draw_mask_enum_id;
  156. Variation _variation;
  157. Mems<Variation> _variations;
  158. MaterialPtr _materials[3];
  159. #if EE_PRIVATE
  160. UniqueMultiMaterialData *_umm;
  161. #else
  162. Ptr _umm;
  163. #endif
  164. #if EE_PRIVATE
  165. C Variation& getVariation ()C;
  166. C Variation& getVariation1(Int variation_1)C; // this is "variation-1"
  167. void setShader (Int lod_index, Variation &variation);
  168. void setShaderMulti(Int lod_index);
  169. void unlinkSolid ()C;
  170. void unlinkShadow()C;
  171. void unlink ()C;
  172. #endif
  173. };
  174. /******************************************************************************/