4 * ***** BEGIN GPL LICENSE BLOCK *****
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21 * All rights reserved.
23 * The Original Code is: all of this file.
25 * Contributor(s): none yet.
27 * ***** END GPL LICENSE BLOCK *****
30 #ifndef BKE_DERIVEDMESH_H
31 #define BKE_DERIVEDMESH_H
33 /** \file BKE_DerivedMesh.h
37 * - Make drawMapped* functions take a predicate function that
38 * determines whether to draw the edge (this predicate can
39 * also set color, etc). This will be slightly more general
40 * and allow some of the functions to be collapsed.
41 * - Once accessor functions are added then single element draw
42 * functions can be implemented using primitive accessors.
43 * - Add function to dispatch to renderer instead of using
47 #include "DNA_customdata_types.h"
48 #include "BKE_customdata.h"
49 #include "BKE_bvhutils.h"
63 struct GPUVertexAttribs;
68 /* number of sub-elements each mesh element has (for interpolation) */
69 #define SUB_ELEMS_VERT 0
70 #define SUB_ELEMS_EDGE 2
71 #define SUB_ELEMS_FACE 4
73 typedef struct DMGridData {
78 typedef struct DMGridAdjacency {
83 typedef enum DerivedMeshType {
89 typedef struct DerivedMesh DerivedMesh;
91 /* Private DerivedMesh data, only for internal DerivedMesh use */
92 CustomData vertData, edgeData, faceData;
93 int numVertData, numEdgeData, numFaceData;
94 int needsFree; /* checked on ->release, is set to 0 for cached results */
95 int deformedOnly; /* set by modifier stack if only deformed from original */
97 struct GPUDrawObject *drawObject;
102 /* Also called in Editmode */
103 int (*getNumVerts)(DerivedMesh *dm);
104 /* Also called in Editmode */
105 int (*getNumFaces)(DerivedMesh *dm);
107 int (*getNumEdges)(DerivedMesh *dm);
109 /* copy a single vert/edge/face from the derived mesh into
110 * *{vert/edge/face}_r. note that the current implementation
111 * of this function can be quite slow, iterating over all
112 * elements (editmesh)
114 void (*getVert)(DerivedMesh *dm, int index, struct MVert *vert_r);
115 void (*getEdge)(DerivedMesh *dm, int index, struct MEdge *edge_r);
116 void (*getFace)(DerivedMesh *dm, int index, struct MFace *face_r);
118 /* return a pointer to the entire array of verts/edges/face from the
119 * derived mesh. if such an array does not exist yet, it will be created,
120 * and freed on the next ->release(). consider using getVert/Edge/Face if
121 * you are only interested in a few verts/edges/faces.
123 struct MVert *(*getVertArray)(DerivedMesh *dm);
124 struct MEdge *(*getEdgeArray)(DerivedMesh *dm);
125 struct MFace *(*getFaceArray)(DerivedMesh *dm);
127 /* copy all verts/edges/faces from the derived mesh into
128 * *{vert/edge/face}_r (must point to a buffer large enough)
130 void (*copyVertArray)(DerivedMesh *dm, struct MVert *vert_r);
131 void (*copyEdgeArray)(DerivedMesh *dm, struct MEdge *edge_r);
132 void (*copyFaceArray)(DerivedMesh *dm, struct MFace *face_r);
134 /* return a copy of all verts/edges/faces from the derived mesh
135 * it is the caller's responsibility to free the returned pointer
137 struct MVert *(*dupVertArray)(DerivedMesh *dm);
138 struct MEdge *(*dupEdgeArray)(DerivedMesh *dm);
139 struct MFace *(*dupFaceArray)(DerivedMesh *dm);
141 /* return a pointer to a single element of vert/edge/face custom data
142 * from the derived mesh (this gives a pointer to the actual data, not
145 void *(*getVertData)(DerivedMesh *dm, int index, int type);
146 void *(*getEdgeData)(DerivedMesh *dm, int index, int type);
147 void *(*getFaceData)(DerivedMesh *dm, int index, int type);
149 /* return a pointer to the entire array of vert/edge/face custom data
150 * from the derived mesh (this gives a pointer to the actual data, not
153 void *(*getVertDataArray)(DerivedMesh *dm, int type);
154 void *(*getEdgeDataArray)(DerivedMesh *dm, int type);
155 void *(*getFaceDataArray)(DerivedMesh *dm, int type);
157 /* optional grid access for subsurf */
158 int (*getNumGrids)(DerivedMesh *dm);
159 int (*getGridSize)(DerivedMesh *dm);
160 DMGridData **(*getGridData)(DerivedMesh *dm);
161 DMGridAdjacency *(*getGridAdjacency)(DerivedMesh *dm);
162 int *(*getGridOffset)(DerivedMesh *dm);
164 /* Iterate over each mapped vertex in the derived mesh, calling the
165 * given function with the original vert and the mapped vert's new
166 * coordinate and normal. For historical reasons the normal can be
167 * passed as a float or short array, only one should be non-NULL.
169 void (*foreachMappedVert)(
171 void (*func)(void *userData, int index, float *co,
172 float *no_f, short *no_s),
175 /* Iterate over each mapped edge in the derived mesh, calling the
176 * given function with the original edge and the mapped edge's new
179 void (*foreachMappedEdge)(DerivedMesh *dm,
180 void (*func)(void *userData, int index,
181 float *v0co, float *v1co),
184 /* Iterate over each mapped face in the derived mesh, calling the
185 * given function with the original face and the mapped face's (or
186 * faces') center and normal.
188 void (*foreachMappedFaceCenter)(DerivedMesh *dm,
189 void (*func)(void *userData, int index,
190 float *cent, float *no),
193 /* Iterate over all vertex points, calling DO_MINMAX with given args.
195 * Also called in Editmode
197 void (*getMinMax)(DerivedMesh *dm, float min_r[3], float max_r[3]);
199 /* Direct Access Operations */
200 /* o Can be undefined */
201 /* o Must be defined for modifiers that only deform however */
203 /* Get vertex location, undefined if index is not valid */
204 void (*getVertCo)(DerivedMesh *dm, int index, float co_r[3]);
206 /* Fill the array (of length .getNumVerts()) with all vertex locations */
207 void (*getVertCos)(DerivedMesh *dm, float (*cos_r)[3]);
209 /* Get smooth vertex normal, undefined if index is not valid */
210 void (*getVertNo)(DerivedMesh *dm, int index, float no_r[3]);
212 /* Get a map of vertices to faces
214 struct ListBase *(*getFaceMap)(struct Object *ob, DerivedMesh *dm);
216 /* Get the BVH used for paint modes
218 struct PBVH *(*getPBVH)(struct Object *ob, DerivedMesh *dm);
220 /* Drawing Operations */
222 /* Draw all vertices as bgl points (no options) */
223 void (*drawVerts)(DerivedMesh *dm);
225 /* Jason Draw all selected vertices as bgl points (no options) */
226 void (*drawSelectedVerts)(DerivedMesh *dm);
228 /* Draw edges in the UV mesh (if exists) */
229 void (*drawUVEdges)(DerivedMesh *dm);
231 /* Draw all edges as lines (no options)
233 * Also called for *final* editmode DerivedMeshes
235 void (*drawEdges)(DerivedMesh *dm, int drawLooseEdges, int drawAllEdges);
237 /* Draw all loose edges (edges w/ no adjoining faces) */
238 void (*drawLooseEdges)(DerivedMesh *dm);
241 * o Set face normal or vertex normal based on inherited face flag
242 * o Use inherited face material index to call setMaterial
243 * o Only if setMaterial returns true
245 * Also called for *final* editmode DerivedMeshes
247 void (*drawFacesSolid)(DerivedMesh *dm, float (*partial_redraw_planes)[4],
248 int fast, int (*setMaterial)(int, void *attribs));
251 * o If useTwoSided, draw front and back using col arrays
252 * o col1,col2 are arrays of length numFace*4 of 4 component colors
253 * in ABGR format, and should be passed as per-face vertex color.
255 void (*drawFacesColored)(DerivedMesh *dm, int useTwoSided,
256 unsigned char *col1, unsigned char *col2);
258 /* Draw all faces using MTFace
259 * o Drawing options too complicated to enumerate, look at code.
261 void (*drawFacesTex)(DerivedMesh *dm,
262 int (*setDrawOptions)(struct MTFace *tface,
263 struct MCol *mcol, int matnr));
265 /* Draw all faces with GLSL materials
266 * o setMaterial is called for every different material nr
267 * o Only if setMaterial returns true
269 void (*drawFacesGLSL)(DerivedMesh *dm,
270 int (*setMaterial)(int, void *attribs));
272 /* Draw mapped faces (no color, or texture)
273 * o Only if !setDrawOptions or
274 * setDrawOptions(userData, mapped-face-index, drawSmooth_r)
277 * If drawSmooth is set to true then vertex normals should be set and
278 * glShadeModel called with GL_SMOOTH. Otherwise the face normal should
279 * be set and glShadeModel called with GL_FLAT.
281 * The setDrawOptions is allowed to not set drawSmooth (for example, when
282 * lighting is disabled), in which case the implementation should draw as
285 void (*drawMappedFaces)(DerivedMesh *dm,
286 int (*setDrawOptions)(void *userData, int index,
288 void *userData, int useColors,
289 int (*setMaterial)(int, void *attribs),
290 int (*compareDrawOptions)(void *userData, int cur_index, int next_index));
292 /* Draw mapped faces using MTFace
293 * o Drawing options too complicated to enumerate, look at code.
295 void (*drawMappedFacesTex)(DerivedMesh *dm,
296 int (*setDrawOptions)(void *userData,
300 /* Draw mapped faces with GLSL materials
301 * o setMaterial is called for every different material nr
302 * o setDrawOptions is called for every face
303 * o Only if setMaterial and setDrawOptions return true
305 void (*drawMappedFacesGLSL)(DerivedMesh *dm,
306 int (*setMaterial)(int, void *attribs),
307 int (*setDrawOptions)(void *userData, int index), void *userData);
309 /* Draw mapped edges as lines
310 * o Only if !setDrawOptions or setDrawOptions(userData, mapped-edge)
313 void (*drawMappedEdges)(DerivedMesh *dm,
314 int (*setDrawOptions)(void *userData, int index),
317 /* Draw mapped edges as lines with interpolation values
318 * o Only if !setDrawOptions or
319 * setDrawOptions(userData, mapped-edge, mapped-v0, mapped-v1, t)
322 * NOTE: This routine is optional!
324 void (*drawMappedEdgesInterp)(DerivedMesh *dm,
325 int (*setDrawOptions)(void *userData,
327 void (*setDrawInterpOptions)(void *userData,
332 /* Release reference to the DerivedMesh. This function decides internally
333 * if the DerivedMesh will be freed, or cached for later use. */
334 void (*release)(DerivedMesh *dm);
337 /* utility function to initialise a DerivedMesh's function pointers to
338 * the default implementation (for those functions which have a default)
340 void DM_init_funcs(DerivedMesh *dm);
342 /* utility function to initialise a DerivedMesh for the desired number
343 * of vertices, edges and faces (doesn't allocate memory for them, just
344 * sets up the custom data layers)
346 void DM_init(DerivedMesh *dm, DerivedMeshType type,
347 int numVerts, int numEdges, int numFaces);
349 /* utility function to initialise a DerivedMesh for the desired number
350 * of vertices, edges and faces, with a layer setup copied from source
352 void DM_from_template(DerivedMesh *dm, DerivedMesh *source,
353 DerivedMeshType type,
354 int numVerts, int numEdges, int numFaces);
356 /* utility function to release a DerivedMesh's layers
357 * returns 1 if DerivedMesh has to be released by the backend, 0 otherwise
359 int DM_release(DerivedMesh *dm);
361 /* utility function to convert a DerivedMesh to a Mesh
363 void DM_to_mesh(DerivedMesh *dm, struct Mesh *me);
365 /* utility function to convert a DerivedMesh to a shape key block
367 void DM_to_meshkey(DerivedMesh *dm, struct Mesh *me, struct KeyBlock *kb);
369 /* set the CD_FLAG_NOCOPY flag in custom data layers where the mask is
370 * zero for the layer type, so only layer types specified by the mask
373 void DM_set_only_copy(DerivedMesh *dm, CustomDataMask mask);
375 /* adds a vertex/edge/face custom data layer to a DerivedMesh, optionally
376 * backed by an external data array
377 * alloctype defines how the layer is allocated or copied, and how it is
378 * freed, see BKE_customdata.h for the different options
380 void DM_add_vert_layer(struct DerivedMesh *dm, int type, int alloctype,
382 void DM_add_edge_layer(struct DerivedMesh *dm, int type, int alloctype,
384 void DM_add_face_layer(struct DerivedMesh *dm, int type, int alloctype,
387 /* custom data access functions
388 * return pointer to data from first layer which matches type
389 * if they return NULL for valid indices, data doesn't exist
390 * note these return pointers - any change modifies the internals of the mesh
392 void *DM_get_vert_data(struct DerivedMesh *dm, int index, int type);
393 void *DM_get_edge_data(struct DerivedMesh *dm, int index, int type);
394 void *DM_get_face_data(struct DerivedMesh *dm, int index, int type);
396 /* custom data layer access functions
397 * return pointer to first data layer which matches type (a flat array)
398 * if they return NULL, data doesn't exist
399 * note these return pointers - any change modifies the internals of the mesh
401 void *DM_get_vert_data_layer(struct DerivedMesh *dm, int type);
402 void *DM_get_edge_data_layer(struct DerivedMesh *dm, int type);
403 void *DM_get_face_data_layer(struct DerivedMesh *dm, int type);
405 /* custom data setting functions
406 * copy supplied data into first layer of type using layer's copy function
407 * (deep copy if appropriate)
409 void DM_set_vert_data(struct DerivedMesh *dm, int index, int type, void *data);
410 void DM_set_edge_data(struct DerivedMesh *dm, int index, int type, void *data);
411 void DM_set_face_data(struct DerivedMesh *dm, int index, int type, void *data);
413 /* custom data copy functions
414 * copy count elements from source_index in source to dest_index in dest
415 * these copy all layers for which the CD_FLAG_NOCOPY flag is not set
417 void DM_copy_vert_data(struct DerivedMesh *source, struct DerivedMesh *dest,
418 int source_index, int dest_index, int count);
419 void DM_copy_edge_data(struct DerivedMesh *source, struct DerivedMesh *dest,
420 int source_index, int dest_index, int count);
421 void DM_copy_face_data(struct DerivedMesh *source, struct DerivedMesh *dest,
422 int source_index, int dest_index, int count);
424 /* custom data free functions
425 * free count elements, starting at index
426 * they free all layers for which the CD_FLAG_NOCOPY flag is not set
428 void DM_free_vert_data(struct DerivedMesh *dm, int index, int count);
429 void DM_free_edge_data(struct DerivedMesh *dm, int index, int count);
430 void DM_free_face_data(struct DerivedMesh *dm, int index, int count);
432 /* interpolates vertex data from the vertices indexed by src_indices in the
433 * source mesh using the given weights and stores the result in the vertex
434 * indexed by dest_index in the dest mesh
436 void DM_interp_vert_data(struct DerivedMesh *source, struct DerivedMesh *dest,
437 int *src_indices, float *weights,
438 int count, int dest_index);
440 /* interpolates edge data from the edges indexed by src_indices in the
441 * source mesh using the given weights and stores the result in the edge indexed
442 * by dest_index in the dest mesh.
443 * if weights is NULL, all weights default to 1.
444 * if vert_weights is non-NULL, any per-vertex edge data is interpolated using
445 * vert_weights[i] multiplied by weights[i].
447 typedef float EdgeVertWeight[SUB_ELEMS_EDGE][SUB_ELEMS_EDGE];
448 void DM_interp_edge_data(struct DerivedMesh *source, struct DerivedMesh *dest,
450 float *weights, EdgeVertWeight *vert_weights,
451 int count, int dest_index);
453 /* interpolates face data from the faces indexed by src_indices in the
454 * source mesh using the given weights and stores the result in the face indexed
455 * by dest_index in the dest mesh.
456 * if weights is NULL, all weights default to 1.
457 * if vert_weights is non-NULL, any per-vertex face data is interpolated using
458 * vert_weights[i] multiplied by weights[i].
460 typedef float FaceVertWeight[SUB_ELEMS_FACE][SUB_ELEMS_FACE];
461 void DM_interp_face_data(struct DerivedMesh *source, struct DerivedMesh *dest,
463 float *weights, FaceVertWeight *vert_weights,
464 int count, int dest_index);
466 void DM_swap_face_data(struct DerivedMesh *dm, int index, const int *corner_indices);
468 /* Temporary? A function to give a colorband to derivedmesh for vertexcolor ranges */
469 void vDM_ColorBand_store(struct ColorBand *coba);
471 /* Simple function to get me->totvert amount of vertices/normals,
472 correctly deformed and subsurfered. Needed especially when vertexgroups are involved.
473 In use now by vertex/weigt paint and particles */
474 float *mesh_get_mapped_verts_nors(struct Scene *scene, struct Object *ob);
477 DerivedMesh *mesh_get_derived_final(struct Scene *scene, struct Object *ob,
478 CustomDataMask dataMask);
479 DerivedMesh *mesh_get_derived_deform(struct Scene *scene, struct Object *ob,
480 CustomDataMask dataMask);
482 DerivedMesh *mesh_create_derived_for_modifier(struct Scene *scene, struct Object *ob, struct ModifierData *md);
484 DerivedMesh *mesh_create_derived_render(struct Scene *scene, struct Object *ob,
485 CustomDataMask dataMask);
487 DerivedMesh *mesh_create_derived_index_render(struct Scene *scene, struct Object *ob, CustomDataMask dataMask, int index);
489 /* same as above but wont use render settings */
490 DerivedMesh *mesh_create_derived(struct Mesh *me, struct Object *ob, float (*vertCos)[3]);
491 DerivedMesh *mesh_create_derived_view(struct Scene *scene, struct Object *ob,
492 CustomDataMask dataMask);
493 DerivedMesh *mesh_create_derived_no_deform(struct Scene *scene, struct Object *ob,
495 CustomDataMask dataMask);
496 DerivedMesh *mesh_create_derived_no_deform_render(struct Scene *scene, struct Object *ob,
498 CustomDataMask dataMask);
500 DerivedMesh *mesh_create_derived_no_virtual(struct Scene *scene, struct Object *ob, float (*vertCos)[3],
501 CustomDataMask dataMask);
502 DerivedMesh *mesh_create_derived_physics(struct Scene *scene, struct Object *ob, float (*vertCos)[3],
503 CustomDataMask dataMask);
505 DerivedMesh *editmesh_get_derived(struct EditMesh *em, float (*vertexCos)[3]);
506 DerivedMesh *editmesh_get_derived_base(struct Object *, struct EditMesh *em);
507 DerivedMesh *editmesh_get_derived_cage(struct Scene *scene, struct Object *,
508 struct EditMesh *em, CustomDataMask dataMask);
509 DerivedMesh *editmesh_get_derived_cage_and_final(struct Scene *scene, struct Object *,
510 struct EditMesh *em, DerivedMesh **final_r,
511 CustomDataMask dataMask);
512 float (*editmesh_get_vertex_cos(struct EditMesh *em, int *numVerts_r))[3];
513 int editmesh_modifier_is_enabled(struct Scene *scene, struct ModifierData *md, DerivedMesh *dm);
514 void makeDerivedMesh(struct Scene *scene, struct Object *ob, struct EditMesh *em, CustomDataMask dataMask);
516 /* returns an array of deform matrices for crazyspace correction, and the
517 number of modifiers left */
518 int editmesh_get_first_deform_matrices(struct Scene *, struct Object *, struct EditMesh *em,
519 float (**deformmats)[3][3], float (**deformcos)[3]);
521 /* returns an array of deform matrices for crazyspace correction when sculpting,
522 and the number of modifiers left */
523 int sculpt_get_deform_matrices(struct Scene *scene, struct Object *ob,
524 float (**deformmats)[3][3], float (**deformcos)[3]);
526 void weight_to_rgb(float input, float *fr, float *fg, float *fb);
528 /* convert layers requested by a GLSL material to actually available layers in
529 * the DerivedMesh, with both a pointer for arrays and an offset for editmesh */
530 typedef struct DMVertexAttribs {
532 struct MTFace *array;
533 int emOffset, glIndex, glTexco;
538 int emOffset, glIndex;
543 int emOffset, glIndex;
548 int emOffset, glIndex, glTexco;
551 int tottface, totmcol, tottang, totorco;
554 void DM_vertex_attributes_from_gpu(DerivedMesh *dm,
555 struct GPUVertexAttribs *gattribs, DMVertexAttribs *attribs);
557 void DM_add_tangent_layer(DerivedMesh *dm);
559 /* Set object's bounding box based on DerivedMesh min/max data */
560 void DM_set_object_boundbox(struct Object *ob, DerivedMesh *dm);