472836e371688be7e2fc9af930c4e87c9cf6ef6e
[blender.git] / source / blender / modifiers / intern / MOD_multires.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software  Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2005 by the Blender Foundation.
19  * All rights reserved.
20  *
21  * Contributor(s): Daniel Dunbar
22  *                 Ton Roosendaal,
23  *                 Ben Batt,
24  *                 Brecht Van Lommel,
25  *                 Campbell Barton
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  *
29  */
30
31 /** \file blender/modifiers/intern/MOD_multires.c
32  *  \ingroup modifiers
33  */
34
35
36 #include <stddef.h>
37
38 #include "DNA_mesh_types.h"
39 #include "DNA_object_types.h"
40
41 #include "BKE_cdderivedmesh.h"
42 #include "BKE_mesh.h"
43 #include "BKE_multires.h"
44 #include "BKE_modifier.h"
45 #include "BKE_paint.h"
46 #include "BKE_subsurf.h"
47
48 #include "MOD_util.h"
49
50 static void initData(ModifierData *md)
51 {
52         MultiresModifierData *mmd = (MultiresModifierData *)md;
53
54         mmd->lvl = 0;
55         mmd->sculptlvl = 0;
56         mmd->renderlvl = 0;
57         mmd->totlvl = 0;
58 }
59
60 static void copyData(ModifierData *md, ModifierData *target)
61 {
62         MultiresModifierData *mmd = (MultiresModifierData *) md;
63         MultiresModifierData *tmmd = (MultiresModifierData *) target;
64
65         tmmd->lvl = mmd->lvl;
66         tmmd->sculptlvl = mmd->sculptlvl;
67         tmmd->renderlvl = mmd->renderlvl;
68         tmmd->totlvl = mmd->totlvl;
69         tmmd->simple = mmd->simple;
70         tmmd->flags = mmd->flags;
71 }
72
73 static DerivedMesh *applyModifier(ModifierData *md, Object *ob, DerivedMesh *dm,
74                                   ModifierApplyFlag flag)
75 {
76         MultiresModifierData *mmd = (MultiresModifierData *)md;
77         DerivedMesh *result;
78         Mesh *me = (Mesh *)ob->data;
79         const int useRenderParams = flag & MOD_APPLY_RENDER;
80         MultiresFlags flags = 0;
81
82         if (mmd->totlvl) {
83                 if (!CustomData_get_layer(&me->ldata, CD_MDISPS)) {
84                         /* multires always needs a displacement layer */
85                         CustomData_add_layer(&me->ldata, CD_MDISPS, CD_CALLOC, NULL, me->totloop);
86                 }
87         }
88
89         flags = MULTIRES_ALLOC_PAINT_MASK;
90         if (useRenderParams)
91                 flags |= MULTIRES_USE_RENDER_PARAMS;
92
93         result = multires_make_derived_from_derived(dm, mmd, ob, flags);
94
95         if (result == dm)
96                 return dm;
97
98         if (useRenderParams || !(flag & MOD_APPLY_USECACHE)) {
99                 DerivedMesh *cddm;
100                 
101                 cddm = CDDM_copy(result);
102
103                 /* copy hidden/masks to vertices */
104                 if (!useRenderParams) {
105                         struct MDisps *mdisps;
106                         struct GridPaintMask *grid_paint_mask;
107                         
108                         mdisps = CustomData_get_layer(&me->ldata, CD_MDISPS);
109                         grid_paint_mask = CustomData_get_layer(&me->ldata, CD_GRID_PAINT_MASK);
110                         
111                         if (mdisps) {
112                                 subsurf_copy_grid_hidden(result, me->mpoly,
113                                                          cddm->getVertArray(cddm),
114                                                          mdisps);
115
116                                 BKE_mesh_flush_hidden_from_verts(cddm->getVertArray(cddm),
117                                                                  cddm->getLoopArray(cddm),
118                                                                  cddm->getEdgeArray(cddm),
119                                                                  cddm->getNumEdges(cddm),
120                                                                  cddm->getPolyArray(cddm),
121                                                                  cddm->getNumPolys(cddm));
122                         }
123                         if (grid_paint_mask) {
124                                 float *paint_mask = CustomData_add_layer(&cddm->vertData,
125                                                                          CD_PAINT_MASK,
126                                                                          CD_CALLOC, NULL,
127                                                                          cddm->getNumVerts(cddm));
128
129                                 subsurf_copy_grid_paint_mask(result, me->mpoly,
130                                                              paint_mask, grid_paint_mask);
131                         }
132                 }
133
134                 result->release(result);
135                 result = cddm;
136         }
137
138         return result;
139 }
140
141
142 ModifierTypeInfo modifierType_Multires = {
143         /* name */              "Multires",
144         /* structName */        "MultiresModifierData",
145         /* structSize */        sizeof(MultiresModifierData),
146         /* type */              eModifierTypeType_Constructive,
147         /* flags */             eModifierTypeFlag_AcceptsMesh |
148                                 eModifierTypeFlag_SupportsMapping |
149                                 eModifierTypeFlag_RequiresOriginalData,
150
151         /* copyData */          copyData,
152         /* deformVerts */       NULL,
153         /* deformMatrices */    NULL,
154         /* deformVertsEM */     NULL,
155         /* deformMatricesEM */  NULL,
156         /* applyModifier */     applyModifier,
157         /* applyModifierEM */   NULL,
158         /* initData */          initData,
159         /* requiredDataMask */  NULL,
160         /* freeData */          NULL,
161         /* isDisabled */        NULL,
162         /* updateDepgraph */    NULL,
163         /* dependsOnTime */     NULL,
164         /* dependsOnNormals */  NULL,
165         /* foreachObjectLink */ NULL,
166         /* foreachIDLink */     NULL,
167         /* foreachTexLink */    NULL,
168 };