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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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
35 * o Make drawMapped* functions take a predicate function that
36 * determines whether to draw the edge (this predicate can
37 * also set color, etc). This will be slightly more general
38 * and allow some of the functions to be collapsed.
39 * o Once accessor functions are added then single element draw
40 * functions can be implemented using primitive accessors.
41 * o Add function to dispatch to renderer instead of using
45 #include "DNA_customdata_types.h"
46 #include "BKE_customdata.h"
47 #include "BKE_bvhutils.h"
60 struct GPUVertexAttribs;
65 /* number of sub-elements each mesh element has (for interpolation) */
66 #define SUB_ELEMS_VERT 0
67 #define SUB_ELEMS_EDGE 2
68 #define SUB_ELEMS_FACE 4
70 typedef struct DMGridData {
75 typedef struct DMGridAdjacency {
80 typedef struct DerivedMesh DerivedMesh;
82 /* Private DerivedMesh data, only for internal DerivedMesh use */
83 CustomData vertData, edgeData, faceData;
84 int numVertData, numEdgeData, numFaceData;
85 int needsFree; /* checked on ->release, is set to 0 for cached results */
86 int deformedOnly; /* set by modifier stack if only deformed from original */
89 struct GPUDrawObject *drawObject;
93 /* Also called in Editmode */
94 int (*getNumVerts)(DerivedMesh *dm);
95 /* Also called in Editmode */
96 int (*getNumFaces)(DerivedMesh *dm);
98 int (*getNumEdges)(DerivedMesh *dm);
100 /* copy a single vert/edge/face from the derived mesh into
101 * *{vert/edge/face}_r. note that the current implementation
102 * of this function can be quite slow, iterating over all
103 * elements (editmesh)
105 void (*getVert)(DerivedMesh *dm, int index, struct MVert *vert_r);
106 void (*getEdge)(DerivedMesh *dm, int index, struct MEdge *edge_r);
107 void (*getFace)(DerivedMesh *dm, int index, struct MFace *face_r);
109 /* return a pointer to the entire array of verts/edges/face from the
110 * derived mesh. if such an array does not exist yet, it will be created,
111 * and freed on the next ->release(). consider using getVert/Edge/Face if
112 * you are only interested in a few verts/edges/faces.
114 struct MVert *(*getVertArray)(DerivedMesh *dm);
115 struct MEdge *(*getEdgeArray)(DerivedMesh *dm);
116 struct MFace *(*getFaceArray)(DerivedMesh *dm);
118 /* copy all verts/edges/faces from the derived mesh into
119 * *{vert/edge/face}_r (must point to a buffer large enough)
121 void (*copyVertArray)(DerivedMesh *dm, struct MVert *vert_r);
122 void (*copyEdgeArray)(DerivedMesh *dm, struct MEdge *edge_r);
123 void (*copyFaceArray)(DerivedMesh *dm, struct MFace *face_r);
125 /* return a copy of all verts/edges/faces from the derived mesh
126 * it is the caller's responsibility to free the returned pointer
128 struct MVert *(*dupVertArray)(DerivedMesh *dm);
129 struct MEdge *(*dupEdgeArray)(DerivedMesh *dm);
130 struct MFace *(*dupFaceArray)(DerivedMesh *dm);
132 /* return a pointer to a single element of vert/edge/face custom data
133 * from the derived mesh (this gives a pointer to the actual data, not
136 void *(*getVertData)(DerivedMesh *dm, int index, int type);
137 void *(*getEdgeData)(DerivedMesh *dm, int index, int type);
138 void *(*getFaceData)(DerivedMesh *dm, int index, int type);
140 /* return a pointer to the entire array of vert/edge/face custom data
141 * from the derived mesh (this gives a pointer to the actual data, not
144 void *(*getVertDataArray)(DerivedMesh *dm, int type);
145 void *(*getEdgeDataArray)(DerivedMesh *dm, int type);
146 void *(*getFaceDataArray)(DerivedMesh *dm, int type);
148 /* optional grid access for subsurf */
149 int (*getNumGrids)(DerivedMesh *dm);
150 int (*getGridSize)(DerivedMesh *dm);
151 DMGridData **(*getGridData)(DerivedMesh *dm);
152 DMGridAdjacency *(*getGridAdjacency)(DerivedMesh *dm);
154 /* Iterate over each mapped vertex in the derived mesh, calling the
155 * given function with the original vert and the mapped vert's new
156 * coordinate and normal. For historical reasons the normal can be
157 * passed as a float or short array, only one should be non-NULL.
159 void (*foreachMappedVert)(
161 void (*func)(void *userData, int index, float *co,
162 float *no_f, short *no_s),
165 /* Iterate over each mapped edge in the derived mesh, calling the
166 * given function with the original edge and the mapped edge's new
169 void (*foreachMappedEdge)(DerivedMesh *dm,
170 void (*func)(void *userData, int index,
171 float *v0co, float *v1co),
174 /* Iterate over each mapped face in the derived mesh, calling the
175 * given function with the original face and the mapped face's (or
176 * faces') center and normal.
178 void (*foreachMappedFaceCenter)(DerivedMesh *dm,
179 void (*func)(void *userData, int index,
180 float *cent, float *no),
183 /* Iterate over all vertex points, calling DO_MINMAX with given args.
185 * Also called in Editmode
187 void (*getMinMax)(DerivedMesh *dm, float min_r[3], float max_r[3]);
189 /* Direct Access Operations */
190 /* o Can be undefined */
191 /* o Must be defined for modifiers that only deform however */
193 /* Get vertex location, undefined if index is not valid */
194 void (*getVertCo)(DerivedMesh *dm, int index, float co_r[3]);
196 /* Fill the array (of length .getNumVerts()) with all vertex locations */
197 void (*getVertCos)(DerivedMesh *dm, float (*cos_r)[3]);
199 /* Get vertex normal, undefined if index is not valid */
200 void (*getVertNo)(DerivedMesh *dm, int index, float no_r[3]);
202 /* Get a map of vertices to faces
204 struct ListBase *(*getFaceMap)(DerivedMesh *dm);
206 /* Get the BVH used for paint modes
208 struct PBVH *(*getPBVH)(struct Object *ob, DerivedMesh *dm);
210 /* Drawing Operations */
212 /* Draw all vertices as bgl points (no options) */
213 void (*drawVerts)(DerivedMesh *dm);
215 /* Draw edges in the UV mesh (if exists) */
216 void (*drawUVEdges)(DerivedMesh *dm);
218 /* Draw all edges as lines (no options)
220 * Also called for *final* editmode DerivedMeshes
222 void (*drawEdges)(DerivedMesh *dm, int drawLooseEdges);
224 /* Draw all loose edges (edges w/ no adjoining faces) */
225 void (*drawLooseEdges)(DerivedMesh *dm);
228 * o Set face normal or vertex normal based on inherited face flag
229 * o Use inherited face material index to call setMaterial
230 * o Only if setMaterial returns true
232 * Also called for *final* editmode DerivedMeshes
234 void (*drawFacesSolid)(DerivedMesh *dm, float (*partial_redraw_planes)[4],
235 int fast, int (*setMaterial)(int, void *attribs));
238 * o If useTwoSided, draw front and back using col arrays
239 * o col1,col2 are arrays of length numFace*4 of 4 component colors
240 * in ABGR format, and should be passed as per-face vertex color.
242 void (*drawFacesColored)(DerivedMesh *dm, int useTwoSided,
243 unsigned char *col1, unsigned char *col2);
245 /* Draw all faces using MTFace
246 * o Drawing options too complicated to enumerate, look at code.
248 void (*drawFacesTex)(DerivedMesh *dm,
249 int (*setDrawOptions)(struct MTFace *tface,
250 struct MCol *mcol, int matnr));
252 /* Draw all faces with GLSL materials
253 * o setMaterial is called for every different material nr
254 * o Only if setMaterial returns true
256 void (*drawFacesGLSL)(DerivedMesh *dm,
257 int (*setMaterial)(int, void *attribs));
259 /* Draw mapped faces (no color, or texture)
260 * o Only if !setDrawOptions or
261 * setDrawOptions(userData, mapped-face-index, drawSmooth_r)
264 * If drawSmooth is set to true then vertex normals should be set and
265 * glShadeModel called with GL_SMOOTH. Otherwise the face normal should
266 * be set and glShadeModel called with GL_FLAT.
268 * The setDrawOptions is allowed to not set drawSmooth (for example, when
269 * lighting is disabled), in which case the implementation should draw as
272 void (*drawMappedFaces)(DerivedMesh *dm,
273 int (*setDrawOptions)(void *userData, int index,
275 void *userData, int useColors);
277 /* Draw mapped faces using MTFace
278 * o Drawing options too complicated to enumerate, look at code.
280 void (*drawMappedFacesTex)(DerivedMesh *dm,
281 int (*setDrawOptions)(void *userData,
285 /* Draw mapped faces with GLSL materials
286 * o setMaterial is called for every different material nr
287 * o setDrawOptions is called for every face
288 * o Only if setMaterial and setDrawOptions return true
290 void (*drawMappedFacesGLSL)(DerivedMesh *dm,
291 int (*setMaterial)(int, void *attribs),
292 int (*setDrawOptions)(void *userData, int index), void *userData);
294 /* Draw mapped edges as lines
295 * o Only if !setDrawOptions or setDrawOptions(userData, mapped-edge)
298 void (*drawMappedEdges)(DerivedMesh *dm,
299 int (*setDrawOptions)(void *userData, int index),
302 /* Draw mapped edges as lines with interpolation values
303 * o Only if !setDrawOptions or
304 * setDrawOptions(userData, mapped-edge, mapped-v0, mapped-v1, t)
307 * NOTE: This routine is optional!
309 void (*drawMappedEdgesInterp)(DerivedMesh *dm,
310 int (*setDrawOptions)(void *userData,
312 void (*setDrawInterpOptions)(void *userData,
317 /* Release reference to the DerivedMesh. This function decides internally
318 * if the DerivedMesh will be freed, or cached for later use. */
319 void (*release)(DerivedMesh *dm);
322 /* utility function to initialise a DerivedMesh's function pointers to
323 * the default implementation (for those functions which have a default)
325 void DM_init_funcs(DerivedMesh *dm);
327 /* utility function to initialise a DerivedMesh for the desired number
328 * of vertices, edges and faces (doesn't allocate memory for them, just
329 * sets up the custom data layers)
331 void DM_init(DerivedMesh *dm, int numVerts, int numEdges, int numFaces);
333 /* utility function to initialise a DerivedMesh for the desired number
334 * of vertices, edges and faces, with a layer setup copied from source
336 void DM_from_template(DerivedMesh *dm, DerivedMesh *source,
337 int numVerts, int numEdges, int numFaces);
339 /* utility function to release a DerivedMesh's layers
340 * returns 1 if DerivedMesh has to be released by the backend, 0 otherwise
342 int DM_release(DerivedMesh *dm);
344 /* utility function to convert a DerivedMesh to a Mesh
346 void DM_to_mesh(DerivedMesh *dm, struct Mesh *me);
348 /* set the CD_FLAG_NOCOPY flag in custom data layers where the mask is
349 * zero for the layer type, so only layer types specified by the mask
352 void DM_set_only_copy(DerivedMesh *dm, CustomDataMask mask);
354 /* adds a vertex/edge/face custom data layer to a DerivedMesh, optionally
355 * backed by an external data array
356 * alloctype defines how the layer is allocated or copied, and how it is
357 * freed, see BKE_customdata.h for the different options
359 void DM_add_vert_layer(struct DerivedMesh *dm, int type, int alloctype,
361 void DM_add_edge_layer(struct DerivedMesh *dm, int type, int alloctype,
363 void DM_add_face_layer(struct DerivedMesh *dm, int type, int alloctype,
366 /* custom data access functions
367 * return pointer to data from first layer which matches type
368 * if they return NULL for valid indices, data doesn't exist
369 * note these return pointers - any change modifies the internals of the mesh
371 void *DM_get_vert_data(struct DerivedMesh *dm, int index, int type);
372 void *DM_get_edge_data(struct DerivedMesh *dm, int index, int type);
373 void *DM_get_face_data(struct DerivedMesh *dm, int index, int type);
375 /* custom data layer access functions
376 * return pointer to first data layer which matches type (a flat array)
377 * if they return NULL, data doesn't exist
378 * note these return pointers - any change modifies the internals of the mesh
380 void *DM_get_vert_data_layer(struct DerivedMesh *dm, int type);
381 void *DM_get_edge_data_layer(struct DerivedMesh *dm, int type);
382 void *DM_get_face_data_layer(struct DerivedMesh *dm, int type);
384 /* custom data setting functions
385 * copy supplied data into first layer of type using layer's copy function
386 * (deep copy if appropriate)
388 void DM_set_vert_data(struct DerivedMesh *dm, int index, int type, void *data);
389 void DM_set_edge_data(struct DerivedMesh *dm, int index, int type, void *data);
390 void DM_set_face_data(struct DerivedMesh *dm, int index, int type, void *data);
392 /* custom data copy functions
393 * copy count elements from source_index in source to dest_index in dest
394 * these copy all layers for which the CD_FLAG_NOCOPY flag is not set
396 void DM_copy_vert_data(struct DerivedMesh *source, struct DerivedMesh *dest,
397 int source_index, int dest_index, int count);
398 void DM_copy_edge_data(struct DerivedMesh *source, struct DerivedMesh *dest,
399 int source_index, int dest_index, int count);
400 void DM_copy_face_data(struct DerivedMesh *source, struct DerivedMesh *dest,
401 int source_index, int dest_index, int count);
403 /* custom data free functions
404 * free count elements, starting at index
405 * they free all layers for which the CD_FLAG_NOCOPY flag is not set
407 void DM_free_vert_data(struct DerivedMesh *dm, int index, int count);
408 void DM_free_edge_data(struct DerivedMesh *dm, int index, int count);
409 void DM_free_face_data(struct DerivedMesh *dm, int index, int count);
411 /* interpolates vertex data from the vertices indexed by src_indices in the
412 * source mesh using the given weights and stores the result in the vertex
413 * indexed by dest_index in the dest mesh
415 void DM_interp_vert_data(struct DerivedMesh *source, struct DerivedMesh *dest,
416 int *src_indices, float *weights,
417 int count, int dest_index);
419 /* interpolates edge data from the edges indexed by src_indices in the
420 * source mesh using the given weights and stores the result in the edge indexed
421 * by dest_index in the dest mesh.
422 * if weights is NULL, all weights default to 1.
423 * if vert_weights is non-NULL, any per-vertex edge data is interpolated using
424 * vert_weights[i] multiplied by weights[i].
426 typedef float EdgeVertWeight[SUB_ELEMS_EDGE][SUB_ELEMS_EDGE];
427 void DM_interp_edge_data(struct DerivedMesh *source, struct DerivedMesh *dest,
429 float *weights, EdgeVertWeight *vert_weights,
430 int count, int dest_index);
432 /* interpolates face data from the faces indexed by src_indices in the
433 * source mesh using the given weights and stores the result in the face indexed
434 * by dest_index in the dest mesh.
435 * if weights is NULL, all weights default to 1.
436 * if vert_weights is non-NULL, any per-vertex face data is interpolated using
437 * vert_weights[i] multiplied by weights[i].
439 typedef float FaceVertWeight[SUB_ELEMS_FACE][SUB_ELEMS_FACE];
440 void DM_interp_face_data(struct DerivedMesh *source, struct DerivedMesh *dest,
442 float *weights, FaceVertWeight *vert_weights,
443 int count, int dest_index);
445 void DM_swap_face_data(struct DerivedMesh *dm, int index, int *corner_indices);
447 /* Temporary? A function to give a colorband to derivedmesh for vertexcolor ranges */
448 void vDM_ColorBand_store(struct ColorBand *coba);
450 /* Simple function to get me->totvert amount of vertices/normals,
451 correctly deformed and subsurfered. Needed especially when vertexgroups are involved.
452 In use now by vertex/weigt paint and particles */
453 float *mesh_get_mapped_verts_nors(struct Scene *scene, struct Object *ob);
456 DerivedMesh *mesh_get_derived_final(struct Scene *scene, struct Object *ob,
457 CustomDataMask dataMask);
458 DerivedMesh *mesh_get_derived_deform(struct Scene *scene, struct Object *ob,
459 CustomDataMask dataMask);
461 DerivedMesh *mesh_create_derived_for_modifier(struct Scene *scene, struct Object *ob, struct ModifierData *md);
463 DerivedMesh *mesh_create_derived_render(struct Scene *scene, struct Object *ob,
464 CustomDataMask dataMask);
466 DerivedMesh *mesh_create_derived_index_render(struct Scene *scene, struct Object *ob, CustomDataMask dataMask, int index);
468 /* same as above but wont use render settings */
469 DerivedMesh *mesh_create_derived_view(struct Scene *scene, struct Object *ob,
470 CustomDataMask dataMask);
471 DerivedMesh *mesh_create_derived_no_deform(struct Scene *scene, struct Object *ob,
473 CustomDataMask dataMask);
474 DerivedMesh *mesh_create_derived_no_deform_render(struct Scene *scene, struct Object *ob,
476 CustomDataMask dataMask);
478 DerivedMesh *mesh_create_derived_no_virtual(struct Scene *scene, struct Object *ob, float (*vertCos)[3],
479 CustomDataMask dataMask);
481 DerivedMesh *editmesh_get_derived_base(struct Object *, struct EditMesh *em);
482 DerivedMesh *editmesh_get_derived_cage(struct Scene *scene, struct Object *,
483 struct EditMesh *em, CustomDataMask dataMask);
484 DerivedMesh *editmesh_get_derived_cage_and_final(struct Scene *scene, struct Object *,
485 struct EditMesh *em, DerivedMesh **final_r,
486 CustomDataMask dataMask);
487 void makeDerivedMesh(struct Scene *scene, struct Object *ob, struct EditMesh *em, CustomDataMask dataMask);
489 /* returns an array of deform matrices for crazyspace correction, and the
490 number of modifiers left */
491 int editmesh_get_first_deform_matrices(struct Object *, struct EditMesh *em, float (**deformmats)[3][3],
492 float (**deformcos)[3]);
494 void weight_to_rgb(float input, float *fr, float *fg, float *fb);
496 /* convert layers requested by a GLSL material to actually available layers in
497 * the DerivedMesh, with both a pointer for arrays and an offset for editmesh */
498 typedef struct DMVertexAttribs {
500 struct MTFace *array;
501 int emOffset, glIndex;
506 int emOffset, glIndex;
511 int emOffset, glIndex;
516 int emOffset, glIndex;
519 int tottface, totmcol, tottang, totorco;
522 void DM_vertex_attributes_from_gpu(DerivedMesh *dm,
523 struct GPUVertexAttribs *gattribs, DMVertexAttribs *attribs);
525 void DM_add_tangent_layer(DerivedMesh *dm);