BKE_DerivedMesh.h 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838
  1. /*
  2. * ***** BEGIN GPL LICENSE BLOCK *****
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version 2
  7. * of the License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software Foundation,
  16. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. *
  18. * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
  19. * All rights reserved.
  20. *
  21. * The Original Code is: all of this file.
  22. *
  23. * Contributor(s): none yet.
  24. *
  25. * ***** END GPL LICENSE BLOCK *****
  26. */
  27. #ifndef __BKE_DERIVEDMESH_H__
  28. #define __BKE_DERIVEDMESH_H__
  29. /** \file BKE_DerivedMesh.h
  30. * \ingroup bke
  31. *
  32. * Basic design of the DerivedMesh system:
  33. *
  34. * DerivedMesh is a common set of interfaces for mesh systems.
  35. *
  36. * There are three main mesh data structures in Blender:
  37. * #Mesh, #CDDerivedMesh and #BMesh.
  38. *
  39. * These, and a few others, all implement DerivedMesh interfaces,
  40. * which contains unified drawing interfaces, a few utility interfaces,
  41. * and a bunch of read-only interfaces intended mostly for conversion from
  42. * one format to another.
  43. *
  44. * All Mesh structures in blender make use of CustomData, which is used to store
  45. * per-element attributes and interpolate them (e.g. uvs, vcols, vgroups, etc).
  46. *
  47. * Mesh is the "serialized" structure, used for storing object-mode mesh data
  48. * and also for saving stuff to disk. It's interfaces are also what DerivedMesh
  49. * uses to communicate with.
  50. *
  51. * CDDM is a little mesh library, that uses Mesh data structures in the backend.
  52. * It's mostly used for modifiers, and has the advantages of not taking much
  53. * resources.
  54. *
  55. * BMesh is a full-on brep, used for editmode, some modifiers, etc. It's much
  56. * more capable (if memory-intensive) then CDDM.
  57. *
  58. * DerivedMesh is somewhat hackish. Many places assumes that a DerivedMesh is
  59. * a CDDM (most of the time by simply copying it and converting it to one).
  60. * CDDM is the original structure for modifiers, but has since been superseded
  61. * by BMesh, at least for the foreseeable future.
  62. */
  63. /*
  64. * Note: This structure is read-only, for all practical purposes.
  65. * At some point in the future, we may want to consider
  66. * creating a replacement structure that implements a proper
  67. * abstract mesh kernel interface. Or, we can leave this
  68. * as it is and stick with using BMesh and CDDM.
  69. */
  70. #include "DNA_defs.h"
  71. #include "DNA_customdata_types.h"
  72. #include "DNA_meshdata_types.h"
  73. #include "BLI_compiler_attrs.h"
  74. #include "BKE_customdata.h"
  75. #include "BKE_bvhutils.h"
  76. struct CCGElem;
  77. struct CCGKey;
  78. struct MVert;
  79. struct MEdge;
  80. struct MFace;
  81. struct MTFace;
  82. struct Object;
  83. struct Scene;
  84. struct Mesh;
  85. struct MLoopNorSpaceArray;
  86. struct BMEditMesh;
  87. struct KeyBlock;
  88. struct ModifierData;
  89. struct MCol;
  90. struct ColorBand;
  91. struct GPUVertexAttribs;
  92. struct GPUDrawObject;
  93. struct PBVH;
  94. /* number of sub-elements each mesh element has (for interpolation) */
  95. #define SUB_ELEMS_VERT 0
  96. #define SUB_ELEMS_EDGE 2
  97. #define SUB_ELEMS_FACE 50
  98. /*
  99. * Note: all mface interfaces now officially operate on tessellated data.
  100. * Also, the mface origindex layer indexes mpolys, not mfaces.
  101. */
  102. typedef struct DMCoNo {
  103. float co[3];
  104. float no[3];
  105. } DMCoNo;
  106. /* keep in sync with MFace/MPoly types */
  107. typedef struct DMFlagMat {
  108. short mat_nr;
  109. char flag;
  110. } DMFlagMat;
  111. typedef enum DerivedMeshType {
  112. DM_TYPE_CDDM,
  113. DM_TYPE_EDITBMESH,
  114. DM_TYPE_CCGDM
  115. } DerivedMeshType;
  116. typedef enum DMDrawOption {
  117. /* the element is hidden or otherwise non-drawable */
  118. DM_DRAW_OPTION_SKIP = 0,
  119. /* normal drawing */
  120. DM_DRAW_OPTION_NORMAL = 1,
  121. /* draw, but don't set the color from mcol */
  122. DM_DRAW_OPTION_NO_MCOL = 2,
  123. /* used in drawMappedFaces, use GL stipple for the face */
  124. DM_DRAW_OPTION_STIPPLE = 3,
  125. } DMDrawOption;
  126. /* Drawing callback types */
  127. typedef int (*DMSetMaterial)(int mat_nr, void *attribs);
  128. typedef int (*DMCompareDrawOptions)(void *userData, int cur_index, int next_index);
  129. typedef void (*DMSetDrawInterpOptions)(void *userData, int index, float t);
  130. typedef DMDrawOption (*DMSetDrawOptions)(void *userData, int index);
  131. typedef DMDrawOption (*DMSetDrawOptionsMappedTex)(void *userData, int origindex, int mat_nr);
  132. typedef DMDrawOption (*DMSetDrawOptionsTex)(struct MTexPoly *mtexpoly, const bool has_vcol, int matnr);
  133. typedef enum DMDrawFlag {
  134. DM_DRAW_USE_COLORS = (1 << 0),
  135. DM_DRAW_ALWAYS_SMOOTH = (1 << 1),
  136. DM_DRAW_USE_ACTIVE_UV = (1 << 2),
  137. DM_DRAW_USE_TEXPAINT_UV = (1 << 3),
  138. DM_DRAW_SKIP_HIDDEN = (1 << 4),
  139. DM_DRAW_SKIP_SELECT = (1 << 5),
  140. DM_DRAW_SELECT_USE_EDITMODE = (1 << 6),
  141. DM_DRAW_NEED_NORMALS = (1 << 7)
  142. } DMDrawFlag;
  143. typedef enum DMForeachFlag {
  144. DM_FOREACH_NOP = 0,
  145. DM_FOREACH_USE_NORMAL = (1 << 0), /* foreachMappedVert, foreachMappedLoop, foreachMappedFaceCenter */
  146. } DMForeachFlag;
  147. typedef enum DMDirtyFlag {
  148. /* dm has valid tessellated faces, but tessellated CDDATA need to be updated. */
  149. DM_DIRTY_TESS_CDLAYERS = 1 << 0,
  150. /* One of the MCOL layers have been updated, force updating of GPUDrawObject's colors buffer.
  151. * This is necessary with modern, VBO draw code, as e.g. in vpaint mode me->mcol may be updated
  152. * without actually rebuilding dm (hence by defautl keeping same GPUDrawObject, and same colors
  153. * buffer, which prevents update during a stroke!). */
  154. DM_DIRTY_MCOL_UPDATE_DRAW = 1 << 1,
  155. /* check this with modifier dependsOnNormals callback to see if normals need recalculation */
  156. DM_DIRTY_NORMALS = 1 << 2,
  157. } DMDirtyFlag;
  158. typedef struct DerivedMesh DerivedMesh;
  159. struct DerivedMesh {
  160. /** Private DerivedMesh data, only for internal DerivedMesh use */
  161. CustomData vertData, edgeData, faceData, loopData, polyData;
  162. int numVertData, numEdgeData, numTessFaceData, numLoopData, numPolyData;
  163. int needsFree; /* checked on ->release, is set to 0 for cached results */
  164. int deformedOnly; /* set by modifier stack if only deformed from original */
  165. BVHCache *bvhCache;
  166. struct GPUDrawObject *drawObject;
  167. DerivedMeshType type;
  168. float auto_bump_scale;
  169. DMDirtyFlag dirty;
  170. int totmat; /* total materials. Will be valid only before object drawing. */
  171. struct Material **mat; /* material array. Will be valid only before object drawing */
  172. /**
  173. * \warning Typical access is done via #getLoopTriArray, #getNumLoopTri.
  174. */
  175. struct {
  176. /* WARNING! swapping between array (ready-to-be-used data) and array_wip (where data is actually computed)
  177. * shall always be protected by same lock as one used for looptris computing. */
  178. struct MLoopTri *array, *array_wip;
  179. int num;
  180. int num_alloc;
  181. } looptris;
  182. /* use for converting to BMesh which doesn't store bevel weight and edge crease by default */
  183. char cd_flag;
  184. short tangent_mask; /* which tangent layers are calculated */
  185. /** Calculate vert and face normals */
  186. void (*calcNormals)(DerivedMesh *dm);
  187. /** Calculate loop (split) normals */
  188. void (*calcLoopNormals)(DerivedMesh *dm, const bool use_split_normals, const float split_angle);
  189. /** Calculate loop (split) normals, and returns split loop normal spacearr. */
  190. void (*calcLoopNormalsSpaceArray)(DerivedMesh *dm, const bool use_split_normals, const float split_angle,
  191. struct MLoopNorSpaceArray *r_lnors_spacearr);
  192. void (*calcLoopTangents)(
  193. DerivedMesh *dm, bool calc_active_tangent,
  194. const char (*tangent_names)[MAX_NAME], int tangent_names_count);
  195. /** Recalculates mesh tessellation */
  196. void (*recalcTessellation)(DerivedMesh *dm);
  197. /** Loop tessellation cache (WARNING! Only call inside threading-protected code!) */
  198. void (*recalcLoopTri)(DerivedMesh *dm);
  199. /** accessor functions */
  200. const struct MLoopTri *(*getLoopTriArray)(DerivedMesh * dm);
  201. int (*getNumLoopTri)(DerivedMesh *dm);
  202. /* Misc. Queries */
  203. /* Also called in Editmode */
  204. int (*getNumVerts)(DerivedMesh *dm);
  205. int (*getNumEdges)(DerivedMesh *dm);
  206. int (*getNumTessFaces)(DerivedMesh *dm);
  207. int (*getNumLoops)(DerivedMesh *dm);
  208. int (*getNumPolys)(DerivedMesh *dm);
  209. /** Copy a single vert/edge/tessellated face from the derived mesh into
  210. * ``*r_{vert/edge/face}``. note that the current implementation
  211. * of this function can be quite slow, iterating over all
  212. * elements (editmesh)
  213. */
  214. void (*getVert)(DerivedMesh *dm, int index, struct MVert *r_vert);
  215. void (*getEdge)(DerivedMesh *dm, int index, struct MEdge *r_edge);
  216. void (*getTessFace)(DerivedMesh *dm, int index, struct MFace *r_face);
  217. /** Return a pointer to the entire array of verts/edges/face from the
  218. * derived mesh. if such an array does not exist yet, it will be created,
  219. * and freed on the next ->release(). consider using getVert/Edge/Face if
  220. * you are only interested in a few verts/edges/faces.
  221. */
  222. struct MVert *(*getVertArray)(DerivedMesh * dm);
  223. struct MEdge *(*getEdgeArray)(DerivedMesh * dm);
  224. struct MFace *(*getTessFaceArray)(DerivedMesh * dm);
  225. struct MLoop *(*getLoopArray)(DerivedMesh * dm);
  226. struct MPoly *(*getPolyArray)(DerivedMesh * dm);
  227. /** Copy all verts/edges/faces from the derived mesh into
  228. * *{vert/edge/face}_r (must point to a buffer large enough)
  229. */
  230. void (*copyVertArray)(DerivedMesh *dm, struct MVert *r_vert);
  231. void (*copyEdgeArray)(DerivedMesh *dm, struct MEdge *r_edge);
  232. void (*copyTessFaceArray)(DerivedMesh *dm, struct MFace *r_face);
  233. void (*copyLoopArray)(DerivedMesh *dm, struct MLoop *r_loop);
  234. void (*copyPolyArray)(DerivedMesh *dm, struct MPoly *r_poly);
  235. /** Return a copy of all verts/edges/faces from the derived mesh
  236. * it is the caller's responsibility to free the returned pointer
  237. */
  238. struct MVert *(*dupVertArray)(DerivedMesh * dm);
  239. struct MEdge *(*dupEdgeArray)(DerivedMesh * dm);
  240. struct MFace *(*dupTessFaceArray)(DerivedMesh * dm);
  241. struct MLoop *(*dupLoopArray)(DerivedMesh * dm);
  242. struct MPoly *(*dupPolyArray)(DerivedMesh * dm);
  243. /** Return a pointer to a single element of vert/edge/face custom data
  244. * from the derived mesh (this gives a pointer to the actual data, not
  245. * a copy)
  246. */
  247. void *(*getVertData)(DerivedMesh *dm, int index, int type);
  248. void *(*getEdgeData)(DerivedMesh *dm, int index, int type);
  249. void *(*getTessFaceData)(DerivedMesh *dm, int index, int type);
  250. void *(*getPolyData)(DerivedMesh *dm, int index, int type);
  251. /** Return a pointer to the entire array of vert/edge/face custom data
  252. * from the derived mesh (this gives a pointer to the actual data, not
  253. * a copy)
  254. */
  255. void *(*getVertDataArray)(DerivedMesh *dm, int type);
  256. void *(*getEdgeDataArray)(DerivedMesh *dm, int type);
  257. void *(*getTessFaceDataArray)(DerivedMesh *dm, int type);
  258. void *(*getLoopDataArray)(DerivedMesh *dm, int type);
  259. void *(*getPolyDataArray)(DerivedMesh *dm, int type);
  260. /** Retrieves the base CustomData structures for
  261. * verts/edges/tessfaces/loops/facdes*/
  262. CustomData *(*getVertDataLayout)(DerivedMesh * dm);
  263. CustomData *(*getEdgeDataLayout)(DerivedMesh * dm);
  264. CustomData *(*getTessFaceDataLayout)(DerivedMesh * dm);
  265. CustomData *(*getLoopDataLayout)(DerivedMesh * dm);
  266. CustomData *(*getPolyDataLayout)(DerivedMesh * dm);
  267. /** Copies all customdata for an element source into dst at index dest */
  268. void (*copyFromVertCData)(DerivedMesh *dm, int source, CustomData *dst, int dest);
  269. void (*copyFromEdgeCData)(DerivedMesh *dm, int source, CustomData *dst, int dest);
  270. void (*copyFromFaceCData)(DerivedMesh *dm, int source, CustomData *dst, int dest);
  271. /** Optional grid access for subsurf */
  272. int (*getNumGrids)(DerivedMesh *dm);
  273. int (*getGridSize)(DerivedMesh *dm);
  274. struct CCGElem **(*getGridData)(DerivedMesh * dm);
  275. int *(*getGridOffset)(DerivedMesh * dm);
  276. void (*getGridKey)(DerivedMesh *dm, struct CCGKey *key);
  277. DMFlagMat *(*getGridFlagMats)(DerivedMesh * dm);
  278. unsigned int **(*getGridHidden)(DerivedMesh * dm);
  279. /** Iterate over each mapped vertex in the derived mesh, calling the
  280. * given function with the original vert and the mapped vert's new
  281. * coordinate and normal. For historical reasons the normal can be
  282. * passed as a float or short array, only one should be non-NULL.
  283. */
  284. void (*foreachMappedVert)(DerivedMesh *dm,
  285. void (*func)(void *userData, int index, const float co[3],
  286. const float no_f[3], const short no_s[3]),
  287. void *userData,
  288. DMForeachFlag flag);
  289. /** Iterate over each mapped edge in the derived mesh, calling the
  290. * given function with the original edge and the mapped edge's new
  291. * coordinates.
  292. */
  293. void (*foreachMappedEdge)(DerivedMesh *dm,
  294. void (*func)(void *userData, int index,
  295. const float v0co[3], const float v1co[3]),
  296. void *userData);
  297. /** Iterate over each mapped loop in the derived mesh, calling the given function
  298. * with the original loop index and the mapped loops's new coordinate and normal.
  299. */
  300. void (*foreachMappedLoop)(DerivedMesh *dm,
  301. void (*func)(void *userData, int vertex_index, int face_index,
  302. const float co[3], const float no[3]),
  303. void *userData,
  304. DMForeachFlag flag);
  305. /** Iterate over each mapped face in the derived mesh, calling the
  306. * given function with the original face and the mapped face's (or
  307. * faces') center and normal.
  308. */
  309. void (*foreachMappedFaceCenter)(DerivedMesh *dm,
  310. void (*func)(void *userData, int index,
  311. const float cent[3], const float no[3]),
  312. void *userData,
  313. DMForeachFlag flag);
  314. /** Iterate over all vertex points, calling DO_MINMAX with given args.
  315. *
  316. * Also called in Editmode
  317. */
  318. void (*getMinMax)(DerivedMesh *dm, float r_min[3], float r_max[3]);
  319. /** Direct Access Operations
  320. * - Can be undefined
  321. * - Must be defined for modifiers that only deform however */
  322. /** Get vertex location, undefined if index is not valid */
  323. void (*getVertCo)(DerivedMesh *dm, int index, float r_co[3]);
  324. /** Fill the array (of length .getNumVerts()) with all vertex locations */
  325. void (*getVertCos)(DerivedMesh *dm, float (*r_cos)[3]);
  326. /** Get smooth vertex normal, undefined if index is not valid */
  327. void (*getVertNo)(DerivedMesh *dm, int index, float r_no[3]);
  328. void (*getPolyNo)(DerivedMesh *dm, int index, float r_no[3]);
  329. /** Get a map of vertices to faces
  330. */
  331. const struct MeshElemMap *(*getPolyMap)(struct Object *ob, DerivedMesh *dm);
  332. /** Get the BVH used for paint modes
  333. */
  334. struct PBVH *(*getPBVH)(struct Object *ob, DerivedMesh *dm);
  335. /* Drawing Operations */
  336. /** Draw all vertices as bgl points (no options) */
  337. void (*drawVerts)(DerivedMesh *dm);
  338. /** Draw edges in the UV mesh (if exists) */
  339. void (*drawUVEdges)(DerivedMesh *dm);
  340. /** Draw all edges as lines (no options)
  341. *
  342. * Also called for *final* editmode DerivedMeshes
  343. */
  344. void (*drawEdges)(DerivedMesh *dm, bool drawLooseEdges, bool drawAllEdges);
  345. /** Draw all loose edges (edges w/ no adjoining faces) */
  346. void (*drawLooseEdges)(DerivedMesh *dm);
  347. /** Draw all faces
  348. * o Set face normal or vertex normal based on inherited face flag
  349. * o Use inherited face material index to call setMaterial
  350. * o Only if setMaterial returns true
  351. *
  352. * Also called for *final* editmode DerivedMeshes
  353. */
  354. void (*drawFacesSolid)(DerivedMesh *dm, float (*partial_redraw_planes)[4],
  355. bool fast, DMSetMaterial setMaterial);
  356. /** Draw all faces using MTFace
  357. * - Drawing options too complicated to enumerate, look at code.
  358. */
  359. void (*drawFacesTex)(DerivedMesh *dm,
  360. DMSetDrawOptionsTex setDrawOptions,
  361. DMCompareDrawOptions compareDrawOptions,
  362. void *userData, DMDrawFlag flag);
  363. /** Draw all faces with GLSL materials
  364. * o setMaterial is called for every different material nr
  365. * o Only if setMaterial returns true
  366. */
  367. void (*drawFacesGLSL)(DerivedMesh *dm, DMSetMaterial setMaterial);
  368. /** Draw mapped faces (no color, or texture)
  369. * - Only if !setDrawOptions or
  370. * setDrawOptions(userData, mapped-face-index, r_drawSmooth)
  371. * returns true
  372. *
  373. * If drawSmooth is set to true then vertex normals should be set and
  374. * glShadeModel called with GL_SMOOTH. Otherwise the face normal should
  375. * be set and glShadeModel called with GL_FLAT.
  376. *
  377. * The setDrawOptions is allowed to not set drawSmooth (for example, when
  378. * lighting is disabled), in which case the implementation should draw as
  379. * smooth shaded.
  380. */
  381. void (*drawMappedFaces)(DerivedMesh *dm,
  382. DMSetDrawOptions setDrawOptions,
  383. DMSetMaterial setMaterial,
  384. DMCompareDrawOptions compareDrawOptions,
  385. void *userData,
  386. DMDrawFlag flag);
  387. /** Draw mapped faces using MTFace
  388. * - Drawing options too complicated to enumerate, look at code.
  389. */
  390. void (*drawMappedFacesTex)(DerivedMesh *dm,
  391. DMSetDrawOptionsMappedTex setDrawOptions,
  392. DMCompareDrawOptions compareDrawOptions,
  393. void *userData, DMDrawFlag flag);
  394. /** Draw mapped faces with GLSL materials
  395. * - setMaterial is called for every different material nr
  396. * - setDrawOptions is called for every face
  397. * - Only if setMaterial and setDrawOptions return true
  398. */
  399. void (*drawMappedFacesGLSL)(DerivedMesh *dm,
  400. DMSetMaterial setMaterial,
  401. DMSetDrawOptions setDrawOptions,
  402. void *userData);
  403. /** Draw mapped edges as lines
  404. * - Only if !setDrawOptions or setDrawOptions(userData, mapped-edge)
  405. * returns true
  406. */
  407. void (*drawMappedEdges)(DerivedMesh *dm,
  408. DMSetDrawOptions setDrawOptions,
  409. void *userData);
  410. /** Draw mapped edges as lines with interpolation values
  411. * - Only if !setDrawOptions or
  412. * setDrawOptions(userData, mapped-edge, mapped-v0, mapped-v1, t)
  413. * returns true
  414. *
  415. * NOTE: This routine is optional!
  416. */
  417. void (*drawMappedEdgesInterp)(DerivedMesh *dm,
  418. DMSetDrawOptions setDrawOptions,
  419. DMSetDrawInterpOptions setDrawInterpOptions,
  420. void *userData);
  421. /** Draw all faces with materials
  422. * - setMaterial is called for every different material nr
  423. * - setFace is called to verify if a face must be hidden
  424. */
  425. void (*drawMappedFacesMat)(DerivedMesh *dm,
  426. void (*setMaterial)(void *userData, int matnr, void *attribs),
  427. bool (*setFace)(void *userData, int index), void *userData);
  428. struct GPUDrawObject *(*gpuObjectNew)(DerivedMesh *dm);
  429. void (*copy_gpu_data)(DerivedMesh *dm, int type, void *varray_p,
  430. const int *mat_orig_to_new, const void *user_data);
  431. /** Release reference to the DerivedMesh. This function decides internally
  432. * if the DerivedMesh will be freed, or cached for later use. */
  433. void (*release)(DerivedMesh *dm);
  434. };
  435. void DM_init_funcs(DerivedMesh *dm);
  436. void DM_init(
  437. DerivedMesh *dm, DerivedMeshType type, int numVerts, int numEdges,
  438. int numFaces, int numLoops, int numPolys);
  439. void DM_from_template_ex(
  440. DerivedMesh *dm, DerivedMesh *source, DerivedMeshType type,
  441. int numVerts, int numEdges, int numTessFaces,
  442. int numLoops, int numPolys,
  443. CustomDataMask mask);
  444. void DM_from_template(
  445. DerivedMesh *dm, DerivedMesh *source,
  446. DerivedMeshType type,
  447. int numVerts, int numEdges, int numFaces,
  448. int numLoops, int numPolys);
  449. /** utility function to release a DerivedMesh's layers
  450. * returns 1 if DerivedMesh has to be released by the backend, 0 otherwise
  451. */
  452. int DM_release(DerivedMesh *dm);
  453. /** utility function to convert a DerivedMesh to a Mesh
  454. */
  455. void DM_to_mesh(DerivedMesh *dm, struct Mesh *me, struct Object *ob, CustomDataMask mask, bool take_ownership);
  456. struct BMEditMesh *DM_to_editbmesh(
  457. struct DerivedMesh *dm,
  458. struct BMEditMesh *existing, const bool do_tessellate);
  459. /* conversion to bmesh only */
  460. void DM_to_bmesh_ex(struct DerivedMesh *dm, struct BMesh *bm, const bool calc_face_normal);
  461. struct BMesh *DM_to_bmesh(struct DerivedMesh *dm, const bool calc_face_normal);
  462. /** Utility function to convert a DerivedMesh to a shape key block */
  463. void DM_to_meshkey(DerivedMesh *dm, struct Mesh *me, struct KeyBlock *kb);
  464. void DM_set_only_copy(DerivedMesh *dm, CustomDataMask mask);
  465. /* adds a vertex/edge/face custom data layer to a DerivedMesh, optionally
  466. * backed by an external data array
  467. * alloctype defines how the layer is allocated or copied, and how it is
  468. * freed, see BKE_customdata.h for the different options
  469. */
  470. void DM_add_vert_layer(
  471. struct DerivedMesh *dm, int type, int alloctype,
  472. void *layer);
  473. void DM_add_edge_layer(
  474. struct DerivedMesh *dm, int type, int alloctype,
  475. void *layer);
  476. void DM_add_tessface_layer(
  477. struct DerivedMesh *dm, int type, int alloctype,
  478. void *layer);
  479. void DM_add_loop_layer(
  480. DerivedMesh *dm, int type, int alloctype,
  481. void *layer);
  482. void DM_add_poly_layer(
  483. struct DerivedMesh *dm, int type, int alloctype,
  484. void *layer);
  485. /* custom data access functions
  486. * return pointer to data from first layer which matches type
  487. * if they return NULL for valid indices, data doesn't exist
  488. * note these return pointers - any change modifies the internals of the mesh
  489. */
  490. void *DM_get_vert_data(struct DerivedMesh *dm, int index, int type);
  491. void *DM_get_edge_data(struct DerivedMesh *dm, int index, int type);
  492. void *DM_get_tessface_data(struct DerivedMesh *dm, int index, int type);
  493. void *DM_get_poly_data(struct DerivedMesh *dm, int index, int type);
  494. /* custom data layer access functions
  495. * return pointer to first data layer which matches type (a flat array)
  496. * if they return NULL, data doesn't exist
  497. * note these return pointers - any change modifies the internals of the mesh
  498. */
  499. void *DM_get_vert_data_layer(struct DerivedMesh *dm, int type);
  500. void *DM_get_edge_data_layer(struct DerivedMesh *dm, int type);
  501. void *DM_get_tessface_data_layer(struct DerivedMesh *dm, int type);
  502. void *DM_get_poly_data_layer(struct DerivedMesh *dm, int type);
  503. void *DM_get_loop_data_layer(struct DerivedMesh *dm, int type);
  504. /* custom data setting functions
  505. * copy supplied data into first layer of type using layer's copy function
  506. * (deep copy if appropriate)
  507. */
  508. void DM_set_vert_data(struct DerivedMesh *dm, int index, int type, void *data);
  509. void DM_set_edge_data(struct DerivedMesh *dm, int index, int type, void *data);
  510. void DM_set_tessface_data(struct DerivedMesh *dm, int index, int type, void *data);
  511. /* custom data copy functions
  512. * copy count elements from source_index in source to dest_index in dest
  513. * these copy all layers for which the CD_FLAG_NOCOPY flag is not set
  514. */
  515. void DM_copy_vert_data(
  516. struct DerivedMesh *source, struct DerivedMesh *dest,
  517. int source_index, int dest_index, int count);
  518. void DM_copy_edge_data(
  519. struct DerivedMesh *source, struct DerivedMesh *dest,
  520. int source_index, int dest_index, int count);
  521. void DM_copy_tessface_data(
  522. struct DerivedMesh *source, struct DerivedMesh *dest,
  523. int source_index, int dest_index, int count);
  524. void DM_copy_loop_data(
  525. struct DerivedMesh *source, struct DerivedMesh *dest,
  526. int source_index, int dest_index, int count);
  527. void DM_copy_poly_data(
  528. struct DerivedMesh *source, struct DerivedMesh *dest,
  529. int source_index, int dest_index, int count);
  530. /* custom data free functions
  531. * free count elements, starting at index
  532. * they free all layers for which the CD_FLAG_NOCOPY flag is not set
  533. */
  534. void DM_free_vert_data(struct DerivedMesh *dm, int index, int count);
  535. void DM_free_edge_data(struct DerivedMesh *dm, int index, int count);
  536. void DM_free_tessface_data(struct DerivedMesh *dm, int index, int count);
  537. void DM_free_loop_data(struct DerivedMesh *dm, int index, int count);
  538. void DM_free_poly_data(struct DerivedMesh *dm, int index, int count);
  539. /*sets up mpolys for a DM based on face iterators in source*/
  540. void DM_DupPolys(DerivedMesh *source, DerivedMesh *target);
  541. void DM_ensure_normals(DerivedMesh *dm);
  542. void DM_ensure_tessface(DerivedMesh *dm);
  543. void DM_ensure_looptri_data(DerivedMesh *dm);
  544. void DM_verttri_from_looptri(MVertTri *verttri, const MLoop *mloop, const MLoopTri *looptri, int looptri_num);
  545. void DM_update_tessface_data(DerivedMesh *dm);
  546. void DM_generate_tangent_tessface_data(DerivedMesh *dm, bool generate);
  547. void DM_update_materials(DerivedMesh *dm, struct Object *ob);
  548. struct MLoopUV *DM_paint_uvlayer_active_get(DerivedMesh *dm, int mat_nr);
  549. void DM_interp_vert_data(
  550. struct DerivedMesh *source, struct DerivedMesh *dest,
  551. int *src_indices, float *weights,
  552. int count, int dest_index);
  553. typedef float EdgeVertWeight[SUB_ELEMS_EDGE][SUB_ELEMS_EDGE];
  554. void DM_interp_edge_data(
  555. struct DerivedMesh *source, struct DerivedMesh *dest,
  556. int *src_indices,
  557. float *weights, EdgeVertWeight *vert_weights,
  558. int count, int dest_index);
  559. typedef float FaceVertWeight[SUB_ELEMS_FACE][SUB_ELEMS_FACE];
  560. void DM_interp_tessface_data(
  561. struct DerivedMesh *source, struct DerivedMesh *dest,
  562. int *src_indices,
  563. float *weights, FaceVertWeight *vert_weights,
  564. int count, int dest_index);
  565. void DM_swap_tessface_data(struct DerivedMesh *dm, int index, const int *corner_indices);
  566. void DM_interp_loop_data(
  567. struct DerivedMesh *source, struct DerivedMesh *dest,
  568. int *src_indices,
  569. float *weights, int count, int dest_index);
  570. void DM_interp_poly_data(
  571. struct DerivedMesh *source, struct DerivedMesh *dest,
  572. int *src_indices,
  573. float *weights, int count, int dest_index);
  574. /* Temporary? A function to give a colorband to derivedmesh for vertexcolor ranges */
  575. void vDM_ColorBand_store(const struct ColorBand *coba, const char alert_color[4]);
  576. /* UNUSED */
  577. #if 0
  578. /** Simple function to get me->totvert amount of vertices/normals,
  579. * correctly deformed and subsurfered. Needed especially when vertexgroups are involved.
  580. * In use now by vertex/weight paint and particles */
  581. DMCoNo *mesh_get_mapped_verts_nors(struct Scene *scene, struct Object *ob);
  582. #endif
  583. void mesh_get_mapped_verts_coords(DerivedMesh *dm, float (*r_cos)[3], const int totcos);
  584. /* */
  585. DerivedMesh *mesh_get_derived_final(
  586. struct Scene *scene, struct Object *ob,
  587. CustomDataMask dataMask);
  588. DerivedMesh *mesh_get_derived_deform(
  589. struct Scene *scene, struct Object *ob,
  590. CustomDataMask dataMask);
  591. DerivedMesh *mesh_create_derived_for_modifier(
  592. struct Scene *scene, struct Object *ob,
  593. struct ModifierData *md, int build_shapekey_layers);
  594. DerivedMesh *mesh_create_derived_render(
  595. struct Scene *scene, struct Object *ob,
  596. CustomDataMask dataMask);
  597. DerivedMesh *getEditDerivedBMesh(
  598. struct BMEditMesh *em, struct Object *ob, CustomDataMask data_mask,
  599. float (*vertexCos)[3]);
  600. DerivedMesh *mesh_create_derived_index_render(
  601. struct Scene *scene, struct Object *ob,
  602. CustomDataMask dataMask, int index);
  603. /* same as above but wont use render settings */
  604. DerivedMesh *mesh_create_derived(struct Mesh *me, float (*vertCos)[3]);
  605. DerivedMesh *mesh_create_derived_view(
  606. struct Scene *scene, struct Object *ob,
  607. CustomDataMask dataMask);
  608. DerivedMesh *mesh_create_derived_no_deform(
  609. struct Scene *scene, struct Object *ob,
  610. float (*vertCos)[3],
  611. CustomDataMask dataMask);
  612. DerivedMesh *mesh_create_derived_no_deform_render(
  613. struct Scene *scene, struct Object *ob,
  614. float (*vertCos)[3],
  615. CustomDataMask dataMask);
  616. /* for gameengine */
  617. DerivedMesh *mesh_create_derived_no_virtual(
  618. struct Scene *scene, struct Object *ob, float (*vertCos)[3],
  619. CustomDataMask dataMask);
  620. DerivedMesh *mesh_create_derived_physics(
  621. struct Scene *scene, struct Object *ob, float (*vertCos)[3],
  622. CustomDataMask dataMask);
  623. DerivedMesh *editbmesh_get_derived_base(
  624. struct Object *ob, struct BMEditMesh *em, CustomDataMask data_mask);
  625. DerivedMesh *editbmesh_get_derived_cage(
  626. struct Scene *scene, struct Object *,
  627. struct BMEditMesh *em, CustomDataMask dataMask);
  628. DerivedMesh *editbmesh_get_derived_cage_and_final(
  629. struct Scene *scene, struct Object *,
  630. struct BMEditMesh *em, CustomDataMask dataMask,
  631. DerivedMesh **r_final);
  632. DerivedMesh *object_get_derived_final(struct Object *ob, const bool for_render);
  633. float (*editbmesh_get_vertex_cos(struct BMEditMesh *em, int *r_numVerts))[3];
  634. bool editbmesh_modifier_is_enabled(struct Scene *scene, struct ModifierData *md, DerivedMesh *dm);
  635. void makeDerivedMesh(
  636. struct Scene *scene, struct Object *ob, struct BMEditMesh *em,
  637. CustomDataMask dataMask, const bool build_shapekey_layers);
  638. void weight_to_rgb(float r_rgb[3], const float weight);
  639. /** Update the weight MCOL preview layer.
  640. * If weights are NULL, use object's active vgroup(s).
  641. * Else, weights must be an array of weight float values.
  642. * If indices is NULL, it must be of numVerts length.
  643. * Else, it must be of num length, as indices, which contains vertices' idx to apply weights to.
  644. * (other vertices are assumed zero weight).
  645. */
  646. void DM_update_weight_mcol(
  647. struct Object *ob, struct DerivedMesh *dm, int const draw_flag,
  648. float *weights, int num, const int *indices);
  649. /** convert layers requested by a GLSL material to actually available layers in
  650. * the DerivedMesh, with both a pointer for arrays and an offset for editmesh */
  651. typedef struct DMVertexAttribs {
  652. struct {
  653. struct MLoopUV *array;
  654. int em_offset, gl_index, gl_texco, gl_info_index;
  655. } tface[MAX_MTFACE];
  656. struct {
  657. struct MLoopCol *array;
  658. int em_offset, gl_index, gl_info_index;
  659. } mcol[MAX_MCOL];
  660. struct {
  661. float (*array)[4];
  662. int em_offset, gl_index, gl_info_index;
  663. } tang[MAX_MTFACE];
  664. struct {
  665. float (*array)[3];
  666. int em_offset, gl_index, gl_texco, gl_info_index;
  667. } orco;
  668. int tottface, totmcol, tottang, totorco;
  669. } DMVertexAttribs;
  670. void DM_vertex_attributes_from_gpu(
  671. DerivedMesh *dm,
  672. struct GPUVertexAttribs *gattribs, DMVertexAttribs *attribs);
  673. void DM_draw_attrib_vertex(DMVertexAttribs *attribs, int a, int index, int vert, int loop);
  674. void DM_draw_attrib_vertex_uniforms(const DMVertexAttribs *attribs);
  675. void DM_calc_tangents_names_from_gpu(
  676. const struct GPUVertexAttribs *gattribs,
  677. char (*tangent_names)[MAX_NAME], int *tangent_names_count);
  678. void DM_add_named_tangent_layer_for_uv(
  679. CustomData *uv_data, CustomData *tan_data, int numLoopData,
  680. const char *layer_name);
  681. #define DM_TANGENT_MASK_ORCO (1 << 9)
  682. void DM_calc_loop_tangents_step_0(
  683. const CustomData *loopData, bool calc_active_tangent,
  684. const char (*tangent_names)[MAX_NAME], int tangent_names_count,
  685. bool *rcalc_act, bool *rcalc_ren, int *ract_uv_n, int *rren_uv_n,
  686. char *ract_uv_name, char *rren_uv_name, short *rtangent_mask);
  687. void DM_calc_loop_tangents(
  688. DerivedMesh *dm, bool calc_active_tangent, const char (*tangent_names)[MAX_NAME],
  689. int tangent_names_count);
  690. void DM_calc_auto_bump_scale(DerivedMesh *dm);
  691. /** Set object's bounding box based on DerivedMesh min/max data */
  692. void DM_set_object_boundbox(struct Object *ob, DerivedMesh *dm);
  693. void DM_init_origspace(DerivedMesh *dm);
  694. /* debug only */
  695. #ifndef NDEBUG
  696. char *DM_debug_info(DerivedMesh *dm);
  697. void DM_debug_print(DerivedMesh *dm);
  698. void DM_debug_print_cdlayers(CustomData *cdata);
  699. bool DM_is_valid(DerivedMesh *dm);
  700. #endif
  701. BLI_INLINE int DM_origindex_mface_mpoly(
  702. const int *index_mf_to_mpoly, const int *index_mp_to_orig, const int i) ATTR_NONNULL(1);
  703. BLI_INLINE int DM_origindex_mface_mpoly(
  704. const int *index_mf_to_mpoly, const int *index_mp_to_orig, const int i)
  705. {
  706. const int j = index_mf_to_mpoly[i];
  707. return (j != ORIGINDEX_NONE) ? (index_mp_to_orig ? index_mp_to_orig[j] : j) : ORIGINDEX_NONE;
  708. }
  709. struct MVert *DM_get_vert_array(struct DerivedMesh *dm, bool *r_allocated);
  710. struct MEdge *DM_get_edge_array(struct DerivedMesh *dm, bool *r_allocated);
  711. struct MLoop *DM_get_loop_array(struct DerivedMesh *dm, bool *r_allocated);
  712. struct MPoly *DM_get_poly_array(struct DerivedMesh *dm, bool *r_allocated);
  713. struct MFace *DM_get_tessface_array(struct DerivedMesh *dm, bool *r_allocated);
  714. #endif /* __BKE_DERIVEDMESH_H__ */