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) 2007 by Nicholas Bishop
21 * All rights reserved.
23 * The Original Code is: all of this file.
25 * Contributor(s): none yet.
27 * ***** END GPL LICENSE BLOCK *****
30 #include "MEM_guardedalloc.h"
32 #include "DNA_key_types.h"
33 #include "DNA_mesh_types.h"
34 #include "DNA_meshdata_types.h"
35 #include "DNA_modifier_types.h"
36 #include "DNA_object_types.h"
37 #include "DNA_scene_types.h"
38 #include "DNA_view3d_types.h"
40 #include "BLI_arithb.h"
41 #include "BLI_blenlib.h"
43 #include "BKE_cdderivedmesh.h"
44 #include "BKE_customdata.h"
45 #include "BKE_depsgraph.h"
46 #include "BKE_DerivedMesh.h"
47 #include "BKE_global.h"
49 #include "BKE_modifier.h"
50 #include "BKE_multires.h"
51 #include "BKE_object.h"
52 #include "BKE_subsurf.h"
57 /* MULTIRES MODIFIER */
58 static const int multires_max_levels = 13;
59 static const int multires_quad_tot[] = {4, 9, 25, 81, 289, 1089, 4225, 16641, 66049, 263169, 1050625, 4198401, 16785409};
60 static const int multires_side_tot[] = {2, 3, 5, 9, 17, 33, 65, 129, 257, 513, 1025, 2049, 4097};
62 MultiresModifierData *find_multires_modifier(Object *ob)
65 MultiresModifierData *mmd = NULL;
67 for(md = ob->modifiers.first; md; md = md->next) {
68 if(md->type == eModifierType_Multires) {
69 mmd = (MultiresModifierData*)md;
78 int multiresModifier_switch_level(Object *ob, const int distance)
80 MultiresModifierData *mmd = find_multires_modifier(ob);
84 if(mmd->lvl < 1) mmd->lvl = 1;
85 else if(mmd->lvl > mmd->totlvl) mmd->lvl = mmd->totlvl;
86 /* XXX: DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);
87 object_handle_update(ob);*/
96 void multiresModifier_join(Object *ob)
101 /* First find the highest level of subdivision */
104 if(TESTBASELIB_BGMODE(v3d, base) && base->object->type==OB_MESH) {
106 for(md = base->object->modifiers.first; md; md = md->next) {
107 if(md->type == eModifierType_Multires) {
108 int totlvl = ((MultiresModifierData*)md)->totlvl;
109 if(totlvl > highest_lvl)
110 highest_lvl = totlvl;
112 /* Ensure that all updates are processed */
113 multires_force_update(base->object);
120 /* No multires meshes selected */
124 /* Subdivide all the displacements to the highest level */
127 if(TESTBASELIB_BGMODE(v3d, base) && base->object->type==OB_MESH) {
128 ModifierData *md = NULL;
129 MultiresModifierData *mmd = NULL;
131 for(md = base->object->modifiers.first; md; md = md->next) {
132 if(md->type == eModifierType_Multires)
133 mmd = (MultiresModifierData*)md;
136 /* If the object didn't have multires enabled, give it a new modifier */
138 md = base->object->modifiers.first;
140 while(md && modifierType_getInfo(md->type)->type == eModifierTypeType_OnlyDeform)
143 mmd = (MultiresModifierData*)modifier_new(eModifierType_Multires);
144 BLI_insertlinkbefore(&base->object->modifiers, md, mmd);
148 multiresModifier_subdivide(mmd, base->object, highest_lvl - mmd->totlvl, 0, 0);
155 /* Returns 0 on success, 1 if the src's totvert doesn't match */
156 int multiresModifier_reshape(MultiresModifierData *mmd, Object *dst, Object *src)
158 Mesh *src_me = get_mesh(src);
159 DerivedMesh *mrdm = dst->derivedFinal;
161 if(mrdm && mrdm->getNumVerts(mrdm) == src_me->totvert) {
162 MVert *mvert = CDDM_get_verts(mrdm);
165 for(i = 0; i < src_me->totvert; ++i)
166 VecCopyf(mvert[i].co, src_me->mvert[i].co);
168 MultiresDM_mark_as_modified(mrdm);
170 dst->derivedFinal = NULL;
178 static void Mat3FromColVecs(float mat[][3], float v1[3], float v2[3], float v3[3])
180 VecCopyf(mat[0], v1);
181 VecCopyf(mat[1], v2);
182 VecCopyf(mat[2], v3);
185 static DerivedMesh *multires_subdisp_pre(DerivedMesh *mrdm, int distance, int simple)
188 SubsurfModifierData smd;
190 memset(&smd, 0, sizeof(SubsurfModifierData));
191 smd.levels = distance;
193 smd.subdivType = ME_SIMPLE_SUBSURF;
195 final = subsurf_make_derived_from_derived_with_multires(mrdm, &smd, NULL, 0, NULL, 0, 0);
200 static void VecAddUf(float a[3], float b[3])
207 static void multires_subdisp(DerivedMesh *orig, Mesh *me, DerivedMesh *final, int lvl, int totlvl,
208 int totsubvert, int totsubedge, int totsubface, int addverts)
211 MultiresModifierData mmd_sub;
212 MVert *mvs = CDDM_get_verts(final);
213 MVert *mvd, *mvd_f1, *mvs_f1, *mvd_f3, *mvd_f4;
214 MVert *mvd_f2, *mvs_f2, *mvs_e1, *mvd_e1, *mvs_e2;
216 int slo1 = multires_side_tot[lvl - 1];
218 int slo2 = multires_side_tot[totlvl - 2];
219 int shi2 = multires_side_tot[totlvl - 1];
220 int skip = multires_side_tot[totlvl - lvl] - 1;
223 mmd_sub.lvl = mmd_sub.totlvl = totlvl;
224 mrdm = multires_dm_create_from_derived(&mmd_sub, orig, me, 0, 0);
226 mvd = CDDM_get_verts(mrdm);
227 /* Need to map from ccg to mrdm */
228 totvert = mrdm->getNumVerts(mrdm);
231 for(i = 0; i < totvert; ++i) {
232 float z[3] = {0,0,0};
233 VecCopyf(mvd[i].co, z);
237 /* Load base verts */
238 for(i = 0; i < me->totvert; ++i)
239 VecAddUf(mvd[totvert - me->totvert + i].co, mvs[totvert - me->totvert + i].co);
244 mvs_f2 = mvs + totvert - totsubvert;
245 mvs_e1 = mvs + totsubface * (skip-1) * (skip-1);
247 for(i = 0; i < me->totface; ++i) {
248 const int end = me->mface[i].v4 ? 4 : 3;
249 int x, y, x2, y2, mov= 0;
251 mvd_f1 += 1 + end * (slo2-2); //center+edgecross
252 mvd_f3 = mvd_f4 = mvd_f1;
254 for(j = 0; j < end; ++j) {
255 mvd_f1 += (skip/2 - 1) * (slo2 - 2) + (skip/2 - 1);
256 /* Update sub faces */
257 for(y = 0; y < sll; ++y) {
258 for(x = 0; x < sll; ++x) {
260 VecAddUf(mvd_f1->co, mvs_f1->co);
263 /* Now we hold the center of the subface at mvd_f1
264 and offset it to the edge cross and face verts */
267 for(k = 0; k < 4; ++k) {
269 else if(k == 1) mov = slo2 - 2;
270 else if(k == 2) mov = 1;
271 else if(k == 3) mov = -(slo2 - 2);
273 for(x2 = 1; x2 < skip/2; ++x2) {
274 VecAddUf((mvd_f1 + mov * x2)->co, mvs_f1->co);
279 /* Main face verts */
280 for(k = 0; k < 4; ++k) {
281 int movx= 0, movy= 0;
283 if(k == 0) { movx = -1; movy = -(slo2 - 2); }
284 else if(k == 1) { movx = slo2 - 2; movy = -1; }
285 else if(k == 2) { movx = 1; movy = slo2 - 2; }
286 else if(k == 3) { movx = -(slo2 - 2); movy = 1; }
288 for(y2 = 1; y2 < skip/2; ++y2) {
289 for(x2 = 1; x2 < skip/2; ++x2) {
290 VecAddUf((mvd_f1 + movy * y2 + movx * x2)->co, mvs_f1->co);
298 mvd_f1 += (skip - 1) * (slo2 - 2) - 1;
300 mvd_f1 -= (skip - 1) * (slo2 - 2) - 1 + skip;
301 mvd_f1 += (slo2 - 2) * (skip/2-1) + skip/2-1 + 1;
304 /* update face center verts */
305 VecAddUf(mvd_f2->co, mvs_f2->co);
310 /* update face edge verts */
311 for(j = 0; j < end; ++j) {
314 /* Super-face edge cross */
315 for(k = 0; k < skip-1; ++k) {
316 VecAddUf(mvd_f2->co, mvs_e1->co);
320 for(x = 1; x < sll; ++x) {
321 VecAddUf(mvd_f2->co, mvs_f2->co);
325 for(k = 0; k < skip-1; ++k) {
326 VecAddUf(mvd_f2->co, mvs_e1->co);
333 for(y = 0; y < sll - 1; ++y) {
334 for(x = 0; x < sll; ++x) {
335 for(k = 0; k < skip - 1; ++k) {
336 VecAddUf(mvd_f3[(skip-1)+(y*skip) + (x*skip+k)*(slo2-2)].co,
344 mvs_e1 = restore + skip - 1;
345 for(y = 0; y < sll - 1; ++y) {
346 for(x = 0; x < sll; ++x) {
347 for(k = 0; k < skip - 1; ++k) {
348 VecAddUf(mvd_f3[(slo2-2)*(skip-1)+(x*skip)+k + y*skip*(slo2-2)].co,
356 mvd_f3 += (slo2-2)*(slo2-2);
360 /* update base (2) face verts */
361 for(j = 0; j < end; ++j) {
362 mvd_f2 += (slo2 - 1) * (skip - 1);
363 for(y = 0; y < sll - 1; ++y) {
364 for(x = 0; x < sll - 1; ++x) {
365 VecAddUf(mvd_f2->co, mvs_f2->co);
369 mvd_f2 += (slo2 - 1) * (skip - 1);
371 mvd_f2 -= (skip - 1);
376 mvd_e1 = mvd + totvert - me->totvert - me->totedge * (shi2-2);
377 mvs_e2 = mvs + totvert - me->totvert - me->totedge * (slo1-2);
378 for(i = 0; i < me->totedge; ++i) {
379 for(j = 0; j < skip - 1; ++j) {
380 VecAddUf(mvd_e1->co, mvs_e1->co);
384 for(j = 0; j < slo1 - 2; j++) {
385 VecAddUf(mvd_e1->co, mvs_e2->co);
389 for(k = 0; k < skip - 1; ++k) {
390 VecAddUf(mvd_e1->co, mvs_e1->co);
397 final->needsFree = 1;
398 final->release(final);
400 MultiresDM_mark_as_modified(mrdm);
404 /* direction=1 for delete higher, direction=0 for lower (not implemented yet) */
405 void multiresModifier_del_levels(struct MultiresModifierData *mmd, struct Object *ob, int direction)
407 Mesh *me = get_mesh(ob);
408 int distance = mmd->totlvl - mmd->lvl;
409 MDisps *mdisps = CustomData_get_layer(&me->fdata, CD_MDISPS);
411 multires_force_update(ob);
413 if(mdisps && distance > 0 && direction == 1) {
414 int skip = multires_side_tot[distance] - 1;
415 int st = multires_side_tot[mmd->totlvl - 1];
416 int totdisp = multires_quad_tot[mmd->lvl - 1];
419 for(i = 0; i < me->totface; ++i) {
420 float (*disps)[3] = MEM_callocN(sizeof(float) * 3 * totdisp, "multires del disps");
422 for(j = 0, y = 0; y < st; y += skip) {
423 for(x = 0; x < st; x += skip) {
424 VecCopyf(disps[j], mdisps[i].disps[y * st + x]);
429 MEM_freeN(mdisps[i].disps);
430 mdisps[i].disps = disps;
431 mdisps[i].totdisp = totdisp;
435 mmd->totlvl = mmd->lvl;
438 void multiresModifier_subdivide(MultiresModifierData *mmd, Object *ob, int distance, int updateblock, int simple)
440 DerivedMesh *final = NULL;
441 int totsubvert = 0, totsubface = 0, totsubedge = 0;
442 Mesh *me = get_mesh(ob);
449 if(mmd->totlvl > multires_max_levels)
450 mmd->totlvl = multires_max_levels;
451 if(mmd->lvl > multires_max_levels)
452 mmd->lvl = multires_max_levels;
454 multires_force_update(ob);
456 mmd->lvl = mmd->totlvl;
457 mmd->totlvl += distance;
459 mdisps = CustomData_get_layer(&me->fdata, CD_MDISPS);
461 mdisps = CustomData_add_layer(&me->fdata, CD_MDISPS, CD_DEFAULT, NULL, me->totface);
463 if(mdisps->disps && !updateblock && mmd->totlvl > 2) {
464 DerivedMesh *orig, *mrdm;
465 MultiresModifierData mmd_sub;
467 orig = CDDM_from_mesh(me, NULL);
468 mmd_sub.lvl = mmd_sub.totlvl = mmd->lvl;
469 mrdm = multires_dm_create_from_derived(&mmd_sub, orig, me, 0, 0);
470 totsubvert = mrdm->getNumVerts(mrdm);
471 totsubedge = mrdm->getNumEdges(mrdm);
472 totsubface = mrdm->getNumFaces(mrdm);
476 final = multires_subdisp_pre(mrdm, distance, simple);
481 for(i = 0; i < me->totface; ++i) {
482 const int totdisp = multires_quad_tot[mmd->totlvl - 1];
483 float (*disps)[3] = MEM_callocN(sizeof(float) * 3 * totdisp, "multires disps");
486 MEM_freeN(mdisps[i].disps);
488 mdisps[i].disps = disps;
489 mdisps[i].totdisp = totdisp;
496 orig = CDDM_from_mesh(me, NULL);
498 multires_subdisp(orig, me, final, mmd->lvl, mmd->totlvl, totsubvert, totsubedge, totsubface, 0);
504 mmd->lvl = mmd->totlvl;
507 typedef struct DisplacerEdges {
508 /* DerivedMesh index at the start of each edge (using face x/y directions to define the start) */
510 /* 1 if edge moves in the positive x or y direction, -1 otherwise */
514 typedef struct DisplacerSpill {
515 /* Index of face (in base mesh), -1 for none */
519 /* 1 = Negative variable axis */
520 /* 2 = Near fixed axis */
524 /* Neighboring edges */
525 DisplacerEdges edges;
528 typedef struct MultiresDisplacer {
533 int dm_first_base_vert_index;
536 int sidetot, interior_st, disp_st;
541 int subco_index, face_index;
544 /* Valence for each corner */
547 /* Neighboring edges for current face */
548 DisplacerEdges edges_primary;
550 /* Neighboring faces */
551 DisplacerSpill spill_x, spill_y;
558 static int mface_v(MFace *f, int v)
560 return v == 0 ? f->v1 : v == 1 ? f->v2 : v == 2 ? f->v3 : v == 3 ? f->v4 : -1;
563 /* Get the edges (and their directions) */
564 static void find_displacer_edges(MultiresDisplacer *d, DerivedMesh *dm, DisplacerEdges *de, MFace *f)
566 ListBase *emap = MultiresDM_get_vert_edge_map(dm);
568 int i, end = f->v4 ? 4 : 3;
569 int offset = dm->getNumVerts(dm) - d->me->totvert - d->me->totedge * d->interior_st;
571 for(i = 0; i < end; ++i) {
572 int vcur = mface_v(f, i);
573 int vnext = mface_v(f, i == end - 1 ? 0 : i + 1);
577 for(n = emap[vcur].first; n; n = n->next) {
578 MEdge *e = &d->me->medge[n->index];
580 if(e->v1 == vnext || e->v2 == vnext) {
581 de->base[i] = n->index * d->interior_st;
582 if(((i == 0 || i == 1) && e->v1 == vnext) ||
583 ((i == 2 || i == 3) && e->v2 == vnext)) {
585 de->base[i] += d->interior_st - 1;
587 de->base[i] += offset;
594 /* Returns in out the corners [0-3] that use v1 and v2 */
595 void find_face_corners(MFace *f, int v1, int v2, int out[2])
597 int i, end = f->v4 ? 4 : 3;
599 for(i = 0; i < end; ++i) {
600 int corner = mface_v(f, i);
608 static void multires_displacer_get_spill_faces(MultiresDisplacer *d, DerivedMesh *dm, MFace *mface)
610 ListBase *map = MultiresDM_get_vert_face_map(dm);
612 int v4 = d->face->v4 ? d->face->v4 : d->face->v1;
615 memset(&d->spill_x, 0, sizeof(DisplacerSpill));
616 memset(&d->spill_y, 0, sizeof(DisplacerSpill));
617 d->spill_x.face = d->spill_y.face = -1;
619 for(n1 = map[d->face->v3].first; n1; n1 = n1->next) {
620 if(n1->index == d->face_index)
623 for(n2 = map[d->face->v2].first; n2; n2 = n2->next) {
624 if(n1->index == n2->index)
625 d->spill_x.face = n1->index;
627 for(n2 = map[v4].first; n2; n2 = n2->next) {
628 if(n1->index == n2->index)
629 d->spill_y.face = n1->index;
633 if(d->spill_x.face != -1) {
634 /* Neighbor of v2/v3 found, find flip and orientation */
635 find_face_corners(&mface[d->spill_x.face], d->face->v2, d->face->v3, crn);
636 lv = mface[d->spill_x.face].v4 ? 3 : 2;
638 if(crn[0] == 0 && crn[1] == lv)
639 d->spill_x.f = 0+2+0;
640 else if(crn[0] == lv && crn[1] == 0)
641 d->spill_x.f = 1+2+0;
642 else if(crn[0] == 1 && crn[1] == 0)
643 d->spill_x.f = 1+2+4;
644 else if(crn[0] == 0 && crn[1] == 1)
645 d->spill_x.f = 0+2+4;
646 else if(crn[0] == 2 && crn[1] == 1)
647 d->spill_x.f = 1+0+0;
648 else if(crn[0] == 1 && crn[1] == 2)
649 d->spill_x.f = 0+0+0;
650 else if(crn[0] == 3 && crn[1] == 2)
651 d->spill_x.f = 0+0+4;
652 else if(crn[0] == 2 && crn[1] == 3)
653 d->spill_x.f = 1+0+4;
655 find_displacer_edges(d, dm, &d->spill_x.edges, &mface[d->spill_x.face]);
658 if(d->spill_y.face != -1) {
659 /* Neighbor of v3/v4 found, find flip and orientation */
660 find_face_corners(&mface[d->spill_y.face], d->face->v3, v4, crn);
661 lv = mface[d->spill_y.face].v4 ? 3 : 2;
663 if(crn[0] == 1 && crn[1] == 0)
664 d->spill_y.f = 1+2+0;
665 else if(crn[0] == 0 && crn[1] == 1)
666 d->spill_y.f = 0+2+0;
667 else if(crn[0] == 2 && crn[1] == 1)
668 d->spill_y.f = 1+0+4;
669 else if(crn[0] == 1 && crn[1] == 2)
670 d->spill_y.f = 0+0+4;
671 else if(crn[0] == 3 && crn[1] == 2)
672 d->spill_y.f = 0+0+0;
673 else if(crn[0] == 2 && crn[1] == 3)
674 d->spill_y.f = 1+0+0;
675 else if(crn[0] == 0 && crn[1] == lv)
676 d->spill_y.f = 0+2+4;
677 else if(crn[0] == lv && crn[1] == 0)
678 d->spill_y.f = 1+2+4;
680 find_displacer_edges(d, dm, &d->spill_y.edges, &mface[d->spill_y.face]);
684 static void find_corner_valences(MultiresDisplacer *d, DerivedMesh *dm)
690 /* Set the vertex valence for the corners */
691 for(i = 0; i < (d->face->v4 ? 4 : 3); ++i)
692 d->valence[i] = BLI_countlist(&MultiresDM_get_vert_edge_map(dm)[mface_v(d->face, i)]);
695 static void multires_displacer_init(MultiresDisplacer *d, DerivedMesh *dm,
696 const int face_index, const int invert)
698 Mesh *me = MultiresDM_get_mesh(dm);
701 d->face = me->mface + face_index;
702 d->face_index = face_index;
703 d->face_offsets = MultiresDM_get_face_offsets(dm);
704 /* Get the multires grid from customdata */
705 d->grid = CustomData_get_layer(&me->fdata, CD_MDISPS);
707 d->grid += face_index;
709 d->spacing = pow(2, MultiresDM_get_totlvl(dm) - MultiresDM_get_lvl(dm));
710 d->sidetot = multires_side_tot[MultiresDM_get_lvl(dm) - 1];
711 d->interior_st = d->sidetot - 2;
712 d->disp_st = multires_side_tot[MultiresDM_get_totlvl(dm) - 1];
715 multires_displacer_get_spill_faces(d, dm, me->mface);
716 find_displacer_edges(d, dm, &d->edges_primary, d->face);
717 find_corner_valences(d, dm);
719 d->dm_first_base_vert_index = dm->getNumVerts(dm) - me->totvert;
722 static void multires_displacer_weight(MultiresDisplacer *d, const float w)
727 static void multires_displacer_anchor(MultiresDisplacer *d, const int type, const int side_index)
729 d->sidendx = side_index;
730 d->x = d->y = d->sidetot / 2;
736 else if(side_index == 1)
738 else if(side_index == 2)
740 else if(side_index == 3)
744 if(side_index == 0) {
748 else if(side_index == 1) {
752 else if(side_index == 2) {
756 else if(side_index == 3) {
766 static void multires_displacer_anchor_edge(MultiresDisplacer *d, int v1, int v2, int x)
770 if(v1 == d->face->v1) {
773 if(v2 == d->face->v2)
775 else if(v2 == d->face->v3) {
776 if(x < d->sidetot / 2)
780 d->y = d->sidetot - 1;
786 else if(v1 == d->face->v2) {
787 d->x = d->sidetot - 1;
789 if(v2 == d->face->v1)
794 else if(v1 == d->face->v3) {
795 d->x = d->sidetot - 1;
796 d->y = d->sidetot - 1;
797 if(v2 == d->face->v2)
799 else if(v2 == d->face->v1) {
800 if(x < d->sidetot / 2)
810 else if(v1 == d->face->v4) {
812 d->y = d->sidetot - 1;
813 if(v2 == d->face->v3)
820 static void multires_displacer_anchor_vert(MultiresDisplacer *d, const int v)
822 const int e = d->sidetot - 1;
829 else if(v == d->face->v3)
831 else if(v == d->face->v4)
835 static void multires_displacer_jump(MultiresDisplacer *d)
837 if(d->sidendx == 0) {
841 else if(d->sidendx == 1) {
845 else if(d->sidendx == 2) {
849 else if(d->sidendx == 3) {
855 /* Treating v1 as (0,0) and v3 as (st-1,st-1),
856 returns the index of the vertex at (x,y).
857 If x or y is >= st, wraps over to the adjacent face,
858 or if there is no adjacent face, returns -2. */
859 static int multires_index_at_loc(int face_index, int x, int y, MultiresDisplacer *d, DisplacerEdges *de)
861 int coord_edge = d->sidetot - 1; /* Max value of x/y at edge of grid */
862 int mid = d->sidetot / 2;
864 int qtot = lim * lim;
865 int base = d->face_offsets[face_index];
868 if(x == d->sidetot || y == d->sidetot) {
869 int flags, v_axis, f_axis, lx, ly;
871 if(x == d->sidetot && d->spill_x.face != -1) {
872 flags = d->spill_x.f;
874 /* Handle triangle seam between v1 and v3 */
875 if(!d->me->mface[d->spill_x.face].v4 &&
876 ((flags == 2 && y >= mid) || (flags == 3 && y < mid)))
879 v_axis = (flags & 1) ? d->sidetot - 1 - y : y;
880 f_axis = (flags & 2) ? 1 : d->sidetot - 2;
881 lx = f_axis, ly = v_axis;
888 return multires_index_at_loc(d->spill_x.face, lx, ly, d, &d->spill_x.edges);
890 else if(y == d->sidetot && d->spill_y.face != -1) {
891 flags = d->spill_y.f;
893 /* Handle triangle seam between v1 and v3 */
894 if(!d->me->mface[d->spill_y.face].v4 &&
895 ((flags == 6 && x >= mid) || (flags == 7 && x < mid)))
898 v_axis = (flags & 1) ? x : d->sidetot - 1 - x;
899 f_axis = (flags & 2) ? 1 : d->sidetot - 2;
900 lx = v_axis, ly = f_axis;
907 return multires_index_at_loc(d->spill_y.face, lx, ly, d, &d->spill_y.edges);
913 else if(x == 0 && y == 0)
914 return d->dm_first_base_vert_index + d->face->v1;
915 else if(x == coord_edge && y == 0)
916 return d->dm_first_base_vert_index + d->face->v2;
917 else if(x == coord_edge && y == coord_edge)
918 return d->dm_first_base_vert_index + d->face->v3;
919 else if(x == 0 && y == coord_edge)
920 return d->dm_first_base_vert_index + d->face->v4;
924 return de->base[3] + de->dir[3] * (y - 1);
926 return de->base[2] + de->dir[2] * (y - 1);
929 return de->base[0] + de->dir[0] * (x - 1);
930 else if(x == d->sidetot - 1)
931 return de->base[1] + de->dir[1] * (y - 1);
932 else if(y == d->sidetot - 1)
933 return de->base[2] + de->dir[2] * (x - 1);
935 else if(x == mid && y == mid)
938 else if(x == mid && y < mid)
939 return base + (mid - y);
940 else if(y == mid && x > mid)
941 return base + lim + (x - mid);
942 else if(x == mid && y > mid)
943 return base + lim*2 + (y - mid);
944 else if(y == mid && x < mid) {
946 return base + lim*3 + (mid - x);
948 return base + lim*2 + (mid - x);
952 int offset = base + lim * (d->face->v4 ? 4 : 3);
953 if(x < mid && y < mid)
954 return offset + ((mid - x - 1)*lim + (mid - y));
955 else if(x > mid && y < mid)
956 return offset + qtot + ((mid - y - 1)*lim + (x - mid));
957 else if(x > mid && y > mid)
958 return offset + qtot*2 + ((x - mid - 1)*lim + (y - mid));
959 else if(x < mid && y > mid)
960 return offset + qtot*3 + ((y - mid - 1)*lim + (mid - x));
966 /* Calculate the TS matrix used for applying displacements.
967 Uses the undisplaced subdivided mesh's curvature to find a
968 smoothly normal and tangents. */
969 static void calc_disp_mat(MultiresDisplacer *d, float mat[3][3])
971 int u = multires_index_at_loc(d->face_index, d->x + 1, d->y, d, &d->edges_primary);
972 int v = multires_index_at_loc(d->face_index, d->x, d->y + 1, d, &d->edges_primary);
973 float norm[3], t1[3], t2[3], inv[3][3];
974 MVert *base = d->subco + d->subco_index;
976 //printf("f=%d, x=%d, y=%d, i=%d, u=%d, v=%d ", d->face_index, d->x, d->y, d->subco_index, u, v);
978 norm[0] = base->no[0] / 32767.0f;
979 norm[1] = base->no[1] / 32767.0f;
980 norm[2] = base->no[2] / 32767.0f;
982 /* Special handling for vertices of valence 3 */
983 if(d->valence[1] == 3 && d->x == d->sidetot - 1 && d->y == 0)
985 else if(d->valence[2] == 3 && d->x == d->sidetot - 1 && d->y == d->sidetot - 1)
987 else if(d->valence[3] == 3 && d->x == 0 && d->y == d->sidetot - 1)
990 /* If either u or v is -2, it's on a boundary. In this
991 case, back up by one row/column and use the same
992 vector as the preceeding sub-edge. */
995 u = multires_index_at_loc(d->face_index, d->x - 1, d->y, d, &d->edges_primary);
996 VecSubf(t1, base->co, d->subco[u].co);
999 VecSubf(t1, d->subco[u].co, base->co);
1002 v = multires_index_at_loc(d->face_index, d->x, d->y - 1, d, &d->edges_primary);
1003 VecSubf(t2, base->co, d->subco[v].co);
1006 VecSubf(t2, d->subco[v].co, base->co);
1008 //printf("uu=%d, vv=%d\n", u, v);
1012 Mat3FromColVecs(mat, t1, t2, norm);
1016 Mat3CpyMat3(mat, inv);
1020 static void multires_displace(MultiresDisplacer *d, float co[3])
1022 float disp[3], mat[3][3];
1024 MVert *subco = &d->subco[d->subco_index];
1026 if(!d->grid || !d->grid->disps) return;
1028 data = d->grid->disps[(d->y * d->spacing) * d->disp_st + (d->x * d->spacing)];
1031 VecSubf(disp, co, subco->co);
1033 VecCopyf(disp, data);
1036 /* Apply ts matrix to displacement */
1037 calc_disp_mat(d, mat);
1038 Mat3MulVecfl(mat, disp);
1041 VecCopyf(data, disp);
1045 if(d->type == 4 || d->type == 5)
1046 VecMulf(disp, d->weight);
1047 VecAddf(co, co, disp);
1053 else if(d->sidendx == 1)
1055 else if(d->sidendx == 2)
1057 else if(d->sidendx == 3)
1060 else if(d->type == 3) {
1063 else if(d->sidendx == 1)
1065 else if(d->sidendx == 2)
1067 else if(d->sidendx == 3)
1072 static void multiresModifier_disp_run(DerivedMesh *dm, MVert *subco, int invert)
1074 const int lvl = MultiresDM_get_lvl(dm);
1075 const int gridFaces = multires_side_tot[lvl - 2] - 1;
1076 const int edgeSize = multires_side_tot[lvl - 1] - 1;
1077 MVert *mvert = CDDM_get_verts(dm);
1078 MEdge *medge = MultiresDM_get_mesh(dm)->medge;
1079 MFace *mface = MultiresDM_get_mesh(dm)->mface;
1080 ListBase *map = MultiresDM_get_vert_face_map(dm);
1081 Mesh *me = MultiresDM_get_mesh(dm);
1082 MultiresDisplacer d;
1088 for(i = 0; i < me->totface; ++i) {
1089 const int numVerts = mface[i].v4 ? 4 : 3;
1092 multires_displacer_init(&d, dm, i, invert);
1093 multires_displacer_anchor(&d, 1, 0);
1094 multires_displace(&d, mvert->co);
1099 for(S = 0; S < numVerts; ++S) {
1100 multires_displacer_anchor(&d, 2, S);
1101 for(x = 1; x < gridFaces; ++x) {
1102 multires_displace(&d, mvert->co);
1109 for(S = 0; S < numVerts; S++) {
1110 multires_displacer_anchor(&d, 3, S);
1111 for(y = 1; y < gridFaces; y++) {
1112 for(x = 1; x < gridFaces; x++) {
1113 multires_displace(&d, mvert->co);
1117 multires_displacer_jump(&d);
1122 for(i = 0; i < me->totedge; ++i) {
1123 const MEdge *e = &medge[i];
1124 for(x = 1; x < edgeSize; ++x) {
1127 for(n1 = map[e->v1].first; n1; n1 = n1->next) {
1128 for(n2 = map[e->v2].first; n2; n2 = n2->next) {
1129 if(n1->index == n2->index)
1133 multires_displacer_weight(&d, 1.0f / numFaces);
1134 /* TODO: Better to have these loops outside the x loop */
1135 for(n1 = map[e->v1].first; n1; n1 = n1->next) {
1136 for(n2 = map[e->v2].first; n2; n2 = n2->next) {
1137 if(n1->index == n2->index) {
1138 multires_displacer_init(&d, dm, n1->index, invert);
1139 multires_displacer_anchor_edge(&d, e->v1, e->v2, x);
1140 multires_displace(&d, mvert->co);
1149 for(i = 0; i < me->totvert; ++i) {
1151 multires_displacer_weight(&d, 1.0f / BLI_countlist(&map[i]));
1152 for(n = map[i].first; n; n = n->next) {
1153 multires_displacer_init(&d, dm, n->index, invert);
1154 multires_displacer_anchor_vert(&d, i);
1155 multires_displace(&d, mvert->co);
1162 CDDM_calc_normals(dm);
1165 static void multiresModifier_update(DerivedMesh *dm)
1170 me = MultiresDM_get_mesh(dm);
1171 mdisps = CustomData_get_layer(&me->fdata, CD_MDISPS);
1174 const int lvl = MultiresDM_get_lvl(dm);
1175 const int totlvl = MultiresDM_get_totlvl(dm);
1178 /* Propagate disps upwards */
1179 DerivedMesh *final, *subco_dm, *orig;
1180 MVert *verts_new = NULL, *cur_lvl_orig_verts = NULL;
1181 MultiresModifierData mmd;
1184 orig = CDDM_from_mesh(me, NULL);
1186 /* Regenerate the current level's vertex coordinates
1187 (includes older displacements but not new sculpts) */
1188 mmd.totlvl = totlvl;
1190 subco_dm = multires_dm_create_from_derived(&mmd, orig, me, 0, 0);
1191 cur_lvl_orig_verts = CDDM_get_verts(subco_dm);
1193 /* Subtract the original vertex cos from the new vertex cos */
1194 verts_new = CDDM_get_verts(dm);
1195 for(i = 0; i < dm->getNumVerts(dm); ++i)
1196 VecSubf(verts_new[i].co, verts_new[i].co, cur_lvl_orig_verts[i].co);
1198 final = multires_subdisp_pre(dm, totlvl - lvl, 0);
1200 multires_subdisp(orig, me, final, lvl, totlvl, dm->getNumVerts(dm), dm->getNumEdges(dm),
1201 dm->getNumFaces(dm), 1);
1203 subco_dm->release(subco_dm);
1204 orig->release(orig);
1207 multiresModifier_disp_run(dm, MultiresDM_get_subco(dm), 1);
1211 void multires_mark_as_modified(struct Object *ob)
1213 if(ob && ob->derivedFinal) {
1214 MultiresDM_mark_as_modified(ob->derivedFinal);
1218 void multires_force_update(Object *ob)
1220 if(ob && ob->derivedFinal) {
1221 ob->derivedFinal->needsFree =1;
1222 ob->derivedFinal->release(ob->derivedFinal);
1223 ob->derivedFinal = NULL;
1227 struct DerivedMesh *multires_dm_create_from_derived(MultiresModifierData *mmd, DerivedMesh *dm, Mesh *me,
1228 int useRenderParams, int isFinalCalc)
1230 SubsurfModifierData smd;
1232 DerivedMesh *result;
1238 memset(&smd, 0, sizeof(SubsurfModifierData));
1239 smd.levels = smd.renderLevels = mmd->lvl - 1;
1240 smd.flags |= eSubsurfModifierFlag_SubsurfUv;
1242 result = subsurf_make_derived_from_derived_with_multires(dm, &smd, &ms, useRenderParams, NULL, isFinalCalc, 0);
1243 for(i = 0; i < result->getNumVerts(result); ++i)
1244 MultiresDM_get_subco(result)[i] = CDDM_get_verts(result)[i];
1245 multiresModifier_disp_run(result, MultiresDM_get_subco(result), 0);
1246 MultiresDM_set_update(result, multiresModifier_update);
1251 /**** Old Multires code ****
1252 ***************************/
1254 /* Does not actually free lvl itself */
1255 void multires_free_level(MultiresLevel *lvl)
1258 if(lvl->faces) MEM_freeN(lvl->faces);
1259 if(lvl->edges) MEM_freeN(lvl->edges);
1260 if(lvl->colfaces) MEM_freeN(lvl->colfaces);
1264 void multires_free(Multires *mr)
1267 MultiresLevel* lvl= mr->levels.first;
1269 /* Free the first-level data */
1271 CustomData_free(&mr->vdata, lvl->totvert);
1272 CustomData_free(&mr->fdata, lvl->totface);
1274 MEM_freeN(mr->edge_flags);
1275 if(mr->edge_creases)
1276 MEM_freeN(mr->edge_creases);
1280 multires_free_level(lvl);
1284 MEM_freeN(mr->verts);
1286 BLI_freelistN(&mr->levels);
1292 static void create_old_vert_face_map(ListBase **map, IndexNode **mem, const MultiresFace *mface,
1293 const int totvert, const int totface)
1296 IndexNode *node = NULL;
1298 (*map) = MEM_callocN(sizeof(ListBase) * totvert, "vert face map");
1299 (*mem) = MEM_callocN(sizeof(IndexNode) * totface*4, "vert face map mem");
1302 /* Find the users */
1303 for(i = 0; i < totface; ++i){
1304 for(j = 0; j < (mface[i].v[3]?4:3); ++j, ++node) {
1306 BLI_addtail(&(*map)[mface[i].v[j]], node);
1311 static void create_old_vert_edge_map(ListBase **map, IndexNode **mem, const MultiresEdge *medge,
1312 const int totvert, const int totedge)
1315 IndexNode *node = NULL;
1317 (*map) = MEM_callocN(sizeof(ListBase) * totvert, "vert edge map");
1318 (*mem) = MEM_callocN(sizeof(IndexNode) * totedge*2, "vert edge map mem");
1321 /* Find the users */
1322 for(i = 0; i < totedge; ++i){
1323 for(j = 0; j < 2; ++j, ++node) {
1325 BLI_addtail(&(*map)[medge[i].v[j]], node);
1330 static MultiresFace *find_old_face(ListBase *map, MultiresFace *faces, int v1, int v2, int v3, int v4)
1333 int v[4] = {v1, v2, v3, v4}, i, j;
1335 for(n1 = map[v1].first; n1; n1 = n1->next) {
1336 int fnd[4] = {0, 0, 0, 0};
1338 for(i = 0; i < 4; ++i) {
1339 for(j = 0; j < 4; ++j) {
1340 if(v[i] == faces[n1->index].v[j])
1345 if(fnd[0] && fnd[1] && fnd[2] && fnd[3])
1346 return &faces[n1->index];
1352 static MultiresEdge *find_old_edge(ListBase *map, MultiresEdge *edges, int v1, int v2)
1356 for(n1 = map[v1].first; n1; n1 = n1->next) {
1357 for(n2 = map[v2].first; n2; n2 = n2->next) {
1358 if(n1->index == n2->index)
1359 return &edges[n1->index];
1366 static void multires_load_old_edges(ListBase **emap, MultiresLevel *lvl, int *vvmap, int dst, int v1, int v2, int mov)
1368 int emid = find_old_edge(emap[2], lvl->edges, v1, v2)->mid;
1369 vvmap[dst + mov] = emid;
1371 if(lvl->next->next) {
1372 multires_load_old_edges(emap + 1, lvl->next, vvmap, dst + mov, v1, emid, mov / 2);
1373 multires_load_old_edges(emap + 1, lvl->next, vvmap, dst + mov, v2, emid, -mov / 2);
1377 static void multires_load_old_faces(ListBase **fmap, ListBase **emap, MultiresLevel *lvl, int *vvmap, int dst,
1378 int v1, int v2, int v3, int v4, int st2, int st3)
1381 int emid13, emid14, emid23, emid24;
1383 if(lvl && lvl->next) {
1384 fmid = find_old_face(fmap[1], lvl->faces, v1, v2, v3, v4)->mid;
1387 emid13 = find_old_edge(emap[1], lvl->edges, v1, v3)->mid;
1388 emid14 = find_old_edge(emap[1], lvl->edges, v1, v4)->mid;
1389 emid23 = find_old_edge(emap[1], lvl->edges, v2, v3)->mid;
1390 emid24 = find_old_edge(emap[1], lvl->edges, v2, v4)->mid;
1393 multires_load_old_faces(fmap + 1, emap + 1, lvl->next, vvmap, dst + st2 * st3 + st3,
1394 fmid, v2, emid23, emid24, st2, st3 / 2);
1396 multires_load_old_faces(fmap + 1, emap + 1, lvl->next, vvmap, dst - st2 * st3 + st3,
1397 emid14, emid24, fmid, v4, st2, st3 / 2);
1399 multires_load_old_faces(fmap + 1, emap + 1, lvl->next, vvmap, dst + st2 * st3 - st3,
1400 emid13, emid23, v3, fmid, st2, st3 / 2);
1402 multires_load_old_faces(fmap + 1, emap + 1, lvl->next, vvmap, dst - st2 * st3 - st3,
1403 v1, fmid, emid13, emid14, st2, st3 / 2);
1405 if(lvl->next->next) {
1406 multires_load_old_edges(emap, lvl->next, vvmap, dst, emid24, fmid, st3);
1407 multires_load_old_edges(emap, lvl->next, vvmap, dst, emid13, fmid, -st3);
1408 multires_load_old_edges(emap, lvl->next, vvmap, dst, emid14, fmid, -st2 * st3);
1409 multires_load_old_edges(emap, lvl->next, vvmap, dst, emid23, fmid, st2 * st3);
1414 /* Loads a multires object stored in the old Multires struct into the new format */
1415 void multires_load_old(DerivedMesh *dm, Multires *mr)
1417 MultiresLevel *lvl, *lvl1;
1420 int totlvl = MultiresDM_get_totlvl(dm);
1421 int st = multires_side_tot[totlvl - 2] - 1;
1422 int extedgelen = multires_side_tot[totlvl - 1] - 2;
1423 int *vvmap; // inorder for dst, map to src
1425 int i, j, s, x, totvert, tottri, totquad;
1430 vdst = CDDM_get_verts(dm);
1431 totvert = dm->getNumVerts(dm);
1432 vvmap = MEM_callocN(sizeof(int) * totvert, "multires vvmap");
1434 lvl1 = mr->levels.first;
1435 /* Load base verts */
1436 for(i = 0; i < lvl1->totvert; ++i) {
1437 vvmap[totvert - lvl1->totvert + i] = src;
1441 /* Original edges */
1442 dst = totvert - lvl1->totvert - extedgelen * lvl1->totedge;
1443 for(i = 0; i < lvl1->totedge; ++i) {
1444 int ldst = dst + extedgelen * i;
1448 for(j = 2; j <= mr->level_count; ++j) {
1449 int base = multires_side_tot[totlvl - j] - 2;
1450 int skip = multires_side_tot[totlvl - j + 1] - 1;
1451 int st = multires_side_tot[j - 2] - 1;
1453 for(x = 0; x < st; ++x)
1454 vvmap[ldst + base + x * skip] = lsrc + st * i + x;
1456 lsrc += lvl->totvert - lvl->prev->totvert;
1463 for(i = 0; i < lvl1->totface; ++i) {
1464 int sides = lvl1->faces[i].v[3] ? 4 : 3;
1466 vvmap[dst] = src + lvl1->totedge + i;
1467 dst += 1 + sides * (st - 1) * st;
1471 /* The rest is only for level 3 and up */
1472 if(lvl1->next && lvl1->next->next) {
1473 ListBase **fmap, **emap;
1474 IndexNode **fmem, **emem;
1476 /* Face edge cross */
1477 tottri = totquad = 0;
1478 crossedgelen = multires_side_tot[totlvl - 2] - 2;
1480 for(i = 0; i < lvl1->totface; ++i) {
1481 int sides = lvl1->faces[i].v[3] ? 4 : 3;
1483 lvl = lvl1->next->next;
1486 for(j = 3; j <= mr->level_count; ++j) {
1487 int base = multires_side_tot[totlvl - j] - 2;
1488 int skip = multires_side_tot[totlvl - j + 1] - 1;
1489 int st = pow(2, j - 2);
1490 int st2 = pow(2, j - 3);
1491 int lsrc = lvl->prev->totvert;
1493 /* Skip exterior edge verts */
1494 lsrc += lvl1->totedge * st;
1496 /* Skip earlier face edge crosses */
1497 lsrc += st2 * (tottri * 3 + totquad * 4);
1499 for(s = 0; s < sides; ++s) {
1500 for(x = 0; x < st2; ++x) {
1501 vvmap[dst + crossedgelen * (s + 1) - base - x * skip - 1] = lsrc;
1509 dst += sides * (st - 1) * st;
1511 if(sides == 4) ++totquad;
1516 /* calculate vert to edge/face maps for each level (except the last) */
1517 fmap = MEM_callocN(sizeof(ListBase*) * (mr->level_count-1), "multires fmap");
1518 emap = MEM_callocN(sizeof(ListBase*) * (mr->level_count-1), "multires emap");
1519 fmem = MEM_callocN(sizeof(IndexNode*) * (mr->level_count-1), "multires fmem");
1520 emem = MEM_callocN(sizeof(IndexNode*) * (mr->level_count-1), "multires emem");
1522 for(i = 0; i < mr->level_count - 1; ++i) {
1523 create_old_vert_face_map(fmap + i, fmem + i, lvl->faces, lvl->totvert, lvl->totface);
1524 create_old_vert_edge_map(emap + i, emem + i, lvl->edges, lvl->totvert, lvl->totedge);
1528 /* Interior face verts */
1529 lvl = lvl1->next->next;
1531 for(j = 0; j < lvl1->totface; ++j) {
1532 int sides = lvl1->faces[j].v[3] ? 4 : 3;
1533 int ldst = dst + 1 + sides * (st - 1);
1535 for(s = 0; s < sides; ++s) {
1536 int st2 = multires_side_tot[totlvl - 2] - 2;
1537 int st3 = multires_side_tot[totlvl - 3] - 2;
1538 int st4 = st3 == 0 ? 1 : (st3 + 1) / 2;
1539 int mid = ldst + st2 * st3 + st3;
1540 int cv = lvl1->faces[j].v[s];
1541 int nv = lvl1->faces[j].v[s == sides - 1 ? 0 : s + 1];
1542 int pv = lvl1->faces[j].v[s == 0 ? sides - 1 : s - 1];
1544 multires_load_old_faces(fmap, emap, lvl1->next, vvmap, mid,
1546 find_old_edge(emap[0], lvl1->edges, pv, cv)->mid,
1547 find_old_edge(emap[0], lvl1->edges, cv, nv)->mid,
1550 ldst += (st - 1) * (st - 1);
1559 for(i = 0; i < mr->level_count - 1; ++i) {
1572 /* Transfer verts */
1573 for(i = 0; i < totvert; ++i)
1574 VecCopyf(vdst[i].co, vsrc[vvmap[i]].co);