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 /* Draw edges in the UV mesh (if exists) */
226 void (*drawUVEdges)(DerivedMesh *dm);
228 /* Draw all edges as lines (no options)
230 * Also called for *final* editmode DerivedMeshes
232 void (*drawEdges)(DerivedMesh *dm, int drawLooseEdges, int drawAllEdges);
234 /* Draw all loose edges (edges w/ no adjoining faces) */
235 void (*drawLooseEdges)(DerivedMesh *dm);
238 * o Set face normal or vertex normal based on inherited face flag
239 * o Use inherited face material index to call setMaterial
240 * o Only if setMaterial returns true
242 * Also called for *final* editmode DerivedMeshes
244 void (*drawFacesSolid)(DerivedMesh *dm, float (*partial_redraw_planes)[4],
245 int fast, int (*setMaterial)(int, void *attribs));
248 * o If useTwoSided, draw front and back using col arrays
249 * o col1,col2 are arrays of length numFace*4 of 4 component colors
250 * in ABGR format, and should be passed as per-face vertex color.
252 void (*drawFacesColored)(DerivedMesh *dm, int useTwoSided,
253 unsigned char *col1, unsigned char *col2);
255 /* Draw all faces using MTFace
256 * o Drawing options too complicated to enumerate, look at code.
258 void (*drawFacesTex)(DerivedMesh *dm,
259 int (*setDrawOptions)(struct MTFace *tface,
260 struct MCol *mcol, int matnr));
262 /* Draw all faces with GLSL materials
263 * o setMaterial is called for every different material nr
264 * o Only if setMaterial returns true
266 void (*drawFacesGLSL)(DerivedMesh *dm,
267 int (*setMaterial)(int, void *attribs));
269 /* Draw mapped faces (no color, or texture)
270 * o Only if !setDrawOptions or
271 * setDrawOptions(userData, mapped-face-index, drawSmooth_r)
274 * If drawSmooth is set to true then vertex normals should be set and
275 * glShadeModel called with GL_SMOOTH. Otherwise the face normal should
276 * be set and glShadeModel called with GL_FLAT.
278 * The setDrawOptions is allowed to not set drawSmooth (for example, when
279 * lighting is disabled), in which case the implementation should draw as
282 void (*drawMappedFaces)(DerivedMesh *dm,
283 int (*setDrawOptions)(void *userData, int index,
285 void *userData, int useColors,
286 int (*setMaterial)(int, void *attribs),
287 int (*compareDrawOptions)(void *userData, int cur_index, int next_index));
289 /* Draw mapped faces using MTFace
290 * o Drawing options too complicated to enumerate, look at code.
292 void (*drawMappedFacesTex)(DerivedMesh *dm,
293 int (*setDrawOptions)(void *userData,
297 /* Draw mapped faces with GLSL materials
298 * o setMaterial is called for every different material nr
299 * o setDrawOptions is called for every face
300 * o Only if setMaterial and setDrawOptions return true
302 void (*drawMappedFacesGLSL)(DerivedMesh *dm,
303 int (*setMaterial)(int, void *attribs),
304 int (*setDrawOptions)(void *userData, int index), void *userData);
306 /* Draw mapped edges as lines
307 * o Only if !setDrawOptions or setDrawOptions(userData, mapped-edge)
310 void (*drawMappedEdges)(DerivedMesh *dm,
311 int (*setDrawOptions)(void *userData, int index),
314 /* Draw mapped edges as lines with interpolation values
315 * o Only if !setDrawOptions or
316 * setDrawOptions(userData, mapped-edge, mapped-v0, mapped-v1, t)
319 * NOTE: This routine is optional!
321 void (*drawMappedEdgesInterp)(DerivedMesh *dm,
322 int (*setDrawOptions)(void *userData,
324 void (*setDrawInterpOptions)(void *userData,
329 /* Release reference to the DerivedMesh. This function decides internally
330 * if the DerivedMesh will be freed, or cached for later use. */
331 void (*release)(DerivedMesh *dm);
334 /* utility function to initialise a DerivedMesh's function pointers to
335 * the default implementation (for those functions which have a default)
337 void DM_init_funcs(DerivedMesh *dm);
339 /* utility function to initialise a DerivedMesh for the desired number
340 * of vertices, edges and faces (doesn't allocate memory for them, just
341 * sets up the custom data layers)
343 void DM_init(DerivedMesh *dm, DerivedMeshType type,
344 int numVerts, int numEdges, int numFaces);
346 /* utility function to initialise a DerivedMesh for the desired number
347 * of vertices, edges and faces, with a layer setup copied from source
349 void DM_from_template(DerivedMesh *dm, DerivedMesh *source,
350 DerivedMeshType type,
351 int numVerts, int numEdges, int numFaces);
353 /* utility function to release a DerivedMesh's layers
354 * returns 1 if DerivedMesh has to be released by the backend, 0 otherwise
356 int DM_release(DerivedMesh *dm);
358 /* utility function to convert a DerivedMesh to a Mesh
360 void DM_to_mesh(DerivedMesh *dm, struct Mesh *me);
362 /* utility function to convert a DerivedMesh to a shape key block
364 void DM_to_meshkey(DerivedMesh *dm, struct Mesh *me, struct KeyBlock *kb);
366 /* set the CD_FLAG_NOCOPY flag in custom data layers where the mask is
367 * zero for the layer type, so only layer types specified by the mask
370 void DM_set_only_copy(DerivedMesh *dm, CustomDataMask mask);
372 /* adds a vertex/edge/face custom data layer to a DerivedMesh, optionally
373 * backed by an external data array
374 * alloctype defines how the layer is allocated or copied, and how it is
375 * freed, see BKE_customdata.h for the different options
377 void DM_add_vert_layer(struct DerivedMesh *dm, int type, int alloctype,
379 void DM_add_edge_layer(struct DerivedMesh *dm, int type, int alloctype,
381 void DM_add_face_layer(struct DerivedMesh *dm, int type, int alloctype,
384 /* custom data access functions
385 * return pointer to data from first layer which matches type
386 * if they return NULL for valid indices, data doesn't exist
387 * note these return pointers - any change modifies the internals of the mesh
389 void *DM_get_vert_data(struct DerivedMesh *dm, int index, int type);
390 void *DM_get_edge_data(struct DerivedMesh *dm, int index, int type);
391 void *DM_get_face_data(struct DerivedMesh *dm, int index, int type);
393 /* custom data layer access functions
394 * return pointer to first data layer which matches type (a flat array)
395 * if they return NULL, data doesn't exist
396 * note these return pointers - any change modifies the internals of the mesh
398 void *DM_get_vert_data_layer(struct DerivedMesh *dm, int type);
399 void *DM_get_edge_data_layer(struct DerivedMesh *dm, int type);
400 void *DM_get_face_data_layer(struct DerivedMesh *dm, int type);
402 /* custom data setting functions
403 * copy supplied data into first layer of type using layer's copy function
404 * (deep copy if appropriate)
406 void DM_set_vert_data(struct DerivedMesh *dm, int index, int type, void *data);
407 void DM_set_edge_data(struct DerivedMesh *dm, int index, int type, void *data);
408 void DM_set_face_data(struct DerivedMesh *dm, int index, int type, void *data);
410 /* custom data copy functions
411 * copy count elements from source_index in source to dest_index in dest
412 * these copy all layers for which the CD_FLAG_NOCOPY flag is not set
414 void DM_copy_vert_data(struct DerivedMesh *source, struct DerivedMesh *dest,
415 int source_index, int dest_index, int count);
416 void DM_copy_edge_data(struct DerivedMesh *source, struct DerivedMesh *dest,
417 int source_index, int dest_index, int count);
418 void DM_copy_face_data(struct DerivedMesh *source, struct DerivedMesh *dest,
419 int source_index, int dest_index, int count);
421 /* custom data free functions
422 * free count elements, starting at index
423 * they free all layers for which the CD_FLAG_NOCOPY flag is not set
425 void DM_free_vert_data(struct DerivedMesh *dm, int index, int count);
426 void DM_free_edge_data(struct DerivedMesh *dm, int index, int count);
427 void DM_free_face_data(struct DerivedMesh *dm, int index, int count);
429 /* interpolates vertex data from the vertices indexed by src_indices in the
430 * source mesh using the given weights and stores the result in the vertex
431 * indexed by dest_index in the dest mesh
433 void DM_interp_vert_data(struct DerivedMesh *source, struct DerivedMesh *dest,
434 int *src_indices, float *weights,
435 int count, int dest_index);
437 /* interpolates edge data from the edges indexed by src_indices in the
438 * source mesh using the given weights and stores the result in the edge indexed
439 * by dest_index in the dest mesh.
440 * if weights is NULL, all weights default to 1.
441 * if vert_weights is non-NULL, any per-vertex edge data is interpolated using
442 * vert_weights[i] multiplied by weights[i].
444 typedef float EdgeVertWeight[SUB_ELEMS_EDGE][SUB_ELEMS_EDGE];
445 void DM_interp_edge_data(struct DerivedMesh *source, struct DerivedMesh *dest,
447 float *weights, EdgeVertWeight *vert_weights,
448 int count, int dest_index);
450 /* interpolates face data from the faces indexed by src_indices in the
451 * source mesh using the given weights and stores the result in the face indexed
452 * by dest_index in the dest mesh.
453 * if weights is NULL, all weights default to 1.
454 * if vert_weights is non-NULL, any per-vertex face data is interpolated using
455 * vert_weights[i] multiplied by weights[i].
457 typedef float FaceVertWeight[SUB_ELEMS_FACE][SUB_ELEMS_FACE];
458 void DM_interp_face_data(struct DerivedMesh *source, struct DerivedMesh *dest,
460 float *weights, FaceVertWeight *vert_weights,
461 int count, int dest_index);
463 void DM_swap_face_data(struct DerivedMesh *dm, int index, const int *corner_indices);
465 /* Temporary? A function to give a colorband to derivedmesh for vertexcolor ranges */
466 void vDM_ColorBand_store(struct ColorBand *coba);
468 /* Simple function to get me->totvert amount of vertices/normals,
469 correctly deformed and subsurfered. Needed especially when vertexgroups are involved.
470 In use now by vertex/weigt paint and particles */
471 float *mesh_get_mapped_verts_nors(struct Scene *scene, struct Object *ob);
474 DerivedMesh *mesh_get_derived_final(struct Scene *scene, struct Object *ob,
475 CustomDataMask dataMask);
476 DerivedMesh *mesh_get_derived_deform(struct Scene *scene, struct Object *ob,
477 CustomDataMask dataMask);
479 DerivedMesh *mesh_create_derived_for_modifier(struct Scene *scene, struct Object *ob, struct ModifierData *md);
481 DerivedMesh *mesh_create_derived_render(struct Scene *scene, struct Object *ob,
482 CustomDataMask dataMask);
484 DerivedMesh *mesh_create_derived_index_render(struct Scene *scene, struct Object *ob, CustomDataMask dataMask, int index);
486 /* same as above but wont use render settings */
487 DerivedMesh *mesh_create_derived(struct Mesh *me, struct Object *ob, float (*vertCos)[3]);
488 DerivedMesh *mesh_create_derived_view(struct Scene *scene, struct Object *ob,
489 CustomDataMask dataMask);
490 DerivedMesh *mesh_create_derived_no_deform(struct Scene *scene, struct Object *ob,
492 CustomDataMask dataMask);
493 DerivedMesh *mesh_create_derived_no_deform_render(struct Scene *scene, struct Object *ob,
495 CustomDataMask dataMask);
497 DerivedMesh *mesh_create_derived_no_virtual(struct Scene *scene, struct Object *ob, float (*vertCos)[3],
498 CustomDataMask dataMask);
499 DerivedMesh *mesh_create_derived_physics(struct Scene *scene, struct Object *ob, float (*vertCos)[3],
500 CustomDataMask dataMask);
502 DerivedMesh *editmesh_get_derived(struct EditMesh *em, float (*vertexCos)[3]);
503 DerivedMesh *editmesh_get_derived_base(struct Object *, struct EditMesh *em);
504 DerivedMesh *editmesh_get_derived_cage(struct Scene *scene, struct Object *,
505 struct EditMesh *em, CustomDataMask dataMask);
506 DerivedMesh *editmesh_get_derived_cage_and_final(struct Scene *scene, struct Object *,
507 struct EditMesh *em, DerivedMesh **final_r,
508 CustomDataMask dataMask);
509 float (*editmesh_get_vertex_cos(struct EditMesh *em, int *numVerts_r))[3];
510 int editmesh_modifier_is_enabled(struct Scene *scene, struct ModifierData *md, DerivedMesh *dm);
511 void makeDerivedMesh(struct Scene *scene, struct Object *ob, struct EditMesh *em, CustomDataMask dataMask);
513 /* returns an array of deform matrices for crazyspace correction, and the
514 number of modifiers left */
515 int editmesh_get_first_deform_matrices(struct Scene *, struct Object *, struct EditMesh *em,
516 float (**deformmats)[3][3], float (**deformcos)[3]);
518 /* returns an array of deform matrices for crazyspace correction when sculpting,
519 and the number of modifiers left */
520 int sculpt_get_deform_matrices(struct Scene *scene, struct Object *ob,
521 float (**deformmats)[3][3], float (**deformcos)[3]);
523 void weight_to_rgb(float input, float *fr, float *fg, float *fb);
525 /* convert layers requested by a GLSL material to actually available layers in
526 * the DerivedMesh, with both a pointer for arrays and an offset for editmesh */
527 typedef struct DMVertexAttribs {
529 struct MTFace *array;
530 int emOffset, glIndex, glTexco;
535 int emOffset, glIndex;
540 int emOffset, glIndex;
545 int emOffset, glIndex, glTexco;
548 int tottface, totmcol, tottang, totorco;
551 void DM_vertex_attributes_from_gpu(DerivedMesh *dm,
552 struct GPUVertexAttribs *gattribs, DMVertexAttribs *attribs);
554 void DM_add_tangent_layer(DerivedMesh *dm);
556 /* Set object's bounding box based on DerivedMesh min/max data */
557 void DM_set_object_boundbox(struct Object *ob, DerivedMesh *dm);